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
37418ccb3d70af8ff26c57bc3eb116186f2c8b0b
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/apply_tac2.lean
0681a9c9d28d3fb34b97417ef9a9eac13045c4bc
[ "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
91
lean
(* import("tactic.lua") *) theorem T (a b : Bool) : a → b → b → a. exact. done.
2398f4aa381e68152fc46884ab97e20f58950ed4
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/probability_mass_function/basic.lean
b772140c53ceaa2b8aa5198eec14efe7432138e4
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
9,270
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, Devon Tuma -/ import topology.instances.ennreal import measure_theory.measure.measure_space /-! # Probability mass functions This file is about probability mass functions or discrete probability measures: a function `α → ℝ≥0` such that the values have (infinite) sum `1`. Construction of monadic `pure` and `bind` is found in `probability_mass_function/monad.lean`, other constructions of `pmf`s are found in `probability_mass_function/constructions.lean`. Given `p : pmf α`, `pmf.to_outer_measure` constructs an `outer_measure` on `α`, by assigning each set the sum of the probabilities of each of its elements. Under this outer measure, every set is Carathéodory-measurable, so we can further extend this to a `measure` on `α`, see `pmf.to_measure`. `pmf.to_measure.is_probability_measure` shows this associated measure is a probability measure. ## Tags probability mass function, discrete probability measure -/ noncomputable theory variables {α : Type*} {β : Type*} {γ : Type*} open_locale classical big_operators nnreal ennreal /-- A probability mass function, or discrete probability measures is a function `α → ℝ≥0` such that the values have (infinite) sum `1`. -/ def {u} pmf (α : Type u) : Type u := { f : α → ℝ≥0 // has_sum f 1 } namespace pmf instance : has_coe_to_fun (pmf α) (λ p, α → ℝ≥0) := ⟨λ p a, p.1 a⟩ @[ext] protected lemma ext : ∀ {p q : pmf α}, (∀ a, p a = q a) → p = q | ⟨f, hf⟩ ⟨g, hg⟩ eq := subtype.eq $ funext eq lemma has_sum_coe_one (p : pmf α) : has_sum p 1 := p.2 lemma summable_coe (p : pmf α) : summable p := (p.has_sum_coe_one).summable @[simp] lemma tsum_coe (p : pmf α) : ∑' a, p a = 1 := p.has_sum_coe_one.tsum_eq /-- The support of a `pmf` is the set where it is nonzero. -/ def support (p : pmf α) : set α := function.support p @[simp] lemma mem_support_iff (p : pmf α) (a : α) : a ∈ p.support ↔ p a ≠ 0 := iff.rfl lemma apply_eq_zero_iff (p : pmf α) (a : α) : p a = 0 ↔ a ∉ p.support := by rw [mem_support_iff, not_not] lemma coe_le_one (p : pmf α) (a : α) : p a ≤ 1 := has_sum_le (by { intro b, split_ifs; simp only [h, zero_le'] }) (has_sum_ite_eq a (p a)) (has_sum_coe_one p) section outer_measure open measure_theory measure_theory.outer_measure /-- Construct an `outer_measure` from a `pmf`, by assigning measure to each set `s : set α` equal to the sum of `p x` for for each `x ∈ α` -/ def to_outer_measure (p : pmf α) : outer_measure α := outer_measure.sum (λ (x : α), p x • dirac x) variables (p : pmf α) (s t : set α) lemma to_outer_measure_apply : p.to_outer_measure s = ∑' x, s.indicator (coe ∘ p) x := tsum_congr (λ x, smul_dirac_apply (p x) x s) lemma to_outer_measure_apply' : p.to_outer_measure s = ↑(∑' (x : α), s.indicator p x) := by simp only [ennreal.coe_tsum (nnreal.indicator_summable (summable_coe p) s), ennreal.coe_indicator, to_outer_measure_apply] @[simp] lemma to_outer_measure_apply_finset (s : finset α) : p.to_outer_measure s = ∑ x in s, ↑(p x) := begin refine (to_outer_measure_apply p s).trans ((@tsum_eq_sum _ _ _ _ _ _ s _).trans _), { exact λ x hx, set.indicator_of_not_mem hx _ }, { exact finset.sum_congr rfl (λ x hx, set.indicator_of_mem hx _) } end lemma to_outer_measure_apply_eq_zero_iff : p.to_outer_measure s = 0 ↔ disjoint p.support s := begin rw [to_outer_measure_apply', ennreal.coe_eq_zero, tsum_eq_zero_iff (nnreal.indicator_summable (summable_coe p) s)], exact function.funext_iff.symm.trans set.indicator_eq_zero', end lemma to_outer_measure_apply_eq_one_iff : p.to_outer_measure s = 1 ↔ p.support ⊆ s := begin rw [to_outer_measure_apply', ennreal.coe_eq_one], refine ⟨λ h a ha, _, λ h, _⟩, { have hsp : ∀ x, s.indicator p x ≤ p x := λ _, set.indicator_apply_le (λ _, le_rfl), have := λ hpa, ne_of_lt (nnreal.tsum_lt_tsum hsp hpa p.summable_coe) (h.trans p.tsum_coe.symm), exact not_not.1 (λ has, ha $ set.indicator_apply_eq_self.1 (le_antisymm (set.indicator_apply_le $ λ _, le_rfl) $ le_of_not_lt $ this) has) }, { suffices : ∀ x, x ∉ s → p x = 0, from trans (tsum_congr $ λ a, (set.indicator_apply s p a).trans (ite_eq_left_iff.2 $ symm ∘ (this a))) p.tsum_coe, exact λ a ha, (p.apply_eq_zero_iff a).2 $ set.not_mem_subset h ha } end @[simp] lemma to_outer_measure_apply_inter_support : p.to_outer_measure (s ∩ p.support) = p.to_outer_measure s := by simp only [to_outer_measure_apply', ennreal.coe_eq_coe, pmf.support, set.indicator_inter_support] /-- Slightly stronger than `outer_measure.mono` having an intersection with `p.support` -/ lemma to_outer_measure_mono {s t : set α} (h : s ∩ p.support ⊆ t) : p.to_outer_measure s ≤ p.to_outer_measure t := le_trans (le_of_eq (to_outer_measure_apply_inter_support p s).symm) (p.to_outer_measure.mono h) lemma to_outer_measure_apply_eq_of_inter_support_eq {s t : set α} (h : s ∩ p.support = t ∩ p.support) : p.to_outer_measure s = p.to_outer_measure t := le_antisymm (p.to_outer_measure_mono (h.symm ▸ (set.inter_subset_left t p.support))) (p.to_outer_measure_mono (h ▸ (set.inter_subset_left s p.support))) @[simp] lemma to_outer_measure_apply_fintype [fintype α] : p.to_outer_measure s = ↑(∑ x, (s.indicator p x)) := (p.to_outer_measure_apply' s).trans (ennreal.coe_eq_coe.2 $ tsum_eq_sum (λ x h, absurd (finset.mem_univ x) h)) @[simp] lemma to_outer_measure_caratheodory (p : pmf α) : (to_outer_measure p).caratheodory = ⊤ := begin refine (eq_top_iff.2 $ le_trans (le_Inf $ λ x hx, _) (le_sum_caratheodory _)), obtain ⟨y, hy⟩ := hx, exact ((le_of_eq (dirac_caratheodory y).symm).trans (le_smul_caratheodory _ _)).trans (le_of_eq hy), end end outer_measure section measure open measure_theory /-- Since every set is Carathéodory-measurable under `pmf.to_outer_measure`, we can further extend this `outer_measure` to a `measure` on `α` -/ def to_measure [measurable_space α] (p : pmf α) : measure α := p.to_outer_measure.to_measure ((to_outer_measure_caratheodory p).symm ▸ le_top) variables [measurable_space α] (p : pmf α) (s t : set α) lemma to_outer_measure_apply_le_to_measure_apply : p.to_outer_measure s ≤ p.to_measure s := le_to_measure_apply p.to_outer_measure _ s lemma to_measure_apply_eq_to_outer_measure_apply (hs : measurable_set s) : p.to_measure s = p.to_outer_measure s := to_measure_apply p.to_outer_measure _ hs lemma to_measure_apply (hs : measurable_set s) : p.to_measure s = ∑' x, s.indicator (coe ∘ p) x := (p.to_measure_apply_eq_to_outer_measure_apply s hs).trans (p.to_outer_measure_apply s) lemma to_measure_apply' (hs : measurable_set s) : p.to_measure s = ↑(∑' x, s.indicator p x) := (p.to_measure_apply_eq_to_outer_measure_apply s hs).trans (p.to_outer_measure_apply' s) lemma to_measure_apply_eq_one_iff (hs : measurable_set s) : p.to_measure s = 1 ↔ p.support ⊆ s := (p.to_measure_apply_eq_to_outer_measure_apply s hs : p.to_measure s = p.to_outer_measure s).symm ▸ (p.to_outer_measure_apply_eq_one_iff s) @[simp] lemma to_measure_apply_inter_support (hs : measurable_set s) (hp : measurable_set p.support) : p.to_measure (s ∩ p.support) = p.to_measure s := by simp [p.to_measure_apply_eq_to_outer_measure_apply s hs, p.to_measure_apply_eq_to_outer_measure_apply _ (hs.inter hp)] lemma to_measure_mono {s t : set α} (hs : measurable_set s) (ht : measurable_set t) (h : s ∩ p.support ⊆ t) : p.to_measure s ≤ p.to_measure t := by simpa only [p.to_measure_apply_eq_to_outer_measure_apply, hs, ht] using to_outer_measure_mono p h lemma to_measure_apply_eq_of_inter_support_eq {s t : set α} (hs : measurable_set s) (ht : measurable_set t) (h : s ∩ p.support = t ∩ p.support) : p.to_measure s = p.to_measure t := by simpa only [p.to_measure_apply_eq_to_outer_measure_apply, hs, ht] using to_outer_measure_apply_eq_of_inter_support_eq p h section measurable_singleton_class variables [measurable_singleton_class α] @[simp] lemma to_measure_apply_finset (s : finset α) : p.to_measure s = ∑ x in s, (p x : ℝ≥0∞) := (p.to_measure_apply_eq_to_outer_measure_apply s s.measurable_set).trans (p.to_outer_measure_apply_finset s) lemma to_measure_apply_of_finite (hs : s.finite) : p.to_measure s = ↑(∑' x, s.indicator p x) := (p.to_measure_apply_eq_to_outer_measure_apply s hs.measurable_set).trans (p.to_outer_measure_apply' s) @[simp] lemma to_measure_apply_fintype [fintype α] : p.to_measure s = ↑(∑ x, s.indicator p x) := (p.to_measure_apply_eq_to_outer_measure_apply s (set.finite.of_fintype s).measurable_set).trans (p.to_outer_measure_apply_fintype s) end measurable_singleton_class /-- The measure associated to a `pmf` by `to_measure` is a probability measure -/ instance to_measure.is_probability_measure (p : pmf α) : is_probability_measure (p.to_measure) := ⟨by simpa only [measurable_set.univ, to_measure_apply_eq_to_outer_measure_apply, set.indicator_univ, to_outer_measure_apply', ennreal.coe_eq_one] using tsum_coe p⟩ end measure end pmf
2ad350872cc640467c8059bc9f4cedab63ea3afe
9cb9db9d79fad57d80ca53543dc07efb7c4f3838
/src/pseudo_normed_group/FiltrationPow.lean
ad246fbf4bc2dcf68472def3dc692162369de90a
[]
no_license
mr-infty/lean-liquid
3ff89d1f66244b434654c59bdbd6b77cb7de0109
a8db559073d2101173775ccbd85729d3a4f1ed4d
refs/heads/master
1,678,465,145,334
1,614,565,310,000
1,614,565,310,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,951
lean
import pseudo_normed_group.breen_deligne import normed_group.NormedGroup open_locale classical nnreal noncomputable theory local attribute [instance] type_pow -- move this def Profinite.of (X : Type*) [topological_space X] [t2_space X] [totally_disconnected_space X] [compact_space X] : Profinite := { to_Top := Top.of X, is_compact := ‹_›, is_t2 := ‹_›, is_totally_disconnected := ‹_› } open NormedGroup opposite Profinite pseudo_normed_group category_theory breen_deligne open profinitely_filtered_pseudo_normed_group open profinitely_filtered_pseudo_normed_group_with_Tinv universe variable u variables (r' : ℝ≥0) {M M₁ M₂ M₃ : Type u} variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M] variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₁] variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₂] variables [profinitely_filtered_pseudo_normed_group_with_Tinv r' M₃] variables (c c₁ c₂ c₃ c₄ : ℝ≥0) (l m n : ℕ) (ϕ : basic_universal_map m n) variables (f : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₁ M₂) variables (g : profinitely_filtered_pseudo_normed_group_with_Tinv_hom r' M₂ M₃) /-- The "functor" that sends `M` and `c` to `(filtration M c)^n` -/ def FiltrationPow (r' : ℝ≥0) (M : Type*) (c : ℝ≥0) (n : ℕ) [profinitely_filtered_pseudo_normed_group_with_Tinv r' M] : Profinite := of ((filtration M c : Type*)^n) namespace FiltrationPow @[simps] def map : FiltrationPow r' M₁ c n ⟶ FiltrationPow r' M₂ c n := { to_fun := λ x j, f.level c (x j), continuous_to_fun := begin -- factor this into a separate lemma `continuous.pi_map`? apply continuous_pi, intro j, exact (f.level_continuous c).comp (continuous_apply j), end } variables (M) @[simp] lemma map_id : map r' c n (profinitely_filtered_pseudo_normed_group_with_Tinv_hom.id) = 𝟙 (FiltrationPow r' M c n) := by { ext, refl } variables {M} lemma map_comp : map r' c n (g.comp f) = map r' c n f ≫ map r' c n g := by { ext, refl } @[simps] def cast_le [fact (c₁ ≤ c₂)] : FiltrationPow r' M c₁ n ⟶ FiltrationPow r' M c₂ n := { to_fun := λ x j, cast_le (x j), continuous_to_fun := begin -- factor this into a separate lemma `continuous.pi_map`? apply continuous_pi, intro j, exact (embedding_cast_le c₁ c₂).continuous.comp (continuous_apply j), end } @[simp] lemma cast_le_refl : cast_le r' c c n = 𝟙 (FiltrationPow r' M c n) := by { ext, refl } lemma cast_le_trans [fact (c₁ ≤ c₂)] [fact (c₂ ≤ c₃)] [fact (c₁ ≤ c₃)] : cast_le r' c₁ c₂ n ≫ cast_le r' c₂ c₃ n = @cast_le r' M _ c₁ c₃ n _ := by { ext, refl } lemma map_comp_cast_le [fact (c₁ ≤ c₂)] : map r' c₁ n f ≫ cast_le r' c₁ c₂ n = cast_le r' c₁ c₂ n ≫ map r' c₂ n f := by { ext, refl } @[simps] def Tinv : FiltrationPow r' M c n ⟶ FiltrationPow r' M (r'⁻¹ * c) n := { to_fun := λ x j, Tinv₀ c (x j), continuous_to_fun := begin -- factor this into a separate lemma `continuous.pi_map`? apply continuous_pi, intro j, exact (Tinv₀_continuous c).comp (continuous_apply j), end } lemma map_comp_Tinv : map r' c n f ≫ Tinv r' c n = Tinv r' c n ≫ map r' (r'⁻¹ * c) n f := by { ext x j, exact (f.map_Tinv (x j)).symm } lemma cast_le_comp_Tinv [fact (c₁ ≤ c₂)] : cast_le r' c₁ c₂ n ≫ (@Tinv r' M _ c₂ n) = Tinv r' c₁ n ≫ cast_le r' (r'⁻¹ * c₁) (r'⁻¹ * c₂) n := by { ext, refl } end FiltrationPow namespace breen_deligne namespace basic_universal_map open FiltrationPow variables (M) {l m n} @[simps] def eval_FP [ϕ.suitable c₁ c₂] : FiltrationPow r' M c₁ m ⟶ FiltrationPow r' M c₂ n := { to_fun := ϕ.eval_png₀ M c₁ c₂, continuous_to_fun := ϕ.eval_png₀_continuous M c₁ c₂ } lemma eval_FP_comp (g : basic_universal_map m n) (f : basic_universal_map l m) [hg : g.suitable c₂ c₃] [hf : f.suitable c₁ c₂] [(g.comp f).suitable c₁ c₃] : (g.comp f).eval_FP r' M c₁ c₃ = f.eval_FP r' M c₁ c₂ ≫ g.eval_FP r' M c₂ c₃ := begin ext j s i, dsimp, simp only [eval_png₀, subtype.coe_mk], rw eval_png_comp, simp only [add_monoid_hom.coe_comp, function.comp_app], refl, end lemma map_comp_eval_FP [ϕ.suitable c₁ c₂] : map r' c₁ m f ≫ ϕ.eval_FP r' M₂ c₁ c₂ = ϕ.eval_FP r' M₁ c₁ c₂ ≫ map r' c₂ n f := begin ext1 x, show ϕ.eval_png₀ M₂ c₁ c₂ (map r' c₁ m f x) = map r' c₂ n f (ϕ.eval_png₀ M₁ c₁ c₂ x), ext j, dsimp only [basic_universal_map.eval_png₀], simp only [basic_universal_map.eval_png_apply, f.map_sum, map_to_fun, subtype.coe_mk, pow_incl_apply, f.level_coe], apply fintype.sum_congr, intro i, simp only [← gsmul_eq_smul], exact (f.to_add_monoid_hom.map_gsmul _ _).symm end lemma cast_le_comp_eval_FP [fact (c₁ ≤ c₂)] [ϕ.suitable c₂ c₄] [ϕ.suitable c₁ c₃] [fact (c₃ ≤ c₄)] : cast_le r' c₁ c₂ m ≫ ϕ.eval_FP r' M c₂ c₄ = ϕ.eval_FP r' M c₁ c₃ ≫ cast_le r' c₃ c₄ n := by { ext, refl } open profinitely_filtered_pseudo_normed_group_with_Tinv lemma Tinv_comp_eval_FP [ϕ.suitable c₁ c₂] : Tinv r' c₁ m ≫ ϕ.eval_FP r' M (r'⁻¹ * c₁) (r'⁻¹ * c₂) = ϕ.eval_FP r' M c₁ c₂ ≫ Tinv r' c₂ n := begin ext1 x, show ϕ.eval_png₀ M (r'⁻¹ * c₁) (r'⁻¹ * c₂) (Tinv r' c₁ m x) = Tinv r' c₂ n (ϕ.eval_png₀ M c₁ c₂ x), ext j, dsimp only [eval_png₀], simp only [eval_png_apply, map_to_fun, subtype.coe_mk, pow_incl_apply, Tinv_to_fun, Tinv₀_coe, profinitely_filtered_pseudo_normed_group_hom.map_sum], apply fintype.sum_congr, intro i, simp only [← gsmul_eq_smul], exact ((profinitely_filtered_pseudo_normed_group_hom.to_add_monoid_hom _).map_gsmul _ _).symm end end basic_universal_map end breen_deligne open breen_deligne
255c73efa2913e14f6eeb412ed3c4bdf47d568b7
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/data/fintype/basic.lean
fae98a610ae9eab7512108f0f517b173123a4665
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
44,490
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Finite types. -/ import data.finset.sort import data.finset.powerset import data.finset.pi import data.array.lemmas universes u v variables {α : Type*} {β : Type*} {γ : Type*} /-- `fintype α` means that `α` is finite, i.e. there are only finitely many distinct elements of type `α`. The evidence of this is a finset `elems` (a list up to permutation without duplicates), together with a proof that everything of type `α` is in the list. -/ class fintype (α : Type*) := (elems [] : finset α) (complete : ∀ x : α, x ∈ elems) namespace finset variable [fintype α] /-- `univ` is the universal finite set of type `finset α` implied from the assumption `fintype α`. -/ def univ : finset α := fintype.elems α @[simp] theorem mem_univ (x : α) : x ∈ (univ : finset α) := fintype.complete x @[simp] theorem mem_univ_val : ∀ x, x ∈ (univ : finset α).1 := mem_univ @[simp] lemma coe_univ : ↑(univ : finset α) = (set.univ : set α) := by ext; simp theorem subset_univ (s : finset α) : s ⊆ univ := λ a _, mem_univ a theorem eq_univ_iff_forall {s : finset α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] @[simp] lemma univ_inter [decidable_eq α] (s : finset α) : univ ∩ s = s := ext $ λ a, by simp @[simp] lemma inter_univ [decidable_eq α] (s : finset α) : s ∩ univ = s := by rw [inter_comm, univ_inter] @[simp] lemma piecewise_univ [∀i : α, decidable (i ∈ (univ : finset α))] {δ : α → Sort*} (f g : Πi, δ i) : univ.piecewise f g = f := by { ext i, simp [piecewise] } lemma univ_map_equiv_to_embedding {α β : Type*} [fintype α] [fintype β] (e : α ≃ β) : univ.map e.to_embedding = univ := begin apply eq_univ_iff_forall.mpr, intro b, rw [mem_map], use e.symm b, simp, end end finset open finset function namespace fintype instance decidable_pi_fintype {α} {β : α → Type*} [∀a, decidable_eq (β a)] [fintype α] : decidable_eq (Πa, β a) := assume f g, decidable_of_iff (∀ a ∈ fintype.elems α, f a = g a) (by simp [function.funext_iff, fintype.complete]) instance decidable_forall_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∀ a, p a) := decidable_of_iff (∀ a ∈ @univ α _, p a) (by simp) instance decidable_exists_fintype {p : α → Prop} [decidable_pred p] [fintype α] : decidable (∃ a, p a) := decidable_of_iff (∃ a ∈ @univ α _, p a) (by simp) instance decidable_eq_equiv_fintype [decidable_eq β] [fintype α] : decidable_eq (α ≃ β) := λ a b, decidable_of_iff (a.1 = b.1) ⟨λ h, equiv.ext (congr_fun h), congr_arg _⟩ instance decidable_injective_fintype [decidable_eq α] [decidable_eq β] [fintype α] : decidable_pred (injective : (α → β) → Prop) := λ x, by unfold injective; apply_instance instance decidable_surjective_fintype [decidable_eq β] [fintype α] [fintype β] : decidable_pred (surjective : (α → β) → Prop) := λ x, by unfold surjective; apply_instance instance decidable_bijective_fintype [decidable_eq α] [decidable_eq β] [fintype α] [fintype β] : decidable_pred (bijective : (α → β) → Prop) := λ x, by unfold bijective; apply_instance instance decidable_left_inverse_fintype [decidable_eq α] [fintype α] (f : α → β) (g : β → α) : decidable (function.right_inverse f g) := show decidable (∀ x, g (f x) = x), by apply_instance instance decidable_right_inverse_fintype [decidable_eq β] [fintype β] (f : α → β) (g : β → α) : decidable (function.left_inverse f g) := show decidable (∀ x, f (g x) = x), by apply_instance /-- Construct a proof of `fintype α` from a universal multiset -/ def of_multiset [decidable_eq α] (s : multiset α) (H : ∀ x : α, x ∈ s) : fintype α := ⟨s.to_finset, by simpa using H⟩ /-- Construct a proof of `fintype α` from a universal list -/ def of_list [decidable_eq α] (l : list α) (H : ∀ x : α, x ∈ l) : fintype α := ⟨l.to_finset, by simpa using H⟩ theorem exists_univ_list (α) [fintype α] : ∃ l : list α, l.nodup ∧ ∀ x : α, x ∈ l := let ⟨l, e⟩ := quotient.exists_rep (@univ α _).1 in by have := and.intro univ.2 mem_univ_val; exact ⟨_, by rwa ← e at this⟩ /-- `card α` is the number of elements in `α`, defined when `α` is a fintype. -/ def card (α) [fintype α] : ℕ := (@univ α _).card /-- If `l` lists all the elements of `α` without duplicates, then `α ≃ fin (l.length)`. -/ def equiv_fin_of_forall_mem_list {α} [decidable_eq α] {l : list α} (h : ∀ x:α, x ∈ l) (nd : l.nodup) : α ≃ fin (l.length) := ⟨λ a, ⟨_, list.index_of_lt_length.2 (h a)⟩, λ i, l.nth_le i.1 i.2, λ a, by simp, λ ⟨i, h⟩, fin.eq_of_veq $ list.nodup_iff_nth_le_inj.1 nd _ _ (list.index_of_lt_length.2 (list.nth_le_mem _ _ _)) h $ by simp⟩ /-- There is (computably) a bijection between `α` and `fin n` where `n = card α`. Since it is not unique, and depends on which permutation of the universe list is used, the bijection is wrapped in `trunc` to preserve computability. -/ def equiv_fin (α) [fintype α] [decidable_eq α] : trunc (α ≃ fin (card α)) := by unfold card finset.card; exact quot.rec_on_subsingleton (@univ α _).1 (λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk (equiv_fin_of_forall_mem_list h nd)) mem_univ_val univ.2 theorem exists_equiv_fin (α) [fintype α] : ∃ n, nonempty (α ≃ fin n) := by haveI := classical.dec_eq α; exact ⟨card α, nonempty_of_trunc (equiv_fin α)⟩ /-- Given a linearly ordered fintype `α` of cardinal `k`, the equiv `mono_equiv_of_fin α h` is the increasing bijection between `fin k` and `α`. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of a map `fin s.card → α` to avoid casting issues in further uses of this function. -/ noncomputable def mono_equiv_of_fin (α) [fintype α] [decidable_linear_order α] {k : ℕ} (h : fintype.card α = k) : fin k ≃ α := equiv.of_bijective (mono_of_fin univ h) begin apply set.bijective_iff_bij_on_univ.2, rw ← @coe_univ α _, exact mono_of_fin_bij_on (univ : finset α) h end instance (α : Type*) : subsingleton (fintype α) := ⟨λ ⟨s₁, h₁⟩ ⟨s₂, h₂⟩, by congr; simp [finset.ext_iff, h₁, h₂]⟩ protected def subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : fintype {x // p x} := ⟨⟨multiset.pmap subtype.mk s.1 (λ x, (H x).1), multiset.nodup_pmap (λ a _ b _, congr_arg subtype.val) s.2⟩, λ ⟨x, px⟩, multiset.mem_pmap.2 ⟨x, (H x).2 px, rfl⟩⟩ theorem subtype_card {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) : @card {x // p x} (fintype.subtype s H) = s.card := multiset.card_pmap _ _ _ theorem card_of_subtype {p : α → Prop} (s : finset α) (H : ∀ x : α, x ∈ s ↔ p x) [fintype {x // p x}] : card {x // p x} = s.card := by rw ← subtype_card s H; congr /-- Construct a fintype from a finset with the same elements. -/ def of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : fintype p := fintype.subtype s H @[simp] theorem card_of_finset {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) : @fintype.card p (of_finset s H) = s.card := fintype.subtype_card s H theorem card_of_finset' {p : set α} (s : finset α) (H : ∀ x, x ∈ s ↔ x ∈ p) [fintype p] : fintype.card p = s.card := by rw ← card_of_finset s H; congr /-- If `f : α → β` is a bijection and `α` is a fintype, then `β` is also a fintype. -/ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : fintype β := ⟨univ.map ⟨f, H.1⟩, λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩ /-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/ def of_surjective [fintype α] [decidable_eq β] (f : α → β) (H : function.surjective f) : fintype β := ⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩ noncomputable def of_injective [fintype β] (f : α → β) (H : function.injective f) : fintype α := by letI := classical.dec; exact if hα : nonempty α then by letI := classical.inhabited_of_nonempty hα; exact of_surjective (inv_fun f) (inv_fun_surjective H) else ⟨∅, λ x, (hα ⟨x⟩).elim⟩ /-- If `f : α ≃ β` and `α` is a fintype, then `β` is also a fintype. -/ def of_equiv (α : Type*) [fintype α] (f : α ≃ β) : fintype β := of_bijective _ f.bijective theorem of_equiv_card [fintype α] (f : α ≃ β) : @card β (of_equiv α f) = card α := multiset.card_map _ _ theorem card_congr {α β} [fintype α] [fintype β] (f : α ≃ β) : card α = card β := by rw ← of_equiv_card f; congr theorem card_eq {α β} [F : fintype α] [G : fintype β] : card α = card β ↔ nonempty (α ≃ β) := ⟨λ h, ⟨by classical; calc α ≃ fin (card α) : trunc.out (equiv_fin α) ... ≃ fin (card β) : by rw h ... ≃ β : (trunc.out (equiv_fin β)).symm⟩, λ ⟨f⟩, card_congr f⟩ def of_subsingleton (a : α) [subsingleton α] : fintype α := ⟨{a}, λ b, finset.mem_singleton.2 (subsingleton.elim _ _)⟩ @[simp] theorem univ_of_subsingleton (a : α) [subsingleton α] : @univ _ (of_subsingleton a) = {a} := rfl @[simp] theorem card_of_subsingleton (a : α) [subsingleton α] : @fintype.card _ (of_subsingleton a) = 1 := rfl end fintype namespace set /-- Construct a finset enumerating a set `s`, given a `fintype` instance. -/ def to_finset (s : set α) [fintype s] : finset α := ⟨(@finset.univ s _).1.map subtype.val, multiset.nodup_map (λ a b, subtype.eq) finset.univ.2⟩ @[simp] theorem mem_to_finset {s : set α} [fintype s] {a : α} : a ∈ s.to_finset ↔ a ∈ s := by simp [to_finset] @[simp] theorem mem_to_finset_val {s : set α} [fintype s] {a : α} : a ∈ s.to_finset.1 ↔ a ∈ s := mem_to_finset -- We use an arbitrary `[fintype s]` instance here, -- not necessarily coming from a `[fintype α]`. @[simp] lemma to_finset_card {α : Type*} (s : set α) [fintype s] : s.to_finset.card = fintype.card s := multiset.card_map subtype.val finset.univ.val @[simp] theorem coe_to_finset (s : set α) [fintype s] : (↑s.to_finset : set α) = s := set.ext $ λ _, mem_to_finset @[simp] theorem to_finset_inj {s t : set α} [fintype s] [fintype t] : s.to_finset = t.to_finset ↔ s = t := ⟨λ h, by rw [← s.coe_to_finset, h, t.coe_to_finset], λ h, by simp [h]; congr⟩ end set lemma finset.card_univ [fintype α] : (finset.univ : finset α).card = fintype.card α := rfl lemma finset.card_univ_diff [fintype α] [decidable_eq α] (s : finset α) : (finset.univ \ s).card = fintype.card α - s.card := finset.card_sdiff (subset_univ s) instance (n : ℕ) : fintype (fin n) := ⟨⟨list.fin_range n, list.nodup_fin_range n⟩, list.mem_fin_range⟩ @[simp] theorem fintype.card_fin (n : ℕ) : fintype.card (fin n) = n := list.length_fin_range n @[simp] lemma finset.card_fin (n : ℕ) : finset.card (finset.univ : finset (fin n)) = n := by rw [finset.card_univ, fintype.card_fin] lemma fin.univ_succ (n : ℕ) : (univ : finset (fin $ n+1)) = insert 0 (univ.image fin.succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop], exact fin.cases (or.inl rfl) (λ i, or.inr ⟨i, trivial, rfl⟩) m end lemma fin.univ_cast_succ (n : ℕ) : (univ : finset (fin $ n+1)) = insert (fin.last n) (univ.image fin.cast_succ) := begin ext m, simp only [mem_univ, mem_insert, true_iff, mem_image, exists_prop, true_and], by_cases h : m.val < n, { right, use fin.cast_lt m h, rw fin.cast_succ_cast_lt }, { left, exact fin.eq_last_of_not_lt h } end /-- Any increasing map between `fin k` and a finset of cardinality `k` has to coincide with the increasing bijection `mono_of_fin s h`. -/ lemma finset.mono_of_fin_unique' [decidable_linear_order α] {s : finset α} {k : ℕ} (h : s.card = k) {f : fin k → α} (fmap : set.maps_to f set.univ ↑s) (hmono : strict_mono f) : f = s.mono_of_fin h := begin have finj : set.inj_on f set.univ := hmono.injective.inj_on _, apply mono_of_fin_unique h (set.bij_on.mk fmap finj (λ y hy, _)) hmono, simp only [set.image_univ, set.mem_range], rcases surj_on_of_inj_on_of_card_le (λ i (hi : i ∈ finset.univ), f i) (λ i hi, fmap (set.mem_univ i)) (λ i j hi hj hij, finj (set.mem_univ i) (set.mem_univ j) hij) (by simp [h]) y hy with ⟨x, _, hx⟩, exact ⟨x, hx.symm⟩ end @[instance, priority 10] def unique.fintype {α : Type*} [unique α] : fintype α := fintype.of_subsingleton (default α) @[simp] lemma univ_unique {α : Type*} [unique α] [f : fintype α] : @finset.univ α _ = {default α} := by rw [subsingleton.elim f (@unique.fintype α _)]; refl instance : fintype empty := ⟨∅, empty.rec _⟩ @[simp] theorem fintype.univ_empty : @univ empty _ = ∅ := rfl @[simp] theorem fintype.card_empty : fintype.card empty = 0 := rfl instance : fintype pempty := ⟨∅, pempty.rec _⟩ @[simp] theorem fintype.univ_pempty : @univ pempty _ = ∅ := rfl @[simp] theorem fintype.card_pempty : fintype.card pempty = 0 := rfl instance : fintype unit := fintype.of_subsingleton () theorem fintype.univ_unit : @univ unit _ = {()} := rfl theorem fintype.card_unit : fintype.card unit = 1 := rfl instance : fintype punit := fintype.of_subsingleton punit.star @[simp] theorem fintype.univ_punit : @univ punit _ = {punit.star} := rfl @[simp] theorem fintype.card_punit : fintype.card punit = 1 := rfl instance : fintype bool := ⟨⟨tt::ff::0, by simp⟩, λ x, by cases x; simp⟩ @[simp] theorem fintype.univ_bool : @univ bool _ = {tt, ff} := rfl instance units_int.fintype : fintype (units ℤ) := ⟨{1, -1}, λ x, by cases int.units_eq_one_or x; simp *⟩ instance additive.fintype : Π [fintype α], fintype (additive α) := id instance multiplicative.fintype : Π [fintype α], fintype (multiplicative α) := id @[simp] theorem fintype.card_units_int : fintype.card (units ℤ) = 2 := rfl noncomputable instance [monoid α] [fintype α] : fintype (units α) := by classical; exact fintype.of_injective units.val units.ext @[simp] theorem fintype.card_bool : fintype.card bool = 2 := rfl def finset.insert_none (s : finset α) : finset (option α) := ⟨none :: s.1.map some, multiset.nodup_cons.2 ⟨by simp, multiset.nodup_map (λ a b, option.some.inj) s.2⟩⟩ @[simp] theorem finset.mem_insert_none {s : finset α} : ∀ {o : option α}, o ∈ s.insert_none ↔ ∀ a ∈ o, a ∈ s | none := iff_of_true (multiset.mem_cons_self _ _) (λ a h, by cases h) | (some a) := multiset.mem_cons.trans $ by simp; refl theorem finset.some_mem_insert_none {s : finset α} {a : α} : some a ∈ s.insert_none ↔ a ∈ s := by simp instance {α : Type*} [fintype α] : fintype (option α) := ⟨univ.insert_none, λ a, by simp⟩ @[simp] theorem fintype.card_option {α : Type*} [fintype α] : fintype.card (option α) = fintype.card α + 1 := (multiset.card_cons _ _).trans (by rw multiset.card_map; refl) instance {α : Type*} (β : α → Type*) [fintype α] [∀ a, fintype (β a)] : fintype (sigma β) := ⟨univ.sigma (λ _, univ), λ ⟨a, b⟩, by simp⟩ @[simp] lemma finset.univ_sigma_univ {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : (univ : finset α).sigma (λ a, (univ : finset (β a))) = univ := rfl instance (α β : Type*) [fintype α] [fintype β] : fintype (α × β) := ⟨univ.product univ, λ ⟨a, b⟩, by simp⟩ @[simp] theorem fintype.card_prod (α β : Type*) [fintype α] [fintype β] : fintype.card (α × β) = fintype.card α * fintype.card β := card_product _ _ def fintype.fintype_prod_left {α β} [decidable_eq α] [fintype (α × β)] [nonempty β] : fintype α := ⟨(fintype.elems (α × β)).image prod.fst, assume a, let ⟨b⟩ := ‹nonempty β› in by simp; exact ⟨b, fintype.complete _⟩⟩ def fintype.fintype_prod_right {α β} [decidable_eq β] [fintype (α × β)] [nonempty α] : fintype β := ⟨(fintype.elems (α × β)).image prod.snd, assume b, let ⟨a⟩ := ‹nonempty α› in by simp; exact ⟨a, fintype.complete _⟩⟩ instance (α : Type*) [fintype α] : fintype (ulift α) := fintype.of_equiv _ equiv.ulift.symm @[simp] theorem fintype.card_ulift (α : Type*) [fintype α] : fintype.card (ulift α) = fintype.card α := fintype.of_equiv_card _ instance (α : Type u) (β : Type v) [fintype α] [fintype β] : fintype (α ⊕ β) := @fintype.of_equiv _ _ (@sigma.fintype _ (λ b, cond b (ulift α) (ulift.{(max u v) v} β)) _ (λ b, by cases b; apply ulift.fintype)) ((equiv.sum_equiv_sigma_bool _ _).symm.trans (equiv.sum_congr equiv.ulift equiv.ulift)) lemma fintype.card_le_of_injective [fintype α] [fintype β] (f : α → β) (hf : function.injective f) : fintype.card α ≤ fintype.card β := by haveI := classical.prop_decidable; exact finset.card_le_card_of_inj_on f (λ _ _, finset.mem_univ _) (λ _ _ _ _ h, hf h) lemma fintype.card_eq_one_iff [fintype α] : fintype.card α = 1 ↔ (∃ x : α, ∀ y, y = x) := by rw [← fintype.card_unit, fintype.card_eq]; exact ⟨λ ⟨a⟩, ⟨a.symm (), λ y, a.injective (subsingleton.elim _ _)⟩, λ ⟨x, hx⟩, ⟨⟨λ _, (), λ _, x, λ _, (hx _).trans (hx _).symm, λ _, subsingleton.elim _ _⟩⟩⟩ lemma fintype.card_eq_zero_iff [fintype α] : fintype.card α = 0 ↔ (α → false) := ⟨λ h a, have e : α ≃ empty := classical.choice (fintype.card_eq.1 (by simp [h])), (e a).elim, λ h, have e : α ≃ empty := ⟨λ a, (h a).elim, λ a, a.elim, λ a, (h a).elim, λ a, a.elim⟩, by simp [fintype.card_congr e]⟩ /-- A `fintype` with cardinality zero is (constructively) equivalent to `pempty`. -/ def fintype.card_eq_zero_equiv_equiv_pempty {α : Type v} [fintype α] : fintype.card α = 0 ≃ (α ≃ pempty.{v+1}) := { to_fun := λ h, { to_fun := λ a, false.elim (fintype.card_eq_zero_iff.1 h a), inv_fun := λ a, pempty.elim a, left_inv := λ a, false.elim (fintype.card_eq_zero_iff.1 h a), right_inv := λ a, pempty.elim a, }, inv_fun := λ e, by { simp only [←fintype.of_equiv_card e], convert fintype.card_pempty, }, left_inv := λ h, rfl, right_inv := λ e, by { ext x, cases e x, } } lemma fintype.card_pos_iff [fintype α] : 0 < fintype.card α ↔ nonempty α := ⟨λ h, classical.by_contradiction (λ h₁, have fintype.card α = 0 := fintype.card_eq_zero_iff.2 (λ a, h₁ ⟨a⟩), lt_irrefl 0 $ by rwa this at h), λ ⟨a⟩, nat.pos_of_ne_zero (mt fintype.card_eq_zero_iff.1 (λ h, h a))⟩ lemma fintype.card_le_one_iff [fintype α] : fintype.card α ≤ 1 ↔ (∀ a b : α, a = b) := let n := fintype.card α in have hn : n = fintype.card α := rfl, match n, hn with | 0 := λ ha, ⟨λ h, λ a, (fintype.card_eq_zero_iff.1 ha.symm a).elim, λ _, ha ▸ nat.le_succ _⟩ | 1 := λ ha, ⟨λ h, λ a b, let ⟨x, hx⟩ := fintype.card_eq_one_iff.1 ha.symm in by rw [hx a, hx b], λ _, ha ▸ le_refl _⟩ | (n+2) := λ ha, ⟨λ h, by rw ← ha at h; exact absurd h dec_trivial, (λ h, fintype.card_unit ▸ fintype.card_le_of_injective (λ _, ()) (λ _ _ _, h _ _))⟩ end lemma fintype.exists_ne_of_one_lt_card [fintype α] (h : 1 < fintype.card α) (a : α) : ∃ b : α, b ≠ a := let ⟨b, hb⟩ := classical.not_forall.1 (mt fintype.card_le_one_iff.2 (not_le_of_gt h)) in let ⟨c, hc⟩ := classical.not_forall.1 hb in by haveI := classical.dec_eq α; exact if hba : b = a then ⟨c, by cc⟩ else ⟨b, hba⟩ lemma fintype.exists_pair_of_one_lt_card [fintype α] (h : 1 < fintype.card α) : ∃ (a b : α), b ≠ a := begin rcases fintype.card_pos_iff.1 (nat.lt_of_succ_lt h) with a, exact ⟨a, fintype.exists_ne_of_one_lt_card h a⟩, end lemma fintype.injective_iff_surjective [fintype α] {f : α → α} : injective f ↔ surjective f := by haveI := classical.prop_decidable; exact have ∀ {f : α → α}, injective f → surjective f, from λ f hinj x, have h₁ : image f univ = univ := eq_of_subset_of_card_le (subset_univ _) ((card_image_of_injective univ hinj).symm ▸ le_refl _), have h₂ : x ∈ image f univ := h₁.symm ▸ mem_univ _, exists_of_bex (mem_image.1 h₂), ⟨this, λ hsurj, has_left_inverse.injective ⟨surj_inv hsurj, left_inverse_of_surjective_of_right_inverse (this (injective_surj_inv _)) (right_inverse_surj_inv _)⟩⟩ lemma fintype.injective_iff_bijective [fintype α] {f : α → α} : injective f ↔ bijective f := by simp [bijective, fintype.injective_iff_surjective] lemma fintype.surjective_iff_bijective [fintype α] {f : α → α} : surjective f ↔ bijective f := by simp [bijective, fintype.injective_iff_surjective] lemma fintype.injective_iff_surjective_of_equiv [fintype α] {f : α → β} (e : α ≃ β) : injective f ↔ surjective f := have injective (e.symm ∘ f) ↔ surjective (e.symm ∘ f), from fintype.injective_iff_surjective, ⟨λ hinj, by simpa [function.comp] using 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 fintype.coe_image_univ [fintype α] [decidable_eq β] {f : α → β} : ↑(finset.image f finset.univ) = set.range f := by { ext x, simp } instance list.subtype.fintype [decidable_eq α] (l : list α) : fintype {x // x ∈ l} := fintype.of_list l.attach l.mem_attach instance multiset.subtype.fintype [decidable_eq α] (s : multiset α) : fintype {x // x ∈ s} := fintype.of_multiset s.attach s.mem_attach instance finset.subtype.fintype (s : finset α) : fintype {x // x ∈ s} := ⟨s.attach, s.mem_attach⟩ instance finset_coe.fintype (s : finset α) : fintype (↑s : set α) := finset.subtype.fintype s @[simp] lemma fintype.card_coe (s : finset α) : fintype.card (↑s : set α) = s.card := card_attach lemma finset.attach_eq_univ {s : finset α} : s.attach = finset.univ := rfl lemma finset.card_le_one_iff {s : finset α} : s.card ≤ 1 ↔ ∀ {x y}, x ∈ s → y ∈ s → x = y := begin let t : set α := ↑s, letI : fintype t := finset_coe.fintype s, have : fintype.card t = s.card := fintype.card_coe s, rw [← this, fintype.card_le_one_iff], split, { assume H x y hx hy, exact subtype.mk.inj (H ⟨x, hx⟩ ⟨y, hy⟩) }, { assume H x y, exact subtype.eq (H x.2 y.2) } end /-- A `finset` of a subsingleton type has cardinality at most one. -/ lemma finset.card_le_one_of_subsingleton [subsingleton α] (s : finset α) : s.card ≤ 1 := finset.card_le_one_iff.2 $ λ _ _ _ _, subsingleton.elim _ _ lemma finset.one_lt_card_iff {s : finset α} : 1 < s.card ↔ ∃ x y, (x ∈ s) ∧ (y ∈ s) ∧ x ≠ y := begin classical, rw ← not_iff_not, push_neg, simpa [classical.or_iff_not_imp_left] using finset.card_le_one_iff end instance plift.fintype (p : Prop) [decidable p] : fintype (plift p) := ⟨if h : p then {⟨h⟩} else ∅, λ ⟨h⟩, by simp [h]⟩ instance Prop.fintype : fintype Prop := ⟨⟨true::false::0, by simp [true_ne_false]⟩, classical.cases (by simp) (by simp)⟩ def set_fintype {α} [fintype α] (s : set α) [decidable_pred s] : fintype s := fintype.subtype (univ.filter (∈ s)) (by simp) namespace function.embedding /-- An embedding from a `fintype` to itself can be promoted to an equivalence. -/ noncomputable def equiv_of_fintype_self_embedding {α : Type*} [fintype α] (e : α ↪ α) : α ≃ α := equiv.of_bijective e (fintype.injective_iff_bijective.1 e.2) @[simp] lemma equiv_of_fintype_self_embedding_to_embedding {α : Type*} [fintype α] (e : α ↪ α) : e.equiv_of_fintype_self_embedding.to_embedding = e := by { ext, refl, } end function.embedding @[simp] lemma finset.univ_map_embedding {α : Type*} [fintype α] (e : α ↪ α) : univ.map e = univ := by rw [← e.equiv_of_fintype_self_embedding_to_embedding, univ_map_equiv_to_embedding] namespace fintype variables [fintype α] [decidable_eq α] {δ : α → Type*} /-- Given for all `a : α` a finset `t a` of `δ a`, then one can define the finset `fintype.pi_finset t` of all functions taking values in `t a` for all `a`. This is the analogue of `finset.pi` where the base finset is `univ` (but formally they are not the same, as there is an additional condition `i ∈ finset.univ` in the `finset.pi` definition). -/ def pi_finset (t : Πa, finset (δ a)) : finset (Πa, δ a) := (finset.univ.pi t).map ⟨λ f a, f a (mem_univ a), λ _ _, by simp [function.funext_iff]⟩ @[simp] lemma mem_pi_finset {t : Πa, finset (δ a)} {f : Πa, δ a} : f ∈ pi_finset t ↔ (∀a, f a ∈ t a) := begin split, { simp only [pi_finset, mem_map, and_imp, forall_prop_of_true, exists_prop, mem_univ, exists_imp_distrib, mem_pi], assume g hg hgf a, rw ← hgf, exact hg a }, { simp only [pi_finset, mem_map, forall_prop_of_true, exists_prop, mem_univ, mem_pi], assume hf, exact ⟨λ a ha, f a, hf, rfl⟩ } end lemma pi_finset_subset (t₁ t₂ : Πa, finset (δ a)) (h : ∀ a, t₁ a ⊆ t₂ a) : pi_finset t₁ ⊆ pi_finset t₂ := λ g hg, mem_pi_finset.2 $ λ a, h a $ mem_pi_finset.1 hg a lemma pi_finset_disjoint_of_disjoint [∀ a, decidable_eq (δ a)] (t₁ t₂ : Πa, finset (δ a)) {a : α} (h : disjoint (t₁ a) (t₂ a)) : disjoint (pi_finset t₁) (pi_finset t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a) (mem_pi_finset.1 hf₁ a) (f₂ a) (mem_pi_finset.1 hf₂ a) (congr_fun eq₁₂ a) end fintype /-! ### pi -/ /-- A dependent product of fintypes, indexed by a fintype, is a fintype. -/ instance pi.fintype {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype (Πa, β a) := ⟨fintype.pi_finset (λ _, univ), by simp⟩ @[simp] lemma fintype.pi_finset_univ {α : Type*} {β : α → Type*} [decidable_eq α] [fintype α] [∀a, fintype (β a)] : fintype.pi_finset (λ a : α, (finset.univ : finset (β a))) = (finset.univ : finset (Π a, β a)) := rfl instance d_array.fintype {n : ℕ} {α : fin n → Type*} [∀n, fintype (α n)] : fintype (d_array n α) := fintype.of_equiv _ (equiv.d_array_equiv_fin _).symm instance array.fintype {n : ℕ} {α : Type*} [fintype α] : fintype (array n α) := d_array.fintype instance vector.fintype {α : Type*} [fintype α] {n : ℕ} : fintype (vector α n) := fintype.of_equiv _ (equiv.vector_equiv_fin _ _).symm instance quotient.fintype [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] : fintype (quotient s) := fintype.of_surjective quotient.mk (λ x, quotient.induction_on x (λ x, ⟨x, rfl⟩)) instance finset.fintype [fintype α] : fintype (finset α) := ⟨univ.powerset, λ x, finset.mem_powerset.2 (finset.subset_univ _)⟩ @[simp] lemma fintype.card_finset [fintype α] : fintype.card (finset α) = 2 ^ (fintype.card α) := finset.card_powerset finset.univ instance subtype.fintype (p : α → Prop) [decidable_pred p] [fintype α] : fintype {x // p x} := set_fintype _ @[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] } theorem fintype.card_subtype_le [fintype α] (p : α → Prop) [decidable_pred p] : fintype.card {x // p x} ≤ fintype.card α := by rw fintype.subtype_card; exact card_le_of_subset (subset_univ _) theorem fintype.card_subtype_lt [fintype α] {p : α → Prop} [decidable_pred p] {x : α} (hx : ¬ p x) : fintype.card {x // p x} < fintype.card α := by rw [fintype.subtype_card]; exact finset.card_lt_card ⟨subset_univ _, classical.not_forall.2 ⟨x, by simp [*, set.mem_def]⟩⟩ instance psigma.fintype {α : Type*} {β : α → Type*} [fintype α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := fintype.of_equiv _ (equiv.psigma_equiv_sigma _).symm instance psigma.fintype_prop_left {α : Prop} {β : α → Type*} [decidable α] [∀ a, fintype (β a)] : fintype (Σ' a, β a) := if h : α then fintype.of_equiv (β h) ⟨λ x, ⟨h, x⟩, psigma.snd, λ _, rfl, λ ⟨_, _⟩, rfl⟩ else ⟨∅, λ x, h x.1⟩ instance psigma.fintype_prop_right {α : Type*} {β : α → Prop} [∀ a, decidable (β a)] [fintype α] : fintype (Σ' a, β a) := fintype.of_equiv {a // β a} ⟨λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, ⟨x, y⟩, λ ⟨x, y⟩, rfl, λ ⟨x, y⟩, rfl⟩ instance psigma.fintype_prop_prop {α : Prop} {β : α → Prop} [decidable α] [∀ a, decidable (β a)] : fintype (Σ' a, β a) := if h : ∃ a, β a then ⟨{⟨h.fst, h.snd⟩}, λ ⟨_, _⟩, by simp⟩ else ⟨∅, λ ⟨x, y⟩, h ⟨x, y⟩⟩ instance set.fintype [fintype α] : fintype (set α) := ⟨(@finset.univ α _).powerset.map ⟨coe, coe_injective⟩, λ s, begin classical, refine mem_map.2 ⟨finset.univ.filter s, mem_powerset.2 (subset_univ _), _⟩, apply (coe_filter _).trans, rw [coe_univ, set.sep_univ], refl end⟩ instance pfun_fintype (p : Prop) [decidable p] (α : p → Type*) [Π hp, fintype (α hp)] : fintype (Π hp : p, α hp) := if hp : p then fintype.of_equiv (α hp) ⟨λ a _, a, λ f, f hp, λ _, rfl, λ _, rfl⟩ else ⟨singleton (λ h, (hp h).elim), by simp [hp, function.funext_iff]⟩ lemma mem_image_univ_iff_mem_range {α β : Type*} [fintype α] [decidable_eq β] {f : α → β} {b : β} : b ∈ univ.image f ↔ b ∈ set.range f := by simp lemma card_lt_card_of_injective_of_not_mem {α β : Type*} [fintype α] [fintype β] (f : α → β) (h : function.injective f) {b : β} (w : b ∉ set.range f) : fintype.card α < fintype.card β := begin classical, calc fintype.card α = (univ : finset α).card : rfl ... = (image f univ).card : (card_image_of_injective univ h).symm ... < (insert b (image f univ)).card : card_lt_card (ssubset_insert (mt mem_image_univ_iff_mem_range.mp w)) ... ≤ (univ : finset β).card : card_le_of_subset (subset_univ _) ... = fintype.card β : rfl end def quotient.fin_choice_aux {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι), (∀ i ∈ l, quotient (S i)) → @quotient (Π i ∈ l, α i) (by apply_instance) | [] f := ⟦λ i, false.elim⟧ | (i::l) f := begin refine quotient.lift_on₂ (f i (list.mem_cons_self _ _)) (quotient.fin_choice_aux l (λ j h, f j (list.mem_cons_of_mem _ h))) _ _, exact λ a l, ⟦λ j h, if e : j = i then by rw e; exact a else l _ (h.resolve_left e)⟧, refine λ a₁ l₁ a₂ l₂ h₁ h₂, quotient.sound (λ j h, _), by_cases e : j = i; simp [e], { subst j, exact h₁ }, { exact h₂ _ _ } end theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] : ∀ (l : list ι) (f : ∀ i ∈ l, α i), quotient.fin_choice_aux l (λ i h, ⟦f i h⟧) = ⟦f⟧ | [] f := quotient.sound (λ i h, h.elim) | (i::l) f := begin simp [quotient.fin_choice_aux, quotient.fin_choice_aux_eq l], refine quotient.sound (λ j h, _), by_cases e : j = i; simp [e], subst j, refl end def quotient.fin_choice {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [S : ∀ i, setoid (α i)] (f : ∀ i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) := quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι, @quotient (Π i ∈ l, α i) (by apply_instance)) finset.univ.1 (λ l, quotient.fin_choice_aux l (λ i _, f i)) (λ a b h, begin have := λ a, quotient.fin_choice_aux_eq a (λ i h, quotient.out (f i)), simp [quotient.out_eq] at this, simp [this], let g := λ a:multiset ι, ⟦λ (i : ι) (h : i ∈ a), quotient.out (f i)⟧, refine eq_of_heq ((eq_rec_heq _ _).trans (_ : g a == g b)), congr' 1, exact quotient.sound h, end)) (λ f, ⟦λ i, f i (finset.mem_univ _)⟧) (λ a b h, quotient.sound $ λ i, h _ _) theorem quotient.fin_choice_eq {ι : Type*} [fintype ι] [decidable_eq ι] {α : ι → Type*} [∀ i, setoid (α i)] (f : ∀ i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ := begin let q, swap, change quotient.lift_on q _ _ = _, have : q = ⟦λ i h, f i⟧, { dsimp [q], exact quotient.induction_on (@finset.univ ι _).1 (λ l, quotient.fin_choice_aux_eq _ _) }, simp [this], exact setoid.refl _ end section equiv open list equiv equiv.perm variables [decidable_eq α] [decidable_eq β] def perms_of_list : list α → list (perm α) | [] := [1] | (a :: l) := perms_of_list l ++ l.bind (λ b, (perms_of_list l).map (λ f, swap a b * f)) lemma length_perms_of_list : ∀ l : list α, length (perms_of_list l) = l.length.fact | [] := rfl | (a :: l) := begin rw [length_cons, nat.fact_succ], simp [perms_of_list, length_bind, length_perms_of_list, function.comp, nat.succ_mul], cc end lemma mem_perms_of_list_of_mem : ∀ {l : list α} {f : perm α} (h : ∀ x, f x ≠ x → x ∈ l), f ∈ perms_of_list l | [] f h := list.mem_singleton.2 $ equiv.ext $ λ x, by simp [imp_false, *] at * | (a::l) f h := if hfa : f a = a then mem_append_left _ $ mem_perms_of_list_of_mem (λ x hx, mem_of_ne_of_mem (λ h, by rw h at hx; exact hx hfa) (h x hx)) else have hfa' : f (f a) ≠ f a, from mt (λ h, f.injective h) hfa, have ∀ (x : α), (swap a (f a) * f) x ≠ x → x ∈ l, from λ x hx, have hxa : x ≠ a, from λ h, by simpa [h, mul_apply] using hx, have hfxa : f x ≠ f a, from mt (λ h, f.injective h) hxa, list.mem_of_ne_of_mem hxa (h x (λ h, by simp [h, mul_apply, swap_apply_def] at hx; split_ifs at hx; cc)), suffices f ∈ perms_of_list l ∨ ∃ (b : α), b ∈ l ∧ ∃ g : perm α, g ∈ perms_of_list l ∧ swap a b * g = f, by simpa [perms_of_list], (@or_iff_not_imp_left _ _ (classical.prop_decidable _)).2 (λ hfl, ⟨f a, if hffa : f (f a) = a then mem_of_ne_of_mem hfa (h _ (mt (λ h, f.injective h) hfa)) else this _ $ by simp [mul_apply, swap_apply_def]; split_ifs; cc, ⟨swap a (f a) * f, mem_perms_of_list_of_mem this, by rw [← mul_assoc, mul_def (swap a (f a)) (swap a (f a)), swap_swap, ← equiv.perm.one_def, one_mul]⟩⟩) lemma mem_of_mem_perms_of_list : ∀ {l : list α} {f : perm α}, f ∈ perms_of_list l → ∀ {x}, f x ≠ x → x ∈ l | [] f h := have f = 1 := by simpa [perms_of_list] using h, by rw this; simp | (a::l) f h := (mem_append.1 h).elim (λ h x hx, mem_cons_of_mem _ (mem_of_mem_perms_of_list h hx)) (λ h x hx, let ⟨y, hy, hy'⟩ := list.mem_bind.1 h in let ⟨g, hg₁, hg₂⟩ := list.mem_map.1 hy' in if hxa : x = a then by simp [hxa] else if hxy : x = y then mem_cons_of_mem _ $ by rwa hxy else mem_cons_of_mem _ $ mem_of_mem_perms_of_list hg₁ $ by rw [eq_inv_mul_iff_mul_eq.2 hg₂, mul_apply, swap_inv, swap_apply_def]; split_ifs; cc) lemma mem_perms_of_list_iff {l : list α} {f : perm α} : f ∈ perms_of_list l ↔ ∀ {x}, f x ≠ x → x ∈ l := ⟨mem_of_mem_perms_of_list, mem_perms_of_list_of_mem⟩ lemma nodup_perms_of_list : ∀ {l : list α} (hl : l.nodup), (perms_of_list l).nodup | [] hl := by simp [perms_of_list] | (a::l) hl := have hl' : l.nodup, from nodup_of_nodup_cons hl, have hln' : (perms_of_list l).nodup, from nodup_perms_of_list hl', have hmeml : ∀ {f : perm α}, f ∈ perms_of_list l → f a = a, from λ f hf, not_not.1 (mt (mem_of_mem_perms_of_list hf) (nodup_cons.1 hl).1), by rw [perms_of_list, list.nodup_append, list.nodup_bind, pairwise_iff_nth_le]; exact ⟨hln', ⟨λ _ _, nodup_map (λ _ _, (mul_right_inj _).1) hln', λ i j hj hij x hx₁ hx₂, let ⟨f, hf⟩ := list.mem_map.1 hx₁ in let ⟨g, hg⟩ := list.mem_map.1 hx₂ in have hix : x a = nth_le l i (lt_trans hij hj), by rw [← hf.2, mul_apply, hmeml hf.1, swap_apply_left], have hiy : x a = nth_le l j hj, by rw [← hg.2, mul_apply, hmeml hg.1, swap_apply_left], absurd (hf.2.trans (hg.2.symm)) $ λ h, ne_of_lt hij $ nodup_iff_nth_le_inj.1 hl' i j (lt_trans hij hj) hj $ by rw [← hix, hiy]⟩, λ f hf₁ hf₂, let ⟨x, hx, hx'⟩ := list.mem_bind.1 hf₂ in let ⟨g, hg⟩ := list.mem_map.1 hx' in have hgxa : g⁻¹ x = a, from f.injective $ by rw [hmeml hf₁, ← hg.2]; simp, have hxa : x ≠ a, from λ h, (list.nodup_cons.1 hl).1 (h ▸ hx), (list.nodup_cons.1 hl).1 $ hgxa ▸ mem_of_mem_perms_of_list hg.1 (by rwa [apply_inv_self, hgxa])⟩ def perms_of_finset (s : finset α) : finset (perm α) := quotient.hrec_on s.1 (λ l hl, ⟨perms_of_list l, nodup_perms_of_list hl⟩) (λ a b hab, hfunext (congr_arg _ (quotient.sound hab)) (λ ha hb _, heq_of_eq $ finset.ext $ 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.fact := by rintros ⟨⟨l⟩, hs⟩; exact length_perms_of_list l def fintype_perm [fintype α] : fintype (perm α) := ⟨perms_of_finset (@finset.univ α _), by simp [mem_perms_of_finset_iff]⟩ instance [fintype α] [fintype β] : fintype (α ≃ β) := if h : fintype.card β = fintype.card α then trunc.rec_on_subsingleton (fintype.equiv_fin α) (λ eα, trunc.rec_on_subsingleton (fintype.equiv_fin β) (λ eβ, @fintype.of_equiv _ (perm α) fintype_perm (equiv_congr (equiv.refl α) (eα.trans (eq.rec_on h eβ.symm)) : (α ≃ α) ≃ (α ≃ β)))) else ⟨∅, λ x, false.elim (h (fintype.card_eq.2 ⟨x.symm⟩))⟩ lemma fintype.card_perm [fintype α] : fintype.card (perm α) = (fintype.card α).fact := subsingleton.elim (@fintype_perm α _ _) (@equiv.fintype α α _ _ _ _) ▸ card_perms_of_finset _ lemma fintype.card_equiv [fintype α] [fintype β] (e : α ≃ β) : fintype.card (α ≃ β) = (fintype.card α).fact := fintype.card_congr (equiv_congr (equiv.refl α) e) ▸ fintype.card_perm lemma univ_eq_singleton_of_card_one {α} [fintype α] (x : α) (h : fintype.card α = 1) : (univ : finset α) = {x} := begin apply symm, apply eq_of_subset_of_card_le (subset_univ ({x})), apply le_of_eq, simp [h, finset.card_univ] end end equiv namespace fintype section choose open fintype open equiv variables [fintype α] [decidable_eq α] (p : α → Prop) [decidable_pred p] def choose_x (hp : ∃! a : α, p a) : {a // p a} := ⟨finset.choose p univ (by simp; exact hp), finset.choose_property _ _ _⟩ def choose (hp : ∃! a, p a) : α := choose_x p hp lemma choose_spec (hp : ∃! a, p a) : p (choose p hp) := (choose_x p hp).property end choose section bijection_inverse open function variables [fintype α] [decidable_eq α] variables [fintype β] [decidable_eq β] variables {f : α → β} /-- ` `bij_inv f` is the unique inverse to a bijection `f`. This acts as a computable alternative to `function.inv_fun`. -/ def bij_inv (f_bij : bijective f) (b : β) : α := fintype.choose (λ a, f a = b) begin rcases f_bij.right b with ⟨a', fa_eq_b⟩, rw ← fa_eq_b, exact ⟨a', ⟨rfl, (λ a h, f_bij.left h)⟩⟩ end lemma left_inverse_bij_inv (f_bij : bijective f) : left_inverse (bij_inv f_bij) f := λ a, f_bij.left (choose_spec (λ a', f a' = f a) _) lemma right_inverse_bij_inv (f_bij : bijective f) : right_inverse (bij_inv f_bij) f := λ b, choose_spec (λ a', f a' = b) _ lemma bijective_bij_inv (f_bij : bijective f) : bijective (bij_inv f_bij) := ⟨(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 class infinite (α : Type*) : Prop := (not_fintype : fintype α → false) @[simp] lemma not_nonempty_fintype {α : Type*} : ¬nonempty (fintype α) ↔ infinite α := ⟨λf, ⟨λ x, f ⟨x⟩⟩, λ⟨f⟩ ⟨x⟩, f x⟩ namespace infinite lemma exists_not_mem_finset [infinite α] (s : finset α) : ∃ x, x ∉ s := classical.not_forall.1 $ λ h, not_fintype ⟨s, h⟩ @[priority 100] -- see Note [lower instance priority] instance nonempty (α : Type*) [infinite α] : nonempty α := nonempty_of_exists (exists_not_mem_finset (∅ : finset α)) lemma of_injective [infinite β] (f : β → α) (hf : injective f) : infinite α := ⟨λ I, by exactI not_fintype (fintype.of_injective f hf)⟩ lemma of_surjective [infinite β] (f : α → β) (hf : surjective f) : infinite α := ⟨λ I, by classical; exactI not_fintype (fintype.of_surjective f hf)⟩ private noncomputable def nat_embedding_aux (α : Type*) [infinite α] : ℕ → α | n := by letI := classical.dec_eq α; exact classical.some (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux m) (λ _, multiset.mem_range.1)).to_finset) private lemma nat_embedding_aux_injective (α : Type*) [infinite α] : function.injective (nat_embedding_aux α) := begin assume m n h, letI := classical.dec_eq α, wlog hmlen : m ≤ n using m n, by_contradiction hmn, have hmn : m < n, from lt_of_le_of_ne hmlen hmn, refine (classical.some_spec (exists_not_mem_finset ((multiset.range n).pmap (λ m (hm : m < n), nat_embedding_aux α m) (λ _, multiset.mem_range.1)).to_finset)) _, refine multiset.mem_to_finset.2 (multiset.mem_pmap.2 ⟨m, multiset.mem_range.2 hmn, _⟩), rw [h, nat_embedding_aux] end noncomputable def nat_embedding (α : Type*) [infinite α] : ℕ ↪ α := ⟨_, nat_embedding_aux_injective α⟩ end infinite lemma not_injective_infinite_fintype [infinite α] [fintype β] (f : α → β) : ¬ injective f := assume (hf : injective f), have H : fintype α := fintype.of_injective f hf, infinite.not_fintype H lemma not_surjective_fintype_infinite [fintype α] [infinite β] (f : α → β) : ¬ surjective f := assume (hf : surjective f), have H : infinite α := infinite.of_surjective f hf, @infinite.not_fintype _ H infer_instance instance nat.infinite : infinite ℕ := ⟨λ ⟨s, hs⟩, finset.not_mem_range_self $ s.subset_range_sup_succ (hs _)⟩ instance int.infinite : infinite ℤ := infinite.of_injective int.of_nat (λ _ _, int.of_nat.inj) 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
8a4be5793a777e800dfd247bf02b7f2298ad3e36
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/control/bitraversable/lemmas.lean
b2f76e42b630a818b1432ec34ea8ba39a596364f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,434
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 control.bitraversable.basic /-! # Bitraversable Lemmas > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions * tfst - traverse on first functor argument * tsnd - traverse on second functor argument ## Lemmas Combination of * bitraverse * tfst * tsnd with the applicatives `id` and `comp` ## References * Hackage: <https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html> ## Tags traversable bitraversable functor bifunctor applicative -/ universes u variables {t : Type u → Type u → Type u} [bitraversable t] variables {β : Type u} namespace bitraversable open functor is_lawful_applicative variables {F G : Type u → Type u} [applicative F] [applicative G] /-- traverse on the first functor argument -/ @[reducible] def tfst {α α'} (f : α → F α') : t α β → F (t α' β) := bitraverse f pure /-- traverse on the second functor argument -/ @[reducible] def tsnd {α α'} (f : α → F α') : t β α → F (t β α') := bitraverse pure f variables [is_lawful_bitraversable t] [is_lawful_applicative F] [is_lawful_applicative G] @[higher_order tfst_id] lemma id_tfst : Π {α β} (x : t α β), tfst id.mk x = id.mk x := @id_bitraverse _ _ _ @[higher_order tsnd_id] lemma id_tsnd : Π {α β} (x : t α β), tsnd id.mk x = id.mk x := @id_bitraverse _ _ _ @[higher_order tfst_comp_tfst] lemma comp_tfst {α₀ α₁ α₂ β} (f : α₀ → F α₁) (f' : α₁ → G α₂) (x : t α₀ β) : comp.mk (tfst f' <$> tfst f x) = tfst (comp.mk ∘ map f' ∘ f) x := by rw ← comp_bitraverse; simp [tfst,map_comp_pure,has_pure.pure] @[higher_order tfst_comp_tsnd] lemma tfst_tsnd {α₀ α₁ β₀ β₁} (f : α₀ → F α₁) (f' : β₀ → G β₁) (x : t α₀ β₀) : comp.mk (tfst f <$> tsnd f' x) = bitraverse (comp.mk ∘ pure ∘ f) (comp.mk ∘ map pure ∘ f') x := by rw ← comp_bitraverse; simp [tfst,tsnd] @[higher_order tsnd_comp_tfst] lemma tsnd_tfst {α₀ α₁ β₀ β₁} (f : α₀ → F α₁) (f' : β₀ → G β₁) (x : t α₀ β₀) : comp.mk (tsnd f' <$> tfst f x) = bitraverse (comp.mk ∘ map pure ∘ f) (comp.mk ∘ pure ∘ f') x := by rw ← comp_bitraverse; simp [tfst,tsnd] @[higher_order tsnd_comp_tsnd] lemma comp_tsnd {α β₀ β₁ β₂} (g : β₀ → F β₁) (g' : β₁ → G β₂) (x : t α β₀) : comp.mk (tsnd g' <$> tsnd g x) = tsnd (comp.mk ∘ map g' ∘ g) x := by rw ← comp_bitraverse; simp [tsnd]; refl open bifunctor private lemma pure_eq_id_mk_comp_id {α} : pure = id.mk ∘ @id α := rfl open function @[higher_order] lemma tfst_eq_fst_id {α α' β} (f : α → α') (x : t α β) : tfst (id.mk ∘ f) x = id.mk (fst f x) := by simp [tfst,fst,pure_eq_id_mk_comp_id,-comp.right_id,bitraverse_eq_bimap_id] @[higher_order] lemma tsnd_eq_snd_id {α β β'} (f : β → β') (x : t α β) : tsnd (id.mk ∘ f) x = id.mk (snd f x) := by simp [tsnd,snd,pure_eq_id_mk_comp_id,-comp.right_id,bitraverse_eq_bimap_id] attribute [functor_norm] comp_bitraverse comp_tsnd comp_tfst tsnd_comp_tsnd tsnd_comp_tfst tfst_comp_tsnd tfst_comp_tfst bitraverse_comp bitraverse_id_id tfst_id tsnd_id end bitraversable
020ecf9712ad382751377f3747979d648afa72e1
e030b0259b777fedcdf73dd966f3f1556d392178
/library/init/meta/decl_cmds.lean
2f086fd7ec2f2e7c530eb16aa054e4f48d881911
[ "Apache-2.0" ]
permissive
fgdorais/lean
17b46a095b70b21fa0790ce74876658dc5faca06
c3b7c54d7cca7aaa25328f0a5660b6b75fe26055
refs/heads/master
1,611,523,590,686
1,484,412,902,000
1,484,412,902,000
38,489,734
0
0
null
1,435,923,380,000
1,435,923,379,000
null
UTF-8
Lean
false
false
1,679
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.meta.rb_map open tactic /- Given a set of constant renamings `replacements` and a declaration name `src_decl_name`, create a new declaration called `new_decl_name` s.t. its type is the type of `src_decl_name` after applying the given constant replacement. Remark: the new type must be definitionally equal to the type of `src_decl_name`. Example: Assume the environment contains def f : nat -> nat := ... def g : nat -> nat := f lemma f_lemma : forall a, f a > 0 := ... Moreover, assume we have a mapping M containing `f -> `g Then, the command run_command copy_decl_updating_type M `f_lemma `g_lemma creates the declaration lemma g_lemma : forall a, g a > 0 := ... -/ meta def copy_decl_updating_type (replacements : name_map name) (src_decl_name : name) (new_decl_name : name) : command := do env ← get_env, decl ← returnex $ env^.get src_decl_name, new_type ← return $ decl^.type^.replace (λ e d, match e with | expr.const n ls := match replacements^.find n with | some new_n := some (expr.const new_n ls) | none := none end | _ := none end), new_value ← return $ expr.const src_decl_name (decl^.univ_params^.for level.param), add_decl (((decl^.to_definition^.update_type new_type)^.update_name new_decl_name)^.update_value new_value), return ()
b53d8503b6830d98b8d96dbdbda26683ed8975fd
7da5ceac20aaab989eeb795a4be9639982e7b35a
/src/category_theory/finite_limits.lean
da2ebc24b379b12344663e56ec8ffd77d687ef54
[ "MIT" ]
permissive
formalabstracts/formalabstracts
46c2f1b3a172e62ca6ffeb46fbbdf1705718af49
b0173da1af45421239d44492eeecd54bf65ee0f6
refs/heads/master
1,606,896,370,374
1,572,988,776,000
1,572,988,776,000
96,763,004
165
28
null
1,555,709,319,000
1,499,680,948,000
Lean
UTF-8
Lean
false
false
9,823
lean
-- Copyright (c) 2018 Jesse Han. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Jesse Han import .limits.shapes.products basic data.dvector .limits.shapes.equalizers category_theory.limits.limits universes v u open category_theory namespace category_theory.limits @[derive decidable_eq] inductive two : Type u | left | right def two.map {C : Sort*} (X Y : C) : two → C | two.left := X | two.right := Y def two.functor {C : Sort u} (X Y : C) [category.{v+1} C] : discrete two ⥤ C := functor.of_function (two.map X Y) def empty.functor (C : Sort*) [category.{v+1} C] : discrete pempty ⥤ C := functor.of_function (λ x, by {cases x} : pempty → C) def empty_cone {C : Sort u} [category.{v+1} C] (A : C) : limits.cone (empty.functor C) := { X := A, π := { app := λ x, by cases x, naturality' := by tidy}} def commutative_square {C : Sort u} [category.{v u} C] {A B A' B' : C} (f_top : A ⟶ B) (d_left : A ⟶ A') (d_right : B ⟶ B') (f_bot : A' ⟶ B') := f_top ≫ d_right = d_left ≫ f_bot variables {C : Type u} [𝒞 : category.{v+1} C] include 𝒞 variable(C) @[class] def has_binary_products := has_limits_of_shape (discrete two.{v}) C @[class] def has_terminal_object : Sort* := has_limits_of_shape.{v} (discrete pempty) C @[class] def has_binary_coproducts := has_colimits_of_shape (discrete two.{v}) C @[class] def has_initial_object : Sort* := has_colimits_of_shape.{v} (discrete pempty) C @[instance] def has_limit_two_of_has_binary_products [H : has_binary_products C] {X Y : C} : has_limit $ two.functor X Y := @has_limits_of_shape.has_limit _ _ _ _ H (two.functor X Y) @[instance] def has_limit_empty_of_has_terminal_object [H : has_terminal_object C] : has_limit $ empty.functor C := @has_limits_of_shape.has_limit _ _ _ _ H (empty.functor C) variable{C} def has_terminal_object.mk (T : C) (h₁ : ∀(X : C), X ⟶ T) (h₂ : ∀{{X : C}} (f g : X ⟶ T), f = g) : has_terminal_object C := ⟨λ F, { cone := ⟨T, ⟨pempty.rec _, pempty.rec _⟩⟩, is_limit := { lift := λ s, h₁ s.X, fac' := λ s, pempty.rec _, uniq' := λ s m h, h₂ _ _ } }⟩ def has_binary_products.mk (m : C → C → C) (p1 : ∀{X Y : C}, m X Y ⟶ X) (p2 : ∀{X Y : C}, m X Y ⟶ Y) (lft : ∀{{X Y Z : C}} (f : Z ⟶ X) (g : Z ⟶ Y), Z ⟶ m X Y) (lft1 : ∀{{X Y Z : C}} (f : Z ⟶ X) (g : Z ⟶ Y), lft f g ≫ p1 = f) (lft2 : ∀{{X Y Z : C}} (f : Z ⟶ X) (g : Z ⟶ Y), lft f g ≫ p2 = g) (lft_unique : ∀{{X Y Z : C}} (f g : Z ⟶ m X Y) (h1 : f ≫ p1 = g ≫ p1) (h2 : f ≫ p2 = g ≫ p2), f = g) : has_binary_products C := begin constructor, intro F, fsplit, { use m (F.obj two.left) (F.obj two.right), apply nat_trans.of_homs, refine two.rec _ _, exact p1, exact p2 }, refine limits.is_limit.mk _ _ _, { rintro ⟨X, f⟩, apply lft (f.app two.left), dsimp, exact f.app two.right }, { rintro ⟨X, f⟩ (_|_), apply lft1, apply lft2 }, { rintro ⟨X, f⟩ g h, dsimp, apply lft_unique, rw [lft1], exact h two.left, rw [lft2], exact h two.right } end def has_initial_object.mk (I : C) (h₁ : ∀(X : C), I ⟶ X) (h₂ : ∀{{X : C}} (f g : I ⟶ X), f = g) : has_initial_object C := ⟨λ F, { cocone := ⟨I, ⟨pempty.rec _, pempty.rec _⟩⟩, is_colimit := { desc := λ s, h₁ s.X, fac' := λ s, pempty.rec _, uniq' := λ s m h, h₂ _ _ } }⟩ def has_binary_coproducts.mk (p : C → C → C) (i1 : ∀{X Y : C}, X ⟶ p X Y) (i2 : ∀{X Y : C}, Y ⟶ p X Y) (dsc : ∀{{X Y Z : C}} (f : X ⟶ Z) (g : Y ⟶ Z), p X Y ⟶ Z) (dsc1 : ∀{{X Y Z : C}} (f : X ⟶ Z) (g : Y ⟶ Z), i1 ≫ dsc f g = f) (dsc2 : ∀{{X Y Z : C}} (f : X ⟶ Z) (g : Y ⟶ Z), i2 ≫ dsc f g = g) (dsc_unique : ∀{{X Y Z : C}} (f g : p X Y ⟶ Z) (h1 : i1 ≫ f = i1 ≫ g) (h2 : i2 ≫ f = i2 ≫ g), f = g) : has_binary_coproducts C := begin constructor, intro F, fsplit, { use p (F.obj two.left) (F.obj two.right), apply nat_trans.of_homs, refine two.rec _ _, exact i1, exact i2 }, refine limits.is_colimit.mk _ _ _, { rintro ⟨X, f⟩, apply dsc (f.app two.left), dsimp, exact f.app two.right }, { rintro ⟨X, f⟩ (_|_), apply dsc1, apply dsc2 }, { rintro ⟨X, f⟩ g h, dsimp, apply dsc_unique, rw [dsc1], exact h two.left, rw [dsc2], exact h two.right } end /-- The binary product is the vertex of the limiting cone to the canonical functor two → 𝒞 associated to X and Y -/ def binary_product (X Y : C) [has_limit $ two.functor X Y] : C := limit (two.functor X Y) namespace binary_product local infix ` × `:60 := binary_product def π₁ {X Y : C} [has_limit $ two.functor X Y] : X × Y ⟶ X := limit.π _ two.left def π₂ {X Y : C} [has_limit $ two.functor X Y] : X × Y ⟶ Y := limit.π _ two.right /-- An alternative version of `π₁` if type-class inference fails -/ def π₁' {X Y : C} {H : has_binary_products C} : X × Y ⟶ X := π₁ /-- An alternative version of `π₂` if type-class inference fails -/ def π₂' {X Y : C} {H : has_binary_products C} : X × Y ⟶ Y := π₂ def dfin.map {n : ℕ} : dvector C n → dfin n → C := λ v d, by {induction v, cases d, cases d, exact v_x, exact v_ih d_a} example {X : C} [has_binary_products C] : X × X × X = (X × X) × X := by refl def cone_of_two_maps {W A₁ A₂: C} (f₁ : W ⟶ A₁) (f₂ : W ⟶ A₂) : cone (two.functor A₁ A₂) := { X := W, π := { app := λ l, two.rec_on l f₁ f₂, naturality' := by tidy}} lemma cone_of_two_maps_object [has_binary_products C] {B₁ B₂ A₁ A₂: C} {f₁ : B₁ × B₂ ⟶ A₁} {f₂ : B₁ × B₂ ⟶ A₂} : (cone_of_two_maps f₁ f₂).X = B₁ × B₂ := by refl def map_to_product.mk {H : has_binary_products C} {W B₁ B₂ : C} (f₁ : W ⟶ B₁) (f₂ : W ⟶ B₂) : W ⟶ B₁ × B₂ := is_limit.lift (limit.is_limit _) (cone_of_two_maps f₁ f₂) def diag [H : has_binary_products C] {B : C} : B ⟶ B × B := map_to_product.mk (𝟙 B) (𝟙 B) protected def map {H : has_binary_products C} {A A' B B' : C} (f : A ⟶ A') (g : B ⟶ B') : A × B ⟶ A' × B' := map_to_product.mk (π₁ ≫ f) (π₂ ≫ g) local infix ` ×.map `:90 := binary_product.map protected def iso {H : has_binary_products C} {A A' B B' : C} (f : A ≅ A') (g : B ≅ B') : A × B ≅ A' × B' := { hom := f.hom ×.map g.hom, inv := f.inv ×.map g.inv, hom_inv_id' := omitted, inv_hom_id' := omitted } local infix ` ×.iso `:90 := binary_product.iso def assoc_hom {H : has_binary_products C} {X Y Z : C} : (X × Y) × Z ⟶ X × (Y × Z) := by apply map_to_product.mk (π₁ ≫ π₁) (π₂ ×.map (𝟙 Z)) def assoc_inv {H : has_binary_products C} {X Y Z : C} : X × (Y × Z) ⟶ (X × Y) × Z := by apply map_to_product.mk (𝟙 X ×.map π₁) (π₂ ≫ π₂) def product_assoc {H : has_binary_products C} {X Y Z : C} : (X × Y) × Z ≅ X × (Y × Z) := { hom := assoc_hom, inv := assoc_inv, hom_inv_id' := omitted, inv_hom_id' := omitted} def product_comm {H : has_binary_products C} {X Y : C} : X × Y ≅ Y × X := { hom := map_to_product.mk π₂ π₁, inv := map_to_product.mk π₂ π₁, hom_inv_id' := omitted, inv_hom_id' := omitted} def product_assoc4 {H : has_binary_products C} {X Y Z W : C} : (X × Y) × (Z × W) ≅ (X × Z) × (Y × W) := product_assoc ≪≫ iso.refl X ×.iso (product_assoc.symm ≪≫ product_comm ×.iso iso.refl W ≪≫ product_assoc) ≪≫ product_assoc.symm example : commutative_square /-unit-/ (𝟙 unit) /- unit -/ (𝟙 unit) (𝟙 unit) /-unit-/ (𝟙 unit) /- unit -/ := by tidy end binary_product open binary_product section terminal_object local infix ` × `:60 := binary_product def terminal_object [has_terminal_object C] : C := limit (empty.functor C) notation `term` := terminal_object def terminal_map [has_terminal_object C] (A : C) : A ⟶ term := is_limit.lift (limit.is_limit (empty.functor C)) (empty_cone A) lemma terminal_map_eq [has_terminal_object C] {A : C} (f g : A ⟶ term) : f = g := omitted lemma mul_one [has_terminal_object C] [has_binary_products C] (G : C) : nonempty $ term × G ≅ G := omitted lemma one_mul [has_terminal_object C] [has_binary_products C] (G : C) : nonempty $ G × term ≅ G := omitted def mul_one_inv [has_terminal_object C] [has_binary_products C] {G : C} : G ⟶ G × term := by apply map_to_product.mk (𝟙 _) (terminal_map G) def one_mul_inv [has_terminal_object C] [has_binary_products C] {G : C} : G ⟶ term × G := by apply map_to_product.mk (terminal_map G) (𝟙 _) end terminal_object section pow local infix ` × `:60 := binary_product /-- The n-fold product of an object with itself -/ def category.pow [has_binary_products C] [has_terminal_object C] (X : C) : ℕ → C | 0 := term | 1 := X | (n+2) := X × category.pow (n+1) end pow namespace finite_limits open binary_product instance fintype_two : fintype two := {elems := { val := ⟦[two.left, two.right]⟧, nodup := by tidy }, complete := λ x, by cases x; tidy} example : fintype pempty := by apply_instance section finite_products variable (C) @[class]def has_finite_products := Π α : Type*, fintype α → has_limits_of_shape.{v} (discrete α) C @[class]def has_equalizers := has_limits_of_shape.{v} (walking_pair) C @[instance] def has_binary_products_of_has_finite_products [H : has_finite_products C] : has_binary_products C := H _ infer_instance @[instance] def has_terminal_object_of_has_finite_products [H : has_finite_products C] : has_limits_of_shape.{v} (discrete pempty) C := H _ infer_instance @[class]def has_finite_limits := @has_finite_products C 𝒞 × @has_equalizers C 𝒞 end finite_products end finite_limits end category_theory.limits
7cd532e744dea0088dcda9d833c005c4e8605c30
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/1845.lean
1293657f94a786a9bc9feb7f0140d81597297a18
[ "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
362
lean
import Lean.Hygiene import Lean.Exception open Lean def bar : StateT Nat Unhygienic Syntax.Term := do modify (· + 1); `("hi") def foo : StateT Nat Unhygienic Syntax.Term := do `(throwError $(← bar)) #eval Unhygienic.run (foo.run 0) |>.2 -- don't do this syntax "←" term : term def foo' : StateT Nat Unhygienic Syntax.Term := do `(throwError $(← bar))
3d2e43a06a7315932cc6c7df29e19f825d409ad6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/convex/side.lean
b4f12bc9539d7f33951c065cfe4d044d6cd3fc41
[ "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
39,397
lean
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import analysis.convex.between import analysis.convex.normed import analysis.normed.group.add_torsor /-! # Sides of affine subspaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines notions of two points being on the same or opposite sides of an affine subspace. ## Main definitions * `s.w_same_side x y`: The points `x` and `y` are weakly on the same side of the affine subspace `s`. * `s.s_same_side x y`: The points `x` and `y` are strictly on the same side of the affine subspace `s`. * `s.w_opp_side x y`: The points `x` and `y` are weakly on opposite sides of the affine subspace `s`. * `s.s_opp_side x y`: The points `x` and `y` are strictly on opposite sides of the affine subspace `s`. -/ variables {R V V' P P' : Type*} open affine_equiv affine_map namespace affine_subspace section strict_ordered_comm_ring variables [strict_ordered_comm_ring R] [add_comm_group V] [module R V] [add_torsor V P] variables [add_comm_group V'] [module R V'] [add_torsor V' P'] include V /-- The points `x` and `y` are weakly on the same side of `s`. -/ def w_same_side (s : affine_subspace R P) (x y : P) : Prop := ∃ p₁ p₂ ∈ s, same_ray R (x -ᵥ p₁) (y -ᵥ p₂) /-- The points `x` and `y` are strictly on the same side of `s`. -/ def s_same_side (s : affine_subspace R P) (x y : P) : Prop := s.w_same_side x y ∧ x ∉ s ∧ y ∉ s /-- The points `x` and `y` are weakly on opposite sides of `s`. -/ def w_opp_side (s : affine_subspace R P) (x y : P) : Prop := ∃ p₁ p₂ ∈ s, same_ray R (x -ᵥ p₁) (p₂ -ᵥ y) /-- The points `x` and `y` are strictly on opposite sides of `s`. -/ def s_opp_side (s : affine_subspace R P) (x y : P) : Prop := s.w_opp_side x y ∧ x ∉ s ∧ y ∉ s include V' lemma w_same_side.map {s : affine_subspace R P} {x y : P} (h : s.w_same_side x y) (f : P →ᵃ[R] P') : (s.map f).w_same_side (f x) (f y) := begin rcases h with ⟨p₁, hp₁, p₂, hp₂, h⟩, refine ⟨f p₁, mem_map_of_mem f hp₁, f p₂, mem_map_of_mem f hp₂, _⟩, simp_rw [←linear_map_vsub], exact h.map f.linear end lemma _root_.function.injective.w_same_side_map_iff {s : affine_subspace R P} {x y : P} {f : P →ᵃ[R] P'} (hf : function.injective f) : (s.map f).w_same_side (f x) (f y) ↔ s.w_same_side x y := begin refine ⟨λ h, _, λ h, h.map _⟩, rcases h with ⟨fp₁, hfp₁, fp₂, hfp₂, h⟩, rw mem_map at hfp₁ hfp₂, rcases hfp₁ with ⟨p₁, hp₁, rfl⟩, rcases hfp₂ with ⟨p₂, hp₂, rfl⟩, refine ⟨p₁, hp₁, p₂, hp₂, _⟩, simp_rw [←linear_map_vsub, (f.linear_injective_iff.2 hf).same_ray_map_iff] at h, exact h end lemma _root_.function.injective.s_same_side_map_iff {s : affine_subspace R P} {x y : P} {f : P →ᵃ[R] P'} (hf : function.injective f) : (s.map f).s_same_side (f x) (f y) ↔ s.s_same_side x y := by simp_rw [s_same_side, hf.w_same_side_map_iff, mem_map_iff_mem_of_injective hf] @[simp] lemma _root_.affine_equiv.w_same_side_map_iff {s : affine_subspace R P} {x y : P} (f : P ≃ᵃ[R] P') : (s.map ↑f).w_same_side (f x) (f y) ↔ s.w_same_side x y := (show function.injective f.to_affine_map, from f.injective).w_same_side_map_iff @[simp] lemma _root_.affine_equiv.s_same_side_map_iff {s : affine_subspace R P} {x y : P} (f : P ≃ᵃ[R] P') : (s.map ↑f).s_same_side (f x) (f y) ↔ s.s_same_side x y := (show function.injective f.to_affine_map, from f.injective).s_same_side_map_iff lemma w_opp_side.map {s : affine_subspace R P} {x y : P} (h : s.w_opp_side x y) (f : P →ᵃ[R] P') : (s.map f).w_opp_side (f x) (f y) := begin rcases h with ⟨p₁, hp₁, p₂, hp₂, h⟩, refine ⟨f p₁, mem_map_of_mem f hp₁, f p₂, mem_map_of_mem f hp₂, _⟩, simp_rw [←linear_map_vsub], exact h.map f.linear end lemma _root_.function.injective.w_opp_side_map_iff {s : affine_subspace R P} {x y : P} {f : P →ᵃ[R] P'} (hf : function.injective f) : (s.map f).w_opp_side (f x) (f y) ↔ s.w_opp_side x y := begin refine ⟨λ h, _, λ h, h.map _⟩, rcases h with ⟨fp₁, hfp₁, fp₂, hfp₂, h⟩, rw mem_map at hfp₁ hfp₂, rcases hfp₁ with ⟨p₁, hp₁, rfl⟩, rcases hfp₂ with ⟨p₂, hp₂, rfl⟩, refine ⟨p₁, hp₁, p₂, hp₂, _⟩, simp_rw [←linear_map_vsub, (f.linear_injective_iff.2 hf).same_ray_map_iff] at h, exact h end lemma _root_.function.injective.s_opp_side_map_iff {s : affine_subspace R P} {x y : P} {f : P →ᵃ[R] P'} (hf : function.injective f) : (s.map f).s_opp_side (f x) (f y) ↔ s.s_opp_side x y := by simp_rw [s_opp_side, hf.w_opp_side_map_iff, mem_map_iff_mem_of_injective hf] @[simp] lemma _root_.affine_equiv.w_opp_side_map_iff {s : affine_subspace R P} {x y : P} (f : P ≃ᵃ[R] P') : (s.map ↑f).w_opp_side (f x) (f y) ↔ s.w_opp_side x y := (show function.injective f.to_affine_map, from f.injective).w_opp_side_map_iff @[simp] lemma _root_.affine_equiv.s_opp_side_map_iff {s : affine_subspace R P} {x y : P} (f : P ≃ᵃ[R] P') : (s.map ↑f).s_opp_side (f x) (f y) ↔ s.s_opp_side x y := (show function.injective f.to_affine_map, from f.injective).s_opp_side_map_iff omit V' lemma w_same_side.nonempty {s : affine_subspace R P} {x y : P} (h : s.w_same_side x y) : (s : set P).nonempty := ⟨h.some, h.some_spec.some⟩ lemma s_same_side.nonempty {s : affine_subspace R P} {x y : P} (h : s.s_same_side x y) : (s : set P).nonempty := ⟨h.1.some, h.1.some_spec.some⟩ lemma w_opp_side.nonempty {s : affine_subspace R P} {x y : P} (h : s.w_opp_side x y) : (s : set P).nonempty := ⟨h.some, h.some_spec.some⟩ lemma s_opp_side.nonempty {s : affine_subspace R P} {x y : P} (h : s.s_opp_side x y) : (s : set P).nonempty := ⟨h.1.some, h.1.some_spec.some⟩ lemma s_same_side.w_same_side {s : affine_subspace R P} {x y : P} (h : s.s_same_side x y) : s.w_same_side x y := h.1 lemma s_same_side.left_not_mem {s : affine_subspace R P} {x y : P} (h : s.s_same_side x y) : x ∉ s := h.2.1 lemma s_same_side.right_not_mem {s : affine_subspace R P} {x y : P} (h : s.s_same_side x y) : y ∉ s := h.2.2 lemma s_opp_side.w_opp_side {s : affine_subspace R P} {x y : P} (h : s.s_opp_side x y) : s.w_opp_side x y := h.1 lemma s_opp_side.left_not_mem {s : affine_subspace R P} {x y : P} (h : s.s_opp_side x y) : x ∉ s := h.2.1 lemma s_opp_side.right_not_mem {s : affine_subspace R P} {x y : P} (h : s.s_opp_side x y) : y ∉ s := h.2.2 lemma w_same_side_comm {s : affine_subspace R P} {x y : P} : s.w_same_side x y ↔ s.w_same_side y x := ⟨λ ⟨p₁, hp₁, p₂, hp₂, h⟩, ⟨p₂, hp₂, p₁, hp₁, h.symm⟩, λ ⟨p₁, hp₁, p₂, hp₂, h⟩, ⟨p₂, hp₂, p₁, hp₁, h.symm⟩⟩ alias w_same_side_comm ↔ w_same_side.symm _ lemma s_same_side_comm {s : affine_subspace R P} {x y : P} : s.s_same_side x y ↔ s.s_same_side y x := by rw [s_same_side, s_same_side, w_same_side_comm, and_comm (x ∉ s)] alias s_same_side_comm ↔ s_same_side.symm _ lemma w_opp_side_comm {s : affine_subspace R P} {x y : P} : s.w_opp_side x y ↔ s.w_opp_side y x := begin split, { rintro ⟨p₁, hp₁, p₂, hp₂, h⟩, refine ⟨p₂, hp₂, p₁, hp₁, _⟩, rwa [same_ray_comm, ←same_ray_neg_iff, neg_vsub_eq_vsub_rev, neg_vsub_eq_vsub_rev] }, { rintro ⟨p₁, hp₁, p₂, hp₂, h⟩, refine ⟨p₂, hp₂, p₁, hp₁, _⟩, rwa [same_ray_comm, ←same_ray_neg_iff, neg_vsub_eq_vsub_rev, neg_vsub_eq_vsub_rev] } end alias w_opp_side_comm ↔ w_opp_side.symm _ lemma s_opp_side_comm {s : affine_subspace R P} {x y : P} : s.s_opp_side x y ↔ s.s_opp_side y x := by rw [s_opp_side, s_opp_side, w_opp_side_comm, and_comm (x ∉ s)] alias s_opp_side_comm ↔ s_opp_side.symm _ lemma not_w_same_side_bot (x y : P) : ¬ (⊥ : affine_subspace R P).w_same_side x y := by simp [w_same_side, not_mem_bot] lemma not_s_same_side_bot (x y : P) : ¬ (⊥ : affine_subspace R P).s_same_side x y := λ h, not_w_same_side_bot x y h.w_same_side lemma not_w_opp_side_bot (x y : P) : ¬ (⊥ : affine_subspace R P).w_opp_side x y := by simp [w_opp_side, not_mem_bot] lemma not_s_opp_side_bot (x y : P) : ¬ (⊥ : affine_subspace R P).s_opp_side x y := λ h, not_w_opp_side_bot x y h.w_opp_side @[simp] lemma w_same_side_self_iff {s : affine_subspace R P} {x : P} : s.w_same_side x x ↔ (s : set P).nonempty := ⟨λ h, h.nonempty, λ ⟨p, hp⟩, ⟨p, hp, p, hp, same_ray.rfl⟩⟩ lemma s_same_side_self_iff {s : affine_subspace R P} {x : P} : s.s_same_side x x ↔ (s : set P).nonempty ∧ x ∉ s := ⟨λ ⟨h, hx, _⟩, ⟨w_same_side_self_iff.1 h, hx⟩, λ ⟨h, hx⟩, ⟨w_same_side_self_iff.2 h, hx, hx⟩⟩ lemma w_same_side_of_left_mem {s : affine_subspace R P} {x : P} (y : P) (hx : x ∈ s) : s.w_same_side x y := begin refine ⟨x, hx, x, hx, _⟩, simp end lemma w_same_side_of_right_mem {s : affine_subspace R P} (x : P) {y : P} (hy : y ∈ s) : s.w_same_side x y := (w_same_side_of_left_mem x hy).symm lemma w_opp_side_of_left_mem {s : affine_subspace R P} {x : P} (y : P) (hx : x ∈ s) : s.w_opp_side x y := begin refine ⟨x, hx, x, hx, _⟩, simp end lemma w_opp_side_of_right_mem {s : affine_subspace R P} (x : P) {y : P} (hy : y ∈ s) : s.w_opp_side x y := (w_opp_side_of_left_mem x hy).symm lemma w_same_side_vadd_left_iff {s : affine_subspace R P} {x y : P} {v : V} (hv : v ∈ s.direction) : s.w_same_side (v +ᵥ x) y ↔ s.w_same_side x y := begin split, { rintro ⟨p₁, hp₁, p₂, hp₂, h⟩, refine ⟨-v +ᵥ p₁, affine_subspace.vadd_mem_of_mem_direction (submodule.neg_mem _ hv) hp₁, p₂, hp₂, _⟩, rwa [vsub_vadd_eq_vsub_sub, sub_neg_eq_add, add_comm, ←vadd_vsub_assoc] }, { rintro ⟨p₁, hp₁, p₂, hp₂, h⟩, refine ⟨v +ᵥ p₁, affine_subspace.vadd_mem_of_mem_direction hv hp₁, p₂, hp₂, _⟩, rwa vadd_vsub_vadd_cancel_left } end lemma w_same_side_vadd_right_iff {s : affine_subspace R P} {x y : P} {v : V} (hv : v ∈ s.direction) : s.w_same_side x (v +ᵥ y) ↔ s.w_same_side x y := by rw [w_same_side_comm, w_same_side_vadd_left_iff hv, w_same_side_comm] lemma s_same_side_vadd_left_iff {s : affine_subspace R P} {x y : P} {v : V} (hv : v ∈ s.direction) : s.s_same_side (v +ᵥ x) y ↔ s.s_same_side x y := by rw [s_same_side, s_same_side, w_same_side_vadd_left_iff hv, vadd_mem_iff_mem_of_mem_direction hv] lemma s_same_side_vadd_right_iff {s : affine_subspace R P} {x y : P} {v : V} (hv : v ∈ s.direction) : s.s_same_side x (v +ᵥ y) ↔ s.s_same_side x y := by rw [s_same_side_comm, s_same_side_vadd_left_iff hv, s_same_side_comm] lemma w_opp_side_vadd_left_iff {s : affine_subspace R P} {x y : P} {v : V} (hv : v ∈ s.direction) : s.w_opp_side (v +ᵥ x) y ↔ s.w_opp_side x y := begin split, { rintro ⟨p₁, hp₁, p₂, hp₂, h⟩, refine ⟨-v +ᵥ p₁, affine_subspace.vadd_mem_of_mem_direction (submodule.neg_mem _ hv) hp₁, p₂, hp₂, _⟩, rwa [vsub_vadd_eq_vsub_sub, sub_neg_eq_add, add_comm, ←vadd_vsub_assoc] }, { rintro ⟨p₁, hp₁, p₂, hp₂, h⟩, refine ⟨v +ᵥ p₁, affine_subspace.vadd_mem_of_mem_direction hv hp₁, p₂, hp₂, _⟩, rwa vadd_vsub_vadd_cancel_left } end lemma w_opp_side_vadd_right_iff {s : affine_subspace R P} {x y : P} {v : V} (hv : v ∈ s.direction) : s.w_opp_side x (v +ᵥ y) ↔ s.w_opp_side x y := by rw [w_opp_side_comm, w_opp_side_vadd_left_iff hv, w_opp_side_comm] lemma s_opp_side_vadd_left_iff {s : affine_subspace R P} {x y : P} {v : V} (hv : v ∈ s.direction) : s.s_opp_side (v +ᵥ x) y ↔ s.s_opp_side x y := by rw [s_opp_side, s_opp_side, w_opp_side_vadd_left_iff hv, vadd_mem_iff_mem_of_mem_direction hv] lemma s_opp_side_vadd_right_iff {s : affine_subspace R P} {x y : P} {v : V} (hv : v ∈ s.direction) : s.s_opp_side x (v +ᵥ y) ↔ s.s_opp_side x y := by rw [s_opp_side_comm, s_opp_side_vadd_left_iff hv, s_opp_side_comm] lemma w_same_side_smul_vsub_vadd_left {s : affine_subspace R P} {p₁ p₂ : P} (x : P) (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) {t : R} (ht : 0 ≤ t) : s.w_same_side (t • (x -ᵥ p₁) +ᵥ p₂) x := begin refine ⟨p₂, hp₂, p₁, hp₁, _⟩, rw vadd_vsub, exact same_ray_nonneg_smul_left _ ht end lemma w_same_side_smul_vsub_vadd_right {s : affine_subspace R P} {p₁ p₂ : P} (x : P) (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) {t : R} (ht : 0 ≤ t) : s.w_same_side x (t • (x -ᵥ p₁) +ᵥ p₂) := (w_same_side_smul_vsub_vadd_left x hp₁ hp₂ ht).symm lemma w_same_side_line_map_left {s : affine_subspace R P} {x : P} (y : P) (h : x ∈ s) {t : R} (ht : 0 ≤ t) : s.w_same_side (line_map x y t) y := w_same_side_smul_vsub_vadd_left y h h ht lemma w_same_side_line_map_right {s : affine_subspace R P} {x : P} (y : P) (h : x ∈ s) {t : R} (ht : 0 ≤ t) : s.w_same_side y (line_map x y t) := (w_same_side_line_map_left y h ht).symm lemma w_opp_side_smul_vsub_vadd_left {s : affine_subspace R P} {p₁ p₂ : P} (x : P) (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) {t : R} (ht : t ≤ 0) : s.w_opp_side (t • (x -ᵥ p₁) +ᵥ p₂) x := begin refine ⟨p₂, hp₂, p₁, hp₁, _⟩, rw [vadd_vsub, ←neg_neg t, neg_smul, ←smul_neg, neg_vsub_eq_vsub_rev], exact same_ray_nonneg_smul_left _ (neg_nonneg.2 ht) end lemma w_opp_side_smul_vsub_vadd_right {s : affine_subspace R P} {p₁ p₂ : P} (x : P) (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) {t : R} (ht : t ≤ 0) : s.w_opp_side x (t • (x -ᵥ p₁) +ᵥ p₂) := (w_opp_side_smul_vsub_vadd_left x hp₁ hp₂ ht).symm lemma w_opp_side_line_map_left {s : affine_subspace R P} {x : P} (y : P) (h : x ∈ s) {t : R} (ht : t ≤ 0) : s.w_opp_side (line_map x y t) y := w_opp_side_smul_vsub_vadd_left y h h ht lemma w_opp_side_line_map_right {s : affine_subspace R P} {x : P} (y : P) (h : x ∈ s) {t : R} (ht : t ≤ 0) : s.w_opp_side y (line_map x y t) := (w_opp_side_line_map_left y h ht).symm lemma _root_.wbtw.w_same_side₂₃ {s : affine_subspace R P} {x y z : P} (h : wbtw R x y z) (hx : x ∈ s) : s.w_same_side y z := begin rcases h with ⟨t, ⟨ht0, -⟩, rfl⟩, exact w_same_side_line_map_left z hx ht0 end lemma _root_.wbtw.w_same_side₃₂ {s : affine_subspace R P} {x y z : P} (h : wbtw R x y z) (hx : x ∈ s) : s.w_same_side z y := (h.w_same_side₂₃ hx).symm lemma _root_.wbtw.w_same_side₁₂ {s : affine_subspace R P} {x y z : P} (h : wbtw R x y z) (hz : z ∈ s) : s.w_same_side x y := h.symm.w_same_side₃₂ hz lemma _root_.wbtw.w_same_side₂₁ {s : affine_subspace R P} {x y z : P} (h : wbtw R x y z) (hz : z ∈ s) : s.w_same_side y x := h.symm.w_same_side₂₃ hz lemma _root_.wbtw.w_opp_side₁₃ {s : affine_subspace R P} {x y z : P} (h : wbtw R x y z) (hy : y ∈ s) : s.w_opp_side x z := begin rcases h with ⟨t, ⟨ht0, ht1⟩, rfl⟩, refine ⟨_, hy, _, hy, _⟩, rcases ht1.lt_or_eq with ht1' | rfl, swap, { simp }, rcases ht0.lt_or_eq with ht0' | rfl, swap, { simp }, refine or.inr (or.inr ⟨1 - t, t, sub_pos.2 ht1', ht0', _⟩), simp_rw [line_map_apply, vadd_vsub_assoc, vsub_vadd_eq_vsub_sub, ←neg_vsub_eq_vsub_rev z x, vsub_self, zero_sub, ←neg_one_smul R (z -ᵥ x), ←add_smul, smul_neg, ←neg_smul, smul_smul], ring_nf end lemma _root_.wbtw.w_opp_side₃₁ {s : affine_subspace R P} {x y z : P} (h : wbtw R x y z) (hy : y ∈ s) : s.w_opp_side z x := h.symm.w_opp_side₁₃ hy end strict_ordered_comm_ring section linear_ordered_field variables [linear_ordered_field R] [add_comm_group V] [module R V] [add_torsor V P] variables [add_comm_group V'] [module R V'] [add_torsor V' P'] include V variables {R} @[simp] lemma w_opp_side_self_iff {s : affine_subspace R P} {x : P} : s.w_opp_side x x ↔ x ∈ s := begin split, { rintro ⟨p₁, hp₁, p₂, hp₂, h⟩, obtain ⟨a, -, -, -, -, h₁, -⟩ := h.exists_eq_smul_add, rw [add_comm, vsub_add_vsub_cancel, ←eq_vadd_iff_vsub_eq] at h₁, rw h₁, exact s.smul_vsub_vadd_mem a hp₂ hp₁ hp₁ }, { exact λ h, ⟨x, h, x, h, same_ray.rfl⟩ } end lemma not_s_opp_side_self (s : affine_subspace R P) (x : P) : ¬s.s_opp_side x x := by simp [s_opp_side] lemma w_same_side_iff_exists_left {s : affine_subspace R P} {x y p₁ : P} (h : p₁ ∈ s) : s.w_same_side x y ↔ x ∈ s ∨ ∃ p₂ ∈ s, same_ray R (x -ᵥ p₁) (y -ᵥ p₂) := begin split, { rintro ⟨p₁', hp₁', p₂', hp₂', h0 | h0 | ⟨r₁, r₂, hr₁, hr₂, hr⟩⟩, { rw vsub_eq_zero_iff_eq at h0, rw h0, exact or.inl hp₁' }, { refine or.inr ⟨p₂', hp₂', _⟩, rw h0, exact same_ray.zero_right _ }, { refine or.inr ⟨(r₁ / r₂) • (p₁ -ᵥ p₁') +ᵥ p₂', s.smul_vsub_vadd_mem _ h hp₁' hp₂', or.inr (or.inr ⟨r₁, r₂, hr₁, hr₂, _⟩)⟩, rw [vsub_vadd_eq_vsub_sub, smul_sub, ←hr, smul_smul, mul_div_cancel' _ hr₂.ne.symm, ←smul_sub, vsub_sub_vsub_cancel_right] } }, { rintro (h' | h'), { exact w_same_side_of_left_mem y h' }, { exact ⟨p₁, h, h'⟩ } } end lemma w_same_side_iff_exists_right {s : affine_subspace R P} {x y p₂ : P} (h : p₂ ∈ s) : s.w_same_side x y ↔ y ∈ s ∨ ∃ p₁ ∈ s, same_ray R (x -ᵥ p₁) (y -ᵥ p₂) := begin rw [w_same_side_comm, w_same_side_iff_exists_left h], simp_rw same_ray_comm end lemma s_same_side_iff_exists_left {s : affine_subspace R P} {x y p₁ : P} (h : p₁ ∈ s) : s.s_same_side x y ↔ x ∉ s ∧ y ∉ s ∧ ∃ p₂ ∈ s, same_ray R (x -ᵥ p₁) (y -ᵥ p₂) := begin rw [s_same_side, and_comm, w_same_side_iff_exists_left h, and_assoc, and.congr_right_iff], intro hx, rw or_iff_right hx end lemma s_same_side_iff_exists_right {s : affine_subspace R P} {x y p₂ : P} (h : p₂ ∈ s) : s.s_same_side x y ↔ x ∉ s ∧ y ∉ s ∧ ∃ p₁ ∈ s, same_ray R (x -ᵥ p₁) (y -ᵥ p₂) := begin rw [s_same_side_comm, s_same_side_iff_exists_left h, ←and_assoc, and_comm (y ∉ s), and_assoc], simp_rw same_ray_comm end lemma w_opp_side_iff_exists_left {s : affine_subspace R P} {x y p₁ : P} (h : p₁ ∈ s) : s.w_opp_side x y ↔ x ∈ s ∨ ∃ p₂ ∈ s, same_ray R (x -ᵥ p₁) (p₂ -ᵥ y) := begin split, { rintro ⟨p₁', hp₁', p₂', hp₂', h0 | h0 | ⟨r₁, r₂, hr₁, hr₂, hr⟩⟩, { rw vsub_eq_zero_iff_eq at h0, rw h0, exact or.inl hp₁' }, { refine or.inr ⟨p₂', hp₂', _⟩, rw h0, exact same_ray.zero_right _ }, { refine or.inr ⟨(-r₁ / r₂) • (p₁ -ᵥ p₁') +ᵥ p₂', s.smul_vsub_vadd_mem _ h hp₁' hp₂', or.inr (or.inr ⟨r₁, r₂, hr₁, hr₂, _⟩)⟩, rw [vadd_vsub_assoc, smul_add, ←hr, smul_smul, neg_div, mul_neg, mul_div_cancel' _ hr₂.ne.symm, neg_smul, neg_add_eq_sub, ←smul_sub, vsub_sub_vsub_cancel_right] } }, { rintro (h' | h'), { exact w_opp_side_of_left_mem y h' }, { exact ⟨p₁, h, h'⟩ } } end lemma w_opp_side_iff_exists_right {s : affine_subspace R P} {x y p₂ : P} (h : p₂ ∈ s) : s.w_opp_side x y ↔ y ∈ s ∨ ∃ p₁ ∈ s, same_ray R (x -ᵥ p₁) (p₂ -ᵥ y) := begin rw [w_opp_side_comm, w_opp_side_iff_exists_left h], split, { rintro (hy | ⟨p, hp, hr⟩), { exact or.inl hy }, refine or.inr ⟨p, hp, _⟩, rwa [same_ray_comm, ←same_ray_neg_iff, neg_vsub_eq_vsub_rev, neg_vsub_eq_vsub_rev] }, { rintro (hy | ⟨p, hp, hr⟩), { exact or.inl hy }, refine or.inr ⟨p, hp, _⟩, rwa [same_ray_comm, ←same_ray_neg_iff, neg_vsub_eq_vsub_rev, neg_vsub_eq_vsub_rev] } end lemma s_opp_side_iff_exists_left {s : affine_subspace R P} {x y p₁ : P} (h : p₁ ∈ s) : s.s_opp_side x y ↔ x ∉ s ∧ y ∉ s ∧ ∃ p₂ ∈ s, same_ray R (x -ᵥ p₁) (p₂ -ᵥ y) := begin rw [s_opp_side, and_comm, w_opp_side_iff_exists_left h, and_assoc, and.congr_right_iff], intro hx, rw or_iff_right hx end lemma s_opp_side_iff_exists_right {s : affine_subspace R P} {x y p₂ : P} (h : p₂ ∈ s) : s.s_opp_side x y ↔ x ∉ s ∧ y ∉ s ∧ ∃ p₁ ∈ s, same_ray R (x -ᵥ p₁) (p₂ -ᵥ y) := begin rw [s_opp_side, and_comm, w_opp_side_iff_exists_right h, and_assoc, and.congr_right_iff, and.congr_right_iff], rintro hx hy, rw or_iff_right hy end lemma w_same_side.trans {s : affine_subspace R P} {x y z : P} (hxy : s.w_same_side x y) (hyz : s.w_same_side y z) (hy : y ∉ s) : s.w_same_side x z := begin rcases hxy with ⟨p₁, hp₁, p₂, hp₂, hxy⟩, rw [w_same_side_iff_exists_left hp₂, or_iff_right hy] at hyz, rcases hyz with ⟨p₃, hp₃, hyz⟩, refine ⟨p₁, hp₁, p₃, hp₃, hxy.trans hyz _⟩, refine λ h, false.elim _, rw vsub_eq_zero_iff_eq at h, exact hy (h.symm ▸ hp₂) end lemma w_same_side.trans_s_same_side {s : affine_subspace R P} {x y z : P} (hxy : s.w_same_side x y) (hyz : s.s_same_side y z) : s.w_same_side x z := hxy.trans hyz.1 hyz.2.1 lemma w_same_side.trans_w_opp_side {s : affine_subspace R P} {x y z : P} (hxy : s.w_same_side x y) (hyz : s.w_opp_side y z) (hy : y ∉ s) : s.w_opp_side x z := begin rcases hxy with ⟨p₁, hp₁, p₂, hp₂, hxy⟩, rw [w_opp_side_iff_exists_left hp₂, or_iff_right hy] at hyz, rcases hyz with ⟨p₃, hp₃, hyz⟩, refine ⟨p₁, hp₁, p₃, hp₃, hxy.trans hyz _⟩, refine λ h, false.elim _, rw vsub_eq_zero_iff_eq at h, exact hy (h.symm ▸ hp₂) end lemma w_same_side.trans_s_opp_side {s : affine_subspace R P} {x y z : P} (hxy : s.w_same_side x y) (hyz : s.s_opp_side y z) : s.w_opp_side x z := hxy.trans_w_opp_side hyz.1 hyz.2.1 lemma s_same_side.trans_w_same_side {s : affine_subspace R P} {x y z : P} (hxy : s.s_same_side x y) (hyz : s.w_same_side y z) : s.w_same_side x z := (hyz.symm.trans_s_same_side hxy.symm).symm lemma s_same_side.trans {s : affine_subspace R P} {x y z : P} (hxy : s.s_same_side x y) (hyz : s.s_same_side y z) : s.s_same_side x z := ⟨hxy.w_same_side.trans_s_same_side hyz, hxy.2.1, hyz.2.2⟩ lemma s_same_side.trans_w_opp_side {s : affine_subspace R P} {x y z : P} (hxy : s.s_same_side x y) (hyz : s.w_opp_side y z) : s.w_opp_side x z := hxy.w_same_side.trans_w_opp_side hyz hxy.2.2 lemma s_same_side.trans_s_opp_side {s : affine_subspace R P} {x y z : P} (hxy : s.s_same_side x y) (hyz : s.s_opp_side y z) : s.s_opp_side x z := ⟨hxy.trans_w_opp_side hyz.1, hxy.2.1, hyz.2.2⟩ lemma w_opp_side.trans_w_same_side {s : affine_subspace R P} {x y z : P} (hxy : s.w_opp_side x y) (hyz : s.w_same_side y z) (hy : y ∉ s) : s.w_opp_side x z := (hyz.symm.trans_w_opp_side hxy.symm hy).symm lemma w_opp_side.trans_s_same_side {s : affine_subspace R P} {x y z : P} (hxy : s.w_opp_side x y) (hyz : s.s_same_side y z) : s.w_opp_side x z := hxy.trans_w_same_side hyz.1 hyz.2.1 lemma w_opp_side.trans {s : affine_subspace R P} {x y z : P} (hxy : s.w_opp_side x y) (hyz : s.w_opp_side y z) (hy : y ∉ s) : s.w_same_side x z := begin rcases hxy with ⟨p₁, hp₁, p₂, hp₂, hxy⟩, rw [w_opp_side_iff_exists_left hp₂, or_iff_right hy] at hyz, rcases hyz with ⟨p₃, hp₃, hyz⟩, rw [←same_ray_neg_iff, neg_vsub_eq_vsub_rev, neg_vsub_eq_vsub_rev] at hyz, refine ⟨p₁, hp₁, p₃, hp₃, hxy.trans hyz _⟩, refine λ h, false.elim _, rw vsub_eq_zero_iff_eq at h, exact hy (h ▸ hp₂) end lemma w_opp_side.trans_s_opp_side {s : affine_subspace R P} {x y z : P} (hxy : s.w_opp_side x y) (hyz : s.s_opp_side y z) : s.w_same_side x z := hxy.trans hyz.1 hyz.2.1 lemma s_opp_side.trans_w_same_side {s : affine_subspace R P} {x y z : P} (hxy : s.s_opp_side x y) (hyz : s.w_same_side y z) : s.w_opp_side x z := (hyz.symm.trans_s_opp_side hxy.symm).symm lemma s_opp_side.trans_s_same_side {s : affine_subspace R P} {x y z : P} (hxy : s.s_opp_side x y) (hyz : s.s_same_side y z) : s.s_opp_side x z := (hyz.symm.trans_s_opp_side hxy.symm).symm lemma s_opp_side.trans_w_opp_side {s : affine_subspace R P} {x y z : P} (hxy : s.s_opp_side x y) (hyz : s.w_opp_side y z) : s.w_same_side x z := (hyz.symm.trans_s_opp_side hxy.symm).symm lemma s_opp_side.trans {s : affine_subspace R P} {x y z : P} (hxy : s.s_opp_side x y) (hyz : s.s_opp_side y z) : s.s_same_side x z := ⟨hxy.trans_w_opp_side hyz.1, hxy.2.1, hyz.2.2⟩ lemma w_same_side_and_w_opp_side_iff {s : affine_subspace R P} {x y : P} : (s.w_same_side x y ∧ s.w_opp_side x y) ↔ (x ∈ s ∨ y ∈ s) := begin split, { rintro ⟨hs, ho⟩, rw w_opp_side_comm at ho, by_contra h, rw not_or_distrib at h, exact h.1 (w_opp_side_self_iff.1 (hs.trans_w_opp_side ho h.2)) }, { rintro (h | h), { exact ⟨w_same_side_of_left_mem y h, w_opp_side_of_left_mem y h⟩ }, { exact ⟨w_same_side_of_right_mem x h, w_opp_side_of_right_mem x h⟩ } } end lemma w_same_side.not_s_opp_side {s : affine_subspace R P} {x y : P} (h : s.w_same_side x y) : ¬s.s_opp_side x y := begin intro ho, have hxy := w_same_side_and_w_opp_side_iff.1 ⟨h, ho.1⟩, rcases hxy with hx | hy, { exact ho.2.1 hx }, { exact ho.2.2 hy } end lemma s_same_side.not_w_opp_side {s : affine_subspace R P} {x y : P} (h : s.s_same_side x y) : ¬s.w_opp_side x y := begin intro ho, have hxy := w_same_side_and_w_opp_side_iff.1 ⟨h.1, ho⟩, rcases hxy with hx | hy, { exact h.2.1 hx }, { exact h.2.2 hy } end lemma s_same_side.not_s_opp_side {s : affine_subspace R P} {x y : P} (h : s.s_same_side x y) : ¬s.s_opp_side x y := λ ho, h.not_w_opp_side ho.1 lemma w_opp_side.not_s_same_side {s : affine_subspace R P} {x y : P} (h : s.w_opp_side x y) : ¬s.s_same_side x y := λ hs, hs.not_w_opp_side h lemma s_opp_side.not_w_same_side {s : affine_subspace R P} {x y : P} (h : s.s_opp_side x y) : ¬s.w_same_side x y := λ hs, hs.not_s_opp_side h lemma s_opp_side.not_s_same_side {s : affine_subspace R P} {x y : P} (h : s.s_opp_side x y) : ¬s.s_same_side x y := λ hs, h.not_w_same_side hs.1 lemma w_opp_side_iff_exists_wbtw {s : affine_subspace R P} {x y : P} : s.w_opp_side x y ↔ ∃ p ∈ s, wbtw R x p y := begin refine ⟨λ h, _, λ ⟨p, hp, h⟩, h.w_opp_side₁₃ hp⟩, rcases h with ⟨p₁, hp₁, p₂, hp₂, (h | h | ⟨r₁, r₂, hr₁, hr₂, h⟩)⟩, { rw vsub_eq_zero_iff_eq at h, rw h, exact ⟨p₁, hp₁, wbtw_self_left _ _ _⟩ }, { rw vsub_eq_zero_iff_eq at h, rw ←h, exact ⟨p₂, hp₂, wbtw_self_right _ _ _⟩ }, { refine ⟨line_map x y (r₂ / (r₁ + r₂)), _, _⟩, { rw [line_map_apply, ←vsub_vadd x p₁, ←vsub_vadd y p₂, vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, ←vadd_assoc, vadd_eq_add], convert s.smul_vsub_vadd_mem (r₂ / (r₁ + r₂)) hp₂ hp₁ hp₁, rw [add_comm (y -ᵥ p₂), smul_sub, smul_add, add_sub_assoc, add_assoc, add_right_eq_self, div_eq_inv_mul, ←neg_vsub_eq_vsub_rev, smul_neg, ←smul_smul, ←h, smul_smul, ←neg_smul, ←sub_smul, ←div_eq_inv_mul, ←div_eq_inv_mul, ←neg_div, ←sub_div, sub_eq_add_neg, ←neg_add, neg_div, div_self (left.add_pos hr₁ hr₂).ne.symm, neg_one_smul, neg_add_self] }, { exact set.mem_image_of_mem _ ⟨div_nonneg hr₂.le (left.add_pos hr₁ hr₂).le, div_le_one_of_le (le_add_of_nonneg_left hr₁.le) (left.add_pos hr₁ hr₂).le⟩ } } end lemma s_opp_side.exists_sbtw {s : affine_subspace R P} {x y : P} (h : s.s_opp_side x y) : ∃ p ∈ s, sbtw R x p y := begin obtain ⟨p, hp, hw⟩ := w_opp_side_iff_exists_wbtw.1 h.w_opp_side, refine ⟨p, hp, hw, _, _⟩, { rintro rfl, exact h.2.1 hp }, { rintro rfl, exact h.2.2 hp }, end lemma _root_.sbtw.s_opp_side_of_not_mem_of_mem {s : affine_subspace R P} {x y z : P} (h : sbtw R x y z) (hx : x ∉ s) (hy : y ∈ s) : s.s_opp_side x z := begin refine ⟨h.wbtw.w_opp_side₁₃ hy, hx, λ hz, hx _⟩, rcases h with ⟨⟨t, ⟨ht0, ht1⟩, rfl⟩, hyx, hyz⟩, rw line_map_apply at hy, have ht : t ≠ 1, { rintro rfl, simpa [line_map_apply] using hyz }, have hy' := vsub_mem_direction hy hz, rw [vadd_vsub_assoc, ←neg_vsub_eq_vsub_rev z, ←neg_one_smul R (z -ᵥ x), ←add_smul, ←sub_eq_add_neg, s.direction.smul_mem_iff (sub_ne_zero_of_ne ht)] at hy', rwa vadd_mem_iff_mem_of_mem_direction (submodule.smul_mem _ _ hy') at hy end lemma s_same_side_smul_vsub_vadd_left {s : affine_subspace R P} {x p₁ p₂ : P} (hx : x ∉ s) (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) {t : R} (ht : 0 < t) : s.s_same_side (t • (x -ᵥ p₁) +ᵥ p₂) x := begin refine ⟨w_same_side_smul_vsub_vadd_left x hp₁ hp₂ ht.le, λ h, hx _, hx⟩, rwa [vadd_mem_iff_mem_direction _ hp₂, s.direction.smul_mem_iff ht.ne.symm, vsub_right_mem_direction_iff_mem hp₁] at h end lemma s_same_side_smul_vsub_vadd_right {s : affine_subspace R P} {x p₁ p₂ : P} (hx : x ∉ s) (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) {t : R} (ht : 0 < t) : s.s_same_side x (t • (x -ᵥ p₁) +ᵥ p₂) := (s_same_side_smul_vsub_vadd_left hx hp₁ hp₂ ht).symm lemma s_same_side_line_map_left {s : affine_subspace R P} {x y : P} (hx : x ∈ s) (hy : y ∉ s) {t : R} (ht : 0 < t) : s.s_same_side (line_map x y t) y := s_same_side_smul_vsub_vadd_left hy hx hx ht lemma s_same_side_line_map_right {s : affine_subspace R P} {x y : P} (hx : x ∈ s) (hy : y ∉ s) {t : R} (ht : 0 < t) : s.s_same_side y (line_map x y t) := (s_same_side_line_map_left hx hy ht).symm lemma s_opp_side_smul_vsub_vadd_left {s : affine_subspace R P} {x p₁ p₂ : P} (hx : x ∉ s) (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) {t : R} (ht : t < 0) : s.s_opp_side (t • (x -ᵥ p₁) +ᵥ p₂) x := begin refine ⟨w_opp_side_smul_vsub_vadd_left x hp₁ hp₂ ht.le, λ h, hx _, hx⟩, rwa [vadd_mem_iff_mem_direction _ hp₂, s.direction.smul_mem_iff ht.ne, vsub_right_mem_direction_iff_mem hp₁] at h end lemma s_opp_side_smul_vsub_vadd_right {s : affine_subspace R P} {x p₁ p₂ : P} (hx : x ∉ s) (hp₁ : p₁ ∈ s) (hp₂ : p₂ ∈ s) {t : R} (ht : t < 0) : s.s_opp_side x (t • (x -ᵥ p₁) +ᵥ p₂) := (s_opp_side_smul_vsub_vadd_left hx hp₁ hp₂ ht).symm lemma s_opp_side_line_map_left {s : affine_subspace R P} {x y : P} (hx : x ∈ s) (hy : y ∉ s) {t : R} (ht : t < 0) : s.s_opp_side (line_map x y t) y := s_opp_side_smul_vsub_vadd_left hy hx hx ht lemma s_opp_side_line_map_right {s : affine_subspace R P} {x y : P} (hx : x ∈ s) (hy : y ∉ s) {t : R} (ht : t < 0) : s.s_opp_side y (line_map x y t) := (s_opp_side_line_map_left hx hy ht).symm lemma set_of_w_same_side_eq_image2 {s : affine_subspace R P} {x p : P} (hx : x ∉ s) (hp : p ∈ s) : {y | s.w_same_side x y} = set.image2 (λ (t : R) q, t • (x -ᵥ p) +ᵥ q) (set.Ici 0) s := begin ext y, simp_rw [set.mem_set_of, set.mem_image2, set.mem_Ici, mem_coe], split, { rw [w_same_side_iff_exists_left hp, or_iff_right hx], rintro ⟨p₂, hp₂, h | h | ⟨r₁, r₂, hr₁, hr₂, h⟩⟩, { rw vsub_eq_zero_iff_eq at h, exact false.elim (hx (h.symm ▸ hp)) }, { rw vsub_eq_zero_iff_eq at h, refine ⟨0, p₂, le_refl _, hp₂, _⟩, simp [h] }, { refine ⟨r₁ / r₂, p₂, (div_pos hr₁ hr₂).le, hp₂, _⟩, rw [div_eq_inv_mul, ←smul_smul, h, smul_smul, inv_mul_cancel hr₂.ne.symm, one_smul, vsub_vadd] } }, { rintro ⟨t, p', ht, hp', rfl⟩, exact w_same_side_smul_vsub_vadd_right x hp hp' ht } end lemma set_of_s_same_side_eq_image2 {s : affine_subspace R P} {x p : P} (hx : x ∉ s) (hp : p ∈ s) : {y | s.s_same_side x y} = set.image2 (λ (t : R) q, t • (x -ᵥ p) +ᵥ q) (set.Ioi 0) s := begin ext y, simp_rw [set.mem_set_of, set.mem_image2, set.mem_Ioi, mem_coe], split, { rw s_same_side_iff_exists_left hp, rintro ⟨-, hy, p₂, hp₂, h | h | ⟨r₁, r₂, hr₁, hr₂, h⟩⟩, { rw vsub_eq_zero_iff_eq at h, exact false.elim (hx (h.symm ▸ hp)) }, { rw vsub_eq_zero_iff_eq at h, exact false.elim (hy (h.symm ▸ hp₂)) }, { refine ⟨r₁ / r₂, p₂, div_pos hr₁ hr₂, hp₂, _⟩, rw [div_eq_inv_mul, ←smul_smul, h, smul_smul, inv_mul_cancel hr₂.ne.symm, one_smul, vsub_vadd] } }, { rintro ⟨t, p', ht, hp', rfl⟩, exact s_same_side_smul_vsub_vadd_right hx hp hp' ht } end lemma set_of_w_opp_side_eq_image2 {s : affine_subspace R P} {x p : P} (hx : x ∉ s) (hp : p ∈ s) : {y | s.w_opp_side x y} = set.image2 (λ (t : R) q, t • (x -ᵥ p) +ᵥ q) (set.Iic 0) s := begin ext y, simp_rw [set.mem_set_of, set.mem_image2, set.mem_Iic, mem_coe], split, { rw [w_opp_side_iff_exists_left hp, or_iff_right hx], rintro ⟨p₂, hp₂, h | h | ⟨r₁, r₂, hr₁, hr₂, h⟩⟩, { rw vsub_eq_zero_iff_eq at h, exact false.elim (hx (h.symm ▸ hp)) }, { rw vsub_eq_zero_iff_eq at h, refine ⟨0, p₂, le_refl _, hp₂, _⟩, simp [h] }, { refine ⟨-r₁ / r₂, p₂, (div_neg_of_neg_of_pos (left.neg_neg_iff.2 hr₁) hr₂).le, hp₂, _⟩, rw [div_eq_inv_mul, ←smul_smul, neg_smul, h, smul_neg, smul_smul, inv_mul_cancel hr₂.ne.symm, one_smul, neg_vsub_eq_vsub_rev, vsub_vadd] } }, { rintro ⟨t, p', ht, hp', rfl⟩, exact w_opp_side_smul_vsub_vadd_right x hp hp' ht } end lemma set_of_s_opp_side_eq_image2 {s : affine_subspace R P} {x p : P} (hx : x ∉ s) (hp : p ∈ s) : {y | s.s_opp_side x y} = set.image2 (λ (t : R) q, t • (x -ᵥ p) +ᵥ q) (set.Iio 0) s := begin ext y, simp_rw [set.mem_set_of, set.mem_image2, set.mem_Iio, mem_coe], split, { rw s_opp_side_iff_exists_left hp, rintro ⟨-, hy, p₂, hp₂, h | h | ⟨r₁, r₂, hr₁, hr₂, h⟩⟩, { rw vsub_eq_zero_iff_eq at h, exact false.elim (hx (h.symm ▸ hp)) }, { rw vsub_eq_zero_iff_eq at h, exact false.elim (hy (h ▸ hp₂)) }, { refine ⟨-r₁ / r₂, p₂, div_neg_of_neg_of_pos (left.neg_neg_iff.2 hr₁) hr₂, hp₂, _⟩, rw [div_eq_inv_mul, ←smul_smul, neg_smul, h, smul_neg, smul_smul, inv_mul_cancel hr₂.ne.symm, one_smul, neg_vsub_eq_vsub_rev, vsub_vadd] } }, { rintro ⟨t, p', ht, hp', rfl⟩, exact s_opp_side_smul_vsub_vadd_right hx hp hp' ht } end lemma w_opp_side_point_reflection {s : affine_subspace R P} {x : P} (y : P) (hx : x ∈ s) : s.w_opp_side y (point_reflection R x y) := (wbtw_point_reflection R _ _).w_opp_side₁₃ hx lemma s_opp_side_point_reflection {s : affine_subspace R P} {x y : P} (hx : x ∈ s) (hy : y ∉ s) : s.s_opp_side y (point_reflection R x y) := begin refine (sbtw_point_reflection_of_ne R (λ h, hy _)).s_opp_side_of_not_mem_of_mem hy hx, rwa ←h end end linear_ordered_field section normed variables [seminormed_add_comm_group V] [normed_space ℝ V] [pseudo_metric_space P] variables [normed_add_torsor V P] include V lemma is_connected_set_of_w_same_side {s : affine_subspace ℝ P} (x : P) (h : (s : set P).nonempty) : is_connected {y | s.w_same_side x y} := begin obtain ⟨p, hp⟩ := h, haveI : nonempty s := ⟨⟨p, hp⟩⟩, by_cases hx : x ∈ s, { convert is_connected_univ, { simp [w_same_side_of_left_mem, hx] }, { exact add_torsor.connected_space V P } }, { rw [set_of_w_same_side_eq_image2 hx hp, ←set.image_prod], refine (is_connected_Ici.prod (is_connected_iff_connected_space.2 _)).image _ ((continuous_fst.smul continuous_const).vadd continuous_snd).continuous_on, convert add_torsor.connected_space s.direction s } end lemma is_preconnected_set_of_w_same_side (s : affine_subspace ℝ P) (x : P) : is_preconnected {y | s.w_same_side x y} := begin rcases set.eq_empty_or_nonempty (s : set P) with h | h, { convert is_preconnected_empty, rw coe_eq_bot_iff at h, simp only [h, not_w_same_side_bot], refl }, { exact (is_connected_set_of_w_same_side x h).is_preconnected } end lemma is_connected_set_of_s_same_side {s : affine_subspace ℝ P} {x : P} (hx : x ∉ s) (h : (s : set P).nonempty) : is_connected {y | s.s_same_side x y} := begin obtain ⟨p, hp⟩ := h, haveI : nonempty s := ⟨⟨p, hp⟩⟩, rw [set_of_s_same_side_eq_image2 hx hp, ←set.image_prod], refine (is_connected_Ioi.prod (is_connected_iff_connected_space.2 _)).image _ ((continuous_fst.smul continuous_const).vadd continuous_snd).continuous_on, convert add_torsor.connected_space s.direction s end lemma is_preconnected_set_of_s_same_side (s : affine_subspace ℝ P) (x : P) : is_preconnected {y | s.s_same_side x y} := begin rcases set.eq_empty_or_nonempty (s : set P) with h | h, { convert is_preconnected_empty, rw coe_eq_bot_iff at h, simp only [h, not_s_same_side_bot], refl }, { by_cases hx : x ∈ s, { convert is_preconnected_empty, simp only [hx, s_same_side, not_true, false_and, and_false], refl }, { exact (is_connected_set_of_s_same_side hx h).is_preconnected } } end lemma is_connected_set_of_w_opp_side {s : affine_subspace ℝ P} (x : P) (h : (s : set P).nonempty) : is_connected {y | s.w_opp_side x y} := begin obtain ⟨p, hp⟩ := h, haveI : nonempty s := ⟨⟨p, hp⟩⟩, by_cases hx : x ∈ s, { convert is_connected_univ, { simp [w_opp_side_of_left_mem, hx] }, { exact add_torsor.connected_space V P } }, { rw [set_of_w_opp_side_eq_image2 hx hp, ←set.image_prod], refine (is_connected_Iic.prod (is_connected_iff_connected_space.2 _)).image _ ((continuous_fst.smul continuous_const).vadd continuous_snd).continuous_on, convert add_torsor.connected_space s.direction s } end lemma is_preconnected_set_of_w_opp_side (s : affine_subspace ℝ P) (x : P) : is_preconnected {y | s.w_opp_side x y} := begin rcases set.eq_empty_or_nonempty (s : set P) with h | h, { convert is_preconnected_empty, rw coe_eq_bot_iff at h, simp only [h, not_w_opp_side_bot], refl }, { exact (is_connected_set_of_w_opp_side x h).is_preconnected } end lemma is_connected_set_of_s_opp_side {s : affine_subspace ℝ P} {x : P} (hx : x ∉ s) (h : (s : set P).nonempty) : is_connected {y | s.s_opp_side x y} := begin obtain ⟨p, hp⟩ := h, haveI : nonempty s := ⟨⟨p, hp⟩⟩, rw [set_of_s_opp_side_eq_image2 hx hp, ←set.image_prod], refine (is_connected_Iio.prod (is_connected_iff_connected_space.2 _)).image _ ((continuous_fst.smul continuous_const).vadd continuous_snd).continuous_on, convert add_torsor.connected_space s.direction s end lemma is_preconnected_set_of_s_opp_side (s : affine_subspace ℝ P) (x : P) : is_preconnected {y | s.s_opp_side x y} := begin rcases set.eq_empty_or_nonempty (s : set P) with h | h, { convert is_preconnected_empty, rw coe_eq_bot_iff at h, simp only [h, not_s_opp_side_bot], refl }, { by_cases hx : x ∈ s, { convert is_preconnected_empty, simp only [hx, s_opp_side, not_true, false_and, and_false], refl }, { exact (is_connected_set_of_s_opp_side hx h).is_preconnected } } end end normed end affine_subspace
04600ad456c5028ad4a66f84e4c4914efdc0dbb0
9dc8cecdf3c4634764a18254e94d43da07142918
/src/tactic/lint/default.lean
e9af66f3150b78a1e242a7050606c867e26c240a
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
5,115
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import algebra.group.to_additive import tactic.lint.frontend import tactic.lint.misc import tactic.lint.simp import tactic.lint.type_classes /-! # Default linters This file defines the list of linters that are run in mathlib CI. Not all linters are considered "default" and run that way. A `linter` is marked as default if it is tagged with the `linter` attribute. -/ open tactic /-- User commands to spot common mistakes in the code * `#lint`: check all declarations in the current file * `#lint_mathlib`: check all declarations in mathlib (so excluding core or other projects, and also excluding the current file) * `#lint_all`: check all declarations in the environment (the current file and all imported files) The following linters are run by default: 1. `unused_arguments` checks for unused arguments in declarations. 2. `def_lemma` checks whether a declaration is incorrectly marked as a def/lemma. 3. `dup_namespce` checks whether a namespace is duplicated in the name of a declaration. 4. `ge_or_gt` checks whether ≥/> is used in the declaration. 5. `instance_priority` checks that instances that always apply have priority below default. 6. `doc_blame` checks for missing doc strings on definitions and constants. 7. `has_nonempty_instance` checks whether every type has an associated `inhabited`, `unique` or `nonempty` instance. 8. `impossible_instance` checks for instances that can never fire. 9. `incorrect_type_class_argument` checks for arguments in [square brackets] that are not classes. 10. `dangerous_instance` checks for instances that generate type-class problems with metavariables. 11. `fails_quickly` tests that type-class inference ends (relatively) quickly when applied to variables. 12. `has_coe_variable` tests that there are no instances of type `has_coe α t` for a variable `α`. 13. `inhabited_nonempty` checks for `inhabited` instance arguments that should be changed to `nonempty`. 14. `simp_nf` checks that the left-hand side of simp lemmas is in simp-normal form. 15. `simp_var_head` checks that there are no variables as head symbol of left-hand sides of simp lemmas. 16. `simp_comm` checks that no commutativity lemmas (such as `add_comm`) are marked simp. 17. `decidable_classical` checks for `decidable` hypotheses that are used in the proof of a proposition but not in the statement, and could be removed using `classical`. Theorems in the `decidable` namespace are exempt. 18. `has_coe_to_fun` checks that every type that coerces to a function has a direct `has_coe_to_fun` instance. 19. `check_type` checks that the statement of a declaration is well-typed. 20. `check_univs` checks that there are no bad `max u v` universe levels. 21. `syn_taut` checks that declarations are not syntactic tautologies. 22. `check_reducibility` checks whether non-instances with a class as type are reducible. 23. `unprintable_interactive` checks that interactive tactics have parser documentation. 24. `to_additive_doc` checks if additive versions of lemmas have documentation. The following linters are not run by default: 1. `doc_blame_thm`, checks for missing doc strings on lemmas and theorems. 2. `explicit_vars_of_iff` checks if there are explicit variables used on both sides of an iff. The command `#list_linters` prints a list of the names of all available linters. You can append a `*` to any command (e.g. `#lint_mathlib*`) to omit the slow tests (4). You can append a `-` to any command (e.g. `#lint_mathlib-`) to run a silent lint that suppresses the output if all checks pass. A silent lint will fail if any test fails. You can append a `+` to any command (e.g. `#lint_mathlib+`) to run a verbose lint that reports the result of each linter, including the successes. You can append a sequence of linter names to any command to run extra tests, in addition to the default ones. e.g. `#lint doc_blame_thm` will run all default tests and `doc_blame_thm`. You can append `only name1 name2 ...` to any command to run a subset of linters, e.g. `#lint only unused_arguments` You can add custom linters by defining a term of type `linter` in the `linter` namespace. A linter defined with the name `linter.my_new_check` can be run with `#lint my_new_check` or `lint only my_new_check`. If you add the attribute `@[linter]` to `linter.my_new_check` it will run by default. Adding the attribute `@[nolint doc_blame unused_arguments]` to a declaration omits it from only the specified linter checks. -/ add_tactic_doc { name := "linting commands", category := doc_category.cmd, decl_names := [`lint_cmd, `lint_mathlib_cmd, `lint_all_cmd, `list_linters], tags := ["linting"] } /-- The default linters used in mathlib CI. -/ meta def mathlib_linters : list name := by do ls ← get_checks tt [] ff, let ls := ls.map (λ ⟨n, _⟩, `linter ++ n), exact (reflect ls)
30bbbce55f43056bcd584790fd80f71929d73ded
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/analysis/calculus/mean_value.lean
bdc129e380f8d47ab6efbf0d750fce10969d392e
[ "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
53,691
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import analysis.calculus.local_extr import analysis.convex.topology /-! # The mean value inequality and equalities In this file we prove the following facts: * `convex.norm_image_sub_le_of_norm_deriv_le` : if `f` is differentiable on a convex set `s` and the norm of its derivative is bounded by `C`, then `f` is Lipschitz continuous on `s` with constant `C`; also a variant in which what is bounded by `C` is the norm of the difference of the derivative from a fixed linear map. * `image_le_of*`, `image_norm_le_of_*` : several similar lemmas deducing `f x ≤ B x` or `∥f x∥ ≤ B x` from upper estimates on `f'` or `∥f'∥`, respectively. These lemmas differ by their assumptions: * `of_liminf_*` lemmas assume that limit inferior of some ratio is less than `B' x`; * `of_deriv_right_*`, `of_norm_deriv_right_*` lemmas assume that the right derivative or its norm is less than `B' x`; * `of_*_lt_*` lemmas assume a strict inequality whenever `f x = B x` or `∥f x∥ = B x`; * `of_*_le_*` lemmas assume a non-strict inequality everywhere on `[a, b)`; * name of a lemma ends with `'` if (1) it assumes that `B` is continuous on `[a, b]` and has a right derivative at every point of `[a, b)`, and (2) the lemma has a counterpart assuming that `B` is differentiable everywhere on `ℝ` * `norm_image_sub_le_*_segment` : if derivative of `f` on `[a, b]` is bounded above by a constant `C`, then `∥f x - f a∥ ≤ C * ∥x - a∥`; several versions deal with right derivative and derivative within `[a, b]` (`has_deriv_within_at` or `deriv_within`). * `convex.is_const_of_fderiv_within_eq_zero` : if a function has derivative `0` on a convex set `s`, then it is a constant on `s`. * `exists_ratio_has_deriv_at_eq_ratio_slope` and `exists_ratio_deriv_eq_ratio_slope` : Cauchy's Mean Value Theorem. * `exists_has_deriv_at_eq_slope` and `exists_deriv_eq_slope` : Lagrange's Mean Value Theorem. * `domain_mvt` : Lagrange's Mean Value Theorem, applied to a segment in a convex domain. * `convex.image_sub_lt_mul_sub_of_deriv_lt`, `convex.mul_sub_lt_image_sub_of_lt_deriv`, `convex.image_sub_le_mul_sub_of_deriv_le`, `convex.mul_sub_le_image_sub_of_le_deriv`, if `∀ x, C (</≤/>/≥) (f' x)`, then `C * (y - x) (</≤/>/≥) (f y - f x)` whenever `x < y`. * `convex.mono_of_deriv_nonneg`, `convex.antimono_of_deriv_nonpos`, `convex.strict_mono_of_deriv_pos`, `convex.strict_antimono_of_deriv_neg` : if the derivative of a function is non-negative/non-positive/positive/negative, then the function is monotone/monotonically decreasing/strictly monotone/strictly monotonically decreasing. * `convex_on_of_deriv_mono`, `convex_on_of_deriv2_nonneg` : if the derivative of a function is increasing or its second derivative is nonnegative, then the original function is convex. * `strict_fderiv_of_cont_diff` : a C^1 function over the reals is strictly differentiable. (This is a corollary of the mean value inequality.) -/ variables {E : Type*} [normed_group E] [normed_space ℝ E] {F : Type*} [normed_group F] [normed_space ℝ F] open metric set asymptotics continuous_linear_map filter open_locale classical topological_space nnreal /-! ### One-dimensional fencing inequalities -/ /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := begin change Icc a b ⊆ {x | f x ≤ B x}, set s := {x | f x ≤ B x} ∩ Icc a b, have A : continuous_on (λ x, (f x, B x)) (Icc a b), from hf.prod hB, have : is_closed s, { simp only [s, inter_comm], exact A.preimage_closed_of_closed is_closed_Icc order_closed_topology.is_closed_le' }, apply this.Icc_subset_of_forall_exists_gt ha, rintros x ⟨hxB, xab⟩ y hy, change f x ≤ B x at hxB, cases lt_or_eq_of_le hxB with hxB hxB, { -- If `f x < B x`, then all we need is continuity of both sides apply @nonempty_of_mem_sets _ (𝓝[Ioi x] x), refine inter_mem_sets _ (Ioc_mem_nhds_within_Ioi ⟨le_refl x, hy⟩), have : ∀ᶠ x in 𝓝[Icc a b] x, f x < B x, from A x (Ico_subset_Icc_self xab) (mem_nhds_sets (is_open_lt continuous_fst continuous_snd) hxB), have : ∀ᶠ x in 𝓝[Ioi x] x, f x < B x, from nhds_within_le_of_mem (Icc_mem_nhds_within_Ioi xab) this, refine mem_sets_of_superset this (set_of_subset_set_of.2 $ λ y, le_of_lt) }, { rcases dense (bound x xab hxB) with ⟨r, hfr, hrB⟩, specialize hf' x xab r hfr, have HB : ∀ᶠ z in 𝓝[Ioi x] x, r < (z - x)⁻¹ * (B z - B x), from (has_deriv_within_at_iff_tendsto_slope' $ lt_irrefl x).1 (hB' x xab) (mem_nhds_sets is_open_Ioi hrB), obtain ⟨z, ⟨hfz, hzB⟩, hz⟩ : ∃ z, ((z - x)⁻¹ * (f z - f x) < r ∧ r < (z - x)⁻¹ * (B z - B x)) ∧ z ∈ Ioc x y, from ((hf'.and_eventually HB).and_eventually (Ioc_mem_nhds_within_Ioi ⟨le_refl _, hy⟩)).exists, have := le_of_lt (lt_trans hfz hzB), refine ⟨z, _, hz⟩, rw [mul_le_mul_left (inv_pos.2 $ sub_pos.2 hz.1), hxB, sub_le_sub_iff_right] at this, exact this } end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by `B'`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_liminf_slope_right_le_deriv_boundary {f : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) -- `bound` actually says `liminf (z - x)⁻¹ * (f z - f x) ≤ B' x` (bound : ∀ x ∈ Ico a b, ∀ r, B' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (f z - f x) < r) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := begin have Hr : ∀ x ∈ Icc a b, ∀ r ∈ Ioi (0:ℝ), f x ≤ B x + r * (x - a), { intros x hx r hr, apply image_le_of_liminf_slope_right_lt_deriv_boundary' hf bound, { rwa [sub_self, mul_zero, add_zero] }, { exact hB.add (continuous_on_const.mul (continuous_id.continuous_on.sub continuous_on_const)) }, { assume x hx, exact (hB' x hx).add (((has_deriv_within_at_id x (Ioi x)).sub_const a).const_mul r) }, { assume x hx _, rw [mul_one], exact (lt_add_iff_pos_right _).2 hr }, exact hx }, assume x hx, have : continuous_within_at (λ r, B x + r * (x - a)) (Ioi 0) 0, from continuous_within_at_const.add (continuous_within_at_id.mul continuous_within_at_const), convert continuous_within_at_const.closure_le _ this (Hr x hx); simp [closure_Ioi] end /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary' {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf (λ x hx r hr, (hf' x hx).liminf_right_slope_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_lt_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, f x = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_deriv_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x ≤ B' x` on `[a, b)`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ lemma image_le_of_deriv_right_le_deriv_boundary {f f' : ℝ → ℝ} {a b : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, f' x ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary hf ha hB hB' $ assume x hx r hr, (hf' x hx).liminf_right_slope_le (lt_of_le_of_lt (bound x hx) hr) /-! ### Vector-valued functions `f : ℝ → E` -/ section variables {f : ℝ → E} {a b : ℝ} /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `B` has right derivative at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(∥f z∥ - ∥f x∥) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. -/ lemma image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary {E : Type*} [normed_group E] {f : ℝ → E} {f' : ℝ → ℝ} (hf : continuous_on f (Icc a b)) -- `hf'` actually says `liminf ∥z - x∥⁻¹ * (∥f z∥ - ∥f x∥) ≤ f' x` (hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r → ∃ᶠ z in 𝓝[Ioi x] x, (z - x)⁻¹ * (∥f z∥ - ∥f x∥) < r) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → f' x < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' (continuous_norm.comp_continuous_on hf) hf' ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary' {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → ∥f' x∥ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary hf (λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le hr) ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_lt_deriv_boundary {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, ∥f x∥ = B x → ∥f' x∥ < B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_norm_deriv_right_lt_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary' {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : continuous_on B (Icc a b)) (hB' : ∀ x ∈ Ico a b, has_deriv_within_at B (B' x) (Ioi x) x) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary (continuous_norm.comp_continuous_on hf) ha hB hB' $ (λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le (lt_of_le_of_lt (bound x hx) hr)) /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ lemma image_norm_le_of_norm_deriv_right_le_deriv_boundary {f' : ℝ → E} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) {B B' : ℝ → ℝ} (ha : ∥f a∥ ≤ B a) (hB : ∀ x, has_deriv_at B (B' x) x) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ B' x) : ∀ ⦃x⦄, x ∈ Icc a b → ∥f x∥ ≤ B x := image_norm_le_of_norm_deriv_right_le_deriv_boundary' hf hf' ha (λ x hx, (hB x).continuous_at.continuous_within_at) (λ x hx, (hB x).has_deriv_within_at) bound /-- A function on `[a, b]` with the norm of the right derivative bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`. -/ theorem norm_image_sub_le_of_norm_deriv_right_le_segment {f' : ℝ → E} {C : ℝ} (hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x) (bound : ∀x ∈ Ico a b, ∥f' x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin let g := λ x, f x - f a, have hg : continuous_on g (Icc a b), from hf.sub continuous_on_const, have hg' : ∀ x ∈ Ico a b, has_deriv_within_at g (f' x) (Ioi x) x, { assume x hx, simpa using (hf' x hx).sub (has_deriv_within_at_const _ _ _) }, let B := λ x, C * (x - a), have hB : ∀ x, has_deriv_at B C x, { assume x, simpa using (has_deriv_at_const x C).mul ((has_deriv_at_id x).sub (has_deriv_at_const x a)) }, convert image_norm_le_of_norm_deriv_right_le_deriv_boundary hg hg' _ hB bound, { simp only [g, B] }, { simp only [g, B], rw [sub_self, norm_zero, sub_self, mul_zero] } end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc a b, has_deriv_within_at f (f' x) (Icc a b) x) (bound : ∀x ∈ Ico a b, ∥f' x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_right_le_segment (λ x hx, (hf x hx).continuous_within_at) (λ x hx, _) bound, apply (hf x $ Ico_subset_Icc_self hx).nhds_within, exact Icc_mem_nhds_within_Ioi hx end /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment {C : ℝ} (hf : differentiable_on ℝ f (Icc a b)) (bound : ∀x ∈ Ico a b, ∥deriv_within f (Icc a b) x∥ ≤ C) : ∀ x ∈ Icc a b, ∥f x - f a∥ ≤ C * (x - a) := begin refine norm_image_sub_le_of_norm_deriv_le_segment' _ bound, exact λ x hx, (hf x hx).has_deriv_within_at end /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01' {f' : ℝ → E} {C : ℝ} (hf : ∀ x ∈ Icc (0:ℝ) 1, has_deriv_within_at f (f' x) (Icc (0:ℝ) 1) x) (bound : ∀x ∈ Ico (0:ℝ) 1, ∥f' x∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment' hf bound 1 (right_mem_Icc.2 zero_le_one) /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01 {C : ℝ} (hf : differentiable_on ℝ f (Icc (0:ℝ) 1)) (bound : ∀x ∈ Ico (0:ℝ) 1, ∥deriv_within f (Icc (0:ℝ) 1) x∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := by simpa only [sub_zero, mul_one] using norm_image_sub_le_of_norm_deriv_le_segment hf bound 1 (right_mem_Icc.2 zero_le_one) end /-! ### Vector-valued functions `f : E → F` -/ /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] F)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := begin /- By composition with `t ↦ x + t • (y-x)`, we reduce to a statement for functions defined on `[0,1]`, for which it is proved in `norm_image_sub_le_of_norm_deriv_le_segment`. We just have to check the differentiability of the composition and bounds on its derivative, which is straightforward but tedious for lack of automation. -/ have C0 : 0 ≤ C := le_trans (norm_nonneg _) (bound x xs), set g : ℝ → E := λ t, x + t • (y - x), have Dg : ∀ t, has_deriv_at g (y-x) t, { assume t, simpa only [one_smul] using ((has_deriv_at_id t).smul_const (y - x)).const_add x }, have segm : Icc 0 1 ⊆ g ⁻¹' s, { rw [← image_subset_iff, ← segment_eq_image'], apply hs.segment_subset xs ys }, have : f x = f (g 0), by { simp only [g], rw [zero_smul, add_zero] }, rw this, have : f y = f (g 1), by { simp only [g], rw [one_smul, add_sub_cancel'_right] }, rw this, have D2: ∀ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) ((f' (g t) : E → F) (y-x)) (Icc (0:ℝ) 1) t, { intros t ht, exact (hf (g t) $ segm ht).comp_has_deriv_within_at _ (Dg t).has_deriv_within_at segm }, apply norm_image_sub_le_of_norm_deriv_le_segment_01' D2, assume t ht, refine le_trans (le_op_norm _ _) (mul_le_mul_of_nonneg_right _ (norm_nonneg _)), exact bound (g t) (segm $ Ico_subset_Icc_self ht) end /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_fderiv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_has_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} {f' : E → (E →L[ℝ] F)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := begin rw lipschitz_on_with_iff_norm_sub_le, intros x x_in y y_in, convert hs.norm_image_sub_le_of_norm_has_fderiv_within_le hf bound y_in x_in, exact nnreal.coe_of_real C ((norm_nonneg $ f' x).trans $ bound x x_in) end /-- The mean value theorem on a convex set: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} {x y : E} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_fderiv_within_le {f : E → F} {C : ℝ} {s : set E} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s:= hs.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) bound /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le {f : E → F} {C : ℝ} {s : set E} {x y : E} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥fderiv ℝ f x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_fderiv_le {f : E → F} {C : ℝ} {s : set E} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥fderiv ℝ f x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound /-- Variant of the mean value inequality on a convex set, using a bound on the difference between the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] F)} {φ : E →L[ℝ] F} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := begin /- We subtract `φ` to define a new function `g` for which `g' = 0`, for which the previous theorem applies, `convex.norm_image_sub_le_of_norm_has_fderiv_within_le`. Then, we just need to glue together the pieces, expressing back `f` in terms of `g`. -/ let g := λy, f y - φ y, have hg : ∀ x ∈ s, has_fderiv_within_at g (f' x - φ) s x := λ x xs, (hf x xs).sub φ.has_fderiv_within_at, calc ∥f y - f x - φ (y - x)∥ = ∥f y - f x - (φ y - φ x)∥ : by simp ... = ∥(f y - φ y) - (f x - φ x)∥ : by abel ... = ∥g y - g x∥ : by simp ... ≤ C * ∥y - x∥ : convex.norm_image_sub_le_of_norm_has_fderiv_within_le hg bound hs xs ys, end /-- Variant of the mean value inequality on a convex set. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {φ : E →L[ℝ] F} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (λ x hx, (hf x hx).has_fderiv_within_at) bound xs ys /-- Variant of the mean value inequality on a convex set. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le' {f : E → F} {C : ℝ} {s : set E} {x y : E} {φ : E →L[ℝ] F} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥fderiv ℝ f x - φ∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x - φ (y - x)∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_fderiv_within_le' (λ x hx, (hf x hx).has_fderiv_at.has_fderiv_within_at) bound xs ys /-- If a function has zero Fréchet derivative at every point of a convex set, then it is a constant on this set. -/ theorem convex.is_const_of_fderiv_within_eq_zero {s : set E} (hs : convex s) {f : E → F} (hf : differentiable_on ℝ f s) (hf' : ∀ x ∈ s, fderiv_within ℝ f s x = 0) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : f x = f y := have bound : ∀ x ∈ s, ∥fderiv_within ℝ f s x∥ ≤ 0, from λ x hx, by simp only [hf' x hx, norm_zero], by simpa only [(dist_eq_norm _ _).symm, zero_mul, dist_le_zero, eq_comm] using hs.norm_image_sub_le_of_norm_fderiv_within_le hf bound hx hy theorem is_const_of_fderiv_eq_zero {f : E → F} (hf : differentiable ℝ f) (hf' : ∀ x, fderiv ℝ f x = 0) (x y : E) : f x = f y := convex_univ.is_const_of_fderiv_within_eq_zero hf.differentiable_on (λ x _, by rw fderiv_within_univ; exact hf' x) trivial trivial /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_deriv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_deriv_within_le {f f' : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : ∀ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := convex.norm_image_sub_le_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) (λ x hx, le_trans (by simp) (bound x hx)) hs xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_deriv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_has_deriv_within_le {f f' : ℝ → F} {C : ℝ} {s : set ℝ} (hs : convex s) (hf : ∀ x ∈ s, has_deriv_within_at f (f' x) s x) (bound : ∀x∈s, ∥f' x∥ ≤ C) : lipschitz_on_with (nnreal.of_real C) f s := convex.lipschitz_on_with_of_norm_has_fderiv_within_le (λ x hx, (hf x hx).has_fderiv_within_at) (λ x hx, le_trans (by simp) (bound x hx)) hs /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv_within` -/ theorem convex.norm_image_sub_le_of_norm_deriv_within_le {f : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥deriv_within f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_deriv_within_le {f : ℝ → F} {C : ℝ} {s : set ℝ} (hs : convex s) (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥deriv_within f s x∥ ≤ C) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_within_at) bound /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv`. -/ theorem convex.norm_image_sub_le_of_norm_deriv_le {f : ℝ → F} {C : ℝ} {s : set ℝ} {x y : ℝ} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥deriv f x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := hs.norm_image_sub_le_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_deriv_le {f : ℝ → F} {C : ℝ} {s : set ℝ} (hf : ∀ x ∈ s, differentiable_at ℝ f x) (bound : ∀x∈s, ∥deriv f x∥ ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := hs.lipschitz_on_with_of_norm_has_deriv_within_le (λ x hx, (hf x hx).has_deriv_at.has_deriv_within_at) bound /-! ### Functions `[a, b] → ℝ`. -/ section interval -- Declare all variables here to make sure they come in a correct order variables (f f' : ℝ → ℝ) {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b)) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hfd : differentiable_on ℝ f (Ioo a b)) (g g' : ℝ → ℝ) (hgc : continuous_on g (Icc a b)) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hgd : differentiable_on ℝ g (Ioo a b)) include hab hfc hff' hgc hgg' /-- Cauchy's Mean Value Theorem, `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope : ∃ c ∈ Ioo a b, (g b - g a) * f' c = (f b - f a) * g' c := begin let h := λ x, (g b - g a) * f x - (f b - f a) * g x, have hI : h a = h b, { simp only [h], ring }, let h' := λ x, (g b - g a) * f' x - (f b - f a) * g' x, have hhh' : ∀ x ∈ Ioo a b, has_deriv_at h (h' x) x, from λ x hx, ((hff' x hx).const_mul (g b - g a)).sub ((hgg' x hx).const_mul (f b - f a)), have hhc : continuous_on h (Icc a b), from (continuous_on_const.mul hfc).sub (continuous_on_const.mul hgc), rcases exists_has_deriv_at_eq_zero h h' hab hhc hI hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end omit hfc hgc /-- Cauchy's Mean Value Theorem, extended `has_deriv_at` version. -/ lemma exists_ratio_has_deriv_at_eq_ratio_slope' {lfa lga lfb lgb : ℝ} (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) (hgg' : ∀ x ∈ Ioo a b, has_deriv_at g (g' x) x) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 lfa)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 lga)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[Iio b] b) (𝓝 lgb)) : ∃ c ∈ Ioo a b, (lgb - lga) * (f' c) = (lfb - lfa) * (g' c) := begin let h := λ x, (lgb - lga) * f x - (lfb - lfa) * g x, have hha : tendsto h (𝓝[Ioi a] a) (𝓝 $ lgb * lfa - lfb * lga), { have : tendsto h (𝓝[Ioi a] a)(𝓝 $ (lgb - lga) * lfa - (lfb - lfa) * lga) := (tendsto_const_nhds.mul hfa).sub (tendsto_const_nhds.mul hga), convert this using 2, ring }, have hhb : tendsto h (𝓝[Iio b] b) (𝓝 $ lgb * lfa - lfb * lga), { have : tendsto h (𝓝[Iio b] b)(𝓝 $ (lgb - lga) * lfb - (lfb - lfa) * lgb) := (tendsto_const_nhds.mul hfb).sub (tendsto_const_nhds.mul hgb), convert this using 2, ring }, let h' := λ x, (lgb - lga) * f' x - (lfb - lfa) * g' x, have hhh' : ∀ x ∈ Ioo a b, has_deriv_at h (h' x) x, { intros x hx, exact ((hff' x hx).const_mul _ ).sub (((hgg' x hx)).const_mul _) }, rcases exists_has_deriv_at_eq_zero' hab hha hhb hhh' with ⟨c, cmem, hc⟩, exact ⟨c, cmem, sub_eq_zero.1 hc⟩ end include hfc omit hgg' /-- Lagrange's Mean Value Theorem, `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_slope : ∃ c ∈ Ioo a b, f' c = (f b - f a) / (b - a) := begin rcases exists_ratio_has_deriv_at_eq_ratio_slope f f' hab hfc hff' id 1 continuous_id.continuous_on (λ x hx, has_deriv_at_id x) with ⟨c, cmem, hc⟩, use [c, cmem], simp only [_root_.id, pi.one_apply, mul_one] at hc, rw [← hc, mul_div_cancel_left], exact ne_of_gt (sub_pos.2 hab) end omit hff' /-- Cauchy's Mean Value Theorem, `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope : ∃ c ∈ Ioo a b, (g b - g a) * (deriv f c) = (f b - f a) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope f (deriv f) hab hfc (λ x hx, ((hfd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) g (deriv g) hgc (λ x hx, ((hgd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) omit hfc /-- Cauchy's Mean Value Theorem, extended `deriv` version. -/ lemma exists_ratio_deriv_eq_ratio_slope' {lfa lga lfb lgb : ℝ} (hdf : differentiable_on ℝ f $ Ioo a b) (hdg : differentiable_on ℝ g $ Ioo a b) (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 lfa)) (hga : tendsto g (𝓝[Ioi a] a) (𝓝 lga)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 lfb)) (hgb : tendsto g (𝓝[Iio b] b) (𝓝 lgb)) : ∃ c ∈ Ioo a b, (lgb - lga) * (deriv f c) = (lfb - lfa) * (deriv g c) := exists_ratio_has_deriv_at_eq_ratio_slope' _ _ hab _ _ (λ x hx, ((hdf x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at) (λ x hx, ((hdg x hx).differentiable_at $ Ioo_mem_nhds hx.1 hx.2).has_deriv_at) hfa hga hfb hgb /-- Lagrange's Mean Value Theorem, `deriv` version. -/ lemma exists_deriv_eq_slope : ∃ c ∈ Ioo a b, deriv f c = (f b - f a) / (b - a) := exists_has_deriv_at_eq_slope f (deriv f) hab hfc (λ x hx, ((hfd x hx).differentiable_at $ mem_nhds_sets is_open_Ioo hx).has_deriv_at) end interval /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C < f'`, then `f` grows faster than `C * x` on `D`, i.e., `C * (y - x) < f y - f x` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.mul_sub_lt_image_sub_of_lt_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_gt : ∀ x ∈ interior D, C < deriv f x) : ∀ x y ∈ D, x < y → C * (y - x) < f y - f x := begin assume x y hx hy hxy, have hxyD : Icc x y ⊆ D, from hD.ord_connected hx hy, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), have : C < (f y - f x) / (y - x), by { rw [← ha], exact hf'_gt _ (hxyD' a_mem) }, exact (lt_div_iff (sub_pos.2 hxy)).1 this end /-- Let `f : ℝ → ℝ` be a differentiable function. If `C < f'`, then `f` grows faster than `C * x`, i.e., `C * (y - x) < f y - f x` whenever `x < y`. -/ theorem mul_sub_lt_image_sub_of_lt_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (hf'_gt : ∀ x, C < deriv f x) ⦃x y⦄ (hxy : x < y) : C * (y - x) < f y - f x := convex_univ.mul_sub_lt_image_sub_of_lt_deriv hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_gt x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C ≤ f'`, then `f` grows at least as fast as `C * x` on `D`, i.e., `C * (y - x) ≤ f y - f x` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.mul_sub_le_image_sub_of_le_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (hf'_ge : ∀ x ∈ interior D, C ≤ deriv f x) : ∀ x y ∈ D, x ≤ y → C * (y - x) ≤ f y - f x := begin assume x y hx hy hxy, cases eq_or_lt_of_le hxy with hxy' hxy', by rw [hxy', sub_self, sub_self, mul_zero], have hxyD : Icc x y ⊆ D, from hD.ord_connected hx hy, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, obtain ⟨a, a_mem, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy' (hf.mono hxyD) (hf'.mono hxyD'), have : C ≤ (f y - f x) / (y - x), by { rw [← ha], exact hf'_ge _ (hxyD' a_mem) }, exact (le_div_iff (sub_pos.2 hxy')).1 this end /-- Let `f : ℝ → ℝ` be a differentiable function. If `C ≤ f'`, then `f` grows at least as fast as `C * x`, i.e., `C * (y - x) ≤ f y - f x` whenever `x ≤ y`. -/ theorem mul_sub_le_image_sub_of_le_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (hf'_ge : ∀ x, C ≤ deriv f x) ⦃x y⦄ (hxy : x ≤ y) : C * (y - x) ≤ f y - f x := convex_univ.mul_sub_le_image_sub_of_le_deriv hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_ge x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.image_sub_lt_mul_sub_of_deriv_lt {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (lt_hf' : ∀ x ∈ interior D, deriv f x < C) : ∀ x y ∈ D, x < y → f y - f x < C * (y - x) := begin assume x y hx hy hxy, have hf'_gt : ∀ x ∈ interior D, -C < deriv (λ y, -f y) x, { assume x hx, rw [deriv.neg, neg_lt_neg_iff], exact lt_hf' x hx }, simpa [-neg_lt_neg_iff] using neg_lt_neg (hD.mul_sub_lt_image_sub_of_lt_deriv hf.neg hf'.neg hf'_gt x y hx hy hxy) end /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x < y`. -/ theorem image_sub_lt_mul_sub_of_deriv_lt {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (lt_hf' : ∀ x, deriv f x < C) ⦃x y⦄ (hxy : x < y) : f y - f x < C * (y - x) := convex_univ.image_sub_lt_mul_sub_of_deriv_lt hf.continuous.continuous_on hf.differentiable_on (λ x _, lt_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' ≤ C`, then `f` grows at most as fast as `C * x` on `D`, i.e., `f y - f x ≤ C * (y - x)` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.image_sub_le_mul_sub_of_deriv_le {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C} (le_hf' : ∀ x ∈ interior D, deriv f x ≤ C) : ∀ x y ∈ D, x ≤ y → f y - f x ≤ C * (y - x) := begin assume x y hx hy hxy, have hf'_ge : ∀ x ∈ interior D, -C ≤ deriv (λ y, -f y) x, { assume x hx, rw [deriv.neg, neg_le_neg_iff], exact le_hf' x hx }, simpa [-neg_le_neg_iff] using neg_le_neg (hD.mul_sub_le_image_sub_of_le_deriv hf.neg hf'.neg hf'_ge x y hx hy hxy) end /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' ≤ C`, then `f` grows at most as fast as `C * x`, i.e., `f y - f x ≤ C * (y - x)` whenever `x ≤ y`. -/ theorem image_sub_le_mul_sub_of_deriv_le {f : ℝ → ℝ} (hf : differentiable ℝ f) {C} (le_hf' : ∀ x, deriv f x ≤ C) ⦃x y⦄ (hxy : x ≤ y) : f y - f x ≤ C * (y - x) := convex_univ.image_sub_le_mul_sub_of_deriv_le hf.continuous.continuous_on hf.differentiable_on (λ x _, le_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is positive, then `f` is a strictly monotonically increasing function on `D`. -/ theorem convex.strict_mono_of_deriv_pos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_pos : ∀ x ∈ interior D, 0 < deriv f x) : ∀ x y ∈ D, x < y → f x < f y := by simpa only [zero_mul, sub_pos] using hD.mul_sub_lt_image_sub_of_lt_deriv hf hf' hf'_pos /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is positive, then `f` is a strictly monotonically increasing function. -/ theorem strict_mono_of_deriv_pos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_pos : ∀ x, 0 < deriv f x) : strict_mono f := λ x y hxy, convex_univ.strict_mono_of_deriv_pos hf.continuous.continuous_on hf.differentiable_on (λ x _, hf'_pos x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonnegative, then `f` is a monotonically increasing function on `D`. -/ theorem convex.mono_of_deriv_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonneg : ∀ x ∈ interior D, 0 ≤ deriv f x) : ∀ x y ∈ D, x ≤ y → f x ≤ f y := by simpa only [zero_mul, sub_nonneg] using hD.mul_sub_le_image_sub_of_le_deriv hf hf' hf'_nonneg /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonnegative, then `f` is a monotonically increasing function. -/ theorem mono_of_deriv_nonneg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, 0 ≤ deriv f x) : monotone f := λ x y hxy, convex_univ.mono_of_deriv_nonneg hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is negative, then `f` is a strictly monotonically decreasing function on `D`. -/ theorem convex.strict_antimono_of_deriv_neg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_neg : ∀ x ∈ interior D, deriv f x < 0) : ∀ x y ∈ D, x < y → f y < f x := by simpa only [zero_mul, sub_lt_zero] using hD.image_sub_lt_mul_sub_of_deriv_lt hf hf' hf'_neg /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is negative, then `f` is a strictly monotonically decreasing function. -/ theorem strict_antimono_of_deriv_neg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, deriv f x < 0) : ∀ ⦃x y⦄, x < y → f y < f x := λ x y hxy, convex_univ.strict_antimono_of_deriv_neg hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonpositive, then `f` is a monotonically decreasing function on `D`. -/ theorem convex.antimono_of_deriv_nonpos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonpos : ∀ x ∈ interior D, deriv f x ≤ 0) : ∀ x y ∈ D, x ≤ y → f y ≤ f x := by simpa only [zero_mul, sub_nonpos] using hD.image_sub_le_mul_sub_of_deriv_le hf hf' hf'_nonpos /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonpositive, then `f` is a monotonically decreasing function. -/ theorem antimono_of_deriv_nonpos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ x, deriv f x ≤ 0) : ∀ ⦃x y⦄, x ≤ y → f y ≤ f x := λ x y hxy, convex_univ.antimono_of_deriv_nonpos hf.continuous.continuous_on hf.differentiable_on (λ x _, hf' x) x y trivial trivial hxy /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv_mono {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_mono : ∀ x y ∈ interior D, x ≤ y → deriv f x ≤ deriv f y) : convex_on D f := convex_on_real_of_slope_mono_adjacent hD begin intros x y z hx hz hxy hyz, -- First we prove some trivial inclusions have hxzD : Icc x z ⊆ D, from hD.ord_connected hx hz, have hxyD : Icc x y ⊆ D, from subset.trans (Icc_subset_Icc_right $ le_of_lt hyz) hxzD, have hxyD' : Ioo x y ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hxyD⟩, have hyzD : Icc y z ⊆ D, from subset.trans (Icc_subset_Icc_left $ le_of_lt hxy) hxzD, have hyzD' : Ioo y z ⊆ interior D, from subset_sUnion_of_mem ⟨is_open_Ioo, subset.trans Ioo_subset_Icc_self hyzD⟩, -- Then we apply MVT to both `[x, y]` and `[y, z]` obtain ⟨a, ⟨hxa, hay⟩, ha⟩ : ∃ a ∈ Ioo x y, deriv f a = (f y - f x) / (y - x), from exists_deriv_eq_slope f hxy (hf.mono hxyD) (hf'.mono hxyD'), obtain ⟨b, ⟨hyb, hbz⟩, hb⟩ : ∃ b ∈ Ioo y z, deriv f b = (f z - f y) / (z - y), from exists_deriv_eq_slope f hyz (hf.mono hyzD) (hf'.mono hyzD'), rw [← ha, ← hb], exact hf'_mono a b (hxyD' ⟨hxa, hay⟩) (hyzD' ⟨hyb, hbz⟩) (le_of_lt $ lt_trans hay hyb) end /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv_mono {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_mono : monotone (deriv f)) : convex_on univ f := convex_on_of_deriv_mono convex_univ hf.continuous.continuous_on hf.differentiable_on (λ x y _ _ h, hf'_mono h) /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is twice differentiable on its interior, and `f''` is nonnegative on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv2_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonneg : ∀ x ∈ interior D, 0 ≤ (deriv^[2] f x)) : convex_on D f := convex_on_of_deriv_mono hD hf hf' $ assume x y hx hy hxy, hD.interior.mono_of_deriv_nonneg hf''.continuous_on (by rwa [interior_interior]) (by rwa [interior_interior]) _ _ hx hy hxy /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonnegative on `ℝ`, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv2_nonneg {f : ℝ → ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonneg : ∀ x, 0 ≤ (deriv^[2] f x)) : convex_on univ f := convex_on_of_deriv2_nonneg convex_univ hf'.continuous.continuous_on hf'.differentiable_on hf''.differentiable_on (λ x _, hf''_nonneg x) /-! ### Functions `f : E → ℝ` -/ /-- Lagrange's Mean Value Theorem, applied to convex domains. -/ theorem domain_mvt {f : E → ℝ} {s : set E} {x y : E} {f' : E → (E →L[ℝ] ℝ)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∃ z ∈ segment x y, f y - f x = f' z (y - x) := begin have hIccIoo := @Ioo_subset_Icc_self ℝ _ 0 1, -- parametrize segment set g : ℝ → E := λ t, x + t • (y - x), have hseg : ∀ t ∈ Icc (0:ℝ) 1, g t ∈ segment x y, { rw segment_eq_image', simp only [mem_image, and_imp, add_right_inj], intros t ht, exact ⟨t, ht, rfl⟩ }, have hseg' : Icc 0 1 ⊆ g ⁻¹' s, { rw ← image_subset_iff, unfold image, change ∀ _, _, intros z Hz, rw mem_set_of_eq at Hz, rcases Hz with ⟨t, Ht, hgt⟩, rw ← hgt, exact hs.segment_subset xs ys (hseg t Ht) }, -- derivative of pullback of f under parametrization have hfg: ∀ t ∈ Icc (0:ℝ) 1, has_deriv_within_at (f ∘ g) ((f' (g t) : E → ℝ) (y-x)) (Icc (0:ℝ) 1) t, { intros t Ht, have hg : has_deriv_at g (y-x) t, { have := ((has_deriv_at_id t).smul_const (y - x)).const_add x, rwa one_smul at this }, exact (hf (g t) $ hseg' Ht).comp_has_deriv_within_at _ hg.has_deriv_within_at hseg' }, -- apply 1-variable mean value theorem to pullback have hMVT : ∃ (t ∈ Ioo (0:ℝ) 1), ((f' (g t) : E → ℝ) (y-x)) = (f (g 1) - f (g 0)) / (1 - 0), { refine exists_has_deriv_at_eq_slope (f ∘ g) _ (by norm_num) _ _, { unfold continuous_on, exact λ t Ht, (hfg t Ht).continuous_within_at }, { refine λ t Ht, (hfg t $ hIccIoo Ht).has_deriv_at _, refine mem_nhds_sets_iff.mpr _, use (Ioo (0:ℝ) 1), refine ⟨hIccIoo, _, Ht⟩, simp [real.Ioo_eq_ball, is_open_ball] } }, -- reinterpret on domain rcases hMVT with ⟨t, Ht, hMVT'⟩, use g t, refine ⟨hseg t $ hIccIoo Ht, _⟩, simp [g, hMVT'], end /-! ### Vector-valued functions `f : E → F`. Strict differentiability. -/ /-- Over the reals, a continuously differentiable function is strictly differentiable. -/ lemma strict_fderiv_of_cont_diff {f : E → F} {s : set E} {x : E} {f' : E → (E →L[ℝ] F)} (hf : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hcont : continuous_on f' s) (hs : s ∈ 𝓝 x) : has_strict_fderiv_at f (f' x) x := begin -- turn little-o definition of strict_fderiv into an epsilon-delta statement apply is_o_iff_forall_is_O_with.mpr, intros c hc, refine is_O_with.of_bound (eventually_iff.mpr (mem_nhds_iff.mpr _)), -- the correct ε is the modulus of continuity of f', shrunk to be inside s rcases (metric.continuous_on_iff.mp hcont x (mem_of_nhds hs) c hc) with ⟨ε₁, H₁, hcont'⟩, rcases (mem_nhds_iff.mp hs) with ⟨ε₂, H₂, hε₂⟩, refine ⟨min ε₁ ε₂, lt_min H₁ H₂, _⟩, -- mess with ε construction set t := ball x (min ε₁ ε₂), have hts : t ⊆ s := λ _ hy, hε₂ (ball_subset_ball (min_le_right ε₁ ε₂) hy), have Hf : ∀ y ∈ t, has_fderiv_within_at f (f' y) t y := λ y yt, has_fderiv_within_at.mono (hf y (hts yt)) hts, have hconv := convex_ball x (min ε₁ ε₂), -- simplify formulas involving the product E × E rintros ⟨a, b⟩ h, simp only [mem_set_of_eq, map_sub], have hab : a ∈ t ∧ b ∈ t := by rwa [mem_ball, prod.dist_eq, max_lt_iff] at h, -- exploit the choice of ε as the modulus of continuity of f' have hf' : ∀ x' ∈ t, ∥f' x' - f' x∥ ≤ c, { intros x' H', refine le_of_lt (hcont' x' (hts H') _), exact ball_subset_ball (min_le_left ε₁ ε₂) H' }, -- apply mean value theorem simpa using convex.norm_image_sub_le_of_norm_has_fderiv_within_le' Hf hf' hconv hab.2 hab.1, end
2ac0127a06309ba2ea6067b48f5c03763540ac9e
88892181780ff536a81e794003fe058062f06758
/src/mod.lean
e4797e2f7bbe7a0140c940266bed63486a3c5da3
[]
no_license
AtnNn/lean-sandbox
fe2c44280444e8bb8146ab8ac391c82b480c0a2e
8c68afbdc09213173aef1be195da7a9a86060a97
refs/heads/master
1,623,004,395,876
1,579,969,507,000
1,579,969,507,000
146,666,368
0
0
null
null
null
null
UTF-8
Lean
false
false
1,321
lean
import data.nat.basic universe u namespace sandbox open nat def mod : nat → nat → nat | _ 0 := 0 | a (succ b) := if h : a ≥ succ b then have a - succ b < a := sub_lt_of_pos_le _ _ succ_pos' h, mod (a - succ b) (succ b) else a local infix % := mod lemma mod_zero {a} : mod a 0 = 0 := begin unfold mod end lemma mod_step {a b} (h : a ≥ b) : mod a b = mod (a - b) b := begin cases b, { refl }, { rw mod.equations._eqn_2, rwa if_pos } end lemma mod_eq_of_lt {a b : nat} (h : a < b) : a % b = a := begin cases b, { exfalso, exact not_succ_le_zero a h }, { rw mod.equations._eqn_2, rw if_neg, exact not_le.mpr h } end lemma zero_mod {b} : 0 % b = 0 := begin cases b, { exact mod_zero }, { apply mod_eq_of_lt, apply nat.zero_lt_succ } end lemma mod_lt {a b} (h : b ≠ 0) : mod a b < b := begin cases b, { exfalso, exact h rfl }, { induction a using nat.case_strong_induction_on with x ih, { rw zero_mod, apply nat.zero_lt_succ }, { apply nat.lt_ge_by_cases, { intro h, rw mod_eq_of_lt h, exact h }, { intro h, rw mod_step h, apply ih, replace h := le_of_succ_le_succ h, rw nat.succ_sub_succ, exact sub_le _ _ } } } end lemma mod_prop {a b} : ∃ k, mod a b * k = a := begin sorry end end sandbox
d863792b8d5c4c00280adcccc51c7fae3c88e2b8
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/synth1.lean
74477ca9bf1d871df2213ba009b7778dccd3f9df
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
1,556
lean
import Lean.Meta open Lean open Lean.Meta class HasCoerce (a b : Type) := (coerce : a → b) def coerce {a b : Type} [HasCoerce a b] : a → b := @HasCoerce.coerce a b _ instance coerceTrans {a b c : Type} [HasCoerce b c] [HasCoerce a b] : HasCoerce a c := ⟨fun x => coerce (coerce x : b)⟩ instance coerceBoolToProp : HasCoerce Bool Prop := ⟨fun y => y = true⟩ instance coerceDecidableEq (x : Bool) : Decidable (coerce x) := inferInstanceAs (Decidable (x = true)) instance coerceNatToBool : HasCoerce Nat Bool := ⟨fun x => x == 0⟩ instance coerceNatToInt : HasCoerce Nat Int := ⟨fun x => Int.ofNat x⟩ def print {α} [ToString α] (a : α) : MetaM Unit := do trace[Meta.synthInstance] (toString a) def tst1 : MetaM Unit := do let inst ← mkAppM `HasCoerce #[mkConst `Nat, mkSort levelZero] let r ← synthInstance inst print r set_option trace.Meta.synthInstance true in set_option trace.Meta.synthInstance.tryResolve false in #eval tst1 def tst2 : MetaM Unit := do let inst ← mkAppM `Bind #[mkConst `IO] -- globalInstances ← getGlobalInstances -- print (format globalInstances) -- result ← globalInstances.getUnify inst -- print result let r ← synthInstance inst print r pure () set_option trace.Meta.synthInstance true in set_option trace.Meta.synthInstance.tryResolve false in #eval tst2 def tst3 : MetaM Unit := do let inst ← mkAppM `BEq #[mkConst `Nat] let r ← synthInstance inst print r pure () set_option trace.Meta.synthInstance true in set_option trace.Meta.synthInstance.tryResolve false in #eval tst3
030f885504349c6fe2d78e81deba17c4a285f0b1
a46d86797f98e604c71128429409acba8288c1f8
/algebra/lattice/flat.lean
9f9b430fde4492bc28bfcb39581898577b43393a
[]
no_license
tizmd/lean-abstract-interpretation
655213d76e84e093910bb6378796cdb4e1ae3565
ad69622adc082e7009f12b17568662a599779260
refs/heads/master
1,610,518,429,734
1,498,128,216,000
1,498,128,216,000
94,891,623
0
0
null
null
null
null
UTF-8
Lean
false
false
4,490
lean
import algebra.lattice.dcpo universe u open lattice namespace lattice inductive flat (α : Type u) : Type u | bot {} : flat | top {} : flat | elem : Π a : α, flat instance {α : Type u} : has_bot (flat α) := ⟨ flat.bot ⟩ instance {α : Type u} : has_top (flat α) := ⟨ flat.top ⟩ namespace flat variables {α : Type u}[decidable_eq α] protected def sup : flat α → flat α → flat α | bot y := y | x bot := x | top _ := top | _ top := top | (elem a) (elem b) := if a = b then elem a else top instance : has_sup (flat α) := ⟨ flat.sup ⟩ @[simp] lemma sup_bot_left (x : flat α) : ⊥ ⊔ x = x := match x with | bot := rfl | top := rfl | (elem _) := rfl end @[simp] lemma sup_bot_right : Π (x : flat α), x ⊔ ⊥ = x | bot := rfl | top := rfl | (elem _) := rfl @[simp] lemma sup_top_left : Π (x : flat α), ⊤ ⊔ x = ⊤ | bot := rfl | top := rfl | (elem _) := rfl @[simp] lemma sup_top_right : Π (x : flat α), x ⊔ ⊤ = ⊤ | bot := rfl | top := rfl | (elem _) := rfl @[simp] lemma sup_idem : Π (x : flat α), x ⊔ x = x | bot := rfl | top := rfl | (elem _) := if_pos rfl lemma sup_comm : Π {x y : flat α}, x ⊔ y = y ⊔ x | bot _ := rfl | top _ := rfl | _ bot := rfl | _ top := rfl | (elem a) (elem b) := if H : a = b then eq.rec_on H (eq.trans sup_idem sup_idem.symm) else begin unfold has_sup.sup, simp [flat.sup], rw if_neg H, rw if_neg (ne.symm H), end protected def inf : flat α → flat α → flat α | bot _ := bot | _ bot := bot | top y := y | x top := x | (elem a) (elem b) := if a = b then elem a else bot instance : has_inf (flat α) := ⟨ flat.inf ⟩ protected def le (x y : flat α) : Prop := y = x ⊔ y instance : has_le (flat α) := ⟨flat.le⟩ protected lemma bot_le {x : flat α} : ⊥ ≤ x := match x with | bot := rfl | top := rfl | elem _ := rfl end lemma eq_of_le_bot {x : flat α} : x ≤ ⊥ → x = ⊥ := begin intro h, cases x, refl, contradiction, contradiction end protected lemma le_top {x : flat α} : x ≤ ⊤ := match x with | bot := rfl | top := rfl | elem _ := rfl end lemma eq_of_top_le {x : flat α} : ⊤ ≤ x → x = ⊤ := begin intro h, cases x, contradiction, refl, contradiction end @[refl] protected lemma le_refl : ∀ x : flat α, x ≤ x := take x, sup_idem.symm @[trans] protected lemma le_trans : ∀ x y z : flat α, x ≤ y → y ≤ z → x ≤ z | bot _ _ := assume hxy hyz, flat.bot_le | top _ _ := assume hxy, eq.rec_on (eq_of_top_le hxy).symm (assume hyz, eq.rec_on (eq_of_top_le hyz).symm (flat.le_refl _)) | _ bot _ := assume hxy hyz, eq.rec_on (eq_of_le_bot hxy).symm flat.bot_le | _ top _ := assume hxy hyz, eq.rec_on (eq_of_top_le hyz).symm flat.le_top | _ _ bot := begin intros hxy hyz, revert hxy, rw eq_of_le_bot hyz, intro hxy, rw eq_of_le_bot hxy, refl end | _ _ top := assume hxy hyz, flat.le_top | (elem a) (elem b) (elem c) := assume hxy hyz, if Hab : a = b then eq.rec_on Hab.symm hyz else begin assert H : elem b = flat.sup (elem a) (elem b), assumption, assert eq : elem b = ⊤, rw H, simp [flat.sup], rw if_neg Hab, refl, contradiction end protected lemma le_antisymm (x y : flat α) : x ≤ y → y ≤ x → x = y := assume hxy hyx, eq.trans hyx (eq.trans sup_comm hxy.symm) protected lemma le_sup_left (x y : flat α) : x ≤ x ⊔ y := end flat end lattice
b19cec950e00012ac3e4196b2464512c73c477c3
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Util/Recognizers.lean
fc074065fca8ee052f0daa5eb09d579dee3fb0d4
[ "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
4,837
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.Environment namespace Lean namespace Expr @[inline] def const? (e : Expr) : Option (Name × List Level) := match e with | Expr.const n us => some (n, us) | _ => none @[inline] def app1? (e : Expr) (fName : Name) : Option Expr := if e.isAppOfArity fName 1 then some e.appArg! else none @[inline] def app2? (e : Expr) (fName : Name) : Option (Expr × Expr) := if e.isAppOfArity fName 2 then some (e.appFn!.appArg!, e.appArg!) else none @[inline] def app3? (e : Expr) (fName : Name) : Option (Expr × Expr × Expr) := if e.isAppOfArity fName 3 then some (e.appFn!.appFn!.appArg!, e.appFn!.appArg!, e.appArg!) else none @[inline] def app4? (e : Expr) (fName : Name) : Option (Expr × Expr × Expr × Expr) := if e.isAppOfArity fName 4 then some (e.appFn!.appFn!.appFn!.appArg!, e.appFn!.appFn!.appArg!, e.appFn!.appArg!, e.appArg!) else none @[inline] def eq? (p : Expr) : Option (Expr × Expr × Expr) := p.app3? ``Eq @[inline] def ne? (p : Expr) : Option (Expr × Expr × Expr) := p.app3? ``Ne @[inline] def iff? (p : Expr) : Option (Expr × Expr) := p.app2? ``Iff @[inline] def eqOrIff? (p : Expr) : Option (Expr × Expr) := if let some (_, lhs, rhs) := p.app3? ``Eq then some (lhs, rhs) else p.iff? @[inline] def not? (p : Expr) : Option Expr := p.app1? ``Not @[inline] def notNot? (p : Expr) : Option Expr := match p.not? with | some p => p.not? | none => none @[inline] def and? (p : Expr) : Option (Expr × Expr) := p.app2? ``And @[inline] def heq? (p : Expr) : Option (Expr × Expr × Expr × Expr) := p.app4? ``HEq def natAdd? (e : Expr) : Option (Expr × Expr) := e.app2? ``Nat.add @[inline] def arrow? : Expr → Option (Expr × Expr) | Expr.forallE _ α β _ => if β.hasLooseBVars then none else some (α, β) | _ => none def isEq (e : Expr) := e.isAppOfArity ``Eq 3 def isHEq (e : Expr) := e.isAppOfArity ``HEq 4 def isIte (e : Expr) := e.isAppOfArity ``ite 5 def isDIte (e : Expr) := e.isAppOfArity ``dite 5 partial def listLit? (e : Expr) : Option (Expr × List Expr) := let rec loop (e : Expr) (acc : List Expr) := if e.isAppOfArity' ``List.nil 1 then some (e.appArg!', acc.reverse) else if e.isAppOfArity' ``List.cons 3 then loop e.appArg!' (e.appFn!'.appArg!' :: acc) else none loop e [] def arrayLit? (e : Expr) : Option (Expr × List Expr) := if e.isAppOfArity' ``List.toArray 2 then listLit? e.appArg!' else none /-- Recognize `α × β` -/ def prod? (e : Expr) : Option (Expr × Expr) := e.app2? ``Prod private def getConstructorVal? (env : Environment) (ctorName : Name) : Option ConstructorVal := match env.find? ctorName with | some (ConstantInfo.ctorInfo v) => v | _ => none def isConstructorApp? (env : Environment) (e : Expr) : Option ConstructorVal := match e with | Expr.lit (Literal.natVal n) => if n == 0 then getConstructorVal? env `Nat.zero else getConstructorVal? env `Nat.succ | _ => match e.getAppFn with | Expr.const n _ => match getConstructorVal? env n with | some v => if v.numParams + v.numFields == e.getAppNumArgs then some v else none | none => none | _ => none def isConstructorApp (env : Environment) (e : Expr) : Bool := e.isConstructorApp? env |>.isSome /-- If `e` is a constructor application, return a pair containing the corresponding `ConstructorVal` and the constructor application arguments. This function treats numerals as constructors. For example, if `e` is the numeral `2`, the result pair is `ConstructorVal` for `Nat.succ`, and the array `#[1]`. The parameter `useRaw` controls how the resulting numeral is represented. If `useRaw := false`, then `mkNatLit` is used, otherwise `mkRawNatLit`. Recall that `mkNatLit` uses the `OfNat.ofNat` application which is the canonical way of representing numerals in the elaborator and tactic framework. We `useRaw := false` in the compiler (aka code generator). -/ def constructorApp? (env : Environment) (e : Expr) (useRaw := false) : Option (ConstructorVal × Array Expr) := do match e with | Expr.lit (Literal.natVal n) => if n == 0 then do let v ← getConstructorVal? env `Nat.zero pure (v, #[]) else do let v ← getConstructorVal? env `Nat.succ pure (v, #[if useRaw then mkRawNatLit (n-1) else mkNatLit (n-1)]) | _ => match e.getAppFn with | Expr.const n _ => do let v ← getConstructorVal? env n if v.numParams + v.numFields == e.getAppNumArgs then pure (v, e.getAppArgs) else none | _ => none end Lean.Expr
d072ffa5df712e032ccdbccc978e99292f7436e0
01ae0d022f2e2fefdaaa898938c1ac1fbce3b3ab
/categories/util/hlist.lean
21a66fa572b599e26721889e89396e0cdb84e98c
[]
no_license
PatrickMassot/lean-category-theory
0f56a83464396a253c28a42dece16c93baf8ad74
ef239978e91f2e1c3b8e88b6e9c64c155dc56c99
refs/heads/master
1,629,739,187,316
1,512,422,659,000
1,512,422,659,000
113,098,786
0
0
null
1,512,424,022,000
1,512,424,022,000
null
UTF-8
Lean
false
false
1,004
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison namespace categories.util inductive {u} hlist : list (Type u) → Type (u+1) | nil : hlist [] | cons : Π {α : Type u} {l : list (Type u)}, α → hlist l → hlist (α::l) notation a :: b := hlist.cons a b notation `[` l:(foldr `, ` (h t, hlist.cons h t) hlist.nil `]`) := l definition hlist.indexed_map { α : Type } { Z W : α → Type } ( f : Π { a : α } ( z : Z a ), W a ) : Π ( X : list α ), hlist (X.map Z) → hlist (X.map W) | list.nil hlist.nil := hlist.nil | (list.cons a X) (hlist.cons z H) := hlist.cons (f z) (hlist.indexed_map X H) -- PROJECT perhaps someone has already done this? -- definition {u} hlist.zip : Π { α β : list (Type u) } ( L : hlist α ) ( R: hlist β ), hlist ((α.zip β).map (λ p, p.1 × p.2)) -- | _ list.nil _ hlist.nil := hlist.nil -- | list.nil _ hlist.nil _ := hlist.nil end categories.util
78a82873f25315cc5f71f35942db563f98c64438
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/group_power/basic.lean
6448fec7ca7dc1e3aaaa1be2749ad1ceaa563eba
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
11,978
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis -/ import algebra.divisibility import algebra.group.commute import data.nat.basic /-! # Power operations on monoids and groups The power operation on monoids and groups. We separate this from group, because it depends on `ℕ`, which in turn depends on other parts of algebra. This module contains lemmas about `a ^ n` and `n • a`, where `n : ℕ` or `n : ℤ`. Further lemmas can be found in `algebra.group_power.lemmas`. The analogous results for groups with zero can be found in `algebra.group_with_zero.power`. ## Notation - `a ^ n` is used as notation for `has_pow.pow a n`; in this file `n : ℕ` or `n : ℤ`. - `n • a` is used as notation for `has_smul.smul n a`; in this file `n : ℕ` or `n : ℤ`. ## Implementation details We adopt the convention that `0^0 = 1`. -/ universes u v w x y z u₁ u₂ variables {α : Type*} {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z} {R : Type u₁} {S : Type u₂} /-! ### Commutativity First we prove some facts about `semiconj_by` and `commute`. They do not require any theory about `pow` and/or `nsmul` and will be useful later in this file. -/ section has_pow variables [has_pow M ℕ] @[simp] lemma pow_ite (P : Prop) [decidable P] (a : M) (b c : ℕ) : a ^ (if P then b else c) = if P then a ^ b else a ^ c := by split_ifs; refl @[simp] lemma ite_pow (P : Prop) [decidable P] (a b : M) (c : ℕ) : (if P then a else b) ^ c = if P then a ^ c else b ^ c := by split_ifs; refl end has_pow section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] @[simp, to_additive one_nsmul] theorem pow_one (a : M) : a^1 = a := by rw [pow_succ, pow_zero, mul_one] /-- Note that most of the lemmas about powers of two refer to it as `sq`. -/ @[to_additive two_nsmul, nolint to_additive_doc] theorem pow_two (a : M) : a^2 = a * a := by rw [pow_succ, pow_one] alias pow_two ← sq @[to_additive] theorem pow_mul_comm' (a : M) (n : ℕ) : a^n * a = a * a^n := commute.pow_self a n @[to_additive add_nsmul] theorem pow_add (a : M) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [nat.add_zero, pow_zero, mul_one], rw [pow_succ', ← mul_assoc, ← ih, ← pow_succ', nat.add_assoc]] @[simp] lemma pow_boole (P : Prop) [decidable P] (a : M) : a ^ (if P then 1 else 0) = if P then a else 1 := by simp -- the attributes are intentionally out of order. `smul_zero` proves `nsmul_zero`. @[to_additive nsmul_zero, simp] theorem one_pow (n : ℕ) : (1 : M)^n = 1 := by induction n with n ih; [exact pow_zero _, rw [pow_succ, ih, one_mul]] @[to_additive mul_nsmul'] theorem pow_mul (a : M) (m n : ℕ) : a^(m * n) = (a^m)^n := begin induction n with n ih, { rw [nat.mul_zero, pow_zero, pow_zero] }, { rw [nat.mul_succ, pow_add, pow_succ', ih] } end @[to_additive nsmul_left_comm] lemma pow_right_comm (a : M) (m n : ℕ) : (a^m)^n = (a^n)^m := by rw [←pow_mul, nat.mul_comm, pow_mul] @[to_additive mul_nsmul] theorem pow_mul' (a : M) (m n : ℕ) : a^(m * n) = (a^n)^m := by rw [nat.mul_comm, pow_mul] @[to_additive nsmul_add_sub_nsmul] theorem pow_mul_pow_sub (a : M) {m n : ℕ} (h : m ≤ n) : a ^ m * a ^ (n - m) = a ^ n := by rw [←pow_add, nat.add_comm, tsub_add_cancel_of_le h] @[to_additive sub_nsmul_nsmul_add] theorem pow_sub_mul_pow (a : M) {m n : ℕ} (h : m ≤ n) : a ^ (n - m) * a ^ m = a ^ n := by rw [←pow_add, tsub_add_cancel_of_le h] /-- If `x ^ n = 1`, then `x ^ m` is the same as `x ^ (m % n)` -/ @[to_additive nsmul_eq_mod_nsmul "If `n • x = 0`, then `m • x` is the same as `(m % n) • x`"] lemma pow_eq_pow_mod {M : Type*} [monoid M] {x : M} (m : ℕ) {n : ℕ} (h : x ^ n = 1) : x ^ m = x ^ (m % n) := begin have t := congr_arg (λ a, x ^ a) (nat.div_add_mod m n).symm, dsimp at t, rw [t, pow_add, pow_mul, h, one_pow, one_mul], end @[to_additive bit0_nsmul] theorem pow_bit0 (a : M) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ @[to_additive bit1_nsmul] theorem pow_bit1 (a : M) (n : ℕ) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] @[to_additive] theorem pow_mul_comm (a : M) (m n : ℕ) : a^m * a^n = a^n * a^m := commute.pow_pow_self a m n @[to_additive] lemma commute.mul_pow {a b : M} (h : commute a b) (n : ℕ) : (a * b) ^ n = a ^ n * b ^ n := nat.rec_on n (by simp only [pow_zero, one_mul]) $ λ n ihn, by simp only [pow_succ, ihn, ← mul_assoc, (h.pow_left n).right_comm] @[to_additive bit0_nsmul'] theorem pow_bit0' (a : M) (n : ℕ) : a ^ bit0 n = (a * a) ^ n := by rw [pow_bit0, (commute.refl a).mul_pow] @[to_additive bit1_nsmul'] theorem pow_bit1' (a : M) (n : ℕ) : a ^ bit1 n = (a * a) ^ n * a := by rw [bit1, pow_succ', pow_bit0'] @[to_additive] lemma pow_mul_pow_eq_one {a b : M} (n : ℕ) (h : a * b = 1) : a ^ n * b ^ n = 1 := begin induction n with n hn, { simp }, { calc a ^ n.succ * b ^ n.succ = a ^ n * a * (b * b ^ n) : by rw [pow_succ', pow_succ] ... = a ^ n * (a * b) * b ^ n : by simp only [mul_assoc] ... = 1 : by simp [h, hn] } end lemma dvd_pow {x y : M} (hxy : x ∣ y) : ∀ {n : ℕ} (hn : n ≠ 0), x ∣ y^n | 0 hn := (hn rfl).elim | (n + 1) hn := by { rw pow_succ, exact hxy.mul_right _ } alias dvd_pow ← has_dvd.dvd.pow lemma dvd_pow_self (a : M) {n : ℕ} (hn : n ≠ 0) : a ∣ a^n := dvd_rfl.pow hn end monoid /-! ### Commutative (additive) monoid -/ section comm_monoid variables [comm_monoid M] [add_comm_monoid A] @[to_additive nsmul_add] theorem mul_pow (a b : M) (n : ℕ) : (a * b)^n = a^n * b^n := (commute.all a b).mul_pow n /-- The `n`th power map on a commutative monoid for a natural `n`, considered as a morphism of monoids. -/ @[to_additive "Multiplication by a natural `n` on a commutative additive monoid, considered as a morphism of additive monoids.", simps] def pow_monoid_hom (n : ℕ) : M →* M := { to_fun := (^ n), map_one' := one_pow _, map_mul' := λ a b, mul_pow a b n } -- the below line causes the linter to complain :-/ -- attribute [simps] pow_monoid_hom nsmul_add_monoid_hom end comm_monoid section div_inv_monoid variable [div_inv_monoid G] open int @[simp, to_additive one_zsmul] theorem zpow_one (a : G) : a ^ (1:ℤ) = a := by { convert pow_one a using 1, exact zpow_coe_nat a 1 } @[to_additive two_zsmul] theorem zpow_two (a : G) : a ^ (2 : ℤ) = a * a := by { convert pow_two a using 1, exact zpow_coe_nat a 2 } @[to_additive neg_one_zsmul] theorem zpow_neg_one (x : G) : x ^ (-1:ℤ) = x⁻¹ := (zpow_neg_succ_of_nat x 0).trans $ congr_arg has_inv.inv (pow_one x) @[to_additive] theorem zpow_neg_coe_of_pos (a : G) : ∀ {n : ℕ}, 0 < n → a ^ -(n:ℤ) = (a ^ n)⁻¹ | (n+1) _ := zpow_neg_succ_of_nat _ _ end div_inv_monoid section division_monoid variables [division_monoid α] {a b : α} @[simp, to_additive] lemma inv_pow (a : α) : ∀ n : ℕ, (a⁻¹) ^ n = (a ^ n)⁻¹ | 0 := by rw [pow_zero, pow_zero, inv_one] | (n + 1) := by rw [pow_succ', pow_succ, inv_pow, mul_inv_rev] -- the attributes are intentionally out of order. `smul_zero` proves `zsmul_zero`. @[to_additive zsmul_zero, simp] lemma one_zpow : ∀ (n : ℤ), (1 : α) ^ n = 1 | (n : ℕ) := by rw [zpow_coe_nat, one_pow] | -[1+ n] := by rw [zpow_neg_succ_of_nat, one_pow, inv_one] @[simp, to_additive neg_zsmul] lemma zpow_neg (a : α) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := div_inv_monoid.zpow_neg' _ _ | 0 := by { change a ^ (0 : ℤ) = (a ^ (0 : ℤ))⁻¹, simp } | -[1+ n] := by { rw [zpow_neg_succ_of_nat, inv_inv, ← zpow_coe_nat], refl } @[to_additive neg_one_zsmul_add] lemma mul_zpow_neg_one (a b : α) : (a * b) ^ (-1 : ℤ) = b ^ (-1 : ℤ) * a ^ (-1 : ℤ) := by simp_rw [zpow_neg_one, mul_inv_rev] @[to_additive zsmul_neg] lemma inv_zpow (a : α) : ∀ 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, to_additive zsmul_neg'] lemma inv_zpow' (a : α) (n : ℤ) : a⁻¹ ^ n = a ^ (-n) := by rw [inv_zpow, zpow_neg] @[to_additive nsmul_zero_sub] lemma one_div_pow (a : α) (n : ℕ) : (1 / a) ^ n = 1 / a ^ n := by simp_rw [one_div, inv_pow] @[to_additive zsmul_zero_sub] lemma one_div_zpow (a : α) (n : ℤ) : (1 / a) ^ n = 1 / a ^ n := by simp_rw [one_div, inv_zpow] @[to_additive add_commute.zsmul_add] protected lemma commute.mul_zpow (h : commute a b) : ∀ (i : ℤ), (a * b) ^ i = a ^ i * b ^ i | (n : ℕ) := by simp [h.mul_pow n] | -[1+n] := by simp [h.mul_pow, (h.pow_pow _ _).eq, mul_inv_rev] end division_monoid section division_comm_monoid variables [division_comm_monoid α] @[to_additive zsmul_add] lemma mul_zpow (a b : α) : ∀ n : ℤ, (a * b) ^ n = a ^ n * b ^ n := (commute.all a b).mul_zpow @[simp, to_additive nsmul_sub] lemma div_pow (a b : α) (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := by simp only [div_eq_mul_inv, mul_pow, inv_pow] @[simp, to_additive zsmul_sub] lemma div_zpow (a b : α) (n : ℤ) : (a / b) ^ n = a ^ n / b ^ n := by simp only [div_eq_mul_inv, mul_zpow, inv_zpow] /-- The `n`-th power map (for an integer `n`) on a commutative group, considered as a group homomorphism. -/ @[to_additive "Multiplication by an integer `n` on a commutative additive group, considered as an additive group homomorphism.", simps] def zpow_group_hom (n : ℤ) : α →* α := { to_fun := (^ n), map_one' := one_zpow n, map_mul' := λ a b, mul_zpow a b n } end division_comm_monoid section group variables [group G] [group H] [add_group A] [add_group B] @[to_additive sub_nsmul] lemma pow_sub (a : G) {m n : ℕ} (h : n ≤ m) : a^(m - n) = a^m * (a^n)⁻¹ := eq_mul_inv_of_mul_eq $ by rw [←pow_add, tsub_add_cancel_of_le h] @[to_additive] lemma pow_inv_comm (a : G) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := (commute.refl a).inv_left.pow_pow _ _ @[to_additive sub_nsmul_neg] lemma inv_pow_sub (a : G) {m n : ℕ} (h : n ≤ m) : a⁻¹^(m - n) = (a^m)⁻¹ * a^n := by rw [pow_sub a⁻¹ h, inv_pow, inv_pow, inv_inv] end group lemma pow_dvd_pow [monoid R] (a : R) {m n : ℕ} (h : m ≤ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_comm, tsub_add_cancel_of_le h]⟩ theorem pow_dvd_pow_of_dvd [comm_monoid R] {a b : R} (h : a ∣ b) : ∀ n : ℕ, a ^ n ∣ b ^ n | 0 := by rw [pow_zero, pow_zero] | (n+1) := by { rw [pow_succ, pow_succ], exact mul_dvd_mul h (pow_dvd_pow_of_dvd n) } lemma of_add_nsmul [add_monoid A] (x : A) (n : ℕ) : multiplicative.of_add (n • x) = (multiplicative.of_add x)^n := rfl lemma of_add_zsmul [sub_neg_monoid A] (x : A) (n : ℤ) : multiplicative.of_add (n • x) = (multiplicative.of_add x)^n := rfl lemma of_mul_pow [monoid A] (x : A) (n : ℕ) : additive.of_mul (x ^ n) = n • (additive.of_mul x) := rfl lemma of_mul_zpow [div_inv_monoid G] (x : G) (n : ℤ) : additive.of_mul (x ^ n) = n • additive.of_mul x := rfl @[simp, to_additive] lemma semiconj_by.zpow_right [group G] {a x y : G} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (x^m) (y^m) | (n : ℕ) := by simp [zpow_coe_nat, h.pow_right n] | -[1+n] := by simp [(h.pow_right n.succ).inv_right] namespace commute variables [group G] {a b : G} @[simp, to_additive] lemma zpow_right (h : commute a b) (m : ℤ) : commute a (b^m) := h.zpow_right m @[simp, to_additive] lemma zpow_left (h : commute a b) (m : ℤ) : commute (a^m) b := (h.symm.zpow_right m).symm @[to_additive] lemma zpow_zpow (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) := (h.zpow_left m).zpow_right n variables (a) (m n : ℤ) @[simp, to_additive] lemma self_zpow : commute a (a ^ n) := (commute.refl a).zpow_right n @[simp, to_additive] lemma zpow_self : commute (a ^ n) a := (commute.refl a).zpow_left n @[simp, to_additive] lemma zpow_zpow_self : commute (a ^ m) (a ^ n) := (commute.refl a).zpow_zpow m n end commute
75bf2f844f92511137acdc24389c6ca4c7ec4d51
9dc8cecdf3c4634764a18254e94d43da07142918
/src/group_theory/p_group.lean
e7114d7c0122554eb6d605659eaf27dd57580e6f
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
14,663
lean
/- Copyright (c) 2018 . All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Thomas Browning -/ import data.zmod.basic import group_theory.index import group_theory.group_action.conj_act import group_theory.group_action.quotient import group_theory.perm.cycle.type import group_theory.specific_groups.cyclic import tactic.interval_cases /-! # p-groups This file contains a proof that if `G` is a `p`-group acting on a finite set `α`, then the number of fixed points of the action is congruent mod `p` to the cardinality of `α`. It also contains proofs of some corollaries of this lemma about existence of fixed points. -/ open_locale big_operators open fintype mul_action variables (p : ℕ) (G : Type*) [group G] /-- A p-group is a group in which every element has prime power order -/ def is_p_group : Prop := ∀ g : G, ∃ k : ℕ, g ^ (p ^ k) = 1 variables {p} {G} namespace is_p_group lemma iff_order_of [hp : fact p.prime] : is_p_group p G ↔ ∀ g : G, ∃ k : ℕ, order_of g = p ^ k := forall_congr (λ g, ⟨λ ⟨k, hk⟩, exists_imp_exists (by exact λ j, Exists.snd) ((nat.dvd_prime_pow hp.out).mp (order_of_dvd_of_pow_eq_one hk)), exists_imp_exists (λ k hk, by rw [←hk, pow_order_of_eq_one])⟩) lemma of_card [fintype G] {n : ℕ} (hG : card G = p ^ n) : is_p_group p G := λ g, ⟨n, by rw [←hG, pow_card_eq_one]⟩ lemma of_bot : is_p_group p (⊥ : subgroup G) := of_card (subgroup.card_bot.trans (pow_zero p).symm) lemma iff_card [fact p.prime] [fintype G] : is_p_group p G ↔ ∃ n : ℕ, card G = p ^ n := begin have hG : card G ≠ 0 := card_ne_zero, refine ⟨λ h, _, λ ⟨n, hn⟩, of_card hn⟩, suffices : ∀ q ∈ nat.factors (card G), q = p, { use (card G).factors.length, rw [←list.prod_repeat, ←list.eq_repeat_of_mem this, nat.prod_factors hG] }, intros q hq, obtain ⟨hq1, hq2⟩ := (nat.mem_factors hG).mp hq, haveI : fact q.prime := ⟨hq1⟩, obtain ⟨g, hg⟩ := exists_prime_order_of_dvd_card q hq2, obtain ⟨k, hk⟩ := (iff_order_of.mp h) g, exact (hq1.pow_eq_iff.mp (hg.symm.trans hk).symm).1.symm, end section G_is_p_group variables (hG : is_p_group p G) include hG lemma of_injective {H : Type*} [group H] (ϕ : H →* G) (hϕ : function.injective ϕ) : is_p_group p H := begin simp_rw [is_p_group, ←hϕ.eq_iff, ϕ.map_pow, ϕ.map_one], exact λ h, hG (ϕ h), end lemma to_subgroup (H : subgroup G) : is_p_group p H := hG.of_injective H.subtype subtype.coe_injective lemma of_surjective {H : Type*} [group H] (ϕ : G →* H) (hϕ : function.surjective ϕ) : is_p_group p H := begin refine λ h, exists.elim (hϕ h) (λ g hg, exists_imp_exists (λ k hk, _) (hG g)), rw [←hg, ←ϕ.map_pow, hk, ϕ.map_one], end lemma to_quotient (H : subgroup G) [H.normal] : is_p_group p (G ⧸ H) := hG.of_surjective (quotient_group.mk' H) quotient.surjective_quotient_mk' lemma of_equiv {H : Type*} [group H] (ϕ : G ≃* H) : is_p_group p H := hG.of_surjective ϕ.to_monoid_hom ϕ.surjective variables [hp : fact p.prime] include hp lemma index (H : subgroup G) [finite (G ⧸ H)] : ∃ n : ℕ, H.index = p ^ n := begin casesI nonempty_fintype (G ⧸ H), obtain ⟨n, hn⟩ := iff_card.mp (hG.to_quotient H.normal_core), obtain ⟨k, hk1, hk2⟩ := (nat.dvd_prime_pow hp.out).mp ((congr_arg _ (H.normal_core.index_eq_card.trans hn)).mp (subgroup.index_dvd_of_le H.normal_core_le)), exact ⟨k, hk2⟩, end lemma nontrivial_iff_card [fintype G] : nontrivial G ↔ ∃ n > 0, card G = p ^ n := ⟨λ hGnt, let ⟨k, hk⟩ := iff_card.1 hG in ⟨k, nat.pos_of_ne_zero $ λ hk0, by rw [hk0, pow_zero] at hk; exactI fintype.one_lt_card.ne' hk, hk⟩, λ ⟨k, hk0, hk⟩, one_lt_card_iff_nontrivial.1 $ hk.symm ▸ one_lt_pow (fact.out p.prime).one_lt (ne_of_gt hk0)⟩ variables {α : Type*} [mul_action G α] lemma card_orbit (a : α) [fintype (orbit G a)] : ∃ n : ℕ, card (orbit G a) = p ^ n := begin let ϕ := orbit_equiv_quotient_stabilizer G a, haveI := fintype.of_equiv (orbit G a) ϕ, rw [card_congr ϕ, ←subgroup.index_eq_card], exact hG.index (stabilizer G a), end variables (α) [fintype α] /-- If `G` is a `p`-group acting on a finite set `α`, then the number of fixed points of the action is congruent mod `p` to the cardinality of `α` -/ lemma card_modeq_card_fixed_points [fintype (fixed_points G α)] : card α ≡ card (fixed_points G α) [MOD p] := begin classical, calc card α = card (Σ y : quotient (orbit_rel G α), {x // quotient.mk' x = y}) : card_congr (equiv.sigma_fiber_equiv (@quotient.mk' _ (orbit_rel G α))).symm ... = ∑ a : quotient (orbit_rel G α), card {x // quotient.mk' x = a} : card_sigma _ ... ≡ ∑ a : fixed_points G α, 1 [MOD p] : _ ... = _ : by simp; refl, rw [←zmod.eq_iff_modeq_nat p, nat.cast_sum, nat.cast_sum], have key : ∀ x, card {y // (quotient.mk' y : quotient (orbit_rel G α)) = quotient.mk' x} = card (orbit G x) := λ x, by simp only [quotient.eq']; congr, refine eq.symm (finset.sum_bij_ne_zero (λ a _ _, quotient.mk' a.1) (λ _ _ _, finset.mem_univ _) (λ a₁ a₂ _ _ _ _ h, subtype.eq ((mem_fixed_points' α).mp a₂.2 a₁.1 (quotient.exact' h))) (λ b, quotient.induction_on' b (λ b _ hb, _)) (λ a ha _, by { rw [key, mem_fixed_points_iff_card_orbit_eq_one.mp a.2] })), obtain ⟨k, hk⟩ := hG.card_orbit b, have : k = 0 := le_zero_iff.1 (nat.le_of_lt_succ (lt_of_not_ge (mt (pow_dvd_pow p) (by rwa [pow_one, ←hk, ←nat.modeq_zero_iff_dvd, ←zmod.eq_iff_modeq_nat, ←key, nat.cast_zero])))), exact ⟨⟨b, mem_fixed_points_iff_card_orbit_eq_one.2 $ by rw [hk, this, pow_zero]⟩, finset.mem_univ _, (ne_of_eq_of_ne nat.cast_one one_ne_zero), rfl⟩, end /-- If a p-group acts on `α` and the cardinality of `α` is not a multiple of `p` then the action has a fixed point. -/ lemma nonempty_fixed_point_of_prime_not_dvd_card (hpα : ¬ p ∣ card α) [finite (fixed_points G α)] : (fixed_points G α).nonempty := @set.nonempty_of_nonempty_subtype _ _ begin casesI nonempty_fintype (fixed_points G α), rw [←card_pos_iff, pos_iff_ne_zero], contrapose! hpα, rw [←nat.modeq_zero_iff_dvd, ←hpα], exact hG.card_modeq_card_fixed_points α, end /-- If a p-group acts on `α` and the cardinality of `α` is a multiple of `p`, and the action has one fixed point, then it has another fixed point. -/ lemma exists_fixed_point_of_prime_dvd_card_of_fixed_point (hpα : p ∣ card α) {a : α} (ha : a ∈ fixed_points G α) : ∃ b, b ∈ fixed_points G α ∧ a ≠ b := begin casesI nonempty_fintype (fixed_points G α), have hpf : p ∣ card (fixed_points G α) := nat.modeq_zero_iff_dvd.mp ((hG.card_modeq_card_fixed_points α).symm.trans hpα.modeq_zero_nat), have hα : 1 < card (fixed_points G α) := (fact.out p.prime).one_lt.trans_le (nat.le_of_dvd (card_pos_iff.2 ⟨⟨a, ha⟩⟩) hpf), exact let ⟨⟨b, hb⟩, hba⟩ := exists_ne_of_one_lt_card hα ⟨a, ha⟩ in ⟨b, hb, λ hab, hba (by simp_rw [hab])⟩ end lemma center_nontrivial [nontrivial G] [finite G] : nontrivial (subgroup.center G) := begin classical, casesI nonempty_fintype G, have := (hG.of_equiv conj_act.to_conj_act).exists_fixed_point_of_prime_dvd_card_of_fixed_point G, rw conj_act.fixed_points_eq_center at this, obtain ⟨g, hg⟩ := this _ (subgroup.center G).one_mem, { exact ⟨⟨1, ⟨g, hg.1⟩, mt subtype.ext_iff.mp hg.2⟩⟩ }, { obtain ⟨n, hn0, hn⟩ := hG.nontrivial_iff_card.mp infer_instance, exact hn.symm ▸ dvd_pow_self _ (ne_of_gt hn0) }, end lemma bot_lt_center [nontrivial G] [finite G] : ⊥ < subgroup.center G := begin haveI := center_nontrivial hG, casesI nonempty_fintype G, classical, exact bot_lt_iff_ne_bot.mpr ((subgroup.center G).one_lt_card_iff_ne_bot.mp fintype.one_lt_card), end end G_is_p_group lemma to_le {H K : subgroup G} (hK : is_p_group p K) (hHK : H ≤ K) : is_p_group p H := hK.of_injective (subgroup.inclusion hHK) (λ a b h, subtype.ext (show _, from subtype.ext_iff.mp h)) lemma to_inf_left {H K : subgroup G} (hH : is_p_group p H) : is_p_group p (H ⊓ K : subgroup G) := hH.to_le inf_le_left lemma to_inf_right {H K : subgroup G} (hK : is_p_group p K) : is_p_group p (H ⊓ K : subgroup G) := hK.to_le inf_le_right lemma map {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : G →* K) : is_p_group p (H.map ϕ) := begin rw [←H.subtype_range, monoid_hom.map_range], exact hH.of_surjective (ϕ.restrict H).range_restrict (ϕ.restrict H).range_restrict_surjective, end lemma comap_of_ker_is_p_group {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : K →* G) (hϕ : is_p_group p ϕ.ker) : is_p_group p (H.comap ϕ) := begin intro g, obtain ⟨j, hj⟩ := hH ⟨ϕ g.1, g.2⟩, rw [subtype.ext_iff, H.coe_pow, subtype.coe_mk, ←ϕ.map_pow] at hj, obtain ⟨k, hk⟩ := hϕ ⟨g.1 ^ p ^ j, hj⟩, rwa [subtype.ext_iff, ϕ.ker.coe_pow, subtype.coe_mk, ←pow_mul, ←pow_add] at hk, exact ⟨j + k, by rwa [subtype.ext_iff, (H.comap ϕ).coe_pow]⟩, end lemma ker_is_p_group_of_injective {K : Type*} [group K] {ϕ : K →* G} (hϕ : function.injective ϕ) : is_p_group p ϕ.ker := (congr_arg (λ Q : subgroup K, is_p_group p Q) (ϕ.ker_eq_bot_iff.mpr hϕ)).mpr is_p_group.of_bot lemma comap_of_injective {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : K →* G) (hϕ : function.injective ϕ) : is_p_group p (H.comap ϕ) := hH.comap_of_ker_is_p_group ϕ (ker_is_p_group_of_injective hϕ) lemma comap_subtype {H : subgroup G} (hH : is_p_group p H) {K : subgroup G} : is_p_group p (H.comap K.subtype) := hH.comap_of_injective K.subtype subtype.coe_injective lemma to_sup_of_normal_right {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) [K.normal] : is_p_group p (H ⊔ K : subgroup G) := begin rw [←quotient_group.ker_mk K, ←subgroup.comap_map_eq], apply (hH.map (quotient_group.mk' K)).comap_of_ker_is_p_group, rwa quotient_group.ker_mk, end lemma to_sup_of_normal_left {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) [H.normal] : is_p_group p (H ⊔ K : subgroup G) := (congr_arg (λ H : subgroup G, is_p_group p H) sup_comm).mp (to_sup_of_normal_right hK hH) lemma to_sup_of_normal_right' {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) (hHK : H ≤ K.normalizer) : is_p_group p (H ⊔ K : subgroup G) := let hHK' := to_sup_of_normal_right (hH.of_equiv (subgroup.comap_subtype_equiv_of_le hHK).symm) (hK.of_equiv (subgroup.comap_subtype_equiv_of_le subgroup.le_normalizer).symm) in ((congr_arg (λ H : subgroup K.normalizer, is_p_group p H) (subgroup.sup_subgroup_of_eq hHK subgroup.le_normalizer)).mp hHK').of_equiv (subgroup.comap_subtype_equiv_of_le (sup_le hHK subgroup.le_normalizer)) lemma to_sup_of_normal_left' {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) (hHK : K ≤ H.normalizer) : is_p_group p (H ⊔ K : subgroup G) := (congr_arg (λ H : subgroup G, is_p_group p H) sup_comm).mp (to_sup_of_normal_right' hK hH hHK) /-- finite p-groups with different p have coprime orders -/ lemma coprime_card_of_ne {G₂ : Type*} [group G₂] (p₁ p₂ : ℕ) [hp₁ : fact p₁.prime] [hp₂ : fact p₂.prime] (hne : p₁ ≠ p₂) (H₁ : subgroup G) (H₂ : subgroup G₂) [fintype H₁] [fintype H₂] (hH₁ : is_p_group p₁ H₁) (hH₂ : is_p_group p₂ H₂) : nat.coprime (fintype.card H₁) (fintype.card H₂) := begin obtain ⟨n₁, heq₁⟩ := iff_card.mp hH₁, rw heq₁, clear heq₁, obtain ⟨n₂, heq₂⟩ := iff_card.mp hH₂, rw heq₂, clear heq₂, exact nat.coprime_pow_primes _ _ (hp₁.elim) (hp₂.elim) hne, end /-- p-groups with different p are disjoint -/ lemma disjoint_of_ne (p₁ p₂ : ℕ) [hp₁ : fact p₁.prime] [hp₂ : fact p₂.prime] (hne : p₁ ≠ p₂) (H₁ H₂ : subgroup G) (hH₁ : is_p_group p₁ H₁) (hH₂ : is_p_group p₂ H₂) : disjoint H₁ H₂ := begin rintro x ⟨hx₁, hx₂⟩, rw subgroup.mem_bot, obtain ⟨n₁, hn₁⟩ := iff_order_of.mp hH₁ ⟨x, hx₁⟩, obtain ⟨n₂, hn₂⟩ := iff_order_of.mp hH₂ ⟨x, hx₂⟩, rw [← order_of_subgroup, subgroup.coe_mk] at hn₁ hn₂, have : p₁ ^ n₁ = p₂ ^ n₂, by rw [← hn₁, ← hn₂], have : n₁ = 0, { contrapose! hne with h, rw ← associated_iff_eq at this ⊢, exact associated.of_pow_associated_of_prime (nat.prime_iff.mp hp₁.elim) (nat.prime_iff.mp hp₂.elim) (ne.bot_lt h) this }, simpa [this] using hn₁, end section p2comm variables [fintype G] [fact p.prime] {n : ℕ} (hGpn : card G = p ^ n) include hGpn open subgroup /-- The cardinality of the `center` of a `p`-group is `p ^ k` where `k` is positive. -/ lemma card_center_eq_prime_pow (hn : 0 < n) [fintype (center G)] : ∃ k > 0, card (center G) = p ^ k := begin have hcG := to_subgroup (of_card hGpn) (center G), rcases iff_card.1 hcG with ⟨k, hk⟩, haveI : nontrivial G := (nontrivial_iff_card $ of_card hGpn).2 ⟨n, hn, hGpn⟩, exact (nontrivial_iff_card hcG).mp (center_nontrivial (of_card hGpn)), end omit hGpn /-- The quotient by the center of a group of cardinality `p ^ 2` is cyclic. -/ lemma cyclic_center_quotient_of_card_eq_prime_sq (hG : card G = p ^ 2) : is_cyclic (G ⧸ (center G)) := begin classical, rcases card_center_eq_prime_pow hG zero_lt_two with ⟨k, hk0, hk⟩, rw [card_eq_card_quotient_mul_card_subgroup (center G), mul_comm, hk] at hG, have hk2 := (nat.pow_dvd_pow_iff_le_right (fact.out p.prime).one_lt).1 ⟨_, hG.symm⟩, interval_cases k, { rw [sq, pow_one, nat.mul_right_inj (fact.out p.prime).pos] at hG, exact is_cyclic_of_prime_card hG }, { exact @is_cyclic_of_subsingleton _ _ ⟨fintype.card_le_one_iff.1 ((nat.mul_right_inj (pow_pos (fact.out p.prime).pos 2)).1 (hG.trans (mul_one (p ^ 2)).symm)).le⟩ }, end /-- A group of order `p ^ 2` is commutative. See also `is_p_group.commutative_of_card_eq_prime_sq` for just the proof that `∀ a b, a * b = b * a` -/ def comm_group_of_card_eq_prime_sq (hG : card G = p ^ 2) : comm_group G := @comm_group_of_cycle_center_quotient _ _ _ _ (cyclic_center_quotient_of_card_eq_prime_sq hG) _ (quotient_group.ker_mk (center G)).le /-- A group of order `p ^ 2` is commutative. See also `is_p_group.comm_group_of_card_eq_prime_sq` for the `comm_group` instance. -/ lemma commutative_of_card_eq_prime_sq (hG : card G = p ^ 2) : ∀ a b : G, a * b = b * a := (comm_group_of_card_eq_prime_sq hG).mul_comm end p2comm end is_p_group
b8eab33c09aea486e02430ab9bfade3db9419131
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/padics/padic_integers.lean
2adc613e6fa8b6a4b3ceca0297fe6783976a39e1
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,816
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Mario Carneiro, Johan Commelin -/ import data.int.modeq import data.zmod.basic import linear_algebra.adic_completion import data.padics.padic_numbers import ring_theory.discrete_valuation_ring import topology.metric_space.cau_seq_filter /-! # p-adic integers This file defines the p-adic integers `ℤ_p` as the subtype of `ℚ_p` with norm `≤ 1`. We show that `ℤ_p` * is complete * is nonarchimedean * is a normed ring * is a local ring * is a discrete valuation ring The relation between `ℤ_[p]` and `zmod p` is established in another file. ## Important definitions * `padic_int` : the type of p-adic numbers ## Notation We introduce the notation `ℤ_[p]` for the p-adic integers. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact (nat.prime p)] as a type class argument. Coercions into `ℤ_p` are set up to work with the `norm_cast` tactic. ## References * [F. Q. Gouêva, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, p-adic integer -/ open nat padic metric local_ring noncomputable theory open_locale classical /-- The p-adic integers ℤ_p are the p-adic numbers with norm ≤ 1. -/ def padic_int (p : ℕ) [fact p.prime] := {x : ℚ_[p] // ∥x∥ ≤ 1} notation `ℤ_[`p`]` := padic_int p namespace padic_int /-! ### Ring structure and coercion to `ℚ_[p]` -/ variables {p : ℕ} [fact p.prime] instance : has_coe ℤ_[p] ℚ_[p] := ⟨subtype.val⟩ lemma ext {x y : ℤ_[p]} : (x : ℚ_[p]) = y → x = y := subtype.ext_iff_val.2 /-- Addition on ℤ_p is inherited from ℚ_p. -/ instance : has_add ℤ_[p] := ⟨λ ⟨x, hx⟩ ⟨y, hy⟩, ⟨x+y, le_trans (padic_norm_e.nonarchimedean _ _) (max_le_iff.2 ⟨hx,hy⟩)⟩⟩ /-- Multiplication on ℤ_p is inherited from ℚ_p. -/ instance : has_mul ℤ_[p] := ⟨λ ⟨x, hx⟩ ⟨y, hy⟩, ⟨x*y, begin rw padic_norm_e.mul, apply mul_le_one; {assumption <|> apply norm_nonneg} end⟩⟩ /-- Negation on ℤ_p is inherited from ℚ_p. -/ instance : has_neg ℤ_[p] := ⟨λ ⟨x, hx⟩, ⟨-x, by simpa⟩⟩ /-- Zero on ℤ_p is inherited from ℚ_p. -/ instance : has_zero ℤ_[p] := ⟨⟨0, by norm_num⟩⟩ instance : inhabited ℤ_[p] := ⟨0⟩ /-- One on ℤ_p is inherited from ℚ_p. -/ instance : has_one ℤ_[p] := ⟨⟨1, by norm_num⟩⟩ @[simp] lemma mk_zero {h} : (⟨0, h⟩ : ℤ_[p]) = (0 : ℤ_[p]) := rfl @[simp] lemma val_eq_coe (z : ℤ_[p]) : z.val = z := rfl @[simp, norm_cast] lemma coe_add : ∀ (z1 z2 : ℤ_[p]), ((z1 + z2 : ℤ_[p]) : ℚ_[p]) = z1 + z2 | ⟨_, _⟩ ⟨_, _⟩ := rfl @[simp, norm_cast] lemma coe_mul : ∀ (z1 z2 : ℤ_[p]), ((z1 * z2 : ℤ_[p]) : ℚ_[p]) = z1 * z2 | ⟨_, _⟩ ⟨_, _⟩ := rfl @[simp, norm_cast] lemma coe_neg : ∀ (z1 : ℤ_[p]), ((-z1 : ℤ_[p]) : ℚ_[p]) = -z1 | ⟨_, _⟩ := rfl @[simp, norm_cast] lemma coe_one : ((1 : ℤ_[p]) : ℚ_[p]) = 1 := rfl @[simp, norm_cast] lemma coe_coe : ∀ n : ℕ, ((n : ℤ_[p]) : ℚ_[p]) = n | 0 := rfl | (k+1) := by simp [coe_coe] @[simp, norm_cast] lemma coe_coe_int : ∀ (z : ℤ), ((z : ℤ_[p]) : ℚ_[p]) = z | (int.of_nat n) := by simp | -[1+n] := by simp @[simp, norm_cast] lemma coe_zero : ((0 : ℤ_[p]) : ℚ_[p]) = 0 := rfl instance : ring ℤ_[p] := begin refine { add := (+), mul := (*), neg := has_neg.neg, zero := 0, one := 1, .. }; intros; ext; simp; ring end /-- The coercion from ℤ[p] to ℚ[p] as a ring homomorphism. -/ def coe.ring_hom : ℤ_[p] →+* ℚ_[p] := { to_fun := (coe : ℤ_[p] → ℚ_[p]), map_zero' := rfl, map_one' := rfl, map_mul' := coe_mul, map_add' := coe_add } @[simp, norm_cast] lemma coe_sub : ∀ (z1 z2 : ℤ_[p]), (↑(z1 - z2) : ℚ_[p]) = ↑z1 - ↑z2 := coe.ring_hom.map_sub @[simp, norm_cast] lemma coet_pow (x : ℤ_[p]) (n : ℕ) : (↑(x^n) : ℚ_[p]) = (↑x : ℚ_[p])^n := coe.ring_hom.map_pow x n @[simp] lemma mk_coe : ∀ (k : ℤ_[p]), (⟨k, k.2⟩ : ℤ_[p]) = k | ⟨_, _⟩ := rfl /-- The inverse of a p-adic integer with norm equal to 1 is also a p-adic integer. Otherwise, the inverse is defined to be 0. -/ def inv : ℤ_[p] → ℤ_[p] | ⟨k, _⟩ := if h : ∥k∥ = 1 then ⟨1/k, by simp [h]⟩ else 0 instance : char_zero ℤ_[p] := { cast_injective := λ m n h, cast_injective $ show (m:ℚ_[p]) = n, by { rw subtype.ext_iff at h, norm_cast at h, exact h } } @[simp, norm_cast] lemma coe_int_eq (z1 z2 : ℤ) : (z1 : ℤ_[p]) = z2 ↔ z1 = z2 := suffices (z1 : ℚ_[p]) = z2 ↔ z1 = z2, from iff.trans (by norm_cast) this, by norm_cast /-- A sequence of integers that is Cauchy with respect to the `p`-adic norm converges to a `p`-adic integer. -/ def of_int_seq (seq : ℕ → ℤ) (h : is_cau_seq (padic_norm p) (λ n, seq n)) : ℤ_[p] := ⟨⟦⟨_, h⟩⟧, show ↑(padic_seq.norm _) ≤ (1 : ℝ), begin rw padic_seq.norm, split_ifs with hne; norm_cast, { exact zero_le_one }, { apply padic_norm.of_int } end ⟩ end padic_int namespace padic_int /-! ### Instances We now show that `ℤ_[p]` is a * complete metric space * normed ring * integral domain -/ variables (p : ℕ) [fact p.prime] instance : metric_space ℤ_[p] := subtype.metric_space instance complete_space : complete_space ℤ_[p] := begin delta padic_int, rw [complete_space_iff_is_complete_range uniform_embedding_subtype_coe, subtype.range_coe_subtype], have : is_complete (closed_ball (0 : ℚ_[p]) 1) := is_closed_ball.is_complete, simpa [closed_ball], end instance : has_norm ℤ_[p] := ⟨λ z, ∥(z : ℚ_[p])∥⟩ variables {p} protected lemma mul_comm : ∀ z1 z2 : ℤ_[p], z1*z2 = z2*z1 | ⟨q1, h1⟩ ⟨q2, h2⟩ := show (⟨q1*q2, _⟩ : ℤ_[p]) = ⟨q2*q1, _⟩, by simp [_root_.mul_comm] protected lemma zero_ne_one : (0 : ℤ_[p]) ≠ 1 := show (⟨(0 : ℚ_[p]), _⟩ : ℤ_[p]) ≠ ⟨(1 : ℚ_[p]), _⟩, from mt subtype.ext_iff_val.1 zero_ne_one protected lemma eq_zero_or_eq_zero_of_mul_eq_zero : ∀ (a b : ℤ_[p]), a * b = 0 → a = 0 ∨ b = 0 | ⟨a, ha⟩ ⟨b, hb⟩ := λ h : (⟨a * b, _⟩ : ℤ_[p]) = ⟨0, _⟩, have a * b = 0, from subtype.ext_iff_val.1 h, (mul_eq_zero.1 this).elim (λ h1, or.inl (by simp [h1]; refl)) (λ h2, or.inr (by simp [h2]; refl)) lemma norm_def {z : ℤ_[p]} : ∥z∥ = ∥(z : ℚ_[p])∥ := rfl variables (p) instance : normed_comm_ring ℤ_[p] := { dist_eq := λ ⟨_, _⟩ ⟨_, _⟩, rfl, norm_mul := λ ⟨_, _⟩ ⟨_, _⟩, norm_mul_le _ _, mul_comm := padic_int.mul_comm } instance : norm_one_class ℤ_[p] := ⟨norm_def.trans norm_one⟩ instance : is_absolute_value (λ z : ℤ_[p], ∥z∥) := { abv_nonneg := norm_nonneg, abv_eq_zero := λ ⟨_, _⟩, by simp [norm_eq_zero], abv_add := λ ⟨_,_⟩ ⟨_, _⟩, norm_add_le _ _, abv_mul := λ _ _, by simp only [norm_def, padic_norm_e.mul, padic_int.coe_mul]} variables {p} instance : integral_domain ℤ_[p] := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y, padic_int.eq_zero_or_eq_zero_of_mul_eq_zero x y, exists_pair_ne := ⟨0, 1, padic_int.zero_ne_one⟩, .. padic_int.normed_comm_ring p } end padic_int namespace padic_int /-! ### Norm -/ variables {p : ℕ} [fact p.prime] lemma norm_le_one : ∀ z : ℤ_[p], ∥z∥ ≤ 1 | ⟨_, h⟩ := h @[simp] lemma norm_mul (z1 z2 : ℤ_[p]) : ∥z1 * z2∥ = ∥z1∥ * ∥z2∥ := by simp [norm_def] @[simp] lemma norm_pow (z : ℤ_[p]) : ∀ n : ℕ, ∥z^n∥ = ∥z∥^n | 0 := by simp | (k+1) := show ∥z*z^k∥ = ∥z∥*∥z∥^k, by {rw norm_mul, congr, apply norm_pow} theorem nonarchimedean : ∀ (q r : ℤ_[p]), ∥q + r∥ ≤ max (∥q∥) (∥r∥) | ⟨_, _⟩ ⟨_, _⟩ := padic_norm_e.nonarchimedean _ _ theorem norm_add_eq_max_of_ne : ∀ {q r : ℤ_[p]}, ∥q∥ ≠ ∥r∥ → ∥q+r∥ = max (∥q∥) (∥r∥) | ⟨_, _⟩ ⟨_, _⟩ := padic_norm_e.add_eq_max_of_ne lemma norm_eq_of_norm_add_lt_right {z1 z2 : ℤ_[p]} (h : ∥z1 + z2∥ < ∥z2∥) : ∥z1∥ = ∥z2∥ := by_contradiction $ λ hne, not_lt_of_ge (by rw norm_add_eq_max_of_ne hne; apply le_max_right) h lemma norm_eq_of_norm_add_lt_left {z1 z2 : ℤ_[p]} (h : ∥z1 + z2∥ < ∥z1∥) : ∥z1∥ = ∥z2∥ := by_contradiction $ λ hne, not_lt_of_ge (by rw norm_add_eq_max_of_ne hne; apply le_max_left) h @[simp] lemma padic_norm_e_of_padic_int (z : ℤ_[p]) : ∥(↑z : ℚ_[p])∥ = ∥z∥ := by simp [norm_def] lemma norm_int_cast_eq_padic_norm (z : ℤ) : ∥(z : ℤ_[p])∥ = ∥(z : ℚ_[p])∥ := by simp [norm_def] @[simp] lemma norm_eq_padic_norm {q : ℚ_[p]} (hq : ∥q∥ ≤ 1) : @norm ℤ_[p] _ ⟨q, hq⟩ = ∥q∥ := rfl @[simp] lemma norm_p : ∥(p : ℤ_[p])∥ = p⁻¹ := show ∥((p : ℤ_[p]) : ℚ_[p])∥ = p⁻¹, by exact_mod_cast padic_norm_e.norm_p @[simp] lemma norm_p_pow (n : ℕ) : ∥(p : ℤ_[p])^n∥ = p^(-n:ℤ) := show ∥((p^n : ℤ_[p]) : ℚ_[p])∥ = p^(-n:ℤ), by { convert padic_norm_e.norm_p_pow n, simp, } private def cau_seq_to_rat_cau_seq (f : cau_seq ℤ_[p] norm) : cau_seq ℚ_[p] (λ a, ∥a∥) := ⟨ λ n, f n, λ _ hε, by simpa [norm, norm_def] using f.cauchy hε ⟩ variables (p) instance complete : cau_seq.is_complete ℤ_[p] norm := ⟨ λ f, have hqn : ∥cau_seq.lim (cau_seq_to_rat_cau_seq f)∥ ≤ 1, from padic_norm_e_lim_le zero_lt_one (λ _, norm_le_one _), ⟨ ⟨_, hqn⟩, λ ε, by simpa [norm, norm_def] using cau_seq.equiv_lim (cau_seq_to_rat_cau_seq f) ε⟩⟩ end padic_int namespace padic_int variables (p : ℕ) [hp_prime : fact p.prime] include hp_prime lemma exists_pow_neg_lt {ε : ℝ} (hε : 0 < ε) : ∃ (k : ℕ), ↑p ^ -((k : ℕ) : ℤ) < ε := begin obtain ⟨k, hk⟩ := exists_nat_gt ε⁻¹, use k, rw ← inv_lt_inv hε (_root_.fpow_pos_of_pos _ _), { rw [fpow_neg, inv_inv', fpow_coe_nat], apply lt_of_lt_of_le hk, norm_cast, apply le_of_lt, convert nat.lt_pow_self _ _ using 1, exact hp_prime.one_lt }, { exact_mod_cast hp_prime.pos } end lemma exists_pow_neg_lt_rat {ε : ℚ} (hε : 0 < ε) : ∃ (k : ℕ), ↑p ^ -((k : ℕ) : ℤ) < ε := begin obtain ⟨k, hk⟩ := @exists_pow_neg_lt p _ ε (by exact_mod_cast hε), use k, rw (show (p : ℝ) = (p : ℚ), by simp) at hk, exact_mod_cast hk end variable {p} lemma norm_int_lt_one_iff_dvd (k : ℤ) : ∥(k : ℤ_[p])∥ < 1 ↔ ↑p ∣ k := suffices ∥(k : ℚ_[p])∥ < 1 ↔ ↑p ∣ k, by rwa norm_int_cast_eq_padic_norm, padic_norm_e.norm_int_lt_one_iff_dvd k lemma norm_int_le_pow_iff_dvd {k : ℤ} {n : ℕ} : ∥(k : ℤ_[p])∥ ≤ ((↑p)^(-n : ℤ)) ↔ ↑p^n ∣ k := suffices ∥(k : ℚ_[p])∥ ≤ ((↑p)^(-n : ℤ)) ↔ ↑(p^n) ∣ k, by simpa [norm_int_cast_eq_padic_norm], padic_norm_e.norm_int_le_pow_iff_dvd _ _ /-! ### Valuation on `ℤ_[p]` -/ /-- `padic_int.valuation` lifts the p-adic valuation on `ℚ` to `ℤ_[p]`. -/ def valuation (x : ℤ_[p]) := padic.valuation (x : ℚ_[p]) lemma norm_eq_pow_val {x : ℤ_[p]} (hx : x ≠ 0) : ∥x∥ = p^(-x.valuation) := begin convert padic.norm_eq_pow_val _, contrapose! hx, exact subtype.val_injective hx end @[simp] lemma valuation_zero : valuation (0 : ℤ_[p]) = 0 := padic.valuation_zero @[simp] lemma valuation_one : valuation (1 : ℤ_[p]) = 0 := padic.valuation_one @[simp] lemma valuation_p : valuation (p : ℤ_[p]) = 1 := by simp [valuation, -cast_eq_of_rat_of_nat] lemma valuation_nonneg (x : ℤ_[p]) : 0 ≤ x.valuation := begin by_cases hx : x = 0, { simp [hx] }, have h : (1 : ℝ) < p := by exact_mod_cast hp_prime.one_lt, rw [← neg_nonpos, ← (fpow_strict_mono h).le_iff_le], show (p : ℝ) ^ -valuation x ≤ p ^ 0, rw [← norm_eq_pow_val hx], simpa using x.property, end @[simp] lemma valuation_p_pow_mul (n : ℕ) (c : ℤ_[p]) (hc : c ≠ 0) : (↑p ^ n * c).valuation = n + c.valuation := begin have : ∥↑p ^ n * c∥ = ∥(p ^ n : ℤ_[p])∥ * ∥c∥, { exact norm_mul _ _ }, have aux : ↑p ^ n * c ≠ 0, { contrapose! hc, rw mul_eq_zero at hc, cases hc, { refine (hp_prime.ne_zero _).elim, exact_mod_cast (pow_eq_zero hc) }, { exact hc } }, rwa [norm_eq_pow_val aux, norm_p_pow, norm_eq_pow_val hc, ← fpow_add, ← neg_add, fpow_inj, neg_inj] at this, { exact_mod_cast hp_prime.pos }, { exact_mod_cast hp_prime.ne_one }, { exact_mod_cast hp_prime.ne_zero }, end section units /-! ### Units of `ℤ_[p]` -/ local attribute [reducible] padic_int lemma mul_inv : ∀ {z : ℤ_[p]}, ∥z∥ = 1 → z * z.inv = 1 | ⟨k, _⟩ h := begin have hk : k ≠ 0, from λ h', @zero_ne_one ℚ_[p] _ _ (by simpa [h'] using h), unfold padic_int.inv, split_ifs, { change (⟨k * (1/k), _⟩ : ℤ_[p]) = 1, simp [hk], refl }, { apply subtype.ext_iff_val.2, simp [mul_inv_cancel hk] } end lemma inv_mul {z : ℤ_[p]} (hz : ∥z∥ = 1) : z.inv * z = 1 := by rw [mul_comm, mul_inv hz] lemma is_unit_iff {z : ℤ_[p]} : is_unit z ↔ ∥z∥ = 1 := ⟨λ h, begin rcases is_unit_iff_dvd_one.1 h with ⟨w, eq⟩, refine le_antisymm (norm_le_one _) _, have := mul_le_mul_of_nonneg_left (norm_le_one w) (norm_nonneg z), rwa [mul_one, ← norm_mul, ← eq, norm_one] at this end, λ h, ⟨⟨z, z.inv, mul_inv h, inv_mul h⟩, rfl⟩⟩ lemma norm_lt_one_add {z1 z2 : ℤ_[p]} (hz1 : ∥z1∥ < 1) (hz2 : ∥z2∥ < 1) : ∥z1 + z2∥ < 1 := lt_of_le_of_lt (nonarchimedean _ _) (max_lt hz1 hz2) lemma norm_lt_one_mul {z1 z2 : ℤ_[p]} (hz2 : ∥z2∥ < 1) : ∥z1 * z2∥ < 1 := calc ∥z1 * z2∥ = ∥z1∥ * ∥z2∥ : by simp ... < 1 : mul_lt_one_of_nonneg_of_lt_one_right (norm_le_one _) (norm_nonneg _) hz2 @[simp] lemma mem_nonunits {z : ℤ_[p]} : z ∈ nonunits ℤ_[p] ↔ ∥z∥ < 1 := by rw lt_iff_le_and_ne; simp [norm_le_one z, nonunits, is_unit_iff] /-- A `p`-adic number `u` with `∥u∥ = 1` is a unit of `ℤ_[p]`. -/ def mk_units {u : ℚ_[p]} (h : ∥u∥ = 1) : units ℤ_[p] := let z : ℤ_[p] := ⟨u, le_of_eq h⟩ in ⟨z, z.inv, mul_inv h, inv_mul h⟩ @[simp] lemma mk_units_eq {u : ℚ_[p]} (h : ∥u∥ = 1) : ((mk_units h : ℤ_[p]) : ℚ_[p]) = u := rfl @[simp] lemma norm_units (u : units ℤ_[p]) : ∥(u : ℤ_[p])∥ = 1 := is_unit_iff.mp $ by simp /-- `unit_coeff hx` is the unit `u` in the unique representation `x = u * p ^ n`. See `unit_coeff_spec`. -/ def unit_coeff {x : ℤ_[p]} (hx : x ≠ 0) : units ℤ_[p] := let u : ℚ_[p] := x*p^(-x.valuation) in have hu : ∥u∥ = 1, by simp [hx, nat.fpow_ne_zero_of_pos (by exact_mod_cast hp_prime.pos) x.valuation, norm_eq_pow_val, fpow_neg, inv_mul_cancel, -cast_eq_of_rat_of_nat], mk_units hu @[simp] lemma unit_coeff_coe {x : ℤ_[p]} (hx : x ≠ 0) : (unit_coeff hx : ℚ_[p]) = x * p ^ (-x.valuation) := rfl lemma unit_coeff_spec {x : ℤ_[p]} (hx : x ≠ 0) : x = (unit_coeff hx : ℤ_[p]) * p ^ int.nat_abs (valuation x) := begin apply subtype.coe_injective, push_cast, have repr : (x : ℚ_[p]) = (unit_coeff hx) * p ^ x.valuation, { rw [unit_coeff_coe, mul_assoc, ← fpow_add], { simp }, { exact_mod_cast hp_prime.ne_zero } }, convert repr using 2, rw [← fpow_coe_nat, int.nat_abs_of_nonneg (valuation_nonneg x)], end end units section norm_le_iff /-! ### Various characterizations of open unit balls -/ lemma norm_le_pow_iff_le_valuation (x : ℤ_[p]) (hx : x ≠ 0) (n : ℕ) : ∥x∥ ≤ p ^ (-n : ℤ) ↔ ↑n ≤ x.valuation := begin rw norm_eq_pow_val hx, lift x.valuation to ℕ using x.valuation_nonneg with k hk, simp only [int.coe_nat_le, fpow_neg, fpow_coe_nat], have aux : ∀ n : ℕ, 0 < (p ^ n : ℝ), { apply pow_pos, exact_mod_cast nat.prime.pos ‹_› }, rw [inv_le_inv (aux _) (aux _)], have : p ^ n ≤ p ^ k ↔ n ≤ k := (pow_right_strict_mono (nat.prime.two_le ‹_›)).le_iff_le, rw [← this], norm_cast, end lemma mem_span_pow_iff_le_valuation (x : ℤ_[p]) (hx : x ≠ 0) (n : ℕ) : x ∈ (ideal.span {p ^ n} : ideal ℤ_[p]) ↔ ↑n ≤ x.valuation := begin rw [ideal.mem_span_singleton], split, { rintro ⟨c, rfl⟩, suffices : c ≠ 0, { rw [valuation_p_pow_mul _ _ this, le_add_iff_nonneg_right], apply valuation_nonneg, }, contrapose! hx, rw [hx, mul_zero], }, { rw [unit_coeff_spec hx] { occs := occurrences.pos [2] }, lift x.valuation to ℕ using x.valuation_nonneg with k hk, simp only [int.nat_abs_of_nat, is_unit_unit, is_unit.dvd_mul_left, int.coe_nat_le], intro H, obtain ⟨k, rfl⟩ := nat.exists_eq_add_of_le H, simp only [pow_add, dvd_mul_right], } end lemma norm_le_pow_iff_mem_span_pow (x : ℤ_[p]) (n : ℕ) : ∥x∥ ≤ p ^ (-n : ℤ) ↔ x ∈ (ideal.span {p ^ n} : ideal ℤ_[p]) := begin by_cases hx : x = 0, { subst hx, simp only [norm_zero, fpow_neg, fpow_coe_nat, inv_nonneg, iff_true, submodule.zero_mem], exact_mod_cast nat.zero_le _ }, rw [norm_le_pow_iff_le_valuation x hx, mem_span_pow_iff_le_valuation x hx], end lemma norm_le_pow_iff_norm_lt_pow_add_one (x : ℤ_[p]) (n : ℤ) : ∥x∥ ≤ p ^ n ↔ ∥x∥ < p ^ (n + 1) := begin have aux : ∀ n : ℤ, 0 < (p ^ n : ℝ), { apply nat.fpow_pos_of_pos, exact nat.prime.pos ‹_› }, by_cases hx0 : x = 0, { simp [hx0, norm_zero, aux, le_of_lt (aux _)], }, rw norm_eq_pow_val hx0, have h1p : 1 < (p : ℝ), { exact_mod_cast nat.prime.one_lt ‹_› }, have H := fpow_strict_mono h1p, rw [H.le_iff_le, H.lt_iff_lt, int.lt_add_one_iff], end lemma norm_lt_pow_iff_norm_le_pow_sub_one (x : ℤ_[p]) (n : ℤ) : ∥x∥ < p ^ n ↔ ∥x∥ ≤ p ^ (n - 1) := by rw [norm_le_pow_iff_norm_lt_pow_add_one, sub_add_cancel] lemma norm_lt_one_iff_dvd (x : ℤ_[p]) : ∥x∥ < 1 ↔ ↑p ∣ x := begin have := norm_le_pow_iff_mem_span_pow x 1, rw [ideal.mem_span_singleton, pow_one] at this, rw [← this, norm_le_pow_iff_norm_lt_pow_add_one], simp only [fpow_zero, int.coe_nat_zero, int.coe_nat_succ, add_left_neg, zero_add], end @[simp] lemma pow_p_dvd_int_iff (n : ℕ) (a : ℤ) : (p ^ n : ℤ_[p]) ∣ a ↔ ↑p ^ n ∣ a := by rw [← norm_int_le_pow_iff_dvd, norm_le_pow_iff_mem_span_pow, ideal.mem_span_singleton] end norm_le_iff section dvr /-! ### Discrete valuation ring -/ instance : local_ring ℤ_[p] := local_of_nonunits_ideal zero_ne_one $ λ x y, by simp; exact norm_lt_one_add lemma p_nonnunit : (p : ℤ_[p]) ∈ nonunits ℤ_[p] := have (p : ℝ)⁻¹ < 1, from inv_lt_one $ by exact_mod_cast hp_prime.one_lt, by simp [this] lemma maximal_ideal_eq_span_p : maximal_ideal ℤ_[p] = ideal.span {p} := begin apply le_antisymm, { intros x hx, rw ideal.mem_span_singleton, simp only [local_ring.mem_maximal_ideal, mem_nonunits] at hx, rwa ← norm_lt_one_iff_dvd, }, { rw [ideal.span_le, set.singleton_subset_iff], exact p_nonnunit } end lemma prime_p : prime (p : ℤ_[p]) := begin rw [← ideal.span_singleton_prime, ← maximal_ideal_eq_span_p], { apply_instance }, { exact_mod_cast hp_prime.ne_zero } end lemma irreducible_p : irreducible (p : ℤ_[p]) := irreducible_of_prime prime_p instance : discrete_valuation_ring ℤ_[p] := discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization ⟨p, irreducible_p, λ x hx, ⟨x.valuation.nat_abs, unit_coeff hx, by rw [mul_comm, ← unit_coeff_spec hx]⟩⟩ lemma ideal_eq_span_pow_p {s : ideal ℤ_[p]} (hs : s ≠ ⊥) : ∃ n : ℕ, s = ideal.span {p ^ n} := discrete_valuation_ring.ideal_eq_span_pow_irreducible hs irreducible_p end dvr end padic_int
43242b74d5fd2f4e3ec2c8ff78ac1b82897b1086
c777c32c8e484e195053731103c5e52af26a25d1
/src/number_theory/number_field/basic.lean
537628af9ebf9f043e5fb14a7251e3de62bc7c59
[ "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
7,370
lean
/- Copyright (c) 2021 Ashvni Narayanan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ashvni Narayanan, Anne Baanen -/ import algebra.char_p.algebra import ring_theory.dedekind_domain.integral_closure /-! # Number fields This file defines a number field and the ring of integers corresponding to it. ## Main definitions - `number_field` defines a number field as a field which has characteristic zero and is finite dimensional over ℚ. - `ring_of_integers` defines the ring of integers (or number ring) corresponding to a number field as the integral closure of ℤ in the number field. ## Implementation notes The definitions that involve a field of fractions choose a canonical field of fractions, but are independent of that choice. ## References * [D. Marcus, *Number Fields*][marcus1977number] * [J.W.S. Cassels, A. Frölich, *Algebraic Number Theory*][cassels1967algebraic] * [P. Samuel, *Algebraic Theory of Numbers*][samuel1970algebraic] ## Tags number field, ring of integers -/ /-- A number field is a field which has characteristic zero and is finite dimensional over ℚ. -/ class number_field (K : Type*) [field K] : Prop := [to_char_zero : char_zero K] [to_finite_dimensional : finite_dimensional ℚ K] open function module open_locale classical big_operators non_zero_divisors /-- `ℤ` with its usual ring structure is not a field. -/ lemma int.not_is_field : ¬ is_field ℤ := λ h, int.not_even_one $ (h.mul_inv_cancel two_ne_zero).imp $ λ a, (by rw ← two_mul; exact eq.symm) namespace number_field variables (K L : Type*) [field K] [field L] [nf : number_field K] include nf -- See note [lower instance priority] attribute [priority 100, instance] number_field.to_char_zero number_field.to_finite_dimensional protected lemma is_algebraic : algebra.is_algebraic ℚ K := algebra.is_algebraic_of_finite _ _ omit nf /-- The ring of integers (or number ring) corresponding to a number field is the integral closure of ℤ in the number field. -/ def ring_of_integers := integral_closure ℤ K localized "notation (name := ring_of_integers) `𝓞` := number_field.ring_of_integers" in number_field lemma mem_ring_of_integers (x : K) : x ∈ 𝓞 K ↔ is_integral ℤ x := iff.rfl lemma is_integral_of_mem_ring_of_integers {K : Type*} [field K] {x : K} (hx : x ∈ 𝓞 K) : is_integral ℤ (⟨x, hx⟩ : 𝓞 K) := begin obtain ⟨P, hPm, hP⟩ := hx, refine ⟨P, hPm, _⟩, rw [← polynomial.aeval_def, ← subalgebra.coe_eq_zero, polynomial.aeval_subalgebra_coe, polynomial.aeval_def, subtype.coe_mk, hP] end /-- Given an algebra between two fields, create an algebra between their two rings of integers. For now, this is not an instance by default as it creates an equal-but-not-defeq diamond with `algebra.id` when `K = L`. This is caused by `x = ⟨x, x.prop⟩` not being defeq on subtypes. This will likely change in Lean 4. -/ def ring_of_integers_algebra [algebra K L] : algebra (𝓞 K) (𝓞 L) := ring_hom.to_algebra { to_fun := λ k, ⟨algebra_map K L k, is_integral.algebra_map k.2⟩, map_zero' := subtype.ext $ by simp only [subtype.coe_mk, subalgebra.coe_zero, map_zero], map_one' := subtype.ext $ by simp only [subtype.coe_mk, subalgebra.coe_one, map_one], map_add' := λ x y, subtype.ext $ by simp only [map_add, subalgebra.coe_add, subtype.coe_mk], map_mul' := λ x y, subtype.ext $ by simp only [subalgebra.coe_mul, map_mul, subtype.coe_mk] } namespace ring_of_integers variables {K} instance [number_field K] : is_fraction_ring (𝓞 K) K := integral_closure.is_fraction_ring_of_finite_extension ℚ _ instance : is_integral_closure (𝓞 K) ℤ K := integral_closure.is_integral_closure _ _ instance [number_field K] : is_integrally_closed (𝓞 K) := integral_closure.is_integrally_closed_of_finite_extension ℚ lemma is_integral_coe (x : 𝓞 K) : is_integral ℤ (x : K) := x.2 lemma map_mem {F L : Type*} [field L] [char_zero K] [char_zero L] [alg_hom_class F ℚ K L] (f : F) (x : 𝓞 K) : f x ∈ 𝓞 L := (mem_ring_of_integers _ _).2 $ map_is_integral_int f $ ring_of_integers.is_integral_coe x /-- The ring of integers of `K` are equivalent to any integral closure of `ℤ` in `K` -/ protected noncomputable def equiv (R : Type*) [comm_ring R] [algebra R K] [is_integral_closure R ℤ K] : 𝓞 K ≃+* R := (is_integral_closure.equiv ℤ R K _).symm.to_ring_equiv variable (K) include nf instance : char_zero (𝓞 K) := char_zero.of_module _ K instance : is_noetherian ℤ (𝓞 K) := is_integral_closure.is_noetherian _ ℚ K _ /-- The ring of integers of a number field is not a field. -/ lemma not_is_field : ¬ is_field (𝓞 K) := begin have h_inj : function.injective ⇑(algebra_map ℤ (𝓞 K)), { exact ring_hom.injective_int (algebra_map ℤ (𝓞 K)) }, intro hf, exact int.not_is_field (((is_integral_closure.is_integral_algebra ℤ K).is_field_iff_is_field h_inj).mpr hf) end instance : is_dedekind_domain (𝓞 K) := is_integral_closure.is_dedekind_domain ℤ ℚ K _ instance : free ℤ (𝓞 K) := is_integral_closure.module_free ℤ ℚ K (𝓞 K) instance : is_localization (algebra.algebra_map_submonoid (𝓞 K) ℤ⁰) K := is_integral_closure.is_localization ℤ ℚ K (𝓞 K) /-- A ℤ-basis of the ring of integers of `K`. -/ noncomputable def basis : basis (free.choose_basis_index ℤ (𝓞 K)) ℤ (𝓞 K) := free.choose_basis ℤ (𝓞 K) end ring_of_integers include nf /-- A basis of `K` over `ℚ` that is also a basis of `𝓞 K` over `ℤ`. -/ noncomputable def integral_basis : basis (free.choose_basis_index ℤ (𝓞 K)) ℚ K := basis.localization_localization ℚ (non_zero_divisors ℤ) K (ring_of_integers.basis K) @[simp] lemma integral_basis_apply (i : free.choose_basis_index ℤ (𝓞 K)) : integral_basis K i = algebra_map (𝓞 K) K (ring_of_integers.basis K i) := basis.localization_localization_apply ℚ (non_zero_divisors ℤ) K (ring_of_integers.basis K) i lemma ring_of_integers.rank : finite_dimensional.finrank ℤ (𝓞 K) = finite_dimensional.finrank ℚ K := is_integral_closure.rank ℤ ℚ K (𝓞 K) end number_field namespace rat open number_field instance number_field : number_field ℚ := { to_char_zero := infer_instance, to_finite_dimensional := -- The vector space structure of `ℚ` over itself can arise in multiple ways: -- all fields are vector spaces over themselves (used in `rat.finite_dimensional`) -- all char 0 fields have a canonical embedding of `ℚ` (used in `number_field`). -- Show that these coincide: by convert (infer_instance : finite_dimensional ℚ ℚ), } /-- The ring of integers of `ℚ` as a number field is just `ℤ`. -/ noncomputable def ring_of_integers_equiv : ring_of_integers ℚ ≃+* ℤ := ring_of_integers.equiv ℤ end rat namespace adjoin_root section open_locale polynomial local attribute [-instance] algebra_rat /-- The quotient of `ℚ[X]` by the ideal generated by an irreducible polynomial of `ℚ[X]` is a number field. -/ instance {f : ℚ[X]} [hf : fact (irreducible f)] : number_field (adjoin_root f) := { to_char_zero := char_zero_of_injective_algebra_map (algebra_map ℚ _).injective, to_finite_dimensional := by convert (adjoin_root.power_basis hf.out.ne_zero).finite_dimensional } end end adjoin_root
fd7dd43917f7763d7b62003a9d7defab29eaf444
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/colimit_limit.lean
ef17325dcf8b30e688e08a79477a8fdb53dfc16d
[]
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
3,753
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.types import Mathlib.category_theory.currying import Mathlib.PostPort universes v u namespace Mathlib /-! # The morphism comparing a colimit of limits with the corresponding limit of colimits. For `F : J × K ⥤ C` there is always a morphism $\colim_k \lim_j F(j,k) → \lim_j \colim_k F(j, k)$. While it is not usually an isomorphism, with additional hypotheses on `J` and `K` it may be, in which case we say that "colimits commute with limits". The prototypical example, proved in `category_theory.limits.filtered_colimit_commutes_finite_limit`, is that when `C = Type`, filtered colimits commute with finite limits. ## References * Borceux, Handbook of categorical algebra 1, Section 2.13 * [Stacks: Filtered colimits](https://stacks.math.columbia.edu/tag/002W) -/ namespace category_theory.limits theorem map_id_left_eq_curry_map {J : Type v} {K : Type v} [small_category J] [small_category K] {C : Type u} [category C] (F : J × K ⥤ C) {j : J} {k : K} {k' : K} {f : k ⟶ k'} : functor.map F (𝟙, f) = functor.map (functor.obj (functor.obj curry F) j) f := rfl theorem map_id_right_eq_curry_swap_map {J : Type v} {K : Type v} [small_category J] [small_category K] {C : Type u} [category C] (F : J × K ⥤ C) {j : J} {j' : J} {f : j ⟶ j'} {k : K} : functor.map F (f, 𝟙) = functor.map (functor.obj (functor.obj curry (prod.swap K J ⋙ F)) k) f := rfl /-- The universal morphism $\colim_k \lim_j F(j,k) → \lim_j \colim_k F(j, k)$. -/ def colimit_limit_to_limit_colimit {J : Type v} {K : Type v} [small_category J] [small_category K] {C : Type u} [category C] (F : J × K ⥤ C) [has_limits_of_shape J C] [has_colimits_of_shape K C] : colimit (functor.obj curry (prod.swap K J ⋙ F) ⋙ lim) ⟶ limit (functor.obj curry F ⋙ colim) := limit.lift (functor.obj curry F ⋙ colim) (cone.mk (cocone.X (colimit.cocone (functor.obj curry (prod.swap K J ⋙ F) ⋙ lim))) (nat_trans.mk fun (j : J) => colimit.desc (functor.obj curry (prod.swap K J ⋙ F) ⋙ lim) (cocone.mk (cocone.X (colimit.cocone (functor.obj (functor.obj curry F) j))) (nat_trans.mk fun (k : K) => limit.π (functor.obj (functor.obj curry (prod.swap K J ⋙ F)) k) j ≫ colimit.ι (functor.obj (functor.obj curry F) j) k)))) /-- Since `colimit_limit_to_limit_colimit` is a morphism from a colimit to a limit, this lemma characterises it. -/ @[simp] theorem ι_colimit_limit_to_limit_colimit_π {J : Type v} {K : Type v} [small_category J] [small_category K] {C : Type u} [category C] (F : J × K ⥤ C) [has_limits_of_shape J C] [has_colimits_of_shape K C] (j : J) (k : K) : colimit.ι (functor.obj curry (prod.swap K J ⋙ F) ⋙ lim) k ≫ colimit_limit_to_limit_colimit F ≫ limit.π (functor.obj curry F ⋙ colim) j = limit.π (functor.obj (functor.obj curry (prod.swap K J ⋙ F)) k) j ≫ colimit.ι (functor.obj (functor.obj curry F) j) k := sorry @[simp] theorem ι_colimit_limit_to_limit_colimit_π_apply {J : Type v} {K : Type v} [small_category J] [small_category K] (F : J × K ⥤ Type v) (j : J) (k : K) (f : functor.obj (functor.obj curry (prod.swap K J ⋙ F) ⋙ lim) k) : limit.π (functor.obj curry F ⋙ colim) j (colimit_limit_to_limit_colimit F (colimit.ι (functor.obj curry (prod.swap K J ⋙ F) ⋙ lim) k f)) = colimit.ι (functor.obj (functor.obj curry F) j) k (limit.π (functor.obj (functor.obj curry (prod.swap K J ⋙ F)) k) j f) := sorry
a7640a6f12389d830e44536ddec82db22878ad52
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/analysis/convex/basic.lean
10c6da16a19b4557c147f04496fd07ed00bb9c69
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
58,182
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import data.set.intervals.ord_connected import data.set.intervals.image_preimage import data.complex.module import linear_algebra.affine_space.affine_map import algebra.module.ordered /-! # Convex sets and functions on real vector spaces In a real vector space, we define the following objects and properties. * `segment x y` is the closed segment joining `x` and `y`. * A set `s` is `convex` if for any two points `x y ∈ s` it includes `segment x y`; * A function `f : E → β` is `convex_on` a set `s` if `s` is itself a convex set, and for any two points `x y ∈ s` the segment joining `(x, f x)` to `(y, f y)` is (non-strictly) above the graph of `f`; equivalently, `convex_on f s` means that the epigraph `{p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2}` is a convex set; * Center mass of a finite set of points with prescribed weights. * Convex hull of a set `s` is the minimal convex set that includes `s`. * Standard simplex `std_simplex ι [fintype ι]` is the intersection of the positive quadrant with the hyperplane `s.sum = 1` in the space `ι → ℝ`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex, and prove Jensen's inequality. Note: To define convexity for functions `f : E → β`, we need `β` to be an ordered vector space, defined using the instance `ordered_semimodule ℝ β`. ## Notations We use the following local notations: * `I = Icc (0:ℝ) 1`; * `[x, y] = segment x y`. They are defined using `local notation`, so they are not available outside of this file. -/ universes u' u v v' w x variables {E : Type u} {F : Type v} {ι : Type w} {ι' : Type x} {α : Type v'} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] [linear_ordered_field α] {s : set E} open set linear_map open_locale classical big_operators local notation `I` := (Icc 0 1 : set ℝ) section sets /-! ### Segment -/ /-- Segments in a vector space. -/ def segment (x y : E) : set E := {z : E | ∃ (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), a • x + b • y = z} local notation `[`x `, ` y `]` := segment x y lemma segment_symm (x y : E) : [x, y] = [y, x] := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma left_mem_segment (x y : E) : x ∈ [x, y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x, y] := segment_symm y x ▸ left_mem_segment y x lemma segment_same (x : E) : [x, x] = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ h, mem_singleton_iff.1 h ▸ left_mem_segment z z⟩ lemma segment_eq_image (x y : E) : segment x y = (λ (θ : ℝ), (1 - θ) • x + θ • y) '' I := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ le_add_of_nonneg_left ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1-θ, θ, sub_nonneg.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_image' (x y : E) : segment x y = (λ (θ : ℝ), x + θ • (y - x)) '' I := by { convert segment_eq_image x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_eq_image₂ (x y : E) : segment x y = (λ p:ℝ×ℝ, p.1 • x + p.2 • y) '' {p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_eq_Icc {a b : ℝ} (h : a ≤ b) : [a, b] = Icc a b := begin rw [segment_eq_image'], show (((+) a) ∘ (λ t, t * (b - a))) '' Icc 0 1 = Icc a b, rw [image_comp, image_mul_right_Icc (@zero_le_one ℝ _) (sub_nonneg.2 h), image_const_add_Icc], simp end lemma segment_eq_Icc' (a b : ℝ) : [a, b] = Icc (min a b) (max a b) := by cases le_total a b; [skip, rw segment_symm]; simp [segment_eq_Icc, *] lemma segment_eq_interval (a b : ℝ) : segment a b = interval a b := segment_eq_Icc' _ _ lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b, a + c] ↔ x ∈ [b, c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj] end lemma segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' [a + b, a + c] = [b, c] := set.ext $ λ x, mem_segment_translate a lemma segment_translate_image (a b c: E) : (λx, a + x) '' [b, c] = [a + b, a + c] := segment_translate_preimage a b c ▸ image_preimage_eq _ $ add_left_surjective a /-! ### Convexity of sets -/ /-- Convexity of sets. -/ def convex (s : set E) := ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s lemma convex_iff_forall_pos : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h x y hx hy a b ha hb hab, h hx hy (le_of_lt ha) (le_of_lt hb) hab, _⟩, intros h x y hx hy a b ha hb hab, cases eq_or_lt_of_le ha with ha ha, { subst a, rw [zero_add] at hab, simp [hab, hy] }, cases eq_or_lt_of_le hb with hb hb, { subst b, rw [add_zero] at hab, simp [hab, hx] }, exact h hx hy ha hb hab end lemma convex_iff_segment_subset : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → [x, y] ⊆ s := by simp only [convex, segment_eq_image₂, subset_def, ball_image_iff, prod.forall, mem_set_of_eq, and_imp] lemma convex.segment_subset (h : convex s) {x y:E} (hx : x ∈ s) (hy : y ∈ s) : [x, y] ⊆ s := convex_iff_segment_subset.1 h hx hy /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff_pointwise_add_subset: convex s ↔ ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s := iff.intro begin rintros hA a b ha hb hab w ⟨au, bv, ⟨u, hu, rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (λ h x y hx hy a b ha hb hab, (h ha hb hab) (set.add_mem_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) /-- Alternative definition of set convexity, using division. -/ lemma convex_iff_div: convex s ↔ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ s := ⟨begin assume h x y hx hy a b ha hb hab, apply h hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin assume h x y hx hy a b ha hb hab, have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ /-! ### Examples of convex sets -/ lemma convex_empty : convex (∅ : set E) := by finish lemma convex_singleton (c : E) : convex ({c} : set E) := begin intros x y hx hy a b ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab, one_smul], exact mem_singleton c end lemma convex_univ : convex (set.univ : set E) := λ _ _ _ _ _ _ _ _ _, trivial lemma convex.inter {t : set E} (hs: convex s) (ht: convex t) : convex (s ∩ t) := λ x y (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) a b (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex s) : convex (⋂₀ S) := assume x y hx hy a b ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {ι : Sort*} {s: ι → set E} (h: ∀ i : ι, convex (s i)) : convex (⋂ i, s i) := (sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h lemma convex.prod {s : set E} {t : set F} (hs : convex s) (ht : convex t) : convex (s.prod t) := begin intros x y hx hy a b ha hb hab, apply mem_prod.2, exact ⟨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma convex.combo_to_vadd {a b : ℝ} {x y : E} (h : a + b = 1) : a • x + b • y = b • (y - x) + x := calc a • x + b • y = (b • y - b • x) + (a • x + b • x) : by abel ... = b • (y - x) + (a + b) • x : by rw [smul_sub, add_smul] ... = b • (y - x) + (1 : ℝ) • x : by rw [h] ... = b • (y - x) + x : by rw [one_smul] /-- Applying an affine map to an affine combination of two points yields an affine combination of the images. -/ lemma convex.combo_affine_apply {a b : ℝ} {x y : E} {f : E →ᵃ[ℝ] F} (h : a + b = 1) : f (a • x + b • y) = a • f x + b • f y := begin simp only [convex.combo_to_vadd h, ← vsub_eq_sub], exact f.apply_line_map _ _ _, end /-- The preimage of a convex set under an affine map is convex. -/ lemma convex.affine_preimage (f : E →ᵃ[ℝ] F) {s : set F} (hs : convex s) : convex (f ⁻¹' s) := begin intros x y xs ys a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs xs ys ha hb hab, end /-- The image of a convex set under an affine map is convex. -/ lemma convex.affine_image (f : E →ᵃ[ℝ] F) {s : set E} (hs : convex s) : convex (f '' s) := begin rintros x y ⟨x', ⟨hx', hx'f⟩⟩ ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a • x' + b • y', ⟨hs hx' hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hx'f, hy'f] end lemma convex.linear_image (hs : convex s) (f : E →ₗ[ℝ] F) : convex (image f s) := hs.affine_image f.to_affine_map lemma convex.is_linear_image (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (f '' s) := hs.linear_image $ hf.mk' f lemma convex.linear_preimage {s : set F} (hs : convex s) (f : E →ₗ[ℝ] F) : convex (preimage f s) := hs.affine_preimage f.to_affine_map lemma convex.is_linear_preimage {s : set F} (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (preimage f s) := hs.linear_preimage $ hf.mk' f lemma convex.neg (hs : convex s) : convex ((λ z, -z) '' s) := hs.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hs : convex s) : convex ((λ z, -z) ⁻¹' s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg lemma convex.smul (c : ℝ) (hs : convex s) : convex (c • s) := hs.linear_image (linear_map.lsmul _ _ c) lemma convex.smul_preimage (c : ℝ) (hs : convex s) : convex ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma convex.add {t : set E} (hs : convex s) (ht : convex t) : convex (s + t) := by { rw ← add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma convex.sub {t : set E} (hs : convex s) (ht : convex t) : convex ((λx : E × E, x.1 - x.2) '' (s.prod t)) := (hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub lemma convex.translate (hs : convex s) (z : E) : convex ((λx, z + x) '' s) := hs.affine_image $ affine_map.const ℝ E z +ᵥ affine_map.id ℝ E /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_right (hs : convex s) (a : E) : convex ((λ z, a + z) ⁻¹' s) := hs.affine_preimage $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_left (hs : convex s) (a : E) : convex ((λ z, z + a) ⁻¹' s) := by simpa only [add_comm] using hs.translate_preimage_right a lemma convex.affinity (hs : convex s) (z : E) (c : ℝ) : convex ((λx, z + c • x) '' s) := hs.affine_image $ affine_map.const ℝ E z +ᵥ c • affine_map.id ℝ E lemma real.convex_iff_ord_connected {s : set ℝ} : convex s ↔ ord_connected s := begin simp only [convex_iff_segment_subset, segment_eq_interval, ord_connected_iff_interval_subset], exact forall_congr (λ x, forall_swap) end alias real.convex_iff_ord_connected ↔ convex.ord_connected set.ord_connected.convex lemma convex_Iio (r : ℝ) : convex (Iio r) := ord_connected_Iio.convex lemma convex_Ioi (r : ℝ) : convex (Ioi r) := ord_connected_Ioi.convex lemma convex_Iic (r : ℝ) : convex (Iic r) := ord_connected_Iic.convex lemma convex_Ici (r : ℝ) : convex (Ici r) := ord_connected_Ici.convex lemma convex_Ioo (r s : ℝ) : convex (Ioo r s) := ord_connected_Ioo.convex lemma convex_Ico (r s : ℝ) : convex (Ico r s) := ord_connected_Ico.convex lemma convex_Ioc (r : ℝ) (s : ℝ) : convex (Ioc r s) := ord_connected_Ioc.convex lemma convex_Icc (r : ℝ) (s : ℝ) : convex (Icc r s) := ord_connected_Icc.convex lemma convex_interval (r : ℝ) (s : ℝ) : convex (interval r s) := ord_connected_interval.convex lemma convex_segment (a b : E) : convex [a, b] := begin have : (λ (t : ℝ), a + t • (b - a)) = (λz : E, a + z) ∘ (λt:ℝ, t • (b - a)) := rfl, rw [segment_eq_image', this, image_comp], refine ((convex_Icc _ _).is_linear_image _).translate _, exact is_linear_map.is_linear_map_smul' _ end lemma convex_halfspace_lt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_le {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w ≤ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_gt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r < f w} := (convex_Ioi r).is_linear_preimage h lemma convex_halfspace_ge {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r ≤ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w = r} := begin show convex (f ⁻¹' {p | p = r}), rw set_of_eq_eq_singleton, exact (convex_singleton r).is_linear_preimage h end lemma convex_halfspace_re_lt (r : ℝ) : convex {c : ℂ | c.re < r} := convex_halfspace_lt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_le (r : ℝ) : convex {c : ℂ | c.re ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_gt (r : ℝ) : convex {c : ℂ | r < c.re } := convex_halfspace_gt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_lge (r : ℝ) : convex {c : ℂ | r ≤ c.re} := convex_halfspace_ge (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_im_lt (r : ℝ) : convex {c : ℂ | c.im < r} := convex_halfspace_lt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_le (r : ℝ) : convex {c : ℂ | c.im ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_gt (r : ℝ) : convex {c : ℂ | r < c.im } := convex_halfspace_gt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_lge (r : ℝ) : convex {c : ℂ | r ≤ c.im} := convex_halfspace_ge (is_linear_map.mk complex.add_im complex.smul_im) _ /-! ### Convex combinations in intervals -/ lemma convex.combo_self (a : α) {x y : α} (h : x + y = 1) : a = x * a + y * a := calc a = 1 * a : by rw [one_mul] ... = (x + y) * a : by rw [h] ... = x * a + y * a : by rw [add_mul] /-- If `x` is in an `Ioo`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ioo {a b x : α} (h : a < b) : x ∈ Ioo a b ↔ ∃ (x_a x_b : α), 0 < x_a ∧ 0 < x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases hab : ¬a < b, { exfalso; exact hab h }, { refine ⟨(b-x) / (b-a), (x-a) / (b-a), _⟩, refine ⟨div_pos (by linarith) (by linarith), div_pos (by linarith) (by linarith),_,_⟩; { field_simp [show b - a ≠ 0, by linarith], ring } } }, { rw [mem_Ioo], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Ioc`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ioc {a b x : α} (h : a < b) : x ∈ Ioc a b ↔ ∃ (x_a x_b : α), 0 ≤ x_a ∧ 0 < x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases h_x : x = b, { exact ⟨0, 1, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioo h).mp ⟨h_ax, lt_of_le_of_ne h_bx h_x⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, by linarith, Ioo_case.2⟩ } }, { rw [mem_Ioc], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Ico`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ico {a b x : α} (h : a < b) : x ∈ Ico a b ↔ ∃ (x_a x_b : α), 0 < x_a ∧ 0 ≤ x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases h_x : x = a, { exact ⟨1, 0, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioo h).mp ⟨lt_of_le_of_ne h_ax (ne.symm h_x), h_bx⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, Ioo_case.1, by linarith, (Ioo_case.2).2⟩ } }, { rw [mem_Ico], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Icc`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Icc {a b x : α} (h : a ≤ b) : x ∈ Icc a b ↔ ∃ (x_a x_b : α), 0 ≤ x_a ∧ 0 ≤ x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { intro x_in_I, rw [Icc, mem_set_of_eq] at x_in_I, rcases x_in_I with ⟨h_ax, h_bx⟩, by_cases hab' : a = b, { exact ⟨0, 1, le_refl 0, by linarith, by ring, by linarith⟩ }, change a ≠ b at hab', replace h : a < b, exact lt_of_le_of_ne h hab', by_cases h_x : x = a, { exact ⟨1, 0, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioc h).mp ⟨lt_of_le_of_ne h_ax (ne.symm h_x), h_bx⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, Ioo_case.1, by linarith, (Ioo_case.2).2⟩ } }, { rw [mem_Icc], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end section submodule open submodule lemma submodule.convex (K : submodule ℝ E) : convex (↑K : set E) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex (K : subspace ℝ E) : convex (↑K : set E) := K.convex end submodule end sets /-! ### Convex and concave functions -/ section functions variables {β : Type*} [ordered_add_comm_monoid β] [semimodule ℝ β] local notation `[`x `, ` y `]` := segment x y /-- Convexity of functions -/ def convex_on (s : set E) (f : E → β) : Prop := convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y /-- Concavity of functions -/ def concave_on (s : set E) (f : E → β) : Prop := convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y) section variables [ordered_semimodule ℝ β] /-- A function `f` is concave iff `-f` is convex. -/ @[simp] lemma neg_convex_on_iff {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] (s : set E) (f : E → γ) : convex_on s (-f) ↔ concave_on s f := begin split, { rintros ⟨hconv, h⟩, refine ⟨hconv, _⟩, intros x y xs ys a b ha hb hab, specialize h xs ys ha hb hab, simp [neg_apply, neg_le, add_comm] at h, exact h }, { rintros ⟨hconv, h⟩, refine ⟨hconv, _⟩, intros x y xs ys a b ha hb hab, specialize h xs ys ha hb hab, simp [neg_apply, neg_le, add_comm, h] } end /-- A function `f` is concave iff `-f` is convex. -/ @[simp] lemma neg_concave_on_iff {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] (s : set E) (f : E → γ) : concave_on s (-f) ↔ convex_on s f:= by rw [← neg_convex_on_iff s (-f), neg_neg f] end lemma convex_on_id {s : set ℝ} (hs : convex s) : convex_on s id := ⟨hs, by { intros, refl }⟩ lemma concave_on_id {s : set ℝ} (hs : convex s) : concave_on s id := ⟨hs, by { intros, refl }⟩ lemma convex_on_const (c : β) (hs : convex s) : convex_on s (λ x:E, c) := ⟨hs, by { intros, simp only [← add_smul, *, one_smul] }⟩ lemma concave_on_const (c : β) (hs : convex s) : concave_on s (λ x:E, c) := @convex_on_const _ _ _ _ (order_dual β) _ _ c hs variables {t : set E} lemma convex_on_iff_div {f : E → β} : convex_on s f ↔ convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a/(a+b)) • x + (b/(a+b)) • y) ≤ (a/(a+b)) • f x + (b/(a+b)) • f y := and_congr iff.rfl ⟨begin intros h x y hx hy a b ha hb hab, apply h hx hy (div_nonneg ha $ le_of_lt hab) (div_nonneg hb $ le_of_lt hab), rw [←add_div], exact div_self (ne_of_gt hab) end, begin intros h x y hx hy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy ha hb, end⟩ lemma concave_on_iff_div {f : E → β} : concave_on s f ↔ convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • f x + (b/(a+b)) • f y ≤ f ((a/(a+b)) • x + (b/(a+b)) • y) := @convex_on_iff_div _ _ _ _ (order_dual β) _ _ _ /-- For a function on a convex set in a linear ordered space, in order to prove that it is convex it suffices to verify the inequality `f (a • x + b • y) ≤ a • f x + b • f y` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.convex_on_of_lt {f : E → β} [linear_order E] (hs : convex s) (hf : ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → x < y → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y) : convex_on s f := begin use hs, intros x y hx hy a b ha hb hab, wlog hxy : x<=y using [x y a b, y x b a], { exact le_total _ _ }, { cases eq_or_lt_of_le hxy with hxy hxy, by { subst y, rw [← add_smul, ← add_smul, hab, one_smul, one_smul] }, cases eq_or_lt_of_le ha with ha ha, by { subst a, rw [zero_add] at hab, subst b, simp }, cases eq_or_lt_of_le hb with hb hb, by { subst b, rw [add_zero] at hab, subst a, simp }, exact hf hx hy hxy ha hb hab } end /-- For a function on a convex set in a linear ordered space, in order to prove that it is concave it suffices to verify the inequality `a • f x + b • f y ≤ f (a • x + b • y)` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.concave_on_of_lt {f : E → β} [linear_order E] (hs : convex s) (hf : ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → x < y → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y)) : concave_on s f := @linear_order.convex_on_of_lt _ _ _ _ (order_dual β) _ _ f _ hs hf /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is convex on `D`. This way of proving convexity of a function is used in the proof of convexity of a function with a monotone derivative. -/ lemma convex_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) : convex_on s f := linear_order.convex_on_of_lt hs begin assume x z hx hz hxz a b ha hb hab, let y := a * x + b * z, have hxy : x < y, { rw [← one_mul x, ← hab, add_mul], exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ }, have hyz : y < z, { rw [← one_mul z, ← hab, add_mul], exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ }, have : (f y - f x) * (z - y) ≤ (f z - f y) * (y - x), from (div_le_div_iff (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz), have A : z - y + (y - x) = z - x, by abel, have B : 0 < z - x, from sub_pos.2 (lt_trans hxy hyz), rw [sub_mul, sub_mul, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le, ← mul_add, A, ← le_div_iff B, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x), mul_comm (f z)] at this, rw [eq_comm, ← sub_eq_iff_eq_add] at hab; subst a, convert this; symmetry; simp only [div_eq_iff (ne_of_gt B), y]; ring end /-- For a function `f` defined on a subset `D` of `ℝ`, if `f` is convex on `D`, then for any three points `x<y<z`, the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma convex_on.slope_mono_adjacent {s : set ℝ} {f : ℝ → ℝ} (hf : convex_on s f) {x y z : ℝ} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f y - f x) / (y - x) ≤ (f z - f y) / (z - y) := begin have h₁ : 0 < y - x := by linarith, have h₂ : 0 < z - y := by linarith, have h₃ : 0 < z - x := by linarith, suffices : f y / (y - x) + f y / (z - y) ≤ f x / (y - x) + f z / (z - y), by { ring at this ⊢, linarith }, set a := (z - y) / (z - x), set b := (y - x) / (z - x), have heqz : a • x + b • z = y, by { field_simp, rw div_eq_iff; [ring, linarith], }, have key, from hf.2 hx hz (show 0 ≤ a, by apply div_nonneg; linarith) (show 0 ≤ b, by apply div_nonneg; linarith) (show a + b = 1, by { field_simp, rw div_eq_iff; [ring, linarith], }), rw heqz at key, replace key := mul_le_mul_of_nonneg_left key (le_of_lt h₃), field_simp [ne_of_gt h₁, ne_of_gt h₂, ne_of_gt h₃, mul_comm (z - x) _] at key ⊢, rw div_le_div_right, { linarith, }, { nlinarith, }, end /-- For a function `f` defined on a convex subset `D` of `ℝ`, `f` is convex on `D` iff for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma convex_on_real_iff_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} : convex_on s f ↔ (∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) := ⟨convex_on.slope_mono_adjacent, convex_on_real_of_slope_mono_adjacent hs⟩ /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is concave on `D`. -/ lemma concave_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) : concave_on s f := begin rw [←neg_convex_on_iff], apply convex_on_real_of_slope_mono_adjacent hs, intros x y z xs zs xy yz, rw [←neg_le_neg_iff, ←neg_div, ←neg_div, neg_sub, neg_sub], simp only [hf xs zs xy yz, neg_sub_neg, pi.neg_apply], end /-- For a function `f` defined on a subset `D` of `ℝ`, if `f` is concave on `D`, then for any three points `x<y<z`, the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma concave_on.slope_mono_adjacent {s : set ℝ} {f : ℝ → ℝ} (hf : concave_on s f) {x y z : ℝ} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f z - f y) / (z - y) ≤ (f y - f x) / (y - x) := begin rw [←neg_le_neg_iff, ←neg_div, ←neg_div, neg_sub, neg_sub], rw [←neg_sub_neg (f y), ←neg_sub_neg (f z)], simp_rw [←pi.neg_apply], rw [←neg_convex_on_iff] at hf, apply convex_on.slope_mono_adjacent hf; assumption, end /-- For a function `f` defined on a convex subset `D` of `ℝ`, `f` is concave on `D` iff for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma concave_on_real_iff_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} : concave_on s f ↔ (∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) := ⟨concave_on.slope_mono_adjacent, concave_on_real_of_slope_mono_adjacent hs⟩ lemma convex_on.subset {f : E → β} (h_convex_on : convex_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : convex_on s f := begin apply and.intro h_convex, intros x y hx hy, exact h_convex_on.2 (h_subset hx) (h_subset hy), end lemma concave_on.subset {f : E → β} (h_concave_on : concave_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : concave_on s f := @convex_on.subset _ _ _ _ (order_dual β) _ _ t f h_concave_on h_subset h_convex lemma convex_on.add {f g : E → β} (hf : convex_on s f) (hg : convex_on s g) : convex_on s (λx, f x + g x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) ≤ (a • f x + b • f y) + (a • g x + b • g y) : add_le_add (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) ... = a • f x + a • g x + b • f y + b • g y : by abel ... = a • (f x + g x) + b • (f y + g y) : by simp [smul_add, add_assoc] end lemma concave_on.add {f g : E → β} (hf : concave_on s f) (hg : concave_on s g) : concave_on s (λx, f x + g x) := @convex_on.add _ _ _ _ (order_dual β) _ _ f g hf hg lemma convex_on.smul [ordered_semimodule ℝ β] {f : E → β} {c : ℝ} (hc : 0 ≤ c) (hf : convex_on s f) : convex_on s (λx, c • f x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc c • f (a • x + b • y) ≤ c • (a • f x + b • f y) : smul_le_smul_of_nonneg (hf.2 hx hy ha hb hab) hc ... = a • (c • f x) + b • (c • f y) : by simp only [smul_add, smul_comm c] end lemma concave_on.smul [ordered_semimodule ℝ β] {f : E → β} {c : ℝ} (hc : 0 ≤ c) (hf : concave_on s f) : concave_on s (λx, c • f x) := @convex_on.smul _ _ _ _ (order_dual β) _ _ _ f c hc hf /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment' {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} {x y : E} {a b : ℝ} (hf : convex_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha hb hab ... ≤ a • max (f x) (f y) + b • max (f x) (f y) : add_le_add (smul_le_smul_of_nonneg (le_max_left _ _) ha) (smul_le_smul_of_nonneg (le_max_right _ _) hb) ... ≤ max (f x) (f y) : by rw [←add_smul, hab, one_smul] /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.le_on_segment' {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} {x y : E} {a b : ℝ} (hf : concave_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min (f x) (f y) ≤ f (a • x + b • y) := @convex_on.le_on_segment' _ _ _ _ (order_dual γ) _ _ _ f x y a b hf hx hy ha hb hab /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x, y]) : f z ≤ max (f x) (f y) := let ⟨a, b, ha, hb, hab, hz⟩ := hz in hz ▸ hf.le_on_segment' hx hy ha hb hab /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.le_on_segment {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x, y]) : min (f x) (f y) ≤ f z := @convex_on.le_on_segment _ _ _ _ (order_dual γ) _ _ _ f hf x y z hx hy hz lemma convex_on.convex_le [ordered_semimodule ℝ β] {f : E → β} (hf : convex_on s f) (r : β) : convex {x ∈ s | f x ≤ r} := convex_iff_segment_subset.2 $ λ x y hx hy z hz, begin refine ⟨hf.1.segment_subset hx.1 hy.1 hz,_⟩, rcases hz with ⟨za,zb,hza,hzb,hzazb,H⟩, rw ←H, calc f (za • x + zb • y) ≤ za • (f x) + zb • (f y) : hf.2 hx.1 hy.1 hza hzb hzazb ... ≤ za • r + zb • r : add_le_add (smul_le_smul_of_nonneg hx.2 hza) (smul_le_smul_of_nonneg hy.2 hzb) ... ≤ r : by simp [←add_smul, hzazb] end lemma concave_on.concave_le [ordered_semimodule ℝ β] {f : E → β} (hf : concave_on s f) (r : β) : convex {x ∈ s | r ≤ f x} := @convex_on.convex_le _ _ _ _ (order_dual β) _ _ _ f hf r lemma convex_on.convex_lt {γ : Type*} [ordered_cancel_add_comm_monoid γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) (r : γ) : convex {x ∈ s | f x < r} := begin intros a b as bs xa xb hxa hxb hxaxb, refine ⟨hf.1 as.1 bs.1 hxa hxb hxaxb, _⟩, dsimp, by_cases H : xa = 0, { have H' : xb = 1 := by rwa [H, zero_add] at hxaxb, rw [H, H', zero_smul, one_smul, zero_add], exact bs.2 }, { calc f (xa • a + xb • b) ≤ xa • (f a) + xb • (f b) : hf.2 as.1 bs.1 hxa hxb hxaxb ... < xa • r + xb • (f b) : (add_lt_add_iff_right (xb • (f b))).mpr (smul_lt_smul_of_pos as.2 (lt_of_le_of_ne hxa (ne.symm H))) ... ≤ xa • r + xb • r : (add_le_add_iff_left (xa • r)).mpr (smul_le_smul_of_nonneg bs.2.le hxb) ... = r : by simp only [←add_smul, hxaxb, one_smul] } end lemma concave_on.convex_lt {γ : Type*} [ordered_cancel_add_comm_monoid γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) (r : γ) : convex {x ∈ s | r < f x} := @convex_on.convex_lt _ _ _ _ (order_dual γ) _ _ _ f hf r lemma convex_on.convex_epigraph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) : convex {p : E × γ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin rintros ⟨x, r⟩ ⟨y, t⟩ ⟨hx, hr⟩ ⟨hy, ht⟩ a b ha hb hab, refine ⟨hf.1 hx hy ha hb hab, _⟩, calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha hb hab ... ≤ a • r + b • t : add_le_add (smul_le_smul_of_nonneg hr ha) (smul_le_smul_of_nonneg ht hb) end lemma concave_on.convex_hypograph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) : convex {p : E × γ | p.1 ∈ s ∧ p.2 ≤ f p.1} := @convex_on.convex_epigraph _ _ _ _ (order_dual γ) _ _ _ f hf lemma convex_on_iff_convex_epigraph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} : convex_on s f ↔ convex {p : E × γ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin refine ⟨convex_on.convex_epigraph, λ h, ⟨_, _⟩⟩, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).1 }, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).2 } end lemma concave_on_iff_convex_hypograph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} : concave_on s f ↔ convex {p : E × γ | p.1 ∈ s ∧ p.2 ≤ f p.1} := @convex_on_iff_convex_epigraph _ _ _ _ (order_dual γ) _ _ _ f /-- If a function is convex on `s`, it remains convex when precomposed by an affine map. -/ lemma convex_on.comp_affine_map {f : F → β} (g : E →ᵃ[ℝ] F) {s : set F} (hf : convex_on s f) : convex_on (g ⁻¹' s) (f ∘ g) := begin refine ⟨hf.1.affine_preimage _,_⟩, intros x y xs ys a b ha hb hab, calc (f ∘ g) (a • x + b • y) = f (g (a • x + b • y)) : rfl ... = f (a • (g x) + b • (g y)) : by rw [convex.combo_affine_apply hab] ... ≤ a • f (g x) + b • f (g y) : hf.2 xs ys ha hb hab ... = a • (f ∘ g) x + b • (f ∘ g) y : rfl end /-- If a function is concave on `s`, it remains concave when precomposed by an affine map. -/ lemma concave_on.comp_affine_map {f : F → β} (g : E →ᵃ[ℝ] F) {s : set F} (hf : concave_on s f) : concave_on (g ⁻¹' s) (f ∘ g) := @convex_on.comp_affine_map _ _ _ _ _ _ (order_dual β) _ _ f g s hf /-- If `g` is convex on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ lemma convex_on.comp_linear_map {g : F → β} {s : set F} (hg : convex_on s g) (f : E →ₗ[ℝ] F) : convex_on (f ⁻¹' s) (g ∘ f) := hg.comp_affine_map f.to_affine_map /-- If `g` is concave on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ lemma concave_on.comp_linear_map {g : F → β} {s : set F} (hg : concave_on s g) (f : E →ₗ[ℝ] F) : concave_on (f ⁻¹' s) (g ∘ f) := hg.comp_affine_map f.to_affine_map /-- If a function is convex on `s`, it remains convex after a translation. -/ lemma convex_on.translate_right {f : E → β} {s : set E} {a : E} (hf : convex_on s f) : convex_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, a + z)) := hf.comp_affine_map $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- If a function is concave on `s`, it remains concave after a translation. -/ lemma concave_on.translate_right {f : E → β} {s : set E} {a : E} (hf : concave_on s f) : concave_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, a + z)) := hf.comp_affine_map $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- If a function is convex on `s`, it remains convex after a translation. -/ lemma convex_on.translate_left {f : E → β} {s : set E} {a : E} (hf : convex_on s f) : convex_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, z + a)) := by simpa only [add_comm] using hf.translate_right /-- If a function is concave on `s`, it remains concave after a translation. -/ lemma concave_on.translate_left {f : E → β} {s : set E} {a : E} (hf : concave_on s f) : concave_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, z + a)) := by simpa only [add_comm] using hf.translate_right end functions /-! ### Center of mass -/ section center_mass /-- Center of mass of a finite collection of points with prescribed weights. Note that we require neither `0 ≤ w i` nor `∑ w = 1`. -/ noncomputable def finset.center_mass (t : finset ι) (w : ι → ℝ) (z : ι → E) : E := (∑ i in t, w i)⁻¹ • (∑ i in t, w i • z i) variables (i j : ι) (c : ℝ) (t : finset ι) (w : ι → ℝ) (z : ι → E) open finset lemma finset.center_mass_empty : (∅ : finset ι).center_mass w z = 0 := by simp only [center_mass, sum_empty, smul_zero] lemma finset.center_mass_pair (hne : i ≠ j) : ({i, j} : finset ι).center_mass w z = (w i / (w i + w j)) • z i + (w j / (w i + w j)) • z j := by simp only [center_mass, sum_pair hne, smul_add, (mul_smul _ _ _).symm, div_eq_inv_mul] variable {w} lemma finset.center_mass_insert (ha : i ∉ t) (hw : ∑ j in t, w j ≠ 0) : (insert i t).center_mass w z = (w i / (w i + ∑ j in t, w j)) • z i + ((∑ j in t, w j) / (w i + ∑ j in t, w j)) • t.center_mass w z := begin simp only [center_mass, sum_insert ha, smul_add, (mul_smul _ _ _).symm, ← div_eq_inv_mul], congr' 2, rw [div_mul_eq_mul_div, mul_inv_cancel hw, one_div] end lemma finset.center_mass_singleton (hw : w i ≠ 0) : ({i} : finset ι).center_mass w z = z i := by rw [center_mass, sum_singleton, sum_singleton, ← mul_smul, inv_mul_cancel hw, one_smul] lemma finset.center_mass_eq_of_sum_1 (hw : ∑ i in t, w i = 1) : t.center_mass w z = ∑ i in t, w i • z i := by simp only [finset.center_mass, hw, inv_one, one_smul] lemma finset.center_mass_smul : t.center_mass w (λ i, c • z i) = c • t.center_mass w z := by simp only [finset.center_mass, finset.smul_sum, (mul_smul _ _ _).symm, mul_comm c, mul_assoc] /-- A convex combination of two centers of mass is a center of mass as well. This version deals with two different index types. -/ lemma finset.center_mass_segment' (s : finset ι) (t : finset ι') (ws : ι → ℝ) (zs : ι → E) (wt : ι' → ℝ) (zt : ι' → E) (hws : ∑ i in s, ws i = 1) (hwt : ∑ i in t, wt i = 1) (a b : ℝ) (hab : a + b = 1) : a • s.center_mass ws zs + b • t.center_mass wt zt = (s.map function.embedding.inl ∪ t.map function.embedding.inr).center_mass (sum.elim (λ i, a * ws i) (λ j, b * wt j)) (sum.elim zs zt) := begin rw [s.center_mass_eq_of_sum_1 _ hws, t.center_mass_eq_of_sum_1 _ hwt, smul_sum, smul_sum, ← finset.sum_sum_elim, finset.center_mass_eq_of_sum_1], { congr' with ⟨⟩; simp only [sum.elim_inl, sum.elim_inr, mul_smul] }, { rw [sum_sum_elim, ← mul_sum, ← mul_sum, hws, hwt, mul_one, mul_one, hab] } end /-- A convex combination of two centers of mass is a center of mass as well. This version works if two centers of mass share the set of original points. -/ lemma finset.center_mass_segment (s : finset ι) (w₁ w₂ : ι → ℝ) (z : ι → E) (hw₁ : ∑ i in s, w₁ i = 1) (hw₂ : ∑ i in s, w₂ i = 1) (a b : ℝ) (hab : a + b = 1) : a • s.center_mass w₁ z + b • s.center_mass w₂ z = s.center_mass (λ i, a * w₁ i + b * w₂ i) z := have hw : ∑ i in s, (a * w₁ i + b * w₂ i) = 1, by simp only [mul_sum.symm, sum_add_distrib, mul_one, *], by simp only [finset.center_mass_eq_of_sum_1, smul_sum, sum_add_distrib, add_smul, mul_smul, *] lemma finset.center_mass_ite_eq (hi : i ∈ t) : t.center_mass (λ j, if (i = j) then 1 else 0) z = z i := begin rw [finset.center_mass_eq_of_sum_1], transitivity ∑ j in t, if (i = j) then z i else 0, { congr' with i, split_ifs, exacts [h ▸ one_smul _ _, zero_smul _ _] }, { rw [sum_ite_eq, if_pos hi] }, { rw [sum_ite_eq, if_pos hi] } end variables {t w} lemma finset.center_mass_subset {t' : finset ι} (ht : t ⊆ t') (h : ∀ i ∈ t', i ∉ t → w i = 0) : t.center_mass w z = t'.center_mass w z := begin rw [center_mass, sum_subset ht h, smul_sum, center_mass, smul_sum], apply sum_subset ht, assume i hit' hit, rw [h i hit' hit, zero_smul, smul_zero] end lemma finset.center_mass_filter_ne_zero : (t.filter (λ i, w i ≠ 0)).center_mass w z = t.center_mass w z := finset.center_mass_subset z (filter_subset _ _) $ λ i hit hit', by simpa only [hit, mem_filter, true_and, ne.def, not_not] using hit' variable {z} /-- The center of mass of a finite subset of a convex set belongs to the set provided that all weights are non-negative, and the total weight is positive. -/ lemma convex.center_mass_mem (hs : convex s) : (∀ i ∈ t, 0 ≤ w i) → (0 < ∑ i in t, w i) → (∀ i ∈ t, z i ∈ s) → t.center_mass w z ∈ s := begin induction t using finset.induction with i t hi ht, { simp [lt_irrefl] }, intros h₀ hpos hmem, have zi : z i ∈ s, from hmem _ (mem_insert_self _ _), have hs₀ : ∀ j ∈ t, 0 ≤ w j, from λ j hj, h₀ j $ mem_insert_of_mem hj, rw [sum_insert hi] at hpos, by_cases hsum_t : ∑ j in t, w j = 0, { have ws : ∀ j ∈ t, w j = 0, from (sum_eq_zero_iff_of_nonneg hs₀).1 hsum_t, have wz : ∑ j in t, w j • z j = 0, from sum_eq_zero (λ i hi, by simp [ws i hi]), simp only [center_mass, sum_insert hi, wz, hsum_t, add_zero], simp only [hsum_t, add_zero] at hpos, rw [← mul_smul, inv_mul_cancel (ne_of_gt hpos), one_smul], exact zi }, { rw [finset.center_mass_insert _ _ _ hi hsum_t], refine convex_iff_div.1 hs zi (ht hs₀ _ _) _ (sum_nonneg hs₀) hpos, { exact lt_of_le_of_ne (sum_nonneg hs₀) (ne.symm hsum_t) }, { intros j hj, exact hmem j (mem_insert_of_mem hj) }, { exact h₀ _ (mem_insert_self _ _) } } end lemma convex.sum_mem (hs : convex s) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s) : ∑ i in t, w i • z i ∈ s := by simpa only [h₁, center_mass, inv_one, one_smul] using hs.center_mass_mem h₀ (h₁.symm ▸ zero_lt_one) hz lemma convex_iff_sum_mem : convex s ↔ (∀ (t : finset E) (w : E → ℝ), (∀ i ∈ t, 0 ≤ w i) → ∑ i in t, w i = 1 → (∀ x ∈ t, x ∈ s) → ∑ x in t, w x • x ∈ s ) := begin refine ⟨λ hs t w hw₀ hw₁ hts, hs.sum_mem hw₀ hw₁ hts, _⟩, intros h x y hx hy a b ha hb hab, by_cases h_cases: x = y, { rw [h_cases, ←add_smul, hab, one_smul], exact hy }, { convert h {x, y} (λ z, if z = y then b else a) _ _ _, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl] }, { simp_intros i hi, cases hi; subst i; simp [ha, hb, if_neg h_cases] }, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl, hab] }, { simp_intros i hi, cases hi; subst i; simp [hx, hy, if_neg h_cases] } } end /-- Jensen's inequality, `finset.center_mass` version. -/ lemma convex_on.map_center_mass_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (hpos : 0 < ∑ i in t, w i) (hmem : ∀ i ∈ t, z i ∈ s) : f (t.center_mass w z) ≤ t.center_mass w (f ∘ z) := begin have hmem' : ∀ i ∈ t, (z i, (f ∘ z) i) ∈ {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2}, from λ i hi, ⟨hmem i hi, le_refl _⟩, convert (hf.convex_epigraph.center_mass_mem h₀ hpos hmem').2; simp only [center_mass, function.comp, prod.smul_fst, prod.fst_sum, prod.smul_snd, prod.snd_sum] end /-- Jensen's inequality, `finset.sum` version. -/ lemma convex_on.map_sum_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hmem : ∀ i ∈ t, z i ∈ s) : f (∑ i in t, w i • z i) ≤ ∑ i in t, w i * (f (z i)) := by simpa only [center_mass, h₁, inv_one, one_smul] using hf.map_center_mass_le h₀ (h₁.symm ▸ zero_lt_one) hmem /-- If a function `f` is convex on `s` takes value `y` at the center of mass of some points `z i ∈ s`, then for some `i` we have `y ≤ f (z i)`. -/ lemma convex_on.exists_ge_of_center_mass {f : E → ℝ} (h : convex_on s f) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) (hz : ∀ i ∈ t, z i ∈ s) : ∃ i ∈ t, f (t.center_mass w z) ≤ f (z i) := begin set y := t.center_mass w z, have : f y ≤ t.center_mass w (f ∘ z) := h.map_center_mass_le hw₀ hws hz, rw ← sum_filter_ne_zero at hws, rw [← finset.center_mass_filter_ne_zero (f ∘ z), center_mass, smul_eq_mul, ← div_eq_inv_mul, le_div_iff hws, mul_sum] at this, replace : ∃ i ∈ t.filter (λ i, w i ≠ 0), f y * w i ≤ w i • (f ∘ z) i := exists_le_of_sum_le (nonempty_of_sum_ne_zero (ne_of_gt hws)) this, rcases this with ⟨i, hi, H⟩, rw [mem_filter] at hi, use [i, hi.1], simp only [smul_eq_mul, mul_comm (w i)] at H, refine (mul_le_mul_right _).1 H, exact lt_of_le_of_ne (hw₀ i hi.1) hi.2.symm end end center_mass /-! ### Convex hull -/ section convex_hull variable {t : set E} /-- The convex hull of a set `s` is the minimal convex set that includes `s`. -/ def convex_hull (s : set E) : set E := ⋂ (t : set E) (hst : s ⊆ t) (ht : convex t), t variable (s) lemma subset_convex_hull : s ⊆ convex_hull s := set.subset_Inter $ λ t, set.subset_Inter $ λ hst, set.subset_Inter $ λ ht, hst lemma convex_convex_hull : convex (convex_hull s) := convex_Inter $ λ t, convex_Inter $ λ ht, convex_Inter id variable {s} lemma convex_hull_min (hst : s ⊆ t) (ht : convex t) : convex_hull s ⊆ t := set.Inter_subset_of_subset t $ set.Inter_subset_of_subset hst $ set.Inter_subset _ ht lemma convex_hull_mono (hst : s ⊆ t) : convex_hull s ⊆ convex_hull t := convex_hull_min (set.subset.trans hst $ subset_convex_hull t) (convex_convex_hull t) lemma convex.convex_hull_eq {s : set E} (hs : convex s) : convex_hull s = s := set.subset.antisymm (convex_hull_min (set.subset.refl _) hs) (subset_convex_hull s) @[simp] lemma convex_hull_singleton {x : E} : convex_hull ({x} : set E) = {x} := (convex_singleton x).convex_hull_eq lemma is_linear_map.image_convex_hull {f : E → F} (hf : is_linear_map ℝ f) : f '' (convex_hull s) = convex_hull (f '' s) := begin refine set.subset.antisymm _ _, { rw [set.image_subset_iff], exact convex_hull_min (set.image_subset_iff.1 $ subset_convex_hull $ f '' s) ((convex_convex_hull (f '' s)).is_linear_preimage hf) }, { exact convex_hull_min (set.image_subset _ $ subset_convex_hull s) ((convex_convex_hull s).is_linear_image hf) } end lemma linear_map.image_convex_hull (f : E →ₗ[ℝ] F) : f '' (convex_hull s) = convex_hull (f '' s) := f.is_linear.image_convex_hull lemma finset.center_mass_mem_convex_hull (t : finset ι) {w : ι → ℝ} (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) {z : ι → E} (hz : ∀ i ∈ t, z i ∈ s) : t.center_mass w z ∈ convex_hull s := (convex_convex_hull s).center_mass_mem hw₀ hws (λ i hi, subset_convex_hull s $ hz i hi) -- TODO : Do we need other versions of the next lemma? /-- Convex hull of `s` is equal to the set of all centers of masses of `finset`s `t`, `z '' t ⊆ s`. This version allows finsets in any type in any universe. -/ lemma convex_hull_eq (s : set E) : convex_hull s = {x : E | ∃ (ι : Type u') (t : finset ι) (w : ι → ℝ) (z : ι → E) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hw₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s), t.center_mass w z = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, use [punit, {punit.star}, λ _, 1, λ _, x, λ _ _, zero_le_one, finset.sum_singleton, λ _ _, hx], simp only [finset.center_mass, finset.sum_singleton, inv_one, one_smul] }, { rintros x y ⟨ι, sx, wx, zx, hwx₀, hwx₁, hzx, rfl⟩ ⟨ι', sy, wy, zy, hwy₀, hwy₁, hzy, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment' _ _ _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, _, _, _, _, rfl⟩, { rintros i hi, rw [finset.mem_union, finset.mem_map, finset.mem_map] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; simp only [sum.elim_inl, sum.elim_inr]; apply_rules [mul_nonneg, hwx₀, hwy₀] }, { simp [finset.sum_sum_elim, finset.mul_sum.symm, *] }, { intros i hi, rw [finset.mem_union, finset.mem_map, finset.mem_map] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; apply_rules [hzx, hzy] } }, { rintros _ ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, exact t.center_mass_mem_convex_hull hw₀ (hw₁.symm ▸ zero_lt_one) hz } end /-- Maximum principle for convex functions. If a function `f` is convex on the convex hull of `s`, then `f` can't have a maximum on `convex_hull s` outside of `s`. -/ lemma convex_on.exists_ge_of_mem_convex_hull {f : E → ℝ} (hf : convex_on (convex_hull s) f) {x} (hx : x ∈ convex_hull s) : ∃ y ∈ s, f x ≤ f y := begin rw convex_hull_eq at hx, rcases hx with ⟨α, t, w, z, hw₀, hw₁, hz, rfl⟩, rcases hf.exists_ge_of_center_mass hw₀ (hw₁.symm ▸ zero_lt_one) (λ i hi, subset_convex_hull s (hz i hi)) with ⟨i, hit, Hi⟩, exact ⟨z i, hz i hit, Hi⟩ end lemma finset.convex_hull_eq (s : finset E) : convex_hull ↑s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in s, w y = 1), s.center_mass w id = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, rw [finset.mem_coe] at hx, refine ⟨_, _, _, finset.center_mass_ite_eq _ _ _ hx⟩, { intros, split_ifs, exacts [zero_le_one, le_refl 0] }, { rw [finset.sum_ite_eq, if_pos hx] } }, { rintros x y ⟨wx, hwx₀, hwx₁, rfl⟩ ⟨wy, hwy₀, hwy₁, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, rfl⟩, { rintros i hi, apply_rules [add_nonneg, mul_nonneg, hwx₀, hwy₀], }, { simp only [finset.sum_add_distrib, finset.mul_sum.symm, mul_one, *] } }, { rintros _ ⟨w, hw₀, hw₁, rfl⟩, exact s.center_mass_mem_convex_hull (λ x hx, hw₀ _ hx) (hw₁.symm ▸ zero_lt_one) (λ x hx, hx) } end lemma set.finite.convex_hull_eq {s : set E} (hs : finite s) : convex_hull s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in hs.to_finset, w y = 1), hs.to_finset.center_mass w id = x} := by simpa only [set.finite.coe_to_finset, set.finite.mem_to_finset, exists_prop] using hs.to_finset.convex_hull_eq lemma convex_hull_eq_union_convex_hull_finite_subsets (s : set E) : convex_hull s = ⋃ (t : finset E) (w : ↑t ⊆ s), convex_hull ↑t := begin refine subset.antisymm _ _, { rw [convex_hull_eq.{u}], rintros x ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, simp only [mem_Union], refine ⟨t.image z, _, _⟩, { rw [finset.coe_image, image_subset_iff], exact hz }, { apply t.center_mass_mem_convex_hull hw₀, { simp only [hw₁, zero_lt_one] }, { exact λ i hi, finset.mem_coe.2 (finset.mem_image_of_mem _ hi) } } }, { exact Union_subset (λ i, Union_subset convex_hull_mono), }, end lemma is_linear_map.convex_hull_image {f : E → F} (hf : is_linear_map ℝ f) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := set.subset.antisymm (convex_hull_min (image_subset _ (subset_convex_hull s)) $ (convex_convex_hull s).is_linear_image hf) (image_subset_iff.2 $ convex_hull_min (image_subset_iff.1 $ subset_convex_hull _) ((convex_convex_hull _).is_linear_preimage hf)) lemma linear_map.convex_hull_image (f : E →ₗ[ℝ] F) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := f.is_linear.convex_hull_image s end convex_hull /-! ### Simplex -/ section simplex variables (ι) [fintype ι] {f : ι → ℝ} /-- The standard simplex in the space of functions `ι → ℝ` is the set of vectors with non-negative coordinates with total sum `1`. -/ def std_simplex (ι : Type*) [fintype ι] : set (ι → ℝ) := {f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1} lemma std_simplex_eq_inter : std_simplex ι = (⋂ x, {f | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] } lemma convex_std_simplex : convex (std_simplex ι) := begin refine λ f g hf hg a b ha hb hab, ⟨λ x, _, _⟩, { apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] }, { erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2, smul_eq_mul, smul_eq_mul, mul_one, mul_one], exact hab } end variable {ι} lemma ite_eq_mem_std_simplex (i : ι) : (λ j, ite (i = j) (1:ℝ) 0) ∈ std_simplex ι := ⟨λ j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)]⟩ /-- `std_simplex ι` is the convex hull of the canonical basis in `ι → ℝ`. -/ lemma convex_hull_basis_eq_std_simplex : convex_hull (range $ λ(i j:ι), if i = j then (1:ℝ) else 0) = std_simplex ι := begin refine subset.antisymm (convex_hull_min _ (convex_std_simplex ι)) _, { rintros _ ⟨i, rfl⟩, exact ite_eq_mem_std_simplex i }, { rintros w ⟨hw₀, hw₁⟩, rw [pi_eq_sum_univ w, ← finset.univ.center_mass_eq_of_sum_1 _ hw₁], exact finset.univ.center_mass_mem_convex_hull (λ i hi, hw₀ i) (hw₁.symm ▸ zero_lt_one) (λ i hi, mem_range_self i) } end variable {ι} /-- The convex hull of a finite set is the image of the standard simplex in `s → ℝ` under the linear map sending each function `w` to `∑ x in s, w x • x`. Since we have no sums over finite sets, we use sum over `@finset.univ _ hs.fintype`. The map is defined in terms of operations on `(s → ℝ) →ₗ[ℝ] ℝ` so that later we will not need to prove that this map is linear. -/ lemma set.finite.convex_hull_eq_image {s : set E} (hs : finite s) : convex_hull s = by haveI := hs.fintype; exact (⇑(∑ x : s, (@linear_map.proj ℝ s _ (λ i, ℝ) _ _ x).smul_right x.1)) '' (std_simplex s) := begin rw [← convex_hull_basis_eq_std_simplex, ← linear_map.convex_hull_image, ← set.range_comp, (∘)], apply congr_arg, convert subtype.range_coe.symm, ext x, simp [linear_map.sum_apply, ite_smul, finset.filter_eq] end /-- All values of a function `f ∈ std_simplex ι` belong to `[0, 1]`. -/ lemma mem_Icc_of_mem_std_simplex (hf : f ∈ std_simplex ι) (x) : f x ∈ I := ⟨hf.1 x, hf.2 ▸ finset.single_le_sum (λ y hy, hf.1 y) (finset.mem_univ x)⟩ end simplex
7b2fcd280118165d609f99dfeb9a226c610e7dff
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/slow/nat_wo_hints.lean
e4ea3f41348f5b5a33fe7aa8ab2736aae5ab8025
[ "Apache-2.0" ]
permissive
tjiaqi/lean
4634d729795c164664d10d093f3545287c76628f
d0ce4cf62f4246b0600c07e074d86e51f2195e30
refs/heads/master
1,622,323,796,480
1,422,643,069,000
1,422,643,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
49,610
lean
---------------------------------------------------------------------------------------------------- -- Copyright (c) 2014 Floris van Doorn. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Floris van Doorn ---------------------------------------------------------------------------------------------------- import logic algebra.binary open tactic binary eq.ops eq open decidable namespace experiment inductive nat : Type := zero : nat, succ : nat → nat namespace nat notation `ℕ`:max := nat definition plus (x y : ℕ) : ℕ := nat.rec x (λ n r, succ r) y definition to_nat [coercion] (n : num) : ℕ := num.rec zero (λ n, pos_num.rec (succ zero) (λ n r, plus r (plus r (succ zero))) (λ n r, plus r r) n) n namespace helper_tactics definition apply_refl := apply @eq.refl tactic_hint apply_refl end helper_tactics open helper_tactics theorem nat_rec_zero {P : ℕ → Type} (x : P 0) (f : ∀m, P m → P (succ m)) : nat.rec x f 0 = x theorem nat_rec_succ {P : ℕ → Type} (x : P 0) (f : ∀m, P m → P (succ m)) (n : ℕ) : nat.rec x f (succ n) = f n (nat.rec x f n) theorem succ_ne_zero (n : ℕ) : succ n ≠ 0 := assume H : succ n = 0, have H2 : true = false, from let f := (nat.rec false (fun a b, true)) in calc true = f (succ n) : _ ... = f 0 : {H} ... = false : _, absurd H2 true_ne_false definition pred (n : ℕ) := nat.rec 0 (fun m x, m) n theorem pred_zero : pred 0 = 0 theorem pred_succ (n : ℕ) : pred (succ n) = n theorem zero_or_succ (n : ℕ) : n = 0 ∨ n = succ (pred n) := induction_on n (or.intro_left _ (eq.refl 0)) (take m IH, or.intro_right _ (show succ m = succ (pred (succ m)), from congr_arg succ ((pred_succ m)⁻¹))) theorem zero_or_succ2 (n : ℕ) : n = 0 ∨ ∃k, n = succ k := or_of_or_of_imp_of_imp (zero_or_succ n) (assume H, H) (assume H : n = succ (pred n), exists.intro (pred n) H) theorem case {P : ℕ → Prop} (n : ℕ) (H1: P 0) (H2 : ∀m, P (succ m)) : P n := induction_on n H1 (take m IH, H2 m) theorem discriminate {B : Prop} {n : ℕ} (H1: n = 0 → B) (H2 : ∀m, n = succ m → B) : B := or.elim (zero_or_succ n) (take H3 : n = 0, H1 H3) (take H3 : n = succ (pred n), H2 (pred n) H3) theorem succ_inj {n m : ℕ} (H : succ n = succ m) : n = m := calc n = pred (succ n) : (pred_succ n)⁻¹ ... = pred (succ m) : {H} ... = m : pred_succ m theorem succ_ne_self (n : ℕ) : succ n ≠ n := induction_on n (take H : 1 = 0, have ne : 1 ≠ 0, from succ_ne_zero 0, absurd H ne) (take k IH H, IH (succ_inj H)) theorem decidable_eq [instance] (n m : ℕ) : decidable (n = m) := have general : ∀n, decidable (n = m), from rec_on m (take n, rec_on n (inl (eq.refl 0)) (λ m iH, inr (succ_ne_zero m))) (λ (m' : ℕ) (iH1 : ∀n, decidable (n = m')), take n, rec_on n (inr (ne.symm (succ_ne_zero m'))) (λ (n' : ℕ) (iH2 : decidable (n' = succ m')), have d1 : decidable (n' = m'), from iH1 n', decidable.rec_on d1 (assume Heq : n' = m', inl (congr_arg succ Heq)) (assume Hne : n' ≠ m', have H1 : succ n' ≠ succ m', from assume Heq, absurd (succ_inj Heq) Hne, inr H1))), general n theorem two_step_induction_on {P : ℕ → Prop} (a : ℕ) (H1 : P 0) (H2 : P 1) (H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : P a := have stronger : P a ∧ P (succ a), from induction_on a (and.intro H1 H2) (take k IH, have IH1 : P k, from and.elim_left IH, have IH2 : P (succ k), from and.elim_right IH, and.intro IH2 (H3 k IH1 IH2)), and.elim_left stronger theorem sub_induction {P : ℕ → ℕ → Prop} (n m : ℕ) (H1 : ∀m, P 0 m) (H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : P n m := have general : ∀m, P n m, from induction_on n (take m : ℕ, H1 m) (take k : ℕ, assume IH : ∀m, P k m, take m : ℕ, discriminate (assume Hm : m = 0, Hm⁻¹ ▸ (H2 k)) (take l : ℕ, assume Hm : m = succ l, Hm⁻¹ ▸ (H3 k l (IH l)))), general m -------------------------------------------------- add definition add (x y : ℕ) : ℕ := plus x y infixl `+` := add theorem add_zero (n : ℕ) : n + 0 = n theorem add_succ (n m : ℕ) : n + succ m = succ (n + m) ---------- comm, assoc theorem zero_add (n : ℕ) : 0 + n = n := induction_on n (add_zero 0) (take m IH, show 0 + succ m = succ m, from calc 0 + succ m = succ (0 + m) : add_succ _ _ ... = succ m : {IH}) theorem succ_add (n m : ℕ) : (succ n) + m = succ (n + m) := induction_on m (calc succ n + 0 = succ n : add_zero (succ n) ... = succ (n + 0) : {symm (add_zero n)}) (take k IH, calc succ n + succ k = succ (succ n + k) : add_succ _ _ ... = succ (succ (n + k)) : {IH} ... = succ (n + succ k) : {symm (add_succ _ _)}) theorem add_comm (n m : ℕ) : n + m = m + n := induction_on m (trans (add_zero _) (symm (zero_add _))) (take k IH, calc n + succ k = succ (n+k) : add_succ _ _ ... = succ (k + n) : {IH} ... = succ k + n : symm (succ_add _ _)) theorem succ_add_eq_add_succ (n m : ℕ) : succ n + m = n + succ m := calc succ n + m = succ (n + m) : succ_add n m ... = n +succ m : symm (add_succ n m) theorem add_comm_succ (n m : ℕ) : n + succ m = m + succ n := calc n + succ m = succ n + m : symm (succ_add_eq_add_succ n m) ... = m + succ n : add_comm (succ n) m theorem add_assoc (n m k : ℕ) : (n + m) + k = n + (m + k) := induction_on k (calc (n + m) + 0 = n + m : add_zero _ ... = n + (m + 0) : {symm (add_zero m)}) (take l IH, calc (n + m) + succ l = succ ((n + m) + l) : add_succ _ _ ... = succ (n + (m + l)) : {IH} ... = n + succ (m + l) : symm (add_succ _ _) ... = n + (m + succ l) : {symm (add_succ _ _)}) theorem add_left_comm (n m k : ℕ) : n + (m + k) = m + (n + k) := left_comm add_comm add_assoc n m k theorem add_right_comm (n m k : ℕ) : n + m + k = n + k + m := right_comm add_comm add_assoc n m k ---------- inversion theorem add_cancel_left {n m k : ℕ} : n + m = n + k → m = k := induction_on n (take H : 0 + m = 0 + k, calc m = 0 + m : symm (zero_add m) ... = 0 + k : H ... = k : zero_add k) (take (n : ℕ) (IH : n + m = n + k → m = k) (H : succ n + m = succ n + k), have H2 : succ (n + m) = succ (n + k), from calc succ (n + m) = succ n + m : symm (succ_add n m) ... = succ n + k : H ... = succ (n + k) : succ_add n k, have H3 : n + m = n + k, from succ_inj H2, IH H3) --rename to and_cancel_right theorem add_cancel_right {n m k : ℕ} (H : n + m = k + m) : n = k := have H2 : m + n = m + k, from calc m + n = n + m : add_comm m n ... = k + m : H ... = m + k : add_comm k m, add_cancel_left H2 theorem eq_zero_of_add_eq_zero_right {n m : ℕ} : n + m = 0 → n = 0 := induction_on n (take (H : 0 + m = 0), eq.refl 0) (take k IH, assume (H : succ k + m = 0), absurd (show succ (k + m) = 0, from calc succ (k + m) = succ k + m : symm (succ_add k m) ... = 0 : H) (succ_ne_zero (k + m))) theorem add_eq_zero_right {n m : ℕ} (H : n + m = 0) : m = 0 := eq_zero_of_add_eq_zero_right (trans (add_comm m n) H) theorem add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 ∧ m = 0 := and.intro (eq_zero_of_add_eq_zero_right H) (add_eq_zero_right H) -- add_eq_self below ---------- misc theorem add_one (n:ℕ) : n + 1 = succ n := calc n + 1 = succ (n + 0) : add_succ _ _ ... = succ n : {add_zero _} theorem add_one_left (n:ℕ) : 1 + n = succ n := calc 1 + n = succ (0 + n) : succ_add _ _ ... = succ n : {zero_add _} --the following theorem has a terrible name, but since the name is not a substring or superstring of another name, it is at least easy to globally replace it theorem induction_plus_one {P : ℕ → Prop} (a : ℕ) (H1 : P 0) (H2 : ∀ (n : ℕ) (IH : P n), P (n + 1)) : P a := nat.rec H1 (take n IH, (add_one n) ▸ (H2 n IH)) a -------------------------------------------------- mul definition mul (n m : ℕ) := nat.rec 0 (fun m x, x + n) m infixl `*` := mul theorem mul_zero_right (n:ℕ) : n * 0 = 0 theorem mul_succ_right (n m:ℕ) : n * succ m = n * m + n set_option unifier.max_steps 100000 ---------- comm, distr, assoc, identity theorem mul_zero_left (n:ℕ) : 0 * n = 0 := induction_on n (mul_zero_right 0) (take m IH, calc 0 * succ m = 0 * m + 0 : mul_succ_right _ _ ... = 0 * m : add_zero _ ... = 0 : IH) theorem mul_succ_left (n m:ℕ) : (succ n) * m = (n * m) + m := induction_on m (calc succ n * 0 = 0 : mul_zero_right _ ... = n * 0 : symm (mul_zero_right _) ... = n * 0 + 0 : symm (add_zero _)) (take k IH, calc succ n * succ k = (succ n * k) + succ n : mul_succ_right _ _ ... = (n * k) + k + succ n : { IH } ... = (n * k) + (k + succ n) : add_assoc _ _ _ ... = (n * k) + (n + succ k) : {add_comm_succ _ _} ... = (n * k) + n + succ k : symm (add_assoc _ _ _) ... = (n * succ k) + succ k : {symm (mul_succ_right n k)}) theorem mul_comm (n m:ℕ) : n * m = m * n := induction_on m (trans (mul_zero_right _) (symm (mul_zero_left _))) (take k IH, calc n * succ k = n * k + n : mul_succ_right _ _ ... = k * n + n : {IH} ... = (succ k) * n : symm (mul_succ_left _ _)) theorem mul_add_distr_left (n m k : ℕ) : (n + m) * k = n * k + m * k := induction_on k (calc (n + m) * 0 = 0 : mul_zero_right _ ... = 0 + 0 : symm (add_zero _) ... = n * 0 + 0 : eq.refl _ ... = n * 0 + m * 0 : eq.refl _) (take l IH, calc (n + m) * succ l = (n + m) * l + (n + m) : mul_succ_right _ _ ... = n * l + m * l + (n + m) : {IH} ... = n * l + m * l + n + m : symm (add_assoc _ _ _) ... = n * l + n + m * l + m : {add_right_comm _ _ _} ... = n * l + n + (m * l + m) : add_assoc _ _ _ ... = n * succ l + (m * l + m) : {symm (mul_succ_right _ _)} ... = n * succ l + m * succ l : {symm (mul_succ_right _ _)}) theorem mul_add_distr_right (n m k : ℕ) : n * (m + k) = n * m + n * k := calc n * (m + k) = (m + k) * n : mul_comm _ _ ... = m * n + k * n : mul_add_distr_left _ _ _ ... = n * m + k * n : {mul_comm _ _} ... = n * m + n * k : {mul_comm _ _} theorem mul_assoc (n m k:ℕ) : (n * m) * k = n * (m * k) := induction_on k (calc (n * m) * 0 = 0 : mul_zero_right _ ... = n * 0 : symm (mul_zero_right _) ... = n * (m * 0) : {symm (mul_zero_right _)}) (take l IH, calc (n * m) * succ l = (n * m) * l + n * m : mul_succ_right _ _ ... = n * (m * l) + n * m : {IH} ... = n * (m * l + m) : symm (mul_add_distr_right _ _ _) ... = n * (m * succ l) : {symm (mul_succ_right _ _)}) theorem mul_comm_left (n m k : ℕ) : n * (m * k) = m * (n * k) := left_comm mul_comm mul_assoc n m k theorem mul_comm_right (n m k : ℕ) : n * m * k = n * k * m := right_comm mul_comm mul_assoc n m k theorem mul_one_right (n : ℕ) : n * 1 = n := calc n * 1 = n * 0 + n : mul_succ_right n 0 ... = 0 + n : {mul_zero_right n} ... = n : zero_add n theorem mul_one_left (n : ℕ) : 1 * n = n := calc 1 * n = n * 1 : mul_comm _ _ ... = n : mul_one_right n ---------- inversion theorem mul_eq_zero {n m : ℕ} (H : n * m = 0) : n = 0 ∨ m = 0 := discriminate (take Hn : n = 0, or.intro_left _ Hn) (take (k : ℕ), assume (Hk : n = succ k), discriminate (take (Hm : m = 0), or.intro_right _ Hm) (take (l : ℕ), assume (Hl : m = succ l), have Heq : succ (k * succ l + l) = n * m, from symm (calc n * m = n * succ l : { Hl } ... = succ k * succ l : { Hk } ... = k * succ l + succ l : mul_succ_left _ _ ... = succ (k * succ l + l) : add_succ _ _), absurd (trans Heq H) (succ_ne_zero _))) -- see more under "positivity" below -------------------------------------------------- le definition le (n m:ℕ) : Prop := ∃k, n + k = m infix `<=` := le infix `≤` := le theorem le_intro {n m k : ℕ} (H : n + k = m) : n ≤ m := exists.intro k H theorem le_elim {n m : ℕ} (H : n ≤ m) : ∃ k, n + k = m := H ---------- partial order (totality is part of lt) theorem le_intro2 (n m : ℕ) : n ≤ n + m := le_intro (eq.refl (n + m)) theorem le_refl (n : ℕ) : n ≤ n := le_intro (add_zero n) theorem zero_le (n : ℕ) : 0 ≤ n := le_intro (zero_add n) theorem le_zero {n : ℕ} (H : n ≤ 0) : n = 0 := obtain (k : ℕ) (Hk : n + k = 0), from le_elim H, eq_zero_of_add_eq_zero_right Hk theorem not_succ_zero_le (n : ℕ) : ¬ succ n ≤ 0 := assume H : succ n ≤ 0, have H2 : succ n = 0, from le_zero H, absurd H2 (succ_ne_zero n) theorem le_zero_inv {n : ℕ} (H : n ≤ 0) : n = 0 := obtain (k : ℕ) (Hk : n + k = 0), from le_elim H, eq_zero_of_add_eq_zero_right Hk theorem le_trans {n m k : ℕ} (H1 : n ≤ m) (H2 : m ≤ k) : n ≤ k := obtain (l1 : ℕ) (Hl1 : n + l1 = m), from le_elim H1, obtain (l2 : ℕ) (Hl2 : m + l2 = k), from le_elim H2, le_intro (calc n + (l1 + l2) = n + l1 + l2 : symm (add_assoc n l1 l2) ... = m + l2 : { Hl1 } ... = k : Hl2) theorem le_antisym {n m : ℕ} (H1 : n ≤ m) (H2 : m ≤ n) : n = m := obtain (k : ℕ) (Hk : n + k = m), from (le_elim H1), obtain (l : ℕ) (Hl : m + l = n), from (le_elim H2), have L1 : k + l = 0, from add_cancel_left (calc n + (k + l) = n + k + l : { symm (add_assoc n k l) } ... = m + l : { Hk } ... = n : Hl ... = n + 0 : symm (add_zero n)), have L2 : k = 0, from eq_zero_of_add_eq_zero_right L1, calc n = n + 0 : symm (add_zero n) ... = n + k : { symm L2 } ... = m : Hk ---------- interaction with add theorem add_le_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k + n ≤ k + m := obtain (l : ℕ) (Hl : n + l = m), from (le_elim H), le_intro (calc k + n + l = k + (n + l) : add_assoc k n l ... = k + m : { Hl }) theorem add_le_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n + k ≤ m + k := (add_comm k m) ▸ (add_comm k n) ▸ (add_le_left H k) theorem add_le {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n + m ≤ k + l := le_trans (add_le_right H1 m) (add_le_left H2 k) theorem add_le_left_inv {n m k : ℕ} (H : k + n ≤ k + m) : n ≤ m := obtain (l : ℕ) (Hl : k + n + l = k + m), from (le_elim H), le_intro (add_cancel_left (calc k + (n + l) = k + n + l : symm (add_assoc k n l) ... = k + m : Hl)) theorem add_le_right_inv {n m k : ℕ} (H : n + k ≤ m + k) : n ≤ m := add_le_left_inv (add_comm m k ▸ add_comm n k ▸ H) ---------- interaction with succ and pred theorem succ_le {n m : ℕ} (H : n ≤ m) : succ n ≤ succ m := add_one m ▸ add_one n ▸ add_le_right H 1 theorem succ_le_cancel {n m : ℕ} (H : succ n ≤ succ m) : n ≤ m := add_le_right_inv ((add_one m)⁻¹ ▸ (add_one n)⁻¹ ▸ H) theorem self_le_succ (n : ℕ) : n ≤ succ n := le_intro (add_one n) theorem le_imp_le_succ {n m : ℕ} (H : n ≤ m) : n ≤ succ m := le_trans H (self_le_succ m) theorem succ_le_left_or {n m : ℕ} (H : n ≤ m) : succ n ≤ m ∨ n = m := obtain (k : ℕ) (Hk : n + k = m), from (le_elim H), discriminate (assume H3 : k = 0, have Heq : n = m, from calc n = n + 0 : (add_zero n)⁻¹ ... = n + k : {H3⁻¹} ... = m : Hk, or.intro_right _ Heq) (take l:ℕ, assume H3 : k = succ l, have Hlt : succ n ≤ m, from (le_intro (calc succ n + l = n + succ l : succ_add_eq_add_succ n l ... = n + k : {H3⁻¹} ... = m : Hk)), or.intro_left _ Hlt) theorem succ_le_left {n m : ℕ} (H1 : n ≤ m) (H2 : n ≠ m) : succ n ≤ m := or_resolve_left (succ_le_left_or H1) H2 theorem succ_le_right_inv {n m : ℕ} (H : n ≤ succ m) : n ≤ m ∨ n = succ m := or_of_or_of_imp_of_imp (succ_le_left_or H) (take H2 : succ n ≤ succ m, show n ≤ m, from succ_le_cancel H2) (take H2 : n = succ m, H2) theorem succ_le_left_inv {n m : ℕ} (H : succ n ≤ m) : n ≤ m ∧ n ≠ m := obtain (k : ℕ) (H2 : succ n + k = m), from (le_elim H), and.intro (have H3 : n + succ k = m, from calc n + succ k = succ n + k : symm (succ_add_eq_add_succ n k) ... = m : H2, show n ≤ m, from le_intro H3) (assume H3 : n = m, have H4 : succ n ≤ n, from subst (symm H3) H, have H5 : succ n = n, from le_antisym H4 (self_le_succ n), show false, from absurd H5 (succ_ne_self n)) theorem le_pred_self (n : ℕ) : pred n ≤ n := case n (subst (symm pred_zero) (le_refl 0)) (take k : ℕ, subst (symm (pred_succ k)) (self_le_succ k)) theorem pred_le {n m : ℕ} (H : n ≤ m) : pred n ≤ pred m := discriminate (take Hn : n = 0, have H2 : pred n = 0, from calc pred n = pred 0 : {Hn} ... = 0 : pred_zero, subst (symm H2) (zero_le (pred m))) (take k : ℕ, assume Hn : n = succ k, obtain (l : ℕ) (Hl : n + l = m), from le_elim H, have H2 : pred n + l = pred m, from calc pred n + l = pred (succ k) + l : {Hn} ... = k + l : {pred_succ k} ... = pred (succ (k + l)) : symm (pred_succ (k + l)) ... = pred (succ k + l) : {symm (succ_add k l)} ... = pred (n + l) : {symm Hn} ... = pred m : {Hl}, le_intro H2) theorem pred_le_left_inv {n m : ℕ} (H : pred n ≤ m) : n ≤ m ∨ n = succ m := discriminate (take Hn : n = 0, or.intro_left _ (subst (symm Hn) (zero_le m))) (take k : ℕ, assume Hn : n = succ k, have H2 : pred n = k, from calc pred n = pred (succ k) : {Hn} ... = k : pred_succ k, have H3 : k ≤ m, from subst H2 H, have H4 : succ k ≤ m ∨ k = m, from succ_le_left_or H3, show n ≤ m ∨ n = succ m, from or_of_or_of_imp_of_imp H4 (take H5 : succ k ≤ m, show n ≤ m, from subst (symm Hn) H5) (take H5 : k = m, show n = succ m, from subst H5 Hn)) -- ### interaction with successor and predecessor theorem le_imp_succ_le_or_eq {n m : ℕ} (H : n ≤ m) : succ n ≤ m ∨ n = m := obtain (k : ℕ) (Hk : n + k = m), from (le_elim H), discriminate (assume H3 : k = 0, have Heq : n = m, from calc n = n + 0 : symm (add_zero n) ... = n + k : {symm H3} ... = m : Hk, or.intro_right _ Heq) (take l : nat, assume H3 : k = succ l, have Hlt : succ n ≤ m, from (le_intro (calc succ n + l = n + succ l : succ_add_eq_add_succ n l ... = n + k : {symm H3} ... = m : Hk)), or.intro_left _ Hlt) theorem le_ne_imp_succ_le {n m : ℕ} (H1 : n ≤ m) (H2 : n ≠ m) : succ n ≤ m := or_resolve_left (le_imp_succ_le_or_eq H1) H2 theorem le_succ_imp_le_or_eq {n m : ℕ} (H : n ≤ succ m) : n ≤ m ∨ n = succ m := or_of_or_of_imp_left (le_imp_succ_le_or_eq H) (take H2 : succ n ≤ succ m, show n ≤ m, from succ_le_cancel H2) theorem succ_le_imp_le_and_ne {n m : ℕ} (H : succ n ≤ m) : n ≤ m ∧ n ≠ m := and.intro (le_trans (self_le_succ n) H) (assume H2 : n = m, have H3 : succ n ≤ n, from subst (symm H2) H, have H4 : succ n = n, from le_antisym H3 (self_le_succ n), show false, from absurd H4 (succ_ne_self n)) theorem pred_le_self (n : ℕ) : pred n ≤ n := case n (subst (symm pred_zero) (le_refl 0)) (take k : nat, subst (symm (pred_succ k)) (self_le_succ k)) theorem pred_le_imp_le_or_eq {n m : ℕ} (H : pred n ≤ m) : n ≤ m ∨ n = succ m := discriminate (take Hn : n = 0, or.intro_left _ (subst (symm Hn) (zero_le m))) (take k : nat, assume Hn : n = succ k, have H2 : pred n = k, from calc pred n = pred (succ k) : {Hn} ... = k : pred_succ k, have H3 : k ≤ m, from subst H2 H, have H4 : succ k ≤ m ∨ k = m, from le_imp_succ_le_or_eq H3, show n ≤ m ∨ n = succ m, from or_of_or_of_imp_of_imp H4 (take H5 : succ k ≤ m, show n ≤ m, from subst (symm Hn) H5) (take H5 : k = m, show n = succ m, from subst H5 Hn)) ---------- interaction with mul theorem mul_le_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k * n ≤ k * m := obtain (l : ℕ) (Hl : n + l = m), from (le_elim H), induction_on k (have H2 : 0 * n = 0 * m, from calc 0 * n = 0 : mul_zero_left n ... = 0 * m : symm (mul_zero_left m), show 0 * n ≤ 0 * m, from subst H2 (le_refl (0 * n))) (take (l : ℕ), assume IH : l * n ≤ l * m, have H2 : l * n + n ≤ l * m + m, from add_le IH H, have H3 : succ l * n ≤ l * m + m, from subst (symm (mul_succ_left l n)) H2, show succ l * n ≤ succ l * m, from subst (symm (mul_succ_left l m)) H3) theorem mul_le_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n * k ≤ m * k := mul_comm k m ▸ mul_comm k n ▸ (mul_le_left H k) theorem mul_le {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n * m ≤ k * l := le_trans (mul_le_right H1 m) (mul_le_left H2 k) -- mul_le_[left|right]_inv below -------------------------------------------------- lt definition lt (n m : ℕ) := succ n ≤ m infix `<` := lt theorem lt_intro {n m k : ℕ} (H : succ n + k = m) : n < m := le_intro H theorem lt_elim {n m : ℕ} (H : n < m) : ∃ k, succ n + k = m := le_elim H theorem lt_intro2 (n m : ℕ) : n < n + succ m := lt_intro (succ_add_eq_add_succ n m) -------------------------------------------------- ge, gt definition ge (n m : ℕ) := m ≤ n infix `>=` := ge infix `≥` := ge definition gt (n m : ℕ) := m < n infix `>` := gt ---------- basic facts theorem lt_ne {n m : ℕ} (H : n < m) : n ≠ m := and.elim_right (succ_le_left_inv H) theorem lt_irrefl (n : ℕ) : ¬ n < n := assume H : n < n, absurd (eq.refl n) (lt_ne H) theorem lt_zero (n : ℕ) : 0 < succ n := succ_le (zero_le n) theorem lt_zero_inv (n : ℕ) : ¬ n < 0 := assume H : n < 0, have H2 : succ n = 0, from le_zero_inv H, absurd H2 (succ_ne_zero n) theorem lt_positive {n m : ℕ} (H : n < m) : ∃k, m = succ k := discriminate (take (Hm : m = 0), absurd (subst Hm H) (lt_zero_inv n)) (take (l : ℕ) (Hm : m = succ l), exists.intro l Hm) ---------- interaction with le theorem lt_imp_le_succ {n m : ℕ} (H : n < m) : succ n ≤ m := H theorem le_succ_imp_lt {n m : ℕ} (H : succ n ≤ m) : n < m := H theorem self_lt_succ (n : ℕ) : n < succ n := le_refl (succ n) theorem lt_imp_le {n m : ℕ} (H : n < m) : n ≤ m := and.elim_left (succ_le_imp_le_and_ne H) theorem le_imp_lt_or_eq {n m : ℕ} (H : n ≤ m) : n < m ∨ n = m := le_imp_succ_le_or_eq H theorem le_ne_imp_lt {n m : ℕ} (H1 : n ≤ m) (H2 : n ≠ m) : n < m := le_ne_imp_succ_le H1 H2 theorem le_imp_lt_succ {n m : ℕ} (H : n ≤ m) : n < succ m := succ_le H theorem lt_succ_imp_le {n m : ℕ} (H : n < succ m) : n ≤ m := succ_le_cancel H ---------- trans, antisym theorem lt_le_trans {n m k : ℕ} (H1 : n < m) (H2 : m ≤ k) : n < k := le_trans H1 H2 theorem le_lt_trans {n m k : ℕ} (H1 : n ≤ m) (H2 : m < k) : n < k := le_trans (succ_le H1) H2 theorem lt_trans {n m k : ℕ} (H1 : n < m) (H2 : m < k) : n < k := lt_le_trans H1 (lt_imp_le H2) theorem le_imp_not_gt {n m : ℕ} (H : n ≤ m) : ¬ n > m := assume H2 : m < n, absurd (le_lt_trans H H2) (lt_irrefl n) theorem lt_imp_not_ge {n m : ℕ} (H : n < m) : ¬ n ≥ m := assume H2 : m ≤ n, absurd (lt_le_trans H H2) (lt_irrefl n) theorem lt_antisym {n m : ℕ} (H : n < m) : ¬ m < n := le_imp_not_gt (lt_imp_le H) ---------- interaction with add theorem add_lt_left {n m : ℕ} (H : n < m) (k : ℕ) : k + n < k + m := add_succ k n ▸ add_le_left H k theorem add_lt_right {n m : ℕ} (H : n < m) (k : ℕ) : n + k < m + k := add_comm k m ▸ add_comm k n ▸ add_lt_left H k theorem add_le_lt {n m k l : ℕ} (H1 : n ≤ k) (H2 : m < l) : n + m < k + l := le_lt_trans (add_le_right H1 m) (add_lt_left H2 k) theorem add_lt_le {n m k l : ℕ} (H1 : n < k) (H2 : m ≤ l) : n + m < k + l := lt_le_trans (add_lt_right H1 m) (add_le_left H2 k) theorem add_lt {n m k l : ℕ} (H1 : n < k) (H2 : m < l) : n + m < k + l := add_lt_le H1 (lt_imp_le H2) theorem add_lt_left_inv {n m k : ℕ} (H : k + n < k + m) : n < m := add_le_left_inv ((add_succ k n)⁻¹ ▸ H) theorem add_lt_right_inv {n m k : ℕ} (H : n + k < m + k) : n < m := add_lt_left_inv (add_comm m k ▸ add_comm n k ▸ H) ---------- interaction with succ (see also the interaction with le) theorem succ_lt {n m : ℕ} (H : n < m) : succ n < succ m := add_one m ▸ add_one n ▸ add_lt_right H 1 theorem succ_lt_inv {n m : ℕ} (H : succ n < succ m) : n < m := add_lt_right_inv ((add_one m)⁻¹ ▸ (add_one n)⁻¹ ▸ H) theorem lt_self_succ (n : ℕ) : n < succ n := le_refl (succ n) theorem succ_lt_right {n m : ℕ} (H : n < m) : n < succ m := lt_trans H (lt_self_succ m) ---------- totality of lt and le theorem le_or_lt (n m : ℕ) : n ≤ m ∨ m < n := induction_on n (or.intro_left _ (zero_le m)) (take (k : ℕ), assume IH : k ≤ m ∨ m < k, or.elim IH (assume H : k ≤ m, obtain (l : ℕ) (Hl : k + l = m), from le_elim H, discriminate (assume H2 : l = 0, have H3 : m = k, from calc m = k + l : symm Hl ... = k + 0 : {H2} ... = k : add_zero k, have H4 : m < succ k, from subst H3 (lt_self_succ m), or.intro_right _ H4) (take l2 : ℕ, assume H2 : l = succ l2, have H3 : succ k + l2 = m, from calc succ k + l2 = k + succ l2 : succ_add_eq_add_succ k l2 ... = k + l : {symm H2} ... = m : Hl, or.intro_left _ (le_intro H3))) (assume H : m < k, or.intro_right _ (succ_lt_right H))) theorem trichotomy_alt (n m : ℕ) : (n < m ∨ n = m) ∨ m < n := or_of_or_of_imp_of_imp (le_or_lt n m) (assume H : n ≤ m, le_imp_lt_or_eq H) (assume H : m < n, H) theorem trichotomy (n m : ℕ) : n < m ∨ n = m ∨ m < n := iff.elim_left or.assoc (trichotomy_alt n m) theorem le_total (n m : ℕ) : n ≤ m ∨ m ≤ n := or_of_or_of_imp_of_imp (le_or_lt n m) (assume H : n ≤ m, H) (assume H : m < n, lt_imp_le H) -- interaction with mul under "positivity" theorem strong_induction_on {P : ℕ → Prop} (n : ℕ) (IH : ∀n, (∀m, m < n → P m) → P n) : P n := have stronger : ∀k, k ≤ n → P k, from induction_on n (take (k : ℕ), assume H : k ≤ 0, have H2 : k = 0, from le_zero_inv H, have H3 : ∀m, m < k → P m, from (take m : ℕ, assume H4 : m < k, have H5 : m < 0, from subst H2 H4, absurd H5 (lt_zero_inv m)), show P k, from IH k H3) (take l : ℕ, assume IHl : ∀k, k ≤ l → P k, take k : ℕ, assume H : k ≤ succ l, or.elim (succ_le_right_inv H) (assume H2 : k ≤ l, show P k, from IHl k H2) (assume H2 : k = succ l, have H3 : ∀m, m < k → P m, from (take m : ℕ, assume H4 : m < k, have H5 : m ≤ l, from lt_succ_imp_le (subst H2 H4), show P m, from IHl m H5), show P k, from IH k H3)), stronger n (le_refl n) theorem case_strong_induction_on {P : ℕ → Prop} (a : ℕ) (H0 : P 0) (Hind : ∀(n : ℕ), (∀m, m ≤ n → P m) → P (succ n)) : P a := strong_induction_on a (take n, case n (assume H : (∀m, m < 0 → P m), H0) (take n, assume H : (∀m, m < succ n → P m), Hind n (take m, assume H1 : m ≤ n, H m (le_imp_lt_succ H1)))) theorem add_eq_self {n m : ℕ} (H : n + m = n) : m = 0 := discriminate (take Hm : m = 0, Hm) (take k : ℕ, assume Hm : m = succ k, have H2 : succ n + k = n, from calc succ n + k = n + succ k : succ_add_eq_add_succ n k ... = n + m : {symm Hm} ... = n : H, have H3 : n < n, from lt_intro H2, have H4 : n ≠ n, from lt_ne H3, absurd (eq.refl n) H4) -------------------------------------------------- positivity -- we use " _ > 0" as canonical way of denoting that a number is positive ---------- basic theorem zero_or_positive (n : ℕ) : n = 0 ∨ n > 0 := or_of_or_of_imp_of_imp (or.swap (le_imp_lt_or_eq (zero_le n))) (take H : 0 = n, symm H) (take H : n > 0, H) theorem succ_positive {n m : ℕ} (H : n = succ m) : n > 0 := subst (symm H) (lt_zero m) theorem ne_zero_positive {n : ℕ} (H : n ≠ 0) : n > 0 := or.elim (zero_or_positive n) (take H2 : n = 0, absurd H2 H) (take H2 : n > 0, H2) theorem pos_imp_eq_succ {n : ℕ} (H : n > 0) : ∃l, n = succ l := discriminate (take H2, absurd (subst H2 H) (lt_irrefl 0)) (take l Hl, exists.intro l Hl) theorem add_positive_right (n : ℕ) {k : ℕ} (H : k > 0) : n + k > n := obtain (l : ℕ) (Hl : k = succ l), from pos_imp_eq_succ H, subst (symm Hl) (lt_intro2 n l) theorem add_positive_left (n : ℕ) {k : ℕ} (H : k > 0) : k + n > n := subst (add_comm n k) (add_positive_right n H) -- Positivity -- --------- -- -- Writing "t > 0" is the preferred way to assert that a natural number is positive. -- ### basic -- See also succ_pos. theorem succ_pos (n : ℕ) : 0 < succ n := succ_le (zero_le n) theorem case_zero_pos {P : ℕ → Prop} (y : ℕ) (H0 : P 0) (H1 : ∀y, y > 0 → P y) : P y := case y H0 (take y', H1 _ (succ_pos _)) theorem zero_or_pos (n : ℕ) : n = 0 ∨ n > 0 := or_of_or_of_imp_left (or.swap (le_imp_lt_or_eq (zero_le n))) (take H : 0 = n, symm H) theorem succ_imp_pos {n m : ℕ} (H : n = succ m) : n > 0 := subst (symm H) (succ_pos m) theorem ne_zero_pos {n : ℕ} (H : n ≠ 0) : n > 0 := or.elim (zero_or_pos n) (take H2 : n = 0, absurd H2 H) (take H2 : n > 0, H2) theorem add_pos_right (n : ℕ) {k : ℕ} (H : k > 0) : n + k > n := subst (add_zero n) (add_lt_left H n) theorem add_pos_left (n : ℕ) {k : ℕ} (H : k > 0) : k + n > n := subst (add_comm n k) (add_pos_right n H) ---------- mul theorem mul_positive {n m : ℕ} (Hn : n > 0) (Hm : m > 0) : n * m > 0 := obtain (k : ℕ) (Hk : n = succ k), from pos_imp_eq_succ Hn, obtain (l : ℕ) (Hl : m = succ l), from pos_imp_eq_succ Hm, succ_positive (calc n * m = succ k * m : {Hk} ... = succ k * succ l : {Hl} ... = succ k * l + succ k : mul_succ_right (succ k) l ... = succ (succ k * l + k) : add_succ _ _) theorem mul_positive_inv_left {n m : ℕ} (H : n * m > 0) : n > 0 := discriminate (assume H2 : n = 0, have H3 : n * m = 0, from calc n * m = 0 * m : {H2} ... = 0 : mul_zero_left m, have H4 : 0 > 0, from subst H3 H, absurd H4 (lt_irrefl 0)) (take l : ℕ, assume Hl : n = succ l, subst (symm Hl) (lt_zero l)) theorem mul_positive_inv_right {n m : ℕ} (H : n * m > 0) : m > 0 := mul_positive_inv_left (subst (mul_comm n m) H) theorem mul_left_inj {n m k : ℕ} (Hn : n > 0) (H : n * m = n * k) : m = k := have general : ∀m, n * m = n * k → m = k, from induction_on k (take m:ℕ, assume H : n * m = n * 0, have H2 : n * m = 0, from calc n * m = n * 0 : H ... = 0 : mul_zero_right n, have H3 : n = 0 ∨ m = 0, from mul_eq_zero H2, or_resolve_right H3 (ne.symm (lt_ne Hn))) (take (l : ℕ), assume (IH : ∀ m, n * m = n * l → m = l), take (m : ℕ), assume (H : n * m = n * succ l), have H2 : n * succ l > 0, from mul_positive Hn (lt_zero l), have H3 : m > 0, from mul_positive_inv_right (subst (symm H) H2), obtain (l2:ℕ) (Hm : m = succ l2), from pos_imp_eq_succ H3, have H4 : n * l2 + n = n * l + n, from calc n * l2 + n = n * succ l2 : symm (mul_succ_right n l2) ... = n * m : {symm Hm} ... = n * succ l : H ... = n * l + n : mul_succ_right n l, have H5 : n * l2 = n * l, from add_cancel_right H4, calc m = succ l2 : Hm ... = succ l : {IH l2 H5}), general m H theorem mul_right_inj {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k := mul_left_inj Hm (subst (mul_comm k m) (subst (mul_comm n m) H)) -- mul_eq_one below ---------- interaction of mul with le and lt theorem mul_lt_left {n m k : ℕ} (Hk : k > 0) (H : n < m) : k * n < k * m := have H2 : k * n < k * n + k, from add_positive_right (k * n) Hk, have H3 : k * n + k ≤ k * m, from subst (mul_succ_right k n) (mul_le_left H k), lt_le_trans H2 H3 theorem mul_lt_right {n m k : ℕ} (Hk : k > 0) (H : n < m) : n * k < m * k := subst (mul_comm k m) (subst (mul_comm k n) (mul_lt_left Hk H)) theorem mul_le_lt {n m k l : ℕ} (Hk : k > 0) (H1 : n ≤ k) (H2 : m < l) : n * m < k * l := le_lt_trans (mul_le_right H1 m) (mul_lt_left Hk H2) theorem mul_lt_le {n m k l : ℕ} (Hl : l > 0) (H1 : n < k) (H2 : m ≤ l) : n * m < k * l := le_lt_trans (mul_le_left H2 n) (mul_lt_right Hl H1) theorem mul_lt {n m k l : ℕ} (H1 : n < k) (H2 : m < l) : n * m < k * l := have H3 : n * m ≤ k * m, from mul_le_right (lt_imp_le H1) m, have H4 : k * m < k * l, from mul_lt_left (le_lt_trans (zero_le n) H1) H2, le_lt_trans H3 H4 theorem mul_lt_left_inv {n m k : ℕ} (H : k * n < k * m) : n < m := have general : ∀ m, k * n < k * m → n < m, from induction_on n (take m : ℕ, assume H2 : k * 0 < k * m, have H3 : 0 < k * m, from mul_zero_right k ▸ H2, show 0 < m, from mul_positive_inv_right H3) (take l : ℕ, assume IH : ∀ m, k * l < k * m → l < m, take m : ℕ, assume H2 : k * succ l < k * m, have H3 : 0 < k * m, from le_lt_trans (zero_le _) H2, have H4 : 0 < m, from mul_positive_inv_right H3, obtain (l2 : ℕ) (Hl2 : m = succ l2), from pos_imp_eq_succ H4, have H5 : k * l + k < k * m, from mul_succ_right k l ▸ H2, have H6 : k * l + k < k * succ l2, from Hl2 ▸ H5, have H7 : k * l + k < k * l2 + k, from mul_succ_right k l2 ▸ H6, have H8 : k * l < k * l2, from add_lt_right_inv H7, have H9 : l < l2, from IH l2 H8, have H10 : succ l < succ l2, from succ_lt H9, show succ l < m, from Hl2⁻¹ ▸ H10), general m H theorem mul_lt_right_inv {n m k : ℕ} (H : n * k < m * k) : n < m := mul_lt_left_inv (mul_comm m k ▸ mul_comm n k ▸ H) theorem mul_le_left_inv {n m k : ℕ} (H : succ k * n ≤ succ k * m) : n ≤ m := have H2 : succ k * n < succ k * m + succ k, from le_lt_trans H (lt_intro2 _ _), have H3 : succ k * n < succ k * succ m, from subst (symm (mul_succ_right (succ k) m)) H2, have H4 : n < succ m, from mul_lt_left_inv H3, show n ≤ m, from lt_succ_imp_le H4 theorem mul_le_right_inv {n m k : ℕ} (H : n * succ m ≤ k * succ m) : n ≤ k := mul_le_left_inv (subst (mul_comm k (succ m)) (subst (mul_comm n (succ m)) H)) theorem mul_eq_one_left {n m : ℕ} (H : n * m = 1) : n = 1 := have H2 : n * m > 0, from subst (symm H) (lt_zero 0), have H3 : n > 0, from mul_positive_inv_left H2, have H4 : m > 0, from mul_positive_inv_right H2, or.elim (le_or_lt n 1) (assume H5 : n ≤ 1, show n = 1, from le_antisym H5 H3) (assume H5 : n > 1, have H6 : n * m ≥ 2 * 1, from mul_le H5 H4, have H7 : 1 ≥ 2, from subst (mul_one_right 2) (subst H H6), absurd (self_lt_succ 1) (le_imp_not_gt H7)) theorem mul_eq_one_right {n m : ℕ} (H : n * m = 1) : m = 1 := mul_eq_one_left (subst (mul_comm n m) H) theorem mul_eq_one {n m : ℕ} (H : n * m = 1) : n = 1 ∧ m = 1 := and.intro (mul_eq_one_left H) (mul_eq_one_right H) -------------------------------------------------- sub definition sub (n m : ℕ) : ℕ := nat.rec n (fun m x, pred x) m infixl `-` := sub theorem sub_zero_right (n : ℕ) : n - 0 = n theorem sub_succ_right (n m : ℕ) : n - succ m = pred (n - m) theorem sub_zero_left (n : ℕ) : 0 - n = 0 := induction_on n (sub_zero_right 0) (take k : ℕ, assume IH : 0 - k = 0, calc 0 - succ k = pred (0 - k) : sub_succ_right 0 k ... = pred 0 : {IH} ... = 0 : pred_zero) theorem sub_succ_succ (n m : ℕ) : succ n - succ m = n - m := induction_on m (calc succ n - 1 = pred (succ n - 0) : sub_succ_right (succ n) 0 ... = pred (succ n) : {sub_zero_right (succ n)} ... = n : pred_succ n ... = n - 0 : symm (sub_zero_right n)) (take k : ℕ, assume IH : succ n - succ k = n - k, calc succ n - succ (succ k) = pred (succ n - succ k) : sub_succ_right (succ n) (succ k) ... = pred (n - k) : {IH} ... = n - succ k : symm (sub_succ_right n k)) theorem sub_one (n : ℕ) : n - 1 = pred n := calc n - 1 = pred (n - 0) : sub_succ_right n 0 ... = pred n : {sub_zero_right n} theorem sub_self (n : ℕ) : n - n = 0 := induction_on n (sub_zero_right 0) (take k IH, trans (sub_succ_succ k k) IH) theorem sub_add_add_right (n m k : ℕ) : (n + k) - (m + k) = n - m := induction_on k (calc (n + 0) - (m + 0) = n - (m + 0) : {add_zero _} ... = n - m : {add_zero _}) (take l : ℕ, assume IH : (n + l) - (m + l) = n - m, calc (n + succ l) - (m + succ l) = succ (n + l) - (m + succ l) : {add_succ _ _} ... = succ (n + l) - succ (m + l) : {add_succ _ _} ... = (n + l) - (m + l) : sub_succ_succ _ _ ... = n - m : IH) theorem sub_add_add_left (n m k : ℕ) : (k + n) - (k + m) = n - m := subst (add_comm m k) (subst (add_comm n k) (sub_add_add_right n m k)) theorem sub_add_left (n m : ℕ) : n + m - m = n := induction_on m (subst (symm (add_zero n)) (sub_zero_right n)) (take k : ℕ, assume IH : n + k - k = n, calc n + succ k - succ k = succ (n + k) - succ k : {add_succ n k} ... = n + k - k : sub_succ_succ _ _ ... = n : IH) theorem sub_sub (n m k : ℕ) : n - m - k = n - (m + k) := induction_on k (calc n - m - 0 = n - m : sub_zero_right _ ... = n - (m + 0) : {symm (add_zero m)}) (take l : ℕ, assume IH : n - m - l = n - (m + l), calc n - m - succ l = pred (n - m - l) : sub_succ_right (n - m) l ... = pred (n - (m + l)) : {IH} ... = n - succ (m + l) : symm (sub_succ_right n (m + l)) ... = n - (m + succ l) : {symm (add_succ m l)}) theorem succ_sub_sub (n m k : ℕ) : succ n - m - succ k = n - m - k := calc succ n - m - succ k = succ n - (m + succ k) : sub_sub _ _ _ ... = succ n - succ (m + k) : {add_succ m k} ... = n - (m + k) : sub_succ_succ _ _ ... = n - m - k : symm (sub_sub n m k) theorem sub_add_right_eq_zero (n m : ℕ) : n - (n + m) = 0 := calc n - (n + m) = n - n - m : symm (sub_sub n n m) ... = 0 - m : {sub_self n} ... = 0 : sub_zero_left m theorem sub_comm (m n k : ℕ) : m - n - k = m - k - n := calc m - n - k = m - (n + k) : sub_sub m n k ... = m - (k + n) : {add_comm n k} ... = m - k - n : symm (sub_sub m k n) theorem succ_sub_one (n : ℕ) : succ n - 1 = n := sub_succ_succ n 0 ⬝ sub_zero_right n ---------- mul theorem mul_pred_left (n m : ℕ) : pred n * m = n * m - m := induction_on n (calc pred 0 * m = 0 * m : {pred_zero} ... = 0 : mul_zero_left _ ... = 0 - m : symm (sub_zero_left m) ... = 0 * m - m : {symm (mul_zero_left m)}) (take k : ℕ, assume IH : pred k * m = k * m - m, calc pred (succ k) * m = k * m : {pred_succ k} ... = k * m + m - m : symm (sub_add_left _ _) ... = succ k * m - m : {symm (mul_succ_left k m)}) theorem mul_pred_right (n m : ℕ) : n * pred m = n * m - n := calc n * pred m = pred m * n : mul_comm _ _ ... = m * n - n : mul_pred_left m n ... = n * m - n : {mul_comm m n} theorem mul_sub_distr_left (n m k : ℕ) : (n - m) * k = n * k - m * k := induction_on m (calc (n - 0) * k = n * k : {sub_zero_right n} ... = n * k - 0 : symm (sub_zero_right _) ... = n * k - 0 * k : {symm (mul_zero_left _)}) (take l : ℕ, assume IH : (n - l) * k = n * k - l * k, calc (n - succ l) * k = pred (n - l) * k : {sub_succ_right n l} ... = (n - l) * k - k : mul_pred_left _ _ ... = n * k - l * k - k : {IH} ... = n * k - (l * k + k) : sub_sub _ _ _ ... = n * k - (succ l * k) : {symm (mul_succ_left l k)}) theorem mul_sub_distr_right (n m k : ℕ) : n * (m - k) = n * m - n * k := calc n * (m - k) = (m - k) * n : mul_comm _ _ ... = m * n - k * n : mul_sub_distr_left _ _ _ ... = n * m - k * n : {mul_comm _ _} ... = n * m - n * k : {mul_comm _ _} -------------------------------------------------- max, min, iteration, maybe: sub, div theorem succ_sub {m n : ℕ} : m ≥ n → succ m - n = succ (m - n) := sub_induction n m (take k, assume H : 0 ≤ k, calc succ k - 0 = succ k : sub_zero_right (succ k) ... = succ (k - 0) : {symm (sub_zero_right k)}) (take k, assume H : succ k ≤ 0, absurd H (not_succ_zero_le k)) (take k l, assume IH : k ≤ l → succ l - k = succ (l - k), take H : succ k ≤ succ l, calc succ (succ l) - succ k = succ l - k : sub_succ_succ (succ l) k ... = succ (l - k) : IH (succ_le_cancel H) ... = succ (succ l - succ k) : {symm (sub_succ_succ l k)}) theorem le_imp_sub_eq_zero {n m : ℕ} (H : n ≤ m) : n - m = 0 := obtain (k : ℕ) (Hk : n + k = m), from le_elim H, subst Hk (sub_add_right_eq_zero n k) theorem add_sub_le {n m : ℕ} : n ≤ m → n + (m - n) = m := sub_induction n m (take k, assume H : 0 ≤ k, calc 0 + (k - 0) = k - 0 : zero_add (k - 0) ... = k : sub_zero_right k) (take k, assume H : succ k ≤ 0, absurd H (not_succ_zero_le k)) (take k l, assume IH : k ≤ l → k + (l - k) = l, take H : succ k ≤ succ l, calc succ k + (succ l - succ k) = succ k + (l - k) : {sub_succ_succ l k} ... = succ (k + (l - k)) : succ_add k (l - k) ... = succ l : {IH (succ_le_cancel H)}) theorem add_sub_ge_left {n m : ℕ} : n ≥ m → n - m + m = n := subst (add_comm m (n - m)) add_sub_le theorem add_sub_ge {n m : ℕ} (H : n ≥ m) : n + (m - n) = n := calc n + (m - n) = n + 0 : {le_imp_sub_eq_zero H} ... = n : add_zero n theorem add_sub_le_left {n m : ℕ} : n ≤ m → n - m + m = m := subst (add_comm m (n - m)) add_sub_ge theorem le_add_sub_left (n m : ℕ) : n ≤ n + (m - n) := or.elim (le_total n m) (assume H : n ≤ m, subst (symm (add_sub_le H)) H) (assume H : m ≤ n, subst (symm (add_sub_ge H)) (le_refl n)) theorem le_add_sub_right (n m : ℕ) : m ≤ n + (m - n) := or.elim (le_total n m) (assume H : n ≤ m, subst (symm (add_sub_le H)) (le_refl m)) (assume H : m ≤ n, subst (symm (add_sub_ge H)) H) theorem sub_split {P : ℕ → Prop} {n m : ℕ} (H1 : n ≤ m → P 0) (H2 : ∀k, m + k = n -> P k) : P (n - m) := or.elim (le_total n m) (assume H3 : n ≤ m, subst (symm (le_imp_sub_eq_zero H3)) (H1 H3)) (assume H3 : m ≤ n, H2 (n - m) (add_sub_le H3)) theorem sub_le_self (n m : ℕ) : n - m ≤ n := sub_split (assume H : n ≤ m, zero_le n) (take k : ℕ, assume H : m + k = n, le_intro (subst (add_comm m k) H)) theorem le_elim_sub (n m : ℕ) (H : n ≤ m) : ∃k, m - k = n := obtain (k : ℕ) (Hk : n + k = m), from le_elim H, exists.intro k (calc m - k = n + k - k : {symm Hk} ... = n : sub_add_left n k) theorem add_sub_assoc {m k : ℕ} (H : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) := have l1 : k ≤ m → n + m - k = n + (m - k), from sub_induction k m (take m : ℕ, assume H : 0 ≤ m, calc n + m - 0 = n + m : sub_zero_right (n + m) ... = n + (m - 0) : {symm (sub_zero_right m)}) (take k : ℕ, assume H : succ k ≤ 0, absurd H (not_succ_zero_le k)) (take k m, assume IH : k ≤ m → n + m - k = n + (m - k), take H : succ k ≤ succ m, calc n + succ m - succ k = succ (n + m) - succ k : {add_succ n m} ... = n + m - k : sub_succ_succ (n + m) k ... = n + (m - k) : IH (succ_le_cancel H) ... = n + (succ m - succ k) : {symm (sub_succ_succ m k)}), l1 H theorem sub_eq_zero_imp_le {n m : ℕ} : n - m = 0 → n ≤ m := sub_split (assume H1 : n ≤ m, assume H2 : 0 = 0, H1) (take k : ℕ, assume H1 : m + k = n, assume H2 : k = 0, have H3 : n = m, from subst (add_zero m) (subst H2 (symm H1)), subst H3 (le_refl n)) theorem sub_sub_split {P : ℕ → ℕ → Prop} {n m : ℕ} (H1 : ∀k, n = m + k -> P k 0) (H2 : ∀k, m = n + k → P 0 k) : P (n - m) (m - n) := or.elim (le_total n m) (assume H3 : n ≤ m, (le_imp_sub_eq_zero H3)⁻¹ ▸ (H2 (m - n) ((add_sub_le H3)⁻¹))) (assume H3 : m ≤ n, (le_imp_sub_eq_zero H3)⁻¹ ▸ (H1 (n - m) ((add_sub_le H3)⁻¹))) theorem sub_intro {n m k : ℕ} (H : n + m = k) : k - n = m := have H2 : k - n + n = m + n, from calc k - n + n = k : add_sub_ge_left (le_intro H) ... = n + m : symm H ... = m + n : add_comm n m, add_cancel_right H2 theorem sub_lt {x y : ℕ} (xpos : x > 0) (ypos : y > 0) : x - y < x := obtain (x' : ℕ) (xeq : x = succ x'), from pos_imp_eq_succ xpos, obtain (y' : ℕ) (yeq : y = succ y'), from pos_imp_eq_succ ypos, have xsuby_eq : x - y = x' - y', from calc x - y = succ x' - y : {xeq} ... = succ x' - succ y' : {yeq} ... = x' - y' : sub_succ_succ _ _, have H1 : x' - y' ≤ x', from sub_le_self _ _, have H2 : x' < succ x', from self_lt_succ _, show x - y < x, from xeq⁻¹ ▸ xsuby_eq⁻¹ ▸ le_lt_trans H1 H2 -- Max, min, iteration, and absolute difference -- -------------------------------------------- definition max (n m : ℕ) : ℕ := n + (m - n) definition min (n m : ℕ) : ℕ := m - (m - n) theorem max_le {n m : ℕ} (H : n ≤ m) : n + (m - n) = m := add_sub_le H theorem max_ge {n m : ℕ} (H : n ≥ m) : n + (m - n) = n := add_sub_ge H theorem left_le_max (n m : ℕ) : n ≤ n + (m - n) := le_add_sub_left n m theorem right_le_max (n m : ℕ) : m ≤ max n m := le_add_sub_right n m -- ### absolute difference -- This section is still incomplete definition dist (n m : ℕ) := (n - m) + (m - n) theorem dist_comm (n m : ℕ) : dist n m = dist m n := add_comm (n - m) (m - n) theorem dist_eq_zero {n m : ℕ} (H : dist n m = 0) : n = m := have H2 : n - m = 0, from eq_zero_of_add_eq_zero_right H, have H3 : n ≤ m, from sub_eq_zero_imp_le H2, have H4 : m - n = 0, from add_eq_zero_right H, have H5 : m ≤ n, from sub_eq_zero_imp_le H4, le_antisym H3 H5 theorem dist_le {n m : ℕ} (H : n ≤ m) : dist n m = m - n := calc dist n m = (n - m) + (m - n) : eq.refl _ ... = 0 + (m - n) : {le_imp_sub_eq_zero H} ... = m - n : zero_add (m - n) theorem dist_ge {n m : ℕ} (H : n ≥ m) : dist n m = n - m := subst (dist_comm m n) (dist_le H) theorem dist_zero_right (n : ℕ) : dist n 0 = n := trans (dist_ge (zero_le n)) (sub_zero_right n) theorem dist_zero_left (n : ℕ) : dist 0 n = n := trans (dist_le (zero_le n)) (sub_zero_right n) theorem dist_intro {n m k : ℕ} (H : n + m = k) : dist k n = m := calc dist k n = k - n : dist_ge (le_intro H) ... = m : sub_intro H theorem dist_add_right (n k m : ℕ) : dist (n + k) (m + k) = dist n m := calc dist (n + k) (m + k) = ((n+k) - (m+k)) + ((m+k)-(n+k)) : eq.refl _ ... = (n - m) + ((m + k) - (n + k)) : {sub_add_add_right _ _ _} ... = (n - m) + (m - n) : {sub_add_add_right _ _ _} theorem dist_add_left (k n m : ℕ) : dist (k + n) (k + m) = dist n m := subst (add_comm m k) (subst (add_comm n k) (dist_add_right n k m)) theorem dist_ge_add_right {n m : ℕ} (H : n ≥ m) : dist n m + m = n := calc dist n m + m = n - m + m : {dist_ge H} ... = n : add_sub_ge_left H theorem dist_eq_intro {n m k l : ℕ} (H : n + m = k + l) : dist n k = dist l m := calc dist n k = dist (n + m) (k + m) : symm (dist_add_right n m k) ... = dist (k + l) (k + m) : {H} ... = dist l m : dist_add_left k l m end nat end experiment
7118c21eaf881a12585bb1d902fbcb51ece5863e
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/int_eq_num.lean
60acc024961e98414b7d9392fb4edb1e876b5751
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
122
lean
def f : int → nat | -100 := 0 | 0 := 1 | 3 := 2 | _ := 4 vm_eval f (-100) vm_eval f 0 vm_eval f 3 vm_eval f 5
66130b875b9bb3737b6412f8b2412a850931317a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/polynomial/quotient.lean
c476511b7c12261a79c3d97440e31bbd02c2edd9
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
10,665
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, David Kurniadi Angdinata, Devon Tuma, Riccardo Brasca -/ import data.polynomial.div import ring_theory.polynomial.basic import ring_theory.ideal.quotient_operations /-! # Quotients of polynomial rings > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ open_locale polynomial namespace polynomial variables {R : Type*} [comm_ring R] /-- For a commutative ring $R$, evaluating a polynomial at an element $x \in R$ induces an isomorphism of $R$-algebras $R[X] / \langle X - x \rangle \cong R$. -/ noncomputable def quotient_span_X_sub_C_alg_equiv (x : R) : (R[X] ⧸ ideal.span ({X - C x} : set R[X])) ≃ₐ[R] R := (ideal.quotient_equiv_alg_of_eq R (by exact ker_eval_ring_hom x : ring_hom.ker (aeval x).to_ring_hom = _)).symm.trans $ ideal.quotient_ker_alg_equiv_of_right_inverse $ λ _, eval_C @[simp] lemma quotient_span_X_sub_C_alg_equiv_mk (x : R) (p : R[X]) : quotient_span_X_sub_C_alg_equiv x (ideal.quotient.mk _ p) = p.eval x := rfl @[simp] lemma quotient_span_X_sub_C_alg_equiv_symm_apply (x : R) (y : R) : (quotient_span_X_sub_C_alg_equiv x).symm y = algebra_map R _ y := rfl /-- For a commutative ring $R$, evaluating a polynomial at an element $y \in R$ induces an isomorphism of $R$-algebras $R[X] / \langle x, X - y \rangle \cong R / \langle x \rangle$. -/ noncomputable def quotient_span_C_X_sub_C_alg_equiv (x y : R) : (R[X] ⧸ (ideal.span {C x, X - C y} : ideal R[X])) ≃ₐ[R] R ⧸ (ideal.span {x} : ideal R) := (ideal.quotient_equiv_alg_of_eq R $ by rw [ideal.span_insert, sup_comm]).trans $ (double_quot.quot_quot_equiv_quot_supₐ R _ _).symm.trans $ (ideal.quotient_equiv_alg _ _ (quotient_span_X_sub_C_alg_equiv y) rfl).trans $ ideal.quotient_equiv_alg_of_eq R $ by { simp only [ideal.map_span, set.image_singleton], congr' 2, exact eval_C } end polynomial namespace ideal noncomputable theory open polynomial variables {R : Type*} [comm_ring R] lemma quotient_map_C_eq_zero {I : ideal R} : ∀ a ∈ I, ((quotient.mk (map (C : R →+* R[X]) I : ideal R[X])).comp C) a = 0 := begin intros a ha, rw [ring_hom.comp_apply, quotient.eq_zero_iff_mem], exact mem_map_of_mem _ ha, end lemma eval₂_C_mk_eq_zero {I : ideal R} : ∀ f ∈ (map (C : R →+* R[X]) I : ideal R[X]), eval₂_ring_hom (C.comp (quotient.mk I)) X f = 0 := begin intros a ha, rw ← sum_monomial_eq a, dsimp, rw eval₂_sum, refine finset.sum_eq_zero (λ n hn, _), dsimp, rw eval₂_monomial (C.comp (quotient.mk I)) X, refine mul_eq_zero_of_left (polynomial.ext (λ m, _)) (X ^ n), erw coeff_C, by_cases h : m = 0, { simpa [h] using quotient.eq_zero_iff_mem.2 ((mem_map_C_iff.1 ha) n) }, { simp [h] } end /-- If `I` is an ideal of `R`, then the ring polynomials over the quotient ring `I.quotient` is isomorphic to the quotient of `R[X]` by the ideal `map C I`, where `map C I` contains exactly the polynomials whose coefficients all lie in `I` -/ def polynomial_quotient_equiv_quotient_polynomial (I : ideal R) : (R ⧸ I)[X] ≃+* R[X] ⧸ (map C I : ideal R[X]) := { to_fun := eval₂_ring_hom (quotient.lift I ((quotient.mk (map C I : ideal R[X])).comp C) quotient_map_C_eq_zero) ((quotient.mk (map C I : ideal R[X]) X)), inv_fun := quotient.lift (map C I : ideal R[X]) (eval₂_ring_hom (C.comp (quotient.mk I)) X) eval₂_C_mk_eq_zero, map_mul' := λ f g, by simp only [coe_eval₂_ring_hom, eval₂_mul], map_add' := λ f g, by simp only [eval₂_add, coe_eval₂_ring_hom], left_inv := begin intro f, apply polynomial.induction_on' f, { intros p q hp hq, simp only [coe_eval₂_ring_hom] at hp, simp only [coe_eval₂_ring_hom] at hq, simp only [coe_eval₂_ring_hom, hp, hq, ring_hom.map_add] }, { rintros n ⟨x⟩, simp only [← smul_X_eq_monomial, C_mul', quotient.lift_mk, submodule.quotient.quot_mk_eq_mk, quotient.mk_eq_mk, eval₂_X_pow, eval₂_smul, coe_eval₂_ring_hom, ring_hom.map_pow, eval₂_C, ring_hom.coe_comp, ring_hom.map_mul, eval₂_X] } end, right_inv := begin rintro ⟨f⟩, apply polynomial.induction_on' f, { simp_intros p q hp hq, rw [hp, hq] }, { intros n a, simp only [← smul_X_eq_monomial, ← C_mul' a (X ^ n), quotient.lift_mk, submodule.quotient.quot_mk_eq_mk, quotient.mk_eq_mk, eval₂_X_pow, eval₂_smul, coe_eval₂_ring_hom, ring_hom.map_pow, eval₂_C, ring_hom.coe_comp, ring_hom.map_mul, eval₂_X] }, end, } @[simp] lemma polynomial_quotient_equiv_quotient_polynomial_symm_mk (I : ideal R) (f : R[X]) : I.polynomial_quotient_equiv_quotient_polynomial.symm (quotient.mk _ f) = f.map (quotient.mk I) := by rw [polynomial_quotient_equiv_quotient_polynomial, ring_equiv.symm_mk, ring_equiv.coe_mk, ideal.quotient.lift_mk, coe_eval₂_ring_hom, eval₂_eq_eval_map, ←polynomial.map_map, ←eval₂_eq_eval_map, polynomial.eval₂_C_X] @[simp] lemma polynomial_quotient_equiv_quotient_polynomial_map_mk (I : ideal R) (f : R[X]) : I.polynomial_quotient_equiv_quotient_polynomial (f.map I^.quotient.mk) = quotient.mk _ f := begin apply (polynomial_quotient_equiv_quotient_polynomial I).symm.injective, rw [ring_equiv.symm_apply_apply, polynomial_quotient_equiv_quotient_polynomial_symm_mk], end /-- If `P` is a prime ideal of `R`, then `R[x]/(P)` is an integral domain. -/ lemma is_domain_map_C_quotient {P : ideal R} (H : is_prime P) : is_domain (R[X] ⧸ (map (C : R →+* R[X]) P : ideal R[X])) := ring_equiv.is_domain (polynomial (R ⧸ P)) (polynomial_quotient_equiv_quotient_polynomial P).symm /-- Given any ring `R` and an ideal `I` of `R[X]`, we get a map `R → R[x] → R[x]/I`. If we let `R` be the image of `R` in `R[x]/I` then we also have a map `R[x] → R'[x]`. In particular we can map `I` across this map, to get `I'` and a new map `R' → R'[x] → R'[x]/I`. This theorem shows `I'` will not contain any non-zero constant polynomials -/ lemma eq_zero_of_polynomial_mem_map_range (I : ideal R[X]) (x : ((quotient.mk I).comp C).range) (hx : C x ∈ (I.map (polynomial.map_ring_hom ((quotient.mk I).comp C).range_restrict))) : x = 0 := begin let i := ((quotient.mk I).comp C).range_restrict, have hi' : (polynomial.map_ring_hom i).ker ≤ I, { refine λ f hf, polynomial_mem_ideal_of_coeff_mem_ideal I f (λ n, _), rw [mem_comap, ← quotient.eq_zero_iff_mem, ← ring_hom.comp_apply], rw [ring_hom.mem_ker, coe_map_ring_hom] at hf, replace hf := congr_arg (λ (f : polynomial _), f.coeff n) hf, simp only [coeff_map, coeff_zero] at hf, rwa [subtype.ext_iff, ring_hom.coe_range_restrict] at hf }, obtain ⟨x, hx'⟩ := x, obtain ⟨y, rfl⟩ := (ring_hom.mem_range).1 hx', refine subtype.eq _, simp only [ring_hom.comp_apply, quotient.eq_zero_iff_mem, zero_mem_class.coe_zero, subtype.val_eq_coe], suffices : C (i y) ∈ (I.map (polynomial.map_ring_hom i)), { obtain ⟨f, hf⟩ := mem_image_of_mem_map_of_surjective (polynomial.map_ring_hom i) (polynomial.map_surjective _ (((quotient.mk I).comp C).range_restrict_surjective)) this, refine sub_add_cancel (C y) f ▸ I.add_mem (hi' _ : (C y - f) ∈ I) hf.1, rw [ring_hom.mem_ker, ring_hom.map_sub, hf.2, sub_eq_zero, coe_map_ring_hom, map_C] }, exact hx, end end ideal namespace mv_polynomial variables {R : Type*} {σ : Type*} [comm_ring R] {r : R} lemma quotient_map_C_eq_zero {I : ideal R} {i : R} (hi : i ∈ I) : (ideal.quotient.mk (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R))).comp C i = 0 := begin simp only [function.comp_app, ring_hom.coe_comp, ideal.quotient.eq_zero_iff_mem], exact ideal.mem_map_of_mem _ hi end lemma eval₂_C_mk_eq_zero {I : ideal R} {a : mv_polynomial σ R} (ha : a ∈ (ideal.map (C : R →+* mv_polynomial σ R) I : ideal (mv_polynomial σ R))) : eval₂_hom (C.comp (ideal.quotient.mk I)) X a = 0 := begin rw as_sum a, rw [coe_eval₂_hom, eval₂_sum], refine finset.sum_eq_zero (λ n hn, _), simp only [eval₂_monomial, function.comp_app, ring_hom.coe_comp], refine mul_eq_zero_of_left _ _, suffices : coeff n a ∈ I, { rw [← @ideal.mk_ker R _ I, ring_hom.mem_ker] at this, simp only [this, C_0] }, exact mem_map_C_iff.1 ha n end /-- If `I` is an ideal of `R`, then the ring `mv_polynomial σ I.quotient` is isomorphic as an `R`-algebra to the quotient of `mv_polynomial σ R` by the ideal generated by `I`. -/ def quotient_equiv_quotient_mv_polynomial (I : ideal R) : mv_polynomial σ (R ⧸ I) ≃ₐ[R] mv_polynomial σ R ⧸ (ideal.map C I : ideal (mv_polynomial σ R)) := { to_fun := eval₂_hom (ideal.quotient.lift I ((ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R))).comp C) (λ i hi, quotient_map_C_eq_zero hi)) (λ i, ideal.quotient.mk (ideal.map C I : ideal (mv_polynomial σ R)) (X i)), inv_fun := ideal.quotient.lift (ideal.map C I : ideal (mv_polynomial σ R)) (eval₂_hom (C.comp (ideal.quotient.mk I)) X) (λ a ha, eval₂_C_mk_eq_zero ha), map_mul' := ring_hom.map_mul _, map_add' := ring_hom.map_add _, left_inv := begin intro f, apply induction_on f, { rintro ⟨r⟩, rw [coe_eval₂_hom, eval₂_C], simp only [submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, mv_polynomial.eval₂_hom_C, function.comp_app, ideal.quotient.mk_eq_mk, mv_polynomial.C_inj, ring_hom.coe_comp], }, { simp_intros p q hp hq only [ring_hom.map_add, mv_polynomial.coe_eval₂_hom, coe_eval₂_hom, mv_polynomial.eval₂_add], rw [hp, hq] }, { simp_intros p i hp only [coe_eval₂_hom], simp only [hp, coe_eval₂_hom, ideal.quotient.lift_mk, eval₂_mul, ring_hom.map_mul, eval₂_X] } end, right_inv := begin rintro ⟨f⟩, apply induction_on f, { intros r, simp only [submodule.quotient.quot_mk_eq_mk, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, ring_hom.coe_comp, eval₂_hom_C] }, { simp_intros p q hp hq only [submodule.quotient.quot_mk_eq_mk, eval₂_add, ring_hom.map_add, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk], rw [hp, hq] }, { simp_intros p i hp only [submodule.quotient.quot_mk_eq_mk, coe_eval₂_hom, ideal.quotient.lift_mk, ideal.quotient.mk_eq_mk, eval₂_mul, ring_hom.map_mul, eval₂_X], simp only [hp] } end, commutes' := λ r, eval₂_hom_C _ _ (ideal.quotient.mk I r) } end mv_polynomial
ccc63900b9fadb194aa934878aa4fd690cf8c05e
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/geometry/manifold/manifold.lean
8215986d2fa0d5bccf09ecb4d701040ca9c13ed3
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
27,206
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.local_homeomorph /-! # Manifolds A manifold is a topological space M locally modelled on a model space H, i.e., the manifold is covered by open subsets on which there are local homeomorphisms (the charts) going to H. If the changes of charts satisfy some additional property (for instance if they are smooth), then M inherits additional structure (it makes sense to talk about smooth manifolds). There are therefore two different ingredients in a manifold: * the set of charts, which is data * the fact that changes of charts belong to some group (in fact groupoid), which is additional Prop. We separate these two parts in the definition: the manifold structure is just the set of charts, and then the different smoothness requirements (smooth manifold, orientable manifold, contact manifold, and so on) are additional properties of these charts. These properties are formalized through the notion of structure groupoid, i.e., a set of local homeomorphisms stable under composition and inverse, to which the change of coordinates should belong. ## Main definitions * `structure_groupoid H` : a subset of local homeomorphisms of `H` stable under composition, inverse and restriction (ex: local diffeos) * `pregroupoid H` : a subset of local homeomorphisms of `H` stable under composition and restriction, but not inverse (ex: smooth maps) * `groupoid_of_pregroupoid`: construct a groupoid from a pregroupoid, by requiring that a map and its inverse both belong to the pregroupoid (ex: construct diffeos from smooth maps) * `continuous_groupoid H` : the groupoid of all local homeomorphisms of `H` * `manifold H M` : manifold structure on `M` modelled on `H`, given by an atlas of local homeomorphisms from `M` to `H` whose sources cover `M`. This is a type class. * `has_groupoid M G` : when `G` is a structure groupoid on `H` and `M` is a manifold modelled on `H`, require that all coordinate changes belong to `G`. This is a type class * `atlas H M` : when `M` is a manifold modelled on `H`, the atlas of this manifold structure, i.e., the set of charts * `structomorph G M M'` : the set of diffeomorphisms between the manifolds `M` and `M'` for the groupoid `G`. We avoid the word diffeomorphisms, keeping it for the smooth category. As a basic example, we give the instance `instance manifold_model_space (H : Type*) [topological_space H] : manifold H H` saying that a topological space is a manifold over itself, with the identity as unique chart. This manifold structure is compatible with any groupoid. ## Implementation notes The atlas in a manifold is *not* a maximal atlas in general: the notion of maximality depends on the groupoid one considers, and changing groupoids changes the maximal atlas. With the current formalization, it makes sense first to choose the atlas, and then to ask whether this precise atlas defines a smooth manifold, an orientable manifold, and so on. A consequence is that structomorphisms between M and M' do *not* induce a bijection between the atlases of M and M': the definition is only that, read in charts, the structomorphism locally belongs to the groupoid under consideration. (This is equivalent to inducing a bijection between elements of the maximal atlas). A consequence is that the invariance under structomorphisms of properties defined in terms of the atlas is not obvious in general, and could require some work in theory (amounting to the fact that these properties only depend on the maximal atlas, for instance). In practice, this does not create any real difficulty. We use the letter `H` for the model space thinking of the case of manifolds with boundary, where the model space is a half space. Manifolds are sometimes defined as topological spaces with an atlas of local diffeomorphisms, and sometimes as spaces with an atlas from which a topology is deduced. We use the former approach: otherwise, there would be an instance from manifolds to topological spaces, which means that any instance search for topological spaces would try to find manifold structures involving a yet unknown model space, leading to problems. However, we also introduce the latter approach, through a structure `manifold_core` making it possible to construct a topology out of a set of local equivs with compatibility conditions (but we do not register it as an instance). In the definition of a manifold, the model space is written as an explicit parameter as there can be several model spaces for a given topological space. For instance, a complex manifold (modelled over ℂ^n) will also be seen sometimes as a real manifold modelled over ℝ^(2n). -/ noncomputable theory local attribute [instance, priority 0] classical.decidable_inhabited classical.prop_decidable universes u variables {H : Type u} {M : Type*} {M' : Type*} {M'' : Type*} /- Notational shortcut for the composition of local homeomorphisms, i.e., `local_homeomorph.trans`. Note that, as is usual for equivs, the composition is from left to right, hence the direction of the arrow. -/ local infixr ` ≫ₕ `:100 := local_homeomorph.trans open set local_homeomorph section groupoid /- One could add to the definition of a structure groupoid the fact that the restriction of an element of the groupoid to any open set still belongs to the groupoid. (This is in Kobayashi-Nomizu.) I am not sure I want this, for instance on H × E where E is a vector space, and the groupoid is made of functions respecting the fibers and linear in the fibers (so that a manifold over this groupoid is naturally a vector bundle) I prefer that the members of the groupoid are always defined on sets of the form s × E The only nontrivial requirement is locality: if a local homeomorphism belongs to the groupoid around each point in its domain of definition, then it belongs to the groupoid. Without this requirement, the composition of diffeomorphisms does not have to be a diffeomorphism. Note that this implies that a local homeomorphism with empty source belongs to any structure groupoid, as it trivially satisfies this condition. There is also a technical point, related to the fact that a local homeomorphism is by definition a global map which is a homeomorphism when restricted to its source subset (and its values outside of the source are not relevant). Therefore, we also require that being a member of the groupoid only depends on the values on the source. -/ /-- A structure groupoid is a set of local homeomorphisms of a topological space stable under composition and inverse. They appear in the definition of the smoothness class of a manifold. -/ structure structure_groupoid (H : Type u) [topological_space H] := (members : set (local_homeomorph H H)) (comp : ∀e e' : local_homeomorph H H, e ∈ members → e' ∈ members → e ≫ₕ e' ∈ members) (inv : ∀e : local_homeomorph H H, e ∈ members → e.symm ∈ members) (id_mem : local_homeomorph.refl H ∈ members) (locality : ∀e : local_homeomorph H H, (∀x ∈ e.source, ∃s, is_open s ∧ x ∈ s ∧ e.restr s ∈ members) → e ∈ members) (eq_on_source : ∀ e e' : local_homeomorph H H, e ∈ members → e' ≈ e → e' ∈ members) variable [topological_space H] @[reducible] instance : has_mem (local_homeomorph H H) (structure_groupoid H) := ⟨λ(e : local_homeomorph H H) (G : structure_groupoid H), e ∈ G.members⟩ /-- Partial order on the set of groupoids, given by inclusion of the members of the groupoid -/ instance structure_groupoid.partial_order : partial_order (structure_groupoid H) := partial_order.lift structure_groupoid.members (λa b h, by { cases a, cases b, dsimp at h, induction h, refl }) (by apply_instance) /-- The trivial groupoid, containing only the identity (and maps with empty source, as this is necessary from the definition) -/ def id_groupoid (H : Type u) [topological_space H] : structure_groupoid H := { members := {local_homeomorph.refl H} ∪ {e : local_homeomorph H H | e.source = ∅}, comp := λe e' he he', begin cases he; simp at he he', { simpa [he] }, { have : (e ≫ₕ e').source ⊆ e.source := sep_subset _ _, rw he at this, have : (e ≫ₕ e') ∈ {e : local_homeomorph H H | e.source = ∅} := disjoint_iff.1 this, exact (mem_union _ _ _).2 (or.inr this) }, end, inv := λe he, begin cases (mem_union _ _ _).1 he with E E, { finish }, { right, simpa [e.to_local_equiv.image_source_eq_target.symm] using E }, end, id_mem := mem_union_left _ (mem_insert _ ∅), locality := λe he, begin cases e.source.eq_empty_or_nonempty with h h, { right, exact h }, { left, rcases h with ⟨x, hx⟩, rcases he x hx with ⟨s, open_s, xs, hs⟩, have x's : x ∈ (e.restr s).source, { rw [restr_source, interior_eq_of_open open_s], exact ⟨hx, xs⟩ }, cases hs, { replace hs : local_homeomorph.restr e s = local_homeomorph.refl H, by simpa using hs, have : (e.restr s).source = univ, by { rw hs, simp }, change (e.to_local_equiv).source ∩ interior s = univ at this, have : univ ⊆ interior s, by { rw ← this, exact inter_subset_right _ _ }, have : s = univ, by rwa [interior_eq_of_open open_s, univ_subset_iff] at this, simpa [this, restr_univ] using hs }, { exfalso, rw mem_set_of_eq at hs, rwa hs at x's } }, end, eq_on_source := λe e' he he'e, begin cases he, { left, have : e = e', { refine eq_of_eq_on_source_univ (setoid.symm he'e) _ _; rw set.mem_singleton_iff.1 he ; refl }, rwa ← this }, { right, change (e.to_local_equiv).source = ∅ at he, rwa [set.mem_set_of_eq, source_eq_of_eq_on_source he'e] } end } /-- Every structure groupoid contains the identity groupoid -/ instance : order_bot (structure_groupoid H) := { bot := id_groupoid H, bot_le := begin assume u f hf, change f ∈ {local_homeomorph.refl H} ∪ {e : local_homeomorph H H | e.source = ∅} at hf, simp only [singleton_union, mem_set_of_eq, mem_insert_iff] at hf, cases hf, { rw hf, apply u.id_mem }, { apply u.locality, assume x hx, rw [hf, mem_empty_eq] at hx, exact hx.elim } end, ..structure_groupoid.partial_order } instance (H : Type u) [topological_space H] : inhabited (structure_groupoid H) := ⟨id_groupoid H⟩ /-- To construct a groupoid, one may consider classes of local homeos such that both the function and its inverse have some property. If this property is stable under composition, one gets a groupoid. `pregroupoid` bundles the properties needed for this construction, with the groupoid of smooth functions with smooth inverses as an application. -/ structure pregroupoid (H : Type*) [topological_space H] := (property : (H → H) → (set H) → Prop) (comp : ∀{f g u v}, property f u → property g v → is_open u → is_open v → is_open (u ∩ f ⁻¹' v) → property (g ∘ f) (u ∩ f ⁻¹' v)) (id_mem : property id univ) (locality : ∀{f u}, is_open u → (∀x∈u, ∃v, is_open v ∧ x ∈ v ∧ property f (u ∩ v)) → property f u) (congr : ∀{f g : H → H} {u}, is_open u → (∀x∈u, g x = f x) → property f u → property g u) /-- Construct a groupoid of local homeos for which the map and its inverse have some property, from a pregroupoid asserting that this property is stable under composition. -/ def pregroupoid.groupoid (PG : pregroupoid H) : structure_groupoid H := { members := {e : local_homeomorph H H | PG.property e e.source ∧ PG.property e.symm e.target}, comp := λe e' he he', begin split, { apply PG.comp he.1 he'.1 e.open_source e'.open_source, apply e.continuous_to_fun.preimage_open_of_open e.open_source e'.open_source }, { apply PG.comp he'.2 he.2 e'.open_target e.open_target, apply e'.continuous_inv_fun.preimage_open_of_open e'.open_target e.open_target } end, inv := λe he, ⟨he.2, he.1⟩, id_mem := ⟨PG.id_mem, PG.id_mem⟩, locality := λe he, begin split, { apply PG.locality e.open_source (λx xu, _), rcases he x xu with ⟨s, s_open, xs, hs⟩, refine ⟨s, s_open, xs, _⟩, convert hs.1, exact (interior_eq_of_open s_open).symm }, { apply PG.locality e.open_target (λx xu, _), rcases he (e.symm x) (e.map_target xu) with ⟨s, s_open, xs, hs⟩, refine ⟨e.target ∩ e.symm ⁻¹' s, _, ⟨xu, xs⟩, _⟩, { exact continuous_on.preimage_open_of_open e.continuous_inv_fun e.open_target s_open }, { rw [← inter_assoc, inter_self], convert hs.2, exact (interior_eq_of_open s_open).symm } }, end, eq_on_source := λe e' he ee', begin split, { apply PG.congr e'.open_source ee'.2, simp only [ee'.1, he.1] }, { have A := eq_on_source_symm ee', apply PG.congr e'.symm.open_source A.2, convert he.2, rw A.1, refl } end } lemma mem_groupoid_of_pregroupoid (PG : pregroupoid H) (e : local_homeomorph H H) : e ∈ PG.groupoid ↔ PG.property e e.source ∧ PG.property e.symm e.target := iff.rfl lemma groupoid_of_pregroupoid_le (PG₁ PG₂ : pregroupoid H) (h : ∀f s, PG₁.property f s → PG₂.property f s) : PG₁.groupoid ≤ PG₂.groupoid := begin assume e he, rw mem_groupoid_of_pregroupoid at he ⊢, exact ⟨h _ _ he.1, h _ _ he.2⟩ end lemma mem_pregroupoid_of_eq_on_source (PG : pregroupoid H) {e e' : local_homeomorph H H} (he' : e ≈ e') (he : PG.property e e.source) : PG.property e' e'.source := begin rw ← he'.1, exact PG.congr e.open_source (λx hx, (he'.2 x hx).symm) he, end /-- The pregroupoid of all local maps on a topological space H -/ @[reducible] def continuous_pregroupoid (H : Type*) [topological_space H] : pregroupoid H := { property := λf s, true, comp := λf g u v hf hg hu hv huv, trivial, id_mem := trivial, locality := λf u u_open h, trivial, congr := λf g u u_open hcongr hf, trivial } instance (H : Type*) [topological_space H] : inhabited (pregroupoid H) := ⟨continuous_pregroupoid H⟩ /-- The groupoid of all local homeomorphisms on a topological space H -/ def continuous_groupoid (H : Type*) [topological_space H] : structure_groupoid H := pregroupoid.groupoid (continuous_pregroupoid H) /-- Every structure groupoid is contained in the groupoid of all local homeomorphisms -/ instance : order_top (structure_groupoid H) := { top := continuous_groupoid H, le_top := λ u f hf, by { split; exact dec_trivial }, ..structure_groupoid.partial_order } end groupoid /-- A manifold is a topological space endowed with an atlas, i.e., a set of local homeomorphisms taking value in a model space `H`, called charts, such that the domains of the charts cover the whole space. We express the covering property by chosing for each x a member `chart_at x` of the atlas containing `x` in its source: in the smooth case, this is convenient to construct the tangent bundle in an efficient way. The model space is written as an explicit parameter as there can be several model spaces for a given topological space. For instance, a complex manifold (modelled over `ℂ^n`) will also be seen sometimes as a real manifold over `ℝ^(2n)`. -/ class manifold (H : Type*) [topological_space H] (M : Type*) [topological_space M] := (atlas [] : set (local_homeomorph M H)) (chart_at [] : M → local_homeomorph M H) (mem_chart_source [] : ∀x, x ∈ (chart_at x).source) (chart_mem_atlas [] : ∀x, chart_at x ∈ atlas) export manifold attribute [simp] mem_chart_source chart_mem_atlas section manifold /-- Any space is a manifold modelled over itself, by just using the identity chart -/ instance manifold_model_space (H : Type*) [topological_space H] : manifold H H := { atlas := {local_homeomorph.refl H}, chart_at := λx, local_homeomorph.refl H, mem_chart_source := λx, mem_univ x, chart_mem_atlas := λx, mem_singleton _ } /-- In the trivial manifold structure of a space modelled over itself through the identity, the atlas members are just the identity -/ @[simp] lemma model_space_atlas {H : Type*} [topological_space H] {e : local_homeomorph H H} : e ∈ atlas H H ↔ e = local_homeomorph.refl H := by simp [atlas, manifold.atlas] /-- In the model space, chart_at is always the identity -/ @[simp] lemma chart_at_model_space_eq {H : Type*} [topological_space H] {x : H} : chart_at H x = local_homeomorph.refl H := by simpa using chart_mem_atlas H x end manifold /-- Sometimes, one may want to construct a manifold structure on a space which does not yet have a topological structure, where the topology would come from the charts. For this, one needs charts that are only local equivs, and continuity properties for their composition. This is formalised in `manifold_core`. -/ @[nolint has_inhabited_instance] structure manifold_core (H : Type*) [topological_space H] (M : Type*) := (atlas : set (local_equiv M H)) (chart_at : M → local_equiv M H) (mem_chart_source : ∀x, x ∈ (chart_at x).source) (chart_mem_atlas : ∀x, chart_at x ∈ atlas) (open_source : ∀e e' : local_equiv M H, e ∈ atlas → e' ∈ atlas → is_open (e.symm.trans e').source) (continuous_to_fun : ∀e e' : local_equiv M H, e ∈ atlas → e' ∈ atlas → continuous_on (e.symm.trans e') (e.symm.trans e').source) namespace manifold_core variables [topological_space H] (c : manifold_core H M) {e : local_equiv M H} /-- Topology generated by a set of charts on a Type. -/ protected def to_topological_space : topological_space M := topological_space.generate_from $ ⋃ (e : local_equiv M H) (he : e ∈ c.atlas) (s : set H) (s_open : is_open s), {e ⁻¹' s ∩ e.source} lemma open_source' (he : e ∈ c.atlas) : @is_open M c.to_topological_space e.source := begin apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], refine ⟨e, he, univ, is_open_univ, _⟩, simp only [set.univ_inter, set.preimage_univ] end lemma open_target (he : e ∈ c.atlas) : is_open e.target := begin have E : e.target ∩ e.symm ⁻¹' e.source = e.target := subset.antisymm (inter_subset_left _ _) (λx hx, ⟨hx, local_equiv.target_subset_preimage_source _ hx⟩), simpa [local_equiv.trans_source, E] using c.open_source e e he he end /-- An element of the atlas in a manifold without topology becomes a local homeomorphism for the topology constructed from this atlas. The `local_homeomorph` version is given in this definition. -/ def local_homeomorph (e : local_equiv M H) (he : e ∈ c.atlas) : @local_homeomorph M H c.to_topological_space _ := { open_source := by convert c.open_source' he, open_target := by convert c.open_target he, continuous_to_fun := begin letI : topological_space M := c.to_topological_space, rw continuous_on_open_iff (c.open_source' he), assume s s_open, rw inter_comm, apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], exact ⟨e, he, ⟨s, s_open, rfl⟩⟩ end, continuous_inv_fun := begin letI : topological_space M := c.to_topological_space, apply continuous_on_open_of_generate_from (c.open_target he), assume t ht, simp only [exists_prop, mem_Union, mem_singleton_iff] at ht, rcases ht with ⟨e', e'_atlas, s, s_open, ts⟩, rw ts, let f := e.symm.trans e', have : is_open (f ⁻¹' s ∩ f.source), by simpa [inter_comm] using (continuous_on_open_iff (c.open_source e e' he e'_atlas)).1 (c.continuous_to_fun e e' he e'_atlas) s s_open, have A : e' ∘ e.symm ⁻¹' s ∩ (e.target ∩ e.symm ⁻¹' e'.source) = e.target ∩ (e' ∘ e.symm ⁻¹' s ∩ e.symm ⁻¹' e'.source), by { rw [← inter_assoc, ← inter_assoc], congr' 1, exact inter_comm _ _ }, simpa [local_equiv.trans_source, preimage_inter, preimage_comp.symm, A] using this end, ..e } /-- Given a manifold without topology, endow it with a genuine manifold structure with respect to the topology constructed from the atlas. -/ def to_manifold : @manifold H _ M c.to_topological_space := { atlas := ⋃ (e : local_equiv M H) (he : e ∈ c.atlas), {c.local_homeomorph e he}, chart_at := λx, c.local_homeomorph (c.chart_at x) (c.chart_mem_atlas x), mem_chart_source := λx, c.mem_chart_source x, chart_mem_atlas := λx, begin simp only [mem_Union, mem_singleton_iff], exact ⟨c.chart_at x, c.chart_mem_atlas x, rfl⟩, end } end manifold_core section has_groupoid variables [topological_space H] [topological_space M] [manifold H M] /-- A manifold has an atlas in a groupoid G if the change of coordinates belong to the groupoid -/ class has_groupoid {H : Type*} [topological_space H] (M : Type*) [topological_space M] [manifold H M] (G : structure_groupoid H) : Prop := (compatible [] : ∀{e e' : local_homeomorph M H}, e ∈ atlas H M → e' ∈ atlas H M → e.symm ≫ₕ e' ∈ G) lemma has_groupoid_of_le {G₁ G₂ : structure_groupoid H} (h : has_groupoid M G₁) (hle : G₁ ≤ G₂) : has_groupoid M G₂ := ⟨ λ e e' he he', hle ((h.compatible : _) he he') ⟩ lemma has_groupoid_of_pregroupoid (PG : pregroupoid H) (h : ∀{e e' : local_homeomorph M H}, e ∈ atlas H M → e' ∈ atlas H M → PG.property (e.symm ≫ₕ e') (e.symm ≫ₕ e').source) : has_groupoid M (PG.groupoid) := ⟨assume e e' he he', (mem_groupoid_of_pregroupoid PG _).mpr ⟨h he he', h he' he⟩⟩ /-- The trivial manifold structure on the model space is compatible with any groupoid -/ instance has_groupoid_model_space (H : Type*) [topological_space H] (G : structure_groupoid H) : has_groupoid H G := { compatible := λe e' he he', begin replace he : e ∈ atlas H H := he, replace he' : e' ∈ atlas H H := he', rw model_space_atlas at he he', simp [he, he', structure_groupoid.id_mem] end } /-- Any manifold structure is compatible with the groupoid of all local homeomorphisms -/ instance has_groupoid_continuous_groupoid : has_groupoid M (continuous_groupoid H) := ⟨begin assume e e' he he', rw [continuous_groupoid, mem_groupoid_of_pregroupoid], simp only [and_self] end⟩ /-- A G-diffeomorphism between two manifolds is a homeomorphism which, when read in the charts, belongs to G. We avoid the word diffeomorph as it is too related to the smooth category, and use structomorph instead. -/ @[nolint has_inhabited_instance] structure structomorph (G : structure_groupoid H) (M : Type*) (M' : Type*) [topological_space M] [topological_space M'] [manifold H M] [manifold H M'] extends homeomorph M M' := (mem_groupoid : ∀c : local_homeomorph M H, ∀c' : local_homeomorph M' H, c ∈ atlas H M → c' ∈ atlas H M' → c.symm ≫ₕ to_homeomorph.to_local_homeomorph ≫ₕ c' ∈ G) variables [topological_space M'] [topological_space M''] {G : structure_groupoid H} [manifold H M'] [manifold H M''] /-- The identity is a diffeomorphism of any manifold, for any groupoid. -/ def structomorph.refl (M : Type*) [topological_space M] [manifold H M] [has_groupoid M G] : structomorph G M M := { mem_groupoid := λc c' hc hc', begin change (local_homeomorph.symm c) ≫ₕ (local_homeomorph.refl M) ≫ₕ c' ∈ G, rw local_homeomorph.refl_trans, exact has_groupoid.compatible G hc hc' end, ..homeomorph.refl M } /-- The inverse of a structomorphism is a structomorphism -/ def structomorph.symm (e : structomorph G M M') : structomorph G M' M := { mem_groupoid := begin assume c c' hc hc', have : (c'.symm ≫ₕ e.to_homeomorph.to_local_homeomorph ≫ₕ c).symm ∈ G := G.inv _ (e.mem_groupoid c' c hc' hc), simp at this, rwa [trans_symm_eq_symm_trans_symm, trans_symm_eq_symm_trans_symm, symm_symm, trans_assoc] at this, end, ..e.to_homeomorph.symm} /-- The composition of structomorphisms is a structomorphism -/ def structomorph.trans (e : structomorph G M M') (e' : structomorph G M' M'') : structomorph G M M'' := { mem_groupoid := begin /- Let c and c' be two charts in M and M''. We want to show that e' ∘ e is smooth in these charts, around any point x. For this, let y = e (c⁻¹ x), and consider a chart g around y. Then g ∘ e ∘ c⁻¹ and c' ∘ e' ∘ g⁻¹ are both smooth as e and e' are structomorphisms, so their composition is smooth, and it coincides with c' ∘ e' ∘ e ∘ c⁻¹ around x. -/ assume c c' hc hc', refine G.locality _ (λx hx, _), let f₁ := e.to_homeomorph.to_local_homeomorph, let f₂ := e'.to_homeomorph.to_local_homeomorph, let f := (e.to_homeomorph.trans e'.to_homeomorph).to_local_homeomorph, have feq : f = f₁ ≫ₕ f₂ := homeomorph.trans_to_local_homeomorph _ _, -- define the atlas g around y let y := (c.symm ≫ₕ f₁) x, let g := chart_at H y, have hg₁ := chart_mem_atlas H y, have hg₂ := mem_chart_source H y, let s := (c.symm ≫ₕ f₁).source ∩ (c.symm ≫ₕ f₁) ⁻¹' g.source, have open_s : is_open s, by apply (c.symm ≫ₕ f₁).continuous_to_fun.preimage_open_of_open; apply open_source, have : x ∈ s, { split, { simp only [trans_source, preimage_univ, inter_univ, homeomorph.to_local_homeomorph_source], rw trans_source at hx, exact hx.1 }, { exact hg₂ } }, refine ⟨s, open_s, ⟨this, _⟩⟩, let F₁ := (c.symm ≫ₕ f₁ ≫ₕ g) ≫ₕ (g.symm ≫ₕ f₂ ≫ₕ c'), have A : F₁ ∈ G := G.comp _ _ (e.mem_groupoid c g hc hg₁) (e'.mem_groupoid g c' hg₁ hc'), let F₂ := (c.symm ≫ₕ f ≫ₕ c').restr s, have : F₁ ≈ F₂ := calc F₁ ≈ c.symm ≫ₕ f₁ ≫ₕ (g ≫ₕ g.symm) ≫ₕ f₂ ≫ₕ c' : by simp [F₁, trans_assoc] ... ≈ c.symm ≫ₕ f₁ ≫ₕ (of_set g.source g.open_source) ≫ₕ f₂ ≫ₕ c' : by simp [eq_on_source_trans, trans_self_symm g] ... ≈ ((c.symm ≫ₕ f₁) ≫ₕ (of_set g.source g.open_source)) ≫ₕ (f₂ ≫ₕ c') : by simp [trans_assoc] ... ≈ ((c.symm ≫ₕ f₁).restr s) ≫ₕ (f₂ ≫ₕ c') : by simp [s, trans_of_set'] ... ≈ ((c.symm ≫ₕ f₁) ≫ₕ (f₂ ≫ₕ c')).restr s : by simp [restr_trans] ... ≈ (c.symm ≫ₕ (f₁ ≫ₕ f₂) ≫ₕ c').restr s : by simp [eq_on_source_restr, trans_assoc] ... ≈ F₂ : by simp [F₂, feq], have : F₂ ∈ G := G.eq_on_source F₁ F₂ A (setoid.symm this), exact this end, ..homeomorph.trans e.to_homeomorph e'.to_homeomorph } end has_groupoid
b5c60eb1633121b0edb5c9021a5dbe02c02d1597
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/category/Preorder_auto.lean
c3ea43ca3b9bf855589b98b672f9ac10ea23a95d
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,269
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.preorder_hom import Mathlib.category_theory.concrete_category.default import Mathlib.algebra.punit_instances import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Category of preorders -/ /-- The category of preorders. -/ def Preorder := category_theory.bundled preorder namespace Preorder protected instance preorder_hom.category_theory.bundled_hom : category_theory.bundled_hom preorder_hom := category_theory.bundled_hom.mk preorder_hom.to_fun preorder_hom.id preorder_hom.comp protected instance concrete_category : category_theory.concrete_category Preorder := category_theory.bundled_hom.category_theory.bundled.category_theory.concrete_category preorder_hom /-- Construct a bundled Preorder from the underlying type and typeclass. -/ def of (α : Type u_1) [preorder α] : Preorder := category_theory.bundled.of α protected instance inhabited : Inhabited Preorder := { default := of PUnit } protected instance preorder (α : Preorder) : preorder ↥α := category_theory.bundled.str α end Mathlib
e0deabea20b9c182be4a47adb54d0e2f4f515e9a
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/topology/instances/nnreal.lean
20e4f7237f14871c985497763bf3abaac08ae39d
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,363
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Nonnegative real numbers. -/ import topology.algebra.infinite_sum import topology.algebra.group_with_zero noncomputable theory open set topological_space metric open_locale topological_space namespace nnreal open_locale nnreal big_operators instance : topological_space ℝ≥0 := infer_instance -- short-circuit type class inference instance : topological_semiring ℝ≥0 := { continuous_mul := continuous_subtype_mk _ $ (continuous_subtype_val.comp continuous_fst).mul (continuous_subtype_val.comp continuous_snd), continuous_add := continuous_subtype_mk _ $ (continuous_subtype_val.comp continuous_fst).add (continuous_subtype_val.comp continuous_snd) } instance : second_countable_topology ℝ≥0 := topological_space.subtype.second_countable_topology _ _ instance : order_topology ℝ≥0 := @order_topology_of_ord_connected _ _ _ _ (Ici 0) _ section coe variable {α : Type*} open filter finset lemma continuous_of_real : continuous nnreal.of_real := continuous_subtype_mk _ $ continuous_id.max continuous_const lemma continuous_coe : continuous (coe : ℝ≥0 → ℝ) := continuous_subtype_val @[simp, norm_cast] lemma tendsto_coe {f : filter α} {m : α → ℝ≥0} {x : ℝ≥0} : tendsto (λa, (m a : ℝ)) f (𝓝 (x : ℝ)) ↔ tendsto m f (𝓝 x) := tendsto_subtype_rng.symm lemma tendsto_coe' {f : filter α} [ne_bot f] {m : α → ℝ≥0} {x : ℝ} : tendsto (λ a, m a : α → ℝ) f (𝓝 x) ↔ ∃ hx : 0 ≤ x, tendsto m f (𝓝 ⟨x, hx⟩) := ⟨λ h, ⟨ge_of_tendsto' h (λ c, (m c).2), tendsto_coe.1 h⟩, λ ⟨hx, hm⟩, tendsto_coe.2 hm⟩ @[simp] lemma map_coe_at_top : map (coe : ℝ≥0 → ℝ) at_top = at_top := map_coe_Ici_at_top 0 lemma comap_coe_at_top : comap (coe : ℝ≥0 → ℝ) at_top = at_top := (at_top_Ici_eq 0).symm @[simp, norm_cast] lemma tendsto_coe_at_top {f : filter α} {m : α → ℝ≥0} : tendsto (λ a, (m a : ℝ)) f at_top ↔ tendsto m f at_top := tendsto_Ici_at_top.symm lemma tendsto_of_real {f : filter α} {m : α → ℝ} {x : ℝ} (h : tendsto m f (𝓝 x)) : tendsto (λa, nnreal.of_real (m a)) f (𝓝 (nnreal.of_real x)) := (continuous_of_real.tendsto _).comp h instance : has_continuous_sub ℝ≥0 := ⟨continuous_subtype_mk _ $ ((continuous_coe.comp continuous_fst).sub (continuous_coe.comp continuous_snd)).max continuous_const⟩ instance : has_continuous_inv' ℝ≥0 := ⟨λ x hx, tendsto_coe.1 $ (real.tendsto_inv $ nnreal.coe_ne_zero.2 hx).comp continuous_coe.continuous_at⟩ @[norm_cast] lemma has_sum_coe {f : α → ℝ≥0} {r : ℝ≥0} : has_sum (λa, (f a : ℝ)) (r : ℝ) ↔ has_sum f r := by simp only [has_sum, coe_sum.symm, tendsto_coe] lemma has_sum_of_real_of_nonneg {f : α → ℝ} (hf_nonneg : ∀ n, 0 ≤ f n) (hf : summable f) : has_sum (λ n, nnreal.of_real (f n)) (nnreal.of_real (∑' n, f n)) := begin have h_sum : (λ s, ∑ b in s, nnreal.of_real (f b)) = λ s, nnreal.of_real (∑ b in s, f b), from funext (λ _, (of_real_sum_of_nonneg (λ n _, hf_nonneg n)).symm), simp_rw [has_sum, h_sum], exact tendsto_of_real hf.has_sum, end @[norm_cast] lemma summable_coe {f : α → ℝ≥0} : summable (λa, (f a : ℝ)) ↔ summable f := begin split, exact assume ⟨a, ha⟩, ⟨⟨a, has_sum_le (λa, (f a).2) has_sum_zero ha⟩, has_sum_coe.1 ha⟩, exact assume ⟨a, ha⟩, ⟨a.1, has_sum_coe.2 ha⟩ end open_locale classical @[norm_cast] lemma coe_tsum {f : α → ℝ≥0} : ↑∑'a, f a = ∑'a, (f a : ℝ) := if hf : summable f then (eq.symm $ (has_sum_coe.2 $ hf.has_sum).tsum_eq) else by simp [tsum, hf, mt summable_coe.1 hf] lemma tsum_mul_left (a : ℝ≥0) (f : α → ℝ≥0) : ∑' x, a * f x = a * ∑' x, f x := nnreal.eq $ by simp only [coe_tsum, nnreal.coe_mul, tsum_mul_left] lemma tsum_mul_right (f : α → ℝ≥0) (a : ℝ≥0) : (∑' x, f x * a) = (∑' x, f x) * a := nnreal.eq $ by simp only [coe_tsum, nnreal.coe_mul, tsum_mul_right] lemma summable_comp_injective {β : Type*} {f : α → ℝ≥0} (hf : summable f) {i : β → α} (hi : function.injective i) : summable (f ∘ i) := nnreal.summable_coe.1 $ show summable ((coe ∘ f) ∘ i), from (nnreal.summable_coe.2 hf).comp_injective hi lemma summable_nat_add (f : ℕ → ℝ≥0) (hf : summable f) (k : ℕ) : summable (λ i, f (i + k)) := summable_comp_injective hf $ add_left_injective k lemma summable_nat_add_iff {f : ℕ → ℝ≥0} (k : ℕ) : summable (λ i, f (i + k)) ↔ summable f := begin rw [← summable_coe, ← summable_coe], exact @summable_nat_add_iff ℝ _ _ _ (λ i, (f i : ℝ)) k, end lemma sum_add_tsum_nat_add {f : ℕ → ℝ≥0} (k : ℕ) (hf : summable f) : ∑' i, f i = (∑ i in range k, f i) + ∑' i, f (i + k) := by rw [←nnreal.coe_eq, coe_tsum, nnreal.coe_add, coe_sum, coe_tsum, sum_add_tsum_nat_add k (nnreal.summable_coe.2 hf)] lemma infi_real_pos_eq_infi_nnreal_pos [complete_lattice α] {f : ℝ → α} : (⨅ (n : ℝ) (h : 0 < n), f n) = (⨅ (n : ℝ≥0) (h : 0 < n), f n) := le_antisymm (infi_le_infi2 $ assume r, ⟨r, infi_le_infi $ assume hr, le_rfl⟩) (le_infi $ assume r, le_infi $ assume hr, infi_le_of_le ⟨r, hr.le⟩ $ infi_le _ hr) end coe end nnreal
6a3ab6f5d873c4e52f75e2a001aa8eaeaa914508
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/group_theory/perm/fin.lean
e6f940f11e3281e61a00378baf66fa879879e010
[ "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
10,755
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import data.equiv.fin import data.equiv.fintype import group_theory.perm.option import group_theory.perm.cycle_type /-! # Permutations of `fin n` -/ open equiv /-- Permutations of `fin (n + 1)` are equivalent to fixing a single `fin (n + 1)` and permuting the remaining with a `perm (fin n)`. The fixed `fin (n + 1)` is swapped with `0`. -/ def equiv.perm.decompose_fin {n : ℕ} : perm (fin n.succ) ≃ fin n.succ × perm (fin n) := ((equiv.perm_congr $ fin_succ_equiv n).trans equiv.perm.decompose_option).trans (equiv.prod_congr (fin_succ_equiv n).symm (equiv.refl _)) @[simp] lemma equiv.perm.decompose_fin_symm_of_refl {n : ℕ} (p : fin (n + 1)) : equiv.perm.decompose_fin.symm (p, equiv.refl _) = swap 0 p := by simp [equiv.perm.decompose_fin, equiv.perm_congr_def] @[simp] lemma equiv.perm.decompose_fin_symm_of_one {n : ℕ} (p : fin (n + 1)) : equiv.perm.decompose_fin.symm (p, 1) = swap 0 p := equiv.perm.decompose_fin_symm_of_refl p @[simp] lemma equiv.perm.decompose_fin_symm_apply_zero {n : ℕ} (p : fin (n + 1)) (e : perm (fin n)) : equiv.perm.decompose_fin.symm (p, e) 0 = p := by simp [equiv.perm.decompose_fin] @[simp] lemma equiv.perm.decompose_fin_symm_apply_succ {n : ℕ} (e : perm (fin n)) (p : fin (n + 1)) (x : fin n) : equiv.perm.decompose_fin.symm (p, e) x.succ = swap 0 p (e x).succ := begin refine fin.cases _ _ p, { simp [equiv.perm.decompose_fin, equiv_functor.map] }, { intros i, by_cases h : i = e x, { simp [h, equiv.perm.decompose_fin, equiv_functor.map] }, { have h' : some (e x) ≠ some i := λ H, h (option.some_injective _ H).symm, have h'' : (e x).succ ≠ i.succ := λ H, h (fin.succ_injective _ H).symm, simp [h, h'', fin.succ_ne_zero, equiv.perm.decompose_fin, equiv_functor.map, swap_apply_of_ne_of_ne, swap_apply_of_ne_of_ne (option.some_ne_none (e x)) h'] } } end @[simp] lemma equiv.perm.decompose_fin_symm_apply_one {n : ℕ} (e : perm (fin (n + 1))) (p : fin (n + 2)) : equiv.perm.decompose_fin.symm (p, e) 1 = swap 0 p (e 0).succ := by rw [← fin.succ_zero_eq_one, equiv.perm.decompose_fin_symm_apply_succ e p 0] @[simp] lemma equiv.perm.decompose_fin.symm_sign {n : ℕ} (p : fin (n + 1)) (e : perm (fin n)) : perm.sign (equiv.perm.decompose_fin.symm (p, e)) = ite (p = 0) 1 (-1) * perm.sign e := by { refine fin.cases _ _ p; simp [equiv.perm.decompose_fin, fin.succ_ne_zero] } /-- The set of all permutations of `fin (n + 1)` can be constructed by augmenting the set of permutations of `fin n` by each element of `fin (n + 1)` in turn. -/ lemma finset.univ_perm_fin_succ {n : ℕ} : @finset.univ (perm $ fin n.succ) _ = (finset.univ : finset $ fin n.succ × perm (fin n)).map equiv.perm.decompose_fin.symm.to_embedding := (finset.univ_map_equiv_to_embedding _).symm section cycle_range /-! ### `cycle_range` section Define the permutations `fin.cycle_range i`, the cycle `(0 1 2 ... i)`. -/ open equiv.perm lemma fin_rotate_succ {n : ℕ} : fin_rotate n.succ = decompose_fin.symm (1, fin_rotate n) := begin ext i, cases n, { simp }, refine fin.cases _ (λ i, _) i, { simp }, rw [coe_fin_rotate, decompose_fin_symm_apply_succ, if_congr (i.succ_eq_last_succ) rfl rfl], split_ifs with h, { simp [h] }, { rw [fin.coe_succ, function.injective.map_swap fin.coe_injective, fin.coe_succ, coe_fin_rotate, if_neg h, fin.coe_zero, fin.coe_one, swap_apply_of_ne_of_ne (nat.succ_ne_zero _) (nat.succ_succ_ne_one _)] } end @[simp] lemma sign_fin_rotate (n : ℕ) : perm.sign (fin_rotate (n + 1)) = (-1) ^ n := begin induction n with n ih, { simp }, { rw fin_rotate_succ, simp [ih, pow_succ] }, end @[simp] lemma support_fin_rotate {n : ℕ} : support (fin_rotate (n + 2)) = finset.univ := by { ext, simp } lemma support_fin_rotate_of_le {n : ℕ} (h : 2 ≤ n) : support (fin_rotate n) = finset.univ := begin obtain ⟨m, rfl⟩ := exists_add_of_le h, rw [add_comm, support_fin_rotate], end lemma is_cycle_fin_rotate {n : ℕ} : is_cycle (fin_rotate (n + 2)) := begin refine ⟨0, dec_trivial, λ x hx', ⟨x, _⟩⟩, clear hx', cases x with x hx, rw [coe_coe, zpow_coe_nat, fin.ext_iff, fin.coe_mk], induction x with x ih, { refl }, rw [pow_succ, perm.mul_apply, coe_fin_rotate_of_ne_last, ih (lt_trans x.lt_succ_self hx)], rw [ne.def, fin.ext_iff, ih (lt_trans x.lt_succ_self hx), fin.coe_last], exact ne_of_lt (nat.lt_of_succ_lt_succ hx), end lemma is_cycle_fin_rotate_of_le {n : ℕ} (h : 2 ≤ n) : is_cycle (fin_rotate n) := begin obtain ⟨m, rfl⟩ := exists_add_of_le h, rw [add_comm], exact is_cycle_fin_rotate end @[simp] lemma cycle_type_fin_rotate {n : ℕ} : cycle_type (fin_rotate (n + 2)) = {n + 2} := begin rw [is_cycle_fin_rotate.cycle_type, support_fin_rotate, ← fintype.card, fintype.card_fin], refl, end lemma cycle_type_fin_rotate_of_le {n : ℕ} (h : 2 ≤ n) : cycle_type (fin_rotate n) = {n} := begin obtain ⟨m, rfl⟩ := exists_add_of_le h, rw [add_comm, cycle_type_fin_rotate] end namespace fin /-- `fin.cycle_range i` is the cycle `(0 1 2 ... i)` leaving `(i+1 ... (n-1))` unchanged. -/ def cycle_range {n : ℕ} (i : fin n) : perm (fin n) := (fin_rotate (i + 1)) .extend_domain (equiv.of_left_inverse' (fin.cast_le (nat.succ_le_of_lt i.is_lt)).to_embedding coe (by { intros x, ext, simp })) lemma cycle_range_of_gt {n : ℕ} {i j : fin n.succ} (h : i < j) : cycle_range i j = j := begin rw [cycle_range, of_left_inverse'_eq_of_injective, ←function.embedding.to_equiv_range_eq_of_injective, ←via_fintype_embedding, via_fintype_embedding_apply_not_mem_range], simpa end lemma cycle_range_of_le {n : ℕ} {i j : fin n.succ} (h : j ≤ i) : cycle_range i j = if j = i then 0 else j + 1 := begin cases n, { simp }, have : j = (fin.cast_le (nat.succ_le_of_lt i.is_lt)).to_embedding ⟨j, lt_of_le_of_lt h (nat.lt_succ_self i)⟩, { simp }, ext, rw [this, cycle_range, of_left_inverse'_eq_of_injective, ←function.embedding.to_equiv_range_eq_of_injective, ←via_fintype_embedding, via_fintype_embedding_apply_image, rel_embedding.coe_fn_to_embedding, coe_cast_le, coe_fin_rotate], simp only [fin.ext_iff, coe_last, coe_mk, coe_zero, fin.eta, apply_ite coe, cast_le_mk], split_ifs with heq, { refl }, { rw fin.coe_add_one_of_lt, exact lt_of_lt_of_le (lt_of_le_of_ne h (mt (congr_arg coe) heq)) (le_last i) } end lemma coe_cycle_range_of_le {n : ℕ} {i j : fin n.succ} (h : j ≤ i) : (cycle_range i j : ℕ) = if j = i then 0 else j + 1 := by { rw [cycle_range_of_le h], split_ifs with h', { refl }, exact coe_add_one_of_lt (calc (j : ℕ) < i : fin.lt_iff_coe_lt_coe.mp (lt_of_le_of_ne h h') ... ≤ n : nat.lt_succ_iff.mp i.2) } lemma cycle_range_of_lt {n : ℕ} {i j : fin n.succ} (h : j < i) : cycle_range i j = j + 1 := by rw [cycle_range_of_le h.le, if_neg h.ne] lemma coe_cycle_range_of_lt {n : ℕ} {i j : fin n.succ} (h : j < i) : (cycle_range i j : ℕ) = j + 1 := by rw [coe_cycle_range_of_le h.le, if_neg h.ne] lemma cycle_range_of_eq {n : ℕ} {i j : fin n.succ} (h : j = i) : cycle_range i j = 0 := by rw [cycle_range_of_le h.le, if_pos h] @[simp] lemma cycle_range_self {n : ℕ} (i : fin n.succ) : cycle_range i i = 0 := cycle_range_of_eq rfl lemma cycle_range_apply {n : ℕ} (i j : fin n.succ) : cycle_range i j = if j < i then j + 1 else if j = i then 0 else j := begin split_ifs with h₁ h₂, { exact cycle_range_of_lt h₁ }, { exact cycle_range_of_eq h₂ }, { exact cycle_range_of_gt (lt_of_le_of_ne (le_of_not_gt h₁) (ne.symm h₂)) }, end @[simp] lemma cycle_range_zero (n : ℕ) : cycle_range (0 : fin n.succ) = 1 := begin ext j, refine fin.cases _ (λ j, _) j, { simp }, { rw [cycle_range_of_gt (fin.succ_pos j), one_apply] }, end @[simp] lemma cycle_range_last (n : ℕ) : cycle_range (last n) = fin_rotate (n + 1) := by { ext i, rw [coe_cycle_range_of_le (le_last _), coe_fin_rotate] } @[simp] lemma cycle_range_zero' {n : ℕ} (h : 0 < n) : cycle_range ⟨0, h⟩ = 1 := begin cases n with n, { cases h }, exact cycle_range_zero n end @[simp] lemma sign_cycle_range {n : ℕ} (i : fin n) : perm.sign (cycle_range i) = (-1) ^ (i : ℕ) := by simp [cycle_range] @[simp] lemma succ_above_cycle_range {n : ℕ} (i j : fin n) : i.succ.succ_above (i.cycle_range j) = swap 0 i.succ j.succ := begin cases n, { rcases j with ⟨_, ⟨⟩⟩ }, rcases lt_trichotomy j i with hlt | heq | hgt, { have : (j + 1).cast_succ = j.succ, { ext, rw [coe_cast_succ, coe_succ, fin.coe_add_one_of_lt (lt_of_lt_of_le hlt i.le_last)] }, rw [fin.cycle_range_of_lt hlt, fin.succ_above_below, this, swap_apply_of_ne_of_ne], { apply fin.succ_ne_zero }, { exact (fin.succ_injective _).ne hlt.ne }, { rw fin.lt_iff_coe_lt_coe, simpa [this] using hlt } }, { rw [heq, fin.cycle_range_self, fin.succ_above_below, swap_apply_right, fin.cast_succ_zero], { rw fin.cast_succ_zero, apply fin.succ_pos } }, { rw [fin.cycle_range_of_gt hgt, fin.succ_above_above, swap_apply_of_ne_of_ne], { apply fin.succ_ne_zero }, { apply (fin.succ_injective _).ne hgt.ne.symm }, { simpa [fin.le_iff_coe_le_coe] using hgt } }, end @[simp] lemma cycle_range_succ_above {n : ℕ} (i : fin (n + 1)) (j : fin n) : i.cycle_range (i.succ_above j) = j.succ := begin cases lt_or_ge j.cast_succ i with h h, { rw [fin.succ_above_below _ _ h, fin.cycle_range_of_lt h, fin.coe_succ_eq_succ] }, { rw [fin.succ_above_above _ _ h, fin.cycle_range_of_gt (fin.le_cast_succ_iff.mp h)] } end @[simp] lemma cycle_range_symm_zero {n : ℕ} (i : fin (n + 1)) : i.cycle_range.symm 0 = i := i.cycle_range.injective (by simp) @[simp] lemma cycle_range_symm_succ {n : ℕ} (i : fin (n + 1)) (j : fin n) : i.cycle_range.symm j.succ = i.succ_above j := i.cycle_range.injective (by simp) lemma is_cycle_cycle_range {n : ℕ} {i : fin (n + 1)} (h0 : i ≠ 0) : is_cycle (cycle_range i) := begin cases i with i hi, cases i, { exact (h0 rfl).elim }, exact is_cycle_fin_rotate.extend_domain _, end @[simp] lemma cycle_type_cycle_range {n : ℕ} {i : fin (n + 1)} (h0 : i ≠ 0) : cycle_type (cycle_range i) = {i + 1} := begin cases i with i hi, cases i, { exact (h0 rfl).elim }, rw [cycle_range, cycle_type_extend_domain], exact cycle_type_fin_rotate, end lemma is_three_cycle_cycle_range_two {n : ℕ} : is_three_cycle (cycle_range 2 : perm (fin (n + 3))) := begin rw [is_three_cycle, cycle_type_cycle_range]; dec_trivial end end fin end cycle_range
dea2c4c5b1833240e5fcf5eeddc11876101a9931
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/zsqrtd/gaussian_int.lean
6f8d254623e9f7186c29958a9c0df5ba8646e63d
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
12,568
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import number_theory.zsqrtd.basic import data.complex.basic import ring_theory.principal_ideal_domain import number_theory.legendre_symbol.quadratic_reciprocity /-! # Gaussian integers The Gaussian integers are complex integer, complex numbers whose real and imaginary parts are both integers. ## Main definitions The Euclidean domain structure on `ℤ[i]` is defined in this file. The homomorphism `to_complex` into the complex numbers is also defined in this file. ## Main statements `prime_iff_mod_four_eq_three_of_nat_prime` A prime natural number is prime in `ℤ[i]` if and only if it is `3` mod `4` ## Notations This file uses the local notation `ℤ[i]` for `gaussian_int` ## Implementation notes Gaussian integers are implemented using the more general definition `zsqrtd`, the type of integers adjoined a square root of `d`, in this case `-1`. The definition is reducible, so that properties and definitions about `zsqrtd` can easily be used. -/ open zsqrtd complex /-- The Gaussian integers, defined as `ℤ√(-1)`. -/ @[reducible] def gaussian_int : Type := zsqrtd (-1) local notation `ℤ[i]` := gaussian_int namespace gaussian_int instance : has_repr ℤ[i] := ⟨λ x, "⟨" ++ repr x.re ++ ", " ++ repr x.im ++ "⟩"⟩ instance : comm_ring ℤ[i] := zsqrtd.comm_ring section local attribute [-instance] complex.field -- Avoid making things noncomputable unnecessarily. /-- The embedding of the Gaussian integers into the complex numbers, as a ring homomorphism. -/ def to_complex : ℤ[i] →+* ℂ := zsqrtd.lift ⟨I, by simp⟩ end instance : has_coe (ℤ[i]) ℂ := ⟨to_complex⟩ lemma to_complex_def (x : ℤ[i]) : (x : ℂ) = x.re + x.im * I := rfl lemma to_complex_def' (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ) = x + y * I := by simp [to_complex_def] lemma to_complex_def₂ (x : ℤ[i]) : (x : ℂ) = ⟨x.re, x.im⟩ := by apply complex.ext; simp [to_complex_def] @[simp] lemma to_real_re (x : ℤ[i]) : ((x.re : ℤ) : ℝ) = (x : ℂ).re := by simp [to_complex_def] @[simp] lemma to_real_im (x : ℤ[i]) : ((x.im : ℤ) : ℝ) = (x : ℂ).im := by simp [to_complex_def] @[simp] lemma to_complex_re (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ).re = x := by simp [to_complex_def] @[simp] lemma to_complex_im (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ).im = y := by simp [to_complex_def] @[simp] lemma to_complex_add (x y : ℤ[i]) : ((x + y : ℤ[i]) : ℂ) = x + y := to_complex.map_add _ _ @[simp] lemma to_complex_mul (x y : ℤ[i]) : ((x * y : ℤ[i]) : ℂ) = x * y := to_complex.map_mul _ _ @[simp] lemma to_complex_one : ((1 : ℤ[i]) : ℂ) = 1 := to_complex.map_one @[simp] lemma to_complex_zero : ((0 : ℤ[i]) : ℂ) = 0 := to_complex.map_zero @[simp] lemma to_complex_neg (x : ℤ[i]) : ((-x : ℤ[i]) : ℂ) = -x := to_complex.map_neg _ @[simp] lemma to_complex_sub (x y : ℤ[i]) : ((x - y : ℤ[i]) : ℂ) = x - y := to_complex.map_sub _ _ @[simp] lemma to_complex_inj {x y : ℤ[i]} : (x : ℂ) = y ↔ x = y := by cases x; cases y; simp [to_complex_def₂] @[simp] lemma to_complex_eq_zero {x : ℤ[i]} : (x : ℂ) = 0 ↔ x = 0 := by rw [← to_complex_zero, to_complex_inj] @[simp] lemma nat_cast_real_norm (x : ℤ[i]) : (x.norm : ℝ) = (x : ℂ).norm_sq := by rw [norm, norm_sq]; simp @[simp] lemma nat_cast_complex_norm (x : ℤ[i]) : (x.norm : ℂ) = (x : ℂ).norm_sq := by cases x; rw [norm, norm_sq]; simp lemma norm_nonneg (x : ℤ[i]) : 0 ≤ norm x := norm_nonneg (by norm_num) _ @[simp] lemma norm_eq_zero {x : ℤ[i]} : norm x = 0 ↔ x = 0 := by rw [← @int.cast_inj ℝ _ _ _]; simp lemma norm_pos {x : ℤ[i]} : 0 < norm x ↔ x ≠ 0 := by rw [lt_iff_le_and_ne, ne.def, eq_comm, norm_eq_zero]; simp [norm_nonneg] @[simp] lemma coe_nat_abs_norm (x : ℤ[i]) : (x.norm.nat_abs : ℤ) = x.norm := int.nat_abs_of_nonneg (norm_nonneg _) @[simp] lemma nat_cast_nat_abs_norm {α : Type*} [ring α] (x : ℤ[i]) : (x.norm.nat_abs : α) = x.norm := by rw [← int.cast_coe_nat, coe_nat_abs_norm] lemma nat_abs_norm_eq (x : ℤ[i]) : x.norm.nat_abs = x.re.nat_abs * x.re.nat_abs + x.im.nat_abs * x.im.nat_abs := int.coe_nat_inj $ begin simp, simp [norm] end instance : has_div ℤ[i] := ⟨λ x y, let n := (rat.of_int (norm y))⁻¹, c := y.conj in ⟨round (rat.of_int (x * c).re * n : ℚ), round (rat.of_int (x * c).im * n : ℚ)⟩⟩ lemma div_def (x y : ℤ[i]) : x / y = ⟨round ((x * conj y).re / norm y : ℚ), round ((x * conj y).im / norm y : ℚ)⟩ := show zsqrtd.mk _ _ = _, by simp [rat.of_int_eq_mk, rat.mk_eq_div, div_eq_mul_inv] lemma to_complex_div_re (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).re = round ((x / y : ℂ).re) := by rw [div_def, ← @rat.round_cast ℝ _ _]; simp [-rat.round_cast, mul_assoc, div_eq_mul_inv, mul_add, add_mul] lemma to_complex_div_im (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).im = round ((x / y : ℂ).im) := by rw [div_def, ← @rat.round_cast ℝ _ _, ← @rat.round_cast ℝ _ _]; simp [-rat.round_cast, mul_assoc, div_eq_mul_inv, mul_add, add_mul] lemma norm_sq_le_norm_sq_of_re_le_of_im_le {x y : ℂ} (hre : |x.re| ≤ |y.re|) (him : |x.im| ≤ |y.im|) : x.norm_sq ≤ y.norm_sq := by rw [norm_sq_apply, norm_sq_apply, ← _root_.abs_mul_self, _root_.abs_mul, ← _root_.abs_mul_self y.re, _root_.abs_mul y.re, ← _root_.abs_mul_self x.im, _root_.abs_mul x.im, ← _root_.abs_mul_self y.im, _root_.abs_mul y.im]; exact (add_le_add (mul_self_le_mul_self (abs_nonneg _) hre) (mul_self_le_mul_self (abs_nonneg _) him)) lemma norm_sq_div_sub_div_lt_one (x y : ℤ[i]) : ((x / y : ℂ) - ((x / y : ℤ[i]) : ℂ)).norm_sq < 1 := calc ((x / y : ℂ) - ((x / y : ℤ[i]) : ℂ)).norm_sq = ((x / y : ℂ).re - ((x / y : ℤ[i]) : ℂ).re + ((x / y : ℂ).im - ((x / y : ℤ[i]) : ℂ).im) * I : ℂ).norm_sq : congr_arg _ $ by apply complex.ext; simp ... ≤ (1 / 2 + 1 / 2 * I).norm_sq : have |(2⁻¹ : ℝ)| = 2⁻¹, from _root_.abs_of_nonneg (by norm_num), norm_sq_le_norm_sq_of_re_le_of_im_le (by rw [to_complex_div_re]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : ℂ).re) (by rw [to_complex_div_im]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : ℂ).im) ... < 1 : by simp [norm_sq]; norm_num instance : has_mod ℤ[i] := ⟨λ x y, x - y * (x / y)⟩ lemma mod_def (x y : ℤ[i]) : x % y = x - y * (x / y) := rfl lemma norm_mod_lt (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (x % y).norm < y.norm := have (y : ℂ) ≠ 0, by rwa [ne.def, ← to_complex_zero, to_complex_inj], (@int.cast_lt ℝ _ _ _ _).1 $ calc ↑(norm (x % y)) = (x - y * (x / y : ℤ[i]) : ℂ).norm_sq : by simp [mod_def] ... = (y : ℂ).norm_sq * (((x / y) - (x / y : ℤ[i])) : ℂ).norm_sq : by rw [← norm_sq_mul, mul_sub, mul_div_cancel' _ this] ... < (y : ℂ).norm_sq * 1 : mul_lt_mul_of_pos_left (norm_sq_div_sub_div_lt_one _ _) (norm_sq_pos.2 this) ... = norm y : by simp lemma nat_abs_norm_mod_lt (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (x % y).norm.nat_abs < y.norm.nat_abs := int.coe_nat_lt.1 (by simp [-int.coe_nat_lt, norm_mod_lt x hy]) lemma norm_le_norm_mul_left (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (norm x).nat_abs ≤ (norm (x * y)).nat_abs := by rw [norm_mul, int.nat_abs_mul]; exact le_mul_of_one_le_right (nat.zero_le _) (int.coe_nat_le.1 (by rw [coe_nat_abs_norm]; exact int.add_one_le_of_lt (norm_pos.2 hy))) instance : nontrivial ℤ[i] := ⟨⟨0, 1, dec_trivial⟩⟩ instance : euclidean_domain ℤ[i] := { quotient := (/), remainder := (%), quotient_zero := by { simp [div_def], refl }, quotient_mul_add_remainder_eq := λ _ _, by simp [mod_def], r := _, r_well_founded := measure_wf (int.nat_abs ∘ norm), remainder_lt := nat_abs_norm_mod_lt, mul_left_not_lt := λ a b hb0, not_lt_of_ge $ norm_le_norm_mul_left a hb0, .. gaussian_int.comm_ring, .. gaussian_int.nontrivial } open principal_ideal_ring lemma mod_four_eq_three_of_nat_prime_of_prime (p : ℕ) [hp : fact p.prime] (hpi : prime (p : ℤ[i])) : p % 4 = 3 := hp.1.eq_two_or_odd.elim (λ hp2, absurd hpi (mt irreducible_iff_prime.2 $ λ ⟨hu, h⟩, begin have := h ⟨1, 1⟩ ⟨1, -1⟩ (hp2.symm ▸ rfl), rw [← norm_eq_one_iff, ← norm_eq_one_iff] at this, exact absurd this dec_trivial end)) (λ hp1, by_contradiction $ λ hp3 : p % 4 ≠ 3, have hp41 : p % 4 = 1, begin rw [← nat.mod_mul_left_mod p 2 2, show 2 * 2 = 4, from rfl] at hp1, have := nat.mod_lt p (show 0 < 4, from dec_trivial), revert this hp3 hp1, generalize : p % 4 = m, dec_trivial!, end, let ⟨k, hk⟩ := (zmod.exists_sq_eq_neg_one_iff p).2 $ by rw hp41; exact dec_trivial in begin obtain ⟨k, k_lt_p, rfl⟩ : ∃ (k' : ℕ) (h : k' < p), (k' : zmod p) = k, { refine ⟨k.val, k.val_lt, zmod.nat_cast_zmod_val k⟩ }, have hpk : p ∣ k ^ 2 + 1, by { rw [pow_two, ← char_p.cast_eq_zero_iff (zmod p) p, nat.cast_add, nat.cast_mul, nat.cast_one, ← hk, add_left_neg], }, have hkmul : (k ^ 2 + 1 : ℤ[i]) = ⟨k, 1⟩ * ⟨k, -1⟩ := by simp [sq, zsqrtd.ext], have hpne1 : p ≠ 1 := ne_of_gt hp.1.one_lt, have hkltp : 1 + k * k < p * p, from calc 1 + k * k ≤ k + k * k : add_le_add_right (nat.pos_of_ne_zero (λ hk0, by clear_aux_decl; simp [*, pow_succ'] at *)) _ ... = k * (k + 1) : by simp [add_comm, mul_add] ... < p * p : mul_lt_mul k_lt_p k_lt_p (nat.succ_pos _) (nat.zero_le _), have hpk₁ : ¬ (p : ℤ[i]) ∣ ⟨k, -1⟩ := λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $ calc (norm (p * x : ℤ[i])).nat_abs = (norm ⟨k, -1⟩).nat_abs : by rw hx ... < (norm (p : ℤ[i])).nat_abs : by simpa [add_comm, norm] using hkltp ... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _ (λ hx0, (show (-1 : ℤ) ≠ 0, from dec_trivial) $ by simpa [hx0] using congr_arg zsqrtd.im hx), have hpk₂ : ¬ (p : ℤ[i]) ∣ ⟨k, 1⟩ := λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $ calc (norm (p * x : ℤ[i])).nat_abs = (norm ⟨k, 1⟩).nat_abs : by rw hx ... < (norm (p : ℤ[i])).nat_abs : by simpa [add_comm, norm] using hkltp ... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _ (λ hx0, (show (1 : ℤ) ≠ 0, from dec_trivial) $ by simpa [hx0] using congr_arg zsqrtd.im hx), have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 (by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff]; exact λ h, (ne_of_lt hp.1.one_lt).symm h.1), obtain ⟨y, hy⟩ := hpk, have := hpi.2.2 ⟨k, 1⟩ ⟨k, -1⟩ ⟨y, by rw [← hkmul, ← nat.cast_mul p, ← hy]; simp⟩, clear_aux_decl, tauto end) lemma sq_add_sq_of_nat_prime_of_not_irreducible (p : ℕ) [hp : fact p.prime] (hpi : ¬irreducible (p : ℤ[i])) : ∃ a b, a^2 + b^2 = p := have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 $ by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff]; exact λ h, (ne_of_lt hp.1.one_lt).symm h.1, have hab : ∃ a b, (p : ℤ[i]) = a * b ∧ ¬ is_unit a ∧ ¬ is_unit b, by simpa [irreducible_iff, hpu, not_forall, not_or_distrib] using hpi, let ⟨a, b, hpab, hau, hbu⟩ := hab in have hnap : (norm a).nat_abs = p, from ((hp.1.mul_eq_prime_sq_iff (mt norm_eq_one_iff.1 hau) (mt norm_eq_one_iff.1 hbu)).1 $ by rw [← int.coe_nat_inj', int.coe_nat_pow, sq, ← @norm_nat_cast (-1), hpab]; simp).1, ⟨a.re.nat_abs, a.im.nat_abs, by simpa [nat_abs_norm_eq, sq] using hnap⟩ lemma prime_of_nat_prime_of_mod_four_eq_three (p : ℕ) [hp : fact p.prime] (hp3 : p % 4 = 3) : prime (p : ℤ[i]) := irreducible_iff_prime.1 $ classical.by_contradiction $ λ hpi, let ⟨a, b, hab⟩ := sq_add_sq_of_nat_prime_of_not_irreducible p hpi in have ∀ a b : zmod 4, a^2 + b^2 ≠ p, by erw [← zmod.nat_cast_mod p 4, hp3]; exact dec_trivial, this a b (hab ▸ by simp) /-- A prime natural number is prime in `ℤ[i]` if and only if it is `3` mod `4` -/ lemma prime_iff_mod_four_eq_three_of_nat_prime (p : ℕ) [hp : fact p.prime] : prime (p : ℤ[i]) ↔ p % 4 = 3 := ⟨mod_four_eq_three_of_nat_prime_of_prime p, prime_of_nat_prime_of_mod_four_eq_three p⟩ end gaussian_int
875c309f3a4a17bfb5c639a9b4098e5fd415c56f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/continuous_function/ordered.lean
7bbf9121a9ebe159959f8959f7f9c06476f5937f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
4,804
lean
/- Copyright © 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Shing Tak Lam -/ import topology.algebra.order.proj_Icc import topology.algebra.order.group import topology.continuous_function.basic /-! # Bundled continuous maps into orders, with order-compatible topology > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ variables {α : Type*} {β : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] namespace continuous_map section variables [linear_ordered_add_comm_group β] [order_topology β] /-- The pointwise absolute value of a continuous function as a continuous function. -/ def abs (f : C(α, β)) : C(α, β) := { to_fun := λ x, |f x|, } @[priority 100] -- see Note [lower instance priority] instance : has_abs C(α, β) := ⟨λf, abs f⟩ @[simp] lemma abs_apply (f : C(α, β)) (x : α) : |f| x = |f x| := rfl end /-! We now set up the partial order and lattice structure (given by pointwise min and max) on continuous functions. -/ section lattice instance partial_order [partial_order β] : partial_order C(α, β) := partial_order.lift (λ f, f.to_fun) (by tidy) lemma le_def [partial_order β] {f g : C(α, β)} : f ≤ g ↔ ∀ a, f a ≤ g a := pi.le_def lemma lt_def [partial_order β] {f g : C(α, β)} : f < g ↔ (∀ a, f a ≤ g a) ∧ (∃ a, f a < g a) := pi.lt_def instance has_sup [linear_order β] [order_closed_topology β] : has_sup C(α, β) := { sup := λ f g, { to_fun := λ a, max (f a) (g a), } } @[simp, norm_cast] lemma sup_coe [linear_order β] [order_closed_topology β] (f g : C(α, β)) : ((f ⊔ g : C(α, β)) : α → β) = (f ⊔ g : α → β) := rfl @[simp] lemma sup_apply [linear_order β] [order_closed_topology β] (f g : C(α, β)) (a : α) : (f ⊔ g) a = max (f a) (g a) := rfl instance [linear_order β] [order_closed_topology β] : semilattice_sup C(α, β) := { le_sup_left := λ f g, le_def.mpr (by simp [le_refl]), le_sup_right := λ f g, le_def.mpr (by simp [le_refl]), sup_le := λ f₁ f₂ g w₁ w₂, le_def.mpr (λ a, by simp [le_def.mp w₁ a, le_def.mp w₂ a]), ..continuous_map.partial_order, ..continuous_map.has_sup, } instance has_inf [linear_order β] [order_closed_topology β] : has_inf C(α, β) := { inf := λ f g, { to_fun := λ a, min (f a) (g a), } } @[simp, norm_cast] lemma inf_coe [linear_order β] [order_closed_topology β] (f g : C(α, β)) : ((f ⊓ g : C(α, β)) : α → β) = (f ⊓ g : α → β) := rfl @[simp] lemma inf_apply [linear_order β] [order_closed_topology β] (f g : C(α, β)) (a : α) : (f ⊓ g) a = min (f a) (g a) := rfl instance [linear_order β] [order_closed_topology β] : semilattice_inf C(α, β) := { inf_le_left := λ f g, le_def.mpr (by simp [le_refl]), inf_le_right := λ f g, le_def.mpr (by simp [le_refl]), le_inf := λ f₁ f₂ g w₁ w₂, le_def.mpr (λ a, by simp [le_def.mp w₁ a, le_def.mp w₂ a]), ..continuous_map.partial_order, ..continuous_map.has_inf, } instance [linear_order β] [order_closed_topology β] : lattice C(α, β) := { ..continuous_map.semilattice_inf, ..continuous_map.semilattice_sup } -- TODO transfer this lattice structure to `bounded_continuous_function` section sup' variables [linear_order γ] [order_closed_topology γ] lemma sup'_apply {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) (b : β) : s.sup' H f b = s.sup' H (λ a, f a b) := finset.comp_sup'_eq_sup'_comp H (λ f : C(β, γ), f b) (λ i j, rfl) @[simp, norm_cast] lemma sup'_coe {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) : ((s.sup' H f : C(β, γ)) : ι → β) = s.sup' H (λ a, (f a : β → γ)) := by { ext, simp [sup'_apply], } end sup' section inf' variables [linear_order γ] [order_closed_topology γ] lemma inf'_apply {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) (b : β) : s.inf' H f b = s.inf' H (λ a, f a b) := @sup'_apply _ γᵒᵈ _ _ _ _ _ _ H f b @[simp, norm_cast] lemma inf'_coe {ι : Type*} {s : finset ι} (H : s.nonempty) (f : ι → C(β, γ)) : ((s.inf' H f : C(β, γ)) : ι → β) = s.inf' H (λ a, (f a : β → γ)) := @sup'_coe _ γᵒᵈ _ _ _ _ _ _ H f end inf' end lattice section extend variables [linear_order α] [order_topology α] {a b : α} (h : a ≤ b) /-- Extend a continuous function `f : C(set.Icc a b, β)` to a function `f : C(α, β)`. -/ def Icc_extend (f : C(set.Icc a b, β)) : C(α, β) := ⟨set.Icc_extend h f⟩ @[simp] lemma coe_Icc_extend (f : C(set.Icc a b, β)) : ((Icc_extend h f : C(α, β)) : α → β) = set.Icc_extend h f := rfl end extend end continuous_map
48f8deb4c3b578a78b18c92cddd947aaa20c77f6
5d166a16ae129621cb54ca9dde86c275d7d2b483
/leanpkg/leanpkg/toml.lean
73e5b86eec2ccf025185a60655d7337d66701646
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
3,816
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Gabriel Ebner -/ import data.buffer.parser -- I'd like to contribute a new naming convention: -- CamelCase for non-terminals. def join (sep : string) : list string → string | [x] := x | [] := "" | (x::xs) := x ++ sep ++ join xs namespace toml inductive value : Type | str : string → value | nat : ℕ → value | bool : bool → value | table : list (string × value) → value namespace value private def escapec : char → string | '\"' := "\\\"" | '\t' := "\\t" | '\n' := "\\n" | '\\' := "\\\\" | c := c.to_string private def escape (s : string) : string := s.fold "" (λ s c, s ++ escapec c) private mutual def to_string_core, to_string_pairs with to_string_core : value → string | (value.str s) := "\"" ++ escape s ++ "\"" | (value.nat n) := to_string n | (value.bool tt) := "true" | (value.bool ff) := "false" | (value.table cs) := "{" ++ to_string_pairs cs ++ "}" with to_string_pairs : list (string × value) → string | [] := "" | [(k, v)] := k ++ " = " ++ to_string_core v | ((k, v)::kvs) := k ++ " = " ++ to_string_core v ++ ", " ++ to_string_pairs kvs protected def to_string : ∀ (v : value), string | (table cs) := join "\n" $ do (h, c) ← cs, match c with | table ds := ["[" ++ h ++ "]\n" ++ join "" (do (k, v) ← ds, [k ++ " = " ++ to_string_core v ++ "\n"])] | _ := ["<error> " ++ to_string_core c] end | v := to_string_core v instance : has_to_string value := ⟨value.to_string⟩ def lookup : value → string → option value | (value.table cs) k := match cs.filter (λ c : string × value, c.fst = k) with | [] := none | (k,v) ::_ := some v end | _ _ := none end value open parser def Comment : parser unit := ch '#' >> many' (sat (≠ '#')) >> optional (ch '\n') >> eps def Ws : parser unit := decorate_error "<whitespace>" $ many' $ one_of' " \t\n".to_list <|> Comment def tok (s : string) := str s >> Ws def StringChar : parser char := sat (λc, c ≠ '\"' ∧ c ≠ '\\' ∧ c.val > 0x1f) <|> (do str "\\", (str "t" >> return '\t') <|> (str "n" >> return '\n') <|> (str "\\" >> return '\\') <|> (str "\"" >> return '\"')) def BasicString : parser string := str "\"" *> (many_char StringChar) <* str "\"" <* Ws def String := BasicString def Nat : parser nat := do s ← many_char1 (one_of "0123456789".to_list), Ws, return s.to_nat def Boolean : parser bool := (tok "true" >> return tt) <|> (tok "false" >> return ff) def BareKey : parser string := do cs ← many_char1 $ sat $ λ c, c.is_alpha ∨ c.is_digit ∨ c = '_' ∨ c = '-', Ws, return cs def Key := BareKey <|> BasicString section parameter Val : parser value def KeyVal : parser (string × value) := do k ← Key, tok "=", v ← Val, return (k, v) def StdTable : parser (string × value) := do tok "[", n ← Key, tok "]", cs ← many KeyVal, return (n, value.table cs) def Table : parser (string × value) := StdTable def StrVal : parser value := value.str <$> String def NatVal : parser value := value.nat <$> Nat def BoolVal : parser value := value.bool <$> Boolean def InlineTable : parser value := tok "{" *> (value.table <$> sep_by (tok ",") KeyVal) <* tok "}" end def Val : parser value := fix $ λ Val, StrVal <|> NatVal <|> BoolVal <|> InlineTable Val def Expression := Table Val def File : parser value := Ws *> (value.table <$> many Expression) /- #eval run_string File $ "[package]\n" ++ "name = \"sss\"\n" ++ "version = \"0.1\"\n" ++ "timeout = 10\n" ++ "\n" ++ "[dependencies]\n" ++ "library_dev = { git = \"https://github.com/leanprover/library_dev\", rev = \"master\" }" -/ end toml
03b37a659f28cf41d7581218e895eea63cf14032
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/data/list/perm.lean
a475bfb16ece48eb6b6938885e65ec6fae4abfc3
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
38,751
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro List permutations. -/ import data.list.basic namespace list universe variables uu vv variables {α : Type uu} {β : Type vv} /-- `perm l₁ l₂` or `l₁ ~ l₂` asserts that `l₁` and `l₂` are permutations of each other. This is defined by induction using pairwise swaps. -/ inductive perm : list α → list α → Prop | nil : perm [] [] | skip : Π (x : α) {l₁ l₂ : list α}, perm l₁ l₂ → perm (x::l₁) (x::l₂) | swap : Π (x y : α) (l : list α), perm (y::x::l) (x::y::l) | trans : Π {l₁ l₂ l₃ : list α}, perm l₁ l₂ → perm l₂ l₃ → perm l₁ l₃ open perm infix ~ := perm @[refl] protected theorem perm.refl : ∀ (l : list α), l ~ l | [] := perm.nil | (x::xs) := skip x (perm.refl xs) @[symm] protected theorem perm.symm {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₂ ~ l₁ := perm.rec_on p perm.nil (λ x l₁ l₂ p₁ r₁, skip x r₁) (λ x y l, swap y x l) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₂ r₁) attribute [trans] perm.trans theorem perm.eqv (α : Type) : equivalence (@perm α) := mk_equivalence (@perm α) (@perm.refl α) (@perm.symm α) (@perm.trans α) instance is_setoid (α : Type) : setoid (list α) := setoid.mk (@perm α) (perm.eqv α) theorem perm_subset {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁ ⊆ l₂ := λ a, perm.rec_on p (λ h, h) (λ x l₁ l₂ p₁ r₁ i, or.elim i (λ ax, by simp [ax]) (λ al₁, or.inr (r₁ al₁))) (λ x y l ayxl, or.elim ayxl (λ ay, by simp [ay]) (λ axl, or.elim axl (λ ax, by simp [ax]) (λ al, or.inr (or.inr al)))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ ainl₁, r₂ (r₁ ainl₁)) theorem mem_of_perm {a : α} {l₁ l₂ : list α} (h : l₁ ~ l₂) : a ∈ l₁ ↔ a ∈ l₂ := iff.intro (λ m, perm_subset h m) (λ m, perm_subset h.symm m) theorem perm_app_left {l₁ l₂ : list α} (t₁ : list α) (p : l₁ ~ l₂) : l₁++t₁ ~ l₂++t₁ := perm.rec_on p (perm.refl ([] ++ t₁)) (λ x l₁ l₂ p₁ r₁, skip x r₁) (λ x y l, swap x y _) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_app_right {t₁ t₂ : list α} : ∀ (l : list α), t₁ ~ t₂ → l++t₁ ~ l++t₂ | [] p := p | (x::xs) p := skip x (perm_app_right xs p) theorem perm_app {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁++t₁ ~ l₂++t₂ := trans (perm_app_left t₁ p₁) (perm_app_right l₂ p₂) theorem perm_app_cons (a : α) {h₁ h₂ t₁ t₂ : list α} (p₁ : h₁ ~ h₂) (p₂ : t₁ ~ t₂) : h₁ ++ a::t₁ ~ h₂ ++ a::t₂ := perm_app p₁ (skip a p₂) @[simp] theorem perm_middle {a : α} : ∀ {l₁ l₂ : list α}, l₁++a::l₂ ~ a::(l₁++l₂) | [] l₂ := perm.refl _ | (b::l₁) l₂ := (skip b (@perm_middle l₁ l₂)).trans (swap a b _) @[simp] theorem perm_cons_app (a : α) (l : list α) : l ++ [a] ~ a::l := by simpa using @perm_middle _ a l [] @[simp] theorem perm_app_comm : ∀ {l₁ l₂ : list α}, (l₁++l₂) ~ (l₂++l₁) | [] l₂ := by simp | (a::t) l₂ := (skip a perm_app_comm).trans perm_middle.symm theorem concat_perm (l : list α) (a : α) : concat l a ~ a :: l := by simp theorem perm_length {l₁ l₂ : list α} (p : l₁ ~ l₂) : length l₁ = length l₂ := perm.rec_on p rfl (λ x l₁ l₂ p r, by simp[r]) (λ x y l, by simp) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, eq.trans r₁ r₂) theorem eq_nil_of_perm_nil {l₁ : list α} (p : [] ~ l₁) : l₁ = [] := eq_nil_of_length_eq_zero (perm_length p).symm theorem perm_nil {l₁ : list α} : l₁ ~ [] ↔ l₁ = [] := ⟨λ p, eq_nil_of_perm_nil p.symm, λ e, e ▸ perm.refl _⟩ theorem not_perm_nil_cons (x : α) (l : list α) : ¬ [] ~ x::l | p := by injection eq_nil_of_perm_nil p theorem eq_singleton_of_perm {a b : α} (p : [a] ~ [b]) : a = b := by simpa using perm_subset p (by simp) theorem eq_singleton_of_perm_inv {a : α} {l : list α} (p : [a] ~ l) : l = [a] := match l, show 1 = _, from perm_length p, p with | [a'], rfl, p := by rw [eq_singleton_of_perm p] end @[simp] theorem reverse_perm : ∀ (l : list α), reverse l ~ l | [] := perm.nil | (a::l) := by rw reverse_cons; exact (perm_cons_app _ _).trans (skip a $ reverse_perm l) theorem perm_cons_app_cons {l l₁ l₂ : list α} (a : α) (p : l ~ l₁++l₂) : a::l ~ l₁++(a::l₂) := trans (skip a p) perm_middle.symm @[simp] theorem perm_repeat {a : α} {n : ℕ} {l : list α} : repeat a n ~ l ↔ repeat a n = l := ⟨λ p, (eq_repeat.2 $ by exact ⟨by simpa using (perm_length p).symm, λ b m, eq_of_mem_repeat $ perm_subset p.symm m⟩).symm, λ h, h ▸ perm.refl _⟩ theorem perm_erase [decidable_eq α] {a : α} {l : list α} (h : a ∈ l) : l ~ a :: l.erase a := let ⟨l₁, l₂, _, e₁, e₂⟩ := exists_erase_eq h in e₂.symm ▸ e₁.symm ▸ perm_middle @[elab_as_eliminator] theorem perm_induction_on {P : list α → list α → Prop} {l₁ l₂ : list α} (p : l₁ ~ l₂) (h₁ : P [] []) (h₂ : ∀ x l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (x::l₁) (x::l₂)) (h₃ : ∀ x y l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (y::x::l₁) (x::y::l₂)) (h₄ : ∀ l₁ l₂ l₃, l₁ ~ l₂ → l₂ ~ l₃ → P l₁ l₂ → P l₂ l₃ → P l₁ l₃) : P l₁ l₂ := have P_refl : ∀ l, P l l, from assume l, list.rec_on l h₁ (λ x xs ih, h₂ x xs xs (perm.refl xs) ih), perm.rec_on p h₁ h₂ (λ x y l, h₃ x y l l (perm.refl l) (P_refl l)) h₄ @[congr] theorem perm_filter_map (f : α → option β) {l₁ l₂ : list α} (p : l₁ ~ l₂) : filter_map f l₁ ~ filter_map f l₂ := begin induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂, { simp }, { simp [filter_map], cases f x with a; simp [filter_map, IH, skip] }, { simp [filter_map], cases f x with a; cases f y with b; simp [filter_map, swap] }, { exact IH₁.trans IH₂ } end @[congr] theorem perm_map (f : α → β) {l₁ l₂ : list α} (p : l₁ ~ l₂) : map f l₁ ~ map f l₂ := by rw ← filter_map_eq_map; apply perm_filter_map _ p theorem perm_pmap {p : α → Prop} (f : Π a, p a → β) {l₁ l₂ : list α} (p : l₁ ~ l₂) {H₁ H₂} : pmap f l₁ H₁ ~ pmap f l₂ H₂ := begin induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂, { simp }, { simp [IH, skip] }, { simp [swap] }, { refine IH₁.trans IH₂, exact λ a m, H₂ a (perm_subset p₂ m) } end theorem perm_filter (p : α → Prop) [decidable_pred p] {l₁ l₂ : list α} (s : l₁ ~ l₂) : filter p l₁ ~ filter p l₂ := by rw ← filter_map_eq_filter; apply perm_filter_map _ s theorem exists_perm_sublist {l₁ l₂ l₂' : list α} (s : l₁ <+ l₂) (p : l₂ ~ l₂') : ∃ l₁' ~ l₁, l₁' <+ l₂' := begin induction p with x l₂ l₂' p IH x y l₂ l₂ m₂ r₂ p₁ p₂ IH₁ IH₂ generalizing l₁ s, { exact ⟨[], eq_nil_of_sublist_nil s ▸ perm.refl _, nil_sublist _⟩ }, { cases s with _ _ _ s l₁ _ _ s, { exact let ⟨l₁', p', s'⟩ := IH s in ⟨l₁', p', s'.cons _ _ _⟩ }, { exact let ⟨l₁', p', s'⟩ := IH s in ⟨x::l₁', skip x p', s'.cons2 _ _ _⟩ } }, { cases s with _ _ _ s l₁ _ _ s; cases s with _ _ _ s l₁ _ _ s, { exact ⟨l₁, perm.refl _, (s.cons _ _ _).cons _ _ _⟩ }, { exact ⟨x::l₁, perm.refl _, (s.cons _ _ _).cons2 _ _ _⟩ }, { exact ⟨y::l₁, perm.refl _, (s.cons2 _ _ _).cons _ _ _⟩ }, { exact ⟨x::y::l₁, perm.swap _ _ _, (s.cons2 _ _ _).cons2 _ _ _⟩ } }, { exact let ⟨m₁, pm, sm⟩ := IH₁ s, ⟨r₁, pr, sr⟩ := IH₂ sm in ⟨r₁, pr.trans pm, sr⟩ } end section rel open relator variables {γ : Type*} {δ : Type*} {r : α → β → Prop} {p : γ → δ → Prop} local infixr ` ∘r ` : 80 := relation.comp lemma perm_comp_perm : (perm ∘r perm : list α → list α → Prop) = perm := begin funext a c, apply propext, split, { exact assume ⟨b, hab, hba⟩, perm.trans hab hba }, { exact assume h, ⟨a, perm.refl a, h⟩ } end lemma perm_comp_forall₂ {l u v} (hlu : perm l u) (huv : forall₂ r u v) : (forall₂ r ∘r perm) l v := begin induction hlu generalizing v, case perm.nil { cases huv, exact ⟨[], forall₂.nil, perm.nil⟩ }, case perm.skip : a l u hlu ih { cases huv with _ b _ v hab huv', rcases ih huv' with ⟨l₂, h₁₂, h₂₃⟩, exact ⟨b::l₂, forall₂.cons hab h₁₂, perm.skip _ h₂₃⟩ }, case perm.swap : a₁ a₂ l₁ l₂ h₂₃ { cases h₂₃ with _ b₁ _ l₂ h₁ hr_₂₃, cases hr_₂₃ with _ b₂ _ l₂ h₂ h₁₂, exact ⟨b₂::b₁::l₂, forall₂.cons h₂ (forall₂.cons h₁ h₁₂), perm.swap _ _ _⟩ }, case perm.trans : la₁ la₂ la₃ _ _ ih₁ ih₂ { rcases ih₂ huv with ⟨lb₂, hab₂, h₂₃⟩, rcases ih₁ hab₂ with ⟨lb₁, hab₁, h₁₂⟩, exact ⟨lb₁, hab₁, perm.trans h₁₂ h₂₃⟩ } end lemma forall₂_comp_perm_eq_perm_comp_forall₂ : forall₂ r ∘r perm = perm ∘r forall₂ r := begin funext l₁ l₃, apply propext, split, { assume h, rcases h with ⟨l₂, h₁₂, h₂₃⟩, have : forall₂ (flip r) l₂ l₁, from h₁₂.flip , rcases perm_comp_forall₂ h₂₃.symm this with ⟨l', h₁, h₂⟩, exact ⟨l', h₂.symm, h₁.flip⟩ }, { exact assume ⟨l₂, h₁₂, h₂₃⟩, perm_comp_forall₂ h₁₂ h₂₃ } end lemma rel_perm_imp (hr : right_unique r) : (forall₂ r ⇒ forall₂ r ⇒ implies) perm perm := assume a b h₁ c d h₂ h, have (flip (forall₂ r) ∘r (perm ∘r forall₂ r)) b d, from ⟨a, h₁, c, h, h₂⟩, have ((flip (forall₂ r) ∘r forall₂ r) ∘r perm) b d, by rwa [← forall₂_comp_perm_eq_perm_comp_forall₂, ← relation.comp_assoc] at this, let ⟨b', ⟨c', hbc, hcb⟩, hbd⟩ := this in have b' = b, from right_unique_forall₂ @hr hcb hbc, this ▸ hbd lemma rel_perm (hr : bi_unique r) : (forall₂ r ⇒ forall₂ r ⇒ (↔)) perm perm := assume a b hab c d hcd, iff.intro (rel_perm_imp hr.2 hab hcd) (rel_perm_imp (assume a b c, left_unique_flip hr.1) hab.flip hcd.flip) end rel section subperm /-- `subperm l₁ l₂`, denoted `l₁ <+~ l₂`, means that `l₁` is a sublist of a permutation of `l₂`. This is an analogue of `l₁ ⊆ l₂` which respects multiplicities of elements, and is used for the `≤` relation on multisets. -/ def subperm (l₁ l₂ : list α) : Prop := ∃ l ~ l₁, l <+ l₂ infix ` <+~ `:50 := subperm theorem perm.subperm_left {l l₁ l₂ : list α} (p : l₁ ~ l₂) : l <+~ l₁ ↔ l <+~ l₂ := suffices ∀ {l₁ l₂ : list α}, l₁ ~ l₂ → l <+~ l₁ → l <+~ l₂, from ⟨this p, this p.symm⟩, λ l₁ l₂ p ⟨u, pu, su⟩, let ⟨v, pv, sv⟩ := exists_perm_sublist su p in ⟨v, pv.trans pu, sv⟩ theorem perm.subperm_right {l₁ l₂ l : list α} (p : l₁ ~ l₂) : l₁ <+~ l ↔ l₂ <+~ l := ⟨λ ⟨u, pu, su⟩, ⟨u, pu.trans p, su⟩, λ ⟨u, pu, su⟩, ⟨u, pu.trans p.symm, su⟩⟩ theorem subperm_of_sublist {l₁ l₂ : list α} (s : l₁ <+ l₂) : l₁ <+~ l₂ := ⟨l₁, perm.refl _, s⟩ theorem subperm_of_perm {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁ <+~ l₂ := ⟨l₂, p.symm, sublist.refl _⟩ theorem subperm.refl (l : list α) : l <+~ l := subperm_of_perm (perm.refl _) theorem subperm.trans {l₁ l₂ l₃ : list α} : l₁ <+~ l₂ → l₂ <+~ l₃ → l₁ <+~ l₃ | s ⟨l₂', p₂, s₂⟩ := let ⟨l₁', p₁, s₁⟩ := p₂.subperm_left.2 s in ⟨l₁', p₁, s₁.trans s₂⟩ theorem length_le_of_subperm {l₁ l₂ : list α} : l₁ <+~ l₂ → length l₁ ≤ length l₂ | ⟨l, p, s⟩ := perm_length p ▸ length_le_of_sublist s theorem subperm.perm_of_length_le {l₁ l₂ : list α} : l₁ <+~ l₂ → length l₂ ≤ length l₁ → l₁ ~ l₂ | ⟨l, p, s⟩ h := suffices l = l₂, from this ▸ p.symm, eq_of_sublist_of_length_le s $ perm_length p.symm ▸ h theorem subperm.antisymm {l₁ l₂ : list α} (h₁ : l₁ <+~ l₂) (h₂ : l₂ <+~ l₁) : l₁ ~ l₂ := h₁.perm_of_length_le (length_le_of_subperm h₂) theorem subset_of_subperm {l₁ l₂ : list α} : l₁ <+~ l₂ → l₁ ⊆ l₂ | ⟨l, p, s⟩ := subset.trans (perm_subset p.symm) (subset_of_sublist s) end subperm theorem exists_perm_append_of_sublist : ∀ {l₁ l₂ : list α}, l₁ <+ l₂ → ∃ l, l₂ ~ l₁ ++ l | ._ ._ sublist.slnil := ⟨nil, perm.refl _⟩ | ._ ._ (sublist.cons l₁ l₂ a s) := let ⟨l, p⟩ := exists_perm_append_of_sublist s in ⟨a::l, (skip a p).trans perm_middle.symm⟩ | ._ ._ (sublist.cons2 l₁ l₂ a s) := let ⟨l, p⟩ := exists_perm_append_of_sublist s in ⟨l, skip a p⟩ theorem perm_countp (p : α → Prop) [decidable_pred p] {l₁ l₂ : list α} (s : l₁ ~ l₂) : countp p l₁ = countp p l₂ := by rw [countp_eq_length_filter, countp_eq_length_filter]; exact perm_length (perm_filter _ s) theorem countp_le_of_subperm (p : α → Prop) [decidable_pred p] {l₁ l₂ : list α} : l₁ <+~ l₂ → countp p l₁ ≤ countp p l₂ | ⟨l, p', s⟩ := perm_countp p p' ▸ countp_le_of_sublist s theorem perm_count [decidable_eq α] {l₁ l₂ : list α} (p : l₁ ~ l₂) (a) : count a l₁ = count a l₂ := perm_countp _ p theorem count_le_of_subperm [decidable_eq α] {l₁ l₂ : list α} (s : l₁ <+~ l₂) (a) : count a l₁ ≤ count a l₂ := countp_le_of_subperm _ s theorem foldl_eq_of_perm {f : β → α → β} {l₁ l₂ : list α} (rcomm : right_commutative f) (p : l₁ ~ l₂) : ∀ b, foldl f b l₁ = foldl f b l₂ := perm_induction_on p (λ b, rfl) (λ x t₁ t₂ p r b, r (f b x)) (λ x y t₁ t₂ p r b, by simp; rw rcomm; exact r (f (f b x) y)) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ b, eq.trans (r₁ b) (r₂ b)) theorem foldr_eq_of_perm {f : α → β → β} {l₁ l₂ : list α} (lcomm : left_commutative f) (p : l₁ ~ l₂) : ∀ b, foldr f b l₁ = foldr f b l₂ := perm_induction_on p (λ b, rfl) (λ x t₁ t₂ p r b, by simp; rw [r b]) (λ x y t₁ t₂ p r b, by simp; rw [lcomm, r b]) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ a, eq.trans (r₁ a) (r₂ a)) lemma rec_heq_of_perm {β : list α → Sort*} {f : Πa l, β l → β (a::l)} {b : β []} {l l' : list α} (hl : perm l l') (f_congr : ∀{a l l' b b'}, perm l l' → b == b' → f a l b == f a l' b') (f_swap : ∀{a a' l b}, f a (a'::l) (f a' l b) == f a' (a::l) (f a l b)) : @list.rec α β b f l == @list.rec α β b f l' := begin induction hl, case list.perm.nil { refl }, case list.perm.skip : a l l' h ih { exact f_congr h ih }, case list.perm.swap : a a' l { exact f_swap }, case list.perm.trans : l₁ l₂ l₃ h₁ h₂ ih₁ ih₂ { exact heq.trans ih₁ ih₂ } end section variables {op : α → α → α} [is_associative α op] [is_commutative α op] local notation a * b := op a b local notation l <*> a := foldl op a l lemma fold_op_eq_of_perm {l₁ l₂ : list α} {a : α} (h : l₁ ~ l₂) : l₁ <*> a = l₂ <*> a := foldl_eq_of_perm (right_comm _ (is_commutative.comm _) (is_associative.assoc _)) h _ end section comm_monoid open list variable [comm_monoid α] @[to_additive list.sum_eq_of_perm] lemma prod_eq_of_perm {l₁ l₂ : list α} (h : perm l₁ l₂) : prod l₁ = prod l₂ := by induction h; simp [*, mul_left_comm] @[to_additive list.sum_reverse] lemma prod_reverse (l : list α) : prod l.reverse = prod l := prod_eq_of_perm $ reverse_perm l end comm_monoid theorem perm_inv_core {a : α} {l₁ l₂ r₁ r₂ : list α} : l₁++a::r₁ ~ l₂++a::r₂ → l₁++r₁ ~ l₂++r₂ := begin generalize e₁ : l₁++a::r₁ = s₁, generalize e₂ : l₂++a::r₂ = s₂, intro p, revert l₁ l₂ r₁ r₂ e₁ e₂, refine perm_induction_on p _ (λ x t₁ t₂ p IH, _) (λ x y t₁ t₂ p IH, _) (λ t₁ t₂ t₃ p₁ p₂ IH₁ IH₂, _); intros l₁ l₂ r₁ r₂ e₁ e₂, { apply (not_mem_nil a).elim, rw ← e₁, simp }, { cases l₁ with y l₁; cases l₂ with z l₂; dsimp at e₁ e₂; injections; subst x, { substs t₁ t₂, exact p }, { substs z t₁ t₂, exact p.trans perm_middle }, { substs y t₁ t₂, exact perm_middle.symm.trans p }, { substs z t₁ t₂, exact skip y (IH rfl rfl) } }, { rcases l₁ with _|⟨y, _|⟨z, l₁⟩⟩; rcases l₂ with _|⟨u, _|⟨v, l₂⟩⟩; dsimp at e₁ e₂; injections; substs x y, { substs r₁ r₂, exact skip a p }, { substs r₁ r₂, exact skip u p }, { substs r₁ v t₂, exact skip u (p.trans perm_middle) }, { substs r₁ r₂, exact skip y p }, { substs r₁ r₂ y u, exact skip a p }, { substs r₁ u v t₂, exact (skip y $ p.trans perm_middle).trans (swap _ _ _) }, { substs r₂ z t₁, exact skip y (perm_middle.symm.trans p) }, { substs r₂ y z t₁, exact (swap _ _ _).trans (skip u $ perm_middle.symm.trans p) }, { substs u v t₁ t₂, exact (swap _ _ _).trans (skip z $ skip y $ IH rfl rfl) } }, { substs t₁ t₃, have : a ∈ t₂ := perm_subset p₁ (by simp), rcases mem_split this with ⟨l₂, r₂, e₂⟩, subst t₂, exact (IH₁ rfl rfl).trans (IH₂ rfl rfl) } end theorem perm_cons_inv {a : α} {l₁ l₂ : list α} : a::l₁ ~ a::l₂ → l₁ ~ l₂ := @perm_inv_core _ _ [] [] _ _ theorem perm_cons (a : α) {l₁ l₂ : list α} : a::l₁ ~ a::l₂ ↔ l₁ ~ l₂ := ⟨perm_cons_inv, skip a⟩ theorem perm_app_left_iff {l₁ l₂ : list α} : ∀ l, l++l₁ ~ l++l₂ ↔ l₁ ~ l₂ | [] := iff.rfl | (a::l) := (perm_cons a).trans (perm_app_left_iff l) theorem perm_app_right_iff {l₁ l₂ : list α} (l) : l₁++l ~ l₂++l ↔ l₁ ~ l₂ := ⟨λ p, (perm_app_left_iff _).1 $ trans perm_app_comm $ trans p perm_app_comm, perm_app_left _⟩ theorem subperm_cons (a : α) {l₁ l₂ : list α} : a::l₁ <+~ a::l₂ ↔ l₁ <+~ l₂ := ⟨λ ⟨l, p, s⟩, begin cases s with _ _ _ s' u _ _ s', { exact (p.subperm_left.2 $ subperm_of_sublist $ sublist_cons _ _).trans (subperm_of_sublist s') }, { exact ⟨u, perm_cons_inv p, s'⟩ } end, λ ⟨l, p, s⟩, ⟨a::l, skip a p, s.cons2 _ _ _⟩⟩ theorem cons_subperm_of_mem {a : α} {l₁ l₂ : list α} (d₁ : nodup l₁) (h₁ : a ∉ l₁) (h₂ : a ∈ l₂) (s : l₁ <+~ l₂) : a :: l₁ <+~ l₂ := begin rcases s with ⟨l, p, s⟩, induction s generalizing l₁, case list.sublist.slnil { cases h₂ }, case list.sublist.cons : r₁ r₂ b s' ih { simp at h₂, cases h₂ with e m, { subst b, exact ⟨a::r₁, skip a p, s'.cons2 _ _ _⟩ }, { rcases ih m d₁ h₁ p with ⟨t, p', s'⟩, exact ⟨t, p', s'.cons _ _ _⟩ } }, case list.sublist.cons2 : r₁ r₂ b s' ih { have bm : b ∈ l₁ := (perm_subset p $ mem_cons_self _ _), have am : a ∈ r₂ := h₂.resolve_left (λ e, h₁ $ e.symm ▸ bm), rcases mem_split bm with ⟨t₁, t₂, rfl⟩, have st : t₁ ++ t₂ <+ t₁ ++ b :: t₂ := by simp, rcases ih am (nodup_of_sublist st d₁) (mt (λ x, subset_of_sublist st x) h₁) (perm_cons_inv $ p.trans perm_middle) with ⟨t, p', s'⟩, exact ⟨b::t, (skip b p').trans $ (swap _ _ _).trans (skip a perm_middle.symm), s'.cons2 _ _ _⟩ } end theorem subperm_app_left {l₁ l₂ : list α} : ∀ l, l++l₁ <+~ l++l₂ ↔ l₁ <+~ l₂ | [] := iff.rfl | (a::l) := (subperm_cons a).trans (subperm_app_left l) theorem subperm_app_right {l₁ l₂ : list α} (l) : l₁++l <+~ l₂++l ↔ l₁ <+~ l₂ := (perm_app_comm.subperm_left.trans perm_app_comm.subperm_right).trans (subperm_app_left l) theorem subperm.exists_of_length_lt {l₁ l₂ : list α} : l₁ <+~ l₂ → length l₁ < length l₂ → ∃ a, a :: l₁ <+~ l₂ | ⟨l, p, s⟩ h := suffices length l < length l₂ → ∃ (a : α), a :: l <+~ l₂, from (this $ perm_length p.symm ▸ h).imp (λ a, (skip a p).subperm_right.1), begin clear subperm.exists_of_length_lt p h l₁, rename l₂ u, induction s with l₁ l₂ a s IH _ _ b s IH; intro h, { cases h }, { cases lt_or_eq_of_le (nat.le_of_lt_succ h : length l₁ ≤ length l₂) with h h, { exact (IH h).imp (λ a s, s.trans (subperm_of_sublist $ sublist_cons _ _)) }, { exact ⟨a, eq_of_sublist_of_length_eq s h ▸ subperm.refl _⟩ } }, { exact (IH $ nat.lt_of_succ_lt_succ h).imp (λ a s, (swap _ _ _).subperm_right.1 $ (subperm_cons _).2 s) } end theorem subperm_of_subset_nodup {l₁ l₂ : list α} (d : nodup l₁) (H : l₁ ⊆ l₂) : l₁ <+~ l₂ := begin induction d with a l₁' h d IH, { exact ⟨nil, perm.nil, nil_sublist _⟩ }, { cases forall_mem_cons.1 H with H₁ H₂, simp at h, exact cons_subperm_of_mem d h H₁ (IH H₂) } end theorem perm_ext {l₁ l₂ : list α} (d₁ : nodup l₁) (d₂ : nodup l₂) : l₁ ~ l₂ ↔ ∀a, a ∈ l₁ ↔ a ∈ l₂ := ⟨λ p a, mem_of_perm p, λ H, subperm.antisymm (subperm_of_subset_nodup d₁ (λ a, (H a).1)) (subperm_of_subset_nodup d₂ (λ a, (H a).2))⟩ theorem perm_ext_sublist_nodup {l₁ l₂ l : list α} (d : nodup l) (s₁ : l₁ <+ l) (s₂ : l₂ <+ l) : l₁ ~ l₂ ↔ l₁ = l₂ := ⟨λ h, begin induction s₂ with l₂ l a s₂ IH l₂ l a s₂ IH generalizing l₁, { exact eq_nil_of_perm_nil h.symm }, { simp at d, cases s₁ with _ _ _ s₁ l₁ _ _ s₁, { exact IH d.2 s₁ h }, { apply d.1.elim, exact subset_of_subperm ⟨_, h.symm, s₂⟩ (mem_cons_self _ _) } }, { simp at d, cases s₁ with _ _ _ s₁ l₁ _ _ s₁, { apply d.1.elim, exact subset_of_subperm ⟨_, h, s₁⟩ (mem_cons_self _ _) }, { rw IH d.2 s₁ (perm_cons_inv h) } } end, λ h, by rw h⟩ section variable [decidable_eq α] -- attribute [congr] theorem erase_perm_erase (a : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₁.erase a ~ l₂.erase a := if h₁ : a ∈ l₁ then have h₂ : a ∈ l₂, from perm_subset p h₁, perm_cons_inv $ trans (perm_erase h₁).symm $ trans p (perm_erase h₂) else have h₂ : a ∉ l₂, from mt (mem_of_perm p).2 h₁, by rw [erase_of_not_mem h₁, erase_of_not_mem h₂]; exact p theorem erase_subperm (a : α) (l : list α) : l.erase a <+~ l := ⟨l.erase a, perm.refl _, erase_sublist _ _⟩ theorem erase_subperm_erase {l₁ l₂ : list α} (a : α) (h : l₁ <+~ l₂) : l₁.erase a <+~ l₂.erase a := let ⟨l, hp, hs⟩ := h in ⟨l.erase a, erase_perm_erase _ hp, erase_sublist_erase _ hs⟩ theorem perm_diff_left {l₁ l₂ : list α} (t : list α) (h : l₁ ~ l₂) : l₁.diff t ~ l₂.diff t := by induction t generalizing l₁ l₂ h; simp [*, erase_perm_erase] theorem perm_diff_right (l : list α) {t₁ t₂ : list α} (h : t₁ ~ t₂) : l.diff t₁ = l.diff t₂ := by induction h generalizing l; simp [*, erase_perm_erase, erase_comm] <|> exact (ih_1 _).trans (ih_2 _) theorem subperm_cons_diff {a : α} : ∀ {l₁ l₂ : list α}, (a :: l₁).diff l₂ <+~ a :: l₁.diff l₂ | l₁ [] := ⟨a::l₁, by simp⟩ | l₁ (b::l₂) := begin repeat {rw diff_cons}, by_cases heq : a = b, { by_cases b ∈ l₁, { rw perm.subperm_right, apply subperm_cons_diff, simp [perm_diff_left, heq, perm_erase h] }, { simp [subperm_of_sublist, sublist.cons, h, heq] } }, { simp [heq, subperm_cons_diff] } end theorem subset_cons_diff {a : α} {l₁ l₂ : list α} : (a :: l₁).diff l₂ ⊆ a :: l₁.diff l₂ := subset_of_subperm subperm_cons_diff theorem perm_bag_inter_left {l₁ l₂ : list α} (t : list α) (h : l₁ ~ l₂) : l₁.bag_inter t ~ l₂.bag_inter t := begin induction h with x _ _ _ _ x y _ _ _ _ _ _ ih_1 ih_2 generalizing t, {simp}, { by_cases x ∈ t; simp [*, skip] }, { by_cases x = y, {simp [h]}, by_cases xt : x ∈ t; by_cases yt : y ∈ t, { simp [xt, yt, mem_erase_of_ne h, mem_erase_of_ne (ne.symm h), erase_comm, swap] }, { simp [xt, yt, mt mem_of_mem_erase, skip] }, { simp [xt, yt, mt mem_of_mem_erase, skip] }, { simp [xt, yt] } }, { exact (ih_1 _).trans (ih_2 _) } end theorem perm_bag_inter_right (l : list α) {t₁ t₂ : list α} (p : t₁ ~ t₂) : l.bag_inter t₁ = l.bag_inter t₂ := begin induction l with a l IH generalizing t₁ t₂ p, {simp}, by_cases a ∈ t₁, { simp [h, (mem_of_perm p).1 h, IH (erase_perm_erase _ p)] }, { simp [h, mt (mem_of_perm p).2 h, IH p] } end theorem cons_perm_iff_perm_erase {a : α} {l₁ l₂ : list α} : a::l₁ ~ l₂ ↔ a ∈ l₂ ∧ l₁ ~ l₂.erase a := ⟨λ h, have a ∈ l₂, from perm_subset h (mem_cons_self a l₁), ⟨this, perm_cons_inv $ h.trans $ perm_erase this⟩, λ ⟨m, h⟩, trans (skip a h) (perm_erase m).symm⟩ theorem perm_iff_count {l₁ l₂ : list α} : l₁ ~ l₂ ↔ ∀ a, count a l₁ = count a l₂ := ⟨perm_count, λ H, begin induction l₁ with a l₁ IH generalizing l₂, { cases l₂ with b l₂, {refl}, specialize H b, simp at H, contradiction }, { have : a ∈ l₂ := count_pos.1 (by rw ← H; simp; apply nat.succ_pos), refine trans (skip a $ IH $ λ b, _) (perm_erase this).symm, specialize H b, rw perm_count (perm_erase this) at H, by_cases b = a; simp [h] at H ⊢; assumption } end⟩ instance decidable_perm : ∀ (l₁ l₂ : list α), decidable (l₁ ~ l₂) | [] [] := is_true $ perm.refl _ | [] (b::l₂) := is_false $ λ h, by have := eq_nil_of_perm_nil h; contradiction | (a::l₁) l₂ := by haveI := decidable_perm l₁ (l₂.erase a); exact decidable_of_iff' _ cons_perm_iff_perm_erase -- @[congr] theorem perm_erase_dup_of_perm {l₁ l₂ : list α} (p : l₁ ~ l₂) : erase_dup l₁ ~ erase_dup l₂ := perm_iff_count.2 $ λ a, if h : a ∈ l₁ then by simp [nodup_erase_dup, h, perm_subset p h] else by simp [h, mt (mem_of_perm p).2 h] -- attribute [congr] theorem perm_insert (a : α) {l₁ l₂ : list α} (p : l₁ ~ l₂) : insert a l₁ ~ insert a l₂ := if h : a ∈ l₁ then by simpa [h, perm_subset p h] using p else by simpa [h, mt (mem_of_perm p).2 h] using skip a p theorem perm_insert_swap (x y : α) (l : list α) : insert x (insert y l) ~ insert y (insert x l) := begin by_cases xl : x ∈ l; by_cases yl : y ∈ l; simp [xl, yl], by_cases xy : x = y, { simp [xy] }, simp [not_mem_cons_of_ne_of_not_mem xy xl, not_mem_cons_of_ne_of_not_mem (ne.symm xy) yl], constructor end theorem perm_union_left {l₁ l₂ : list α} (t₁ : list α) (h : l₁ ~ l₂) : l₁ ∪ t₁ ~ l₂ ∪ t₁ := begin induction h with a _ _ _ ih _ _ _ _ _ _ _ _ ih_1 ih_2; try {simp}, { exact perm_insert a ih }, { apply perm_insert_swap }, { exact ih_1.trans ih_2 } end theorem perm_union_right (l : list α) {t₁ t₂ : list α} (h : t₁ ~ t₂) : l ∪ t₁ ~ l ∪ t₂ := by induction l; simp [*, perm_insert] -- @[congr] theorem perm_union {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁ ∪ t₁ ~ l₂ ∪ t₂ := trans (perm_union_left t₁ p₁) (perm_union_right l₂ p₂) theorem perm_inter_left {l₁ l₂ : list α} (t₁ : list α) : l₁ ~ l₂ → l₁ ∩ t₁ ~ l₂ ∩ t₁ := perm_filter _ theorem perm_inter_right (l : list α) {t₁ t₂ : list α} (p : t₁ ~ t₂) : l ∩ t₁ = l ∩ t₂ := by dsimp [(∩), list.inter]; congr; funext a; rw [mem_of_perm p] -- @[congr] theorem perm_inter {l₁ l₂ t₁ t₂ : list α} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : l₁ ∩ t₁ ~ l₂ ∩ t₂ := perm_inter_right l₂ p₂ ▸ perm_inter_left t₁ p₁ end theorem perm_pairwise {R : α → α → Prop} (S : symmetric R) : ∀ {l₁ l₂ : list α} (p : l₁ ~ l₂), pairwise R l₁ ↔ pairwise R l₂ := suffices ∀ {l₁ l₂}, l₁ ~ l₂ → pairwise R l₁ → pairwise R l₂, from λ l₁ l₂ p, ⟨this p, this p.symm⟩, λ l₁ l₂ p d, begin induction d with a l₁ h d IH generalizing l₂, { rw eq_nil_of_perm_nil p, constructor }, { have : a ∈ l₂ := perm_subset p (mem_cons_self _ _), rcases mem_split this with ⟨s₂, t₂, rfl⟩, have p' := perm_cons_inv (p.trans perm_middle), refine (pairwise_middle S).2 (pairwise_cons.2 ⟨λ b m, _, IH _ p'⟩), exact h _ (perm_subset p'.symm m) } end theorem perm_nodup {l₁ l₂ : list α} : l₁ ~ l₂ → (nodup l₁ ↔ nodup l₂) := perm_pairwise $ @ne.symm α theorem perm_bind_left {l₁ l₂ : list α} (f : α → list β) (p : l₁ ~ l₂) : l₁.bind f ~ l₂.bind f := begin induction p with a l₁ l₂ p IH a b l l₁ l₂ l₃ p₁ p₂ IH₁ IH₂, {simp}, { simp, exact perm_app_right _ IH }, { simp, rw [← append_assoc, ← append_assoc], exact perm_app_left _ perm_app_comm }, { exact trans IH₁ IH₂ } end theorem perm_bind_right (l : list α) {f g : α → list β} (h : ∀ a, f a ~ g a) : l.bind f ~ l.bind g := by induction l with a l IH; simp; exact perm_app (h a) IH theorem perm_product_left {l₁ l₂ : list α} (t₁ : list β) (p : l₁ ~ l₂) : product l₁ t₁ ~ product l₂ t₁ := perm_bind_left _ p theorem perm_product_right (l : list α) {t₁ t₂ : list β} (p : t₁ ~ t₂) : product l t₁ ~ product l t₂ := perm_bind_right _ $ λ a, perm_map _ p @[congr] theorem perm_product {l₁ l₂ : list α} {t₁ t₂ : list β} (p₁ : l₁ ~ l₂) (p₂ : t₁ ~ t₂) : product l₁ t₁ ~ product l₂ t₂ := trans (perm_product_left t₁ p₁) (perm_product_right l₂ p₂) theorem sublists_cons_perm_append (a : α) (l : list α) : sublists (a :: l) ~ sublists l ++ map (cons a) (sublists l) := begin simp [sublists, sublists_aux_cons_cons], refine skip _ ((skip _ _).trans perm_middle.symm), induction sublists_aux l cons with b l IH; simp, exact skip b ((skip _ IH).trans perm_middle.symm) end theorem sublists_perm_sublists' : ∀ l : list α, sublists l ~ sublists' l | [] := perm.refl _ | (a::l) := let IH := sublists_perm_sublists' l in by rw sublists'_cons; exact (sublists_cons_perm_append _ _).trans (perm_app IH (perm_map _ IH)) theorem revzip_sublists (l : list α) : ∀ l₁ l₂, (l₁, l₂) ∈ revzip l.sublists → l₁ ++ l₂ ~ l := begin rw revzip, apply list.reverse_rec_on l, { intros l₁ l₂ h, simp at h, simp [h] }, { intros l a IH l₁ l₂ h, rw [sublists_concat, reverse_append, zip_append, ← map_reverse, zip_map_right, zip_map_left] at h; [simp at h, simp], rcases h with ⟨l₁, l₂', h, rfl, rfl⟩ | ⟨l₁', l₂, h, rfl, rfl⟩, { rw ← append_assoc, exact perm_app_left _ (IH _ _ h) }, { rw append_assoc, apply (perm_app_right _ perm_app_comm).trans, rw ← append_assoc, exact perm_app_left _ (IH _ _ h) } } end theorem revzip_sublists' (l : list α) : ∀ l₁ l₂, (l₁, l₂) ∈ revzip l.sublists' → l₁ ++ l₂ ~ l := begin rw revzip, induction l with a l IH; intros l₁ l₂ h, { simp at h, simp [h] }, { rw [sublists'_cons, reverse_append, zip_append, ← map_reverse, zip_map_right, zip_map_left] at h; [simp at h, simp], rcases h with ⟨l₁, l₂', h, rfl, rfl⟩ | ⟨l₁', l₂, h, rfl, rfl⟩, { exact perm_middle.trans (skip _ (IH _ _ h)) }, { exact skip _ (IH _ _ h) } } end /- enumerating permutations -/ section permutations theorem 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] theorem permutations_aux2_snd_nil (t : α) (ts : list α) (r : list β) (f : list α → β) : (permutations_aux2 t ts r [] f).2 = r := rfl @[simp] theorem 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 theorem 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 * theorem 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; intro h, { rcases h with e | ⟨l₁, l₂, l0, ye, _⟩, { subst l', exact ⟨[], y::ys, by simp⟩ }, { substs l' ys, exact ⟨y::l₁, l₂, l0, by simp⟩ } }, { rcases h with ⟨_ | ⟨y', l₁⟩, l₂, l0, ye, rfl⟩, { simp [ye] }, { simp at ye, rcases ye with ⟨rfl, rfl⟩, exact or.inr ⟨l₁, l₂, l0, by simp⟩ } } } end theorem 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 theorem 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 * theorem 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}] theorem 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] theorem 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] theorem 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}, simp [ih (λ l m, H l (mem_cons_of_mem _ m)), H l (mem_cons_self _ _), mul_add] end theorem perm_of_mem_permutations_aux : ∀ {ts is l : list α}, l ∈ permutations_aux ts is → l ~ ts ++ is := begin refine permutations_aux.rec (by simp) _, introv IH1 IH2 m, rw [permutations_aux_cons, permutations, mem_foldr_permutations_aux2] at m, rcases m with m | ⟨l₁, l₂, m, _, e⟩, { exact (IH1 m).trans perm_middle }, { subst e, have p : l₁ ++ l₂ ~ is, { simp [permutations] at m, cases m with e m, {simp [e]}, exact is.append_nil ▸ IH2 m }, exact (perm_app_left _ (perm_middle.trans (skip _ p))).trans (skip _ perm_app_comm) } end theorem perm_of_mem_permutations {l₁ l₂ : list α} (h : l₁ ∈ permutations l₂) : l₁ ~ l₂ := (eq_or_mem_of_mem_cons h).elim (λ e, e ▸ perm.refl _) (λ m, append_nil l₂ ▸ perm_of_mem_permutations_aux m) theorem length_permutations_aux : ∀ ts is : list α, length (permutations_aux ts is) + is.length.fact = (length ts + length is).fact := begin refine permutations_aux.rec (by simp) _, intros t ts is IH1 IH2, have IH2 : length (permutations_aux is nil) + 1 = is.length.fact, { simpa using IH2 }, simp [-add_comm, nat.fact, nat.add_succ, mul_comm] at IH1, rw [permutations_aux_cons, length_foldr_permutations_aux2' _ _ _ _ _ (λ l m, perm_length (perm_of_mem_permutations m)), permutations, length, length, IH2, nat.succ_add, nat.fact_succ, mul_comm (nat.succ _), ← IH1, add_comm (_*_), add_assoc, nat.mul_succ, mul_comm] end theorem length_permutations (l : list α) : length (permutations l) = (length l).fact := length_permutations_aux l [] theorem mem_permutations_of_perm_lemma {is l : list α} (H : l ~ [] ++ is → (∃ ts' ~ [], l = ts' ++ is) ∨ l ∈ permutations_aux is []) : l ~ is → l ∈ permutations is := by simpa [permutations, perm_nil] using H theorem mem_permutations_aux_of_perm : ∀ {ts is l : list α}, l ~ is ++ ts → (∃ is' ~ is, l = is' ++ ts) ∨ l ∈ permutations_aux ts is := begin refine permutations_aux.rec (by simp) _, intros t ts is IH1 IH2 l p, rw [permutations_aux_cons, mem_foldr_permutations_aux2], rcases IH1 (p.trans perm_middle) with ⟨is', p', e⟩ | m, { clear p, subst e, rcases mem_split (perm_subset p'.symm (mem_cons_self _ _)) with ⟨l₁, l₂, e⟩, subst is', have p := perm_cons_inv (perm_middle.symm.trans p'), cases l₂ with a l₂', { exact or.inl ⟨l₁, by simpa using p⟩ }, { exact or.inr (or.inr ⟨l₁, a::l₂', mem_permutations_of_perm_lemma IH2 p, by simp⟩) } }, { exact or.inr (or.inl m) } end @[simp] theorem mem_permutations (s t : list α) : s ∈ permutations t ↔ s ~ t := ⟨perm_of_mem_permutations, mem_permutations_of_perm_lemma mem_permutations_aux_of_perm⟩ end permutations end list
34f853e542af15fd2db2ba973ae826ea08b16905
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/theories/finite_group_theory/action.lean
fe044a84a639dc701a37bb7992a0a5182ceb72ee
[ "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
22,566
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ import algebra.group data .hom .perm .finsubg namespace group_theory open finset function local attribute perm.f [coercion] private lemma and_left_true {a b : Prop} (Pa : a) : a ∧ b ↔ b := by rewrite [iff_true_intro Pa, true_and] section def variables {G S : Type} [group G] [fintype S] definition is_fixed_point (hom : G → perm S) (H : finset G) (a : S) : Prop := ∀ h, h ∈ H → hom h a = a variables [decidable_eq S] definition orbit (hom : G → perm S) (H : finset G) (a : S) : finset S := image (move_by a) (image hom H) definition fixed_points [reducible] (hom : G → perm S) (H : finset G) : finset S := {a ∈ univ | orbit hom H a = '{a}} variable [decidable_eq G] -- required by {x ∈ H |p x} filtering definition moverset (hom : G → perm S) (H : finset G) (a b : S) : finset G := {f ∈ H | hom f a = b} definition stab (hom : G → perm S) (H : finset G) (a : S) : finset G := {f ∈ H | hom f a = a} end def section orbit_stabilizer variables {G S : Type} [group G] [decidable_eq G] [fintype S] [decidable_eq S] section variables {hom : G → perm S} {H : finset G} {a : S} [Hom : is_hom_class hom] include Hom lemma exists_of_orbit {b : S} : b ∈ orbit hom H a → ∃ h, h ∈ H ∧ hom h a = b := assume Pb, obtain p (Pp₁ : p ∈ image hom H) (Pp₂ : move_by a p = b), from exists_of_mem_image Pb, obtain h (Ph₁ : h ∈ H) (Ph₂ : hom h = p), from exists_of_mem_image Pp₁, have Phab : hom h a = b, from calc hom h a = p a : Ph₂ ... = b : Pp₂, exists.intro h (and.intro Ph₁ Phab) lemma orbit_of_exists {b : S} : (∃ h, h ∈ H ∧ hom h a = b) → b ∈ orbit hom H a := assume Pex, obtain h PinH Phab, from Pex, mem_image (mem_image_of_mem hom PinH) Phab lemma is_fixed_point_of_mem_fixed_points : a ∈ fixed_points hom H → is_fixed_point hom H a := assume Pain, take h, assume Phin, eq_of_mem_singleton (of_mem_sep Pain ▸ orbit_of_exists (exists.intro h (and.intro Phin rfl))) lemma mem_fixed_points_of_exists_of_is_fixed_point : (∃ h, h ∈ H) → is_fixed_point hom H a → a ∈ fixed_points hom H := assume Pex Pfp, mem_sep_of_mem !mem_univ (ext take x, iff.intro (assume Porb, obtain h Phin Pha, from exists_of_orbit Porb, by rewrite [mem_singleton_iff, -Pha, Pfp h Phin]) (obtain h Phin, from Pex, by rewrite mem_singleton_iff; intro Peq; rewrite Peq; apply orbit_of_exists; existsi h; apply and.intro Phin (Pfp h Phin))) lemma is_fixed_point_iff_mem_fixed_points_of_exists : (∃ h, h ∈ H) → (a ∈ fixed_points hom H ↔ is_fixed_point hom H a) := assume Pex, iff.intro is_fixed_point_of_mem_fixed_points (mem_fixed_points_of_exists_of_is_fixed_point Pex) lemma is_fixed_point_iff_mem_fixed_points [finsubgH : is_finsubg H] : a ∈ fixed_points hom H ↔ is_fixed_point hom H a := is_fixed_point_iff_mem_fixed_points_of_exists (exists.intro 1 !finsubg_has_one) lemma is_fixed_point_of_one : is_fixed_point hom ('{1}) a := take h, assume Ph, by rewrite [eq_of_mem_singleton Ph, hom_map_one] lemma fixed_points_of_one : fixed_points hom ('{1}) = univ := ext take s, iff.intro (assume Pl, mem_univ s) (assume Pr, mem_fixed_points_of_exists_of_is_fixed_point (exists.intro 1 !mem_singleton) is_fixed_point_of_one) open fintype lemma card_fixed_points_of_one : card (fixed_points hom ('{1})) = card S := by rewrite [fixed_points_of_one] end -- these are already specified by stab hom H a variables {hom : G → perm S} {H : finset G} {a : S} variable [Hom : is_hom_class hom] include Hom lemma perm_f_mul (f g : G): perm.f ((hom f) * (hom g)) a = ((hom f) ∘ (hom g)) a := rfl lemma stab_lmul {f g : G} : g ∈ stab hom H a → hom (f*g) a = hom f a := assume Pgstab, have hom g a = a, from of_mem_sep Pgstab, calc hom (f*g) a = perm.f ((hom f) * (hom g)) a : is_hom hom ... = ((hom f) ∘ (hom g)) a : by rewrite perm_f_mul ... = (hom f) a : by unfold comp; rewrite this lemma stab_subset : stab hom H a ⊆ H := begin apply subset_of_forall, intro f Pfstab, apply mem_of_mem_sep Pfstab end lemma reverse_move {h g : G} : g ∈ moverset hom H a (hom h a) → hom (h⁻¹*g) a = a := assume Pg, have hom g a = hom h a, from of_mem_sep Pg, calc hom (h⁻¹*g) a = perm.f ((hom h⁻¹) * (hom g)) a : by rewrite (is_hom hom) ... = ((hom h⁻¹) ∘ hom g) a : by rewrite perm_f_mul ... = perm.f ((hom h)⁻¹ * hom h) a : by unfold comp; rewrite [this, perm_f_mul, hom_map_inv hom h] ... = perm.f (1 : perm S) a : by rewrite (mul.left_inv (hom h)) ... = a : by esimp lemma moverset_inj_on_orbit : set.inj_on (moverset hom H a) (ts (orbit hom H a)) := take b1 b2, assume Pb1, obtain h1 Ph1₁ Ph1₂, from exists_of_orbit Pb1, have Ph1b1 : h1 ∈ moverset hom H a b1, from mem_sep_of_mem Ph1₁ Ph1₂, assume Psetb2 Pmeq, begin subst b1, rewrite Pmeq at Ph1b1, apply of_mem_sep Ph1b1 end variable [finsubgH : is_finsubg H] include finsubgH lemma subg_stab_of_move {h g : G} : h ∈ H → g ∈ moverset hom H a (hom h a) → h⁻¹*g ∈ stab hom H a := assume Ph Pg, have Phinvg : h⁻¹*g ∈ H, from begin apply finsubg_mul_closed H, apply finsubg_has_inv H, assumption, apply mem_of_mem_sep Pg end, mem_sep_of_mem Phinvg (reverse_move Pg) lemma subg_stab_closed : finset_mul_closed_on (stab hom H a) := take f g, assume Pfstab, have Pf : hom f a = a, from of_mem_sep Pfstab, assume Pgstab, have Pfg : hom (f*g) a = a, from calc hom (f*g) a = (hom f) a : stab_lmul Pgstab ... = a : Pf, have PfginH : (f*g) ∈ H, from finsubg_mul_closed H (mem_of_mem_sep Pfstab) (mem_of_mem_sep Pgstab), mem_sep_of_mem PfginH Pfg lemma subg_stab_has_one : 1 ∈ stab hom H a := have P : hom 1 a = a, from calc hom 1 a = perm.f (1 : perm S) a : {hom_map_one hom} ... = a : rfl, have PoneinH : 1 ∈ H, from finsubg_has_one H, mem_sep_of_mem PoneinH P lemma subg_stab_has_inv : finset_has_inv (stab hom H a) := take f, assume Pfstab, have Pf : hom f a = a, from of_mem_sep Pfstab, have Pfinv : hom f⁻¹ a = a, from calc hom f⁻¹ a = hom f⁻¹ ((hom f) a) : by rewrite Pf ... = perm.f ((hom f⁻¹) * (hom f)) a : by rewrite perm_f_mul ... = hom (f⁻¹ * f) a : by rewrite (is_hom hom) ... = hom 1 a : by rewrite mul.left_inv ... = perm.f (1 : perm S) a : by rewrite (hom_map_one hom), have PfinvinH : f⁻¹ ∈ H, from finsubg_has_inv H (mem_of_mem_sep Pfstab), mem_sep_of_mem PfinvinH Pfinv definition subg_stab_is_finsubg [instance] : is_finsubg (stab hom H a) := is_finsubg.mk subg_stab_has_one subg_stab_closed subg_stab_has_inv lemma subg_lcoset_eq_moverset {h : G} : h ∈ H → fin_lcoset (stab hom H a) h = moverset hom H a (hom h a) := assume Ph, ext (take g, iff.intro (assume Pl, obtain f (Pf₁ : f ∈ stab hom H a) (Pf₂ : h*f = g), from exists_of_mem_image Pl, have Pfstab : hom f a = a, from of_mem_sep Pf₁, have PginH : g ∈ H, begin subst Pf₂, apply finsubg_mul_closed H, assumption, apply mem_of_mem_sep Pf₁ end, have Pga : hom g a = hom h a, from calc hom g a = hom (h*f) a : by subst g ... = hom h a : stab_lmul Pf₁, mem_sep_of_mem PginH Pga) (assume Pr, begin rewrite [↑fin_lcoset, mem_image_iff], existsi h⁻¹*g, split, exact subg_stab_of_move Ph Pr, apply mul_inv_cancel_left end)) lemma subg_moverset_of_orbit_is_lcoset_of_stab (b : S) : b ∈ orbit hom H a → ∃ h, h ∈ H ∧ fin_lcoset (stab hom H a) h = moverset hom H a b := assume Porb, obtain p (Pp₁ : p ∈ image hom H) (Pp₂ : move_by a p = b), from exists_of_mem_image Porb, obtain h (Ph₁ : h ∈ H) (Ph₂ : hom h = p), from exists_of_mem_image Pp₁, have Phab : hom h a = b, from by subst p; assumption, exists.intro h (and.intro Ph₁ (Phab ▸ subg_lcoset_eq_moverset Ph₁)) lemma subg_lcoset_of_stab_is_moverset_of_orbit (h : G) : h ∈ H → ∃ b, b ∈ orbit hom H a ∧ moverset hom H a b = fin_lcoset (stab hom H a) h := assume Ph, have Pha : (hom h a) ∈ orbit hom H a, by apply mem_image_of_mem; apply mem_image_of_mem; exact Ph, exists.intro (hom h a) (and.intro Pha (eq.symm (subg_lcoset_eq_moverset Ph))) lemma subg_moversets_of_orbit_eq_stab_lcosets : image (moverset hom H a) (orbit hom H a) = fin_lcosets (stab hom H a) H := ext (take s, iff.intro (assume Pl, obtain b Pb₁ Pb₂, from exists_of_mem_image Pl, obtain h Ph, from subg_moverset_of_orbit_is_lcoset_of_stab b Pb₁, begin rewrite [↑fin_lcosets, mem_image_eq], existsi h, subst Pb₂, assumption end) (assume Pr, obtain h Ph₁ Ph₂, from exists_of_mem_image Pr, obtain b Pb, from @subg_lcoset_of_stab_is_moverset_of_orbit G S _ _ _ _ hom H a Hom _ h Ph₁, begin rewrite [mem_image_eq], existsi b, subst Ph₂, assumption end)) open nat theorem orbit_stabilizer_theorem : card H = card (orbit hom H a) * card (stab hom H a) := calc card H = card (fin_lcosets (stab hom H a) H) * card (stab hom H a) : lagrange_theorem stab_subset ... = card (image (moverset hom H a) (orbit hom H a)) * card (stab hom H a) : subg_moversets_of_orbit_eq_stab_lcosets ... = card (orbit hom H a) * card (stab hom H a) : card_image_eq_of_inj_on moverset_inj_on_orbit end orbit_stabilizer section orbit_partition variables {G S : Type} [group G] [decidable_eq G] [fintype S] [decidable_eq S] variables {hom : G → perm S} [Hom : is_hom_class hom] {H : finset G} [subgH : is_finsubg H] include Hom subgH lemma in_orbit_refl {a : S} : a ∈ orbit hom H a := mem_image (mem_image (finsubg_has_one H) (hom_map_one hom)) rfl lemma in_orbit_trans {a b c : S} : a ∈ orbit hom H b → b ∈ orbit hom H c → a ∈ orbit hom H c := assume Painb Pbinc, obtain h PhinH Phba, from exists_of_orbit Painb, obtain g PginH Pgcb, from exists_of_orbit Pbinc, orbit_of_exists (exists.intro (h*g) (and.intro (finsubg_mul_closed H PhinH PginH) (calc hom (h*g) c = perm.f ((hom h) * (hom g)) c : is_hom hom ... = ((hom h) ∘ (hom g)) c : by rewrite perm_f_mul ... = (hom h) b : Pgcb ... = a : Phba))) lemma in_orbit_symm {a b : S} : a ∈ orbit hom H b → b ∈ orbit hom H a := assume Painb, obtain h PhinH Phba, from exists_of_orbit Painb, have perm.f (hom h)⁻¹ a = b, by rewrite [-Phba, -perm_f_mul, mul.left_inv], have (hom h⁻¹) a = b, by rewrite [hom_map_inv, this], orbit_of_exists (exists.intro h⁻¹ (and.intro (finsubg_has_inv H PhinH) this)) lemma orbit_is_partition : is_partition (orbit hom H) := take a b, propext (iff.intro (assume Painb, obtain h PhinH Phba, from exists_of_orbit Painb, ext take c, iff.intro (assume Pcina, in_orbit_trans Pcina Painb) (assume Pcinb, obtain g PginH Pgbc, from exists_of_orbit Pcinb, in_orbit_trans Pcinb (in_orbit_symm Painb))) (assume Peq, Peq ▸ in_orbit_refl)) variables (hom) (H) open nat finset.partition fintype definition orbit_partition : @partition S _ := mk univ (orbit hom H) orbit_is_partition (restriction_imp_union (orbit hom H) orbit_is_partition (λ a Pa, !subset_univ)) definition orbits : finset (finset S) := equiv_classes (orbit_partition hom H) definition fixed_point_orbits : finset (finset S) := {cls ∈ orbits hom H | card cls = 1} variables {hom} {H} lemma exists_iff_mem_orbits (orb : finset S) : orb ∈ orbits hom H ↔ ∃ a : S, orbit hom H a = orb := begin esimp [orbits, equiv_classes, orbit_partition], rewrite [mem_image_iff], apply iff.intro, intro Pl, cases Pl with a Pa, rewrite (and_left_true !mem_univ) at Pa, existsi a, exact Pa, intro Pr, cases Pr with a Pa, rewrite -true_and at Pa, rewrite -(iff_true_intro (mem_univ a)) at Pa, existsi a, exact Pa end lemma exists_of_mem_orbits {orb : finset S} : orb ∈ orbits hom H → ∃ a : S, orbit hom H a = orb := iff.elim_left (exists_iff_mem_orbits orb) lemma fixed_point_orbits_eq : fixed_point_orbits hom H = image (orbit hom H) (fixed_points hom H) := ext take s, iff.intro (assume Pin, obtain Psin Ps, from iff.elim_left !mem_sep_iff Pin, obtain a Pa, from exists_of_mem_orbits Psin, mem_image (mem_sep_of_mem !mem_univ (eq.symm (eq_of_card_eq_of_subset (by rewrite [Pa, Ps]) (subset_of_forall take x, assume Pxin, eq_of_mem_singleton Pxin ▸ in_orbit_refl)))) Pa) (assume Pin, obtain a Pain Porba, from exists_of_mem_image Pin, mem_sep_of_mem (begin esimp [orbits, equiv_classes, orbit_partition], rewrite [mem_image_iff], existsi a, exact and.intro !mem_univ Porba end) (begin substvars, rewrite [of_mem_sep Pain] end)) lemma orbit_inj_on_fixed_points : set.inj_on (orbit hom H) (ts (fixed_points hom H)) := take a₁ a₂, begin rewrite [-*mem_eq_mem_to_set, ↑fixed_points, *mem_sep_iff], intro Pa₁ Pa₂, rewrite [and.right Pa₁, and.right Pa₂], exact eq_of_singleton_eq end lemma card_fixed_point_orbits_eq : card (fixed_point_orbits hom H) = card (fixed_points hom H) := by rewrite fixed_point_orbits_eq; apply card_image_eq_of_inj_on orbit_inj_on_fixed_points lemma orbit_class_equation : card S = Sum (orbits hom H) card := class_equation (orbit_partition hom H) lemma card_fixed_point_orbits : Sum (fixed_point_orbits hom H) card = card (fixed_point_orbits hom H) := calc Sum _ _ = Sum (fixed_point_orbits hom H) (λ x, 1) : Sum_ext (take c Pin, of_mem_sep Pin) ... = card (fixed_point_orbits hom H) * 1 : Sum_const_eq_card_mul ... = card (fixed_point_orbits hom H) : mul_one (card (fixed_point_orbits hom H)) local attribute nat.comm_semiring [instance] lemma orbit_class_equation' : card S = card (fixed_points hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card := calc card S = Sum (orbits hom H) finset.card : orbit_class_equation ... = Sum (fixed_point_orbits hom H) finset.card + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : Sum_binary_union ... = card (fixed_point_orbits hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : by rewrite -card_fixed_point_orbits ... = card (fixed_points hom H) + Sum {cls ∈ orbits hom H | card cls ≠ 1} card : by rewrite card_fixed_point_orbits_eq end orbit_partition section cayley variables {G : Type} [group G] [fintype G] definition action_by_lmul : G → perm G := take g, perm.mk (lmul_by g) (lmul_inj g) variable [decidable_eq G] lemma action_by_lmul_hom : homomorphic (@action_by_lmul G _ _) := take g₁ (g₂ : G), eq.symm (calc action_by_lmul g₁ * action_by_lmul g₂ = perm.mk ((lmul_by g₁)∘(lmul_by g₂)) _ : rfl ... = perm.mk (lmul_by (g₁*g₂)) _ : by congruence; apply coset.lmul_compose) lemma action_by_lmul_inj : injective (@action_by_lmul G _ _) := take g₁ g₂, assume Peq, perm.no_confusion Peq (λ Pfeq Pqeq, have Pappeq : g₁*1 = g₂*1, from congr_fun Pfeq _, calc g₁ = g₁ * 1 : mul_one ... = g₂ * 1 : Pappeq ... = g₂ : mul_one) definition action_by_lmul_is_iso [instance] : is_iso_class (@action_by_lmul G _ _) := is_iso_class.mk action_by_lmul_hom action_by_lmul_inj end cayley section lcosets open fintype subtype variables {G : Type} [group G] [fintype G] [decidable_eq G] variables H : finset G definition action_on_lcoset : G → perm (lcoset_type univ H) := take g, perm.mk (lcoset_lmul (mem_univ g)) lcoset_lmul_inj private definition lcoset_of (g : G) : lcoset_type univ H := tag (fin_lcoset H g) (exists.intro g (and.intro !mem_univ rfl)) variable {H} lemma action_on_lcoset_eq (g : G) (J : lcoset_type univ H) : elt_of (action_on_lcoset H g J) = fin_lcoset (elt_of J) g := rfl lemma action_on_lcoset_hom : homomorphic (action_on_lcoset H) := take g₁ g₂, eq_of_feq (funext take S, subtype.eq (by rewrite [↑action_on_lcoset, ↑lcoset_lmul, -fin_lcoset_compose])) definition action_on_lcoset_is_hom [instance] : is_hom_class (action_on_lcoset H) := is_hom_class.mk action_on_lcoset_hom variable [finsubgH : is_finsubg H] include finsubgH lemma aol_fixed_point_subset_normalizer (J : lcoset_type univ H) : is_fixed_point (action_on_lcoset H) H J → elt_of J ⊆ normalizer H := obtain j Pjin Pj, from exists_of_lcoset_type J, assume Pfp, have PH : ∀ {h}, h ∈ H → fin_lcoset (fin_lcoset H j) h = fin_lcoset H j, from take h, assume Ph, by rewrite [Pj, -action_on_lcoset_eq, Pfp h Ph], subset_of_forall take g, begin rewrite [-Pj, fin_lcoset_same, -inv_inv at {2}], intro Pg, rewrite -Pg at PH, apply finsubg_has_inv, apply mem_sep_of_mem !mem_univ, intro h Ph, have Phg : fin_lcoset (fin_lcoset H g) h = fin_lcoset H g, from PH Ph, revert Phg, rewrite [↑conj_by, inv_inv, mul.assoc, fin_lcoset_compose, -fin_lcoset_same, ↑fin_lcoset, mem_image_iff, ↑lmul_by], intro Pex, cases Pex with k Pand, cases Pand with Pkin Pk, rewrite [-Pk, inv_mul_cancel_left], exact Pkin end lemma aol_fixed_point_of_mem_normalizer {g : G} : g ∈ normalizer H → is_fixed_point (action_on_lcoset H) H (lcoset_of H g) := assume Pgin, take h, assume Phin, subtype.eq (by rewrite [action_on_lcoset_eq, ↑lcoset_of, lrcoset_same_of_mem_normalizer Pgin, fin_lrcoset_comm, finsubg_lcoset_id Phin]) lemma aol_fixed_points_eq_normalizer : Union (fixed_points (action_on_lcoset H) H) elt_of = normalizer H := ext take g, begin rewrite [mem_Union_iff], apply iff.intro, intro Pl, cases Pl with L PL, revert PL, rewrite [is_fixed_point_iff_mem_fixed_points], intro Pg, apply mem_of_subset_of_mem, apply aol_fixed_point_subset_normalizer L, exact and.left Pg, exact and.right Pg, intro Pr, existsi (lcoset_of H g), apply and.intro, rewrite [is_fixed_point_iff_mem_fixed_points], exact aol_fixed_point_of_mem_normalizer Pr, exact fin_mem_lcoset g end open nat lemma card_aol_fixed_points_eq_card_cosets : card (fixed_points (action_on_lcoset H) H) = card (lcoset_type (normalizer H) H) := have Peq : card (fixed_points (action_on_lcoset H) H) * card H = card (lcoset_type (normalizer H) H) * card H, from calc card _ * card H = card (Union (fixed_points (action_on_lcoset H) H) elt_of) : card_Union_lcosets ... = card (normalizer H) : aol_fixed_points_eq_normalizer ... = card (lcoset_type (normalizer H) H) * card H : lagrange_theorem' subset_normalizer, eq_of_mul_eq_mul_right (card_pos_of_mem !finsubg_has_one) Peq end lcosets section perm_fin open fin nat eq.ops variable {n : nat} definition lift_perm (p : perm (fin n)) : perm (fin (succ n)) := perm.mk (lift_fun p) (lift_fun_of_inj (perm.inj p)) definition lower_perm (p : perm (fin (succ n))) (P : p maxi = maxi) : perm (fin n) := perm.mk (lower_inj p (perm.inj p) P) (take i j, begin rewrite [-eq_iff_veq, *lower_inj_apply, eq_iff_veq], apply injective_comp (perm.inj p) lift_succ_inj end) lemma lift_lower_eq : ∀ {p : perm (fin (succ n))} (P : p maxi = maxi), lift_perm (lower_perm p P) = p | (perm.mk pf Pinj) := assume Pmax, begin rewrite [↑lift_perm], congruence, apply funext, intro i, have Pfmax : pf maxi = maxi, by apply Pmax, have Pd : decidable (i = maxi), from _, cases Pd with Pe Pne, rewrite [Pe, Pfmax], apply lift_fun_max, rewrite [lift_fun_of_ne_max Pne, ↑lower_perm, ↑lift_succ], rewrite [-eq_iff_veq, -val_lift, lower_inj_apply, eq_iff_veq], congruence, rewrite [-eq_iff_veq] end lemma lift_perm_inj : injective (@lift_perm n) := take p1 p2, assume Peq, eq_of_feq (lift_fun_inj (feq_of_eq Peq)) lemma lift_perm_inj_on_univ : set.inj_on (@lift_perm n) (ts univ) := eq.symm to_set_univ ▸ iff.elim_left set.injective_iff_inj_on_univ lift_perm_inj lemma lift_to_stab : image (@lift_perm n) univ = stab id univ maxi := ext (take (pp : perm (fin (succ n))), iff.intro (assume Pimg, obtain p P_ Pp, from exists_of_mem_image Pimg, have Ppp : pp maxi = maxi, from calc pp maxi = lift_perm p maxi : {eq.symm Pp} ... = lift_fun p maxi : rfl ... = maxi : lift_fun_max, mem_sep_of_mem !mem_univ Ppp) (assume Pstab, have Ppp : pp maxi = maxi, from of_mem_sep Pstab, mem_image !mem_univ (lift_lower_eq Ppp))) definition move_from_max_to (i : fin (succ n)) : perm (fin (succ n)) := perm.mk (madd (i - maxi)) madd_inj lemma orbit_max : orbit (@id (perm (fin (succ n)))) univ maxi = univ := ext (take i, iff.intro (assume P, !mem_univ) (assume P, begin apply mem_image, apply mem_image, apply mem_univ (move_from_max_to i), apply rfl, apply sub_add_cancel end)) lemma card_orbit_max : card (orbit (@id (perm (fin (succ n)))) univ maxi) = succ n := calc card (orbit (@id (perm (fin (succ n)))) univ maxi) = card univ : by rewrite orbit_max ... = succ n : card_fin (succ n) open fintype lemma card_lift_to_stab : card (stab (@id (perm (fin (succ n)))) univ maxi) = card (perm (fin n)) := calc finset.card (stab (@id (perm (fin (succ n)))) univ maxi) = finset.card (image (@lift_perm n) univ) : by rewrite lift_to_stab ... = card univ : by rewrite (card_image_eq_of_inj_on lift_perm_inj_on_univ) lemma card_perm_step : card (perm (fin (succ n))) = (succ n) * card (perm (fin n)) := calc card (perm (fin (succ n))) = card (orbit id univ maxi) * card (stab id univ maxi) : orbit_stabilizer_theorem ... = (succ n) * card (stab id univ maxi) : {card_orbit_max} ... = (succ n) * card (perm (fin n)) : by rewrite -card_lift_to_stab end perm_fin end group_theory
ea02a0a4c0e85f3c3281144ef9cc81fa34a91109
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/preserves/shapes/biproducts.lean
d98231a3610dcea8b058a8f608b0f777eb87a510
[ "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
15,241
lean
/- Copyright (c) 2022 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.shapes.biproducts import category_theory.limits.preserves.shapes.zero /-! # Preservation of biproducts > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We define the image of a (binary) bicone under a functor that preserves zero morphisms and define classes `preserves_biproduct` and `preserves_binary_biproduct`. We then * show that a functor that preserves biproducts of a two-element type preserves binary biproducts, * construct the comparison morphisms between the image of a biproduct and the biproduct of the images and show that the biproduct is preserved if one of them is an isomorphism, * give the canonical isomorphism between the image of a biproduct and the biproduct of the images in case that the biproduct is preserved. -/ universes w₁ w₂ v₁ v₂ u₁ u₂ noncomputable theory open category_theory open category_theory.limits namespace category_theory variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] section has_zero_morphisms variables [has_zero_morphisms C] [has_zero_morphisms D] namespace functor section map variables (F : C ⥤ D) [preserves_zero_morphisms F] section bicone variables {J : Type w₁} /-- The image of a bicone under a functor. -/ @[simps] def map_bicone {f : J → C} (b : bicone f) : bicone (F.obj ∘ f) := { X := F.obj b.X, π := λ j, F.map (b.π j), ι := λ j, F.map (b.ι j), ι_π := λ j j', begin rw ← F.map_comp, split_ifs, { subst h, simp only [bicone_ι_π_self, category_theory.functor.map_id, eq_to_hom_refl] }, { rw [bicone_ι_π_ne _ h, F.map_zero] } end } lemma map_bicone_whisker {K : Type w₂} {g : K ≃ J} {f : J → C} (c : bicone f) : F.map_bicone (c.whisker g) = (F.map_bicone c).whisker g := rfl end bicone /-- The image of a binary bicone under a functor. -/ @[simps] def map_binary_bicone {X Y : C} (b : binary_bicone X Y) : binary_bicone (F.obj X) (F.obj Y) := { X := F.obj b.X, fst := F.map b.fst, snd := F.map b.snd, inl := F.map b.inl, inr := F.map b.inr, inl_fst' := by rw [← F.map_comp, b.inl_fst, F.map_id], inl_snd' := by rw [← F.map_comp, b.inl_snd, F.map_zero], inr_fst' := by rw [← F.map_comp, b.inr_fst, F.map_zero], inr_snd' := by rw [← F.map_comp, b.inr_snd, F.map_id] } end map end functor open category_theory.functor namespace limits section bicone variables {J : Type w₁} {K : Type w₂} /-- A functor `F` preserves biproducts of `f` if `F` maps every bilimit bicone over `f` to a bilimit bicone over `F.obj ∘ f`. -/ class preserves_biproduct (f : J → C) (F : C ⥤ D) [preserves_zero_morphisms F] := (preserves : Π {b : bicone f}, b.is_bilimit → (F.map_bicone b).is_bilimit) /-- A functor `F` preserves biproducts of `f` if `F` maps every bilimit bicone over `f` to a bilimit bicone over `F.obj ∘ f`. -/ def is_bilimit_of_preserves {f : J → C} (F : C ⥤ D) [preserves_zero_morphisms F] [preserves_biproduct f F] {b : bicone f} (hb : b.is_bilimit) : (F.map_bicone b).is_bilimit := preserves_biproduct.preserves hb variables (J) /-- A functor `F` preserves biproducts of shape `J` if it preserves biproducts of `f` for every `f : J → C`. -/ class preserves_biproducts_of_shape (F : C ⥤ D) [preserves_zero_morphisms F] := (preserves : Π {f : J → C}, preserves_biproduct f F) attribute [instance, priority 100] preserves_biproducts_of_shape.preserves end bicone /-- A functor `F` preserves finite biproducts if it preserves biproducts of shape `J` whenever `J` is a fintype. -/ class preserves_finite_biproducts (F : C ⥤ D) [preserves_zero_morphisms F] := (preserves : Π {J : Type} [fintype J], preserves_biproducts_of_shape J F) attribute [instance, priority 100] preserves_finite_biproducts.preserves /-- A functor `F` preserves biproducts if it preserves biproducts of any shape `J` of size `w`. The usual notion of preservation of biproducts is recovered by choosing `w` to be the universe of the morphisms of `C`. -/ class preserves_biproducts (F : C ⥤ D) [preserves_zero_morphisms F] := (preserves : Π {J : Type w₁}, preserves_biproducts_of_shape J F) attribute [instance, priority 100] preserves_biproducts.preserves /-- Preserving biproducts at a bigger universe level implies preserving biproducts at a smaller universe level. -/ def preserves_biproducts_shrink (F : C ⥤ D) [preserves_zero_morphisms F] [hp : preserves_biproducts.{max w₁ w₂} F] : preserves_biproducts.{w₁} F := ⟨λ J, ⟨λ f, ⟨λ b ib, ((F.map_bicone b).whisker_is_bilimit_iff _).to_fun (is_bilimit_of_preserves F ((b.whisker_is_bilimit_iff equiv.ulift.{w₂}).inv_fun ib))⟩⟩⟩ @[priority 100] instance preserves_finite_biproducts_of_preserves_biproducts (F : C ⥤ D) [preserves_zero_morphisms F] [preserves_biproducts.{w₁} F] : preserves_finite_biproducts F := { preserves := λ J _, by letI := preserves_biproducts_shrink.{0} F; apply_instance } /-- A functor `F` preserves binary biproducts of `X` and `Y` if `F` maps every bilimit bicone over `X` and `Y` to a bilimit bicone over `F.obj X` and `F.obj Y`. -/ class preserves_binary_biproduct (X Y : C) (F : C ⥤ D) [preserves_zero_morphisms F] := (preserves : Π {b : binary_bicone X Y}, b.is_bilimit → (F.map_binary_bicone b).is_bilimit) /-- A functor `F` preserves binary biproducts of `X` and `Y` if `F` maps every bilimit bicone over `X` and `Y` to a bilimit bicone over `F.obj X` and `F.obj Y`. -/ def is_binary_bilimit_of_preserves {X Y : C} (F : C ⥤ D) [preserves_zero_morphisms F] [preserves_binary_biproduct X Y F] {b : binary_bicone X Y} (hb : b.is_bilimit) : (F.map_binary_bicone b).is_bilimit := preserves_binary_biproduct.preserves hb /-- A functor `F` preserves binary biproducts if it preserves the binary biproduct of `X` and `Y` for all `X` and `Y`. -/ class preserves_binary_biproducts (F : C ⥤ D) [preserves_zero_morphisms F] := (preserves : Π {X Y : C}, preserves_binary_biproduct X Y F . tactic.apply_instance) /-- A functor that preserves biproducts of a pair preserves binary biproducts. -/ def preserves_binary_biproduct_of_preserves_biproduct (F : C ⥤ D) [preserves_zero_morphisms F] (X Y : C) [preserves_biproduct (pair_function X Y) F] : preserves_binary_biproduct X Y F := { preserves := λ b hb, { is_limit := is_limit.of_iso_limit ((is_limit.postcompose_hom_equiv (by exact diagram_iso_pair _) _).symm ((is_bilimit_of_preserves F (b.to_bicone_is_bilimit.symm hb)).is_limit)) $ cones.ext (iso.refl _) (λ j, by { rcases j with ⟨⟨⟩⟩, tidy, }), is_colimit := is_colimit.of_iso_colimit ((is_colimit.precompose_inv_equiv (by exact diagram_iso_pair _ ) _).symm ((is_bilimit_of_preserves F (b.to_bicone_is_bilimit.symm hb)).is_colimit)) $ cocones.ext (iso.refl _) (λ j, by { rcases j with ⟨⟨⟩⟩, tidy, }) } } /-- A functor that preserves biproducts of a pair preserves binary biproducts. -/ def preserves_binary_biproducts_of_preserves_biproducts (F : C ⥤ D) [preserves_zero_morphisms F] [preserves_biproducts_of_shape walking_pair F] : preserves_binary_biproducts F := { preserves := λ X Y, preserves_binary_biproduct_of_preserves_biproduct F X Y } attribute [instance, priority 100] preserves_binary_biproducts.preserves end limits open category_theory.limits namespace functor section bicone variables {J : Type w₁} (F : C ⥤ D) (f : J → C) [has_biproduct f] section variables [has_biproduct (F.obj ∘ f)] /-- As for products, any functor between categories with biproducts gives rise to a morphism `F.obj (⨁ f) ⟶ ⨁ (F.obj ∘ f)`. -/ def biproduct_comparison : F.obj (⨁ f) ⟶ ⨁ (F.obj ∘ f) := biproduct.lift (λ j, F.map (biproduct.π f j)) @[simp, reassoc] lemma biproduct_comparison_π (j : J) : biproduct_comparison F f ≫ biproduct.π _ j = F.map (biproduct.π f j) := biproduct.lift_π _ _ /-- As for coproducts, any functor between categories with biproducts gives rise to a morphism `⨁ (F.obj ∘ f) ⟶ F.obj (⨁ f)` -/ def biproduct_comparison' : ⨁ (F.obj ∘ f) ⟶ F.obj (⨁ f) := biproduct.desc (λ j, F.map (biproduct.ι f j)) @[simp, reassoc] lemma ι_biproduct_comparison' (j : J) : biproduct.ι _ j ≫ biproduct_comparison' F f = F.map (biproduct.ι f j) := biproduct.ι_desc _ _ variables [preserves_zero_morphisms F] /-- The composition in the opposite direction is equal to the identity if and only if `F` preserves the biproduct, see `preserves_biproduct_of_mono_biproduct_comparison`. -/ @[simp, reassoc] lemma biproduct_comparison'_comp_biproduct_comparison : biproduct_comparison' F f ≫ biproduct_comparison F f = 𝟙 (⨁ (F.obj ∘ f)) := by { classical, ext, simp [biproduct.ι_π, ← functor.map_comp, eq_to_hom_map] } /-- `biproduct_comparison F f` is a split epimorphism. -/ @[simps] def split_epi_biproduct_comparison : split_epi (biproduct_comparison F f) := ⟨biproduct_comparison' F f⟩ instance : is_split_epi (biproduct_comparison F f) := is_split_epi.mk' (split_epi_biproduct_comparison F f) /-- `biproduct_comparison' F f` is a split monomorphism. -/ @[simps] def split_mono_biproduct_comparison' : split_mono (biproduct_comparison' F f) := ⟨biproduct_comparison F f⟩ instance : is_split_mono (biproduct_comparison' F f) := is_split_mono.mk' (split_mono_biproduct_comparison' F f) end variables [preserves_zero_morphisms F] [preserves_biproduct f F] instance has_biproduct_of_preserves : has_biproduct (F.obj ∘ f) := has_biproduct.mk { bicone := F.map_bicone (biproduct.bicone f), is_bilimit := preserves_biproduct.preserves (biproduct.is_bilimit _) } /-- If `F` preserves a biproduct, we get a definitionally nice isomorphism `F.obj (⨁ f) ≅ ⨁ (F.obj ∘ f)`. -/ @[simp] def map_biproduct : F.obj (⨁ f) ≅ ⨁ (F.obj ∘ f) := biproduct.unique_up_to_iso _ (preserves_biproduct.preserves (biproduct.is_bilimit _)) lemma map_biproduct_hom : (map_biproduct F f).hom = biproduct.lift (λ j, F.map (biproduct.π f j)) := rfl lemma map_biproduct_inv : (map_biproduct F f).inv = biproduct.desc (λ j, F.map (biproduct.ι f j)) := rfl end bicone variables (F : C ⥤ D) (X Y : C) [has_binary_biproduct X Y] section variables [has_binary_biproduct (F.obj X) (F.obj Y)] /-- As for products, any functor between categories with binary biproducts gives rise to a morphism `F.obj (X ⊞ Y) ⟶ F.obj X ⊞ F.obj Y`. -/ def biprod_comparison : F.obj (X ⊞ Y) ⟶ F.obj X ⊞ F.obj Y := biprod.lift (F.map biprod.fst) (F.map biprod.snd) @[simp, reassoc] lemma biprod_comparison_fst : biprod_comparison F X Y ≫ biprod.fst = F.map biprod.fst := biprod.lift_fst _ _ @[simp, reassoc] lemma biprod_comparison_snd : biprod_comparison F X Y ≫ biprod.snd = F.map biprod.snd := biprod.lift_snd _ _ /-- As for coproducts, any functor between categories with binary biproducts gives rise to a morphism `F.obj X ⊞ F.obj Y ⟶ F.obj (X ⊞ Y)`. -/ def biprod_comparison' : F.obj X ⊞ F.obj Y ⟶ F.obj (X ⊞ Y) := biprod.desc (F.map biprod.inl) (F.map biprod.inr) @[simp, reassoc] lemma inl_biprod_comparison' : biprod.inl ≫ biprod_comparison' F X Y = F.map biprod.inl := biprod.inl_desc _ _ @[simp, reassoc] lemma inr_biprod_comparison' : biprod.inr ≫ biprod_comparison' F X Y = F.map biprod.inr := biprod.inr_desc _ _ variables [preserves_zero_morphisms F] /-- The composition in the opposite direction is equal to the identity if and only if `F` preserves the biproduct, see `preserves_binary_biproduct_of_mono_biprod_comparison`. -/ @[simp, reassoc] lemma biprod_comparison'_comp_biprod_comparison : biprod_comparison' F X Y ≫ biprod_comparison F X Y = 𝟙 (F.obj X ⊞ F.obj Y) := by { ext; simp [← functor.map_comp] } /-- `biprod_comparison F X Y` is a split epi. -/ @[simps] def split_epi_biprod_comparison : split_epi (biprod_comparison F X Y) := ⟨biprod_comparison' F X Y⟩ instance : is_split_epi (biprod_comparison F X Y) := is_split_epi.mk' (split_epi_biprod_comparison F X Y) /-- `biprod_comparison' F X Y` is a split mono. -/ @[simps] def split_mono_biprod_comparison' : split_mono (biprod_comparison' F X Y) := ⟨biprod_comparison F X Y⟩ instance : is_split_mono (biprod_comparison' F X Y) := is_split_mono.mk' (split_mono_biprod_comparison' F X Y) end variables [preserves_zero_morphisms F] [preserves_binary_biproduct X Y F] instance has_binary_biproduct_of_preserves : has_binary_biproduct (F.obj X) (F.obj Y) := has_binary_biproduct.mk { bicone := F.map_binary_bicone (binary_biproduct.bicone X Y), is_bilimit := preserves_binary_biproduct.preserves (binary_biproduct.is_bilimit _ _) } /-- If `F` preserves a binary biproduct, we get a definitionally nice isomorphism `F.obj (X ⊞ Y) ≅ F.obj X ⊞ F.obj Y`. -/ @[simp] def map_biprod : F.obj (X ⊞ Y) ≅ F.obj X ⊞ F.obj Y := biprod.unique_up_to_iso _ _ (preserves_binary_biproduct.preserves (binary_biproduct.is_bilimit _ _)) lemma map_biprod_hom : (map_biprod F X Y).hom = biprod.lift (F.map biprod.fst) (F.map biprod.snd) := rfl lemma map_biprod_inv : (map_biprod F X Y).inv = biprod.desc (F.map biprod.inl) (F.map biprod.inr) := rfl end functor namespace limits variables (F : C ⥤ D) [preserves_zero_morphisms F] section bicone variables {J : Type w₁} (f : J → C) [has_biproduct f] [preserves_biproduct f F] {W : C} lemma biproduct.map_lift_map_biprod (g : Π j, W ⟶ f j) : F.map (biproduct.lift g) ≫ (F.map_biproduct f).hom = biproduct.lift (λ j, F.map (g j)) := by { ext, simp [← F.map_comp] } lemma biproduct.map_biproduct_inv_map_desc (g : Π j, f j ⟶ W) : (F.map_biproduct f).inv ≫ F.map (biproduct.desc g) = biproduct.desc (λ j, F.map (g j)) := by { ext, simp [← F.map_comp] } lemma biproduct.map_biproduct_hom_desc (g : Π j, f j ⟶ W) : (F.map_biproduct f).hom ≫ biproduct.desc (λ j, F.map (g j)) = F.map (biproduct.desc g) := by rw [← biproduct.map_biproduct_inv_map_desc, iso.hom_inv_id_assoc] end bicone section binary_bicone variables (X Y : C) [has_binary_biproduct X Y] [preserves_binary_biproduct X Y F] {W : C} lemma biprod.map_lift_map_biprod (f : W ⟶ X) (g : W ⟶ Y) : F.map (biprod.lift f g) ≫ (F.map_biprod X Y).hom = biprod.lift (F.map f) (F.map g) := by ext; simp [← F.map_comp] lemma biprod.lift_map_biprod (f : W ⟶ X) (g : W ⟶ Y) : biprod.lift (F.map f) (F.map g) ≫ (F.map_biprod X Y).inv = F.map (biprod.lift f g) := by rw [← biprod.map_lift_map_biprod, category.assoc, iso.hom_inv_id, category.comp_id] lemma biprod.map_biprod_inv_map_desc (f : X ⟶ W) (g : Y ⟶ W) : (F.map_biprod X Y).inv ≫ F.map (biprod.desc f g) = biprod.desc (F.map f) (F.map g) := by ext; simp [← F.map_comp] lemma biprod.map_biprod_hom_desc (f : X ⟶ W) (g : Y ⟶ W) : (F.map_biprod X Y).hom ≫ biprod.desc (F.map f) (F.map g) = F.map (biprod.desc f g) := by rw [← biprod.map_biprod_inv_map_desc, iso.hom_inv_id_assoc] end binary_bicone end limits end has_zero_morphisms end category_theory
9ad0a059c6a7b602e3cb73477bc06bc455bc9462
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world5/level4.lean
bde858cd54118c08c53e7f4805cf71c5798575b9
[ "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,997
lean
/- Tactic : apply ## Summary If `h : P → Q` is a hypothesis, and the goal is `⊢ Q` then `apply h` changes the goal to `⊢ P`. ## Details If you have a function `h : P → Q` and your goal is `⊢ Q` then `apply h` changes the goal to `⊢ P`. The logic is simple: if you are trying to create a term of type `Q`, but `h` is a function which turns terms of type `P` into terms of type `Q`, then it will suffice to construct a term of type `P`. A mathematician might say: "we need to construct an element of $Q$, but we have a function $h:P\to Q$ so it suffices to construct an element of $P$". Or alternatively "we need to prove $Q$, but we have a proof $h$ that $P\implies Q$ so it suffices to prove $P$". -/ /- # Function world. ## Level 4: the `apply` tactic. Let's do the same level again: ![diagram](https://wwwf.imperial.ac.uk/~buzzard/xena/natural_number_game_images/function_diag.jpg) We are given $p \in P$ and our goal is to find an element of $U$, or in other words to find a path through the maze that links $P$ to $U$. In level 3 we solved this by using `have`s to move forward, from $P$ to $Q$ to $T$ to $U$. Using the `apply` tactic we can instead construct the path backwards, moving from $U$ to $T$ to $Q$ to $P$. Our goal is to construct an element of the set $U$. But $l:T\to U$ is a function, so it would suffice to construct an element of $T$. Tell Lean this by starting the proof below with `apply l,` and notice that our assumptions don't change but *the goal changes* from `⊢ U` to `⊢ T`. Keep `apply`ing functions until your goal is `P`, and try not to get lost! Now solve this goal with `exact p`. Note: you will need to learn the difference between `exact p` (which works) and `exact P` (which doesn't, because $P$ is not an element of $P$). -/ /- Definition Given an element of $P$ we can define an element of $U$. -/ example (P Q R S T U: Type) (p : P) (h : P → Q) (i : Q → R) (j : Q → T) (k : S → T) (l : T → U) : U := begin end
f992cc3ff72450b6fae203a6d49cdb6f4b8e6731
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Binary/Apply.lean
8260a899b493873b7cef1d77f8d47c9f12ae76c5
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
18,701
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Daniel Selsam -/ import Lean import Mathport.Util.Misc import Mathport.Bridge.Config import Mathport.Binary.Basic import Mathport.Binary.NDRec import Mathport.Binary.EnvModification import Mathport.Binary.TranslateName import Mathport.Binary.TranslateExpr namespace Mathport.Binary open Lean Lean.Meta Lean.Elab Lean.Elab.Command def inCurrentModule [Monad M] [MonadEnv M] (n : Name) : M Bool := return (← getEnv).getModuleIdxFor? n |>.isNone def printIndType (lps : List Name) (indType : InductiveType) : BinportM Unit := do println! "[inductive] {indType.name}.\{{lps}}. : {indType.type}" for ctor in indType.ctors do println! " {ctor.name} : {ctor.type}" def printDeclDebug (decl : Declaration) : BinportM Unit := do match decl with | Declaration.thmDecl thm => println! "--- {thm.name} ---" println! "[type] {thm.type}" println! "[value] {thm.value}" | _ => pure () def stubValue : Declaration → MetaM Declaration | .thmDecl val => return .thmDecl { val with value := ← mkSorry val.type true } | .defnDecl val => return .opaqueDecl { val with value := ← mkSorry val.type true, isUnsafe := false } | .mutualDefnDecl defns => .mutualDefnDecl <$> defns.mapM fun defn => return { defn with value := ← mkSorry defn.type true } | d => pure d def stubType : Declaration → MetaM Declaration | .axiomDecl val => return .axiomDecl { val with type := mkPUnit (← inferTypeD val.type) } | .thmDecl val => do stubValue (.thmDecl { val with type := mkPUnit (← inferTypeD val.type) }) | .defnDecl val => do stubValue (.defnDecl { val with type := mkPUnit (← inferTypeD val.type) }) | .opaqueDecl val => return .opaqueDecl { val with type := mkPUnit (← inferTypeD val.type) } | .mutualDefnDecl defns => do stubValue <| .mutualDefnDecl <|← defns.mapM fun defn => return { defn with type := mkPUnit (← inferTypeD defn.type) } | .inductDecl lp _ tys uns => (.inductDecl lp 0 · uns) <$> tys.mapM fun ty => return { ty with type := ← try forallTelescopeReducing ty.type fun _ => pure catch _ => pure (.sort (.succ .zero)) ctors := ← ty.ctors.mapM fun ctor => return { ctor with type := .const ty.name (lp.map .param) } } | d => pure d where inferTypeD ty := try inferType ty catch _ => pure (.sort .zero) mkPUnit tyty := let u := match tyty with | .sort lvl => lvl | _ => .zero mkConst ``PUnit [u] def addDecl (decl : Declaration) : CoreM Unit := withMaxHeartbeat (50000 * 1000) do Lean.addDecl decl def refineAddDecl (decl : Declaration) : BinportM (Declaration × ClashKind) := do let path := (← read).path println! "[addDecl] START REFINE {path.mod3} {decl.toName}" let ⟨decl, clashKind⟩ ← refineLean4NamesAndUpdateMap decl match clashKind with | ClashKind.found "" => println! "[addDecl] FOUND DEF-EQ {path.mod3} {decl.toName}" | ClashKind.found _ => println! "[addDecl] FOUND DUBIOUS {path.mod3} {decl.toName}" | ClashKind.freshDecl => println! "[addDecl] START CHECK {path.mod3} {decl.toName}" try if (← read).config.error2warning && decl matches .thmDecl .. then throwError "skipping proof of theorem" liftCoreM do addDecl decl catch ex => println! "[kernel] {← ex.toMessageData.toString}" if (← read).config.error2warning then -- printDeclDebug decl try println! "[addDecl] stubbing value of {decl.toName}" let decl ← liftMetaM <| stubValue decl liftCoreM do addDecl decl catch _ => println! "[addDecl] stubbing type of {decl.toName}" let decl ← liftMetaM <| stubType decl try liftCoreM do addDecl decl catch ex => println! "[addDecl] failed to port {decl.toName}" throw ex else throw ex modifyEnv fun env => binportTag.ext.addEntry env decl.toName println! "[addDecl] END CHECK {path.mod3} {decl.toName}" if shouldGenCodeFor decl then println! "[compile] {decl.toName} START" match (← getEnv).compileDecl {} decl with | Except.ok env => println! "[compile] {decl.toName} SUCCESS!" setEnv env | Except.error err => let msg := err.toMessageData (← getOptions) let msg ← msg.toString println! "[compile] {decl.toName} {msg}" pure (decl, clashKind) where shouldGenCodeFor (decl : Declaration) : Bool := -- TODO: sadly, noncomputable comes after the definition -- (so if this isn't good enough, we will need to refactor) match decl with | Declaration.defnDecl _ => false -- https://github.com/leanprover-community/mathport/issues/172 | _ => false def setAttr (attr : Attribute) (declName : Name) : BinportM Unit := do let env ← getEnv match getAttributeImpl env attr.name with | Except.error errMsg => throwError errMsg | Except.ok attrImpl => liftMetaM $ attrImpl.add declName attr.stx attr.kind def equationFor? (n : Name) : Option Name := let n₁ : Name := n.getPrefix if n₁.isStr && n₁.getString! == "equations" then some n₁.getPrefix else none def maybeRegisterEquation (eqn : Name) : BinportM Unit := do -- example: list.nth.equations._eqn_1 match equationFor? eqn with | some defn => modify λ s => { s with name2equations := s.name2equations.insertWith (· ++ ·) defn [eqn] } | none => pure () def applyExport (d : ExportDecl) : BinportM Unit := do -- we use the variable names of elabExport if not d.exceptNames.isEmpty then warnStr s!"export of {d.ns} with exceptions is ignored" else if d.nsAs != Name.anonymous then warnStr s!"export of {d.ns} with 'nsAs' is ignored" else if ¬ d.hadExplicit then warnStr s!"export of {d.ns} with no explicits is ignored" else let mut env ← getEnv for (n1, n2) in d.renames do -- TODO: naive name translation doesn't work for the alias -- We should probably inspect the suffixes and modify/remove the prefixes -- env := addAlias env (← lookupLean4Name n1) (← lookupLean4Name n2) println! "[export] SKIP {n1} := {n2}" continue setEnv env def applyMixfix (kind : MixfixKind) (n : Name) (prec : Nat) (tok : String) : BinportM Unit := do try let n ← lookupNameExt! n -- For now, we avoid the `=` `=` clash by making all Mathlib notations -- lower priority than the Lean4 ones. let prio : Nat := (← liftMacroM <| evalOptPrio none).pred let stxPrec : Syntax.Prec := Quote.quote prec let stxName : Option Syntax.Ident := none let stxPrio : Option Syntax.Prio := some (quote prio) let stxOp : TSyntax strLitKind := Syntax.mkStrLit tok let stxFun : Syntax.Term := mkIdent n let stx ← match kind with | MixfixKind.infixl => `(infixl:$stxPrec $[(name := $stxName)]? $[(priority := $stxPrio)]? $stxOp => $stxFun) | MixfixKind.infixr => `(infixr:$stxPrec $[(name := $stxName)]? $[(priority := $stxPrio)]? $stxOp => $stxFun) | MixfixKind.prefix => `(prefix:$stxPrec $[(name := $stxName)]? $[(priority := $stxPrio)]? $stxOp => $stxFun) | MixfixKind.postfix => `(postfix:$stxPrec $[(name := $stxName)]? $[(priority := $stxPrio)]? $stxOp => $stxFun) | MixfixKind.singleton => let correctPrec : Option Syntax.Prec := some (quote Parser.maxPrec) `(notation $[: $correctPrec]? $[(name := $stxName)]? $[(priority := $stxPrio)]? $stxOp:str => $stxFun) let nextIdx : Nat := (← get).nNotations modify λ s => { s with nNotations := nextIdx + 1 } let ns : Syntax.Ident := mkIdent s!"{"__".intercalate ((← read).path.mod4.components.map Name.getString!)}_{nextIdx}" let stx ← `(namespace $ns $stx end $ns) elabCommand stx catch ex => warn ex def applySimpLemma (n : Name) (prio : Nat) : BinportM Unit := do -- TODO: remove these once https://github.com/leanprover-community/mathlib/pull/8738 (+ friends) are merged let badSimps := #[`set.eq_on_empty, `punit.eq_punit, `list.cons_injective, `list.length_injective, `list.reverse_injective] if badSimps.contains n then return () tryAddSimpLemma (← lookupNameExt! n) prio for eqn in (← get).name2equations.findD n [] do tryAddSimpLemma (← lookupNameExt! eqn) prio where tryAddSimpLemma (n : Name) (prio : Nat) : BinportM Unit := try liftMetaM $ addSimpTheorem (ext := simpExtension) (declName := n) (post := True) (inv := False) (attrKind := AttributeKind.global) (prio := prio) println! "[simp] {n} {prio}" catch ex => warn ex def applyReducibility (n : Name) (status : ReducibilityStatus) : BinportM Unit := do -- (note: this will fail/no-op if it declares reducible in a new module) try setAttr { name := reducibilityToName status } (← lookupNameExt! n) catch ex => warn ex where reducibilityToName (status : ReducibilityStatus) : Name := match status with | ReducibilityStatus.reducible => `reducible | ReducibilityStatus.semireducible => `semireducible | ReducibilityStatus.irreducible => `irreducible def applyProjection (proj : ProjectionInfo) : BinportM Unit := do try -- we lookup names inside `try`, because meta things may have been skipped let projName ← lookupNameExt! proj.projName let ctorName ← lookupNameExt! proj.ctorName let structName := ctorName.getPrefix unless ← inCurrentModule structName do return setEnv $ addProjectionFnInfo (← getEnv) projName ctorName proj.nParams proj.index proj.fromClass let descr := (← get).structures.findD structName ⟨structName, #[]⟩ match (← getEnv).find? ctorName with | some (ConstantInfo.ctorInfo ctor) => let fieldInfo ← mkFieldInfo ctor.numParams ctor.type projName proj.projName modify fun s => { s with structures := s.structures.insert structName ⟨descr.structName, descr.fields.push fieldInfo⟩ } | _ => warnStr "projection for something other than constructor {projName}, {ctorName}" catch ex => warn ex where mkFieldInfo (numParams : Nat) (ctorType : Expr) (projName projName3 : Name) : BinportM StructureFieldInfo := do match projName, projName3 with | Name.str _ fieldName .., Name.str _ fieldName3 .. => pure { fieldName projFn := projName subobject? := getSubobject? numParams ctorType fieldName3 -- TODO: what to put here? binderInfo := BinderInfo.default } | _, _ => throwError "unexpected projName with num field: {projName}" getSubobject? (numParams : Nat) (type : Expr) (fieldName3 : String) : Option Name := Id.run do -- Note: we do not translate binder names, so we need the *lean3* fieldName here let candidateName := "_" ++ fieldName3 let mut type := type let mut i := 0 while type.isForall do if i ≥ numParams then match type.bindingName! with | Name.str Name.anonymous s .. => if s == candidateName then return some type.bindingDomain!.getAppFn.constName! | _ => pure () type := type.bindingBody! i := i + 1 return none def applyClass (n : Name) : BinportM Unit := do -- (for meta classes, Lean4 won't know about the decl) try match addClass (← getEnv) (← lookupNameExt! n) with | Except.error msg => warnStr msg | Except.ok env => setEnv env catch ex => warn ex def applyInstance (_nc ni : Name) (prio : Nat) : BinportM Unit := do -- (for meta instances, Lean4 won't know about the decl) -- TODO: `prio.pred`? if (← read).config.disabledInstances.contains ni then return () try liftMetaM $ addInstance (← lookupNameExt! ni) AttributeKind.global prio setAttr { name := `infer_tc_goals_rl } (← lookupNameExt! ni) catch ex => warn ex def applyAxiomVal (ax : AxiomVal) : BinportM Unit := do let (decl, clashKind) ← refineAddDecl $ Declaration.axiomDecl { ax with type := (← trExpr ax.type) } if clashKind == ClashKind.freshDecl then maybeRegisterEquation decl.toName def applyTheoremVal (thm : TheoremVal) : BinportM Unit := do let (decl, clashKind) ← refineAddDecl $ Declaration.thmDecl { thm with type := (← trExpr thm.type), value := (← trExpr thm.value) } if clashKind == ClashKind.freshDecl then maybeRegisterEquation decl.toName def applyDefinitionVal (defn : DefinitionVal) : BinportM Unit := do if ← isBadSUnfold3 defn.name then return () let mut hints := defn.hints let mut forceAbbrev := false if (← read).config.forceAbbrevs.contains defn.name then hints := ReducibilityHints.abbrev forceAbbrev := true -- Only for definitions (really, instances) do we try to translate the coes let type ← trExpr defn.type let value ← trExpr defn.value discard <| refineAddDecl $ Declaration.defnDecl { defn with type := type value := value hints := hints } if forceAbbrev then applyReducibility defn.name ReducibilityStatus.reducible where isBadSUnfold3 (n3 : Name) : BinportM Bool := do if !n3.isStr then return false if n3.getString! != "_sunfold" then return false let pfix4 ← lookupNameExt! n3.getPrefix match (← getEnv).find? (pfix4 ++ `_main) with | some cinfo => match cinfo.value? with -- bad means the original function isn't actually recursive | some v => pure $ Option.isNone $ v.find? fun e => e.isConst && e.constName!.isStr && e.constName!.getString! == "brec_on" | _ => throwError "should have value" | _ => return false /- this can happen when e.g. `nat.add._main -> Nat.add` (which may be needed due to eqn lemmas) -/ def applyInductiveDecl (lps : List Name) (nParams : Nat) (indType : InductiveType) (isUnsafe : Bool) : BinportM Unit := do -- The `Module` inductive type includes `module` in its constructor types, which gets mapped to `Module`, causing confusion. -- In the past, we worked around this by changing `module` -> `ModuleS`, but this is highly undesirable. -- Now, we simple first change all the `Module` names to `_indSelf`, then change `_indSelf` later. let indType := indType.replaceSelfWithPlaceholder let ind? := some (indType.name, indType.type, lps) let decl := Declaration.inductDecl lps nParams [{ indType with type := (← trExpr indType.type), ctors := (← indType.ctors.mapM fun ctor => do pure { ctor with type := (← trExpr ctor.type (ind? := ind?)) }) }] isUnsafe let (decl, clashKind) ← refineAddDecl decl if clashKind == ClashKind.freshDecl then mkAuxDecls decl.toName match ← liftMetaM $ mkNDRec decl.toName (indType.name ++ `ndrec /- old name -/) with | some ndRec => do -- TODO: this will create a spurious alignment, and will *miss* the alignment `eq.rec` -> `Eq.ndrec` -- For now, we just add the missing alignment manually let (ndRecDecl, clashKind) ← refineAddDecl ndRec addNameAlignment (indType.name ++ `rec) ndRecDecl.toName if clashKind == ClashKind.freshDecl then setAttr { name := `reducible } ndRecDecl.toName | none => pure () where mkAuxDecls (name : Name) : BinportM Unit := do try -- these may fail for the invalid inductive types currently being accepted -- by the temporary patch https://github.com/dselsam/lean4/commit/1bef1cb3498cf81f93095bda16ed8bc65af42535 mkRecOn name mkCasesOn name liftMetaM $ Lean.mkNoConfusion name mkBelow name mkIBelow name mkBRecOn name mkBInductionOn name catch _ => pure () def applyPosition (n : Name) (line col : Nat) : BinportM Unit := do let range := DeclarationRanges.mk { pos := { line := line, column := col }, charUtf16 := col, endPos := { line := line, column := col }, endCharUtf16 := col } { pos := { line := line, column := col }, charUtf16 := col, endPos := { line := line, column := col }, endCharUtf16 := col} if let some n ← lookupNameExt n then if ← inCurrentModule n then Lean.addDeclarationRanges n range def applyToAdditive (src tgt : Name) : BinportM Unit := do let src4 ← lookupNameExt! src let tgt4 ← match ← lookupNameExt tgt with | some tgt => pure tgt | none => let n4 ← mkCandidateLean4Name tgt ((← getEnv).find? src4).get!.type addNameAlignment tgt n4 (synthetic := true) pure n4 if let some tgt' := ToAdditive.findTranslation? (← getEnv) src4 then if tgt' != tgt4 then println! "[applyToAdditive] ignoring to_additive {src4} => {tgt4} incompatible with {tgt'}" else liftCoreM $ ToAdditive.insertTranslation src4 tgt4 def applyModification (mod : EnvModification) : BinportM Unit := withReader (fun ctx => { ctx with currDecl := mod.toName }) do println! "[apply] {mod}" match mod with | EnvModification.mixfix .. -- synport handles notation | EnvModification.private .. | EnvModification.protected .. | EnvModification.position .. => pure () | EnvModification.export d => applyExport d | EnvModification.simp n prio => applySimpLemma n prio | EnvModification.reducibility n kind => applyReducibility n kind | EnvModification.projection proj => applyProjection proj | EnvModification.class n => applyClass n | EnvModification.instance nc ni prio => applyInstance nc ni prio | EnvModification.toAdditive src tgt => applyToAdditive src tgt | EnvModification.decl d => match d with | Declaration.axiomDecl ax => applyAxiomVal ax | Declaration.thmDecl thm => applyTheoremVal thm | Declaration.defnDecl defn => applyDefinitionVal defn | Declaration.inductDecl lps nps [ind] iu => applyInductiveDecl lps nps ind iu | _ => throwError "unexpected declaration type" def applyModificationPost (mod : EnvModification) : BinportM Unit := do match mod with | EnvModification.position n line col => applyPosition n line col | _ => pure () def postprocessModule : BinportM Unit := do registerStructures where registerStructures := do for (structName, structDescr) in (← get).structures.toList do modifyEnv fun env => registerStructure env structDescr println! "[registerStructure] {structName}" for { fieldName, projFn, subobject?, .. } in structDescr.fields do println! "[registerStructure.field] {structName} {fieldName} {projFn} {subobject?}" end Mathport.Binary
8c407f7832d22d9fe07db1ec32749255e3903af2
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/ACltBug.lean
50f9337833af5916cbcca1b403ea5a67df65a47d
[ "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
454
lean
attribute [local simp] Nat.mul_comm Nat.mul_assoc Nat.mul_left_comm attribute [local simp] Nat.add_assoc Nat.add_comm Nat.add_left_comm example (w x y z : Nat) (p : Nat → Prop) (h : p (x * y + z * w * x)) : p (x * w * z + y * x) := by simp at *; assumption example (x y z : Nat) (p : Nat → Prop) (h₁ : p (1 * x + y)) (h₂ : p (x * z * 1)) : p (y + 0 + x) ∧ p (z * x) := by simp at * <;> constructor <;> assumption
25b2475161bf9c6eaa58c93b6690c8dd9a8ecc8b
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/sets_functions_and_relations/unnamed_110.lean
ac521b06af9f89f8dea5262505ab4f7fcc520d1a
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
167
lean
variable {α : Type*} variables (s t u : set α) -- BEGIN example (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := begin intros x xsu, exact ⟨h xsu.1, xsu.2⟩ end -- END
81a5c6da59ec18a6a7434b9c5b950fb503c35578
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/test/amc12a-2002-p13.lean
29fbe1b4c25e680e593d0cbc3d60a84e8159ba4f
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
350
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.real.basic import data.real.sqrt example (a b : ℝ) (h₀ : 0 < a ∧ 0 < b) (h₁ : a ≠ b) (h₂ : abs (a - 1/a) = 1 ) (h₃ : abs (b - 1/b) = 1) : a + b = real.sqrt 5 := begin sorry end
15c6cd0df25d3c3fb934ad0e074555388ef49177
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/group_theory/group_action/defs.lean
919f5becf0d84ff0727810235a72516200f0875e
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,195
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.equiv.basic import algebra.group.defs import algebra.group.hom import logic.embedding /-! # Definitions of group actions This file defines a hierarchy of group action type-classes: * `has_scalar α β` * `mul_action α β` * `distrib_mul_action α β` The hierarchy is extended further by `semimodule`, defined elsewhere. Also provided are type-classes regarding the interaction of different group actions, * `smul_comm_class M N α` * `is_scalar_tower M N α` ## Notation `a • b` is used as notation for `smul a b`. ## Implementation details This file should avoid depending on other parts of `group_theory`, to avoid import cycles. More sophisticated lemmas belong in `group_theory.group_action`. -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open function /-- Typeclass for types with a scalar multiplication operation, denoted `•` (`\bu`) -/ class has_scalar (α : Type u) (γ : Type v) := (smul : α → γ → γ) infixr ` • `:73 := has_scalar.smul /-- Typeclass for multiplicative actions by monoids. This generalizes group actions. -/ @[protect_proj] class mul_action (α : Type u) (β : Type v) [monoid α] extends has_scalar α β := (one_smul : ∀ b : β, (1 : α) • b = b) (mul_smul : ∀ (x y : α) (b : β), (x * y) • b = x • y • b) /-- A typeclass mixin saying that two actions on the same space commute. -/ class smul_comm_class (M N α : Type*) [has_scalar M α] [has_scalar N α] : Prop := (smul_comm : ∀ (m : M) (n : N) (a : α), m • n • a = n • m • a) export mul_action (mul_smul) smul_comm_class (smul_comm) /-- Frequently, we find ourselves wanting to express a bilinear map `M →ₗ[R] N →ₗ[R] P` or an equivalence between maps `(M →ₗ[R] N) ≃ₗ[R] (M' →ₗ[R] N')` where the maps have an associated ring `R`. Unfortunately, using definitions like these requires that `R` satisfy `comm_semiring R`, and not just `semiring R`. Using `M →ₗ[R] N →+ P` and `(M →ₗ[R] N) ≃+ (M' →ₗ[R] N')` avoids this problem, but throws away structure that is useful for when we _do_ have a commutative (semi)ring. To avoid making this compromise, we instead state these definitions as `M →ₗ[R] N →ₗ[S] P` or `(M →ₗ[R] N) ≃ₗ[S] (M' →ₗ[R] N')` and require `smul_comm_class S R` on the appropriate modules. When the caller has `comm_semiring R`, they can set `S = R` and `smul_comm_class_self` will populate the instance. If the caller only has `semiring R` they can still set either `R = ℕ` or `S = ℕ`, and `add_comm_monoid.nat_smul_comm_class` or `add_comm_monoid.nat_smul_comm_class'` will populate the typeclass, which is still sufficient to recover a `≃+` or `→+` structure. An example of where this is used is `linear_map.prod_equiv`. -/ library_note "bundled maps over different rings" /-- Commutativity of actions is a symmetric relation. This lemma can't be an instance because this would cause a loop in the instance search graph. -/ lemma smul_comm_class.symm (M N α : Type*) [has_scalar M α] [has_scalar N α] [smul_comm_class M N α] : smul_comm_class N M α := ⟨λ a' a b, (smul_comm a a' b).symm⟩ instance smul_comm_class_self (M α : Type*) [comm_monoid M] [mul_action M α] : smul_comm_class M M α := ⟨λ a a' b, by rw [← mul_smul, mul_comm, mul_smul]⟩ /-- An instance of `is_scalar_tower M N α` states that the multiplicative action of `M` on `α` is determined by the multiplicative actions of `M` on `N` and `N` on `α`. -/ class is_scalar_tower (M N α : Type*) [has_scalar M N] [has_scalar N α] [has_scalar M α] : Prop := (smul_assoc : ∀ (x : M) (y : N) (z : α), (x • y) • z = x • (y • z)) @[simp] lemma smul_assoc {M N} [has_scalar M N] [has_scalar N α] [has_scalar M α] [is_scalar_tower M N α] (x : M) (y : N) (z : α) : (x • y) • z = x • y • z := is_scalar_tower.smul_assoc x y z section variables [monoid α] [mul_action α β] lemma smul_smul (a₁ a₂ : α) (b : β) : a₁ • a₂ • b = (a₁ * a₂) • b := (mul_smul _ _ _).symm variable (α) @[simp] theorem one_smul (b : β) : (1 : α) • b = b := mul_action.one_smul _ variables {α} /-- Pullback a multiplicative action along an injective map respecting `•`. -/ protected def function.injective.mul_action [has_scalar α γ] (f : γ → β) (hf : injective f) (smul : ∀ (c : α) x, f (c • x) = c • f x) : mul_action α γ := { smul := (•), one_smul := λ x, hf $ (smul _ _).trans $ one_smul _ (f x), mul_smul := λ c₁ c₂ x, hf $ by simp only [smul, mul_smul] } /-- Pushforward a multiplicative action along a surjective map respecting `•`. -/ protected def function.surjective.mul_action [has_scalar α γ] (f : β → γ) (hf : surjective f) (smul : ∀ (c : α) x, f (c • x) = c • f x) : mul_action α γ := { smul := (•), one_smul := λ y, by { rcases hf y with ⟨x, rfl⟩, rw [← smul, one_smul] }, mul_smul := λ c₁ c₂ y, by { rcases hf y with ⟨x, rfl⟩, simp only [← smul, mul_smul] } } section ite variables (p : Prop) [decidable p] lemma ite_smul (a₁ a₂ : α) (b : β) : (ite p a₁ a₂) • b = ite p (a₁ • b) (a₂ • b) := by split_ifs; refl lemma smul_ite (a : α) (b₁ b₂ : β) : a • (ite p b₁ b₂) = ite p (a • b₁) (a • b₂) := by split_ifs; refl end ite namespace mul_action variables (α) /-- The regular action of a monoid on itself by left multiplication. -/ def regular : mul_action α α := { smul := λ a₁ a₂, a₁ * a₂, one_smul := λ a, one_mul a, mul_smul := λ a₁ a₂ a₃, mul_assoc _ _ _, } section regular local attribute [instance] regular instance is_scalar_tower.left : is_scalar_tower α α β := ⟨λ x y z, mul_smul x y z⟩ end regular variables (α β) /-- Embedding induced by action. -/ def to_fun : β ↪ (α → β) := ⟨λ y x, x • y, λ y₁ y₂ H, one_smul α y₁ ▸ one_smul α y₂ ▸ by convert congr_fun H 1⟩ variables {α β} @[simp] lemma to_fun_apply (x : α) (y : β) : mul_action.to_fun α β y x = x • y := rfl variable (β) /-- An action of `α` on `β` and a monoid homomorphism `γ → α` induce an action of `γ` on `β`. -/ def comp_hom [monoid γ] (g : γ →* α) : mul_action γ β := { smul := λ x b, (g x) • b, one_smul := by simp [g.map_one, mul_action.one_smul], mul_smul := by simp [g.map_mul, mul_action.mul_smul] } end mul_action end section compatible_scalar @[simp] lemma smul_one_smul {M} (N) [monoid N] [has_scalar M N] [mul_action N α] [has_scalar M α] [is_scalar_tower M N α] (x : M) (y : α) : (x • (1 : N)) • y = x • y := by rw [smul_assoc, one_smul] end compatible_scalar /-- Typeclass for multiplicative actions on additive structures. This generalizes group modules. -/ class distrib_mul_action (α : Type u) (β : Type v) [monoid α] [add_monoid β] extends mul_action α β := (smul_add : ∀(r : α) (x y : β), r • (x + y) = r • x + r • y) (smul_zero : ∀(r : α), r • (0 : β) = 0) section variables [monoid α] [add_monoid β] [distrib_mul_action α β] theorem smul_add (a : α) (b₁ b₂ : β) : a • (b₁ + b₂) = a • b₁ + a • b₂ := distrib_mul_action.smul_add _ _ _ @[simp] theorem smul_zero (a : α) : a • (0 : β) = 0 := distrib_mul_action.smul_zero _ /-- Pullback a distributive multiplicative action along an injective additive monoid homomorphism. -/ protected def function.injective.distrib_mul_action [add_monoid γ] [has_scalar α γ] (f : γ →+ β) (hf : injective f) (smul : ∀ (c : α) x, f (c • x) = c • f x) : distrib_mul_action α γ := { smul := (•), smul_add := λ c x y, hf $ by simp only [smul, f.map_add, smul_add], smul_zero := λ c, hf $ by simp only [smul, f.map_zero, smul_zero], .. hf.mul_action f smul } /-- Pushforward a distributive multiplicative action along a surjective additive monoid homomorphism.-/ protected def function.surjective.distrib_mul_action [add_monoid γ] [has_scalar α γ] (f : β →+ γ) (hf : surjective f) (smul : ∀ (c : α) x, f (c • x) = c • f x) : distrib_mul_action α γ := { smul := (•), smul_add := λ c x y, by { rcases hf x with ⟨x, rfl⟩, rcases hf y with ⟨y, rfl⟩, simp only [smul_add, ← smul, ← f.map_add] }, smul_zero := λ c, by simp only [← f.map_zero, ← smul, smul_zero], .. hf.mul_action f smul } variable (β) /-- Scalar multiplication by `r` as an `add_monoid_hom`. -/ def const_smul_hom (r : α) : β →+ β := { to_fun := (•) r, map_zero' := smul_zero r, map_add' := smul_add r } variable {β} @[simp] lemma const_smul_hom_apply (r : α) (x : β) : const_smul_hom β r x = r • x := rfl end section variables [monoid α] [add_group β] [distrib_mul_action α β] @[simp] theorem smul_neg (r : α) (x : β) : r • (-x) = -(r • x) := eq_neg_of_add_eq_zero $ by rw [← smul_add, neg_add_self, smul_zero] theorem smul_sub (r : α) (x y : β) : r • (x - y) = r • x - r • y := by rw [sub_eq_add_neg, sub_eq_add_neg, smul_add, smul_neg] end
b5d46221f90fa68172e763f8f20c4310ee3d283d
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/NonassociativeRing.lean
86bfc22b915f6ef2416b8db75bccf53be6694f7c
[]
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
13,993
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 NonassociativeRing structure NonassociativeRing (A : Type) : Type := (times : (A → (A → A))) (plus : (A → (A → A))) (one : A) (lunit_one : (∀ {x : A} , (times one x) = x)) (runit_one : (∀ {x : A} , (times x one) = x)) (associative_times : (∀ {x y z : A} , (times (times x y) z) = (times x (times y z)))) (inv : (A → A)) (leftInverse_inv_op_one : (∀ {x : A} , (times x (inv x)) = one)) (rightInverse_inv_op_one : (∀ {x : A} , (times (inv x) x) = one)) (commutative_times : (∀ {x y : A} , (times x y) = (times y x))) (leftDistributive_times_plus : (∀ {x y z : A} , (times x (plus y z)) = (plus (times x y) (times x z)))) (rightDistributive_times_plus : (∀ {x y z : A} , (times (plus y z) x) = (plus (times y x) (times z x)))) open NonassociativeRing structure Sig (AS : Type) : Type := (timesS : (AS → (AS → AS))) (plusS : (AS → (AS → AS))) (oneS : AS) (invS : (AS → AS)) structure Product (A : Type) : Type := (timesP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (plusP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (oneP : (Prod A A)) (invP : ((Prod A A) → (Prod A A))) (lunit_1P : (∀ {xP : (Prod A A)} , (timesP oneP xP) = xP)) (runit_1P : (∀ {xP : (Prod A A)} , (timesP xP oneP) = xP)) (associative_timesP : (∀ {xP yP zP : (Prod A A)} , (timesP (timesP xP yP) zP) = (timesP xP (timesP yP zP)))) (leftInverse_inv_op_1P : (∀ {xP : (Prod A A)} , (timesP xP (invP xP)) = oneP)) (rightInverse_inv_op_1P : (∀ {xP : (Prod A A)} , (timesP (invP xP) xP) = oneP)) (commutative_timesP : (∀ {xP yP : (Prod A A)} , (timesP xP yP) = (timesP yP xP))) (leftDistributive_times_plusP : (∀ {xP yP zP : (Prod A A)} , (timesP xP (plusP yP zP)) = (plusP (timesP xP yP) (timesP xP zP)))) (rightDistributive_times_plusP : (∀ {xP yP zP : (Prod A A)} , (timesP (plusP yP zP) xP) = (plusP (timesP yP xP) (timesP zP xP)))) structure Hom {A1 : Type} {A2 : Type} (No1 : (NonassociativeRing A1)) (No2 : (NonassociativeRing A2)) : Type := (hom : (A1 → A2)) (pres_times : (∀ {x1 x2 : A1} , (hom ((times No1) x1 x2)) = ((times No2) (hom x1) (hom x2)))) (pres_plus : (∀ {x1 x2 : A1} , (hom ((plus No1) x1 x2)) = ((plus No2) (hom x1) (hom x2)))) (pres_one : (hom (one No1)) = (one No2)) (pres_inv : (∀ {x1 : A1} , (hom ((inv No1) x1)) = ((inv No2) (hom x1)))) structure RelInterp {A1 : Type} {A2 : Type} (No1 : (NonassociativeRing A1)) (No2 : (NonassociativeRing A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_times : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((times No1) x1 x2) ((times No2) y1 y2)))))) (interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus No1) x1 x2) ((plus No2) y1 y2)))))) (interp_one : (interp (one No1) (one No2))) (interp_inv : (∀ {x1 : A1} {y1 : A2} , ((interp x1 y1) → (interp ((inv No1) x1) ((inv No2) y1))))) inductive NonassociativeRingTerm : Type | timesL : (NonassociativeRingTerm → (NonassociativeRingTerm → NonassociativeRingTerm)) | plusL : (NonassociativeRingTerm → (NonassociativeRingTerm → NonassociativeRingTerm)) | oneL : NonassociativeRingTerm | invL : (NonassociativeRingTerm → NonassociativeRingTerm) open NonassociativeRingTerm inductive ClNonassociativeRingTerm (A : Type) : Type | sing : (A → ClNonassociativeRingTerm) | timesCl : (ClNonassociativeRingTerm → (ClNonassociativeRingTerm → ClNonassociativeRingTerm)) | plusCl : (ClNonassociativeRingTerm → (ClNonassociativeRingTerm → ClNonassociativeRingTerm)) | oneCl : ClNonassociativeRingTerm | invCl : (ClNonassociativeRingTerm → ClNonassociativeRingTerm) open ClNonassociativeRingTerm inductive OpNonassociativeRingTerm (n : ℕ) : Type | v : ((fin n) → OpNonassociativeRingTerm) | timesOL : (OpNonassociativeRingTerm → (OpNonassociativeRingTerm → OpNonassociativeRingTerm)) | plusOL : (OpNonassociativeRingTerm → (OpNonassociativeRingTerm → OpNonassociativeRingTerm)) | oneOL : OpNonassociativeRingTerm | invOL : (OpNonassociativeRingTerm → OpNonassociativeRingTerm) open OpNonassociativeRingTerm inductive OpNonassociativeRingTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpNonassociativeRingTerm2) | sing2 : (A → OpNonassociativeRingTerm2) | timesOL2 : (OpNonassociativeRingTerm2 → (OpNonassociativeRingTerm2 → OpNonassociativeRingTerm2)) | plusOL2 : (OpNonassociativeRingTerm2 → (OpNonassociativeRingTerm2 → OpNonassociativeRingTerm2)) | oneOL2 : OpNonassociativeRingTerm2 | invOL2 : (OpNonassociativeRingTerm2 → OpNonassociativeRingTerm2) open OpNonassociativeRingTerm2 def simplifyCl {A : Type} : ((ClNonassociativeRingTerm A) → (ClNonassociativeRingTerm A)) | (timesCl oneCl x) := x | (timesCl x oneCl) := x | (timesCl x1 x2) := (timesCl (simplifyCl x1) (simplifyCl x2)) | (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2)) | oneCl := oneCl | (invCl x1) := (invCl (simplifyCl x1)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpNonassociativeRingTerm n) → (OpNonassociativeRingTerm n)) | (timesOL oneOL x) := x | (timesOL x oneOL) := x | (timesOL x1 x2) := (timesOL (simplifyOpB x1) (simplifyOpB x2)) | (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2)) | oneOL := oneOL | (invOL x1) := (invOL (simplifyOpB x1)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpNonassociativeRingTerm2 n A) → (OpNonassociativeRingTerm2 n A)) | (timesOL2 oneOL2 x) := x | (timesOL2 x oneOL2) := x | (timesOL2 x1 x2) := (timesOL2 (simplifyOp x1) (simplifyOp x2)) | (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2)) | oneOL2 := oneOL2 | (invOL2 x1) := (invOL2 (simplifyOp x1)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((NonassociativeRing A) → (NonassociativeRingTerm → A)) | No (timesL x1 x2) := ((times No) (evalB No x1) (evalB No x2)) | No (plusL x1 x2) := ((plus No) (evalB No x1) (evalB No x2)) | No oneL := (one No) | No (invL x1) := ((inv No) (evalB No x1)) def evalCl {A : Type} : ((NonassociativeRing A) → ((ClNonassociativeRingTerm A) → A)) | No (sing x1) := x1 | No (timesCl x1 x2) := ((times No) (evalCl No x1) (evalCl No x2)) | No (plusCl x1 x2) := ((plus No) (evalCl No x1) (evalCl No x2)) | No oneCl := (one No) | No (invCl x1) := ((inv No) (evalCl No x1)) def evalOpB {A : Type} {n : ℕ} : ((NonassociativeRing A) → ((vector A n) → ((OpNonassociativeRingTerm n) → A))) | No vars (v x1) := (nth vars x1) | No vars (timesOL x1 x2) := ((times No) (evalOpB No vars x1) (evalOpB No vars x2)) | No vars (plusOL x1 x2) := ((plus No) (evalOpB No vars x1) (evalOpB No vars x2)) | No vars oneOL := (one No) | No vars (invOL x1) := ((inv No) (evalOpB No vars x1)) def evalOp {A : Type} {n : ℕ} : ((NonassociativeRing A) → ((vector A n) → ((OpNonassociativeRingTerm2 n A) → A))) | No vars (v2 x1) := (nth vars x1) | No vars (sing2 x1) := x1 | No vars (timesOL2 x1 x2) := ((times No) (evalOp No vars x1) (evalOp No vars x2)) | No vars (plusOL2 x1 x2) := ((plus No) (evalOp No vars x1) (evalOp No vars x2)) | No vars oneOL2 := (one No) | No vars (invOL2 x1) := ((inv No) (evalOp No vars x1)) def inductionB {P : (NonassociativeRingTerm → Type)} : ((∀ (x1 x2 : NonassociativeRingTerm) , ((P x1) → ((P x2) → (P (timesL x1 x2))))) → ((∀ (x1 x2 : NonassociativeRingTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → ((P oneL) → ((∀ (x1 : NonassociativeRingTerm) , ((P x1) → (P (invL x1)))) → (∀ (x : NonassociativeRingTerm) , (P x)))))) | ptimesl pplusl p1l pinvl (timesL x1 x2) := (ptimesl _ _ (inductionB ptimesl pplusl p1l pinvl x1) (inductionB ptimesl pplusl p1l pinvl x2)) | ptimesl pplusl p1l pinvl (plusL x1 x2) := (pplusl _ _ (inductionB ptimesl pplusl p1l pinvl x1) (inductionB ptimesl pplusl p1l pinvl x2)) | ptimesl pplusl p1l pinvl oneL := p1l | ptimesl pplusl p1l pinvl (invL x1) := (pinvl _ (inductionB ptimesl pplusl p1l pinvl x1)) def inductionCl {A : Type} {P : ((ClNonassociativeRingTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClNonassociativeRingTerm A)) , ((P x1) → ((P x2) → (P (timesCl x1 x2))))) → ((∀ (x1 x2 : (ClNonassociativeRingTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → ((P oneCl) → ((∀ (x1 : (ClNonassociativeRingTerm A)) , ((P x1) → (P (invCl x1)))) → (∀ (x : (ClNonassociativeRingTerm A)) , (P x))))))) | psing ptimescl ppluscl p1cl pinvcl (sing x1) := (psing x1) | psing ptimescl ppluscl p1cl pinvcl (timesCl x1 x2) := (ptimescl _ _ (inductionCl psing ptimescl ppluscl p1cl pinvcl x1) (inductionCl psing ptimescl ppluscl p1cl pinvcl x2)) | psing ptimescl ppluscl p1cl pinvcl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing ptimescl ppluscl p1cl pinvcl x1) (inductionCl psing ptimescl ppluscl p1cl pinvcl x2)) | psing ptimescl ppluscl p1cl pinvcl oneCl := p1cl | psing ptimescl ppluscl p1cl pinvcl (invCl x1) := (pinvcl _ (inductionCl psing ptimescl ppluscl p1cl pinvcl x1)) def inductionOpB {n : ℕ} {P : ((OpNonassociativeRingTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpNonassociativeRingTerm n)) , ((P x1) → ((P x2) → (P (timesOL x1 x2))))) → ((∀ (x1 x2 : (OpNonassociativeRingTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → ((P oneOL) → ((∀ (x1 : (OpNonassociativeRingTerm n)) , ((P x1) → (P (invOL x1)))) → (∀ (x : (OpNonassociativeRingTerm n)) , (P x))))))) | pv ptimesol pplusol p1ol pinvol (v x1) := (pv x1) | pv ptimesol pplusol p1ol pinvol (timesOL x1 x2) := (ptimesol _ _ (inductionOpB pv ptimesol pplusol p1ol pinvol x1) (inductionOpB pv ptimesol pplusol p1ol pinvol x2)) | pv ptimesol pplusol p1ol pinvol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv ptimesol pplusol p1ol pinvol x1) (inductionOpB pv ptimesol pplusol p1ol pinvol x2)) | pv ptimesol pplusol p1ol pinvol oneOL := p1ol | pv ptimesol pplusol p1ol pinvol (invOL x1) := (pinvol _ (inductionOpB pv ptimesol pplusol p1ol pinvol x1)) def inductionOp {n : ℕ} {A : Type} {P : ((OpNonassociativeRingTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpNonassociativeRingTerm2 n A)) , ((P x1) → ((P x2) → (P (timesOL2 x1 x2))))) → ((∀ (x1 x2 : (OpNonassociativeRingTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → ((P oneOL2) → ((∀ (x1 : (OpNonassociativeRingTerm2 n A)) , ((P x1) → (P (invOL2 x1)))) → (∀ (x : (OpNonassociativeRingTerm2 n A)) , (P x)))))))) | pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 (v2 x1) := (pv2 x1) | pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 (sing2 x1) := (psing2 x1) | pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 (timesOL2 x1 x2) := (ptimesol2 _ _ (inductionOp pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 x1) (inductionOp pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 x2)) | pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 x1) (inductionOp pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 x2)) | pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 oneOL2 := p1ol2 | pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 (invOL2 x1) := (pinvol2 _ (inductionOp pv2 psing2 ptimesol2 pplusol2 p1ol2 pinvol2 x1)) def stageB : (NonassociativeRingTerm → (Staged NonassociativeRingTerm)) | (timesL x1 x2) := (stage2 timesL (codeLift2 timesL) (stageB x1) (stageB x2)) | (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2)) | oneL := (Now oneL) | (invL x1) := (stage1 invL (codeLift1 invL) (stageB x1)) def stageCl {A : Type} : ((ClNonassociativeRingTerm A) → (Staged (ClNonassociativeRingTerm A))) | (sing x1) := (Now (sing x1)) | (timesCl x1 x2) := (stage2 timesCl (codeLift2 timesCl) (stageCl x1) (stageCl x2)) | (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2)) | oneCl := (Now oneCl) | (invCl x1) := (stage1 invCl (codeLift1 invCl) (stageCl x1)) def stageOpB {n : ℕ} : ((OpNonassociativeRingTerm n) → (Staged (OpNonassociativeRingTerm n))) | (v x1) := (const (code (v x1))) | (timesOL x1 x2) := (stage2 timesOL (codeLift2 timesOL) (stageOpB x1) (stageOpB x2)) | (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2)) | oneOL := (Now oneOL) | (invOL x1) := (stage1 invOL (codeLift1 invOL) (stageOpB x1)) def stageOp {n : ℕ} {A : Type} : ((OpNonassociativeRingTerm2 n A) → (Staged (OpNonassociativeRingTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (timesOL2 x1 x2) := (stage2 timesOL2 (codeLift2 timesOL2) (stageOp x1) (stageOp x2)) | (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2)) | oneOL2 := (Now oneOL2) | (invOL2 x1) := (stage1 invOL2 (codeLift1 invOL2) (stageOp x1)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (timesT : ((Repr A) → ((Repr A) → (Repr A)))) (plusT : ((Repr A) → ((Repr A) → (Repr A)))) (oneT : (Repr A)) (invT : ((Repr A) → (Repr A))) end NonassociativeRing
b1de76406e0d37b81ee3f97f18a307f7257d3af9
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/assert_tac.lean
ad95c5112593da4a09fd156c24ef3ed61405e7a4
[ "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,336
lean
import logic variables {A : Type} {a a' : A} definition to_eq₁ (H : a == a') : a = a' := begin assert H₁ : ∀ (Ht : A = A), eq.rec_on Ht a = a, intro Ht, exact (eq.refl (eq.rec_on Ht a)), show a = a', from heq.rec_on H H₁ (eq.refl A) end definition to_eq₂ (H : a == a') : a = a' := begin have H₁ : ∀ (Ht : A = A), eq.rec_on Ht a = a, begin intro Ht, exact (eq.refl (eq.rec_on Ht a)) end, show a = a', from heq.rec_on H H₁ (eq.refl A) end definition to_eq₃ (H : a == a') : a = a' := begin have H₁ : ∀ (Ht : A = A), eq.rec_on Ht a = a, by intro Ht; exact (eq.refl (eq.rec_on Ht a)), show a = a', from heq.rec_on H H₁ (eq.refl A) end definition to_eq₄ (H : a == a') : a = a' := begin have H₁ : ∀ (Ht : A = A), eq.rec_on Ht a = a, from assume Ht, eq.refl (eq.rec_on Ht a), show a = a', from heq.rec_on H H₁ (eq.refl A) end definition to_eq₅ (H : a == a') : a = a' := begin have H₁ : ∀ (Ht : A = A), eq.rec_on Ht a = a, proof λ Ht, eq.refl (eq.rec_on Ht a) qed, show a = a', from heq.rec_on H H₁ (eq.refl A) end definition to_eq₆ (H : a == a') : a = a' := begin have H₁ : ∀ (Ht : A = A), eq.rec_on Ht a = a, from assume Ht, eq.refl (eq.rec_on Ht a), show a = a', from heq.rec_on H H₁ (eq.refl A) end
c5b912ca1bad03ed5428f57e16c4379409abc695
ebfc36a919e0b75a83886ec635f471d7f2dca171
/archive/2021/AoC/Day1.lean
ca97ead0cb46b6d28d91eaca6f676d28994e0e8c
[]
no_license
kaychaks/AoC
b933a125e2c55f632c7728eea841d827d1b5ef38
962cc536dda5156ac0b624f156146bf6d12ad8d2
refs/heads/master
1,671,715,037,914
1,671,632,408,000
1,671,632,408,000
160,005,656
0
0
null
null
null
null
UTF-8
Lean
false
false
1,954
lean
namespace Day1 open IO.FS section Common variable {α β γ ζ : Type u} variable {a : Type u -> Type v} def liftA3 [Applicative a] (f: α -> β -> γ -> ζ) (x: a α) (y: a β) (z: a γ) : a ζ := f <$> x <*> y <*> z end Common section Part1 open Prod def measureLarge (x: Nat × String) (z: String) : Nat × String := if x.snd.toNat! < z.toNat! then (x.fst + 1, z) else (x.fst, z) def measureLargeNat (x: Nat × Nat) (z: Nat) : Nat × Nat := if x.snd < z then (x.fst + 1, z) else (x.fst, z) def totalIncr (s: Sum Nat String) (xs: Array String := #[]) (ys: Array Nat := #[]): Nat := match s with | Sum.inr _ => Prod.fst <| Array.foldl measureLarge (0, Array.get! xs 0) xs | _ => Prod.fst <| Array.foldl measureLargeNat (0, Array.get! ys 0) ys end Part1 section Part2 def lookAheadSum? (xs: Array String) (idx: Nat) : OptionM Nat := let size := xs.size if size = 0 then Option.none else let x: OptionM Nat := String.toNat! <$> xs.get? idx let y: OptionM Nat := String.toNat! <$> xs.get? (idx + 1) let z: OptionM Nat := String.toNat! <$> xs.get? (idx + 2) liftA3 (· + · + ·) x y z def windowEndIndex (length: Nat): Nat := length - (length % 3) /- below code might look like imperative shit but Lean is smart enough to convert the same via join points. So, everything gets converted to functional code in the end -/ def threeWindowSums (xs: Array String): Array Nat := do let mut ys := #[] for id in [:windowEndIndex xs.size] do match (lookAheadSum? xs id) with | some x => ys := ys.push x | _ => () return ys end Part2 def run : IO Unit := do let day1 := "./data/day1.txt" let readings <- lines day1 let part1Total := totalIncr (Sum.inr "") readings let part2Total := totalIncr (Sum.inl 0) (ys := threeWindowSums readings) IO.println (part1Total, part2Total) end Day1
2ac281dc8f12b81b979943ff7d85e2d2f36d85df
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/data/rat/basic.lean
b005167adf5abb18da0334c81635c23d0d02b75c
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
24,729
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.int.sqrt import data.equiv.encodable import algebra.group import algebra.euclidean_domain import algebra.ordered_field /-! # Basics for the Rational Numbers ## Summary We define a rational number `q` as a structure `{ num, denom, pos, cop }`, where - `num` is the numerator of `q`, - `denom` is the denominator of `q`, - `pos` is a proof that `denom > 0`, and - `cop` is a proof `num` and `denom` are coprime. We then define the expected (discrete) field structure on `ℚ` and prove basic lemmas about it. Moreoever, we provide the expected casts from `ℕ` and `ℤ` into `ℚ`, i.e. `(↑n : ℚ) = n / 1`. ## Main Definitions - `rat` is the structure encoding `ℚ`. - `rat.mk n d` constructs a rational number `q = n / d` from `n d : ℤ`. ## Notations - `/.` is infix notation for `rat.mk`. ## Tags rat, rationals, field, ℚ, numerator, denominator, num, denom -/ /-- `rat`, or `ℚ`, is the type of rational numbers. It is defined as the set of pairs ⟨n, d⟩ of integers such that `d` is positive and `n` and `d` are coprime. This representation is preferred to the quotient because without periodic reduction, the numerator and denominator can grow exponentially (for example, adding 1/2 to itself repeatedly). -/ structure rat := mk' :: (num : ℤ) (denom : ℕ) (pos : 0 < denom) (cop : num.nat_abs.coprime denom) notation `ℚ` := rat namespace rat protected def repr : ℚ → string | ⟨n, d, _, _⟩ := if d = 1 then _root_.repr n else _root_.repr n ++ "/" ++ _root_.repr d instance : has_repr ℚ := ⟨rat.repr⟩ instance : has_to_string ℚ := ⟨rat.repr⟩ meta instance : has_to_format ℚ := ⟨coe ∘ rat.repr⟩ instance : encodable ℚ := encodable.of_equiv (Σ n : ℤ, {d : ℕ // 0 < d ∧ n.nat_abs.coprime d}) ⟨λ ⟨a, b, c, d⟩, ⟨a, b, c, d⟩, λ⟨a, b, c, d⟩, ⟨a, b, c, d⟩, λ ⟨a, b, c, d⟩, rfl, λ⟨a, b, c, d⟩, rfl⟩ /-- Embed an integer as a rational number -/ def of_int (n : ℤ) : ℚ := ⟨n, 1, nat.one_pos, nat.coprime_one_right _⟩ instance : has_zero ℚ := ⟨of_int 0⟩ instance : has_one ℚ := ⟨of_int 1⟩ instance : inhabited ℚ := ⟨0⟩ /-- Form the quotient `n / d` where `n:ℤ` and `d:ℕ+` (not necessarily coprime) -/ def mk_pnat (n : ℤ) : ℕ+ → ℚ | ⟨d, dpos⟩ := let n' := n.nat_abs, g := n'.gcd d in ⟨n / g, d / g, begin apply (nat.le_div_iff_mul_le _ _ (nat.gcd_pos_of_pos_right _ dpos)).2, simp, exact nat.le_of_dvd dpos (nat.gcd_dvd_right _ _) end, begin have : int.nat_abs (n / ↑g) = n' / g, { cases int.nat_abs_eq n with e e; rw e, { refl }, rw [int.neg_div_of_dvd, int.nat_abs_neg], { refl }, exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) }, rw this, exact nat.coprime_div_gcd_div_gcd (nat.gcd_pos_of_pos_right _ dpos) end⟩ /-- Form the quotient `n / d` where `n:ℤ` and `d:ℕ`. In the case `d = 0`, we define `n / 0 = 0` by convention. -/ def mk_nat (n : ℤ) (d : ℕ) : ℚ := if d0 : d = 0 then 0 else mk_pnat n ⟨d, nat.pos_of_ne_zero d0⟩ /-- Form the quotient `n / d` where `n d : ℤ`. -/ def mk : ℤ → ℤ → ℚ | n (d : ℕ) := mk_nat n d | n -[1+ d] := mk_pnat (-n) d.succ_pnat localized "infix ` /. `:70 := rat.mk" in rat theorem mk_pnat_eq (n d h) : mk_pnat n ⟨d, h⟩ = n /. d := by change n /. d with dite _ _ _; simp [ne_of_gt h] theorem mk_nat_eq (n d) : mk_nat n d = n /. d := rfl @[simp] theorem mk_zero (n) : n /. 0 = 0 := rfl @[simp] theorem zero_mk_pnat (n) : mk_pnat 0 n = 0 := by cases n; simp [mk_pnat]; change int.nat_abs 0 with 0; simp *; refl @[simp] theorem zero_mk_nat (n) : mk_nat 0 n = 0 := by by_cases n = 0; simp [*, mk_nat] @[simp] theorem zero_mk (n) : 0 /. n = 0 := by cases n; simp [mk] private lemma gcd_abs_dvd_left {a b} : (nat.gcd (int.nat_abs a) b : ℤ) ∣ a := int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ nat.gcd_dvd_left (int.nat_abs a) b @[simp] theorem mk_eq_zero {a b : ℤ} (b0 : b ≠ 0) : a /. b = 0 ↔ a = 0 := begin constructor; intro h; [skip, {subst a, simp}], have : ∀ {a b}, mk_pnat a b = 0 → a = 0, { intros a b e, cases b with b h, injection e with e, apply int.eq_mul_of_div_eq_right gcd_abs_dvd_left e }, cases b with b; simp [mk, mk_nat] at h, { simp [mt (congr_arg int.of_nat) b0] at h, exact this h }, { apply neg_inj, simp [this h] } end theorem mk_eq : ∀ {a b c d : ℤ} (hb : b ≠ 0) (hd : d ≠ 0), a /. b = c /. d ↔ a * d = c * b := suffices ∀ a b c d hb hd, mk_pnat a ⟨b, hb⟩ = mk_pnat c ⟨d, hd⟩ ↔ a * d = c * b, begin intros, cases b with b b; simp [mk, mk_nat, nat.succ_pnat], simp [mt (congr_arg int.of_nat) hb], all_goals { cases d with d d; simp [mk, mk_nat, nat.succ_pnat], simp [mt (congr_arg int.of_nat) hd], all_goals { rw this, try {refl} } }, { change a * ↑(d.succ) = -c * ↑b ↔ a * -(d.succ) = c * b, constructor; intro h; apply neg_inj; simpa [left_distrib, neg_add_eq_iff_eq_add, eq_neg_iff_add_eq_zero, neg_eq_iff_add_eq_zero] using h }, { change -a * ↑d = c * b.succ ↔ a * d = c * -b.succ, constructor; intro h; apply neg_inj; simpa [left_distrib, eq_comm] using h }, { change -a * d.succ = -c * b.succ ↔ a * -d.succ = c * -b.succ, simp [left_distrib, sub_eq_add_neg], cc } end, begin intros, simp [mk_pnat], constructor; intro h, { cases h with ha hb, have ha, { have dv := @gcd_abs_dvd_left, have := int.eq_mul_of_div_eq_right dv ha, rw ← int.mul_div_assoc _ dv at this, exact int.eq_mul_of_div_eq_left (dvd_mul_of_dvd_right dv _) this.symm }, have hb, { have dv := λ {a b}, nat.gcd_dvd_right (int.nat_abs a) b, have := nat.eq_mul_of_div_eq_right dv hb, rw ← nat.mul_div_assoc _ dv at this, exact nat.eq_mul_of_div_eq_left (dvd_mul_of_dvd_right dv _) this.symm }, have m0 : (a.nat_abs.gcd b * c.nat_abs.gcd d : ℤ) ≠ 0, { refine int.coe_nat_ne_zero.2 (ne_of_gt _), apply mul_pos; apply nat.gcd_pos_of_pos_right; assumption }, apply eq_of_mul_eq_mul_right m0, simpa [mul_comm, mul_left_comm] using congr (congr_arg (*) ha.symm) (congr_arg coe hb) }, { suffices : ∀ a c, a * d = c * b → a / a.gcd b = c / c.gcd d ∧ b / a.gcd b = d / c.gcd d, { cases this a.nat_abs c.nat_abs (by simpa [int.nat_abs_mul] using congr_arg int.nat_abs h) with h₁ h₂, have hs := congr_arg int.sign h, simp [int.sign_eq_one_of_pos (int.coe_nat_lt.2 hb), int.sign_eq_one_of_pos (int.coe_nat_lt.2 hd)] at hs, conv in a { rw ← int.sign_mul_nat_abs a }, conv in c { rw ← int.sign_mul_nat_abs c }, rw [int.mul_div_assoc, int.mul_div_assoc], exact ⟨congr (congr_arg (*) hs) (congr_arg coe h₁), h₂⟩, all_goals { exact int.coe_nat_dvd.2 (nat.gcd_dvd_left _ _) } }, intros a c h, suffices bd : b / a.gcd b = d / c.gcd d, { refine ⟨_, bd⟩, apply nat.eq_of_mul_eq_mul_left hb, rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), mul_comm, nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), bd, ← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), h, mul_comm, nat.mul_div_assoc _ (nat.gcd_dvd_left _ _)] }, suffices : ∀ {a c : ℕ} (b>0) (d>0), a * d = c * b → b / a.gcd b ≤ d / c.gcd d, { exact le_antisymm (this _ hb _ hd h) (this _ hd _ hb h.symm) }, intros a c b hb d hd h, have gb0 := nat.gcd_pos_of_pos_right a hb, have gd0 := nat.gcd_pos_of_pos_right c hd, apply nat.le_of_dvd, apply (nat.le_div_iff_mul_le _ _ gd0).2, simp, apply nat.le_of_dvd hd (nat.gcd_dvd_right _ _), apply (nat.coprime_div_gcd_div_gcd gb0).symm.dvd_of_dvd_mul_left, refine ⟨c / c.gcd d, _⟩, rw [← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), ← nat.mul_div_assoc _ (nat.gcd_dvd_right _ _)], apply congr_arg (/ c.gcd d), rw [mul_comm, ← nat.mul_div_assoc _ (nat.gcd_dvd_left _ _), mul_comm, h, nat.mul_div_assoc _ (nat.gcd_dvd_right _ _), mul_comm] } end @[simp] theorem div_mk_div_cancel_left {a b c : ℤ} (c0 : c ≠ 0) : (a * c) /. (b * c) = a /. b := begin by_cases b0 : b = 0, { subst b0, simp }, apply (mk_eq (mul_ne_zero b0 c0) b0).2, simp [mul_comm, mul_assoc] end @[simp] theorem num_denom : ∀ {a : ℚ}, a.num /. a.denom = a | ⟨n, d, h, (c:_=1)⟩ := show mk_nat n d = _, by simp [mk_nat, ne_of_gt h, mk_pnat, c] theorem num_denom' {n d h c} : (⟨n, d, h, c⟩ : ℚ) = n /. d := num_denom.symm theorem of_int_eq_mk (z : ℤ) : of_int z = z /. 1 := num_denom' @[elab_as_eliminator] def {u} num_denom_cases_on {C : ℚ → Sort u} : ∀ (a : ℚ) (H : ∀ n d, 0 < d → (int.nat_abs n).coprime d → C (n /. d)), C a | ⟨n, d, h, c⟩ H := by rw num_denom'; exact H n d h c @[elab_as_eliminator] def {u} num_denom_cases_on' {C : ℚ → Sort u} (a : ℚ) (H : ∀ (n:ℤ) (d:ℕ), d ≠ 0 → C (n /. d)) : C a := num_denom_cases_on a $ λ n d h c, H n d $ ne_of_gt h theorem num_dvd (a) {b : ℤ} (b0 : b ≠ 0) : (a /. b).num ∣ a := begin cases e : a /. b with n d h c, rw [rat.num_denom', rat.mk_eq b0 (ne_of_gt (int.coe_nat_pos.2 h))] at e, refine (int.nat_abs_dvd.1 $ int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ c.dvd_of_dvd_mul_right _), have := congr_arg int.nat_abs e, simp [int.nat_abs_mul, int.nat_abs_of_nat] at this, simp [this] end theorem denom_dvd (a b : ℤ) : ((a /. b).denom : ℤ) ∣ b := begin by_cases b0 : b = 0, {simp [b0]}, cases e : a /. b with n d h c, rw [num_denom', mk_eq b0 (ne_of_gt (int.coe_nat_pos.2 h))] at e, refine (int.dvd_nat_abs.1 $ int.coe_nat_dvd.2 $ c.symm.dvd_of_dvd_mul_left _), rw [← int.nat_abs_mul, ← int.coe_nat_dvd, int.dvd_nat_abs, ← e], simp end protected def add : ℚ → ℚ → ℚ | ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := mk_pnat (n₁ * d₂ + n₂ * d₁) ⟨d₁ * d₂, mul_pos h₁ h₂⟩ instance : has_add ℚ := ⟨rat.add⟩ theorem lift_binop_eq (f : ℚ → ℚ → ℚ) (f₁ : ℤ → ℤ → ℤ → ℤ → ℤ) (f₂ : ℤ → ℤ → ℤ → ℤ → ℤ) (fv : ∀ {n₁ d₁ h₁ c₁ n₂ d₂ h₂ c₂}, f ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ = f₁ n₁ d₁ n₂ d₂ /. f₂ n₁ d₁ n₂ d₂) (f0 : ∀ {n₁ d₁ n₂ d₂} (d₁0 : d₁ ≠ 0) (d₂0 : d₂ ≠ 0), f₂ n₁ d₁ n₂ d₂ ≠ 0) (a b c d : ℤ) (b0 : b ≠ 0) (d0 : d ≠ 0) (H : ∀ {n₁ d₁ n₂ d₂} (h₁ : a * d₁ = n₁ * b) (h₂ : c * d₂ = n₂ * d), f₁ n₁ d₁ n₂ d₂ * f₂ a b c d = f₁ a b c d * f₂ n₁ d₁ n₂ d₂) : f (a /. b) (c /. d) = f₁ a b c d /. f₂ a b c d := begin generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, generalize hc : c /. d = x, cases x with n₂ d₂ h₂ c₂, rw num_denom' at hc, rw fv, have d₁0 := ne_of_gt (int.coe_nat_lt.2 h₁), have d₂0 := ne_of_gt (int.coe_nat_lt.2 h₂), exact (mk_eq (f0 d₁0 d₂0) (f0 b0 d0)).2 (H ((mk_eq b0 d₁0).1 ha) ((mk_eq d0 d₂0).1 hc)) end @[simp] theorem add_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : a /. b + c /. d = (a * d + c * b) /. (b * d) := begin apply lift_binop_eq rat.add; intros; try {assumption}, { apply mk_pnat_eq }, { apply mul_ne_zero d₁0 d₂0 }, calc (n₁ * d₂ + n₂ * d₁) * (b * d) = (n₁ * b) * d₂ * d + (n₂ * d) * (d₁ * b) : by simp [mul_add, mul_comm, mul_left_comm] ... = (a * d₁) * d₂ * d + (c * d₂) * (d₁ * b) : by rw [h₁, h₂] ... = (a * d + c * b) * (d₁ * d₂) : by simp [mul_add, mul_comm, mul_left_comm] end protected def neg : ℚ → ℚ | ⟨n, d, h, c⟩ := ⟨-n, d, h, by simp [c]⟩ instance : has_neg ℚ := ⟨rat.neg⟩ @[simp] theorem neg_def {a b : ℤ} : -(a /. b) = -a /. b := begin by_cases b0 : b = 0, { subst b0, simp, refl }, generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, show rat.mk' _ _ _ _ = _, rw num_denom', have d0 := ne_of_gt (int.coe_nat_lt.2 h₁), apply (mk_eq d0 b0).2, have h₁ := (mk_eq b0 d0).1 ha, simp only [neg_mul_eq_neg_mul_symm, congr_arg has_neg.neg h₁] end protected def mul : ℚ → ℚ → ℚ | ⟨n₁, d₁, h₁, c₁⟩ ⟨n₂, d₂, h₂, c₂⟩ := mk_pnat (n₁ * n₂) ⟨d₁ * d₂, mul_pos h₁ h₂⟩ instance : has_mul ℚ := ⟨rat.mul⟩ @[simp] theorem mul_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : (a /. b) * (c /. d) = (a * c) /. (b * d) := begin apply lift_binop_eq rat.mul; intros; try {assumption}, { apply mk_pnat_eq }, { apply mul_ne_zero d₁0 d₂0 }, cc end protected def inv : ℚ → ℚ | ⟨(n+1:ℕ), d, h, c⟩ := ⟨d, n+1, n.succ_pos, c.symm⟩ | ⟨0, d, h, c⟩ := 0 | ⟨-[1+ n], d, h, c⟩ := ⟨-d, n+1, n.succ_pos, nat.coprime.symm $ by simp; exact c⟩ instance : has_inv ℚ := ⟨rat.inv⟩ @[simp] theorem inv_def {a b : ℤ} : (a /. b)⁻¹ = b /. a := begin by_cases a0 : a = 0, { subst a0, simp, refl }, by_cases b0 : b = 0, { subst b0, simp, refl }, generalize ha : a /. b = x, cases x with n d h c, rw num_denom' at ha, refine eq.trans (_ : rat.inv ⟨n, d, h, c⟩ = d /. n) _, { cases n with n; [cases n with n, skip], { refl }, { change int.of_nat n.succ with (n+1:ℕ), unfold rat.inv, rw num_denom' }, { unfold rat.inv, rw num_denom', refl } }, have n0 : n ≠ 0, { refine mt (λ (n0 : n = 0), _) a0, subst n0, simp at ha, exact (mk_eq_zero b0).1 ha }, have d0 := ne_of_gt (int.coe_nat_lt.2 h), have ha := (mk_eq b0 d0).1 ha, apply (mk_eq n0 a0).2, cc end variables (a b c : ℚ) protected theorem add_zero : a + 0 = a := num_denom_cases_on' a $ λ n d h, by rw [← zero_mk d]; simp [h, -zero_mk] protected theorem zero_add : 0 + a = a := num_denom_cases_on' a $ λ n d h, by rw [← zero_mk d]; simp [h, -zero_mk] protected theorem add_comm : a + b = b + a := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, by simp [h₁, h₂]; cc protected theorem add_assoc : a + b + c = a + (b + c) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero, mul_add, mul_comm, mul_left_comm, add_left_comm, add_assoc] protected theorem add_left_neg : -a + a = 0 := num_denom_cases_on' a $ λ n d h, by simp [h] protected theorem mul_one : a * 1 = a := num_denom_cases_on' a $ λ n d h, by change (1:ℚ) with 1 /. 1; simp [h] protected theorem one_mul : 1 * a = a := num_denom_cases_on' a $ λ n d h, by change (1:ℚ) with 1 /. 1; simp [h] protected theorem mul_comm : a * b = b * a := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, by simp [h₁, h₂, mul_comm] protected theorem mul_assoc : a * b * c = a * (b * c) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero, mul_comm, mul_left_comm] protected theorem add_mul : (a + b) * c = a * c + b * c := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, num_denom_cases_on' c $ λ n₃ d₃ h₃, by simp [h₁, h₂, h₃, mul_ne_zero]; refine (div_mk_div_cancel_left (int.coe_nat_ne_zero.2 h₃)).symm.trans _; simp [mul_add, mul_comm, mul_assoc, mul_left_comm] protected theorem mul_add : a * (b + c) = a * b + a * c := by rw [rat.mul_comm, rat.add_mul, rat.mul_comm, rat.mul_comm c a] protected theorem zero_ne_one : 0 ≠ (1:ℚ) := mt (λ (h : 0 = 1 /. 1), (mk_eq_zero one_ne_zero).1 h.symm) one_ne_zero protected theorem mul_inv_cancel : a ≠ 0 → a * a⁻¹ = 1 := num_denom_cases_on' a $ λ n d h a0, have n0 : n ≠ 0, from mt (by intro e; subst e; simp) a0, by simp [h, n0, mul_comm]; exact eq.trans (by simp) (@div_mk_div_cancel_left 1 1 _ n0) protected theorem inv_mul_cancel (h : a ≠ 0) : a⁻¹ * a = 1 := eq.trans (rat.mul_comm _ _) (rat.mul_inv_cancel _ h) instance : decidable_eq ℚ := by tactic.mk_dec_eq_instance instance : field ℚ := { zero := 0, add := rat.add, neg := rat.neg, one := 1, mul := rat.mul, inv := rat.inv, zero_add := rat.zero_add, add_zero := rat.add_zero, add_comm := rat.add_comm, add_assoc := rat.add_assoc, add_left_neg := rat.add_left_neg, mul_one := rat.mul_one, one_mul := rat.one_mul, mul_comm := rat.mul_comm, mul_assoc := rat.mul_assoc, left_distrib := rat.mul_add, right_distrib := rat.add_mul, zero_ne_one := rat.zero_ne_one, mul_inv_cancel := rat.mul_inv_cancel, inv_zero := rfl } /- Extra instances to short-circuit type class resolution -/ instance : division_ring ℚ := by apply_instance instance : integral_domain ℚ := by apply_instance -- TODO(Mario): this instance slows down data.real.basic --instance : domain ℚ := by apply_instance instance : nonzero_comm_ring ℚ := by apply_instance instance : comm_ring ℚ := by apply_instance --instance : ring ℚ := by apply_instance instance : comm_semiring ℚ := by apply_instance instance : semiring ℚ := by apply_instance instance : add_comm_group ℚ := by apply_instance instance : add_group ℚ := by apply_instance instance : add_comm_monoid ℚ := by apply_instance instance : add_monoid ℚ := by apply_instance instance : add_left_cancel_semigroup ℚ := by apply_instance instance : add_right_cancel_semigroup ℚ := by apply_instance instance : add_comm_semigroup ℚ := by apply_instance instance : add_semigroup ℚ := by apply_instance instance : comm_monoid ℚ := by apply_instance instance : monoid ℚ := by apply_instance instance : comm_semigroup ℚ := by apply_instance instance : semigroup ℚ := by apply_instance theorem sub_def {a b c d : ℤ} (b0 : b ≠ 0) (d0 : d ≠ 0) : a /. b - c /. d = (a * d - c * b) /. (b * d) := by simp [b0, d0, sub_eq_add_neg] @[simp] lemma denom_neg_eq_denom : ∀ q : ℚ, (-q).denom = q.denom | ⟨_, d, _, _⟩ := rfl @[simp] lemma num_neg_eq_neg_num : ∀ q : ℚ, (-q).num = -(q.num) | ⟨n, _, _, _⟩ := rfl @[simp] lemma num_zero : rat.num 0 = 0 := rfl lemma zero_of_num_zero {q : ℚ} (hq : q.num = 0) : q = 0 := have q = q.num /. q.denom, from num_denom.symm, by simpa [hq] lemma zero_iff_num_zero {q : ℚ} : q = 0 ↔ q.num = 0 := ⟨λ _, by simp *, zero_of_num_zero⟩ lemma num_ne_zero_of_ne_zero {q : ℚ} (h : q ≠ 0) : q.num ≠ 0 := assume : q.num = 0, h $ zero_of_num_zero this @[simp] lemma num_one : (1 : ℚ).num = 1 := rfl @[simp] lemma denom_one : (1 : ℚ).denom = 1 := rfl lemma denom_ne_zero (q : ℚ) : q.denom ≠ 0 := ne_of_gt q.pos lemma eq_iff_mul_eq_mul {p q : ℚ} : p = q ↔ p.num * q.denom = q.num * p.denom := begin conv_lhs { rw [←(@num_denom p), ←(@num_denom q)] }, apply rat.mk_eq, { exact_mod_cast p.denom_ne_zero }, { exact_mod_cast q.denom_ne_zero } end lemma mk_num_ne_zero_of_ne_zero {q : ℚ} {n d : ℤ} (hq : q ≠ 0) (hqnd : q = n /. d) : n ≠ 0 := assume : n = 0, hq $ by simpa [this] using hqnd lemma mk_denom_ne_zero_of_ne_zero {q : ℚ} {n d : ℤ} (hq : q ≠ 0) (hqnd : q = n /. d) : d ≠ 0 := assume : d = 0, hq $ by simpa [this] using hqnd lemma mk_ne_zero_of_ne_zero {n d : ℤ} (h : n ≠ 0) (hd : d ≠ 0) : n /. d ≠ 0 := assume : n /. d = 0, h $ (mk_eq_zero hd).1 this lemma mul_num_denom (q r : ℚ) : q * r = (q.num * r.num) /. ↑(q.denom * r.denom) := have hq' : (↑q.denom : ℤ) ≠ 0, by have := denom_ne_zero q; simpa, have hr' : (↑r.denom : ℤ) ≠ 0, by have := denom_ne_zero r; simpa, suffices (q.num /. ↑q.denom) * (r.num /. ↑r.denom) = (q.num * r.num) /. ↑(q.denom * r.denom), by simpa using this, by simp [mul_def hq' hr', -num_denom] lemma div_num_denom (q r : ℚ) : q / r = (q.num * r.denom) /. (q.denom * r.num) := if hr : r.num = 0 then have hr' : r = 0, from zero_of_num_zero hr, by simp * else calc q / r = q * r⁻¹ : div_eq_mul_inv ... = (q.num /. q.denom) * (r.num /. r.denom)⁻¹ : by simp ... = (q.num /. q.denom) * (r.denom /. r.num) : by rw inv_def ... = (q.num * r.denom) /. (q.denom * r.num) : mul_def (by simpa using denom_ne_zero q) hr lemma num_denom_mk {q : ℚ} {n d : ℤ} (hn : n ≠ 0) (hd : d ≠ 0) (qdf : q = n /. d) : ∃ c : ℤ, n = c * q.num ∧ d = c * q.denom := have hq : q ≠ 0, from assume : q = 0, hn $ (rat.mk_eq_zero hd).1 (by cc), have q.num /. q.denom = n /. d, by rwa [num_denom], have q.num * d = n * ↑(q.denom), from (rat.mk_eq (by simp [rat.denom_ne_zero]) hd).1 this, begin existsi n / q.num, have hqdn : q.num ∣ n, begin rw qdf, apply rat.num_dvd, assumption end, split, { rw int.div_mul_cancel hqdn }, { apply int.eq_mul_div_of_mul_eq_mul_of_dvd_left, { apply rat.num_ne_zero_of_ne_zero hq }, repeat { assumption } } end theorem mk_pnat_num (n : ℤ) (d : ℕ+) : (mk_pnat n d).num = n / nat.gcd n.nat_abs d := by cases d; refl theorem mk_pnat_denom (n : ℤ) (d : ℕ+) : (mk_pnat n d).denom = d / nat.gcd n.nat_abs d := by cases d; refl theorem mul_num (q₁ q₂ : ℚ) : (q₁ * q₂).num = (q₁.num * q₂.num) / nat.gcd (q₁.num * q₂.num).nat_abs (q₁.denom * q₂.denom) := by cases q₁; cases q₂; refl theorem mul_denom (q₁ q₂ : ℚ) : (q₁ * q₂).denom = (q₁.denom * q₂.denom) / nat.gcd (q₁.num * q₂.num).nat_abs (q₁.denom * q₂.denom) := by cases q₁; cases q₂; refl theorem mul_self_num (q : ℚ) : (q * q).num = q.num * q.num := by rw [mul_num, int.nat_abs_mul, nat.coprime.gcd_eq_one, int.coe_nat_one, int.div_one]; exact (q.cop.mul_right q.cop).mul (q.cop.mul_right q.cop) theorem mul_self_denom (q : ℚ) : (q * q).denom = q.denom * q.denom := by rw [rat.mul_denom, int.nat_abs_mul, nat.coprime.gcd_eq_one, nat.div_one]; exact (q.cop.mul_right q.cop).mul (q.cop.mul_right q.cop) lemma add_num_denom (q r : ℚ) : q + r = ((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ) := have hqd : (q.denom : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 q.3, have hrd : (r.denom : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 r.3, by conv_lhs { rw [←@num_denom q, ←@num_denom r, rat.add_def hqd hrd] }; simp [mul_comm] section casts theorem coe_int_eq_mk : ∀ (z : ℤ), ↑z = z /. 1 | (n : ℕ) := show (n:ℚ) = n /. 1, by induction n with n IH n; simp [*, show (1:ℚ) = 1 /. 1, from rfl] | -[1+ n] := show (-(n + 1) : ℚ) = -[1+ n] /. 1, begin induction n with n IH, {refl}, show -(n + 1 + 1 : ℚ) = -[1+ n.succ] /. 1, rw [neg_add, IH], simpa [show -1 = (-1) /. 1, from rfl] end theorem mk_eq_div (n d : ℤ) : n /. d = ((n : ℚ) / d) := begin by_cases d0 : d = 0, {simp [d0, div_zero]}, simp [division_def, coe_int_eq_mk, mul_def one_ne_zero d0] end theorem coe_int_eq_of_int (z : ℤ) : ↑z = of_int z := (coe_int_eq_mk z).trans (of_int_eq_mk z).symm @[simp, norm_cast] theorem coe_int_num (n : ℤ) : (n : ℚ).num = n := by rw coe_int_eq_of_int; refl @[simp, norm_cast] theorem coe_int_denom (n : ℤ) : (n : ℚ).denom = 1 := by rw coe_int_eq_of_int; refl lemma coe_int_num_of_denom_eq_one {q : ℚ} (hq : q.denom = 1) : ↑(q.num) = q := by { conv_rhs { rw [←(@num_denom q), hq] }, rw [coe_int_eq_mk], refl } instance : can_lift ℚ ℤ := ⟨coe, λ q, q.denom = 1, λ q hq, ⟨q.num, coe_int_num_of_denom_eq_one hq⟩⟩ theorem coe_nat_eq_mk (n : ℕ) : ↑n = n /. 1 := by rw [← int.cast_coe_nat, coe_int_eq_mk] @[simp, norm_cast] theorem coe_nat_num (n : ℕ) : (n : ℚ).num = n := by rw [← int.cast_coe_nat, coe_int_num] @[simp, norm_cast] theorem coe_nat_denom (n : ℕ) : (n : ℚ).denom = 1 := by rw [← int.cast_coe_nat, coe_int_denom] end casts lemma inv_def' {q : ℚ} : q⁻¹ = (q.denom : ℚ) / q.num := by { conv_lhs { rw ←(@num_denom q) }, cases q, simp [div_num_denom] } @[simp] lemma mul_own_denom_eq_num {q : ℚ} : q * q.denom = q.num := begin suffices : mk (q.num) ↑(q.denom) * mk ↑(q.denom) 1 = mk (q.num) 1, by { conv { for q [1] { rw ←(@num_denom q) }}, rwa [coe_int_eq_mk, coe_nat_eq_mk] }, have : (q.denom : ℤ) ≠ 0, from ne_of_gt (by exact_mod_cast q.pos), rw [(rat.mul_def this one_ne_zero), (mul_comm (q.denom : ℤ) 1), (div_mk_div_cancel_left this)] end end rat
96054b2658321112d44ce94962da1759be118ebe
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/tests/lean/run/match1.lean
21c9a976ddff8833c2dd9062705aca0dfd85037a
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,356
lean
def f (xs : List Nat) : List Bool := xs.map fun | 0 => true | _ => false #eval f [1, 2, 0, 2] theorem ex1 : f [1, 0, 2] = [false, true, false] := rfl #check f def g (xs : List Nat) : List Bool := xs.map $ by { intro | 0 => exact true | _ => exact false } theorem ex2 : g [1, 0, 2] = [false, true, false] := rfl theorem ex3 {p q r : Prop} : p ∨ q → r → (q ∧ r) ∨ (p ∧ r) := by intro | Or.inl hp, h => { apply Or.inr; apply And.intro; assumption; assumption } | Or.inr hq, h => { apply Or.inl; exact ⟨hq, h⟩ } inductive C | mk₁ : Nat → C | mk₂ : Nat → Nat → C def C.x : C → Nat | C.mk₁ x => x | C.mk₂ x _ => x def head : {α : Type} → List α → Option α | _, a::as => some a | _, _ => none theorem ex4 : head [1, 2] = some 1 := rfl def head2 : {α : Type} → List α → Option α := @fun | _, a::as => some a | _, _ => none theorem ex5 : head2 [1, 2] = some 1 := rfl def head3 {α : Type} (xs : List α) : Option α := let rec aux : {α : Type} → List α → Option α | _, a::as => some a | _, _ => none; aux xs theorem ex6 : head3 [1, 2] = some 1 := rfl inductive Vec.{u} (α : Type u) : Nat → Type u | nil : Vec α 0 | cons {n} (head : α) (tail : Vec α n) : Vec α (n+1) def Vec.mapHead1 {α β δ} : {n : Nat} → Vec α n → Vec β n → (α → β → δ) → Option δ | _, nil, nil, f => none | _, cons a as, cons b bs, f => some (f a b) def Vec.mapHead2 {α β δ} : {n : Nat} → Vec α n → Vec β n → (α → β → δ) → Option δ | _, nil, nil, f => none | _, @cons _ n a as, cons b bs, f => some (f a b) def Vec.mapHead3 {α β δ} : {n : Nat} → Vec α n → Vec β n → (α → β → δ) → Option δ | _, nil, nil, f => none | _, cons (tail := as) (head := a), cons b bs, f => some (f a b) inductive Foo | mk₁ (x y z w : Nat) | mk₂ (x y z w : Nat) def Foo.z : Foo → Nat | mk₁ (z := z) .. => z | mk₂ (z := z) .. => z #eval (Foo.mk₁ 10 20 30 40).z theorem ex7 : (Foo.mk₁ 10 20 30 40).z = 30 := rfl def Foo.addY? : Foo × Foo → Option Nat | (mk₁ (y := y₁) .., mk₁ (y := y₂) ..) => some (y₁ + y₂) | _ => none #eval Foo.addY? (Foo.mk₁ 1 2 3 4, Foo.mk₁ 10 20 30 40) theorem ex8 : Foo.addY? (Foo.mk₁ 1 2 3 4, Foo.mk₁ 10 20 30 40) = some 22 := rfl instance {α} : Inhabited (Sigma fun m => Vec α m) := ⟨⟨0, Vec.nil⟩⟩ partial def filter {α} (p : α → Bool) : {n : Nat} → Vec α n → Sigma fun m => Vec α m | _, Vec.nil => ⟨0, Vec.nil⟩ | _, Vec.cons x xs => match p x, filter p xs with | true, ⟨_, ys⟩ => ⟨_, Vec.cons x ys⟩ | false, ys => ys inductive Bla | ofNat (x : Nat) | ofBool (x : Bool) def Bla.optional? : Bla → Option Nat | ofNat x => some x | ofBool _ => none def Bla.isNat? (b : Bla) : Option { x : Nat // optional? b = some x } := match b.optional? with | some y => some ⟨y, rfl⟩ | none => none def foo (b : Bla) : Option Nat := b.optional? theorem fooEq (b : Bla) : foo b = b.optional? := rfl def Bla.isNat2? (b : Bla) : Option { x : Nat // optional? b = some x } := match h:foo b with | some y => some ⟨y, Eq.trans (fooEq b).symm h⟩ | none => none def foo2 (x : Nat) : Nat := match x, rfl : (y : Nat) → x = y → Nat with | 0, h => 0 | x+1, h => 1
e89c05ff03c7fe78d10a4c5fd31c9418c7000f4f
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/rwEqThms.lean
d320382b20a016af79cf9dd60bd99a89b695744e
[ "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
791
lean
example {a : α} {as bs : List α} (h : bs = a::as) : as.length + 1 = bs.length := by rw [← List.length] trace_state -- lhs was folded rw [h] example {a : α} {as bs : List α} (h : as = bs) : (a::b::as).length = bs.length + 2 := by rw [List.length, List.length] trace_state -- lhs was unfolded rw [h] example {a : α} {as bs : List α} (h : as = bs) : (a::b::as).length = (b::bs).length + 1 := by conv => lhs; rw [List.length, List.length] trace_state -- lhs was unfolded conv => rhs; rw [List.length] trace_state -- rhs was unfolded rw [h] example {a : α} {as bs : List α} (h : as = bs) : id (id ((a::b::as).length)) = (b::bs).length + 1 := by rw [id] trace_state rw [id] trace_state rw [List.length, List.length, List.length] trace_state rw [h]
f672df0f7e00096b7b94c80c52ab3636629662d0
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/free_auto.lean
013d7d6b7952eb4d63f94d8c91d5ae1ae413e96b
[]
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,820
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.equiv.basic import Mathlib.control.applicative import Mathlib.control.traversable.basic import Mathlib.PostPort universes u l v u_1 namespace Mathlib /-! # Free constructions ## Main definitions * `free_magma α`: free magma (structure with binary operation without any axioms) over alphabet `α`, defined inductively, with traversable instance and decidable equality. * `magma.free_semigroup α`: free semigroup over magma `α`. * `free_semigroup α`: free semigroup over alphabet `α`, defined as a synonym for `α × list α` (i.e. nonempty lists), with traversable instance and decidable equality. * `free_semigroup_free_magma α`: isomorphism between `magma.free_semigroup (free_magma α)` and `free_semigroup α`. -/ /-- Free magma over a given alphabet. -/ inductive free_magma (α : Type u) where | of : α → free_magma α | mul : free_magma α → free_magma α → free_magma α /-- Free nonabelian additive magma over a given alphabet. -/ inductive free_add_magma (α : Type u) where | of : α → free_add_magma α | add : free_add_magma α → free_add_magma α → free_add_magma α namespace free_magma protected instance Mathlib.free_add_magma.inhabited {α : Type u} [Inhabited α] : Inhabited (free_add_magma α) := { default := free_add_magma.of Inhabited.default } protected instance Mathlib.free_add_magma.has_add {α : Type u} : Add (free_add_magma α) := { add := free_add_magma.add } @[simp] theorem Mathlib.free_add_magma.add_eq {α : Type u} (x : free_add_magma α) (y : free_add_magma α) : free_add_magma.add x y = x + y := rfl /-- Recursor for `free_magma` using `x * y` instead of `free_magma.mul x y`. -/ def Mathlib.free_add_magma.rec_on' {α : Type u} {C : free_add_magma α → Sort l} (x : free_add_magma α) (ih1 : (x : α) → C (free_add_magma.of x)) (ih2 : (x y : free_add_magma α) → C x → C y → C (x + y)) : C x := free_add_magma.rec_on x ih1 ih2 end free_magma /-- Lifts a function `α → β` to a magma homomorphism `free_magma α → β` given a magma `β`. -/ def free_magma.lift {α : Type u} {β : Type v} [Mul β] (f : α → β) : free_magma α → β := sorry /-- Lifts a function `α → β` to an additive magma homomorphism `free_add_magma α → β` given an additive magma `β`. -/ def free_add_magma.lift {α : Type u} {β : Type v} [Add β] (f : α → β) : free_add_magma α → β := sorry namespace free_magma @[simp] theorem Mathlib.free_add_magma.lift_of {α : Type u} {β : Type v} [Add β] (f : α → β) (x : α) : free_add_magma.lift f (free_add_magma.of x) = f x := rfl @[simp] theorem lift_mul {α : Type u} {β : Type v} [Mul β] (f : α → β) (x : free_magma α) (y : free_magma α) : lift f (x * y) = lift f x * lift f y := rfl theorem lift_unique {α : Type u} {β : Type v} [Mul β] (f : free_magma α → β) (hf : ∀ (x y : free_magma α), f (x * y) = f x * f y) : f = lift (f ∘ of) := sorry end free_magma /-- The unique magma homomorphism `free_magma α → free_magma β` that sends each `of x` to `of (f x)`. -/ def free_magma.map {α : Type u} {β : Type v} (f : α → β) : free_magma α → free_magma β := sorry /-- The unique additive magma homomorphism `free_add_magma α → free_add_magma β` that sends each `of x` to `of (f x)`. -/ def free_add_magma.map {α : Type u} {β : Type v} (f : α → β) : free_add_magma α → free_add_magma β := sorry namespace free_magma @[simp] theorem Mathlib.free_add_magma.map_of {α : Type u} {β : Type v} (f : α → β) (x : α) : free_add_magma.map f (free_add_magma.of x) = free_add_magma.of (f x) := rfl @[simp] theorem Mathlib.free_add_magma.map_add {α : Type u} {β : Type v} (f : α → β) (x : free_add_magma α) (y : free_add_magma α) : free_add_magma.map f (x + y) = free_add_magma.map f x + free_add_magma.map f y := rfl protected instance Mathlib.free_add_magma.monad : Monad free_add_magma := sorry /-- Recursor on `free_magma` using `pure` instead of `of`. -/ protected def Mathlib.free_add_magma.rec_on'' {α : Type u} {C : free_add_magma α → Sort l} (x : free_add_magma α) (ih1 : (x : α) → C (pure x)) (ih2 : (x y : free_add_magma α) → C x → C y → C (x + y)) : C x := free_add_magma.rec_on' x ih1 ih2 @[simp] theorem Mathlib.free_add_magma.map_pure {α : Type u} {β : Type u} (f : α → β) (x : α) : f <$> pure x = pure (f x) := rfl @[simp] theorem Mathlib.free_add_magma.map_add' {α : Type u} {β : Type u} (f : α → β) (x : free_add_magma α) (y : free_add_magma α) : f <$> (x + y) = f <$> x + f <$> y := rfl @[simp] theorem Mathlib.free_add_magma.pure_bind {α : Type u} {β : Type u} (f : α → free_add_magma β) (x : α) : pure x >>= f = f x := rfl @[simp] theorem mul_bind {α : Type u} {β : Type u} (f : α → free_magma β) (x : free_magma α) (y : free_magma α) : x * y >>= f = (x >>= f) * (y >>= f) := rfl @[simp] theorem Mathlib.free_add_magma.pure_seq {α : Type u} {β : Type u} {f : α → β} {x : free_add_magma α} : pure f <*> x = f <$> x := rfl @[simp] theorem mul_seq {α : Type u} {β : Type u} {f : free_magma (α → β)} {g : free_magma (α → β)} {x : free_magma α} : f * g <*> x = (f <*> x) * (g <*> x) := rfl protected instance Mathlib.free_add_magma.is_lawful_monad : is_lawful_monad free_add_magma := sorry end free_magma /-- `free_magma` is traversable. -/ protected def free_magma.traverse {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u} (F : α → m β) : free_magma α → m (free_magma β) := sorry /-- `free_add_magma` is traversable. -/ protected def free_add_magma.traverse {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u} (F : α → m β) : free_add_magma α → m (free_add_magma β) := sorry namespace free_magma protected instance Mathlib.free_add_magma.traversable : traversable free_add_magma := traversable.mk free_add_magma.traverse @[simp] theorem Mathlib.free_add_magma.traverse_pure {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : α) : traverse F (pure x) = pure <$> F x := rfl @[simp] theorem Mathlib.free_add_magma.traverse_pure' {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) : traverse F ∘ pure = fun (x : α) => pure <$> F x := rfl @[simp] theorem Mathlib.free_add_magma.traverse_add {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : free_add_magma α) (y : free_add_magma α) : traverse F (x + y) = Add.add <$> traverse F x <*> traverse F y := rfl @[simp] theorem Mathlib.free_add_magma.traverse_add' {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) : function.comp (traverse F) ∘ Add.add = fun (x y : free_add_magma α) => Add.add <$> traverse F x <*> traverse F y := rfl @[simp] theorem Mathlib.free_add_magma.traverse_eq {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : free_add_magma α) : free_add_magma.traverse F x = traverse F x := rfl @[simp] theorem mul_map_seq {α : Type u} (x : free_magma α) (y : free_magma α) : Mul.mul <$> x <*> y = x * y := rfl protected instance Mathlib.free_add_magma.is_lawful_traversable : is_lawful_traversable free_add_magma := is_lawful_traversable.mk sorry sorry sorry sorry end free_magma /-- Representation of an element of a free magma. -/ protected def free_magma.repr {α : Type u} [has_repr α] : free_magma α → string := sorry /-- Representation of an element of a free additive magma. -/ protected def free_add_magma.repr {α : Type u} [has_repr α] : free_add_magma α → string := sorry protected instance free_add_magma.has_repr {α : Type u} [has_repr α] : has_repr (free_add_magma α) := has_repr.mk free_add_magma.repr /-- Length of an element of a free magma. -/ def free_magma.length {α : Type u} : free_magma α → ℕ := sorry /-- Length of an element of a free additive magma. -/ def free_add_magma.length {α : Type u} : free_add_magma α → ℕ := sorry /-- Associativity relations for a magma. -/ inductive magma.free_semigroup.r (α : Type u) [Mul α] : α → α → Prop where | intro : ∀ (x y z : α), magma.free_semigroup.r α (x * y * z) (x * (y * z)) | left : ∀ (w x y z : α), magma.free_semigroup.r α (w * (x * y * z)) (w * (x * (y * z))) /-- Associativity relations for an additive magma. -/ inductive add_magma.free_add_semigroup.r (α : Type u) [Add α] : α → α → Prop where | intro : ∀ (x y z : α), add_magma.free_add_semigroup.r α (x + y + z) (x + (y + z)) | left : ∀ (w x y z : α), add_magma.free_add_semigroup.r α (w + (x + y + z)) (w + (x + (y + z))) namespace magma /-- Free semigroup over a magma. -/ def free_semigroup (α : Type u) [Mul α] := Quot sorry namespace free_semigroup /-- Embedding from magma to its free semigroup. -/ def Mathlib.add_magma.free_add_semigroup.of {α : Type u} [Add α] : α → add_magma.free_add_semigroup α := Quot.mk (add_magma.free_add_semigroup.r α) protected instance Mathlib.add_magma.free_add_semigroup.inhabited {α : Type u} [Add α] [Inhabited α] : Inhabited (add_magma.free_add_semigroup α) := { default := add_magma.free_add_semigroup.of Inhabited.default } protected theorem Mathlib.add_magma.free_add_semigroup.induction_on {α : Type u} [Add α] {C : add_magma.free_add_semigroup α → Prop} (x : add_magma.free_add_semigroup α) (ih : ∀ (x : α), C (add_magma.free_add_semigroup.of x)) : C x := quot.induction_on x ih theorem of_mul_assoc {α : Type u} [Mul α] (x : α) (y : α) (z : α) : of (x * y * z) = of (x * (y * z)) := quot.sound (r.intro x y z) theorem of_mul_assoc_left {α : Type u} [Mul α] (w : α) (x : α) (y : α) (z : α) : of (w * (x * y * z)) = of (w * (x * (y * z))) := quot.sound (r.left w x y z) theorem of_mul_assoc_right {α : Type u} [Mul α] (w : α) (x : α) (y : α) (z : α) : of (w * x * y * z) = of (w * (x * y) * z) := sorry protected instance semigroup {α : Type u} [Mul α] : semigroup (free_semigroup α) := semigroup.mk (fun (x y : free_semigroup α) => quot.lift_on x (fun (p : α) => quot.lift_on y (fun (q : α) => Quot.mk (r α) (p * q)) sorry) sorry) sorry theorem Mathlib.add_magma.free_add_semigroup.of_add {α : Type u} [Add α] (x : α) (y : α) : add_magma.free_add_semigroup.of (x + y) = add_magma.free_add_semigroup.of x + add_magma.free_add_semigroup.of y := rfl /-- Lifts a magma homomorphism `α → β` to a semigroup homomorphism `magma.free_semigroup α → β` given a semigroup `β`. -/ def lift {α : Type u} [Mul α] {β : Type v} [semigroup β] (f : α → β) (hf : ∀ (x y : α), f (x * y) = f x * f y) : free_semigroup α → β := Quot.lift f sorry @[simp] theorem lift_of {α : Type u} [Mul α] {β : Type v} [semigroup β] (f : α → β) {hf : ∀ (x y : α), f (x * y) = f x * f y} (x : α) : lift f hf (of x) = f x := rfl @[simp] theorem lift_mul {α : Type u} [Mul α] {β : Type v} [semigroup β] (f : α → β) {hf : ∀ (x y : α), f (x * y) = f x * f y} (x : free_semigroup α) (y : free_semigroup α) : lift f hf (x * y) = lift f hf x * lift f hf y := quot.induction_on x fun (p : α) => quot.induction_on y fun (q : α) => hf p q theorem Mathlib.add_magma.free_add_semigroup.lift_unique {α : Type u} [Add α] {β : Type v} [add_semigroup β] (f : add_magma.free_add_semigroup α → β) (hf : ∀ (x y : add_magma.free_add_semigroup α), f (x + y) = f x + f y) : f = add_magma.free_add_semigroup.lift (f ∘ add_magma.free_add_semigroup.of) fun (p q : α) => hf (add_magma.free_add_semigroup.of p) (add_magma.free_add_semigroup.of q) := funext fun (x : add_magma.free_add_semigroup α) => quot.induction_on x fun (p : α) => rfl /-- From a magma homomorphism `α → β` to a semigroup homomorphism `magma.free_semigroup α → magma.free_semigroup β`. -/ def Mathlib.add_magma.free_add_semigroup.map {α : Type u} [Add α] {β : Type v} [Add β] (f : α → β) (hf : ∀ (x y : α), f (x + y) = f x + f y) : add_magma.free_add_semigroup α → add_magma.free_add_semigroup β := add_magma.free_add_semigroup.lift (add_magma.free_add_semigroup.of ∘ f) sorry @[simp] theorem Mathlib.add_magma.free_add_semigroup.map_of {α : Type u} [Add α] {β : Type v} [Add β] (f : α → β) {hf : ∀ (x y : α), f (x + y) = f x + f y} (x : α) : add_magma.free_add_semigroup.map f hf (add_magma.free_add_semigroup.of x) = add_magma.free_add_semigroup.of (f x) := rfl @[simp] theorem map_mul {α : Type u} [Mul α] {β : Type v} [Mul β] (f : α → β) {hf : ∀ (x y : α), f (x * y) = f x * f y} (x : free_semigroup α) (y : free_semigroup α) : map f hf (x * y) = map f hf x * map f hf y := lift_mul (of ∘ f) x y end free_semigroup end magma /-- Free semigroup over a given alphabet. (Note: In this definition, the free semigroup does not contain the empty word.) -/ def free_semigroup (α : Type u) := α × List α namespace free_semigroup protected instance semigroup {α : Type u} : semigroup (free_semigroup α) := semigroup.mk (fun (L1 L2 : free_semigroup α) => (prod.fst L1, prod.snd L1 ++ prod.fst L2 :: prod.snd L2)) sorry /-- The embedding `α → free_semigroup α`. -/ def Mathlib.free_add_semigroup.of {α : Type u} (x : α) : free_add_semigroup α := (x, []) protected instance Mathlib.free_add_semigroup.inhabited {α : Type u} [Inhabited α] : Inhabited (free_add_semigroup α) := { default := free_add_semigroup.of Inhabited.default } /-- Recursor for free semigroup using `of` and `*`. -/ protected def Mathlib.free_add_semigroup.rec_on {α : Type u} {C : free_add_semigroup α → Sort l} (x : free_add_semigroup α) (ih1 : (x : α) → C (free_add_semigroup.of x)) (ih2 : (x : α) → (y : free_add_semigroup α) → C (free_add_semigroup.of x) → C y → C (free_add_semigroup.of x + y)) : C x := prod.rec_on x fun (f : α) (s : List α) => list.rec_on s ih1 (fun (hd : α) (tl : List α) (ih : (_a : α) → C (_a, tl)) (f : α) => ih2 f (hd, tl) (ih1 f) (ih hd)) f end free_semigroup /-- Auxiliary function for `free_semigroup.lift`. -/ def free_semigroup.lift' {α : Type u} {β : Type v} [semigroup β] (f : α → β) : α → List α → β := sorry /-- Auxiliary function for `free_semigroup.lift`. -/ def free_add_semigroup.lift' {α : Type u} {β : Type v} [add_semigroup β] (f : α → β) : α → List α → β := sorry namespace free_semigroup /-- Lifts a function `α → β` to a semigroup homomorphism `free_semigroup α → β` given a semigroup `β`. -/ def lift {α : Type u} {β : Type v} [semigroup β] (f : α → β) (x : free_semigroup α) : β := lift' f (prod.fst x) (prod.snd x) @[simp] theorem lift_of {α : Type u} {β : Type v} [semigroup β] (f : α → β) (x : α) : lift f (of x) = f x := rfl theorem lift_of_mul {α : Type u} {β : Type v} [semigroup β] (f : α → β) (x : α) (y : free_semigroup α) : lift f (of x * y) = f x * lift f y := rfl @[simp] theorem Mathlib.free_add_semigroup.lift_add {α : Type u} {β : Type v} [add_semigroup β] (f : α → β) (x : free_add_semigroup α) (y : free_add_semigroup α) : free_add_semigroup.lift f (x + y) = free_add_semigroup.lift f x + free_add_semigroup.lift f y := sorry theorem Mathlib.free_add_semigroup.lift_unique {α : Type u} {β : Type v} [add_semigroup β] (f : free_add_semigroup α → β) (hf : ∀ (x y : free_add_semigroup α), f (x + y) = f x + f y) : f = free_add_semigroup.lift (f ∘ free_add_semigroup.of) := sorry /-- The unique semigroup homomorphism that sends `of x` to `of (f x)`. -/ def Mathlib.free_add_semigroup.map {α : Type u} {β : Type v} (f : α → β) : free_add_semigroup α → free_add_semigroup β := free_add_semigroup.lift (free_add_semigroup.of ∘ f) @[simp] theorem Mathlib.free_add_semigroup.map_of {α : Type u} {β : Type v} (f : α → β) (x : α) : free_add_semigroup.map f (free_add_semigroup.of x) = free_add_semigroup.of (f x) := rfl @[simp] theorem Mathlib.free_add_semigroup.map_add {α : Type u} {β : Type v} (f : α → β) (x : free_add_semigroup α) (y : free_add_semigroup α) : free_add_semigroup.map f (x + y) = free_add_semigroup.map f x + free_add_semigroup.map f y := free_add_semigroup.lift_add (free_add_semigroup.of ∘ f) x y protected instance Mathlib.free_add_semigroup.monad : Monad free_add_semigroup := sorry /-- Recursor that uses `pure` instead of `of`. -/ def rec_on' {α : Type u} {C : free_semigroup α → Sort l} (x : free_semigroup α) (ih1 : (x : α) → C (pure x)) (ih2 : (x : α) → (y : free_semigroup α) → C (pure x) → C y → C (pure x * y)) : C x := free_semigroup.rec_on x ih1 ih2 @[simp] theorem map_pure {α : Type u} {β : Type u} (f : α → β) (x : α) : f <$> pure x = pure (f x) := rfl @[simp] theorem map_mul' {α : Type u} {β : Type u} (f : α → β) (x : free_semigroup α) (y : free_semigroup α) : f <$> (x * y) = f <$> x * f <$> y := map_mul f x y @[simp] theorem pure_bind {α : Type u} {β : Type u} (f : α → free_semigroup β) (x : α) : pure x >>= f = f x := rfl @[simp] theorem mul_bind {α : Type u} {β : Type u} (f : α → free_semigroup β) (x : free_semigroup α) (y : free_semigroup α) : x * y >>= f = (x >>= f) * (y >>= f) := lift_mul f x y @[simp] theorem Mathlib.free_add_semigroup.pure_seq {α : Type u} {β : Type u} {f : α → β} {x : free_add_semigroup α} : pure f <*> x = f <$> x := rfl @[simp] theorem mul_seq {α : Type u} {β : Type u} {f : free_semigroup (α → β)} {g : free_semigroup (α → β)} {x : free_semigroup α} : f * g <*> x = (f <*> x) * (g <*> x) := mul_bind (fun (_x : α → β) => (fun (α β : Type u) (f : α → β) (x : free_semigroup α) => lift (of ∘ f) x) α β _x x) f g protected instance Mathlib.free_add_semigroup.is_lawful_monad : is_lawful_monad free_add_semigroup := sorry /-- `free_semigroup` is traversable. -/ protected def Mathlib.free_add_semigroup.traverse {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u} (F : α → m β) (x : free_add_semigroup α) : m (free_add_semigroup β) := free_add_semigroup.rec_on' x (fun (x : α) => pure <$> F x) fun (x : α) (y : free_add_semigroup α) (ihx ihy : m (free_add_semigroup β)) => Add.add <$> ihx <*> ihy protected instance Mathlib.free_add_semigroup.traversable : traversable free_add_semigroup := traversable.mk free_add_semigroup.traverse @[simp] theorem traverse_pure {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : α) : traverse F (pure x) = pure <$> F x := rfl @[simp] theorem Mathlib.free_add_semigroup.traverse_pure' {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) : traverse F ∘ pure = fun (x : α) => pure <$> F x := rfl @[simp] theorem Mathlib.free_add_semigroup.traverse_add {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) [is_lawful_applicative m] (x : free_add_semigroup α) (y : free_add_semigroup α) : traverse F (x + y) = Add.add <$> traverse F x <*> traverse F y := sorry @[simp] theorem Mathlib.free_add_semigroup.traverse_add' {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) [is_lawful_applicative m] : function.comp (traverse F) ∘ Add.add = fun (x y : free_add_semigroup α) => Add.add <$> traverse F x <*> traverse F y := funext fun (x : free_add_semigroup α) => funext fun (y : free_add_semigroup α) => free_add_semigroup.traverse_add F x y @[simp] theorem Mathlib.free_add_semigroup.traverse_eq {α : Type u} {β : Type u} {m : Type u → Type u} [Applicative m] (F : α → m β) (x : free_add_semigroup α) : free_add_semigroup.traverse F x = traverse F x := rfl @[simp] theorem Mathlib.free_add_semigroup.add_map_seq {α : Type u} (x : free_add_semigroup α) (y : free_add_semigroup α) : Add.add <$> x <*> y = x + y := rfl protected instance Mathlib.free_add_semigroup.is_lawful_traversable : is_lawful_traversable free_add_semigroup := is_lawful_traversable.mk sorry sorry sorry sorry protected instance Mathlib.free_add_semigroup.decidable_eq {α : Type u} [DecidableEq α] : DecidableEq (free_add_semigroup α) := prod.decidable_eq end free_semigroup /-- Isomorphism between `magma.free_semigroup (free_magma α)` and `free_semigroup α`. -/ def free_add_semigroup_free_add_magma (α : Type u) : add_magma.free_add_semigroup (free_add_magma α) ≃ free_add_semigroup α := equiv.mk (add_magma.free_add_semigroup.lift (free_add_magma.lift free_add_semigroup.of) sorry) (free_add_semigroup.lift (add_magma.free_add_semigroup.of ∘ free_add_magma.of)) sorry sorry @[simp] theorem free_semigroup_free_magma_mul {α : Type u} (x : magma.free_semigroup (free_magma α)) (y : magma.free_semigroup (free_magma α)) : coe_fn (free_semigroup_free_magma α) (x * y) = coe_fn (free_semigroup_free_magma α) x * coe_fn (free_semigroup_free_magma α) y := magma.free_semigroup.lift_mul (free_magma.lift free_semigroup.of) x y end Mathlib
db283a2d29265916775168ef3b6ee84f43932280
1a61aba1b67cddccce19532a9596efe44be4285f
/tests/lean/run/rewriter12.lean
e20076f20f7896763872a16f42b9e19da6fa38cc
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
302
lean
import data.nat open nat constant f : nat → nat example (x y : nat) (H1 : (λ z, z + 0) x = y) : f x = f y := by rewrite [▸* at H1, ^[add, nat.rec_on, of_num] at H1, H1] example (x y : nat) (H1 : (λ z, z + 0) x = y) : f x = f y := by rewrite [▸* at H1, ↑[add, nat.rec_on, of_num] at H1, H1]
4353558909aaefddafd380abb54d5562bec5e1d8
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/algebra/pointwise.lean
d57aaf48f22ec462705219faf042b97f1938ca70
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
13,129
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Floris van Doorn -/ import algebra.module /-! # Pointwise addition, multiplication, and scalar multiplication of sets. This file defines pointwise algebraic operations on sets. * For a type `α` with multiplication, multiplication is defined on `set α` by taking `s * t` to be the set of all `x * y` where `x ∈ s` and `y ∈ t`. Similarly for addition. * For `α` a semigroup, `set α` is a semigroup. * If `α` is a (commutative) monoid, we define an alias `set_semiring α` for `set α`, which then becomes a (commutative) semiring with union as addition and pointwise multiplication as multiplication. * For a type `β` with scalar multiplication by another type `α`, this file defines a scalar multiplication of `set β` by `set α` and a separate scalar multiplication of `set β` by `α`. Appropriate definitions and results are also transported to the additive theory via `to_additive`. ## Implementation notes * The following expressions are considered in simp-normal form in a group: `(λ h, h * g) ⁻¹' s`, `(λ h, g * h) ⁻¹' s`, `(λ h, h * g⁻¹) ⁻¹' s`, `(λ h, g⁻¹ * h) ⁻¹' s`, `s * t`, `s⁻¹`, `(1 : set _)` (and similarly for additive variants). Expressions equal to one of these will be simplified. ## Tags set multiplication, set addition, pointwise addition, pointwise multiplication -/ namespace set open function variables {α : Type*} {β : Type*} {s s₁ s₂ t t₁ t₂ u : set α} {a b : α} {x y : β} /-! Properties about 1 -/ @[to_additive] instance [has_one α] : has_one (set α) := ⟨{1}⟩ @[simp, to_additive] lemma singleton_one [has_one α] : ({1} : set α) = 1 := rfl @[simp, to_additive] lemma mem_one [has_one α] : a ∈ (1 : set α) ↔ a = 1 := iff.rfl @[to_additive] lemma one_mem_one [has_one α] : (1 : α) ∈ (1 : set α) := eq.refl _ @[simp, to_additive] theorem one_subset [has_one α] : 1 ⊆ s ↔ (1 : α) ∈ s := singleton_subset_iff @[to_additive] theorem one_nonempty [has_one α] : (1 : set α).nonempty := ⟨1, rfl⟩ @[simp, to_additive] theorem image_one [has_one α] {f : α → β} : f '' 1 = {f 1} := image_singleton /-! Properties about multiplication -/ @[to_additive] instance [has_mul α] : has_mul (set α) := ⟨image2 has_mul.mul⟩ @[simp, to_additive] lemma image2_mul [has_mul α] : image2 has_mul.mul s t = s * t := rfl @[to_additive] lemma mem_mul [has_mul α] : a ∈ s * t ↔ ∃ x y, x ∈ s ∧ y ∈ t ∧ x * y = a := iff.rfl @[to_additive] lemma mul_mem_mul [has_mul α] (ha : a ∈ s) (hb : b ∈ t) : a * b ∈ s * t := mem_image2_of_mem ha hb @[to_additive add_image_prod] lemma image_mul_prod [has_mul α] : (λ x : α × α, x.fst * x.snd) '' s.prod t = s * t := image_prod _ @[simp, to_additive] lemma image_mul_left [group α] : (λ b, a * b) '' t = (λ b, a⁻¹ * b) ⁻¹' t := by { rw image_eq_preimage_of_inverse; intro c; simp } @[simp, to_additive] lemma image_mul_right [group α] : (λ a, a * b) '' t = (λ a, a * b⁻¹) ⁻¹' t := by { rw image_eq_preimage_of_inverse; intro c; simp } @[to_additive] lemma image_mul_left' [group α] : (λ b, a⁻¹ * b) '' t = (λ b, a * b) ⁻¹' t := by simp @[to_additive] lemma image_mul_right' [group α] : (λ a, a * b⁻¹) '' t = (λ a, a * b) ⁻¹' t := by simp @[simp, to_additive] lemma preimage_mul_left_one [group α] : (λ b, a * b) ⁻¹' 1 = {a⁻¹} := by rw [← image_mul_left', image_one, mul_one] @[simp, to_additive] lemma preimage_mul_right_one [group α] : (λ a, a * b) ⁻¹' 1 = {b⁻¹} := by rw [← image_mul_right', image_one, one_mul] @[to_additive] lemma preimage_mul_left_one' [group α] : (λ b, a⁻¹ * b) ⁻¹' 1 = {a} := by simp @[to_additive] lemma preimage_mul_right_one' [group α] : (λ a, a * b) ⁻¹' 1 = {b⁻¹} := by simp @[simp, to_additive] lemma mul_singleton [has_mul α] : s * {b} = (λ a, a * b) '' s := image2_singleton_right @[simp, to_additive] lemma singleton_mul [has_mul α] : {a} * t = (λ b, a * b) '' t := image2_singleton_left @[simp, to_additive] lemma singleton_mul_singleton [has_mul α] : ({a} : set α) * {b} = {a * b} := image2_singleton @[to_additive set.add_semigroup] instance [semigroup α] : semigroup (set α) := { mul_assoc := by { intros, simp only [← image2_mul, image2_image2_left, image2_image2_right, mul_assoc] }, ..set.has_mul } @[to_additive set.add_monoid] instance [monoid α] : monoid (set α) := { mul_one := λ s, by { simp only [← singleton_one, mul_singleton, mul_one, image_id'] }, one_mul := λ s, by { simp only [← singleton_one, singleton_mul, one_mul, image_id'] }, ..set.semigroup, ..set.has_one } @[to_additive] protected lemma mul_comm [comm_semigroup α] : s * t = t * s := by simp only [← image2_mul, image2_swap _ s, mul_comm] @[to_additive set.add_comm_monoid] instance [comm_monoid α] : comm_monoid (set α) := { mul_comm := λ _ _, set.mul_comm, ..set.monoid } @[to_additive] lemma singleton.is_mul_hom [has_mul α] : is_mul_hom (singleton : α → set α) := { map_mul := λ a b, singleton_mul_singleton.symm } @[simp, to_additive] lemma empty_mul [has_mul α] : ∅ * s = ∅ := image2_empty_left @[simp, to_additive] lemma mul_empty [has_mul α] : s * ∅ = ∅ := image2_empty_right @[to_additive] lemma mul_subset_mul [has_mul α] (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ * s₂ ⊆ t₁ * t₂ := image2_subset h₁ h₂ @[to_additive] lemma union_mul [has_mul α] : (s ∪ t) * u = (s * u) ∪ (t * u) := image2_union_left @[to_additive] lemma mul_union [has_mul α] : s * (t ∪ u) = (s * t) ∪ (s * u) := image2_union_right @[to_additive] lemma Union_mul_left_image [has_mul α] : (⋃ a ∈ s, (λ x, a * x) '' t) = s * t := Union_image_left _ @[to_additive] lemma Union_mul_right_image [has_mul α] : (⋃ a ∈ t, (λ x, x * a) '' s) = s * t := Union_image_right _ @[simp, to_additive] lemma univ_mul_univ [monoid α] : (univ : set α) * univ = univ := begin have : ∀x, ∃a b : α, a * b = x := λx, ⟨x, ⟨1, mul_one x⟩⟩, simpa only [mem_mul, eq_univ_iff_forall, mem_univ, true_and] end /-- `singleton` is a monoid hom. -/ @[to_additive singleton_add_hom "singleton is an add monoid hom"] def singleton_hom [monoid α] : α →* set α := { to_fun := singleton, map_one' := rfl, map_mul' := λ a b, singleton_mul_singleton.symm } @[to_additive] lemma nonempty.mul [has_mul α] : s.nonempty → t.nonempty → (s * t).nonempty := nonempty.image2 @[to_additive] lemma finite.mul [has_mul α] (hs : finite s) (ht : finite t) : finite (s * t) := hs.image2 _ ht /-- multiplication preserves finiteness -/ @[to_additive "addition preserves finiteness"] def fintype_mul [has_mul α] [decidable_eq α] (s t : set α) [hs : fintype s] [ht : fintype t] : fintype (s * t : set α) := set.fintype_image2 _ s t /-! Properties about inversion -/ @[to_additive set.has_neg'] -- todo: remove prime once name becomes available instance [has_inv α] : has_inv (set α) := ⟨preimage has_inv.inv⟩ @[simp, to_additive] lemma mem_inv [has_inv α] : a ∈ s⁻¹ ↔ a⁻¹ ∈ s := iff.rfl @[to_additive] lemma inv_mem_inv [group α] : a⁻¹ ∈ s⁻¹ ↔ a ∈ s := by simp only [mem_inv, inv_inv] @[simp, to_additive] lemma inv_preimage [has_inv α] : has_inv.inv ⁻¹' s = s⁻¹ := rfl @[simp, to_additive] lemma image_inv [group α] : has_inv.inv '' s = s⁻¹ := by { simp only [← inv_preimage], rw [image_eq_preimage_of_inverse]; intro; simp only [inv_inv] } @[simp, to_additive] lemma inter_inv [has_inv α] : (s ∩ t)⁻¹ = s⁻¹ ∩ t⁻¹ := preimage_inter @[simp, to_additive] lemma union_inv [has_inv α] : (s ∪ t)⁻¹ = s⁻¹ ∪ t⁻¹ := preimage_union @[simp, to_additive] lemma compl_inv [has_inv α] : (sᶜ)⁻¹ = (s⁻¹)ᶜ := preimage_compl @[simp, to_additive] protected lemma inv_inv [group α] : s⁻¹⁻¹ = s := by { simp only [← inv_preimage, preimage_preimage, inv_inv, preimage_id'] } @[simp, to_additive] protected lemma univ_inv [group α] : (univ : set α)⁻¹ = univ := preimage_univ /-! Properties about scalar multiplication -/ /-- Scaling a set: multiplying every element by a scalar. -/ instance has_scalar_set [has_scalar α β] : has_scalar α (set β) := ⟨λ a, image (has_scalar.smul a)⟩ @[simp] lemma image_smul [has_scalar α β] {t : set β} : (λ x, a • x) '' t = a • t := rfl lemma mem_smul_set [has_scalar α β] {t : set β} : x ∈ a • t ↔ ∃ y, y ∈ t ∧ a • y = x := iff.rfl lemma smul_mem_smul_set [has_scalar α β] {t : set β} (hy : y ∈ t) : a • y ∈ a • t := ⟨y, hy, rfl⟩ lemma smul_set_union [has_scalar α β] {s t : set β} : a • (s ∪ t) = a • s ∪ a • t := by simp only [← image_smul, image_union] @[simp] lemma smul_set_empty [has_scalar α β] (a : α) : a • (∅ : set β) = ∅ := by rw [← image_smul, image_empty] lemma smul_set_mono [has_scalar α β] {s t : set β} (h : s ⊆ t) : a • s ⊆ a • t := by { simp only [← image_smul, image_subset, h] } /-- Pointwise scalar multiplication by a set of scalars. -/ instance [has_scalar α β] : has_scalar (set α) (set β) := ⟨image2 has_scalar.smul⟩ @[simp] lemma image2_smul [has_scalar α β] {t : set β} : image2 has_scalar.smul s t = s • t := rfl lemma mem_smul [has_scalar α β] {t : set β} : x ∈ s • t ↔ ∃ a y, a ∈ s ∧ y ∈ t ∧ a • y = x := iff.rfl lemma image_smul_prod [has_scalar α β] {t : set β} : (λ x : α × β, x.fst • x.snd) '' s.prod t = s • t := image_prod _ lemma singleton_smul [has_scalar α β] {t : set β} : ({a} : set α) • t = a • t := image2_singleton_left section monoid /-! `set α` as a `(∪,*)`-semiring -/ /-- An alias for `set α`, which has a semiring structure given by `∪` as "addition" and pointwise multiplication `*` as "multiplication". -/ @[derive inhabited] def set_semiring (α : Type*) : Type* := set α /-- The identitiy function `set α → set_semiring α`. -/ protected def up (s : set α) : set_semiring α := s /-- The identitiy function `set_semiring α → set α`. -/ protected def set_semiring.down (s : set_semiring α) : set α := s @[simp] protected lemma down_up {s : set α} : s.up.down = s := rfl @[simp] protected lemma up_down {s : set_semiring α} : s.down.up = s := rfl instance set_semiring.semiring [monoid α] : semiring (set_semiring α) := { add := λ s t, (s ∪ t : set α), zero := (∅ : set α), add_assoc := union_assoc, zero_add := empty_union, add_zero := union_empty, add_comm := union_comm, zero_mul := λ s, empty_mul, mul_zero := λ s, mul_empty, left_distrib := λ _ _ _, mul_union, right_distrib := λ _ _ _, union_mul, ..set.monoid } instance set_semiring.comm_semiring [comm_monoid α] : comm_semiring (set_semiring α) := { ..set.comm_monoid, ..set_semiring.semiring } /-- A multiplicative action of a monoid on a type β gives also a multiplicative action on the subsets of β. -/ instance mul_action_set [monoid α] [mul_action α β] : mul_action α (set β) := { mul_smul := by { intros, simp only [← image_smul, image_image, ← mul_smul] }, one_smul := by { intros, simp only [← image_smul, image_eta, one_smul, image_id'] }, ..set.has_scalar_set } section is_mul_hom open is_mul_hom variables [has_mul α] [has_mul β] (m : α → β) [is_mul_hom m] @[to_additive] lemma image_mul : m '' (s * t) = m '' s * m '' t := by { simp only [← image2_mul, image_image2, image2_image_left, image2_image_right, map_mul m] } @[to_additive] lemma preimage_mul_preimage_subset {s t : set β} : m ⁻¹' s * m ⁻¹' t ⊆ m ⁻¹' (s * t) := by { rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, _, ‹_›, ‹_›, (map_mul _ _ _).symm ⟩ } end is_mul_hom /-- The image of a set under function is a ring homomorphism with respect to the pointwise operations on sets. -/ def image_hom [monoid α] [monoid β] (f : α →* β) : set_semiring α →+* set_semiring β := { to_fun := image f, map_zero' := image_empty _, map_one' := by simp only [← singleton_one, image_singleton, is_monoid_hom.map_one f], map_add' := image_union _, map_mul' := λ _ _, image_mul _ } end monoid end set section open set variables {α : Type*} {β : Type*} /-- A nonempty set in a semimodule is scaled by zero to the singleton containing 0 in the semimodule. -/ lemma zero_smul_set [semiring α] [add_comm_monoid β] [semimodule α β] {s : set β} (h : s.nonempty) : (0 : α) • s = (0 : set β) := by simp only [← image_smul, image_eta, zero_smul, h.image_const, singleton_zero] lemma mem_inv_smul_set_iff [field α] [mul_action α β] {a : α} (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a⁻¹ • A ↔ a • x ∈ A := by simp only [← image_smul, mem_image, inv_smul_eq_iff ha, exists_eq_right] lemma mem_smul_set_iff_inv_smul_mem [field α] [mul_action α β] {a : α} (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a • A ↔ a⁻¹ • x ∈ A := by rw [← mem_inv_smul_set_iff $ inv_ne_zero ha, inv_inv'] end
29c1115187c57b8356c81ad79b5d92869876cfcc
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/combinatorics/set_family/harris_kleitman.lean
236ffca1e72bfc4165a084b848a5a8e25b2afb3f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,256
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import combinatorics.set_family.compression.down import order.upper_lower.basic import data.fintype.big_operators /-! # Harris-Kleitman inequality > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file proves the Harris-Kleitman inequality. This relates `𝒜.card * ℬ.card` and `2 ^ card α * (𝒜 ∩ ℬ).card` where `𝒜` and `ℬ` are upward- or downcard-closed finite families of finsets. This can be interpreted as saying that any two lower sets (resp. any two upper sets) correlate in the uniform measure. ## Main declarations * `is_lower_set.le_card_inter_finset`: One form of the Harris-Kleitman inequality. ## References * [D. J. Kleitman, *Families of non-disjoint subsets*][kleitman1966] -/ open finset open_locale big_operators variables {α : Type*} [decidable_eq α] {𝒜 ℬ : finset (finset α)} {s : finset α} {a : α} lemma is_lower_set.non_member_subfamily (h : is_lower_set (𝒜 : set (finset α))) : is_lower_set (𝒜.non_member_subfamily a : set (finset α)) := λ s t hts, by { simp_rw [mem_coe, mem_non_member_subfamily], exact and.imp (h hts) (mt $ @hts _) } lemma is_lower_set.member_subfamily (h : is_lower_set (𝒜 : set (finset α))) : is_lower_set (𝒜.member_subfamily a : set (finset α)) := begin rintro s t hts, simp_rw [mem_coe, mem_member_subfamily], exact and.imp (h $ insert_subset_insert _ hts) (mt $ @hts _), end lemma is_lower_set.member_subfamily_subset_non_member_subfamily (h : is_lower_set (𝒜 : set (finset α))) : 𝒜.member_subfamily a ⊆ 𝒜.non_member_subfamily a := λ s, by { rw [mem_member_subfamily, mem_non_member_subfamily], exact and.imp_left (h $ subset_insert _ _) } /-- **Harris-Kleitman inequality**: Any two lower sets of finsets correlate. -/ lemma is_lower_set.le_card_inter_finset' (h𝒜 : is_lower_set (𝒜 : set (finset α))) (hℬ : is_lower_set (ℬ : set (finset α))) (h𝒜s : ∀ t ∈ 𝒜, t ⊆ s) (hℬs : ∀ t ∈ ℬ, t ⊆ s) : 𝒜.card * ℬ.card ≤ 2 ^ s.card * (𝒜 ∩ ℬ).card := begin induction s using finset.induction with a s hs ih generalizing 𝒜 ℬ, { simp_rw [subset_empty, ←subset_singleton_iff', subset_singleton_iff] at h𝒜s hℬs, obtain rfl | rfl := h𝒜s, { simp only [card_empty, empty_inter, mul_zero, zero_mul] }, obtain rfl | rfl := hℬs, { simp only [card_empty, inter_empty, mul_zero, zero_mul] }, { simp only [card_empty, pow_zero, inter_singleton_of_mem, mem_singleton, card_singleton] } }, rw [card_insert_of_not_mem hs, ←card_member_subfamily_add_card_non_member_subfamily a 𝒜, ←card_member_subfamily_add_card_non_member_subfamily a ℬ, add_mul, mul_add, mul_add, add_comm (_ * _), add_add_add_comm], refine (add_le_add_right (mul_add_mul_le_mul_add_mul (card_le_of_subset h𝒜.member_subfamily_subset_non_member_subfamily) $ card_le_of_subset hℬ.member_subfamily_subset_non_member_subfamily) _).trans _, rw [←two_mul, pow_succ, mul_assoc], have h₀ : ∀ 𝒞 : finset (finset α), (∀ t ∈ 𝒞, t ⊆ insert a s) → ∀ t ∈ 𝒞.non_member_subfamily a, t ⊆ s, { rintro 𝒞 h𝒞 t ht, rw mem_non_member_subfamily at ht, exact (subset_insert_iff_of_not_mem ht.2).1 (h𝒞 _ ht.1) }, have h₁ : ∀ 𝒞 : finset (finset α), (∀ t ∈ 𝒞, t ⊆ insert a s) → ∀ t ∈ 𝒞.member_subfamily a, t ⊆ s, { rintro 𝒞 h𝒞 t ht, rw mem_member_subfamily at ht, exact (subset_insert_iff_of_not_mem ht.2).1 ((subset_insert _ _).trans $ h𝒞 _ ht.1) }, refine mul_le_mul_left' _ _, refine (add_le_add (ih (h𝒜.member_subfamily) (hℬ.member_subfamily) (h₁ _ h𝒜s) $ h₁ _ hℬs) $ ih (h𝒜.non_member_subfamily) (hℬ.non_member_subfamily) (h₀ _ h𝒜s) $ h₀ _ hℬs).trans_eq _, rw [←mul_add, ←member_subfamily_inter, ←non_member_subfamily_inter, card_member_subfamily_add_card_non_member_subfamily], end variables [fintype α] /-- **Harris-Kleitman inequality**: Any two lower sets of finsets correlate. -/ lemma is_lower_set.le_card_inter_finset (h𝒜 : is_lower_set (𝒜 : set (finset α))) (hℬ : is_lower_set (ℬ : set (finset α))) : 𝒜.card * ℬ.card ≤ 2 ^ fintype.card α * (𝒜 ∩ ℬ).card := h𝒜.le_card_inter_finset' hℬ (λ _ _, subset_univ _) $ λ _ _, subset_univ _ /-- **Harris-Kleitman inequality**: Upper sets and lower sets of finsets anticorrelate. -/ lemma is_upper_set.card_inter_le_finset (h𝒜 : is_upper_set (𝒜 : set (finset α))) (hℬ : is_lower_set (ℬ : set (finset α))) : 2 ^ fintype.card α * (𝒜 ∩ ℬ).card ≤ 𝒜.card * ℬ.card := begin rw [←is_lower_set_compl, ←coe_compl] at h𝒜, have := h𝒜.le_card_inter_finset hℬ, rwa [card_compl, fintype.card_finset, tsub_mul, tsub_le_iff_tsub_le, ←mul_tsub, ←card_sdiff (inter_subset_right _ _), sdiff_inter_self_right, sdiff_compl, _root_.inf_comm] at this, end /-- **Harris-Kleitman inequality**: Lower sets and upper sets of finsets anticorrelate. -/ lemma is_lower_set.card_inter_le_finset (h𝒜 : is_lower_set (𝒜 : set (finset α))) (hℬ : is_upper_set (ℬ : set (finset α))) : 2 ^ fintype.card α * (𝒜 ∩ ℬ).card ≤ 𝒜.card * ℬ.card := by { rw [inter_comm, mul_comm 𝒜.card], exact hℬ.card_inter_le_finset h𝒜 } /-- **Harris-Kleitman inequality**: Any two upper sets of finsets correlate. -/ lemma is_upper_set.le_card_inter_finset (h𝒜 : is_upper_set (𝒜 : set (finset α))) (hℬ : is_upper_set (ℬ : set (finset α))) : 𝒜.card * ℬ.card ≤ 2 ^ fintype.card α * (𝒜 ∩ ℬ).card := begin rw [←is_lower_set_compl, ←coe_compl] at h𝒜, have := h𝒜.card_inter_le_finset hℬ, rwa [card_compl, fintype.card_finset, tsub_mul, le_tsub_iff_le_tsub, ←mul_tsub, ←card_sdiff (inter_subset_right _ _), sdiff_inter_self_right, sdiff_compl, _root_.inf_comm] at this, { exact mul_le_mul_left' (card_le_of_subset $ inter_subset_right _ _) _ }, { rw ←fintype.card_finset, exact mul_le_mul_right' (card_le_univ _) _ } end
6828fc1742b536f76c3fcfbecb02ea512637c380
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/invertible.lean
9d78a1351b3872d1920db2bf391200a1e40b4475
[ "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
8,810
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Anne Baanen A typeclass for the two-sided multiplicative inverse. -/ import algebra.char_zero import algebra.char_p /-! # Invertible elements This file defines a typeclass `invertible a` for elements `a` with a multiplicative inverse. The intent of the typeclass is to provide a way to write e.g. `⅟2` in a ring like `ℤ[1/2]` where some inverses exist but there is no general `⁻¹` operator; or to specify that a field has characteristic `≠ 2`. It is the `Type`-valued analogue to the `Prop`-valued `is_unit`. This file also includes some instances of `invertible` for specific numbers in characteristic zero. Some more cases are given as a `def`, to be included only when needed. To construct instances for concrete numbers, `invertible_of_nonzero` is a useful definition. ## Notation * `⅟a` is `invertible.inv_of a`, the inverse of `a` ## Implementation notes The `invertible` class lives in `Type`, not `Prop`, to make computation easier. If multiplication is associative, `invertible` is a subsingleton anyway. The `simp` normal form tries to normalize `⅟a` to `a ⁻¹`. Otherwise, it pushes `⅟` inside the expression as much as possible. ## Tags invertible, inverse element, inv_of, a half, one half, a third, one third, ½, ⅓ -/ universes u variables {α : Type u} /-- `invertible a` gives a two-sided multiplicative inverse of `a`. -/ class invertible [has_mul α] [has_one α] (a : α) : Type u := (inv_of : α) (inv_of_mul_self : inv_of * a = 1) (mul_inv_of_self : a * inv_of = 1) -- This notation has the same precedence as `has_inv.inv`. notation `⅟`:1034 := invertible.inv_of @[simp] lemma inv_of_mul_self [has_mul α] [has_one α] (a : α) [invertible a] : ⅟a * a = 1 := invertible.inv_of_mul_self @[simp] lemma mul_inv_of_self [has_mul α] [has_one α] (a : α) [invertible a] : a * ⅟a = 1 := invertible.mul_inv_of_self @[simp] lemma mul_inv_of_mul_self_cancel [monoid α] (a b : α) [invertible b] : a * ⅟b * b = a := by simp [mul_assoc] @[simp] lemma mul_mul_inv_of_self_cancel [monoid α] (a b : α) [invertible b] : a * b * ⅟b = a := by simp [mul_assoc] lemma inv_of_eq_right_inv [monoid α] {a b : α} [invertible a] (hac : a * b = 1) : ⅟a = b := left_inv_eq_right_inv (inv_of_mul_self _) hac lemma invertible_unique {α : Type u} [monoid α] (a b : α) (h : a = b) [invertible a] [invertible b] : ⅟a = ⅟b := by { apply inv_of_eq_right_inv, rw [h, mul_inv_of_self], } instance [monoid α] (a : α) : subsingleton (invertible a) := ⟨ λ ⟨b, hba, hab⟩ ⟨c, hca, hac⟩, by { congr, exact left_inv_eq_right_inv hba hac } ⟩ /-- An `invertible` element is a unit. -/ def unit_of_invertible [monoid α] (a : α) [invertible a] : units α := { val := a, inv := ⅟a, val_inv := by simp, inv_val := by simp, } @[simp] lemma unit_of_invertible_val [monoid α] (a : α) [invertible a] : (unit_of_invertible a : α) = a := rfl @[simp] lemma unit_of_invertible_inv [monoid α] (a : α) [invertible a] : (↑(unit_of_invertible a)⁻¹ : α) = ⅟a := rfl lemma is_unit_of_invertible [monoid α] (a : α) [invertible a] : is_unit a := ⟨unit_of_invertible a, rfl⟩ /-- Each element of a group is invertible. -/ def invertible_of_group [group α] (a : α) : invertible a := ⟨a⁻¹, inv_mul_self a, mul_inv_self a⟩ @[simp] lemma inv_of_eq_group_inv [group α] (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_self a) /-- `1` is the inverse of itself -/ def invertible_one [monoid α] : invertible (1 : α) := ⟨ 1, mul_one _, one_mul _ ⟩ @[simp] lemma inv_of_one [monoid α] [invertible (1 : α)] : ⅟(1 : α) = 1 := inv_of_eq_right_inv (mul_one _) /-- `-⅟a` is the inverse of `-a` -/ def invertible_neg [ring α] (a : α) [invertible a] : invertible (-a) := ⟨ -⅟a, by simp, by simp ⟩ @[simp] lemma inv_of_neg [ring α] (a : α) [invertible a] [invertible (-a)] : ⅟(-a) = -⅟a := inv_of_eq_right_inv (by simp) /-- `a` is the inverse of `⅟a`. -/ instance invertible_inv_of [has_one α] [has_mul α] {a : α} [invertible a] : invertible (⅟a) := ⟨ a, mul_inv_of_self a, inv_of_mul_self a ⟩ @[simp] lemma inv_of_inv_of [monoid α] {a : α} [invertible a] [invertible (⅟a)] : ⅟(⅟a) = a := inv_of_eq_right_inv (inv_of_mul_self _) /-- `⅟b * ⅟a` is the inverse of `a * b` -/ def invertible_mul [monoid α] (a b : α) [invertible a] [invertible b] : invertible (a * b) := ⟨ ⅟b * ⅟a, by simp [←mul_assoc], by simp [←mul_assoc] ⟩ @[simp] lemma inv_of_mul [monoid α] (a b : α) [invertible a] [invertible b] [invertible (a * b)] : ⅟(a * b) = ⅟b * ⅟a := inv_of_eq_right_inv (by simp [←mul_assoc]) lemma commute_inv_of {M : Type*} [has_one M] [has_mul M] (m : M) [invertible m] : commute m (⅟m) := calc m * ⅟m = 1 : mul_inv_of_self m ... = ⅟ m * m : (inv_of_mul_self m).symm instance invertible_pow {M : Type*} [monoid M] (m : M) [invertible m] (n : ℕ) : invertible (m ^ n) := { inv_of := ⅟ m ^ n, inv_of_mul_self := by rw [← (commute_inv_of m).symm.mul_pow, inv_of_mul_self, one_pow], mul_inv_of_self := by rw [← (commute_inv_of m).mul_pow, mul_inv_of_self, one_pow] } section group_with_zero variable [group_with_zero α] lemma nonzero_of_invertible (a : α) [invertible a] : a ≠ 0 := λ ha, zero_ne_one $ calc 0 = ⅟a * a : by simp [ha] ... = 1 : inv_of_mul_self a /-- `a⁻¹` is an inverse of `a` if `a ≠ 0` -/ def invertible_of_nonzero {a : α} (h : a ≠ 0) : invertible a := ⟨ a⁻¹, inv_mul_cancel h, mul_inv_cancel h ⟩ @[simp] lemma inv_of_eq_inv (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_cancel (nonzero_of_invertible a)) @[simp] lemma inv_mul_cancel_of_invertible (a : α) [invertible a] : a⁻¹ * a = 1 := inv_mul_cancel (nonzero_of_invertible a) @[simp] lemma mul_inv_cancel_of_invertible (a : α) [invertible a] : a * a⁻¹ = 1 := mul_inv_cancel (nonzero_of_invertible a) @[simp] lemma div_mul_cancel_of_invertible (a b : α) [invertible b] : a / b * b = a := div_mul_cancel a (nonzero_of_invertible b) @[simp] lemma mul_div_cancel_of_invertible (a b : α) [invertible b] : a * b / b = a := mul_div_cancel a (nonzero_of_invertible b) @[simp] lemma div_self_of_invertible (a : α) [invertible a] : a / a = 1 := div_self (nonzero_of_invertible a) /-- `b / a` is the inverse of `a / b` -/ def invertible_div (a b : α) [invertible a] [invertible b] : invertible (a / b) := ⟨b / a, by simp [←mul_div_assoc], by simp [←mul_div_assoc]⟩ @[simp] lemma inv_of_div (a b : α) [invertible a] [invertible b] [invertible (a / b)] : ⅟(a / b) = b / a := inv_of_eq_right_inv (by simp [←mul_div_assoc]) /-- `a` is the inverse of `a⁻¹` -/ def invertible_inv {a : α} [invertible a] : invertible (a⁻¹) := ⟨ a, by simp, by simp ⟩ end group_with_zero /-- Monoid homs preserve invertibility. -/ def invertible.map {R : Type*} {S : Type*} [monoid R] [monoid S] (f : R →* S) (r : R) [invertible r] : invertible (f r) := { inv_of := f (⅟r), inv_of_mul_self := by rw [← f.map_mul, inv_of_mul_self, f.map_one], mul_inv_of_self := by rw [← f.map_mul, mul_inv_of_self, f.map_one] } section ring_char /-- A natural number `t` is invertible in a field `K` if the charactistic of `K` does not divide `t`. -/ def invertible_of_ring_char_not_dvd {K : Type*} [field K] {t : ℕ} (not_dvd : ¬(ring_char K ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((ring_char.spec K t).mp h)) end ring_char section char_p /-- A natural number `t` is invertible in a field `K` of charactistic `p` if `p` does not divide `t`. -/ def invertible_of_char_p_not_dvd {K : Type*} [field K] {p : ℕ} [char_p K p] {t : ℕ} (not_dvd : ¬(p ∣ t)) : invertible (t : K) := invertible_of_nonzero (λ h, not_dvd ((char_p.cast_eq_zero_iff K p t).mp h)) instance invertible_of_pos {K : Type*} [field K] [char_zero K] (n : ℕ) [h : fact (0 < n)] : invertible (n : K) := invertible_of_nonzero $ by simpa [nat.pos_iff_ne_zero] using h end char_p section division_ring variable [division_ring α] instance invertible_succ [char_zero α] (n : ℕ) : invertible (n.succ : α) := invertible_of_nonzero (nat.cast_ne_zero.mpr (nat.succ_ne_zero _)) /-! A few `invertible n` instances for small numerals `n`. Feel free to add your own number when you need its inverse. -/ instance invertible_two [char_zero α] : invertible (2 : α) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 2 ≠ 0)) instance invertible_three [char_zero α] : invertible (3 : α) := invertible_of_nonzero (by exact_mod_cast (dec_trivial : 3 ≠ 0)) end division_ring
9980aaa1d663bd643d06282774df0efd3b4e7821
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/closure1.lean
a2cf62402f84ed5345f490532ac014c88a0a6578
[ "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,645
lean
import Lean open Lean open Lean.Meta universe u inductive Vec (α : Type u) : Nat → Type u | nil : Vec α 0 | cons {n} : α → Vec α n → Vec α (n+1) set_option trace.Meta.debug true def printDef (declName : Name) : MetaM Unit := do let cinfo ← getConstInfo declName; trace[Meta.debug] cinfo.value! instance : Coe Name FVarId where coe n := { name := n } instance : Coe Name MVarId where coe n := { name := n } instance : Coe Name LMVarId where coe n := { name := n } def tst1 : MetaM Unit := do let u := mkLevelParam `u let v := mkLevelMVar `v let m1 ← mkFreshExprMVar (mkSort levelOne) withLocalDeclD `α (mkSort u) $ fun α => do withLocalDeclD `β (mkSort v) $ fun β => do let m2 ← mkFreshExprMVar (← mkArrow α m1) withLocalDeclD `a α $ fun a => do withLocalDeclD `f (← mkArrow α α) $ fun f => do withLetDecl `b α (mkApp f a) $ fun b => do let t := mkApp m2 (mkApp f b) let e ← mkAuxDefinitionFor `foo1 t trace[Meta.debug] e printDef `foo1 #eval tst1 def tst2 : MetaM Unit := do let u := mkLevelParam `u withLocalDeclD `α (mkSort (mkLevelSucc u)) $ fun α => do withLocalDeclD `v1 (mkApp2 (mkConst `Vec [u]) α (mkNatLit 10)) $ fun v1 => withLetDecl `n (mkConst `Nat) (mkNatLit 10) $ fun n => withLocalDeclD `v2 (mkApp2 (mkConst `Vec [u]) α n) $ fun v2 => do let m ← mkFreshExprMVar (← mkArrow (mkApp2 (mkConst `Vec [u]) α (mkNatLit 10)) (mkSort levelZero)) withLocalDeclD `p (mkSort levelZero) $ fun p => do let t ← mkEq v1 v2 let t := mkApp2 (mkConst `And) t (mkApp2 (mkConst `Or) (mkApp m v2) p) let e ← mkAuxDefinitionFor `foo2 t trace[Meta.debug] e printDef `foo2 #eval tst2
860bb2c2d911132a421702bc6c7b86485b77d4cc
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/uniform_space/basic.lean
22e1275bd01b62eb473348f226dd837210bcac81
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
82,465
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import order.filter.small_sets import topology.subset_properties import topology.nhds_set /-! # Uniform spaces Uniform spaces are a generalization of metric spaces and topological groups. Many concepts directly generalize to uniform spaces, e.g. * uniform continuity (in this file) * completeness (in `cauchy.lean`) * extension of uniform continuous functions to complete spaces (in `uniform_embedding.lean`) * totally bounded sets (in `cauchy.lean`) * totally bounded complete sets are compact (in `cauchy.lean`) A uniform structure on a type `X` is a filter `𝓤 X` on `X × X` satisfying some conditions which makes it reasonable to say that `∀ᶠ (p : X × X) in 𝓤 X, ...` means "for all p.1 and p.2 in X close enough, ...". Elements of this filter are called entourages of `X`. The two main examples are: * If `X` is a metric space, `V ∈ 𝓤 X ↔ ∃ ε > 0, { p | dist p.1 p.2 < ε } ⊆ V` * If `G` is an additive topological group, `V ∈ 𝓤 G ↔ ∃ U ∈ 𝓝 (0 : G), {p | p.2 - p.1 ∈ U} ⊆ V` Those examples are generalizations in two different directions of the elementary example where `X = ℝ` and `V ∈ 𝓤 ℝ ↔ ∃ ε > 0, { p | |p.2 - p.1| < ε } ⊆ V` which features both the topological group structure on `ℝ` and its metric space structure. Each uniform structure on `X` induces a topology on `X` characterized by > `nhds_eq_comap_uniformity : ∀ {x : X}, 𝓝 x = comap (prod.mk x) (𝓤 X)` where `prod.mk x : X → X × X := (λ y, (x, y))` is the partial evaluation of the product constructor. The dictionary with metric spaces includes: * an upper bound for `dist x y` translates into `(x, y) ∈ V` for some `V ∈ 𝓤 X` * a ball `ball x r` roughly corresponds to `uniform_space.ball x V := {y | (x, y) ∈ V}` for some `V ∈ 𝓤 X`, but the later is more general (it includes in particular both open and closed balls for suitable `V`). In particular we have: `is_open_iff_ball_subset {s : set X} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 X, ball x V ⊆ s` The triangle inequality is abstracted to a statement involving the composition of relations in `X`. First note that the triangle inequality in a metric space is equivalent to `∀ (x y z : X) (r r' : ℝ), dist x y ≤ r → dist y z ≤ r' → dist x z ≤ r + r'`. Then, for any `V` and `W` with type `set (X × X)`, the composition `V ○ W : set (X × X)` is defined as `{ p : X × X | ∃ z, (p.1, z) ∈ V ∧ (z, p.2) ∈ W }`. In the metric space case, if `V = { p | dist p.1 p.2 ≤ r }` and `W = { p | dist p.1 p.2 ≤ r' }` then the triangle inequality, as reformulated above, says `V ○ W` is contained in `{p | dist p.1 p.2 ≤ r + r'}` which is the entourage associated to the radius `r + r'`. In general we have `mem_ball_comp (h : y ∈ ball x V) (h' : z ∈ ball y W) : z ∈ ball x (V ○ W)`. Note that this discussion does not depend on any axiom imposed on the uniformity filter, it is simply captured by the definition of composition. The uniform space axioms ask the filter `𝓤 X` to satisfy the following: * every `V ∈ 𝓤 X` contains the diagonal `id_rel = { p | p.1 = p.2 }`. This abstracts the fact that `dist x x ≤ r` for every non-negative radius `r` in the metric space case and also that `x - x` belongs to every neighborhood of zero in the topological group case. * `V ∈ 𝓤 X → prod.swap '' V ∈ 𝓤 X`. This is tightly related the fact that `dist x y = dist y x` in a metric space, and to continuity of negation in the topological group case. * `∀ V ∈ 𝓤 X, ∃ W ∈ 𝓤 X, W ○ W ⊆ V`. In the metric space case, it corresponds to cutting the radius of a ball in half and applying the triangle inequality. In the topological group case, it comes from continuity of addition at `(0, 0)`. These three axioms are stated more abstractly in the definition below, in terms of operations on filters, without directly manipulating entourages. ## Main definitions * `uniform_space X` is a uniform space structure on a type `X` * `uniform_continuous f` is a predicate saying a function `f : α → β` between uniform spaces is uniformly continuous : `∀ r ∈ 𝓤 β, ∀ᶠ (x : α × α) in 𝓤 α, (f x.1, f x.2) ∈ r` In this file we also define a complete lattice structure on the type `uniform_space X` of uniform structures on `X`, as well as the pullback (`uniform_space.comap`) of uniform structures coming from the pullback of filters. Like distance functions, uniform structures cannot be pushed forward in general. ## Notations Localized in `uniformity`, we have the notation `𝓤 X` for the uniformity on a uniform space `X`, and `○` for composition of relations, seen as terms with type `set (X × X)`. ## Implementation notes There is already a theory of relations in `data/rel.lean` where the main definition is `def rel (α β : Type*) := α → β → Prop`. The relations used in the current file involve only one type, but this is not the reason why we don't reuse `data/rel.lean`. We use `set (α × α)` instead of `rel α α` because we really need sets to use the filter library, and elements of filters on `α × α` have type `set (α × α)`. The structure `uniform_space X` bundles a uniform structure on `X`, a topology on `X` and an assumption saying those are compatible. This may not seem mathematically reasonable at first, but is in fact an instance of the forgetful inheritance pattern. See Note [forgetful inheritance] below. ## References The formalization uses the books: * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] But it makes a more systematic use of the filter library. -/ open set filter classical open_locale classical topological_space filter set_option eqn_compiler.zeta true universes u /-! ### Relations, seen as `set (α × α)` -/ variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} {ι : Sort*} /-- The identity relation, or the graph of the identity function -/ def id_rel {α : Type*} := {p : α × α | p.1 = p.2} @[simp] theorem mem_id_rel {a b : α} : (a, b) ∈ @id_rel α ↔ a = b := iff.rfl @[simp] theorem id_rel_subset {s : set (α × α)} : id_rel ⊆ s ↔ ∀ a, (a, a) ∈ s := by simp [subset_def]; exact forall_congr (λ a, by simp) /-- The composition of relations -/ def comp_rel {α : Type u} (r₁ r₂ : set (α×α)) := {p : α × α | ∃z:α, (p.1, z) ∈ r₁ ∧ (z, p.2) ∈ r₂} localized "infix (name := uniformity.comp_rel) ` ○ `:55 := comp_rel" in uniformity @[simp] theorem mem_comp_rel {r₁ r₂ : set (α×α)} {x y : α} : (x, y) ∈ r₁ ○ r₂ ↔ ∃ z, (x, z) ∈ r₁ ∧ (z, y) ∈ r₂ := iff.rfl @[simp] theorem swap_id_rel : prod.swap '' id_rel = @id_rel α := set.ext $ assume ⟨a, b⟩, by simp [image_swap_eq_preimage_swap]; exact eq_comm theorem monotone_comp_rel [preorder β] {f g : β → set (α×α)} (hf : monotone f) (hg : monotone g) : monotone (λx, (f x) ○ (g x)) := assume a b h p ⟨z, h₁, h₂⟩, ⟨z, hf h h₁, hg h h₂⟩ @[mono] lemma comp_rel_mono {f g h k: set (α×α)} (h₁ : f ⊆ h) (h₂ : g ⊆ k) : f ○ g ⊆ h ○ k := λ ⟨x, y⟩ ⟨z, h, h'⟩, ⟨z, h₁ h, h₂ h'⟩ lemma prod_mk_mem_comp_rel {a b c : α} {s t : set (α×α)} (h₁ : (a, c) ∈ s) (h₂ : (c, b) ∈ t) : (a, b) ∈ s ○ t := ⟨c, h₁, h₂⟩ @[simp] lemma id_comp_rel {r : set (α×α)} : id_rel ○ r = r := set.ext $ assume ⟨a, b⟩, by simp lemma comp_rel_assoc {r s t : set (α×α)} : (r ○ s) ○ t = r ○ (s ○ t) := by ext p; cases p; simp only [mem_comp_rel]; tauto lemma left_subset_comp_rel {s t : set (α × α)} (h : id_rel ⊆ t) : s ⊆ s ○ t := λ ⟨x, y⟩ xy_in, ⟨y, xy_in, h $ by exact rfl⟩ lemma right_subset_comp_rel {s t : set (α × α)} (h : id_rel ⊆ s) : t ⊆ s ○ t := λ ⟨x, y⟩ xy_in, ⟨x, h $ by exact rfl, xy_in⟩ lemma subset_comp_self {s : set (α × α)} (h : id_rel ⊆ s) : s ⊆ s ○ s := left_subset_comp_rel h lemma subset_iterate_comp_rel {s t : set (α × α)} (h : id_rel ⊆ s) (n : ℕ) : t ⊆ (((○) s) ^[n] t) := begin induction n with n ihn generalizing t, exacts [subset.rfl, (right_subset_comp_rel h).trans ihn] end /-- The relation is invariant under swapping factors. -/ def symmetric_rel (V : set (α × α)) : Prop := prod.swap ⁻¹' V = V /-- The maximal symmetric relation contained in a given relation. -/ def symmetrize_rel (V : set (α × α)) : set (α × α) := V ∩ prod.swap ⁻¹' V lemma symmetric_symmetrize_rel (V : set (α × α)) : symmetric_rel (symmetrize_rel V) := by simp [symmetric_rel, symmetrize_rel, preimage_inter, inter_comm, ← preimage_comp] lemma symmetrize_rel_subset_self (V : set (α × α)) : symmetrize_rel V ⊆ V := sep_subset _ _ @[mono] lemma symmetrize_mono {V W: set (α × α)} (h : V ⊆ W) : symmetrize_rel V ⊆ symmetrize_rel W := inter_subset_inter h $ preimage_mono h lemma symmetric_rel.mk_mem_comm {V : set (α × α)} (hV : symmetric_rel V) {x y : α} : (x, y) ∈ V ↔ (y, x) ∈ V := set.ext_iff.1 hV (y, x) lemma symmetric_rel.eq {U : set (α × α)} (hU : symmetric_rel U) : prod.swap ⁻¹' U = U := hU lemma symmetric_rel.inter {U V : set (α × α)} (hU : symmetric_rel U) (hV : symmetric_rel V) : symmetric_rel (U ∩ V) := by rw [symmetric_rel, preimage_inter, hU.eq, hV.eq] /-- This core description of a uniform space is outside of the type class hierarchy. It is useful for constructions of uniform spaces, when the topology is derived from the uniform space. -/ structure uniform_space.core (α : Type u) := (uniformity : filter (α × α)) (refl : 𝓟 id_rel ≤ uniformity) (symm : tendsto prod.swap uniformity uniformity) (comp : uniformity.lift' (λs, s ○ s) ≤ uniformity) /-- An alternative constructor for `uniform_space.core`. This version unfolds various `filter`-related definitions. -/ def uniform_space.core.mk' {α : Type u} (U : filter (α × α)) (refl : ∀ (r ∈ U) x, (x, x) ∈ r) (symm : ∀ r ∈ U, prod.swap ⁻¹' r ∈ U) (comp : ∀ r ∈ U, ∃ t ∈ U, t ○ t ⊆ r) : uniform_space.core α := ⟨U, λ r ru, id_rel_subset.2 (refl _ ru), symm, begin intros r ru, rw [mem_lift'_sets], exact comp _ ru, apply monotone_comp_rel; exact monotone_id, end⟩ /-- Defining an `uniform_space.core` from a filter basis satisfying some uniformity-like axioms. -/ def uniform_space.core.mk_of_basis {α : Type u} (B : filter_basis (α × α)) (refl : ∀ (r ∈ B) x, (x, x) ∈ r) (symm : ∀ r ∈ B, ∃ t ∈ B, t ⊆ prod.swap ⁻¹' r) (comp : ∀ r ∈ B, ∃ t ∈ B, t ○ t ⊆ r) : uniform_space.core α := { uniformity := B.filter, refl := B.has_basis.ge_iff.mpr (λ r ru, id_rel_subset.2 $ refl _ ru), symm := (B.has_basis.tendsto_iff B.has_basis).mpr symm, comp := (has_basis.le_basis_iff (B.has_basis.lift' (monotone_comp_rel monotone_id monotone_id)) B.has_basis).mpr comp } /-- A uniform space generates a topological space -/ def uniform_space.core.to_topological_space {α : Type u} (u : uniform_space.core α) : topological_space α := { is_open := λs, ∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ u.uniformity, is_open_univ := by simp; intro; exact univ_mem, is_open_inter := assume s t hs ht x ⟨xs, xt⟩, by filter_upwards [hs x xs, ht x xt]; simp {contextual := tt}, is_open_sUnion := assume s hs x ⟨t, ts, xt⟩, by filter_upwards [hs t ts x xt] with p ph h using ⟨t, ts, ph h⟩ } lemma uniform_space.core_eq : ∀{u₁ u₂ : uniform_space.core α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | ⟨u₁, _, _, _⟩ ⟨u₂, _, _, _⟩ h := by { congr, exact h } -- the topological structure is embedded in the uniform structure -- to avoid instance diamond issues. See Note [forgetful inheritance]. /-- A uniform space is a generalization of the "uniform" topological aspects of a metric space. It consists of a filter on `α × α` called the "uniformity", which satisfies properties analogous to the reflexivity, symmetry, and triangle properties of a metric. A metric space has a natural uniformity, and a uniform space has a natural topology. A topological group also has a natural uniformity, even when it is not metrizable. -/ class uniform_space (α : Type u) extends topological_space α, uniform_space.core α := (is_open_uniformity : ∀s, is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ uniformity)) /-- Alternative constructor for `uniform_space α` when a topology is already given. -/ @[pattern] def uniform_space.mk' {α} (t : topological_space α) (c : uniform_space.core α) (is_open_uniformity : ∀s:set α, t.is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ c.uniformity)) : uniform_space α := ⟨c, is_open_uniformity⟩ /-- Construct a `uniform_space` from a `uniform_space.core`. -/ def uniform_space.of_core {α : Type u} (u : uniform_space.core α) : uniform_space α := { to_core := u, to_topological_space := u.to_topological_space, is_open_uniformity := assume a, iff.rfl } /-- Construct a `uniform_space` from a `u : uniform_space.core` and a `topological_space` structure that is equal to `u.to_topological_space`. -/ def uniform_space.of_core_eq {α : Type u} (u : uniform_space.core α) (t : topological_space α) (h : t = u.to_topological_space) : uniform_space α := { to_core := u, to_topological_space := t, is_open_uniformity := assume a, h.symm ▸ iff.rfl } lemma uniform_space.to_core_to_topological_space (u : uniform_space α) : u.to_core.to_topological_space = u.to_topological_space := topological_space_eq $ funext $ assume s, by rw [uniform_space.core.to_topological_space, uniform_space.is_open_uniformity] @[ext] lemma uniform_space_eq : ∀{u₁ u₂ : uniform_space α}, u₁.uniformity = u₂.uniformity → u₁ = u₂ | (uniform_space.mk' t₁ u₁ o₁) (uniform_space.mk' t₂ u₂ o₂) h := have u₁ = u₂, from uniform_space.core_eq h, have t₁ = t₂, from topological_space_eq $ funext $ assume s, by rw [o₁, o₂]; simp [this], by simp [*] lemma uniform_space.of_core_eq_to_core (u : uniform_space α) (t : topological_space α) (h : t = u.to_core.to_topological_space) : uniform_space.of_core_eq u.to_core t h = u := uniform_space_eq rfl /-- Replace topology in a `uniform_space` instance with a propositionally (but possibly not definitionally) equal one. -/ @[reducible] def uniform_space.replace_topology {α : Type*} [i : topological_space α] (u : uniform_space α) (h : i = u.to_topological_space) : uniform_space α := uniform_space.of_core_eq u.to_core i $ h.trans u.to_core_to_topological_space.symm lemma uniform_space.replace_topology_eq {α : Type*} [i : topological_space α] (u : uniform_space α) (h : i = u.to_topological_space) : u.replace_topology h = u := u.of_core_eq_to_core _ _ section uniform_space variables [uniform_space α] /-- The uniformity is a filter on α × α (inferred from an ambient uniform space structure on α). -/ def uniformity (α : Type u) [uniform_space α] : filter (α × α) := (@uniform_space.to_core α _).uniformity localized "notation (name := uniformity) `𝓤` := uniformity" in uniformity lemma is_open_uniformity {s : set α} : is_open s ↔ (∀x∈s, { p : α × α | p.1 = x → p.2 ∈ s } ∈ 𝓤 α) := uniform_space.is_open_uniformity s lemma refl_le_uniformity : 𝓟 id_rel ≤ 𝓤 α := (@uniform_space.to_core α _).refl instance uniformity.ne_bot [nonempty α] : ne_bot (𝓤 α) := begin inhabit α, refine (principal_ne_bot_iff.2 _).mono refl_le_uniformity, exact ⟨(default, default), rfl⟩ end lemma refl_mem_uniformity {x : α} {s : set (α × α)} (h : s ∈ 𝓤 α) : (x, x) ∈ s := refl_le_uniformity h rfl lemma mem_uniformity_of_eq {x y : α} {s : set (α × α)} (h : s ∈ 𝓤 α) (hx : x = y) : (x, y) ∈ s := hx ▸ refl_mem_uniformity h lemma symm_le_uniformity : map (@prod.swap α α) (𝓤 _) ≤ (𝓤 _) := (@uniform_space.to_core α _).symm lemma comp_le_uniformity : (𝓤 α).lift' (λs:set (α×α), s ○ s) ≤ 𝓤 α := (@uniform_space.to_core α _).comp lemma tendsto_swap_uniformity : tendsto (@prod.swap α α) (𝓤 α) (𝓤 α) := symm_le_uniformity lemma comp_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, t ○ t ⊆ s := have s ∈ (𝓤 α).lift' (λt:set (α×α), t ○ t), from comp_le_uniformity hs, (mem_lift'_sets $ monotone_comp_rel monotone_id monotone_id).mp this /-- If `s ∈ 𝓤 α`, then for any natural `n`, for a subset `t` of a sufficiently small set in `𝓤 α`, we have `t ○ t ○ ... ○ t ⊆ s` (`n` compositions). -/ lemma eventually_uniformity_iterate_comp_subset {s : set (α × α)} (hs : s ∈ 𝓤 α) (n : ℕ) : ∀ᶠ t in (𝓤 α).small_sets, ((○) t) ^[n] t ⊆ s := begin suffices : ∀ᶠ t in (𝓤 α).small_sets, t ⊆ s ∧ (((○) t) ^[n] t ⊆ s), from (eventually_and.1 this).2, induction n with n ihn generalizing s, { simpa }, rcases comp_mem_uniformity_sets hs with ⟨t, htU, hts⟩, refine (ihn htU).mono (λ U hU, _), rw [function.iterate_succ_apply'], exact ⟨hU.1.trans $ (subset_comp_self $ refl_le_uniformity htU).trans hts, (comp_rel_mono hU.1 hU.2).trans hts⟩ end /-- If `s ∈ 𝓤 α`, then for any natural `n`, for a subset `t` of a sufficiently small set in `𝓤 α`, we have `t ○ t ⊆ s`. -/ lemma eventually_uniformity_comp_subset {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∀ᶠ t in (𝓤 α).small_sets, t ○ t ⊆ s := eventually_uniformity_iterate_comp_subset hs 1 /-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is transitive. -/ lemma filter.tendsto.uniformity_trans {l : filter β} {f₁ f₂ f₃ : β → α} (h₁₂ : tendsto (λ x, (f₁ x, f₂ x)) l (𝓤 α)) (h₂₃ : tendsto (λ x, (f₂ x, f₃ x)) l (𝓤 α)) : tendsto (λ x, (f₁ x, f₃ x)) l (𝓤 α) := begin refine le_trans (le_lift' $ λ s hs, mem_map.2 _) comp_le_uniformity, filter_upwards [h₁₂ hs, h₂₃ hs] with x hx₁₂ hx₂₃ using ⟨_, hx₁₂, hx₂₃⟩, end /-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is symmetric -/ lemma filter.tendsto.uniformity_symm {l : filter β} {f : β → α × α} (h : tendsto f l (𝓤 α)) : tendsto (λ x, ((f x).2, (f x).1)) l (𝓤 α) := tendsto_swap_uniformity.comp h /-- Relation `λ f g, tendsto (λ x, (f x, g x)) l (𝓤 α)` is reflexive. -/ lemma tendsto_diag_uniformity (f : β → α) (l : filter β) : tendsto (λ x, (f x, f x)) l (𝓤 α) := assume s hs, mem_map.2 $ univ_mem' $ λ x, refl_mem_uniformity hs lemma tendsto_const_uniformity {a : α} {f : filter β} : tendsto (λ _, (a, a)) f (𝓤 α) := tendsto_diag_uniformity (λ _, a) f lemma symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, (∀a b, (a, b) ∈ t → (b, a) ∈ t) ∧ t ⊆ s := have preimage prod.swap s ∈ 𝓤 α, from symm_le_uniformity hs, ⟨s ∩ preimage prod.swap s, inter_mem hs this, λ a b ⟨h₁, h₂⟩, ⟨h₂, h₁⟩, inter_subset_left _ _⟩ lemma comp_symm_of_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, (∀{a b}, (a, b) ∈ t → (b, a) ∈ t) ∧ t ○ t ⊆ s := let ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs in let ⟨t', ht', ht'₁, ht'₂⟩ := symm_of_uniformity ht₁ in ⟨t', ht', ht'₁, subset.trans (monotone_comp_rel monotone_id monotone_id ht'₂) ht₂⟩ lemma uniformity_le_symm : 𝓤 α ≤ (@prod.swap α α) <$> 𝓤 α := by rw [map_swap_eq_comap_swap]; from map_le_iff_le_comap.1 tendsto_swap_uniformity lemma uniformity_eq_symm : 𝓤 α = (@prod.swap α α) <$> 𝓤 α := le_antisymm uniformity_le_symm symm_le_uniformity @[simp] lemma comap_swap_uniformity : comap (@prod.swap α α) (𝓤 α) = 𝓤 α := (congr_arg _ uniformity_eq_symm).trans $ comap_map prod.swap_injective lemma symmetrize_mem_uniformity {V : set (α × α)} (h : V ∈ 𝓤 α) : symmetrize_rel V ∈ 𝓤 α := begin apply (𝓤 α).inter_sets h, rw [← image_swap_eq_preimage_swap, uniformity_eq_symm], exact image_mem_map h, end /-- Symmetric entourages form a basis of `𝓤 α` -/ lemma uniform_space.has_basis_symmetric : (𝓤 α).has_basis (λ s : set (α × α), s ∈ 𝓤 α ∧ symmetric_rel s) id := has_basis_self.2 $ λ t t_in, ⟨symmetrize_rel t, symmetrize_mem_uniformity t_in, symmetric_symmetrize_rel t, symmetrize_rel_subset_self t⟩ theorem uniformity_lift_le_swap {g : set (α×α) → filter β} {f : filter β} (hg : monotone g) (h : (𝓤 α).lift (λs, g (preimage prod.swap s)) ≤ f) : (𝓤 α).lift g ≤ f := calc (𝓤 α).lift g ≤ (filter.map (@prod.swap α α) $ 𝓤 α).lift g : lift_mono uniformity_le_symm le_rfl ... ≤ _ : by rw [map_lift_eq2 hg, image_swap_eq_preimage_swap]; exact h lemma uniformity_lift_le_comp {f : set (α×α) → filter β} (h : monotone f) : (𝓤 α).lift (λs, f (s ○ s)) ≤ (𝓤 α).lift f := calc (𝓤 α).lift (λs, f (s ○ s)) = ((𝓤 α).lift' (λs:set (α×α), s ○ s)).lift f : begin rw [lift_lift'_assoc], exact monotone_comp_rel monotone_id monotone_id, exact h end ... ≤ (𝓤 α).lift f : lift_mono comp_le_uniformity le_rfl lemma comp_le_uniformity3 : (𝓤 α).lift' (λs:set (α×α), s ○ (s ○ s)) ≤ (𝓤 α) := calc (𝓤 α).lift' (λd, d ○ (d ○ d)) = (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), s ○ (t ○ t))) : begin rw [lift_lift'_same_eq_lift'], exact (assume x, monotone_comp_rel monotone_const $ monotone_comp_rel monotone_id monotone_id), exact (assume x, monotone_comp_rel monotone_id monotone_const), end ... ≤ (𝓤 α).lift (λs, (𝓤 α).lift' (λt:set(α×α), s ○ t)) : lift_mono' $ assume s hs, @uniformity_lift_le_comp α _ _ (𝓟 ∘ (○) s) $ monotone_principal.comp (monotone_comp_rel monotone_const monotone_id) ... = (𝓤 α).lift' (λs:set(α×α), s ○ s) : lift_lift'_same_eq_lift' (assume s, monotone_comp_rel monotone_const monotone_id) (assume s, monotone_comp_rel monotone_id monotone_const) ... ≤ (𝓤 α) : comp_le_uniformity /-- See also `comp_open_symm_mem_uniformity_sets`. -/ lemma comp_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, symmetric_rel t ∧ t ○ t ⊆ s := begin obtain ⟨w, w_in, w_sub⟩ : ∃ w ∈ 𝓤 α, w ○ w ⊆ s := comp_mem_uniformity_sets hs, use [symmetrize_rel w, symmetrize_mem_uniformity w_in, symmetric_symmetrize_rel w], have : symmetrize_rel w ⊆ w := symmetrize_rel_subset_self w, calc symmetrize_rel w ○ symmetrize_rel w ⊆ w ○ w : by mono ... ⊆ s : w_sub, end lemma subset_comp_self_of_mem_uniformity {s : set (α × α)} (h : s ∈ 𝓤 α) : s ⊆ s ○ s := subset_comp_self (refl_le_uniformity h) lemma comp_comp_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, symmetric_rel t ∧ t ○ t ○ t ⊆ s := begin rcases comp_symm_mem_uniformity_sets hs with ⟨w, w_in, w_symm, w_sub⟩, rcases comp_symm_mem_uniformity_sets w_in with ⟨t, t_in, t_symm, t_sub⟩, use [t, t_in, t_symm], have : t ⊆ t ○ t := subset_comp_self_of_mem_uniformity t_in, calc t ○ t ○ t ⊆ w ○ t : by mono ... ⊆ w ○ (t ○ t) : by mono ... ⊆ w ○ w : by mono ... ⊆ s : w_sub, end /-! ### Balls in uniform spaces -/ /-- The ball around `(x : β)` with respect to `(V : set (β × β))`. Intended to be used for `V ∈ 𝓤 β`, but this is not needed for the definition. Recovers the notions of metric space ball when `V = {p | dist p.1 p.2 < r }`. -/ def uniform_space.ball (x : β) (V : set (β × β)) : set β := (prod.mk x) ⁻¹' V open uniform_space (ball) lemma uniform_space.mem_ball_self (x : α) {V : set (α × α)} (hV : V ∈ 𝓤 α) : x ∈ ball x V := refl_mem_uniformity hV /-- The triangle inequality for `uniform_space.ball` -/ lemma mem_ball_comp {V W : set (β × β)} {x y z} (h : y ∈ ball x V) (h' : z ∈ ball y W) : z ∈ ball x (V ○ W) := prod_mk_mem_comp_rel h h' lemma ball_subset_of_comp_subset {V W : set (β × β)} {x y} (h : x ∈ ball y W) (h' : W ○ W ⊆ V) : ball x W ⊆ ball y V := λ z z_in, h' (mem_ball_comp h z_in) lemma ball_mono {V W : set (β × β)} (h : V ⊆ W) (x : β) : ball x V ⊆ ball x W := preimage_mono h lemma ball_inter (x : β) (V W : set (β × β)) : ball x (V ∩ W) = ball x V ∩ ball x W := preimage_inter lemma ball_inter_left (x : β) (V W : set (β × β)) : ball x (V ∩ W) ⊆ ball x V := ball_mono (inter_subset_left V W) x lemma ball_inter_right (x : β) (V W : set (β × β)) : ball x (V ∩ W) ⊆ ball x W := ball_mono (inter_subset_right V W) x lemma mem_ball_symmetry {V : set (β × β)} (hV : symmetric_rel V) {x y} : x ∈ ball y V ↔ y ∈ ball x V := show (x, y) ∈ prod.swap ⁻¹' V ↔ (x, y) ∈ V, by { unfold symmetric_rel at hV, rw hV } lemma ball_eq_of_symmetry {V : set (β × β)} (hV : symmetric_rel V) {x} : ball x V = {y | (y, x) ∈ V} := by { ext y, rw mem_ball_symmetry hV, exact iff.rfl } lemma mem_comp_of_mem_ball {V W : set (β × β)} {x y z : β} (hV : symmetric_rel V) (hx : x ∈ ball z V) (hy : y ∈ ball z W) : (x, y) ∈ V ○ W := begin rw mem_ball_symmetry hV at hx, exact ⟨z, hx, hy⟩ end lemma uniform_space.is_open_ball (x : α) {V : set (α × α)} (hV : is_open V) : is_open (ball x V) := hV.preimage $ continuous_const.prod_mk continuous_id lemma mem_comp_comp {V W M : set (β × β)} (hW' : symmetric_rel W) {p : β × β} : p ∈ V ○ M ○ W ↔ ((ball p.1 V ×ˢ ball p.2 W) ∩ M).nonempty := begin cases p with x y, split, { rintros ⟨z, ⟨w, hpw, hwz⟩, hzy⟩, exact ⟨(w, z), ⟨hpw, by rwa mem_ball_symmetry hW'⟩, hwz⟩, }, { rintro ⟨⟨w, z⟩, ⟨w_in, z_in⟩, hwz⟩, rwa mem_ball_symmetry hW' at z_in, use [z, w] ; tauto }, end /-! ### Neighborhoods in uniform spaces -/ lemma mem_nhds_uniformity_iff_right {x : α} {s : set α} : s ∈ 𝓝 x ↔ {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α := begin refine ⟨_, λ hs, _⟩, { simp only [mem_nhds_iff, is_open_uniformity, and_imp, exists_imp_distrib], intros t ts ht xt, filter_upwards [ht x xt] using λ y h eq, ts (h eq) }, { refine mem_nhds_iff.mpr ⟨{x | {p : α × α | p.1 = x → p.2 ∈ s} ∈ 𝓤 α}, _, _, hs⟩, { exact λ y hy, refl_mem_uniformity hy rfl }, { refine is_open_uniformity.mpr (λ y hy, _), rcases comp_mem_uniformity_sets hy with ⟨t, ht, tr⟩, filter_upwards [ht], rintro ⟨a, b⟩ hp' rfl, filter_upwards [ht], rintro ⟨a', b'⟩ hp'' rfl, exact @tr (a, b') ⟨a', hp', hp''⟩ rfl } } end lemma mem_nhds_uniformity_iff_left {x : α} {s : set α} : s ∈ 𝓝 x ↔ {p : α × α | p.2 = x → p.1 ∈ s} ∈ 𝓤 α := by { rw [uniformity_eq_symm, mem_nhds_uniformity_iff_right], refl } lemma nhds_eq_comap_uniformity_aux {α : Type u} {x : α} {s : set α} {F : filter (α × α)} : {p : α × α | p.fst = x → p.snd ∈ s} ∈ F ↔ s ∈ comap (prod.mk x) F := by rw mem_comap ; from iff.intro (assume hs, ⟨_, hs, assume x hx, hx rfl⟩) (assume ⟨t, h, ht⟩, F.sets_of_superset h $ assume ⟨p₁, p₂⟩ hp (h : p₁ = x), ht $ by simp [h.symm, hp]) lemma nhds_eq_comap_uniformity {x : α} : 𝓝 x = (𝓤 α).comap (prod.mk x) := by { ext s, rw [mem_nhds_uniformity_iff_right], exact nhds_eq_comap_uniformity_aux } /-- See also `is_open_iff_open_ball_subset`. -/ lemma is_open_iff_ball_subset {s : set α} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 α, ball x V ⊆ s := begin simp_rw [is_open_iff_mem_nhds, nhds_eq_comap_uniformity], exact iff.rfl, end lemma nhds_basis_uniformity' {p : ι → Prop} {s : ι → set (α × α)} (h : (𝓤 α).has_basis p s) {x : α} : (𝓝 x).has_basis p (λ i, ball x (s i)) := by { rw [nhds_eq_comap_uniformity], exact h.comap (prod.mk x) } lemma nhds_basis_uniformity {p : ι → Prop} {s : ι → set (α × α)} (h : (𝓤 α).has_basis p s) {x : α} : (𝓝 x).has_basis p (λ i, {y | (y, x) ∈ s i}) := begin replace h := h.comap prod.swap, rw [← map_swap_eq_comap_swap, ← uniformity_eq_symm] at h, exact nhds_basis_uniformity' h end lemma uniform_space.mem_nhds_iff {x : α} {s : set α} : s ∈ 𝓝 x ↔ ∃ V ∈ 𝓤 α, ball x V ⊆ s := begin rw [nhds_eq_comap_uniformity, mem_comap], exact iff.rfl, end lemma uniform_space.ball_mem_nhds (x : α) ⦃V : set (α × α)⦄ (V_in : V ∈ 𝓤 α) : ball x V ∈ 𝓝 x := begin rw uniform_space.mem_nhds_iff, exact ⟨V, V_in, subset.refl _⟩ end lemma uniform_space.mem_nhds_iff_symm {x : α} {s : set α} : s ∈ 𝓝 x ↔ ∃ V ∈ 𝓤 α, symmetric_rel V ∧ ball x V ⊆ s := begin rw uniform_space.mem_nhds_iff, split, { rintros ⟨V, V_in, V_sub⟩, use [symmetrize_rel V, symmetrize_mem_uniformity V_in, symmetric_symmetrize_rel V], exact subset.trans (ball_mono (symmetrize_rel_subset_self V) x) V_sub }, { rintros ⟨V, V_in, V_symm, V_sub⟩, exact ⟨V, V_in, V_sub⟩ } end lemma uniform_space.has_basis_nhds (x : α) : has_basis (𝓝 x) (λ s : set (α × α), s ∈ 𝓤 α ∧ symmetric_rel s) (λ s, ball x s) := ⟨λ t, by simp [uniform_space.mem_nhds_iff_symm, and_assoc]⟩ open uniform_space lemma uniform_space.mem_closure_iff_symm_ball {s : set α} {x} : x ∈ closure s ↔ ∀ {V}, V ∈ 𝓤 α → symmetric_rel V → (s ∩ ball x V).nonempty := by simp [mem_closure_iff_nhds_basis (has_basis_nhds x), set.nonempty] lemma uniform_space.mem_closure_iff_ball {s : set α} {x} : x ∈ closure s ↔ ∀ {V}, V ∈ 𝓤 α → (ball x V ∩ s).nonempty := by simp [mem_closure_iff_nhds_basis' (nhds_basis_uniformity' (𝓤 α).basis_sets)] lemma uniform_space.has_basis_nhds_prod (x y : α) : has_basis (𝓝 (x, y)) (λ s, s ∈ 𝓤 α ∧ symmetric_rel s) $ λ s, ball x s ×ˢ ball y s := begin rw nhds_prod_eq, apply (has_basis_nhds x).prod_same_index (has_basis_nhds y), rintro U V ⟨U_in, U_symm⟩ ⟨V_in, V_symm⟩, exact ⟨U ∩ V, ⟨(𝓤 α).inter_sets U_in V_in, U_symm.inter V_symm⟩, ball_inter_left x U V, ball_inter_right y U V⟩, end lemma nhds_eq_uniformity {x : α} : 𝓝 x = (𝓤 α).lift' (ball x) := (nhds_basis_uniformity' (𝓤 α).basis_sets).eq_binfi lemma nhds_eq_uniformity' {x : α} : 𝓝 x = (𝓤 α).lift' (λ s, {y | (y, x) ∈ s}) := (nhds_basis_uniformity (𝓤 α).basis_sets).eq_binfi lemma mem_nhds_left (x : α) {s : set (α×α)} (h : s ∈ 𝓤 α) : {y : α | (x, y) ∈ s} ∈ 𝓝 x := ball_mem_nhds x h lemma mem_nhds_right (y : α) {s : set (α×α)} (h : s ∈ 𝓤 α) : {x : α | (x, y) ∈ s} ∈ 𝓝 y := mem_nhds_left _ (symm_le_uniformity h) lemma is_compact.nhds_set_basis_uniformity {p : ι → Prop} {s : ι → set (α × α)} (hU : (𝓤 α).has_basis p s) {K : set α} (hK : is_compact K) : (𝓝ˢ K).has_basis p (λ i, ⋃ x ∈ K, ball x (s i)) := begin refine ⟨λ U, _⟩, simp only [mem_nhds_set_iff_forall, (nhds_basis_uniformity' hU).mem_iff, Union₂_subset_iff], refine ⟨λ H, _, λ ⟨i, hpi, hi⟩ x hx, ⟨i, hpi, hi x hx⟩⟩, replace H : ∀ x ∈ K, ∃ i : {i // p i}, ball x (s i ○ s i) ⊆ U, { intros x hx, rcases H x hx with ⟨i, hpi, hi⟩, rcases comp_mem_uniformity_sets (hU.mem_of_mem hpi) with ⟨t, ht_mem, ht⟩, rcases hU.mem_iff.1 ht_mem with ⟨j, hpj, hj⟩, exact ⟨⟨j, hpj⟩, subset.trans (ball_mono ((comp_rel_mono hj hj).trans ht) _) hi⟩ }, haveI : nonempty {a // p a}, from nonempty_subtype.2 hU.ex_mem, choose! I hI using H, rcases hK.elim_nhds_subcover (λ x, ball x $ s (I x)) (λ x hx, ball_mem_nhds _ $ hU.mem_of_mem (I x).2) with ⟨t, htK, ht⟩, obtain ⟨i, hpi, hi⟩ : ∃ i (hpi : p i), s i ⊆ ⋂ x ∈ t, s (I x), from hU.mem_iff.1 ((bInter_finset_mem t).2 (λ x hx, hU.mem_of_mem (I x).2)), rw [subset_Inter₂_iff] at hi, refine ⟨i, hpi, λ x hx, _⟩, rcases mem_Union₂.1 (ht hx) with ⟨z, hzt : z ∈ t, hzx : x ∈ ball z (s (I z))⟩, calc ball x (s i) ⊆ ball z (s (I z) ○ s (I z)) : λ y hy, ⟨x, hzx, hi z hzt hy⟩ ... ⊆ U : hI z (htK z hzt), end lemma tendsto_right_nhds_uniformity {a : α} : tendsto (λa', (a', a)) (𝓝 a) (𝓤 α) := assume s, mem_nhds_right a lemma tendsto_left_nhds_uniformity {a : α} : tendsto (λa', (a, a')) (𝓝 a) (𝓤 α) := assume s, mem_nhds_left a lemma lift_nhds_left {x : α} {g : set α → filter β} (hg : monotone g) : (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) := eq.trans begin rw [nhds_eq_uniformity], exact (filter.lift_assoc $ monotone_principal.comp $ monotone_preimage.comp monotone_preimage ) end (congr_arg _ $ funext $ assume s, filter.lift_principal hg) lemma lift_nhds_right {x : α} {g : set α → filter β} (hg : monotone g) : (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (y, x) ∈ s}) := calc (𝓝 x).lift g = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : lift_nhds_left hg ... = ((@prod.swap α α) <$> (𝓤 α)).lift (λs:set (α×α), g {y | (x, y) ∈ s}) : by rw [←uniformity_eq_symm] ... = (𝓤 α).lift (λs:set (α×α), g {y | (x, y) ∈ image prod.swap s}) : map_lift_eq2 $ hg.comp monotone_preimage ... = _ : by simp [image_swap_eq_preimage_swap] lemma nhds_nhds_eq_uniformity_uniformity_prod {a b : α} : 𝓝 a ×ᶠ 𝓝 b = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set (α×α), {y : α | (y, a) ∈ s} ×ˢ {y : α | (b, y) ∈ t})) := begin rw [nhds_eq_uniformity', nhds_eq_uniformity, prod_lift'_lift'], { refl }, { exact monotone_preimage }, { exact monotone_preimage }, end lemma nhds_eq_uniformity_prod {a b : α} : 𝓝 (a, b) = (𝓤 α).lift' (λs:set (α×α), {y : α | (y, a) ∈ s} ×ˢ {y : α | (b, y) ∈ s}) := begin rw [nhds_prod_eq, nhds_nhds_eq_uniformity_uniformity_prod, lift_lift'_same_eq_lift'], { intro s, exact monotone_const.set_prod monotone_preimage }, { intro t, exact monotone_preimage.set_prod monotone_const } end lemma nhdset_of_mem_uniformity {d : set (α×α)} (s : set (α×α)) (hd : d ∈ 𝓤 α) : ∃(t : set (α×α)), is_open t ∧ s ⊆ t ∧ t ⊆ {p | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} := let cl_d := {p:α×α | ∃x y, (p.1, x) ∈ d ∧ (x, y) ∈ s ∧ (y, p.2) ∈ d} in have ∀p ∈ s, ∃t ⊆ cl_d, is_open t ∧ p ∈ t, from assume ⟨x, y⟩ hp, _root_.mem_nhds_iff.mp $ show cl_d ∈ 𝓝 (x, y), begin rw [nhds_eq_uniformity_prod, mem_lift'_sets], exact ⟨d, hd, assume ⟨a, b⟩ ⟨ha, hb⟩, ⟨x, y, ha, hp, hb⟩⟩, exact monotone_preimage.set_prod monotone_preimage end, have ∃t:(Π(p:α×α) (h:p ∈ s), set (α×α)), ∀p, ∀h:p ∈ s, t p h ⊆ cl_d ∧ is_open (t p h) ∧ p ∈ t p h, by simp [classical.skolem] at this; simp; assumption, match this with | ⟨t, ht⟩ := ⟨(⋃ p:α×α, ⋃ h : p ∈ s, t p h : set (α×α)), is_open_Union $ assume (p:α×α), is_open_Union $ assume hp, (ht p hp).right.left, assume ⟨a, b⟩ hp, begin simp; exact ⟨a, b, hp, (ht (a,b) hp).right.right⟩ end, Union_subset $ assume p, Union_subset $ assume hp, (ht p hp).left⟩ end /-- Entourages are neighborhoods of the diagonal. -/ lemma nhds_le_uniformity (x : α) : 𝓝 (x, x) ≤ 𝓤 α := begin intros V V_in, rcases comp_symm_mem_uniformity_sets V_in with ⟨w, w_in, w_symm, w_sub⟩, have : ball x w ×ˢ ball x w ∈ 𝓝 (x, x), { rw nhds_prod_eq, exact prod_mem_prod (ball_mem_nhds x w_in) (ball_mem_nhds x w_in) }, apply mem_of_superset this, rintros ⟨u, v⟩ ⟨u_in, v_in⟩, exact w_sub (mem_comp_of_mem_ball w_symm u_in v_in) end /-- Entourages are neighborhoods of the diagonal. -/ lemma supr_nhds_le_uniformity : (⨆ x : α, 𝓝 (x, x)) ≤ 𝓤 α := supr_le nhds_le_uniformity /-- Entourages are neighborhoods of the diagonal. -/ lemma nhds_set_diagonal_le_uniformity : 𝓝ˢ (diagonal α) ≤ 𝓤 α := (nhds_set_diagonal α).trans_le supr_nhds_le_uniformity /-! ### Closure and interior in uniform spaces -/ lemma closure_eq_uniformity (s : set $ α × α) : closure s = ⋂ V ∈ {V | V ∈ 𝓤 α ∧ symmetric_rel V}, V ○ s ○ V := begin ext ⟨x, y⟩, simp only [mem_closure_iff_nhds_basis (uniform_space.has_basis_nhds_prod x y), mem_Inter, mem_set_of_eq, and_imp, mem_comp_comp, exists_prop, ← mem_inter_eq, inter_comm, set.nonempty] { contextual := tt } end lemma uniformity_has_basis_closed : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α ∧ is_closed V) id := begin refine filter.has_basis_self.2 (λ t h, _), rcases comp_comp_symm_mem_uniformity_sets h with ⟨w, w_in, w_symm, r⟩, refine ⟨closure w, mem_of_superset w_in subset_closure, is_closed_closure, _⟩, refine subset.trans _ r, rw closure_eq_uniformity, apply Inter_subset_of_subset, apply Inter_subset, exact ⟨w_in, w_symm⟩ end lemma uniformity_eq_uniformity_closure : 𝓤 α = (𝓤 α).lift' closure := eq.symm $ uniformity_has_basis_closed.lift'_closure_eq_self $ λ _, and.right lemma filter.has_basis.uniformity_closure {p : ι → Prop} {U : ι → set (α × α)} (h : (𝓤 α).has_basis p U) : (𝓤 α).has_basis p (λ i, closure (U i)) := (@uniformity_eq_uniformity_closure α _).symm ▸ h.lift'_closure /-- Closed entourages form a basis of the uniformity filter. -/ lemma uniformity_has_basis_closure : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α) closure := (𝓤 α).basis_sets.uniformity_closure lemma closure_eq_inter_uniformity {t : set (α×α)} : closure t = (⋂ d ∈ 𝓤 α, d ○ (t ○ d)) := calc closure t = ⋂ V (hV : V ∈ 𝓤 α ∧ symmetric_rel V), V ○ t ○ V : closure_eq_uniformity t ... = ⋂ V ∈ 𝓤 α, V ○ t ○ V : eq.symm $ uniform_space.has_basis_symmetric.bInter_mem $ λ V₁ V₂ hV, comp_rel_mono (comp_rel_mono hV subset.rfl) hV ... = ⋂ V ∈ 𝓤 α, V ○ (t ○ V) : by simp only [comp_rel_assoc] lemma uniformity_eq_uniformity_interior : 𝓤 α = (𝓤 α).lift' interior := le_antisymm (le_infi $ assume d, le_infi $ assume hd, let ⟨s, hs, hs_comp⟩ := (mem_lift'_sets $ monotone_comp_rel monotone_id $ monotone_comp_rel monotone_id monotone_id).mp (comp_le_uniformity3 hd) in let ⟨t, ht, hst, ht_comp⟩ := nhdset_of_mem_uniformity s hs in have s ⊆ interior d, from calc s ⊆ t : hst ... ⊆ interior d : (subset_interior_iff_subset_of_open ht).mpr $ λ x (hx : x ∈ t), let ⟨x, y, h₁, h₂, h₃⟩ := ht_comp hx in hs_comp ⟨x, h₁, y, h₂, h₃⟩, have interior d ∈ 𝓤 α, by filter_upwards [hs] using this, by simp [this]) (assume s hs, ((𝓤 α).lift' interior).sets_of_superset (mem_lift' hs) interior_subset) lemma interior_mem_uniformity {s : set (α × α)} (hs : s ∈ 𝓤 α) : interior s ∈ 𝓤 α := by rw [uniformity_eq_uniformity_interior]; exact mem_lift' hs lemma mem_uniformity_is_closed {s : set (α×α)} (h : s ∈ 𝓤 α) : ∃t ∈ 𝓤 α, is_closed t ∧ t ⊆ s := let ⟨t, ⟨ht_mem, htc⟩, hts⟩ := uniformity_has_basis_closed.mem_iff.1 h in ⟨t, ht_mem, htc, hts⟩ lemma is_open_iff_open_ball_subset {s : set α} : is_open s ↔ ∀ x ∈ s, ∃ V ∈ 𝓤 α, is_open V ∧ ball x V ⊆ s := begin rw is_open_iff_ball_subset, split; intros h x hx, { obtain ⟨V, hV, hV'⟩ := h x hx, exact ⟨interior V, interior_mem_uniformity hV, is_open_interior, (ball_mono interior_subset x).trans hV'⟩, }, { obtain ⟨V, hV, -, hV'⟩ := h x hx, exact ⟨V, hV, hV'⟩, }, end /-- The uniform neighborhoods of all points of a dense set cover the whole space. -/ lemma dense.bUnion_uniformity_ball {s : set α} {U : set (α × α)} (hs : dense s) (hU : U ∈ 𝓤 α) : (⋃ x ∈ s, ball x U) = univ := begin refine Union₂_eq_univ_iff.2 (λ y, _), rcases hs.inter_nhds_nonempty (mem_nhds_right y hU) with ⟨x, hxs, hxy : (x, y) ∈ U⟩, exact ⟨x, hxs, hxy⟩ end /-! ### Uniformity bases -/ /-- Open elements of `𝓤 α` form a basis of `𝓤 α`. -/ lemma uniformity_has_basis_open : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α ∧ is_open V) id := has_basis_self.2 $ λ s hs, ⟨interior s, interior_mem_uniformity hs, is_open_interior, interior_subset⟩ lemma filter.has_basis.mem_uniformity_iff {p : β → Prop} {s : β → set (α×α)} (h : (𝓤 α).has_basis p s) {t : set (α × α)} : t ∈ 𝓤 α ↔ ∃ i (hi : p i), ∀ a b, (a, b) ∈ s i → (a, b) ∈ t := h.mem_iff.trans $ by simp only [prod.forall, subset_def] /-- Open elements `s : set (α × α)` of `𝓤 α` such that `(x, y) ∈ s ↔ (y, x) ∈ s` form a basis of `𝓤 α`. -/ lemma uniformity_has_basis_open_symmetric : has_basis (𝓤 α) (λ V : set (α × α), V ∈ 𝓤 α ∧ is_open V ∧ symmetric_rel V) id := begin simp only [← and_assoc], refine uniformity_has_basis_open.restrict (λ s hs, ⟨symmetrize_rel s, _⟩), exact ⟨⟨symmetrize_mem_uniformity hs.1, is_open.inter hs.2 (hs.2.preimage continuous_swap)⟩, symmetric_symmetrize_rel s, symmetrize_rel_subset_self s⟩ end lemma comp_open_symm_mem_uniformity_sets {s : set (α × α)} (hs : s ∈ 𝓤 α) : ∃ t ∈ 𝓤 α, is_open t ∧ symmetric_rel t ∧ t ○ t ⊆ s := begin obtain ⟨t, ht₁, ht₂⟩ := comp_mem_uniformity_sets hs, obtain ⟨u, ⟨hu₁, hu₂, hu₃⟩, hu₄ : u ⊆ t⟩ := uniformity_has_basis_open_symmetric.mem_iff.mp ht₁, exact ⟨u, hu₁, hu₂, hu₃, (comp_rel_mono hu₄ hu₄).trans ht₂⟩, end section variable (α) lemma uniform_space.has_seq_basis [is_countably_generated $ 𝓤 α] : ∃ V : ℕ → set (α × α), has_antitone_basis (𝓤 α) V ∧ ∀ n, symmetric_rel (V n) := let ⟨U, hsym, hbasis⟩ := uniform_space.has_basis_symmetric.exists_antitone_subbasis in ⟨U, hbasis, λ n, (hsym n).2⟩ end lemma filter.has_basis.bInter_bUnion_ball {p : ι → Prop} {U : ι → set (α × α)} (h : has_basis (𝓤 α) p U) (s : set α) : (⋂ i (hi : p i), ⋃ x ∈ s, ball x (U i)) = closure s := begin ext x, simp [mem_closure_iff_nhds_basis (nhds_basis_uniformity h), ball] end /-! ### Uniform continuity -/ /-- A function `f : α → β` is *uniformly continuous* if `(f x, f y)` tends to the diagonal as `(x, y)` tends to the diagonal. In other words, if `x` is sufficiently close to `y`, then `f x` is close to `f y` no matter where `x` and `y` are located in `α`. -/ def uniform_continuous [uniform_space β] (f : α → β) := tendsto (λx:α×α, (f x.1, f x.2)) (𝓤 α) (𝓤 β) /-- A function `f : α → β` is *uniformly continuous* on `s : set α` if `(f x, f y)` tends to the diagonal as `(x, y)` tends to the diagonal while remaining in `s ×ˢ s`. In other words, if `x` is sufficiently close to `y`, then `f x` is close to `f y` no matter where `x` and `y` are located in `s`.-/ def uniform_continuous_on [uniform_space β] (f : α → β) (s : set α) : Prop := tendsto (λ x : α × α, (f x.1, f x.2)) (𝓤 α ⊓ principal (s ×ˢ s)) (𝓤 β) theorem uniform_continuous_def [uniform_space β] {f : α → β} : uniform_continuous f ↔ ∀ r ∈ 𝓤 β, { x : α × α | (f x.1, f x.2) ∈ r} ∈ 𝓤 α := iff.rfl theorem uniform_continuous_iff_eventually [uniform_space β] {f : α → β} : uniform_continuous f ↔ ∀ r ∈ 𝓤 β, ∀ᶠ (x : α × α) in 𝓤 α, (f x.1, f x.2) ∈ r := iff.rfl theorem uniform_continuous_on_univ [uniform_space β] {f : α → β} : uniform_continuous_on f univ ↔ uniform_continuous f := by rw [uniform_continuous_on, uniform_continuous, univ_prod_univ, principal_univ, inf_top_eq] lemma uniform_continuous_of_const [uniform_space β] {c : α → β} (h : ∀a b, c a = c b) : uniform_continuous c := have (λ (x : α × α), (c (x.fst), c (x.snd))) ⁻¹' id_rel = univ, from eq_univ_iff_forall.2 $ assume ⟨a, b⟩, h a b, le_trans (map_le_iff_le_comap.2 $ by simp [comap_principal, this, univ_mem]) refl_le_uniformity lemma uniform_continuous_id : uniform_continuous (@id α) := by simp [uniform_continuous]; exact tendsto_id lemma uniform_continuous_const [uniform_space β] {b : β} : uniform_continuous (λa:α, b) := uniform_continuous_of_const $ λ _ _, rfl lemma uniform_continuous.comp [uniform_space β] [uniform_space γ] {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) : uniform_continuous (g ∘ f) := hg.comp hf lemma filter.has_basis.uniform_continuous_iff [uniform_space β] {p : γ → Prop} {s : γ → set (α×α)} (ha : (𝓤 α).has_basis p s) {q : δ → Prop} {t : δ → set (β×β)} (hb : (𝓤 β).has_basis q t) {f : α → β} : uniform_continuous f ↔ ∀ i (hi : q i), ∃ j (hj : p j), ∀ x y, (x, y) ∈ s j → (f x, f y) ∈ t i := (ha.tendsto_iff hb).trans $ by simp only [prod.forall] lemma filter.has_basis.uniform_continuous_on_iff [uniform_space β] {p : γ → Prop} {s : γ → set (α×α)} (ha : (𝓤 α).has_basis p s) {q : δ → Prop} {t : δ → set (β×β)} (hb : (𝓤 β).has_basis q t) {f : α → β} {S : set α} : uniform_continuous_on f S ↔ ∀ i (hi : q i), ∃ j (hj : p j), ∀ x y ∈ S, (x, y) ∈ s j → (f x, f y) ∈ t i := ((ha.inf_principal (S ×ˢ S)).tendsto_iff hb).trans $ by simp [prod.forall, set.inter_comm (s _), ball_mem_comm] end uniform_space open_locale uniformity section constructions instance : partial_order (uniform_space α) := { le := λt s, t.uniformity ≤ s.uniformity, le_antisymm := assume t s h₁ h₂, uniform_space_eq $ le_antisymm h₁ h₂, le_refl := assume t, le_rfl, le_trans := assume a b c h₁ h₂, le_trans h₁ h₂ } instance : has_Inf (uniform_space α) := ⟨assume s, uniform_space.of_core { uniformity := (⨅u∈s, @uniformity α u), refl := le_infi $ assume u, le_infi $ assume hu, u.refl, symm := le_infi $ assume u, le_infi $ assume hu, le_trans (map_mono $ infi_le_of_le _ $ infi_le _ hu) u.symm, comp := le_infi $ assume u, le_infi $ assume hu, le_trans (lift'_mono (infi_le_of_le _ $ infi_le _ hu) $ le_rfl) u.comp }⟩ private lemma Inf_le {tt : set (uniform_space α)} {t : uniform_space α} (h : t ∈ tt) : Inf tt ≤ t := show (⨅u∈tt, @uniformity α u) ≤ t.uniformity, from infi_le_of_le t $ infi_le _ h private lemma le_Inf {tt : set (uniform_space α)} {t : uniform_space α} (h : ∀t'∈tt, t ≤ t') : t ≤ Inf tt := show t.uniformity ≤ (⨅u∈tt, @uniformity α u), from le_infi $ assume t', le_infi $ assume ht', h t' ht' instance : has_top (uniform_space α) := ⟨uniform_space.of_core { uniformity := ⊤, refl := le_top, symm := le_top, comp := le_top }⟩ instance : has_bot (uniform_space α) := ⟨{ to_topological_space := ⊥, uniformity := 𝓟 id_rel, refl := le_rfl, symm := by simp [tendsto]; apply subset.refl, comp := begin rw [lift'_principal], {simp}, exact monotone_comp_rel monotone_id monotone_id end, is_open_uniformity := assume s, by simp [is_open_fold, subset_def, id_rel] {contextual := tt } } ⟩ instance : has_inf (uniform_space α) := ⟨λ u₁ u₂, @uniform_space.replace_topology _ (u₁.to_topological_space ⊓ u₂.to_topological_space) (uniform_space.of_core { uniformity := u₁.uniformity ⊓ u₂.uniformity, refl := le_inf u₁.refl u₂.refl, symm := u₁.symm.inf u₂.symm, comp := (lift'_inf_le _ _ _).trans $ inf_le_inf u₁.comp u₂.comp }) $ eq_of_nhds_eq_nhds $ λ a, by simpa only [nhds_inf, nhds_eq_comap_uniformity] using comap_inf.symm⟩ instance : complete_lattice (uniform_space α) := { sup := λa b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf (λ _ ⟨h, _⟩, h), le_sup_right := λ a b, le_Inf (λ _ ⟨_, h⟩, h), sup_le := λ a b c h₁ h₂, Inf_le ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c h₁ h₂, show a.uniformity ≤ _, from le_inf h₁ h₂, inf_le_left := λ a b, show _ ≤ a.uniformity, from inf_le_left, inf_le_right := λ a b, show _ ≤ b.uniformity, from inf_le_right, top := ⊤, le_top := λ a, show a.uniformity ≤ ⊤, from le_top, bot := ⊥, bot_le := λ u, u.refl, Sup := λ tt, Inf {t | ∀ t' ∈ tt, t' ≤ t}, le_Sup := λ s u h, le_Inf (λ u' h', h' u h), Sup_le := λ s u h, Inf_le h, Inf := Inf, le_Inf := λ s a hs, le_Inf hs, Inf_le := λ s a ha, Inf_le ha, ..uniform_space.partial_order } lemma infi_uniformity {ι : Sort*} {u : ι → uniform_space α} : (infi u).uniformity = (⨅i, (u i).uniformity) := show (⨅a (h : ∃i:ι, u i = a), a.uniformity) = _, from le_antisymm (le_infi $ assume i, infi_le_of_le (u i) $ infi_le _ ⟨i, rfl⟩) (le_infi $ assume a, le_infi $ assume ⟨i, (ha : u i = a)⟩, ha ▸ infi_le _ _) lemma infi_uniformity' {ι : Sort*} {u : ι → uniform_space α} : @uniformity α (infi u) = (⨅i, @uniformity α (u i)) := infi_uniformity lemma inf_uniformity {u v : uniform_space α} : (u ⊓ v).uniformity = u.uniformity ⊓ v.uniformity := rfl lemma inf_uniformity' {u v : uniform_space α} : @uniformity α (u ⊓ v) = @uniformity α u ⊓ @uniformity α v := rfl instance inhabited_uniform_space : inhabited (uniform_space α) := ⟨⊥⟩ instance inhabited_uniform_space_core : inhabited (uniform_space.core α) := ⟨@uniform_space.to_core _ default⟩ /-- Given `f : α → β` and a uniformity `u` on `β`, the inverse image of `u` under `f` is the inverse image in the filter sense of the induced function `α × α → β × β`. -/ def uniform_space.comap (f : α → β) (u : uniform_space β) : uniform_space α := { uniformity := u.uniformity.comap (λp:α×α, (f p.1, f p.2)), to_topological_space := u.to_topological_space.induced f, refl := le_trans (by simp; exact assume ⟨a, b⟩ (h : a = b), h ▸ rfl) (comap_mono u.refl), symm := by simp [tendsto_comap_iff, prod.swap, (∘)]; exact tendsto_swap_uniformity.comp tendsto_comap, comp := le_trans begin rw [comap_lift'_eq, comap_lift'_eq2], exact (lift'_mono' $ assume s hs ⟨a₁, a₂⟩ ⟨x, h₁, h₂⟩, ⟨f x, h₁, h₂⟩), exact monotone_comp_rel monotone_id monotone_id end (comap_mono u.comp), is_open_uniformity := λ s, begin change (@is_open α (u.to_topological_space.induced f) s ↔ _), simp [is_open_iff_nhds, nhds_induced, mem_nhds_uniformity_iff_right, filter.comap, and_comm], refine ball_congr (λ x hx, ⟨_, _⟩), { rintro ⟨t, hts, ht⟩, refine ⟨_, ht, _⟩, rintro ⟨x₁, x₂⟩ h rfl, exact hts (h rfl) }, { rintro ⟨t, ht, hts⟩, exact ⟨{y | (f x, y) ∈ t}, λ y hy, @hts (x, y) hy rfl, mem_nhds_uniformity_iff_right.1 $ mem_nhds_left _ ht⟩ } end } lemma uniformity_comap [uniform_space α] [uniform_space β] {f : α → β} (h : ‹uniform_space α› = uniform_space.comap f ‹uniform_space β›) : 𝓤 α = comap (prod.map f f) (𝓤 β) := by { rw h, refl } lemma uniform_space_comap_id {α : Type*} : uniform_space.comap (id : α → α) = id := by ext u ; dsimp only [uniform_space.comap, id] ; rw [prod.id_prod, filter.comap_id] lemma uniform_space.comap_comap {α β γ} [uγ : uniform_space γ] {f : α → β} {g : β → γ} : uniform_space.comap (g ∘ f) uγ = uniform_space.comap f (uniform_space.comap g uγ) := by ext ; dsimp only [uniform_space.comap] ; rw filter.comap_comap lemma uniform_space.comap_inf {α γ} {u₁ u₂ : uniform_space γ} {f : α → γ} : (u₁ ⊓ u₂).comap f = u₁.comap f ⊓ u₂.comap f := uniform_space_eq comap_inf lemma uniform_space.comap_infi {ι α γ} {u : ι → uniform_space γ} {f : α → γ} : (⨅ i, u i).comap f = ⨅ i, (u i).comap f := begin ext : 1, change (𝓤 _) = (𝓤 _), simp [uniformity_comap rfl, infi_uniformity'] end lemma uniform_space.comap_mono {α γ} {f : α → γ} : monotone (λ u : uniform_space γ, u.comap f) := begin intros u₁ u₂ hu, change (𝓤 _) ≤ (𝓤 _), rw uniformity_comap rfl, exact comap_mono hu end lemma uniform_continuous_iff {α β} {uα : uniform_space α} {uβ : uniform_space β} {f : α → β} : uniform_continuous f ↔ uα ≤ uβ.comap f := filter.map_le_iff_le_comap lemma le_iff_uniform_continuous_id {u v : uniform_space α} : u ≤ v ↔ @uniform_continuous _ _ u v id := by rw [uniform_continuous_iff, uniform_space_comap_id, id] lemma uniform_continuous_comap {f : α → β} [u : uniform_space β] : @uniform_continuous α β (uniform_space.comap f u) u f := tendsto_comap theorem to_topological_space_comap {f : α → β} {u : uniform_space β} : @uniform_space.to_topological_space _ (uniform_space.comap f u) = topological_space.induced f (@uniform_space.to_topological_space β u) := rfl lemma uniform_continuous_comap' {f : γ → β} {g : α → γ} [v : uniform_space β] [u : uniform_space α] (h : uniform_continuous (f ∘ g)) : @uniform_continuous α γ u (uniform_space.comap f v) g := tendsto_comap_iff.2 h lemma to_nhds_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) (a : α) : @nhds _ (@uniform_space.to_topological_space _ u₁) a ≤ @nhds _ (@uniform_space.to_topological_space _ u₂) a := by rw [@nhds_eq_uniformity α u₁ a, @nhds_eq_uniformity α u₂ a]; exact (lift'_mono h le_rfl) lemma to_topological_space_mono {u₁ u₂ : uniform_space α} (h : u₁ ≤ u₂) : @uniform_space.to_topological_space _ u₁ ≤ @uniform_space.to_topological_space _ u₂ := le_of_nhds_le_nhds $ to_nhds_mono h lemma uniform_continuous.continuous [uniform_space α] [uniform_space β] {f : α → β} (hf : uniform_continuous f) : continuous f := continuous_iff_le_induced.mpr $ to_topological_space_mono $ uniform_continuous_iff.1 hf lemma to_topological_space_bot : @uniform_space.to_topological_space α ⊥ = ⊥ := rfl lemma to_topological_space_top : @uniform_space.to_topological_space α ⊤ = ⊤ := top_unique $ assume s hs, s.eq_empty_or_nonempty.elim (assume : s = ∅, this.symm ▸ @is_open_empty _ ⊤) (assume ⟨x, hx⟩, have s = univ, from top_unique $ assume y hy, hs x hx (x, y) rfl, this.symm ▸ @is_open_univ _ ⊤) lemma to_topological_space_infi {ι : Sort*} {u : ι → uniform_space α} : (infi u).to_topological_space = ⨅i, (u i).to_topological_space := begin refine (eq_of_nhds_eq_nhds $ assume a, _), rw [nhds_infi, nhds_eq_uniformity], change (infi u).uniformity.lift' (preimage $ prod.mk a) = _, rw [infi_uniformity, lift'_infi_of_map_univ _ preimage_univ], { simp only [nhds_eq_uniformity], refl }, { exact ball_inter _ } end lemma to_topological_space_Inf {s : set (uniform_space α)} : (Inf s).to_topological_space = (⨅i∈s, @uniform_space.to_topological_space α i) := begin rw [Inf_eq_infi], simp only [← to_topological_space_infi], end lemma to_topological_space_inf {u v : uniform_space α} : (u ⊓ v).to_topological_space = u.to_topological_space ⊓ v.to_topological_space := rfl section uniform_continuous_infi lemma uniform_continuous_inf_rng {f : α → β} {u₁ : uniform_space α} {u₂ u₃ : uniform_space β} (h₁ : @@uniform_continuous u₁ u₂ f) (h₂ : @@uniform_continuous u₁ u₃ f) : @@uniform_continuous u₁ (u₂ ⊓ u₃) f := tendsto_inf.mpr ⟨h₁, h₂⟩ lemma uniform_continuous_inf_dom_left {f : α → β} {u₁ u₂ : uniform_space α} {u₃ : uniform_space β} (hf : @@uniform_continuous u₁ u₃ f) : @@uniform_continuous (u₁ ⊓ u₂) u₃ f := tendsto_inf_left hf lemma uniform_continuous_inf_dom_right {f : α → β} {u₁ u₂ : uniform_space α} {u₃ : uniform_space β} (hf : @@uniform_continuous u₂ u₃ f) : @@uniform_continuous (u₁ ⊓ u₂) u₃ f := tendsto_inf_right hf lemma uniform_continuous_Inf_dom {f : α → β} {u₁ : set (uniform_space α)} {u₂ : uniform_space β} {u : uniform_space α} (h₁ : u ∈ u₁) (hf : @@uniform_continuous u u₂ f) : @@uniform_continuous (Inf u₁) u₂ f := begin rw [uniform_continuous, Inf_eq_infi', infi_uniformity'], exact tendsto_infi' ⟨u, h₁⟩ hf end lemma uniform_continuous_Inf_rng {f : α → β} {u₁ : uniform_space α} {u₂ : set (uniform_space β)} (h : ∀u∈u₂, @@uniform_continuous u₁ u f) : @@uniform_continuous u₁ (Inf u₂) f := begin rw [uniform_continuous, Inf_eq_infi', infi_uniformity'], exact tendsto_infi.mpr (λ ⟨u, hu⟩, h u hu) end lemma uniform_continuous_infi_dom {f : α → β} {u₁ : ι → uniform_space α} {u₂ : uniform_space β} {i : ι} (hf : @@uniform_continuous (u₁ i) u₂ f) : @@uniform_continuous (infi u₁) u₂ f := begin rw [uniform_continuous, infi_uniformity'], exact tendsto_infi' i hf end lemma uniform_continuous_infi_rng {f : α → β} {u₁ : uniform_space α} {u₂ : ι → uniform_space β} (h : ∀i, @@uniform_continuous u₁ (u₂ i) f) : @@uniform_continuous u₁ (infi u₂) f := by rwa [uniform_continuous, infi_uniformity', tendsto_infi] end uniform_continuous_infi /-- A uniform space with the discrete uniformity has the discrete topology. -/ lemma discrete_topology_of_discrete_uniformity [hα : uniform_space α] (h : uniformity α = 𝓟 id_rel) : discrete_topology α := ⟨(uniform_space_eq h.symm : ⊥ = hα) ▸ rfl⟩ instance : uniform_space empty := ⊥ instance : uniform_space punit := ⊥ instance : uniform_space bool := ⊥ instance : uniform_space ℕ := ⊥ instance : uniform_space ℤ := ⊥ section variables [uniform_space α] open additive multiplicative instance : uniform_space (additive α) := ‹uniform_space α› instance : uniform_space (multiplicative α) := ‹uniform_space α› lemma uniform_continuous_of_mul : uniform_continuous (of_mul : α → additive α) := uniform_continuous_id lemma uniform_continuous_to_mul : uniform_continuous (to_mul : additive α → α) := uniform_continuous_id lemma uniform_continuous_of_add : uniform_continuous (of_add : α → multiplicative α) := uniform_continuous_id lemma uniform_continuous_to_add : uniform_continuous (to_add : multiplicative α → α) := uniform_continuous_id lemma uniformity_additive : 𝓤 (additive α) = (𝓤 α).map (prod.map of_mul of_mul) := by { convert map_id.symm, exact prod.map_id } lemma uniformity_multiplicative : 𝓤 (multiplicative α) = (𝓤 α).map (prod.map of_add of_add) := by { convert map_id.symm, exact prod.map_id } end instance {p : α → Prop} [t : uniform_space α] : uniform_space (subtype p) := uniform_space.comap subtype.val t lemma uniformity_subtype {p : α → Prop} [t : uniform_space α] : 𝓤 (subtype p) = comap (λq:subtype p × subtype p, (q.1.1, q.2.1)) (𝓤 α) := rfl lemma uniform_continuous_subtype_val {p : α → Prop} [uniform_space α] : uniform_continuous (subtype.val : {a : α // p a} → α) := uniform_continuous_comap lemma uniform_continuous_subtype_coe {p : α → Prop} [uniform_space α] : uniform_continuous (coe : {a : α // p a} → α) := uniform_continuous_subtype_val lemma uniform_continuous.subtype_mk {p : α → Prop} [uniform_space α] [uniform_space β] {f : β → α} (hf : uniform_continuous f) (h : ∀x, p (f x)) : uniform_continuous (λx, ⟨f x, h x⟩ : β → subtype p) := uniform_continuous_comap' hf lemma uniform_continuous_on_iff_restrict [uniform_space α] [uniform_space β] {f : α → β} {s : set α} : uniform_continuous_on f s ↔ uniform_continuous (s.restrict f) := begin unfold uniform_continuous_on set.restrict uniform_continuous tendsto, rw [show (λ x : s × s, (f x.1, f x.2)) = prod.map f f ∘ coe, by ext x; cases x; refl, uniformity_comap rfl, show prod.map subtype.val subtype.val = (coe : s × s → α × α), by ext x; cases x; refl], conv in (map _ (comap _ _)) { rw ← filter.map_map }, rw subtype_coe_map_comap_prod, refl, end lemma tendsto_of_uniform_continuous_subtype [uniform_space α] [uniform_space β] {f : α → β} {s : set α} {a : α} (hf : uniform_continuous (λx:s, f x.val)) (ha : s ∈ 𝓝 a) : tendsto f (𝓝 a) (𝓝 (f a)) := by rw [(@map_nhds_subtype_coe_eq α _ s a (mem_of_mem_nhds ha) ha).symm]; exact tendsto_map' (continuous_iff_continuous_at.mp hf.continuous _) lemma uniform_continuous_on.continuous_on [uniform_space α] [uniform_space β] {f : α → β} {s : set α} (h : uniform_continuous_on f s) : continuous_on f s := begin rw uniform_continuous_on_iff_restrict at h, rw continuous_on_iff_continuous_restrict, exact h.continuous end @[to_additive] instance [uniform_space α] : uniform_space (αᵐᵒᵖ) := uniform_space.comap mul_opposite.unop ‹_› @[to_additive] lemma uniformity_mul_opposite [uniform_space α] : 𝓤 (αᵐᵒᵖ) = comap (λ q : αᵐᵒᵖ × αᵐᵒᵖ, (q.1.unop, q.2.unop)) (𝓤 α) := rfl @[simp, to_additive] lemma comap_uniformity_mul_opposite [uniform_space α] : comap (λ p : α × α, (mul_opposite.op p.1, mul_opposite.op p.2)) (𝓤 αᵐᵒᵖ) = 𝓤 α := by simpa [uniformity_mul_opposite, comap_comap, (∘)] using comap_id namespace mul_opposite @[to_additive] lemma uniform_continuous_unop [uniform_space α] : uniform_continuous (unop : αᵐᵒᵖ → α) := uniform_continuous_comap @[to_additive] lemma uniform_continuous_op [uniform_space α] : uniform_continuous (op : α → αᵐᵒᵖ) := uniform_continuous_comap' uniform_continuous_id end mul_opposite section prod /- a similar product space is possible on the function space (uniformity of pointwise convergence), but we want to have the uniformity of uniform convergence on function spaces -/ instance [u₁ : uniform_space α] [u₂ : uniform_space β] : uniform_space (α × β) := u₁.comap prod.fst ⊓ u₂.comap prod.snd -- check the above produces no diamond example [u₁ : uniform_space α] [u₂ : uniform_space β] : (prod.topological_space : topological_space (α × β)) = uniform_space.to_topological_space := rfl theorem uniformity_prod [uniform_space α] [uniform_space β] : 𝓤 (α × β) = (𝓤 α).comap (λp:(α × β) × α × β, (p.1.1, p.2.1)) ⊓ (𝓤 β).comap (λp:(α × β) × α × β, (p.1.2, p.2.2)) := rfl lemma uniformity_prod_eq_comap_prod [uniform_space α] [uniform_space β] : 𝓤 (α × β) = comap (λ p : (α × β) × (α × β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (𝓤 α ×ᶠ 𝓤 β) := by rw [uniformity_prod, filter.prod, comap_inf, comap_comap, comap_comap] lemma uniformity_prod_eq_prod [uniform_space α] [uniform_space β] : 𝓤 (α × β) = map (λ p : (α × α) × (β × β), ((p.1.1, p.2.1), (p.1.2, p.2.2))) (𝓤 α ×ᶠ 𝓤 β) := by rw [map_swap4_eq_comap, uniformity_prod_eq_comap_prod] lemma mem_uniformity_of_uniform_continuous_invariant [uniform_space α] [uniform_space β] {s : set (β × β)} {f : α → α → β} (hf : uniform_continuous (λ p : α × α, f p.1 p.2)) (hs : s ∈ 𝓤 β) : ∃ u ∈ 𝓤 α, ∀ a b c, (a, b) ∈ u → (f a c, f b c) ∈ s := begin rw [uniform_continuous, uniformity_prod_eq_prod, tendsto_map'_iff, (∘)] at hf, rcases mem_prod_iff.1 (mem_map.1 $ hf hs) with ⟨u, hu, v, hv, huvt⟩, exact ⟨u, hu, λ a b c hab, @huvt ((_, _), (_, _)) ⟨hab, refl_mem_uniformity hv⟩⟩ end lemma mem_uniform_prod [t₁ : uniform_space α] [t₂ : uniform_space β] {a : set (α × α)} {b : set (β × β)} (ha : a ∈ 𝓤 α) (hb : b ∈ 𝓤 β) : {p:(α×β)×(α×β) | (p.1.1, p.2.1) ∈ a ∧ (p.1.2, p.2.2) ∈ b } ∈ (@uniformity (α × β) _) := by rw [uniformity_prod]; exact inter_mem_inf (preimage_mem_comap ha) (preimage_mem_comap hb) lemma tendsto_prod_uniformity_fst [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.1, p.2.1)) (𝓤 (α × β)) (𝓤 α) := le_trans (map_mono inf_le_left) map_comap_le lemma tendsto_prod_uniformity_snd [uniform_space α] [uniform_space β] : tendsto (λp:(α×β)×(α×β), (p.1.2, p.2.2)) (𝓤 (α × β)) (𝓤 β) := le_trans (map_mono inf_le_right) map_comap_le lemma uniform_continuous_fst [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.1) := tendsto_prod_uniformity_fst lemma uniform_continuous_snd [uniform_space α] [uniform_space β] : uniform_continuous (λp:α×β, p.2) := tendsto_prod_uniformity_snd variables [uniform_space α] [uniform_space β] [uniform_space γ] lemma uniform_continuous.prod_mk {f₁ : α → β} {f₂ : α → γ} (h₁ : uniform_continuous f₁) (h₂ : uniform_continuous f₂) : uniform_continuous (λa, (f₁ a, f₂ a)) := by rw [uniform_continuous, uniformity_prod]; exact tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma uniform_continuous.prod_mk_left {f : α × β → γ} (h : uniform_continuous f) (b) : uniform_continuous (λ a, f (a,b)) := h.comp (uniform_continuous_id.prod_mk uniform_continuous_const) lemma uniform_continuous.prod_mk_right {f : α × β → γ} (h : uniform_continuous f) (a) : uniform_continuous (λ b, f (a,b)) := h.comp (uniform_continuous_const.prod_mk uniform_continuous_id) lemma uniform_continuous.prod_map [uniform_space δ] {f : α → γ} {g : β → δ} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (prod.map f g) := (hf.comp uniform_continuous_fst).prod_mk (hg.comp uniform_continuous_snd) lemma to_topological_space_prod {α} {β} [u : uniform_space α] [v : uniform_space β] : @uniform_space.to_topological_space (α × β) prod.uniform_space = @prod.topological_space α β u.to_topological_space v.to_topological_space := rfl /-- A version of `uniform_continuous_inf_dom_left` for binary functions -/ lemma uniform_continuous_inf_dom_left₂ {α β γ} {f : α → β → γ} {ua1 ua2 : uniform_space α} {ub1 ub2 : uniform_space β} {uc1 : uniform_space γ} (h : by haveI := ua1; haveI := ub1; exact uniform_continuous (λ p : α × β, f p.1 p.2)) : by haveI := ua1 ⊓ ua2; haveI := ub1 ⊓ ub2; exact uniform_continuous (λ p : α × β, f p.1 p.2) := begin -- proof essentially copied from ``continuous_inf_dom_left₂` have ha := @uniform_continuous_inf_dom_left _ _ id ua1 ua2 ua1 (@uniform_continuous_id _ (id _)), have hb := @uniform_continuous_inf_dom_left _ _ id ub1 ub2 ub1 (@uniform_continuous_id _ (id _)), have h_unif_cont_id := @uniform_continuous.prod_map _ _ _ _ ( ua1 ⊓ ua2) (ub1 ⊓ ub2) ua1 ub1 _ _ ha hb, exact @uniform_continuous.comp _ _ _ (id _) (id _) _ _ _ h h_unif_cont_id, end /-- A version of `uniform_continuous_inf_dom_right` for binary functions -/ lemma uniform_continuous_inf_dom_right₂ {α β γ} {f : α → β → γ} {ua1 ua2 : uniform_space α} {ub1 ub2 : uniform_space β} {uc1 : uniform_space γ} (h : by haveI := ua2; haveI := ub2; exact uniform_continuous (λ p : α × β, f p.1 p.2)) : by haveI := ua1 ⊓ ua2; haveI := ub1 ⊓ ub2; exact uniform_continuous (λ p : α × β, f p.1 p.2) := begin -- proof essentially copied from ``continuous_inf_dom_right₂` have ha := @uniform_continuous_inf_dom_right _ _ id ua1 ua2 ua2 (@uniform_continuous_id _ (id _)), have hb := @uniform_continuous_inf_dom_right _ _ id ub1 ub2 ub2 (@uniform_continuous_id _ (id _)), have h_unif_cont_id := @uniform_continuous.prod_map _ _ _ _ (ua1 ⊓ ua2) (ub1 ⊓ ub2) ua2 ub2 _ _ ha hb, exact @uniform_continuous.comp _ _ _ (id _) (id _) _ _ _ h h_unif_cont_id, end /-- A version of `uniform_continuous_Inf_dom` for binary functions -/ lemma uniform_continuous_Inf_dom₂ {α β γ} {f : α → β → γ} {uas : set (uniform_space α)} {ubs : set (uniform_space β)} {ua : uniform_space α} {ub : uniform_space β} {uc : uniform_space γ} (ha : ua ∈ uas) (hb : ub ∈ ubs) (hf : uniform_continuous (λ p : α × β, f p.1 p.2)): by haveI := Inf uas; haveI := Inf ubs; exact @uniform_continuous _ _ _ uc (λ p : α × β, f p.1 p.2) := begin -- proof essentially copied from ``continuous_Inf_dom` let t : uniform_space (α × β) := prod.uniform_space, have ha := uniform_continuous_Inf_dom ha uniform_continuous_id, have hb := uniform_continuous_Inf_dom hb uniform_continuous_id, have h_unif_cont_id := @uniform_continuous.prod_map _ _ _ _ (Inf uas) (Inf ubs) ua ub _ _ ha hb, exact @uniform_continuous.comp _ _ _ (id _) (id _) _ _ _ hf h_unif_cont_id, end end prod section open uniform_space function variables {δ' : Type*} [uniform_space α] [uniform_space β] [uniform_space γ] [uniform_space δ] [uniform_space δ'] local notation f ` ∘₂ ` g := function.bicompr f g /-- Uniform continuity for functions of two variables. -/ def uniform_continuous₂ (f : α → β → γ) := uniform_continuous (uncurry f) lemma uniform_continuous₂_def (f : α → β → γ) : uniform_continuous₂ f ↔ uniform_continuous (uncurry f) := iff.rfl lemma uniform_continuous₂.uniform_continuous {f : α → β → γ} (h : uniform_continuous₂ f) : uniform_continuous (uncurry f) := h lemma uniform_continuous₂_curry (f : α × β → γ) : uniform_continuous₂ (function.curry f) ↔ uniform_continuous f := by rw [uniform_continuous₂, uncurry_curry] lemma uniform_continuous₂.comp {f : α → β → γ} {g : γ → δ} (hg : uniform_continuous g) (hf : uniform_continuous₂ f) : uniform_continuous₂ (g ∘₂ f) := hg.comp hf lemma uniform_continuous₂.bicompl {f : α → β → γ} {ga : δ → α} {gb : δ' → β} (hf : uniform_continuous₂ f) (hga : uniform_continuous ga) (hgb : uniform_continuous gb) : uniform_continuous₂ (bicompl f ga gb) := hf.uniform_continuous.comp (hga.prod_map hgb) end lemma to_topological_space_subtype [u : uniform_space α] {p : α → Prop} : @uniform_space.to_topological_space (subtype p) subtype.uniform_space = @subtype.topological_space α p u.to_topological_space := rfl section sum variables [uniform_space α] [uniform_space β] open sum /-- Uniformity on a disjoint union. Entourages of the diagonal in the union are obtained by taking independently an entourage of the diagonal in the first part, and an entourage of the diagonal in the second part. -/ def uniform_space.core.sum : uniform_space.core (α ⊕ β) := uniform_space.core.mk' (map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β)) (λ r ⟨H₁, H₂⟩ x, by cases x; [apply refl_mem_uniformity H₁, apply refl_mem_uniformity H₂]) (λ r ⟨H₁, H₂⟩, ⟨symm_le_uniformity H₁, symm_le_uniformity H₂⟩) (λ r ⟨Hrα, Hrβ⟩, begin rcases comp_mem_uniformity_sets Hrα with ⟨tα, htα, Htα⟩, rcases comp_mem_uniformity_sets Hrβ with ⟨tβ, htβ, Htβ⟩, refine ⟨_, ⟨mem_map_iff_exists_image.2 ⟨tα, htα, subset_union_left _ _⟩, mem_map_iff_exists_image.2 ⟨tβ, htβ, subset_union_right _ _⟩⟩, _⟩, rintros ⟨_, _⟩ ⟨z, ⟨⟨a, b⟩, hab, ⟨⟩⟩ | ⟨⟨a, b⟩, hab, ⟨⟩⟩, ⟨⟨_, c⟩, hbc, ⟨⟩⟩ | ⟨⟨_, c⟩, hbc, ⟨⟩⟩⟩, { have A : (a, c) ∈ tα ○ tα := ⟨b, hab, hbc⟩, exact Htα A }, { have A : (a, c) ∈ tβ ○ tβ := ⟨b, hab, hbc⟩, exact Htβ A } end) /-- The union of an entourage of the diagonal in each set of a disjoint union is again an entourage of the diagonal. -/ lemma union_mem_uniformity_sum {a : set (α × α)} (ha : a ∈ 𝓤 α) {b : set (β × β)} (hb : b ∈ 𝓤 β) : ((λ p : (α × α), (inl p.1, inl p.2)) '' a ∪ (λ p : (β × β), (inr p.1, inr p.2)) '' b) ∈ (@uniform_space.core.sum α β _ _).uniformity := ⟨mem_map_iff_exists_image.2 ⟨_, ha, subset_union_left _ _⟩, mem_map_iff_exists_image.2 ⟨_, hb, subset_union_right _ _⟩⟩ /- To prove that the topology defined by the uniform structure on the disjoint union coincides with the disjoint union topology, we need two lemmas saying that open sets can be characterized by the uniform structure -/ lemma uniformity_sum_of_open_aux {s : set (α ⊕ β)} (hs : is_open s) {x : α ⊕ β} (xs : x ∈ s) : { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity := begin cases x, { refine mem_of_superset (union_mem_uniformity_sum (mem_nhds_uniformity_iff_right.1 (is_open.mem_nhds hs.1 xs)) univ_mem) (union_subset _ _); rintro _ ⟨⟨_, b⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, { refine mem_of_superset (union_mem_uniformity_sum univ_mem (mem_nhds_uniformity_iff_right.1 (is_open.mem_nhds hs.2 xs))) (union_subset _ _); rintro _ ⟨⟨a, _⟩, h, ⟨⟩⟩ ⟨⟩, exact h rfl }, end lemma open_of_uniformity_sum_aux {s : set (α ⊕ β)} (hs : ∀x ∈ s, { p : ((α ⊕ β) × (α ⊕ β)) | p.1 = x → p.2 ∈ s } ∈ (@uniform_space.core.sum α β _ _).uniformity) : is_open s := begin split, { refine (@is_open_iff_mem_nhds α _ _).2 (λ a ha, mem_nhds_uniformity_iff_right.2 _), rcases mem_map_iff_exists_image.1 (hs _ ha).1 with ⟨t, ht, st⟩, refine mem_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl }, { refine (@is_open_iff_mem_nhds β _ _).2 (λ b hb, mem_nhds_uniformity_iff_right.2 _), rcases mem_map_iff_exists_image.1 (hs _ hb).2 with ⟨t, ht, st⟩, refine mem_of_superset ht _, rintro p pt rfl, exact st ⟨_, pt, rfl⟩ rfl } end /- We can now define the uniform structure on the disjoint union -/ instance sum.uniform_space : uniform_space (α ⊕ β) := { to_core := uniform_space.core.sum, is_open_uniformity := λ s, ⟨uniformity_sum_of_open_aux, open_of_uniformity_sum_aux⟩ } lemma sum.uniformity : 𝓤 (α ⊕ β) = map (λ p : α × α, (inl p.1, inl p.2)) (𝓤 α) ⊔ map (λ p : β × β, (inr p.1, inr p.2)) (𝓤 β) := rfl end sum end constructions -- For a version of the Lebesgue number lemma assuming only a sequentially compact space, -- see topology/sequences.lean /-- Let `c : ι → set α` be an open cover of a compact set `s`. Then there exists an entourage `n` such that for each `x ∈ s` its `n`-neighborhood is contained in some `c i`. -/ lemma lebesgue_number_lemma {α : Type u} [uniform_space α] {s : set α} {ι} {c : ι → set α} (hs : is_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ i, {y | (x, y) ∈ n} ⊆ c i := begin let u := λ n, {x | ∃ i (m ∈ 𝓤 α), {y | (x, y) ∈ m ○ n} ⊆ c i}, have hu₁ : ∀ n ∈ 𝓤 α, is_open (u n), { refine λ n hn, is_open_uniformity.2 _, rintro x ⟨i, m, hm, h⟩, rcases comp_mem_uniformity_sets hm with ⟨m', hm', mm'⟩, apply (𝓤 α).sets_of_superset hm', rintros ⟨x, y⟩ hp rfl, refine ⟨i, m', hm', λ z hz, h (monotone_comp_rel monotone_id monotone_const mm' _)⟩, dsimp [-mem_comp_rel] at hz ⊢, rw comp_rel_assoc, exact ⟨y, hp, hz⟩ }, have hu₂ : s ⊆ ⋃ n ∈ 𝓤 α, u n, { intros x hx, rcases mem_Union.1 (hc₂ hx) with ⟨i, h⟩, rcases comp_mem_uniformity_sets (is_open_uniformity.1 (hc₁ i) x h) with ⟨m', hm', mm'⟩, exact mem_bUnion hm' ⟨i, _, hm', λ y hy, mm' hy rfl⟩ }, rcases hs.elim_finite_subcover_image hu₁ hu₂ with ⟨b, bu, b_fin, b_cover⟩, refine ⟨_, (bInter_mem b_fin).2 bu, λ x hx, _⟩, rcases mem_Union₂.1 (b_cover hx) with ⟨n, bn, i, m, hm, h⟩, refine ⟨i, λ y hy, h _⟩, exact prod_mk_mem_comp_rel (refl_mem_uniformity hm) (bInter_subset_of_mem bn hy) end /-- Let `c : set (set α)` be an open cover of a compact set `s`. Then there exists an entourage `n` such that for each `x ∈ s` its `n`-neighborhood is contained in some `t ∈ c`. -/ lemma lebesgue_number_lemma_sUnion {α : Type u} [uniform_space α] {s : set α} {c : set (set α)} (hs : is_compact s) (hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃ n ∈ 𝓤 α, ∀ x ∈ s, ∃ t ∈ c, ∀ y, (x, y) ∈ n → y ∈ t := by rw sUnion_eq_Union at hc₂; simpa using lebesgue_number_lemma hs (by simpa) hc₂ /-- A useful consequence of the Lebesgue number lemma: given any compact set `K` contained in an open set `U`, we can find an (open) entourage `V` such that the ball of size `V` about any point of `K` is contained in `U`. -/ lemma lebesgue_number_of_compact_open [uniform_space α] {K U : set α} (hK : is_compact K) (hU : is_open U) (hKU : K ⊆ U) : ∃ V ∈ 𝓤 α, is_open V ∧ ∀ x ∈ K, uniform_space.ball x V ⊆ U := begin let W : K → set (α × α) := λ k, classical.some $ is_open_iff_open_ball_subset.mp hU k.1 $ hKU k.2, have hW : ∀ k, W k ∈ 𝓤 α ∧ is_open (W k) ∧ uniform_space.ball k.1 (W k) ⊆ U, { intros k, obtain ⟨h₁, h₂, h₃⟩ := classical.some_spec (is_open_iff_open_ball_subset.mp hU k.1 (hKU k.2)), exact ⟨h₁, h₂, h₃⟩, }, let c : K → set α := λ k, uniform_space.ball k.1 (W k), have hc₁ : ∀ k, is_open (c k), { exact λ k, uniform_space.is_open_ball k.1 (hW k).2.1, }, have hc₂ : K ⊆ ⋃ i, c i, { intros k hk, simp only [mem_Union, set_coe.exists], exact ⟨k, hk, uniform_space.mem_ball_self k (hW ⟨k, hk⟩).1⟩, }, have hc₃ : ∀ k, c k ⊆ U, { exact λ k, (hW k).2.2, }, obtain ⟨V, hV, hV'⟩ := lebesgue_number_lemma hK hc₁ hc₂, refine ⟨interior V, interior_mem_uniformity hV, is_open_interior, _⟩, intros k hk, obtain ⟨k', hk'⟩ := hV' k hk, exact ((ball_mono interior_subset k).trans hk').trans (hc₃ k'), end /-! ### Expressing continuity properties in uniform spaces We reformulate the various continuity properties of functions taking values in a uniform space in terms of the uniformity in the target. Since the same lemmas (essentially with the same names) also exist for metric spaces and emetric spaces (reformulating things in terms of the distance or the edistance in the target), we put them in a namespace `uniform` here. In the metric and emetric space setting, there are also similar lemmas where one assumes that both the source and the target are metric spaces, reformulating things in terms of the distance on both sides. These lemmas are generally written without primes, and the versions where only the target is a metric space is primed. We follow the same convention here, thus giving lemmas with primes. -/ namespace uniform variables [uniform_space α] theorem tendsto_nhds_right {f : filter β} {u : β → α} {a : α} : tendsto u f (𝓝 a) ↔ tendsto (λ x, (a, u x)) f (𝓤 α) := ⟨λ H, tendsto_left_nhds_uniformity.comp H, λ H s hs, by simpa [mem_of_mem_nhds hs] using H (mem_nhds_uniformity_iff_right.1 hs)⟩ theorem tendsto_nhds_left {f : filter β} {u : β → α} {a : α} : tendsto u f (𝓝 a) ↔ tendsto (λ x, (u x, a)) f (𝓤 α) := ⟨λ H, tendsto_right_nhds_uniformity.comp H, λ H s hs, by simpa [mem_of_mem_nhds hs] using H (mem_nhds_uniformity_iff_left.1 hs)⟩ theorem continuous_at_iff'_right [topological_space β] {f : β → α} {b : β} : continuous_at f b ↔ tendsto (λ x, (f b, f x)) (𝓝 b) (𝓤 α) := by rw [continuous_at, tendsto_nhds_right] theorem continuous_at_iff'_left [topological_space β] {f : β → α} {b : β} : continuous_at f b ↔ tendsto (λ x, (f x, f b)) (𝓝 b) (𝓤 α) := by rw [continuous_at, tendsto_nhds_left] theorem continuous_at_iff_prod [topological_space β] {f : β → α} {b : β} : continuous_at f b ↔ tendsto (λ x : β × β, (f x.1, f x.2)) (𝓝 (b, b)) (𝓤 α) := ⟨λ H, le_trans (H.prod_map' H) (nhds_le_uniformity _), λ H, continuous_at_iff'_left.2 $ H.comp $ tendsto_id.prod_mk_nhds tendsto_const_nhds⟩ theorem continuous_within_at_iff'_right [topological_space β] {f : β → α} {b : β} {s : set β} : continuous_within_at f s b ↔ tendsto (λ x, (f b, f x)) (𝓝[s] b) (𝓤 α) := by rw [continuous_within_at, tendsto_nhds_right] theorem continuous_within_at_iff'_left [topological_space β] {f : β → α} {b : β} {s : set β} : continuous_within_at f s b ↔ tendsto (λ x, (f x, f b)) (𝓝[s] b) (𝓤 α) := by rw [continuous_within_at, tendsto_nhds_left] theorem continuous_on_iff'_right [topological_space β] {f : β → α} {s : set β} : continuous_on f s ↔ ∀ b ∈ s, tendsto (λ x, (f b, f x)) (𝓝[s] b) (𝓤 α) := by simp [continuous_on, continuous_within_at_iff'_right] theorem continuous_on_iff'_left [topological_space β] {f : β → α} {s : set β} : continuous_on f s ↔ ∀ b ∈ s, tendsto (λ x, (f x, f b)) (𝓝[s] b) (𝓤 α) := by simp [continuous_on, continuous_within_at_iff'_left] theorem continuous_iff'_right [topological_space β] {f : β → α} : continuous f ↔ ∀ b, tendsto (λ x, (f b, f x)) (𝓝 b) (𝓤 α) := continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_right theorem continuous_iff'_left [topological_space β] {f : β → α} : continuous f ↔ ∀ b, tendsto (λ x, (f x, f b)) (𝓝 b) (𝓤 α) := continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_left end uniform lemma filter.tendsto.congr_uniformity {α β} [uniform_space β] {f g : α → β} {l : filter α} {b : β} (hf : tendsto f l (𝓝 b)) (hg : tendsto (λ x, (f x, g x)) l (𝓤 β)) : tendsto g l (𝓝 b) := uniform.tendsto_nhds_right.2 $ (uniform.tendsto_nhds_right.1 hf).uniformity_trans hg lemma uniform.tendsto_congr {α β} [uniform_space β] {f g : α → β} {l : filter α} {b : β} (hfg : tendsto (λ x, (f x, g x)) l (𝓤 β)) : tendsto f l (𝓝 b) ↔ tendsto g l (𝓝 b) := ⟨λ h, h.congr_uniformity hfg, λ h, h.congr_uniformity hfg.uniformity_symm⟩
3f55287f6c054a28531a62450d98cc9563099336
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/category/Module/subobject.lean
5999a7b57538e53d2b2aef9f002161c3ed5c6f4f
[ "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
2,171
lean
/- Copyright (c) 2021 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import algebra.category.Module.epi_mono import category_theory.subobject.well_powered /-! # Subobjects in the category of `R`-modules We construct an explicit order isomorphism between the categorical subobjects of an `R`-module `M` and its submodules. This immediately implies that the category of `R`-modules is well-powered. -/ open category_theory open category_theory.subobject open_locale Module universes v u namespace Module variables {R : Type u} [ring R] (M : Module.{v} R) /-- The categorical subobjects of a module `M` are in one-to-one correspondence with its submodules.-/ noncomputable def subobject_Module : subobject M ≃o submodule R M := order_iso.symm ({ inv_fun := λ S, S.arrow.range, to_fun := λ N, subobject.mk ↾N.subtype, right_inv := λ S, eq.symm begin fapply eq_mk_of_comm, { apply linear_equiv.to_Module_iso'_left, apply linear_equiv.of_bijective (linear_map.cod_restrict S.arrow.range S.arrow _), { simpa only [linear_map.ker_cod_restrict] using ker_eq_bot_of_mono _ }, { rw [linear_map.range_cod_restrict, submodule.comap_subtype_self] }, { exact linear_map.mem_range_self _ } }, { ext, refl } end, left_inv := λ N, begin convert congr_arg linear_map.range (underlying_iso_arrow ↾N.subtype) using 1, { have : (underlying_iso ↾N.subtype).inv = (underlying_iso ↾N.subtype).symm.to_linear_equiv, { ext, refl }, rw [this, comp_def, linear_equiv.range_comp] }, { exact (submodule.range_subtype _).symm } end, map_rel_iff' := λ S T, begin refine ⟨λ h, _, λ h, mk_le_mk_of_comm ↟(submodule.of_le h) (by { ext, refl })⟩, convert linear_map.range_comp_le_range (of_mk_le_mk _ _ h) ↾T.subtype, { simpa only [←comp_def, of_mk_le_mk_comp] using (submodule.range_subtype _).symm }, { exact (submodule.range_subtype _).symm } end }) instance well_powered_Module : well_powered (Module.{v} R) := ⟨λ M, ⟨⟨_, ⟨(subobject_Module M).to_equiv⟩⟩⟩⟩ end Module
009926570216f785c2aee89a25529a29b6f36712
3c693e12637d1cf47effc09ab5e21700d1278e73
/src/equivalence_relations/structure_examples.lean
0cf08d42987a3a451029f12c0ca83aba6d923ecf
[]
no_license
ImperialCollegeLondon/Example-Lean-Projects
e731664ae046980921a69ccfeb2286674080c5bb
87b27ba616eaf03f3642000829a481a1932dd08e
refs/heads/master
1,685,399,670,721
1,623,092,696,000
1,623,092,696,000
275,571,570
19
1
null
1,593,361,524,000
1,593,344,124,000
Lean
UTF-8
Lean
false
false
1,342
lean
import tactic -- what's a structure? structure group' (G : Type) [has_mul G] [has_one G] [has_inv G] := (mul_assoc : ∀ a b c : G, (a * b) * c = a * (b * c)) (one_mul : ∀ a : G, 1 * a = a) (mul_one : ∀ a : G, a * 1 = a) (mul_left_inv : ∀ a : G, a⁻¹ * a = 1) -- easy structure @[ext] structure silly := (sillyint : ℤ) (sillynat : ℕ) #print prefix silly /- silly.sillyint : silly → ℤ silly.sillynat : silly → ℕ silly.mk : ℤ → ℕ → silly silly.rec -- useful for induction silly.ext : ∀ (x y : silly), x.sillyint = y.sillyint → x.sillynat = y.sillynat → x = y silly.ext_iff : ∀ (x y : silly), x = y ↔ x.sillyint = y.sillyint ∧ x.sillynat = y.sillynat -/ -- #check silly -- silly : Type, i.e. silly is a Type -- how to make a term of type silly def x : silly := silly.mk 7 37 def y : silly := { sillyint := 0, sillynat := 1 } -- silly is an implementation of ℤ × ℕ -- ℤ × ℕ is another implentation of this example : x ≠ y := begin intro h, rw silly.ext_iff at h, cases h, simp [x,y] at *, linarith [x, y], end -- proof that silly is the same as ℤ × ℕ example : silly ≃ ℤ × ℕ := { to_fun := λ s, ⟨s.sillyint, s.sillynat⟩, inv_fun := λ x, {sillyint := x.1, sillynat := x.2}, left_inv := λ s, by {ext; refl}, right_inv := λ x, by {ext; refl} }
27123bf50727268b84a44400abb26cbe979d88bb
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/inductiveUnivErrorMsg.lean
6eb27f2c3f685959f81f16886f6be8fac96b05be
[ "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
145
lean
inductive Bar | foobar : Foo → Bar | somelist : List Bar → Bar inductive Bar2 | foobar : (x : Foo) → Bar2 | somelist : List Bar2 → Bar2
c829e8017666888c0d8dc893a7a1a6e54bcd366a
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/geometry/manifold/smooth_manifold_with_corners.lean
a257ef7545a57d94217dde4b2a5e95c50d8cdc4c
[ "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
38,960
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.times_cont_diff import geometry.manifold.charted_space /-! # Smooth manifolds (possibly with boundary or corners) A smooth manifold is a manifold modelled on a normed vector space, or a subset like a half-space (to get manifolds with boundaries) for which the changes of coordinates are smooth maps. We define a model with corners as a map `I : H → E` embedding nicely the topological space `H` in the vector space `E` (or more precisely as a structure containing all the relevant properties). Given such a model with corners `I` on `(E, H)`, we define the groupoid of local homeomorphisms of `H` which are smooth when read in `E` (for any regularity `n : with_top ℕ`). With this groupoid at hand and the general machinery of charted spaces, we thus get the notion of `C^n` manifold with respect to any model with corners `I` on `(E, H)`. We also introduce a specific type class for `C^∞` manifolds as these are the most commonly used. ## Main definitions * `model_with_corners 𝕜 E H` : a structure containing informations on the way a space `H` embeds in a model vector space E over the field `𝕜`. This is all that is needed to define a smooth manifold with model space `H`, and model vector space `E`. * `model_with_corners_self 𝕜 E` : trivial model with corners structure on the space `E` embedded in itself by the identity. * `times_cont_diff_groupoid n I` : when `I` is a model with corners on `(𝕜, E, H)`, this is the groupoid of local homeos of `H` which are of class `C^n` over the normed field `𝕜`, when read in `E`. * `smooth_manifold_with_corners I M` : a type class saying that the charted space `M`, modelled on the space `H`, has `C^∞` changes of coordinates with respect to the model with corners `I` on `(𝕜, E, H)`. This type class is just a shortcut for `has_groupoid M (times_cont_diff_groupoid ∞ I)`. * `ext_chart_at I x`: in a smooth manifold with corners with the model `I` on `(E, H)`, the charts take values in `H`, but often we may want to use their `E`-valued version, obtained by composing the charts with `I`. Since the target is in general not open, we can not register them as local homeomorphisms, but we register them as local equivs. `ext_chart_at I x` is the canonical such local equiv around `x`. As specific examples of models with corners, we define (in the file `real_instances.lean`) * `model_with_corners_self ℝ (euclidean_space (fin n))` for the model space used to define `n`-dimensional real manifolds without boundary (with notation `𝓡 n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_half_space n)` for the model space used to define `n`-dimensional real manifolds with boundary (with notation `𝓡∂ n` in the locale `manifold`) * `model_with_corners ℝ (euclidean_space (fin n)) (euclidean_quadrant n)` for the model space used to define `n`-dimensional real manifolds with corners With these definitions at hand, to invoke an `n`-dimensional real manifold without boundary, one could use `variables {n : ℕ} {M : Type*} [topological_space M] [charted_space (euclidean_space (fin n)) M] [smooth_manifold_with_corners (𝓡 n) M]`. However, this is not the recommended way: a theorem proved using this assumption would not apply for instance to the tangent space of such a manifold, which is modelled on `(euclidean_space (fin n)) × (euclidean_space (fin n))` and not on `euclidean_space (fin (2 * n))`! In the same way, it would not apply to product manifolds, modelled on `(euclidean_space (fin n)) × (euclidean_space (fin m))`. The right invocation does not focus on one specific construction, but on all constructions sharing the right properties, like `variables {E : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] {I : model_with_corners ℝ E E} [I.boundaryless] {M : Type*} [topological_space M] [charted_space E M] [smooth_manifold_with_corners I M]` Here, `I.boundaryless` is a typeclass property ensuring that there is no boundary (this is for instance the case for `model_with_corners_self`, or products of these). Note that one could consider as a natural assumption to only use the trivial model with corners `model_with_corners_self ℝ E`, but again in product manifolds the natural model with corners will not be this one but the product one (and they are not defeq as `(λp : E × F, (p.1, p.2))` is not defeq to the identity). So, it is important to use the above incantation to maximize the applicability of theorems. ## Implementation notes We want to talk about manifolds modelled on a vector space, but also on manifolds with boundary, modelled on a half space (or even manifolds with corners). For the latter examples, we still want to define smooth functions, tangent bundles, and so on. As smooth functions are well defined on vector spaces or subsets of these, one could take for model space a subtype of a vector space. With the drawback that the whole vector space itself (which is the most basic example) is not directly a subtype of itself: the inclusion of `univ : set E` in `set E` would show up in the definition, instead of `id`. A good abstraction covering both cases it to have a vector space `E` (with basic example the Euclidean space), a model space `H` (with basic example the upper half space), and an embedding of `H` into `E` (which can be the identity for `H = E`, or `subtype.val` for manifolds with corners). We say that the pair `(E, H)` with their embedding is a model with corners, and we encompass all the relevant properties (in particular the fact that the image of `H` in `E` should have unique differentials) in the definition of `model_with_corners`. We concentrate on `C^∞` manifolds: all the definitions work equally well for `C^n` manifolds, but later on it is a pain to carry all over the smoothness parameter, especially when one wants to deal with `C^k` functions as there would be additional conditions `k ≤ n` everywhere. Since one deals almost all the time with `C^∞` (or analytic) manifolds, this seems to be a reasonable choice that one could revisit later if needed. `C^k` manifolds are still available, but they should be called using `has_groupoid M (times_cont_diff_groupoid k I)` where `I` is the model with corners. I have considered using the model with corners `I` as a typeclass argument, possibly `out_param`, to get lighter notations later on, but it did not turn out right, as on `E × F` there are two natural model with corners, the trivial (identity) one, and the product one, and they are not defeq and one needs to indicate to Lean which one we want to use. This means that when talking on objects on manifolds one will most often need to specify the model with corners one is using. For instance, the tangent bundle will be `tangent_bundle I M` and the derivative will be `mfderiv I I' f`, instead of the more natural notations `tangent_bundle 𝕜 M` and `mfderiv 𝕜 f` (the field has to be explicit anyway, as some manifolds could be considered both as real and complex manifolds). -/ noncomputable theory universes u v w u' v' w' open set filter open_locale manifold filter topological_space localized "notation `∞` := (⊤ : with_top ℕ)" in manifold section model_with_corners /-! ### Models with corners. -/ /-- A structure containing informations on the way a space `H` embeds in a model vector space `E` over the field `𝕜`. This is all what is needed to define a smooth manifold with model space `H`, and model vector space `E`. -/ @[nolint has_inhabited_instance] structure model_with_corners (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] (H : Type*) [topological_space H] extends local_equiv H E := (source_eq : source = univ) (unique_diff' : unique_diff_on 𝕜 (range to_fun)) (continuous_to_fun : continuous to_fun . tactic.interactive.continuity') (continuous_inv_fun : continuous inv_fun . tactic.interactive.continuity') attribute [simp, mfld_simps] model_with_corners.source_eq /-- A vector space is a model with corners. -/ def model_with_corners_self (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] : model_with_corners 𝕜 E E := { to_fun := id, inv_fun := id, source := univ, target := univ, source_eq := rfl, map_source' := λ_ _, mem_univ _, map_target' := λ_ _, mem_univ _, left_inv' := λ_ _, rfl, right_inv' := λ_ _, rfl, unique_diff' := by { rw range_id, exact unique_diff_on_univ }, continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id } localized "notation `𝓘(` 𝕜 `, ` E `)` := model_with_corners_self 𝕜 E" in manifold localized "notation `𝓘(` 𝕜 `)` := model_with_corners_self 𝕜 𝕜" in manifold section variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) namespace model_with_corners instance : has_coe_to_fun (model_with_corners 𝕜 E H) := ⟨_, λ e, e.to_fun⟩ /-- The inverse to a model with corners, only registered as a local equiv. -/ protected def symm : local_equiv E H := I.to_local_equiv.symm /- Register a few lemmas to make sure that `simp` puts expressions in normal form -/ @[simp, mfld_simps] lemma to_local_equiv_coe : (I.to_local_equiv : H → E) = I := rfl @[simp, mfld_simps] lemma mk_coe (e : local_equiv H E) (a b c d) : ((model_with_corners.mk e a b c d : model_with_corners 𝕜 E H) : H → E) = (e : H → E) := rfl @[simp, mfld_simps] lemma to_local_equiv_coe_symm : (I.to_local_equiv.symm : E → H) = I.symm := rfl @[simp, mfld_simps] lemma mk_symm (e : local_equiv H E) (a b c d) : (model_with_corners.mk e a b c d : model_with_corners 𝕜 E H).symm = e.symm := rfl protected lemma unique_diff : unique_diff_on 𝕜 (range I) := I.unique_diff' @[continuity] protected lemma continuous : continuous I := I.continuous_to_fun protected lemma continuous_at {x} : continuous_at I x := I.continuous.continuous_at protected lemma continuous_within_at {s x} : continuous_within_at I s x := I.continuous_at.continuous_within_at @[continuity] lemma continuous_symm : continuous I.symm := I.continuous_inv_fun lemma continuous_at_symm {x} : continuous_at I.symm x := I.continuous_symm.continuous_at lemma continuous_within_at_symm {s x} : continuous_within_at I.symm s x := I.continuous_symm.continuous_within_at @[simp, mfld_simps] lemma target_eq : I.target = range (I : H → E) := by { rw [← image_univ, ← I.source_eq], exact (I.to_local_equiv.image_source_eq_target).symm } @[simp, mfld_simps] protected lemma left_inv (x : H) : I.symm (I x) = x := by { refine I.left_inv' _, simp } protected lemma left_inverse : function.left_inverse I.symm I := I.left_inv @[simp, mfld_simps] lemma symm_comp_self : I.symm ∘ I = id := I.left_inverse.comp_eq_id protected lemma right_inv_on : right_inv_on I.symm I (range I) := I.left_inverse.right_inv_on_range @[simp, mfld_simps] protected lemma right_inv {x : E} (hx : x ∈ range I) : I (I.symm x) = x := I.right_inv_on hx protected lemma image_eq (s : set H) : I '' s = I.symm ⁻¹' s ∩ range I := begin refine (I.to_local_equiv.image_eq_target_inter_inv_preimage _).trans _, { rw I.source_eq, exact subset_univ _ }, { rw [inter_comm, I.target_eq, I.to_local_equiv_coe_symm] } end protected lemma closed_embedding : closed_embedding I := I.left_inverse.closed_embedding I.continuous_symm I.continuous lemma closed_range : is_closed (range I) := I.closed_embedding.closed_range lemma map_nhds_eq (x : H) : map I (𝓝 x) = 𝓝[range I] (I x) := I.closed_embedding.to_embedding.map_nhds_eq x lemma image_mem_nhds_within {x : H} {s : set H} (hs : s ∈ 𝓝 x) : I '' s ∈ 𝓝[range I] (I x) := I.map_nhds_eq x ▸ image_mem_map hs lemma symm_map_nhds_within_range (x : H) : map I.symm (𝓝[range I] (I x)) = 𝓝 x := by rw [← I.map_nhds_eq, map_map, I.symm_comp_self, map_id] lemma unique_diff_preimage {s : set H} (hs : is_open s) : unique_diff_on 𝕜 (I.symm ⁻¹' s ∩ range I) := by { rw inter_comm, exact I.unique_diff.inter (hs.preimage I.continuous_inv_fun) } lemma unique_diff_preimage_source {β : Type*} [topological_space β] {e : local_homeomorph H β} : unique_diff_on 𝕜 (I.symm ⁻¹' (e.source) ∩ range I) := I.unique_diff_preimage e.open_source lemma unique_diff_at_image {x : H} : unique_diff_within_at 𝕜 (range I) (I x) := I.unique_diff _ (mem_range_self _) protected lemma locally_compact [locally_compact_space E] (I : model_with_corners 𝕜 E H) : locally_compact_space H := begin have : ∀ (x : H), (𝓝 x).has_basis (λ s, s ∈ 𝓝 (I x) ∧ is_compact s) (λ s, I.symm '' (s ∩ range ⇑I)), { intro x, rw ← I.symm_map_nhds_within_range, exact ((compact_basis_nhds (I x)).inf_principal _).map _ }, refine locally_compact_space_of_has_basis this _, rintro x s ⟨-, hsc⟩, exact (hsc.inter_right I.closed_range).image I.continuous_symm end end model_with_corners section variables (𝕜 E) /-- In the trivial model with corners, the associated local equiv is the identity. -/ @[simp, mfld_simps] lemma model_with_corners_self_local_equiv : (𝓘(𝕜, E)).to_local_equiv = local_equiv.refl E := rfl @[simp, mfld_simps] lemma model_with_corners_self_coe : (𝓘(𝕜, E) : E → E) = id := rfl @[simp, mfld_simps] lemma model_with_corners_self_coe_symm : (𝓘(𝕜, E).symm : E → E) = id := rfl end end section model_with_corners_prod /-- Given two model_with_corners `I` on `(E, H)` and `I'` on `(E', H')`, we define the model with corners `I.prod I'` on `(E × E', H × H')`. This appears in particular for the manifold structure on the tangent bundle to a manifold modelled on `(E, H)`: it will be modelled on `(E × E, H × E)`. -/ def model_with_corners.prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') : model_with_corners 𝕜 (E × E') (model_prod H H') := { to_fun := λ p, (I p.1, I' p.2), inv_fun := λ p, (I.symm p.1, I'.symm p.2), source := (univ : set (H × H')), target := set.prod (range I) (range I'), map_source' := λ ⟨x, x'⟩ _, by simp [-mem_range, mem_range_self], map_target' := λ ⟨x, x'⟩ _, mem_univ _, left_inv' := λ ⟨x, x'⟩ _, by simp, right_inv' := λ ⟨x, x'⟩ ⟨hx, hx'⟩, by simp [hx, hx'], source_eq := rfl, unique_diff' := begin have : range (λ(p : model_prod H H'), (I p.1, I' p.2)) = set.prod (range I) (range I'), by { dsimp [model_prod], rw ← prod_range_range_eq }, rw this, exact unique_diff_on.prod I.unique_diff I'.unique_diff, end, continuous_to_fun := (continuous.comp I.continuous_to_fun continuous_fst).prod_mk (continuous.comp I'.continuous_to_fun continuous_snd), continuous_inv_fun := (continuous.comp I.continuous_inv_fun continuous_fst).prod_mk (continuous.comp I'.continuous_inv_fun continuous_snd) } /-- Special case of product model with corners, which is trivial on the second factor. This shows up as the model to tangent bundles. -/ @[reducible] def model_with_corners.tangent {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) : model_with_corners 𝕜 (E × E) (model_prod H E) := I.prod (𝓘(𝕜, E)) variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {F : Type*} [normed_group F] [normed_space 𝕜 F] {F' : Type*} [normed_group F'] [normed_space 𝕜 F'] {H : Type*} [topological_space H] {H' : Type*} [topological_space H'] {G : Type*} [topological_space G] {G' : Type*} [topological_space G'] {I : model_with_corners 𝕜 E H} {J : model_with_corners 𝕜 F G} @[simp, mfld_simps] lemma model_with_corners_prod_to_local_equiv : (I.prod J).to_local_equiv = (I.to_local_equiv).prod (J.to_local_equiv) := begin ext1 x, { refl, }, { intro x, refl, }, { simp only [set.univ_prod_univ, model_with_corners.source_eq, local_equiv.prod_source], } end @[simp, mfld_simps] lemma model_with_corners_prod_coe (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : (I.prod I' : _ × _ → _ × _) = prod.map I I' := rfl @[simp, mfld_simps] lemma model_with_corners_prod_coe_symm (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') : ((I.prod I').symm : _ × _ → _ × _) = prod.map I.symm I'.symm := rfl end model_with_corners_prod section boundaryless /-- Property ensuring that the model with corners `I` defines manifolds without boundary. -/ class model_with_corners.boundaryless {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) : Prop := (range_eq_univ : range I = univ) /-- The trivial model with corners has no boundary -/ instance model_with_corners_self_boundaryless (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] : (model_with_corners_self 𝕜 E).boundaryless := ⟨by simp⟩ /-- If two model with corners are boundaryless, their product also is -/ instance model_with_corners.range_eq_univ_prod {𝕜 : Type u} [nondiscrete_normed_field 𝕜] {E : Type v} [normed_group E] [normed_space 𝕜 E] {H : Type w} [topological_space H] (I : model_with_corners 𝕜 E H) [I.boundaryless] {E' : Type v'} [normed_group E'] [normed_space 𝕜 E'] {H' : Type w'} [topological_space H'] (I' : model_with_corners 𝕜 E' H') [I'.boundaryless] : (I.prod I').boundaryless := begin split, dsimp [model_with_corners.prod, model_prod], rw [← prod_range_range_eq, model_with_corners.boundaryless.range_eq_univ, model_with_corners.boundaryless.range_eq_univ, univ_prod_univ] end end boundaryless section times_cont_diff_groupoid /-! ### Smooth functions on models with corners -/ variables {m n : with_top ℕ} {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] variable (n) /-- Given a model with corners `(E, H)`, we define the groupoid of `C^n` transformations of `H` as the maps that are `C^n` when read in `E` through `I`. -/ def times_cont_diff_groupoid : structure_groupoid H := pregroupoid.groupoid { property := λf s, times_cont_diff_on 𝕜 n (I ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I), comp := λf g u v hf hg hu hv huv, begin have : I ∘ (g ∘ f) ∘ I.symm = (I ∘ g ∘ I.symm) ∘ (I ∘ f ∘ I.symm), by { ext x, simp }, rw this, apply times_cont_diff_on.comp hg _, { rintros x ⟨hx1, hx2⟩, simp only with mfld_simps at ⊢ hx1, exact hx1.2 }, { refine hf.mono _, rintros x ⟨hx1, hx2⟩, exact ⟨hx1.1, hx2⟩ } end, id_mem := begin apply times_cont_diff_on.congr (times_cont_diff_id.times_cont_diff_on), rintros x ⟨hx1, hx2⟩, rcases mem_range.1 hx2 with ⟨y, hy⟩, rw ← hy, simp only with mfld_simps, end, locality := λf u hu H, begin apply times_cont_diff_on_of_locally_times_cont_diff_on, rintros y ⟨hy1, hy2⟩, rcases mem_range.1 hy2 with ⟨x, hx⟩, rw ← hx at ⊢ hy1, simp only with mfld_simps at ⊢ hy1, rcases H x hy1 with ⟨v, v_open, xv, hv⟩, have : ((I.symm ⁻¹' (u ∩ v)) ∩ (range I)) = ((I.symm ⁻¹' u) ∩ (range I) ∩ I.symm ⁻¹' v), { rw [preimage_inter, inter_assoc, inter_assoc], congr' 1, rw inter_comm }, rw this at hv, exact ⟨I.symm ⁻¹' v, v_open.preimage I.continuous_symm, by simpa, hv⟩ end, congr := λf g u hu fg hf, begin apply hf.congr, rintros y ⟨hy1, hy2⟩, rcases mem_range.1 hy2 with ⟨x, hx⟩, rw ← hx at ⊢ hy1, simp only with mfld_simps at ⊢ hy1, rw fg _ hy1 end } variable {n} /-- Inclusion of the groupoid of `C^n` local diffeos in the groupoid of `C^m` local diffeos when `m ≤ n` -/ lemma times_cont_diff_groupoid_le (h : m ≤ n) : times_cont_diff_groupoid n I ≤ times_cont_diff_groupoid m I := begin rw [times_cont_diff_groupoid, times_cont_diff_groupoid], apply groupoid_of_pregroupoid_le, assume f s hfs, exact times_cont_diff_on.of_le hfs h end /-- The groupoid of `0`-times continuously differentiable maps is just the groupoid of all local homeomorphisms -/ lemma times_cont_diff_groupoid_zero_eq : times_cont_diff_groupoid 0 I = continuous_groupoid H := begin apply le_antisymm le_top, assume u hu, -- we have to check that every local homeomorphism belongs to `times_cont_diff_groupoid 0 I`, -- by unfolding its definition change u ∈ times_cont_diff_groupoid 0 I, rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], simp only [times_cont_diff_on_zero], split, { apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous) _ (subset_univ _), apply continuous_on.comp u.continuous_to_fun I.continuous_symm.continuous_on (inter_subset_left _ _) }, { apply continuous_on.comp (@continuous.continuous_on _ _ _ _ _ univ I.continuous) _ (subset_univ _), apply continuous_on.comp u.continuous_inv_fun I.continuous_inv_fun.continuous_on (inter_subset_left _ _) }, end variable (n) /-- An identity local homeomorphism belongs to the `C^n` groupoid. -/ lemma of_set_mem_times_cont_diff_groupoid {s : set H} (hs : is_open s) : local_homeomorph.of_set s hs ∈ times_cont_diff_groupoid n I := begin rw [times_cont_diff_groupoid, mem_groupoid_of_pregroupoid], suffices h : times_cont_diff_on 𝕜 n (I ∘ I.symm) (I.symm ⁻¹' s ∩ range I), by simp [h], have : times_cont_diff_on 𝕜 n id (univ : set E) := times_cont_diff_id.times_cont_diff_on, exact this.congr_mono (λ x hx, by simp [hx.2]) (subset_univ _) end /-- The composition of a local homeomorphism from `H` to `M` and its inverse belongs to the `C^n` groupoid. -/ lemma symm_trans_mem_times_cont_diff_groupoid (e : local_homeomorph M H) : e.symm.trans e ∈ times_cont_diff_groupoid n I := begin have : e.symm.trans e ≈ local_homeomorph.of_set e.target e.open_target := local_homeomorph.trans_symm_self _, exact structure_groupoid.eq_on_source _ (of_set_mem_times_cont_diff_groupoid n I e.open_target) this end variables {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] /-- The product of two smooth local homeomorphisms is smooth. -/ lemma times_cont_diff_groupoid_prod {I : model_with_corners 𝕜 E H} {I' : model_with_corners 𝕜 E' H'} {e : local_homeomorph H H} {e' : local_homeomorph H' H'} (he : e ∈ times_cont_diff_groupoid ⊤ I) (he' : e' ∈ times_cont_diff_groupoid ⊤ I') : e.prod e' ∈ times_cont_diff_groupoid ⊤ (I.prod I') := begin cases he with he he_symm, cases he' with he' he'_symm, simp only at he he_symm he' he'_symm, split; simp only [local_equiv.prod_source, local_homeomorph.prod_to_local_equiv], { have h3 := times_cont_diff_on.prod_map he he', rw [← I.image_eq, ← I'.image_eq, set.prod_image_image_eq] at h3, rw ← (I.prod I').image_eq, exact h3, }, { have h3 := times_cont_diff_on.prod_map he_symm he'_symm, rw [← I.image_eq, ← I'.image_eq, set.prod_image_image_eq] at h3, rw ← (I.prod I').image_eq, exact h3, } end /-- The `C^n` groupoid is closed under restriction. -/ instance : closed_under_restriction (times_cont_diff_groupoid n I) := (closed_under_restriction_iff_id_le _).mpr begin apply structure_groupoid.le_iff.mpr, rintros e ⟨s, hs, hes⟩, apply (times_cont_diff_groupoid n I).eq_on_source' _ _ _ hes, exact of_set_mem_times_cont_diff_groupoid n I hs, end end times_cont_diff_groupoid end model_with_corners section smooth_manifold_with_corners /-! ### Smooth manifolds with corners -/ set_option old_structure_cmd true /-- Typeclass defining smooth manifolds with corners with respect to a model with corners, over a field `𝕜` and with infinite smoothness to simplify typeclass search and statements later on. -/ @[ancestor has_groupoid] class smooth_manifold_with_corners {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] extends has_groupoid M (times_cont_diff_groupoid ∞ I) : Prop lemma smooth_manifold_with_corners_of_times_cont_diff_on {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] (h : ∀ (e e' : local_homeomorph M H), e ∈ atlas H M → e' ∈ atlas H M → times_cont_diff_on 𝕜 ⊤ (I ∘ (e.symm ≫ₕ e') ∘ I.symm) (I.symm ⁻¹' (e.symm ≫ₕ e').source ∩ range I)) : smooth_manifold_with_corners I M := { compatible := begin haveI : has_groupoid M (times_cont_diff_groupoid ∞ I) := has_groupoid_of_pregroupoid _ h, apply structure_groupoid.compatible, end } /-- For any model with corners, the model space is a smooth manifold -/ instance model_space_smooth {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} : smooth_manifold_with_corners I H := { .. has_groupoid_model_space _ _ } end smooth_manifold_with_corners namespace smooth_manifold_with_corners /- We restate in the namespace `smooth_manifolds_with_corners` some lemmas that hold for general charted space with a structure groupoid, avoiding the need to specify the groupoid `times_cont_diff_groupoid ∞ I` explicitly. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) (M : Type*) [topological_space M] [charted_space H M] /-- The maximal atlas of `M` for the smooth manifold with corners structure corresponding to the model with corners `I`. -/ def maximal_atlas := (times_cont_diff_groupoid ∞ I).maximal_atlas M variable {M} lemma mem_maximal_atlas_of_mem_atlas [smooth_manifold_with_corners I M] {e : local_homeomorph M H} (he : e ∈ atlas H M) : e ∈ maximal_atlas I M := structure_groupoid.mem_maximal_atlas_of_mem_atlas _ he lemma chart_mem_maximal_atlas [smooth_manifold_with_corners I M] (x : M) : chart_at H x ∈ maximal_atlas I M := structure_groupoid.chart_mem_maximal_atlas _ x variable {I} lemma compatible_of_mem_maximal_atlas {e e' : local_homeomorph M H} (he : e ∈ maximal_atlas I M) (he' : e' ∈ maximal_atlas I M) : e.symm.trans e' ∈ times_cont_diff_groupoid ∞ I := structure_groupoid.compatible_of_mem_maximal_atlas he he' /-- The product of two smooth manifolds with corners is naturally a smooth manifold with corners. -/ instance prod {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H : Type*} [topological_space H] {I : model_with_corners 𝕜 E H} {H' : Type*} [topological_space H'] {I' : model_with_corners 𝕜 E' H'} (M : Type*) [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] (M' : Type*) [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] : smooth_manifold_with_corners (I.prod I') (M×M') := { compatible := begin rintros f g ⟨f1, hf1, f2, hf2, hf⟩ ⟨g1, hg1, g2, hg2, hg⟩, rw [hf, hg, local_homeomorph.prod_symm, local_homeomorph.prod_trans], have h1 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I) hf1 hg1, have h2 := has_groupoid.compatible (times_cont_diff_groupoid ⊤ I') hf2 hg2, exact times_cont_diff_groupoid_prod h1 h2, end } end smooth_manifold_with_corners section extended_charts open_locale topological_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] (x : M) {s t : set M} /-! ### Extended charts In a smooth manifold with corners, the model space is the space `H`. However, we will also need to use extended charts taking values in the model vector space `E`. These extended charts are not `local_homeomorph` as the target is not open in `E` in general, but we can still register them as `local_equiv`. -/ /-- The preferred extended chart on a manifold with corners around a point `x`, from a neighborhood of `x` to the model vector space. -/ @[simp, mfld_simps] def ext_chart_at (x : M) : local_equiv M E := (chart_at H x).to_local_equiv.trans I.to_local_equiv lemma ext_chart_at_coe : ⇑(ext_chart_at I x) = I ∘ chart_at H x := rfl lemma ext_chart_at_coe_symm : ⇑(ext_chart_at I x).symm = (chart_at H x).symm ∘ I.symm := rfl lemma ext_chart_at_source : (ext_chart_at I x).source = (chart_at H x).source := by rw [ext_chart_at, local_equiv.trans_source, I.source_eq, preimage_univ, inter_univ] lemma ext_chart_at_open_source : is_open (ext_chart_at I x).source := by { rw ext_chart_at_source, exact (chart_at H x).open_source } lemma mem_ext_chart_source : x ∈ (ext_chart_at I x).source := by simp only [ext_chart_at_source, mem_chart_source] lemma ext_chart_at_to_inv : (ext_chart_at I x).symm ((ext_chart_at I x) x) = x := (ext_chart_at I x).left_inv (mem_ext_chart_source I x) lemma ext_chart_at_source_mem_nhds' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : (ext_chart_at I x).source ∈ 𝓝 x' := mem_nhds_sets (ext_chart_at_open_source I x) h lemma ext_chart_at_source_mem_nhds : (ext_chart_at I x).source ∈ 𝓝 x := ext_chart_at_source_mem_nhds' I x (mem_ext_chart_source I x) lemma ext_chart_at_source_mem_nhds_within' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : (ext_chart_at I x).source ∈ 𝓝[s] x' := mem_nhds_within_of_mem_nhds (ext_chart_at_source_mem_nhds' I x h) lemma ext_chart_at_source_mem_nhds_within : (ext_chart_at I x).source ∈ 𝓝[s] x := mem_nhds_within_of_mem_nhds (ext_chart_at_source_mem_nhds I x) lemma ext_chart_at_continuous_on : continuous_on (ext_chart_at I x) (ext_chart_at I x).source := begin refine I.continuous.comp_continuous_on _, rw ext_chart_at_source, exact (chart_at H x).continuous_on end lemma ext_chart_at_continuous_at' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : continuous_at (ext_chart_at I x) x' := (ext_chart_at_continuous_on I x).continuous_at $ ext_chart_at_source_mem_nhds' I x h lemma ext_chart_at_continuous_at : continuous_at (ext_chart_at I x) x := ext_chart_at_continuous_at' _ _ (mem_ext_chart_source I x) lemma ext_chart_at_continuous_on_symm : continuous_on (ext_chart_at I x).symm (ext_chart_at I x).target := begin apply continuous_on.comp (chart_at H x).continuous_on_symm I.continuous_symm.continuous_on, simp [ext_chart_at, local_equiv.trans_target] end lemma ext_chart_at_map_nhds' {x y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x) (𝓝 y) = 𝓝[range I] (ext_chart_at I x y) := begin rw [ext_chart_at_coe, (∘), ← I.map_nhds_eq, ← (chart_at H x).map_nhds_eq, map_map], rwa ext_chart_at_source at hy end lemma ext_chart_at_map_nhds : map (ext_chart_at I x) (𝓝 x) = 𝓝[range I] (ext_chart_at I x x) := ext_chart_at_map_nhds' I $ mem_ext_chart_source I x lemma ext_chart_at_target_mem_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) : (ext_chart_at I x).target ∈ 𝓝[range I] (ext_chart_at I x y) := begin rw [← local_equiv.image_source_eq_target, ← ext_chart_at_map_nhds' I hy], exact image_mem_map (ext_chart_at_source_mem_nhds' _ _ hy) end lemma ext_chart_at_target_mem_nhds_within : (ext_chart_at I x).target ∈ 𝓝[range I] (ext_chart_at I x x) := ext_chart_at_target_mem_nhds_within' I x (mem_ext_chart_source I x) lemma ext_chart_at_target_subset_range : (ext_chart_at I x).target ⊆ range I := by simp only with mfld_simps lemma nhds_within_ext_chart_target_eq' {y : M} (hy : y ∈ (ext_chart_at I x).source) : 𝓝[(ext_chart_at I x).target] (ext_chart_at I x y) = 𝓝[range I] (ext_chart_at I x y) := (nhds_within_mono _ (ext_chart_at_target_subset_range _ _)).antisymm $ nhds_within_le_of_mem (ext_chart_at_target_mem_nhds_within' _ _ hy) lemma nhds_within_ext_chart_target_eq : 𝓝[(ext_chart_at I x).target] ((ext_chart_at I x) x) = 𝓝[range I] ((ext_chart_at I x) x) := nhds_within_ext_chart_target_eq' I x (mem_ext_chart_source I x) lemma ext_chart_continuous_at_symm'' {y : E} (h : y ∈ (ext_chart_at I x).target) : continuous_at (ext_chart_at I x).symm y := continuous_at.comp ((chart_at H x).continuous_at_symm h.2) (I.continuous_symm.continuous_at) lemma ext_chart_continuous_at_symm' {x' : M} (h : x' ∈ (ext_chart_at I x).source) : continuous_at (ext_chart_at I x).symm (ext_chart_at I x x') := ext_chart_continuous_at_symm'' I _ $ (ext_chart_at I x).map_source h lemma ext_chart_continuous_at_symm : continuous_at (ext_chart_at I x).symm ((ext_chart_at I x) x) := ext_chart_continuous_at_symm' I x (mem_ext_chart_source I x) lemma ext_chart_continuous_on_symm : continuous_on (ext_chart_at I x).symm (ext_chart_at I x).target := λ y hy, (ext_chart_continuous_at_symm'' _ _ hy).continuous_within_at lemma ext_chart_at_map_nhds_within_eq_image' {y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x) (𝓝[s] y) = 𝓝[ext_chart_at I x '' ((ext_chart_at I x).source ∩ s)] (ext_chart_at I x y) := by set e := ext_chart_at I x; calc map e (𝓝[s] y) = map e (𝓝[e.source ∩ s] y) : congr_arg (map e) (nhds_within_inter_of_mem (ext_chart_at_source_mem_nhds_within' I x hy)).symm ... = 𝓝[e '' (e.source ∩ s)] (e y) : ((ext_chart_at I x).left_inv_on.mono $ inter_subset_left _ _).map_nhds_within_eq ((ext_chart_at I x).left_inv hy) (ext_chart_continuous_at_symm' I x hy).continuous_within_at (ext_chart_at_continuous_at' I x hy).continuous_within_at lemma ext_chart_at_map_nhds_within_eq_image : map (ext_chart_at I x) (𝓝[s] x) = 𝓝[ext_chart_at I x '' ((ext_chart_at I x).source ∩ s)] (ext_chart_at I x x) := ext_chart_at_map_nhds_within_eq_image' I x (mem_ext_chart_source I x) lemma ext_chart_at_map_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x) (𝓝[s] y) = 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x y) := by rw [ext_chart_at_map_nhds_within_eq_image' I x hy, nhds_within_inter, ← nhds_within_ext_chart_target_eq' _ _ hy, ← nhds_within_inter, (ext_chart_at I x).image_source_inter_eq', inter_comm] lemma ext_chart_at_map_nhds_within : map (ext_chart_at I x) (𝓝[s] x) = 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x x) := ext_chart_at_map_nhds_within' I x (mem_ext_chart_source I x) lemma ext_chart_at_symm_map_nhds_within' {y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x).symm (𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x y)) = 𝓝[s] y := begin rw [← ext_chart_at_map_nhds_within' I x hy, map_map, map_congr, map_id], exact (ext_chart_at I x).left_inv_on.eq_on.eventually_eq_of_mem (ext_chart_at_source_mem_nhds_within' _ _ hy) end lemma ext_chart_at_symm_map_nhds_within_range' {y : M} (hy : y ∈ (ext_chart_at I x).source) : map (ext_chart_at I x).symm (𝓝[range I] (ext_chart_at I x y)) = 𝓝 y := by rw [← nhds_within_univ, ← ext_chart_at_symm_map_nhds_within' I x hy, preimage_univ, univ_inter] lemma ext_chart_at_symm_map_nhds_within : map (ext_chart_at I x).symm (𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] (ext_chart_at I x x)) = 𝓝[s] x := ext_chart_at_symm_map_nhds_within' I x (mem_ext_chart_source I x) lemma ext_chart_at_symm_map_nhds_within_range : map (ext_chart_at I x).symm (𝓝[range I] (ext_chart_at I x x)) = 𝓝 x := ext_chart_at_symm_map_nhds_within_range' I x (mem_ext_chart_source I x) /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point in the source is a neighborhood of the preimage, within a set. -/ lemma ext_chart_preimage_mem_nhds_within' {x' : M} (h : x' ∈ (ext_chart_at I x).source) (ht : t ∈ 𝓝[s] x') : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x') := by rwa [← ext_chart_at_symm_map_nhds_within' I x h, mem_map] at ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of the base point is a neighborhood of the preimage, within a set. -/ lemma ext_chart_preimage_mem_nhds_within (ht : t ∈ 𝓝[s] x) : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x) := ext_chart_preimage_mem_nhds_within' I x (mem_ext_chart_source I x) ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point is a neighborhood of the preimage. -/ lemma ext_chart_preimage_mem_nhds (ht : t ∈ 𝓝 x) : (ext_chart_at I x).symm ⁻¹' t ∈ 𝓝 ((ext_chart_at I x) x) := begin apply (ext_chart_continuous_at_symm I x).preimage_mem_nhds, rwa (ext_chart_at I x).left_inv (mem_ext_chart_source _ _) end /-- Technical lemma to rewrite suitably the preimage of an intersection under an extended chart, to bring it into a convenient form to apply derivative lemmas. -/ lemma ext_chart_preimage_inter_eq : ((ext_chart_at I x).symm ⁻¹' (s ∩ t) ∩ range I) = ((ext_chart_at I x).symm ⁻¹' s ∩ range I) ∩ ((ext_chart_at I x).symm ⁻¹' t) := by mfld_set_tac end extended_charts /-- In the case of the manifold structure on a vector space, the extended charts are just the identity.-/ lemma ext_chart_model_space_eq_id (𝕜 : Type*) [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] (x : E) : ext_chart_at (model_with_corners_self 𝕜 E) x = local_equiv.refl E := by simp only with mfld_simps
f5e438a8fd868162b4bc0aa1e415c0fcee74de20
aa5a655c05e5359a70646b7154e7cac59f0b4132
/src/Init/Data/Format.lean
7f56a4d18a78d8c2d8a70f4a4f858f800793522b
[ "Apache-2.0" ]
permissive
lambdaxymox/lean4
ae943c960a42247e06eff25c35338268d07454cb
278d47c77270664ef29715faab467feac8a0f446
refs/heads/master
1,677,891,867,340
1,612,500,005,000
1,612,500,005,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
265
lean
/- Copyright (c) 2018 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.Format.Basic import Init.Data.Format.Macro import Init.Data.Format.Instances
aacd75d3564d07511f7f6f1d77c87cd08ae1021a
46125763b4dbf50619e8846a1371029346f4c3db
/src/analysis/normed_space/bounded_linear_maps.lean
314c21737139a0223310ae79a289d659506e9de2
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
18,759
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl Continuous linear functions -- functions between normed vector spaces which are bounded and linear. -/ import algebra.field import analysis.normed_space.operator_norm analysis.normed_space.multilinear noncomputable theory open_locale classical filter open filter (tendsto) open metric variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_group F] [normed_space 𝕜 F] variables {G : Type*} [normed_group G] [normed_space 𝕜 G] set_option class.instance_max_depth 70 /-- A function `f` satisfies `is_bounded_linear_map 𝕜 f` if it is linear and satisfies the inequality `∥ f x ∥ ≤ M * ∥ x ∥` for some positive constant `M`. -/ structure is_bounded_linear_map (𝕜 : Type*) [normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] (f : E → F) extends is_linear_map 𝕜 f : Prop := (bound : ∃ M, 0 < M ∧ ∀ x : E, ∥ f x ∥ ≤ M * ∥ x ∥) lemma is_linear_map.with_bound {f : E → F} (hf : is_linear_map 𝕜 f) (M : ℝ) (h : ∀ x : E, ∥ f x ∥ ≤ M * ∥ x ∥) : is_bounded_linear_map 𝕜 f := ⟨ hf, classical.by_cases (assume : M ≤ 0, ⟨1, zero_lt_one, assume x, le_trans (h x) $ mul_le_mul_of_nonneg_right (le_trans this zero_le_one) (norm_nonneg x)⟩) (assume : ¬ M ≤ 0, ⟨M, lt_of_not_ge this, h⟩)⟩ /-- A continuous linear map satisfies `is_bounded_linear_map` -/ lemma continuous_linear_map.is_bounded_linear_map (f : E →L[𝕜] F) : is_bounded_linear_map 𝕜 f := { bound := f.bound, ..f.to_linear_map } namespace is_bounded_linear_map /-- Construct a linear map from a function `f` satisfying `is_bounded_linear_map 𝕜 f`. -/ def to_linear_map (f : E → F) (h : is_bounded_linear_map 𝕜 f) : E →ₗ[𝕜] F := (is_linear_map.mk' _ h.to_is_linear_map) /-- Construct a continuous linear map from is_bounded_linear_map -/ def to_continuous_linear_map {f : E → F} (hf : is_bounded_linear_map 𝕜 f) : E →L[𝕜] F := { cont := let ⟨C, Cpos, hC⟩ := hf.bound in linear_map.continuous_of_bound _ C hC, ..to_linear_map f hf} lemma zero : is_bounded_linear_map 𝕜 (λ (x:E), (0:F)) := (0 : E →ₗ F).is_linear.with_bound 0 $ by simp [le_refl] lemma id : is_bounded_linear_map 𝕜 (λ (x:E), x) := linear_map.id.is_linear.with_bound 1 $ by simp [le_refl] lemma fst : is_bounded_linear_map 𝕜 (λ x : E × F, x.1) := begin refine (linear_map.fst 𝕜 E F).is_linear.with_bound 1 (λx, _), rw one_mul, exact le_max_left _ _ end lemma snd : is_bounded_linear_map 𝕜 (λ x : E × F, x.2) := begin refine (linear_map.snd 𝕜 E F).is_linear.with_bound 1 (λx, _), rw one_mul, exact le_max_right _ _ end variables { f g : E → F } lemma smul (c : 𝕜) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, c • f e) := let ⟨hlf, M, hMp, hM⟩ := hf in (c • hlf.mk' f).is_linear.with_bound (∥c∥ * M) $ assume x, calc ∥c • f x∥ = ∥c∥ * ∥f x∥ : norm_smul c (f x) ... ≤ ∥c∥ * (M * ∥x∥) : mul_le_mul_of_nonneg_left (hM _) (norm_nonneg _) ... = (∥c∥ * M) * ∥x∥ : (mul_assoc _ _ _).symm lemma neg (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λ e, -f e) := begin rw show (λ e, -f e) = (λ e, (-1 : 𝕜) • f e), { funext, simp }, exact smul (-1) hf end lemma add (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e + g e) := let ⟨hlf, Mf, hMfp, hMf⟩ := hf in let ⟨hlg, Mg, hMgp, hMg⟩ := hg in (hlf.mk' _ + hlg.mk' _).is_linear.with_bound (Mf + Mg) $ assume x, calc ∥f x + g x∥ ≤ Mf * ∥x∥ + Mg * ∥x∥ : norm_add_le_of_le (hMf x) (hMg x) ... ≤ (Mf + Mg) * ∥x∥ : by rw add_mul lemma sub (hf : is_bounded_linear_map 𝕜 f) (hg : is_bounded_linear_map 𝕜 g) : is_bounded_linear_map 𝕜 (λ e, f e - g e) := add hf (neg hg) lemma comp {g : F → G} (hg : is_bounded_linear_map 𝕜 g) (hf : is_bounded_linear_map 𝕜 f) : is_bounded_linear_map 𝕜 (g ∘ f) := (hg.to_continuous_linear_map.comp hf.to_continuous_linear_map).is_bounded_linear_map lemma tendsto (x : E) (hf : is_bounded_linear_map 𝕜 f) : f →_{x} (f x) := let ⟨hf, M, hMp, hM⟩ := hf in tendsto_iff_norm_tendsto_zero.2 $ squeeze_zero (assume e, norm_nonneg _) (assume e, calc ∥f e - f x∥ = ∥hf.mk' f (e - x)∥ : by rw (hf.mk' _).map_sub e x; refl ... ≤ M * ∥e - x∥ : hM (e - x)) (suffices (λ (e : E), M * ∥e - x∥) →_{x} (M * 0), by simpa, tendsto_const_nhds.mul (lim_norm _)) lemma continuous (hf : is_bounded_linear_map 𝕜 f) : continuous f := continuous_iff_continuous_at.2 $ λ _, hf.tendsto _ lemma lim_zero_bounded_linear_map (hf : is_bounded_linear_map 𝕜 f) : (f →_{0} 0) := (hf.1.mk' _).map_zero ▸ continuous_iff_continuous_at.1 hf.continuous 0 section open asymptotics filter theorem is_O_id {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) : is_O f (λ x, x) l := let ⟨M, hMp, hM⟩ := h.bound in ⟨M, mem_sets_of_superset univ_mem_sets (λ x _, hM x)⟩ theorem is_O_comp {E : Type*} {g : F → G} (hg : is_bounded_linear_map 𝕜 g) {f : E → F} (l : filter E) : is_O (λ x', g (f x')) f l := (hg.is_O_id ⊤).comp_tendsto lattice.le_top theorem is_O_sub {f : E → F} (h : is_bounded_linear_map 𝕜 f) (l : filter E) (x : E) : is_O (λ x', f (x' - x)) (λ x', x' - x) l := is_O_comp h l end end is_bounded_linear_map section set_option class.instance_max_depth 240 variables {ι : Type*} [decidable_eq ι] [fintype ι] /-- Taking the cartesian product of two continuous linear maps is a bounded linear operation. -/ lemma is_bounded_linear_map_prod_iso : is_bounded_linear_map 𝕜 (λ(p : (E →L[𝕜] F) × (E →L[𝕜] G)), (p.1.prod p.2 : (E →L[𝕜] (F × G)))) := begin refine is_linear_map.with_bound ⟨λu v, rfl, λc u, rfl⟩ 1 (λp, _), simp only [norm, one_mul], refine continuous_linear_map.op_norm_le_bound _ (le_trans (norm_nonneg _) (le_max_left _ _)) (λu, _), simp only [norm, continuous_linear_map.prod, max_le_iff], split, { calc ∥p.1 u∥ ≤ ∥p.1∥ * ∥u∥ : continuous_linear_map.le_op_norm _ _ ... ≤ max (∥p.1∥) (∥p.2∥) * ∥u∥ : mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg _) }, { calc ∥p.2 u∥ ≤ ∥p.2∥ * ∥u∥ : continuous_linear_map.le_op_norm _ _ ... ≤ max (∥p.1∥) (∥p.2∥) * ∥u∥ : mul_le_mul_of_nonneg_right (le_max_right _ _) (norm_nonneg _) } end /-- Taking the cartesian product of two continuous multilinear maps is a bounded linear operation. -/ lemma is_bounded_linear_map_prod_multilinear {E : ι → Type*} [∀i, normed_group (E i)] [∀i, normed_space 𝕜 (E i)] : is_bounded_linear_map 𝕜 (λ p : (continuous_multilinear_map 𝕜 E F) × (continuous_multilinear_map 𝕜 E G), p.1.prod p.2) := { add := λ p₁ p₂, by { ext1 m, refl }, smul := λ c p, by { ext1 m, refl }, bound := ⟨1, zero_lt_one, λ p, begin rw one_mul, apply continuous_multilinear_map.op_norm_le_bound _ (norm_nonneg _) (λ m, _), rw [continuous_multilinear_map.prod_apply, norm_prod_le_iff], split, { exact le_trans (p.1.le_op_norm m) (mul_le_mul_of_nonneg_right (norm_fst_le p) (finset.prod_nonneg (λ i hi, norm_nonneg _))) }, { exact le_trans (p.2.le_op_norm m) (mul_le_mul_of_nonneg_right (norm_snd_le p) (finset.prod_nonneg (λ i hi, norm_nonneg _))) }, end⟩ } /-- Given a fixed continuous linear map `g`, associating to a continuous multilinear map `f` the continuous multilinear map `f (g m₁, ..., g mₙ)` is a bounded linear operation. -/ lemma is_bounded_linear_map_continuous_multilinear_map_comp_linear (g : G →L[𝕜] E) : is_bounded_linear_map 𝕜 (λ f : continuous_multilinear_map 𝕜 (λ (i : ι), E) F, f.comp_continuous_linear_map 𝕜 E g) := begin refine is_linear_map.with_bound ⟨λ f₁ f₂, by { ext m, refl }, λ c f, by { ext m, refl }⟩ (∥g∥ ^ (fintype.card ι)) (λ f, _), apply continuous_multilinear_map.op_norm_le_bound _ _ (λ m, _), { apply_rules [mul_nonneg', pow_nonneg, norm_nonneg, norm_nonneg] }, calc ∥f (g ∘ m)∥ ≤ ∥f∥ * finset.univ.prod (λ (i : ι), ∥g (m i)∥) : f.le_op_norm _ ... ≤ ∥f∥ * finset.univ.prod (λ (i : ι), ∥g∥ * ∥m i∥) : begin apply mul_le_mul_of_nonneg_left _ (norm_nonneg _), exact finset.prod_le_prod (λ i hi, norm_nonneg _) (λ i hi, g.le_op_norm _) end ... = ∥g∥ ^ fintype.card ι * ∥f∥ * finset.univ.prod (λ (i : ι), ∥m i∥) : by { simp [finset.prod_mul_distrib, finset.card_univ], ring } end end section bilinear_map variable (𝕜) /-- A map `f : E × F → G` satisfies `is_bounded_bilinear_map 𝕜 f` if it is bilinear and continuous. -/ structure is_bounded_bilinear_map (f : E × F → G) : Prop := (add_left : ∀(x₁ x₂ : E) (y : F), f (x₁ + x₂, y) = f (x₁, y) + f (x₂, y)) (smul_left : ∀(c : 𝕜) (x : E) (y : F), f (c • x, y) = c • f (x,y)) (add_right : ∀(x : E) (y₁ y₂ : F), f (x, y₁ + y₂) = f (x, y₁) + f (x, y₂)) (smul_right : ∀(c : 𝕜) (x : E) (y : F), f (x, c • y) = c • f (x,y)) (bound : ∃C>0, ∀(x : E) (y : F), ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥) variable {𝕜} variable {f : E × F → G} lemma is_bounded_bilinear_map.map_sub_left (h : is_bounded_bilinear_map 𝕜 f) {x y : E} {z : F} : f (x - y, z) = f (x, z) - f(y, z) := calc f (x - y, z) = f (x + (-1 : 𝕜) • y, z) : by simp [sub_eq_add_neg] ... = f (x, z) + (-1 : 𝕜) • f (y, z) : by simp only [h.add_left, h.smul_left] ... = f (x, z) - f (y, z) : by simp [sub_eq_add_neg] lemma is_bounded_bilinear_map.map_sub_right (h : is_bounded_bilinear_map 𝕜 f) {x : E} {y z : F} : f (x, y - z) = f (x, y) - f (x, z) := calc f (x, y - z) = f (x, y + (-1 : 𝕜) • z) : by simp [sub_eq_add_neg] ... = f (x, y) + (-1 : 𝕜) • f (x, z) : by simp only [h.add_right, h.smul_right] ... = f (x, y) - f (x, z) : by simp [sub_eq_add_neg] lemma is_bounded_bilinear_map.is_bounded_linear_map_left (h : is_bounded_bilinear_map 𝕜 f) (y : F) : is_bounded_linear_map 𝕜 (λ x, f (x, y)) := { add := λ x x', h.add_left _ _ _, smul := λ c x, h.smul_left _ _ _, bound := begin rcases h.bound with ⟨C, C_pos, hC⟩, refine ⟨C * (∥y∥ + 1), mul_pos' C_pos (lt_of_lt_of_le (zero_lt_one) (by simp)), λ x, _⟩, have : ∥y∥ ≤ ∥y∥ + 1, by simp [zero_le_one], calc ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥ : hC x y ... ≤ C * ∥x∥ * (∥y∥ + 1) : by apply_rules [norm_nonneg, mul_le_mul_of_nonneg_left, le_of_lt C_pos, mul_nonneg'] ... = C * (∥y∥ + 1) * ∥x∥ : by ring end } lemma is_bounded_bilinear_map.is_bounded_linear_map_right (h : is_bounded_bilinear_map 𝕜 f) (x : E) : is_bounded_linear_map 𝕜 (λ y, f (x, y)) := { add := λ y y', h.add_right _ _ _, smul := λ c y, h.smul_right _ _ _, bound := begin rcases h.bound with ⟨C, C_pos, hC⟩, refine ⟨C * (∥x∥ + 1), mul_pos' C_pos (lt_of_lt_of_le (zero_lt_one) (by simp)), λ y, _⟩, have : ∥x∥ ≤ ∥x∥ + 1, by simp [zero_le_one], calc ∥f (x, y)∥ ≤ C * ∥x∥ * ∥y∥ : hC x y ... ≤ C * (∥x∥ + 1) * ∥y∥ : by apply_rules [mul_le_mul_of_nonneg_right, norm_nonneg, mul_le_mul_of_nonneg_left, le_of_lt C_pos] end } lemma is_bounded_bilinear_map_smul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × E), p.1 • p.2) := { add_left := add_smul, smul_left := λc x y, by simp [smul_smul], add_right := smul_add, smul_right := λc x y, by simp [smul_smul, mul_comm], bound := ⟨1, zero_lt_one, λx y, by simp [norm_smul]⟩ } lemma is_bounded_bilinear_map_mul : is_bounded_bilinear_map 𝕜 (λ (p : 𝕜 × 𝕜), p.1 * p.2) := is_bounded_bilinear_map_smul lemma is_bounded_bilinear_map_comp : is_bounded_bilinear_map 𝕜 (λ(p : (E →L[𝕜] F) × (F →L[𝕜] G)), p.2.comp p.1) := { add_left := λx₁ x₂ y, begin ext z, change y (x₁ z + x₂ z) = y (x₁ z) + y (x₂ z), rw y.map_add end, smul_left := λc x y, begin ext z, change y (c • (x z)) = c • y (x z), rw continuous_linear_map.map_smul end, add_right := λx y₁ y₂, rfl, smul_right := λc x y, rfl, bound := ⟨1, zero_lt_one, λx y, calc ∥continuous_linear_map.comp ((x, y).snd) ((x, y).fst)∥ ≤ ∥y∥ * ∥x∥ : continuous_linear_map.op_norm_comp_le _ _ ... = 1 * ∥x∥ * ∥ y∥ : by ring ⟩ } lemma continuous_linear_map.is_bounded_linear_map_comp_left (g : continuous_linear_map 𝕜 F G) : is_bounded_linear_map 𝕜 (λ(f : E →L[𝕜] F), continuous_linear_map.comp g f) := is_bounded_bilinear_map_comp.is_bounded_linear_map_left _ lemma continuous_linear_map.is_bounded_linear_map_comp_right (f : continuous_linear_map 𝕜 E F) : is_bounded_linear_map 𝕜 (λ(g : F →L[𝕜] G), continuous_linear_map.comp g f) := is_bounded_bilinear_map_comp.is_bounded_linear_map_right _ lemma is_bounded_bilinear_map_apply : is_bounded_bilinear_map 𝕜 (λp : (E →L[𝕜] F) × E, p.1 p.2) := { add_left := by simp, smul_left := by simp, add_right := by simp, smul_right := by simp, bound := ⟨1, zero_lt_one, by simp [continuous_linear_map.le_op_norm]⟩ } /-- The function `continuous_linear_map.smul_right`, associating to a continuous linear map `f : E → 𝕜` and a scalar `c : F` the tensor product `f ⊗ c` as a continuous linear map from `E` to `F`, is a bounded bilinear map. -/ lemma is_bounded_bilinear_map_smul_right : is_bounded_bilinear_map 𝕜 (λp, (continuous_linear_map.smul_right : (E →L[𝕜] 𝕜) → F → (E →L[𝕜] F)) p.1 p.2) := { add_left := λm₁ m₂ f, by { ext z, simp [add_smul] }, smul_left := λc m f, by { ext z, simp [mul_smul] }, add_right := λm f₁ f₂, by { ext z, simp [smul_add] }, smul_right := λc m f, by { ext z, simp [smul_smul, mul_comm] }, bound := ⟨1, zero_lt_one, λm f, by simp⟩ } /-- The composition of a continuous linear map with a continuous multilinear map is a bounded bilinear operation. -/ lemma is_bounded_bilinear_map_comp_multilinear {ι : Type*} {E : ι → Type*} [decidable_eq ι] [fintype ι] [∀i, normed_group (E i)] [∀i, normed_space 𝕜 (E i)] : is_bounded_bilinear_map 𝕜 (λ p : (F →L[𝕜] G) × (continuous_multilinear_map 𝕜 E F), p.1.comp_continuous_multilinear_map p.2) := { add_left := λ g₁ g₂ f, by { ext m, refl }, smul_left := λ c g f, by { ext m, refl }, add_right := λ g f₁ f₂, by { ext m, simp }, smul_right := λ c g f, by { ext m, simp }, bound := ⟨1, zero_lt_one, λ g f, begin apply continuous_multilinear_map.op_norm_le_bound _ _ (λm, _), { apply_rules [mul_nonneg, zero_le_one, norm_nonneg, norm_nonneg] }, calc ∥g (f m)∥ ≤ ∥g∥ * ∥f m∥ : g.le_op_norm _ ... ≤ ∥g∥ * (∥f∥ * finset.univ.prod (λ (i : ι), ∥m i∥)) : mul_le_mul_of_nonneg_left (f.le_op_norm _) (norm_nonneg _) ... = 1 * ∥g∥ * ∥f∥ * finset.univ.prod (λ (i : ι), ∥m i∥) : by ring end⟩ } /-- Definition of the derivative of a bilinear map `f`, given at a point `p` by `q ↦ f(p.1, q.2) + f(q.1, p.2)` as in the standard formula for the derivative of a product. We define this function here a bounded linear map from `E × F` to `G`. The fact that this is indeed the derivative of `f` is proved in `is_bounded_bilinear_map.has_fderiv_at` in `fderiv.lean`-/ def is_bounded_bilinear_map.linear_deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : (E × F) →ₗ[𝕜] G := { to_fun := λq, f (p.1, q.2) + f (q.1, p.2), add := λq₁ q₂, begin change f (p.1, q₁.2 + q₂.2) + f (q₁.1 + q₂.1, p.2) = f (p.1, q₁.2) + f (q₁.1, p.2) + (f (p.1, q₂.2) + f (q₂.1, p.2)), simp [h.add_left, h.add_right], abel end, smul := λc q, begin change f (p.1, c • q.2) + f (c • q.1, p.2) = c • (f (p.1, q.2) + f (q.1, p.2)), simp [h.smul_left, h.smul_right, smul_add] end } /-- The derivative of a bounded bilinear map at a point `p : E × F`, as a continuous linear map from `E × F` to `G`. -/ def is_bounded_bilinear_map.deriv (h : is_bounded_bilinear_map 𝕜 f) (p : E × F) : (E × F) →L[𝕜] G := (h.linear_deriv p).mk_continuous_of_exists_bound $ begin rcases h.bound with ⟨C, Cpos, hC⟩, refine ⟨C * ∥p.1∥ + C * ∥p.2∥, λq, _⟩, calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p.1∥ * ∥q∥ + C * ∥q∥ * ∥p.2∥ : begin apply add_le_add, exact mul_le_mul_of_nonneg_left (le_max_right _ _) (mul_nonneg (le_of_lt Cpos) (norm_nonneg _)), apply mul_le_mul_of_nonneg_right _ (norm_nonneg _), exact mul_le_mul_of_nonneg_left (le_max_left _ _) (le_of_lt Cpos), end ... = (C * ∥p.1∥ + C * ∥p.2∥) * ∥q∥ : by ring end @[simp] lemma is_bounded_bilinear_map_deriv_coe (h : is_bounded_bilinear_map 𝕜 f) (p q : E × F) : h.deriv p q = f (p.1, q.2) + f (q.1, p.2) := rfl set_option class.instance_max_depth 100 /-- Given a bounded bilinear map `f`, the map associating to a point `p` the derivative of `f` at `p` is itself a bounded linear map. -/ lemma is_bounded_bilinear_map.is_bounded_linear_map_deriv (h : is_bounded_bilinear_map 𝕜 f) : is_bounded_linear_map 𝕜 (λp : E × F, h.deriv p) := begin rcases h.bound with ⟨C, Cpos, hC⟩, refine is_linear_map.with_bound ⟨λp₁ p₂, _, λc p, _⟩ (C + C) (λp, _), { ext q, simp [h.add_left, h.add_right], abel }, { ext q, simp [h.smul_left, h.smul_right, smul_add] }, { refine continuous_linear_map.op_norm_le_bound _ (mul_nonneg (add_nonneg (le_of_lt Cpos) (le_of_lt Cpos)) (norm_nonneg _)) (λq, _), calc ∥f (p.1, q.2) + f (q.1, p.2)∥ ≤ C * ∥p.1∥ * ∥q.2∥ + C * ∥q.1∥ * ∥p.2∥ : norm_add_le_of_le (hC _ _) (hC _ _) ... ≤ C * ∥p∥ * ∥q∥ + C * ∥q∥ * ∥p∥ : by apply_rules [add_le_add, mul_le_mul, norm_nonneg, le_of_lt Cpos, le_refl, le_max_left, le_max_right, mul_nonneg, norm_nonneg, norm_nonneg, norm_nonneg] ... = (C + C) * ∥p∥ * ∥q∥ : by ring }, end end bilinear_map
fbc9c498b2f0e7af5e43696c96a66135b177d65e
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/category_theory/concrete_category/bundled.lean
6969fa8ee7cddce31fd16413bab26886263b86e3
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
1,386
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl, Reid Barton, Sean Leather Bundled types. -/ /-! `bundled c` provides a uniform structure for bundling a type equipped with a type class. We provide `category` instances for these in `unbundled_hom.lean` (for categories with unbundled homs, e.g. topological spaces) and in `bundled_hom.lean` (for categories with bundled homs, e.g. monoids). -/ universes u v namespace category_theory variables {c d : Type u → Type v} {α : Type u} /-- `bundled` is a type bundled with a type class instance for that type. Only the type class is exposed as a parameter. -/ structure bundled (c : Type u → Type v) : Type (max (u+1) v) := (α : Type u) (str : c α . tactic.apply_instance) namespace bundled /-- A generic function for lifting a type equipped with an instance to a bundled object. -/ -- Usually explicit instances will provide their own version of this, e.g. `Mon.of` and `Top.of`. def of {c : Type u → Type v} (α : Type u) [str : c α] : bundled c := ⟨α, str⟩ instance : has_coe_to_sort (bundled c) := { S := Type u, coe := bundled.α } /-- Map over the bundled structure -/ def map (f : Π {α}, c α → d α) (b : bundled c) : bundled d := ⟨b.α, f b.str⟩ end bundled end category_theory
1bbc3a5c2f5eef81610592bb57c437a2adf04344
6f1049e897f569e5c47237de40321e62f0181948
/src/solutions/04_exists.lean
590c63b025961489df15703c7f244edbb77d5487
[ "Apache-2.0" ]
permissive
anrddh/tutorials
f654a0807b9523608544836d9a81939f8e1dceb8
3ba43804e7b632201c494cdaa8da5406f1a255f9
refs/heads/master
1,655,542,921,827
1,588,846,595,000
1,588,846,595,000
262,330,134
0
0
null
1,588,944,346,000
1,588,944,345,000
null
UTF-8
Lean
false
false
3,017
lean
import data.real.basic import data.int.parity /- In this file, we learn how to handle the ∃ quantifier. In order to prove `∃ x, P x`, we give some x₀ using tactic `use x₀` and then prove `P x₀`. This x₀ can be an object from the local context or a more complicated expression. -/ example : ∃ n : ℕ, 8 = 2*n := begin use 4, refl, -- this is the tactic analogue of the rfl proof term end /- In order to use `h : ∃ x, P x`, we use the cases tactic to fix one x₀ that works. Again h can come straight from the local context or can be a more complicated expression. -/ example (n : ℕ) (h : ∃ k : ℕ, n = k + 1) : n > 0 := begin -- Let's fix k₀ such that n = k₀ + 1. cases h with k₀ hk₀, -- It now suffices to prove k₀ + 1 > 0. rw hk₀, -- and we have a lemma about this exact nat.succ_pos k₀, end /- The next exercises use divisibility in ℤ (beware the ∣ symbol which is not ASCII). By definition, a ∣ b ↔ ∃ k, b = a*k, so you can prove a ∣ b using the use tactic. -/ -- Until the end of this file, a, b and c will denote integers, unless -- explicitly stated otherwise variables (a b c : ℤ) -- 0029 example (h₁ : a ∣ b) (h₂ : b ∣ c) : a ∣ c := begin -- sorry cases h₁ with k hk, cases h₂ with l hl, use k*l, calc c = b*l : hl ... = (a*k)*l : by rw hk ... = a*(k*l) : by ring, -- sorry end /- A very common pattern is to have an assumption or lemma asserting h : ∃ x, y = ... and this is used through the combo: cases h with x hx, rw hx at ... The tactic rcases both allows to do recursive cases, as indicated by its name, but also simplifies the above combo when the name hx is replaced by the special name rfl, as in the following example. It uses the anonymous constructor angle brackets syntax. -/ example (h1 : a ∣ b) (h2 : a ∣ c) : a ∣ b+c := begin rcases h1 with ⟨k, rfl⟩, rcases h2 with ⟨l, rfl⟩, use k+l, ring, end /- You can use the same rfl trick with the `rintros` tactic. -/ example : a ∣ b → a ∣ c → a ∣ b+c := begin rintros ⟨k, rfl⟩ ⟨l, rfl⟩, use k+l, ring, end -- 0030 example : 0 ∣ a ↔ a = 0 := begin -- sorry split, { rintro ⟨k, rfl⟩, ring, }, { rintro rfl, use 0, refl, }, -- sorry end /- We can nom start combining quantifiers, using the definition surjective (f : X → Y) := ∀ y, ∃ x, f x = y -/ open function -- In the remaining of this file, f and g will denote functions from -- ℝ to ℝ. variables (f g : ℝ → ℝ) -- 0031 example (h : surjective (g ∘ f)) : surjective g := begin -- sorry intro y, rcases h y with ⟨w, rfl⟩, use f w, -- sorry end /- This above exercise can be done in three lines. Try again with the next exercise in four lines. -/ -- 0032 example (hf : surjective f) (hg : surjective g) : surjective (g ∘ f) := begin -- sorry intro z, rcases hg z with ⟨y, rfl⟩, rcases hf y with ⟨x, rfl⟩, use x, -- sorry end
3db47cb5314e4abd242a0e28fd49b85df53294e0
bb31430994044506fa42fd667e2d556327e18dfe
/src/order/liminf_limsup.lean
8540b8d61e19fae74a113c7d6fec18385988dd3a
[ "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
45,239
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Johannes Hölzl, Rémy Degenne -/ import order.filter.cofinite import order.hom.complete_lattice /-! # liminfs and limsups of functions and filters Defines the Liminf/Limsup of a function taking values in a conditionally complete lattice, with respect to an arbitrary filter. We define `Limsup f` (`Liminf f`) where `f` is a filter taking values in a conditionally complete lattice. `Limsup f` is the smallest element `a` such that, eventually, `u ≤ a` (and vice versa for `Liminf f`). To work with the Limsup along a function `u` use `Limsup (map u f)`. Usually, one defines the Limsup as `Inf (Sup s)` where the Inf is taken over all sets in the filter. For instance, in ℕ along a function `u`, this is `Inf_n (Sup_{k ≥ n} u k)` (and the latter quantity decreases with `n`, so this is in fact a limit.). There is however a difficulty: it is well possible that `u` is not bounded on the whole space, only eventually (think of `Limsup (λx, 1/x)` on ℝ. Then there is no guarantee that the quantity above really decreases (the value of the `Sup` beforehand is not really well defined, as one can not use ∞), so that the Inf could be anything. So one can not use this `Inf Sup ...` definition in conditionally complete lattices, and one has to use a less tractable definition. In conditionally complete lattices, the definition is only useful for filters which are eventually bounded above (otherwise, the Limsup would morally be +∞, which does not belong to the space) and which are frequently bounded below (otherwise, the Limsup would morally be -∞, which is not in the space either). We start with definitions of these concepts for arbitrary filters, before turning to the definitions of Limsup and Liminf. In complete lattices, however, it coincides with the `Inf Sup` definition. -/ open filter set open_locale filter variables {α β γ ι : Type*} namespace filter section relation /-- `f.is_bounded (≺)`: the filter `f` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. `r` will be usually instantiated with `≤` or `≥`. -/ def is_bounded (r : α → α → Prop) (f : filter α) := ∃ b, ∀ᶠ x in f, r x b /-- `f.is_bounded_under (≺) u`: the image of the filter `f` under `u` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. -/ def is_bounded_under (r : α → α → Prop) (f : filter β) (u : β → α) := (map u f).is_bounded r variables {r : α → α → Prop} {f g : filter α} /-- `f` is eventually bounded if and only if, there exists an admissible set on which it is bounded. -/ lemma is_bounded_iff : f.is_bounded r ↔ (∃s∈f.sets, ∃b, s ⊆ {x | r x b}) := iff.intro (assume ⟨b, hb⟩, ⟨{a | r a b}, hb, b, subset.refl _⟩) (assume ⟨s, hs, b, hb⟩, ⟨b, mem_of_superset hs hb⟩) /-- A bounded function `u` is in particular eventually bounded. -/ lemma is_bounded_under_of {f : filter β} {u : β → α} : (∃b, ∀x, r (u x) b) → f.is_bounded_under r u | ⟨b, hb⟩ := ⟨b, show ∀ᶠ x in f, r (u x) b, from eventually_of_forall hb⟩ lemma is_bounded_bot : is_bounded r ⊥ ↔ nonempty α := by simp [is_bounded, exists_true_iff_nonempty] lemma is_bounded_top : is_bounded r ⊤ ↔ (∃t, ∀x, r x t) := by simp [is_bounded, eq_univ_iff_forall] lemma is_bounded_principal (s : set α) : is_bounded r (𝓟 s) ↔ (∃t, ∀x∈s, r x t) := by simp [is_bounded, subset_def] lemma is_bounded_sup [is_trans α r] (hr : ∀b₁ b₂, ∃b, r b₁ b ∧ r b₂ b) : is_bounded r f → is_bounded r g → is_bounded r (f ⊔ g) | ⟨b₁, h₁⟩ ⟨b₂, h₂⟩ := let ⟨b, rb₁b, rb₂b⟩ := hr b₁ b₂ in ⟨b, eventually_sup.mpr ⟨h₁.mono (λ x h, trans h rb₁b), h₂.mono (λ x h, trans h rb₂b)⟩⟩ lemma is_bounded.mono (h : f ≤ g) : is_bounded r g → is_bounded r f | ⟨b, hb⟩ := ⟨b, h hb⟩ lemma is_bounded_under.mono {f g : filter β} {u : β → α} (h : f ≤ g) : g.is_bounded_under r u → f.is_bounded_under r u := λ hg, hg.mono (map_mono h) lemma is_bounded_under.mono_le [preorder β] {l : filter α} {u v : α → β} (hu : is_bounded_under (≤) l u) (hv : v ≤ᶠ[l] u) : is_bounded_under (≤) l v := hu.imp $ λ b hb, (eventually_map.1 hb).mp $ hv.mono $ λ x, le_trans lemma is_bounded_under.mono_ge [preorder β] {l : filter α} {u v : α → β} (hu : is_bounded_under (≥) l u) (hv : u ≤ᶠ[l] v) : is_bounded_under (≥) l v := @is_bounded_under.mono_le α βᵒᵈ _ _ _ _ hu hv lemma is_bounded_under_const [is_refl α r] {l : filter β} {a : α} : is_bounded_under r l (λ _, a) := ⟨a, eventually_map.2 $ eventually_of_forall $ λ _, refl _⟩ lemma is_bounded.is_bounded_under {q : β → β → Prop} {u : α → β} (hf : ∀a₀ a₁, r a₀ a₁ → q (u a₀) (u a₁)) : f.is_bounded r → f.is_bounded_under q u | ⟨b, h⟩ := ⟨u b, show ∀ᶠ x in f, q (u x) (u b), from h.mono (λ x, hf x b)⟩ lemma not_is_bounded_under_of_tendsto_at_top [preorder β] [no_max_order β] {f : α → β} {l : filter α} [l.ne_bot] (hf : tendsto f l at_top) : ¬ is_bounded_under (≤) l f := begin rintro ⟨b, hb⟩, rw eventually_map at hb, obtain ⟨b', h⟩ := exists_gt b, have hb' := (tendsto_at_top.mp hf) b', have : {x : α | f x ≤ b} ∩ {x : α | b' ≤ f x} = ∅ := eq_empty_of_subset_empty (λ x hx, (not_le_of_lt h) (le_trans hx.2 hx.1)), exact (nonempty_of_mem (hb.and hb')).ne_empty this end lemma not_is_bounded_under_of_tendsto_at_bot [preorder β] [no_min_order β] {f : α → β} {l : filter α} [l.ne_bot](hf : tendsto f l at_bot) : ¬ is_bounded_under (≥) l f := @not_is_bounded_under_of_tendsto_at_top α βᵒᵈ _ _ _ _ _ hf lemma is_bounded_under.bdd_above_range_of_cofinite [semilattice_sup β] {f : α → β} (hf : is_bounded_under (≤) cofinite f) : bdd_above (range f) := begin rcases hf with ⟨b, hb⟩, haveI : nonempty β := ⟨b⟩, rw [← image_univ, ← union_compl_self {x | f x ≤ b}, image_union, bdd_above_union], exact ⟨⟨b, ball_image_iff.2 $ λ x, id⟩, (hb.image f).bdd_above⟩ end lemma is_bounded_under.bdd_below_range_of_cofinite [semilattice_inf β] {f : α → β} (hf : is_bounded_under (≥) cofinite f) : bdd_below (range f) := @is_bounded_under.bdd_above_range_of_cofinite α βᵒᵈ _ _ hf lemma is_bounded_under.bdd_above_range [semilattice_sup β] {f : ℕ → β} (hf : is_bounded_under (≤) at_top f) : bdd_above (range f) := by { rw ← nat.cofinite_eq_at_top at hf, exact hf.bdd_above_range_of_cofinite } lemma is_bounded_under.bdd_below_range [semilattice_inf β] {f : ℕ → β} (hf : is_bounded_under (≥) at_top f) : bdd_below (range f) := @is_bounded_under.bdd_above_range βᵒᵈ _ _ hf /-- `is_cobounded (≺) f` states that the filter `f` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. There is a subtlety in this definition: we want `f.is_cobounded` to hold for any `f` in the case of complete lattices. This will be relevant to deduce theorems on complete lattices from their versions on conditionally complete lattices with additional assumptions. We have to be careful in the edge case of the trivial filter containing the empty set: the other natural definition `¬ ∀ a, ∀ᶠ n in f, a ≤ n` would not work as well in this case. -/ def is_cobounded (r : α → α → Prop) (f : filter α) := ∃b, ∀a, (∀ᶠ x in f, r x a) → r b a /-- `is_cobounded_under (≺) f u` states that the image of the filter `f` under the map `u` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. -/ def is_cobounded_under (r : α → α → Prop) (f : filter β) (u : β → α) := (map u f).is_cobounded r /-- To check that a filter is frequently bounded, it suffices to have a witness which bounds `f` at some point for every admissible set. This is only an implication, as the other direction is wrong for the trivial filter.-/ lemma is_cobounded.mk [is_trans α r] (a : α) (h : ∀s∈f, ∃x∈s, r a x) : f.is_cobounded r := ⟨a, assume y s, let ⟨x, h₁, h₂⟩ := h _ s in trans h₂ h₁⟩ /-- A filter which is eventually bounded is in particular frequently bounded (in the opposite direction). At least if the filter is not trivial. -/ lemma is_bounded.is_cobounded_flip [is_trans α r] [ne_bot f] : f.is_bounded r → f.is_cobounded (flip r) | ⟨a, ha⟩ := ⟨a, assume b hb, let ⟨x, rxa, rbx⟩ := (ha.and hb).exists in show r b a, from trans rbx rxa⟩ lemma is_bounded.is_cobounded_ge [preorder α] [ne_bot f] (h : f.is_bounded (≤)) : f.is_cobounded (≥) := h.is_cobounded_flip lemma is_bounded.is_cobounded_le [preorder α] [ne_bot f] (h : f.is_bounded (≥)) : f.is_cobounded (≤) := h.is_cobounded_flip lemma is_cobounded_bot : is_cobounded r ⊥ ↔ (∃b, ∀x, r b x) := by simp [is_cobounded] lemma is_cobounded_top : is_cobounded r ⊤ ↔ nonempty α := by simp [is_cobounded, eq_univ_iff_forall, exists_true_iff_nonempty] {contextual := tt} lemma is_cobounded_principal (s : set α) : (𝓟 s).is_cobounded r ↔ (∃b, ∀a, (∀x∈s, r x a) → r b a) := by simp [is_cobounded, subset_def] lemma is_cobounded.mono (h : f ≤ g) : f.is_cobounded r → g.is_cobounded r | ⟨b, hb⟩ := ⟨b, assume a ha, hb a (h ha)⟩ end relation lemma is_cobounded_le_of_bot [preorder α] [order_bot α] {f : filter α} : f.is_cobounded (≤) := ⟨⊥, assume a h, bot_le⟩ lemma is_cobounded_ge_of_top [preorder α] [order_top α] {f : filter α} : f.is_cobounded (≥) := ⟨⊤, assume a h, le_top⟩ lemma is_bounded_le_of_top [preorder α] [order_top α] {f : filter α} : f.is_bounded (≤) := ⟨⊤, eventually_of_forall $ λ _, le_top⟩ lemma is_bounded_ge_of_bot [preorder α] [order_bot α] {f : filter α} : f.is_bounded (≥) := ⟨⊥, eventually_of_forall $ λ _, bot_le⟩ @[simp] lemma _root_.order_iso.is_bounded_under_le_comp [preorder α] [preorder β] (e : α ≃o β) {l : filter γ} {u : γ → α} : is_bounded_under (≤) l (λ x, e (u x)) ↔ is_bounded_under (≤) l u := e.surjective.exists.trans $ exists_congr $ λ a, by simp only [eventually_map, e.le_iff_le] @[simp] lemma _root_.order_iso.is_bounded_under_ge_comp [preorder α] [preorder β] (e : α ≃o β) {l : filter γ} {u : γ → α} : is_bounded_under (≥) l (λ x, e (u x)) ↔ is_bounded_under (≥) l u := e.dual.is_bounded_under_le_comp @[simp, to_additive] lemma is_bounded_under_le_inv [ordered_comm_group α] {l : filter β} {u : β → α} : is_bounded_under (≤) l (λ x, (u x)⁻¹) ↔ is_bounded_under (≥) l u := (order_iso.inv α).is_bounded_under_ge_comp @[simp, to_additive] lemma is_bounded_under_ge_inv [ordered_comm_group α] {l : filter β} {u : β → α} : is_bounded_under (≥) l (λ x, (u x)⁻¹) ↔ is_bounded_under (≤) l u := (order_iso.inv α).is_bounded_under_le_comp lemma is_bounded_under.sup [semilattice_sup α] {f : filter β} {u v : β → α} : f.is_bounded_under (≤) u → f.is_bounded_under (≤) v → f.is_bounded_under (≤) (λa, u a ⊔ v a) | ⟨bu, (hu : ∀ᶠ x in f, u x ≤ bu)⟩ ⟨bv, (hv : ∀ᶠ x in f, v x ≤ bv)⟩ := ⟨bu ⊔ bv, show ∀ᶠ x in f, u x ⊔ v x ≤ bu ⊔ bv, by filter_upwards [hu, hv] with _ using sup_le_sup⟩ @[simp] lemma is_bounded_under_le_sup [semilattice_sup α] {f : filter β} {u v : β → α} : f.is_bounded_under (≤) (λ a, u a ⊔ v a) ↔ f.is_bounded_under (≤) u ∧ f.is_bounded_under (≤) v := ⟨λ h, ⟨h.mono_le $ eventually_of_forall $ λ _, le_sup_left, h.mono_le $ eventually_of_forall $ λ _, le_sup_right⟩, λ h, h.1.sup h.2⟩ lemma is_bounded_under.inf [semilattice_inf α] {f : filter β} {u v : β → α} : f.is_bounded_under (≥) u → f.is_bounded_under (≥) v → f.is_bounded_under (≥) (λa, u a ⊓ v a) := @is_bounded_under.sup αᵒᵈ β _ _ _ _ @[simp] lemma is_bounded_under_ge_inf [semilattice_inf α] {f : filter β} {u v : β → α} : f.is_bounded_under (≥) (λ a, u a ⊓ v a) ↔ f.is_bounded_under (≥) u ∧ f.is_bounded_under (≥) v := @is_bounded_under_le_sup αᵒᵈ _ _ _ _ _ lemma is_bounded_under_le_abs [linear_ordered_add_comm_group α] {f : filter β} {u : β → α} : f.is_bounded_under (≤) (λ a, |u a|) ↔ f.is_bounded_under (≤) u ∧ f.is_bounded_under (≥) u := is_bounded_under_le_sup.trans $ and_congr iff.rfl is_bounded_under_le_neg /-- Filters are automatically bounded or cobounded in complete lattices. To use the same statements in complete and conditionally complete lattices but let automation fill automatically the boundedness proofs in complete lattices, we use the tactic `is_bounded_default` in the statements, in the form `(hf : f.is_bounded (≥) . is_bounded_default)`. -/ meta def is_bounded_default : tactic unit := tactic.applyc ``is_cobounded_le_of_bot <|> tactic.applyc ``is_cobounded_ge_of_top <|> tactic.applyc ``is_bounded_le_of_top <|> tactic.applyc ``is_bounded_ge_of_bot section conditionally_complete_lattice variables [conditionally_complete_lattice α] /-- The `Limsup` of a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `x ≤ a`. -/ def Limsup (f : filter α) : α := Inf { a | ∀ᶠ n in f, n ≤ a } /-- The `Liminf` of a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `x ≥ a`. -/ def Liminf (f : filter α) : α := Sup { a | ∀ᶠ n in f, a ≤ n } /-- The `limsup` of a function `u` along a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `u x ≤ a`. -/ def limsup (u : β → α) (f : filter β) : α := Limsup (map u f) /-- The `liminf` of a function `u` along a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `u x ≥ a`. -/ def liminf (u : β → α) (f : filter β) : α := Liminf (map u f) /-- The `blimsup` of a function `u` along a filter `f`, bounded by a predicate `p`, is the infimum of the `a` such that, eventually for `f`, `u x ≤ a` whenever `p x` holds. -/ def blimsup (u : β → α) (f : filter β) (p : β → Prop) := Inf { a | ∀ᶠ x in f, p x → u x ≤ a } /-- The `bliminf` of a function `u` along a filter `f`, bounded by a predicate `p`, is the supremum of the `a` such that, eventually for `f`, `a ≤ u x` whenever `p x` holds. -/ def bliminf (u : β → α) (f : filter β) (p : β → Prop) := Sup { a | ∀ᶠ x in f, p x → a ≤ u x } section variables {f : filter β} {u : β → α} {p : β → Prop} theorem limsup_eq : limsup u f = Inf { a | ∀ᶠ n in f, u n ≤ a } := rfl theorem liminf_eq : liminf u f = Sup { a | ∀ᶠ n in f, a ≤ u n } := rfl theorem blimsup_eq : blimsup u f p = Inf { a | ∀ᶠ x in f, p x → u x ≤ a } := rfl theorem bliminf_eq : bliminf u f p = Sup { a | ∀ᶠ x in f, p x → a ≤ u x } := rfl end @[simp] lemma blimsup_true (f : filter β) (u : β → α) : blimsup u f (λ x, true) = limsup u f := by simp [blimsup_eq, limsup_eq] @[simp] lemma bliminf_true (f : filter β) (u : β → α) : bliminf u f (λ x, true) = liminf u f := by simp [bliminf_eq, liminf_eq] lemma blimsup_eq_limsup_subtype {f : filter β} {u : β → α} {p : β → Prop} : blimsup u f p = limsup (u ∘ (coe : {x | p x} → β)) (comap coe f) := begin simp only [blimsup_eq, limsup_eq, function.comp_app, eventually_comap, set_coe.forall, subtype.coe_mk, mem_set_of_eq], congr, ext a, exact eventually_congr (eventually_of_forall (λ x, ⟨λ hx y hy hxy, hxy.symm ▸ (hx (hxy ▸ hy)), λ hx hx', hx x hx' rfl⟩)), end lemma bliminf_eq_liminf_subtype {f : filter β} {u : β → α} {p : β → Prop} : bliminf u f p = liminf (u ∘ (coe : {x | p x} → β)) (comap coe f) := @blimsup_eq_limsup_subtype αᵒᵈ β _ f u p theorem Limsup_le_of_le {f : filter α} {a} (hf : f.is_cobounded (≤) . is_bounded_default) (h : ∀ᶠ n in f, n ≤ a) : Limsup f ≤ a := cInf_le hf h theorem le_Liminf_of_le {f : filter α} {a} (hf : f.is_cobounded (≥) . is_bounded_default) (h : ∀ᶠ n in f, a ≤ n) : a ≤ Liminf f := le_cSup hf h theorem limsup_le_of_le {f : filter β} {u : β → α} {a} (hf : f.is_cobounded_under (≤) u . is_bounded_default) (h : ∀ᶠ n in f, u n ≤ a) : limsup u f ≤ a := cInf_le hf h theorem le_liminf_of_le {f : filter β} {u : β → α} {a} (hf : f.is_cobounded_under (≥) u . is_bounded_default) (h : ∀ᶠ n in f, a ≤ u n) : a ≤ liminf u f := le_cSup hf h theorem le_Limsup_of_le {f : filter α} {a} (hf : f.is_bounded (≤) . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, n ≤ b) → a ≤ b) : a ≤ Limsup f := le_cInf hf h theorem Liminf_le_of_le {f : filter α} {a} (hf : f.is_bounded (≥) . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, b ≤ n) → b ≤ a) : Liminf f ≤ a := cSup_le hf h theorem le_limsup_of_le {f : filter β} {u : β → α} {a} (hf : f.is_bounded_under (≤) u . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, u n ≤ b) → a ≤ b) : a ≤ limsup u f := le_cInf hf h theorem liminf_le_of_le {f : filter β} {u : β → α} {a} (hf : f.is_bounded_under (≥) u . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, b ≤ u n) → b ≤ a) : liminf u f ≤ a := cSup_le hf h theorem Liminf_le_Limsup {f : filter α} [ne_bot f] (h₁ : f.is_bounded (≤) . is_bounded_default) (h₂ : f.is_bounded (≥) . is_bounded_default) : Liminf f ≤ Limsup f := Liminf_le_of_le h₂ $ assume a₀ ha₀, le_Limsup_of_le h₁ $ assume a₁ ha₁, show a₀ ≤ a₁, from let ⟨b, hb₀, hb₁⟩ := (ha₀.and ha₁).exists in le_trans hb₀ hb₁ lemma liminf_le_limsup {f : filter β} [ne_bot f] {u : β → α} (h : f.is_bounded_under (≤) u . is_bounded_default) (h' : f.is_bounded_under (≥) u . is_bounded_default) : liminf u f ≤ limsup u f := Liminf_le_Limsup h h' lemma Limsup_le_Limsup {f g : filter α} (hf : f.is_cobounded (≤) . is_bounded_default) (hg : g.is_bounded (≤) . is_bounded_default) (h : ∀ a, (∀ᶠ n in g, n ≤ a) → ∀ᶠ n in f, n ≤ a) : Limsup f ≤ Limsup g := cInf_le_cInf hf hg h lemma Liminf_le_Liminf {f g : filter α} (hf : f.is_bounded (≥) . is_bounded_default) (hg : g.is_cobounded (≥) . is_bounded_default) (h : ∀ a, (∀ᶠ n in f, a ≤ n) → ∀ᶠ n in g, a ≤ n) : Liminf f ≤ Liminf g := cSup_le_cSup hg hf h lemma limsup_le_limsup {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : u ≤ᶠ[f] v) (hu : f.is_cobounded_under (≤) u . is_bounded_default) (hv : f.is_bounded_under (≤) v . is_bounded_default) : limsup u f ≤ limsup v f := Limsup_le_Limsup hu hv $ assume b, h.trans lemma liminf_le_liminf {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a ≤ v a) (hu : f.is_bounded_under (≥) u . is_bounded_default) (hv : f.is_cobounded_under (≥) v . is_bounded_default) : liminf u f ≤ liminf v f := @limsup_le_limsup βᵒᵈ α _ _ _ _ h hv hu lemma Limsup_le_Limsup_of_le {f g : filter α} (h : f ≤ g) (hf : f.is_cobounded (≤) . is_bounded_default) (hg : g.is_bounded (≤) . is_bounded_default) : Limsup f ≤ Limsup g := Limsup_le_Limsup hf hg (assume a ha, h ha) lemma Liminf_le_Liminf_of_le {f g : filter α} (h : g ≤ f) (hf : f.is_bounded (≥) . is_bounded_default) (hg : g.is_cobounded (≥) . is_bounded_default) : Liminf f ≤ Liminf g := Liminf_le_Liminf hf hg (assume a ha, h ha) lemma limsup_le_limsup_of_le {α β} [conditionally_complete_lattice β] {f g : filter α} (h : f ≤ g) {u : α → β} (hf : f.is_cobounded_under (≤) u . is_bounded_default) (hg : g.is_bounded_under (≤) u . is_bounded_default) : limsup u f ≤ limsup u g := Limsup_le_Limsup_of_le (map_mono h) hf hg lemma liminf_le_liminf_of_le {α β} [conditionally_complete_lattice β] {f g : filter α} (h : g ≤ f) {u : α → β} (hf : f.is_bounded_under (≥) u . is_bounded_default) (hg : g.is_cobounded_under (≥) u . is_bounded_default) : liminf u f ≤ liminf u g := Liminf_le_Liminf_of_le (map_mono h) hf hg theorem Limsup_principal {s : set α} (h : bdd_above s) (hs : s.nonempty) : Limsup (𝓟 s) = Sup s := by simp [Limsup]; exact cInf_upper_bounds_eq_cSup h hs theorem Liminf_principal {s : set α} (h : bdd_below s) (hs : s.nonempty) : Liminf (𝓟 s) = Inf s := @Limsup_principal αᵒᵈ _ s h hs lemma limsup_congr {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : limsup u f = limsup v f := begin rw limsup_eq, congr' with b, exact eventually_congr (h.mono $ λ x hx, by simp [hx]) end lemma blimsup_congr {f : filter β} {u v : β → α} {p : β → Prop} (h : ∀ᶠ a in f, p a → u a = v a) : blimsup u f p = blimsup v f p := begin rw blimsup_eq, congr' with b, refine eventually_congr (h.mono $ λ x hx, ⟨λ h₁ h₂, _, λ h₁ h₂, _⟩), { rw ← hx h₂, exact h₁ h₂, }, { rw hx h₂, exact h₁ h₂, }, end lemma bliminf_congr {f : filter β} {u v : β → α} {p : β → Prop} (h : ∀ᶠ a in f, p a → u a = v a) : bliminf u f p = bliminf v f p := @blimsup_congr αᵒᵈ _ _ _ _ _ _ h lemma liminf_congr {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : liminf u f = liminf v f := @limsup_congr βᵒᵈ _ _ _ _ _ h lemma limsup_const {α : Type*} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : limsup (λ x, b) f = b := by simpa only [limsup_eq, eventually_const] using cInf_Ici lemma liminf_const {α : Type*} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : liminf (λ x, b) f = b := @limsup_const βᵒᵈ α _ f _ b end conditionally_complete_lattice section complete_lattice variables [complete_lattice α] @[simp] theorem Limsup_bot : Limsup (⊥ : filter α) = ⊥ := bot_unique $ Inf_le $ by simp @[simp] theorem Liminf_bot : Liminf (⊥ : filter α) = ⊤ := top_unique $ le_Sup $ by simp @[simp] theorem Limsup_top : Limsup (⊤ : filter α) = ⊤ := top_unique $ le_Inf $ by simp [eq_univ_iff_forall]; exact assume b hb, (top_unique $ hb _) @[simp] theorem Liminf_top : Liminf (⊤ : filter α) = ⊥ := bot_unique $ Sup_le $ by simp [eq_univ_iff_forall]; exact assume b hb, (bot_unique $ hb _) @[simp] lemma blimsup_false {f : filter β} {u : β → α} : blimsup u f (λ x, false) = ⊥ := by simp [blimsup_eq] @[simp] lemma bliminf_false {f : filter β} {u : β → α} : bliminf u f (λ x, false) = ⊤ := by simp [bliminf_eq] /-- Same as limsup_const applied to `⊥` but without the `ne_bot f` assumption -/ lemma limsup_const_bot {f : filter β} : limsup (λ x : β, (⊥ : α)) f = (⊥ : α) := begin rw [limsup_eq, eq_bot_iff], exact Inf_le (eventually_of_forall (λ x, le_rfl)), end /-- Same as limsup_const applied to `⊤` but without the `ne_bot f` assumption -/ lemma liminf_const_top {f : filter β} : liminf (λ x : β, (⊤ : α)) f = (⊤ : α) := @limsup_const_bot αᵒᵈ β _ _ theorem has_basis.Limsup_eq_infi_Sup {ι} {p : ι → Prop} {s} {f : filter α} (h : f.has_basis p s) : Limsup f = ⨅ i (hi : p i), Sup (s i) := le_antisymm (le_infi₂ $ λ i hi, Inf_le $ h.eventually_iff.2 ⟨i, hi, λ x, le_Sup⟩) (le_Inf $ assume a ha, let ⟨i, hi, ha⟩ := h.eventually_iff.1 ha in infi₂_le_of_le _ hi $ Sup_le ha) theorem has_basis.Liminf_eq_supr_Inf {p : ι → Prop} {s : ι → set α} {f : filter α} (h : f.has_basis p s) : Liminf f = ⨆ i (hi : p i), Inf (s i) := @has_basis.Limsup_eq_infi_Sup αᵒᵈ _ _ _ _ _ h theorem Limsup_eq_infi_Sup {f : filter α} : Limsup f = ⨅ s ∈ f, Sup s := f.basis_sets.Limsup_eq_infi_Sup theorem Liminf_eq_supr_Inf {f : filter α} : Liminf f = ⨆ s ∈ f, Inf s := @Limsup_eq_infi_Sup αᵒᵈ _ _ theorem limsup_le_supr {f : filter β} {u : β → α} : limsup u f ≤ ⨆ n, u n := limsup_le_of_le (by is_bounded_default) (eventually_of_forall (le_supr u)) theorem infi_le_liminf {f : filter β} {u : β → α} : (⨅ n, u n) ≤ liminf u f := le_liminf_of_le (by is_bounded_default) (eventually_of_forall (infi_le u)) /-- In a complete lattice, the limsup of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem limsup_eq_infi_supr {f : filter β} {u : β → α} : limsup u f = ⨅ s ∈ f, ⨆ a ∈ s, u a := (f.basis_sets.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, id] lemma limsup_eq_infi_supr_of_nat {u : ℕ → α} : limsup u at_top = ⨅ n : ℕ, ⨆ i ≥ n, u i := (at_top_basis.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, infi_const]; refl lemma limsup_eq_infi_supr_of_nat' {u : ℕ → α} : limsup u at_top = ⨅ n : ℕ, ⨆ i : ℕ, u (i + n) := by simp only [limsup_eq_infi_supr_of_nat, supr_ge_eq_supr_nat_add] theorem has_basis.limsup_eq_infi_supr {p : ι → Prop} {s : ι → set β} {f : filter β} {u : β → α} (h : f.has_basis p s) : limsup u f = ⨅ i (hi : p i), ⨆ a ∈ s i, u a := (h.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, id] lemma blimsup_congr' {f : filter β} {p q : β → Prop} {u : β → α} (h : ∀ᶠ x in f, u x ≠ ⊥ → (p x ↔ q x)) : blimsup u f p = blimsup u f q := begin simp only [blimsup_eq], congr, ext a, refine eventually_congr (h.mono $ λ b hb, _), cases eq_or_ne (u b) ⊥ with hu hu, { simp [hu], }, rw hb hu, end lemma bliminf_congr' {f : filter β} {p q : β → Prop} {u : β → α} (h : ∀ᶠ x in f, u x ≠ ⊤ → (p x ↔ q x)) : bliminf u f p = bliminf u f q := @blimsup_congr' αᵒᵈ β _ _ _ _ _ h lemma blimsup_eq_infi_bsupr {f : filter β} {p : β → Prop} {u : β → α} : blimsup u f p = ⨅ s ∈ f, ⨆ b (hb : p b ∧ b ∈ s), u b := begin refine le_antisymm (Inf_le_Inf _) (infi_le_iff.mpr $ λ a ha, le_Inf_iff.mpr $ λ a' ha', _), { rintros - ⟨s, rfl⟩, simp only [mem_set_of_eq, le_infi_iff], conv { congr, funext, rw imp.swap, }, refine eventually_imp_distrib_left.mpr (λ h, eventually_iff_exists_mem.2 ⟨s, h, λ x h₁ h₂, _⟩), exact @le_supr₂ α β (λ b, p b ∧ b ∈ s) _ (λ b hb, u b) x ⟨h₂, h₁⟩, }, { obtain ⟨s, hs, hs'⟩ := eventually_iff_exists_mem.mp ha', simp_rw imp.swap at hs', exact (le_infi_iff.mp (ha s) hs).trans (by simpa only [supr₂_le_iff, and_imp]), }, end lemma blimsup_eq_infi_bsupr_of_nat {p : ℕ → Prop} {u : ℕ → α} : blimsup u at_top p = ⨅ i, ⨆ j (hj : p j ∧ i ≤ j), u j := by simp only [blimsup_eq_limsup_subtype, mem_preimage, mem_Ici, function.comp_app, cinfi_pos, supr_subtype, (at_top_basis.comap (coe : {x | p x} → ℕ)).limsup_eq_infi_supr, mem_set_of_eq, subtype.coe_mk, supr_and] /-- In a complete lattice, the liminf of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem liminf_eq_supr_infi {f : filter β} {u : β → α} : liminf u f = ⨆ s ∈ f, ⨅ a ∈ s, u a := @limsup_eq_infi_supr αᵒᵈ β _ _ _ lemma liminf_eq_supr_infi_of_nat {u : ℕ → α} : liminf u at_top = ⨆ n : ℕ, ⨅ i ≥ n, u i := @limsup_eq_infi_supr_of_nat αᵒᵈ _ u lemma liminf_eq_supr_infi_of_nat' {u : ℕ → α} : liminf u at_top = ⨆ n : ℕ, ⨅ i : ℕ, u (i + n) := @limsup_eq_infi_supr_of_nat' αᵒᵈ _ _ theorem has_basis.liminf_eq_supr_infi {p : ι → Prop} {s : ι → set β} {f : filter β} {u : β → α} (h : f.has_basis p s) : liminf u f = ⨆ i (hi : p i), ⨅ a ∈ s i, u a := @has_basis.limsup_eq_infi_supr αᵒᵈ _ _ _ _ _ _ _ h lemma bliminf_eq_supr_binfi {f : filter β} {p : β → Prop} {u : β → α} : bliminf u f p = ⨆ s ∈ f, ⨅ b (hb : p b ∧ b ∈ s), u b := @blimsup_eq_infi_bsupr αᵒᵈ β _ f p u lemma bliminf_eq_supr_binfi_of_nat {p : ℕ → Prop} {u : ℕ → α} : bliminf u at_top p = ⨆ i, ⨅ j (hj : p j ∧ i ≤ j), u j := @blimsup_eq_infi_bsupr_of_nat αᵒᵈ _ p u lemma limsup_eq_Inf_Sup {ι R : Type*} (F : filter ι) [complete_lattice R] (a : ι → R) : limsup a F = Inf ((λ I, Sup (a '' I)) '' F.sets) := begin refine le_antisymm _ _, { rw limsup_eq, refine Inf_le_Inf (λ x hx, _), rcases (mem_image _ F.sets x).mp hx with ⟨I, ⟨I_mem_F, hI⟩⟩, filter_upwards [I_mem_F] with i hi, exact hI ▸ le_Sup (mem_image_of_mem _ hi), }, { refine le_Inf_iff.mpr (λ b hb, Inf_le_of_le (mem_image_of_mem _ $ filter.mem_sets.mpr hb) $ Sup_le _), rintros _ ⟨_, h, rfl⟩, exact h, }, end lemma liminf_eq_Sup_Inf {ι R : Type*} (F : filter ι) [complete_lattice R] (a : ι → R) : liminf a F = Sup ((λ I, Inf (a '' I)) '' F.sets) := @filter.limsup_eq_Inf_Sup ι (order_dual R) _ _ a @[simp] lemma liminf_nat_add (f : ℕ → α) (k : ℕ) : liminf (λ i, f (i + k)) at_top = liminf f at_top := by { simp_rw liminf_eq_supr_infi_of_nat, exact supr_infi_ge_nat_add f k } @[simp] lemma limsup_nat_add (f : ℕ → α) (k : ℕ) : limsup (λ i, f (i + k)) at_top = limsup f at_top := @liminf_nat_add αᵒᵈ _ f k lemma liminf_le_of_frequently_le' {α β} [complete_lattice β] {f : filter α} {u : α → β} {x : β} (h : ∃ᶠ a in f, u a ≤ x) : liminf u f ≤ x := begin rw liminf_eq, refine Sup_le (λ b hb, _), have hbx : ∃ᶠ a in f, b ≤ x, { revert h, rw [←not_imp_not, not_frequently, not_frequently], exact λ h, hb.mp (h.mono (λ a hbx hba hax, hbx (hba.trans hax))), }, exact hbx.exists.some_spec, end lemma le_limsup_of_frequently_le' {α β} [complete_lattice β] {f : filter α} {u : α → β} {x : β} (h : ∃ᶠ a in f, x ≤ u a) : x ≤ limsup u f := @liminf_le_of_frequently_le' _ βᵒᵈ _ _ _ _ h /-- If `f : α → α` is a morphism of complete lattices, then the limsup of its iterates of any `a : α` is a fixed point. -/ @[simp] lemma complete_lattice_hom.apply_limsup_iterate (f : complete_lattice_hom α α) (a : α) : f (limsup (λ n, f^[n] a) at_top) = limsup (λ n, f^[n] a) at_top := begin rw [limsup_eq_infi_supr_of_nat', map_infi], simp_rw [_root_.map_supr, ← function.comp_apply f, ← function.iterate_succ' f, ← nat.add_succ], conv_rhs { rw infi_split _ ((<) (0 : ℕ)), }, simp only [not_lt, le_zero_iff, infi_infi_eq_left, add_zero, infi_nat_gt_zero_eq, left_eq_inf], refine (infi_le (λ i, ⨆ j, (f^[j + (i + 1)]) a) 0).trans _, simp only [zero_add, function.comp_app, supr_le_iff], exact λ i, le_supr (λ i, (f^[i] a)) (i + 1), end /-- If `f : α → α` is a morphism of complete lattices, then the liminf of its iterates of any `a : α` is a fixed point. -/ lemma complete_lattice_hom.apply_liminf_iterate (f : complete_lattice_hom α α) (a : α) : f (liminf (λ n, f^[n] a) at_top) = liminf (λ n, f^[n] a) at_top := (complete_lattice_hom.dual f).apply_limsup_iterate _ variables {f g : filter β} {p q : β → Prop} {u v : β → α} lemma blimsup_mono (h : ∀ x, p x → q x) : blimsup u f p ≤ blimsup u f q := Inf_le_Inf $ λ a ha, ha.mono $ by tauto lemma bliminf_antitone (h : ∀ x, p x → q x) : bliminf u f q ≤ bliminf u f p := Sup_le_Sup $ λ a ha, ha.mono $ by tauto lemma mono_blimsup' (h : ∀ᶠ x in f, p x → u x ≤ v x) : blimsup u f p ≤ blimsup v f p := Inf_le_Inf $ λ a ha, (ha.and h).mono $ λ x hx hx', (hx.2 hx').trans (hx.1 hx') lemma mono_blimsup (h : ∀ x, p x → u x ≤ v x) : blimsup u f p ≤ blimsup v f p := mono_blimsup' $ eventually_of_forall h lemma mono_bliminf' (h : ∀ᶠ x in f, p x → u x ≤ v x) : bliminf u f p ≤ bliminf v f p := Sup_le_Sup $ λ a ha, (ha.and h).mono $ λ x hx hx', (hx.1 hx').trans (hx.2 hx') lemma mono_bliminf (h : ∀ x, p x → u x ≤ v x) : bliminf u f p ≤ bliminf v f p := mono_bliminf' $ eventually_of_forall h lemma bliminf_antitone_filter (h : f ≤ g) : bliminf u g p ≤ bliminf u f p := Sup_le_Sup $ λ a ha, ha.filter_mono h lemma blimsup_monotone_filter (h : f ≤ g) : blimsup u f p ≤ blimsup u g p := Inf_le_Inf $ λ a ha, ha.filter_mono h @[simp] lemma blimsup_and_le_inf : blimsup u f (λ x, p x ∧ q x) ≤ blimsup u f p ⊓ blimsup u f q := le_inf (blimsup_mono $ by tauto) (blimsup_mono $ by tauto) @[simp] lemma bliminf_sup_le_and : bliminf u f p ⊔ bliminf u f q ≤ bliminf u f (λ x, p x ∧ q x) := @blimsup_and_le_inf αᵒᵈ β _ f p q u /-- See also `filter.blimsup_or_eq_sup`. -/ @[simp] lemma blimsup_sup_le_or : blimsup u f p ⊔ blimsup u f q ≤ blimsup u f (λ x, p x ∨ q x) := sup_le (blimsup_mono $ by tauto) (blimsup_mono $ by tauto) /-- See also `filter.bliminf_or_eq_inf`. -/ @[simp] lemma bliminf_or_le_inf : bliminf u f (λ x, p x ∨ q x) ≤ bliminf u f p ⊓ bliminf u f q := @blimsup_sup_le_or αᵒᵈ β _ f p q u lemma order_iso.apply_blimsup [complete_lattice γ] (e : α ≃o γ) : e (blimsup u f p) = blimsup (e ∘ u) f p := begin simp only [blimsup_eq, map_Inf, function.comp_app], congr, ext c, obtain ⟨a, rfl⟩ := e.surjective c, simp, end lemma order_iso.apply_bliminf [complete_lattice γ] (e : α ≃o γ) : e (bliminf u f p) = bliminf (e ∘ u) f p := @order_iso.apply_blimsup αᵒᵈ β γᵒᵈ _ f p u _ e.dual lemma Sup_hom.apply_blimsup_le [complete_lattice γ] (g : Sup_hom α γ) : g (blimsup u f p) ≤ blimsup (g ∘ u) f p := begin simp only [blimsup_eq_infi_bsupr], refine ((order_hom_class.mono g).map_infi₂_le _).trans _, simp only [_root_.map_supr], end lemma Inf_hom.le_apply_bliminf [complete_lattice γ] (g : Inf_hom α γ) : bliminf (g ∘ u) f p ≤ g (bliminf u f p) := @Sup_hom.apply_blimsup_le αᵒᵈ β γᵒᵈ _ f p u _ g.dual end complete_lattice section complete_distrib_lattice variables [complete_distrib_lattice α] {f : filter β} {p q : β → Prop} {u : β → α} @[simp] lemma blimsup_or_eq_sup : blimsup u f (λ x, p x ∨ q x) = blimsup u f p ⊔ blimsup u f q := begin refine le_antisymm _ blimsup_sup_le_or, simp only [blimsup_eq, Inf_sup_eq, sup_Inf_eq, le_infi₂_iff, mem_set_of_eq], refine λ a' ha' a ha, Inf_le ((ha.and ha').mono $ λ b h hb, _), exact or.elim hb (λ hb, le_sup_of_le_left $ h.1 hb) (λ hb, le_sup_of_le_right $ h.2 hb), end @[simp] lemma bliminf_or_eq_inf : bliminf u f (λ x, p x ∨ q x) = bliminf u f p ⊓ bliminf u f q := @blimsup_or_eq_sup αᵒᵈ β _ f p q u lemma sup_limsup [ne_bot f] (a : α) : a ⊔ limsup u f = limsup (λ x, a ⊔ u x) f := begin simp only [limsup_eq_infi_supr, supr_sup_eq, sup_binfi_eq], congr, ext s, congr, ext hs, congr, exact (bsupr_const (nonempty_of_mem hs)).symm, end lemma inf_liminf [ne_bot f] (a : α) : a ⊓ liminf u f = liminf (λ x, a ⊓ u x) f := @sup_limsup αᵒᵈ β _ f _ _ _ lemma sup_liminf (a : α) : a ⊔ liminf u f = liminf (λ x, a ⊔ u x) f := begin simp only [liminf_eq_supr_infi], rw [sup_comm, bsupr_sup (⟨univ, univ_mem⟩ : ∃ (i : set β), i ∈ f)], simp_rw [binfi_sup_eq, @sup_comm _ _ a], end lemma inf_limsup (a : α) : a ⊓ limsup u f = limsup (λ x, a ⊓ u x) f := @sup_liminf αᵒᵈ β _ f _ _ end complete_distrib_lattice section complete_boolean_algebra variables [complete_boolean_algebra α] (f : filter β) (u : β → α) lemma limsup_compl : (limsup u f)ᶜ = liminf (compl ∘ u) f := by simp only [limsup_eq_infi_supr, liminf_eq_supr_infi, compl_infi, compl_supr] lemma liminf_compl : (liminf u f)ᶜ = limsup (compl ∘ u) f := by simp only [limsup_eq_infi_supr, liminf_eq_supr_infi, compl_infi, compl_supr] lemma limsup_sdiff (a : α) : (limsup u f) \ a = limsup (λ b, (u b) \ a) f := begin simp only [limsup_eq_infi_supr, sdiff_eq], rw binfi_inf (⟨univ, univ_mem⟩ : ∃ (i : set β), i ∈ f), simp_rw [inf_comm, inf_bsupr_eq, inf_comm], end lemma liminf_sdiff [ne_bot f] (a : α) : (liminf u f) \ a = liminf (λ b, (u b) \ a) f := by simp only [sdiff_eq, @inf_comm _ _ _ aᶜ, inf_liminf] lemma sdiff_limsup [ne_bot f] (a : α) : a \ limsup u f = liminf (λ b, a \ u b) f := begin rw ← compl_inj_iff, simp only [sdiff_eq, liminf_compl, (∘), compl_inf, compl_compl, sup_limsup], end lemma sdiff_liminf (a : α) : a \ liminf u f = limsup (λ b, a \ u b) f := begin rw ← compl_inj_iff, simp only [sdiff_eq, limsup_compl, (∘), compl_inf, compl_compl, sup_liminf], end end complete_boolean_algebra section set_lattice variables {p : ι → Prop} {s : ι → set α} lemma cofinite.blimsup_set_eq : blimsup s cofinite p = { x | { n | p n ∧ x ∈ s n }.infinite } := begin simp only [blimsup_eq, le_eq_subset, eventually_cofinite, not_forall, Inf_eq_sInter, exists_prop], ext x, refine ⟨λ h, _, λ hx t h, _⟩; contrapose! h, { simp only [mem_sInter, mem_set_of_eq, not_forall, exists_prop], exact ⟨{x}ᶜ, by simpa using h, by simp⟩, }, { exact hx.mono (λ i hi, ⟨hi.1, λ hit, h (hit hi.2)⟩), }, end lemma cofinite.bliminf_set_eq : bliminf s cofinite p = { x | { n | p n ∧ x ∉ s n }.finite } := begin rw ← compl_inj_iff, simpa only [bliminf_eq_supr_binfi, compl_infi, compl_supr, ← blimsup_eq_infi_bsupr, cofinite.blimsup_set_eq], end /-- In other words, `limsup cofinite s` is the set of elements lying inside the family `s` infinitely often. -/ lemma cofinite.limsup_set_eq : limsup s cofinite = { x | { n | x ∈ s n }.infinite } := by simp only [← cofinite.blimsup_true s, cofinite.blimsup_set_eq, true_and] /-- In other words, `liminf cofinite s` is the set of elements lying outside the family `s` finitely often. -/ lemma cofinite.liminf_set_eq : liminf s cofinite = { x | { n | x ∉ s n }.finite } := by simp only [← cofinite.bliminf_true s, cofinite.bliminf_set_eq, true_and] lemma exists_forall_mem_of_has_basis_mem_blimsup {l : filter β} {b : ι → set β} {q : ι → Prop} (hl : l.has_basis q b) {u : β → set α} {p : β → Prop} {x : α} (hx : x ∈ blimsup u l p) : ∃ f : {i | q i} → β, ∀ i, x ∈ u (f i) ∧ p (f i) ∧ f i ∈ b i := begin rw blimsup_eq_infi_bsupr at hx, simp only [supr_eq_Union, infi_eq_Inter, mem_Inter, mem_Union, exists_prop] at hx, choose g hg hg' using hx, refine ⟨λ (i : {i | q i}), g (b i) (hl.mem_of_mem i.2), λ i, ⟨_, _⟩⟩, { exact hg' (b i) (hl.mem_of_mem i.2), }, { exact hg (b i) (hl.mem_of_mem i.2), }, end lemma exists_forall_mem_of_has_basis_mem_blimsup' {l : filter β} {b : ι → set β} (hl : l.has_basis (λ _, true) b) {u : β → set α} {p : β → Prop} {x : α} (hx : x ∈ blimsup u l p) : ∃ f : ι → β, ∀ i, x ∈ u (f i) ∧ p (f i) ∧ f i ∈ b i := begin obtain ⟨f, hf⟩ := exists_forall_mem_of_has_basis_mem_blimsup hl hx, exact ⟨λ i, f ⟨i, trivial⟩, λ i, hf ⟨i, trivial⟩⟩, end end set_lattice section conditionally_complete_linear_order lemma frequently_lt_of_lt_Limsup {f : filter α} [conditionally_complete_linear_order α] {a : α} (hf : f.is_cobounded (≤) . is_bounded_default) (h : a < Limsup f) : ∃ᶠ n in f, a < n := begin contrapose! h, simp only [not_frequently, not_lt] at h, exact Limsup_le_of_le hf h, end lemma frequently_lt_of_Liminf_lt {f : filter α} [conditionally_complete_linear_order α] {a : α} (hf : f.is_cobounded (≥) . is_bounded_default) (h : Liminf f < a) : ∃ᶠ n in f, n < a := @frequently_lt_of_lt_Limsup (order_dual α) f _ a hf h lemma eventually_lt_of_lt_liminf {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : b < liminf u f) (hu : f.is_bounded_under (≥) u . is_bounded_default) : ∀ᶠ a in f, b < u a := begin obtain ⟨c, hc, hbc⟩ : ∃ (c : β) (hc : c ∈ {c : β | ∀ᶠ (n : α) in f, c ≤ u n}), b < c := exists_lt_of_lt_cSup hu h, exact hc.mono (λ x hx, lt_of_lt_of_le hbc hx) end lemma eventually_lt_of_limsup_lt {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : limsup u f < b) (hu : f.is_bounded_under (≤) u . is_bounded_default) : ∀ᶠ a in f, u a < b := @eventually_lt_of_lt_liminf _ βᵒᵈ _ _ _ _ h hu lemma le_limsup_of_frequently_le {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu_le : ∃ᶠ x in f, b ≤ u x) (hu : f.is_bounded_under (≤) u . is_bounded_default) : b ≤ limsup u f := begin revert hu_le, rw [←not_imp_not, not_frequently], simp_rw ←lt_iff_not_ge, exact λ h, eventually_lt_of_limsup_lt h hu, end lemma liminf_le_of_frequently_le {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu_le : ∃ᶠ x in f, u x ≤ b) (hu : f.is_bounded_under (≥) u . is_bounded_default) : liminf u f ≤ b := @le_limsup_of_frequently_le _ βᵒᵈ _ f u b hu_le hu lemma frequently_lt_of_lt_limsup {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu : f.is_cobounded_under (≤) u . is_bounded_default) (h : b < limsup u f) : ∃ᶠ x in f, b < u x := begin contrapose! h, apply Limsup_le_of_le hu, simpa using h, end lemma frequently_lt_of_liminf_lt {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu : f.is_cobounded_under (≥) u . is_bounded_default) (h : liminf u f < b) : ∃ᶠ x in f, u x < b := @frequently_lt_of_lt_limsup _ βᵒᵈ _ f u b hu h end conditionally_complete_linear_order end filter section order open filter lemma monotone.is_bounded_under_le_comp [nonempty β] [linear_order β] [preorder γ] [no_max_order γ] {g : β → γ} {f : α → β} {l : filter α} (hg : monotone g) (hg' : tendsto g at_top at_top) : is_bounded_under (≤) l (g ∘ f) ↔ is_bounded_under (≤) l f := begin refine ⟨_, λ h, h.is_bounded_under hg⟩, rintro ⟨c, hc⟩, rw eventually_map at hc, obtain ⟨b, hb⟩ : ∃ b, ∀ a ≥ b, c < g a := eventually_at_top.1 (hg'.eventually_gt_at_top c), exact ⟨b, hc.mono $ λ x hx, not_lt.1 (λ h, (hb _ h.le).not_le hx)⟩ end lemma monotone.is_bounded_under_ge_comp [nonempty β] [linear_order β] [preorder γ] [no_min_order γ] {g : β → γ} {f : α → β} {l : filter α} (hg : monotone g) (hg' : tendsto g at_bot at_bot) : is_bounded_under (≥) l (g ∘ f) ↔ is_bounded_under (≥) l f := hg.dual.is_bounded_under_le_comp hg' lemma antitone.is_bounded_under_le_comp [nonempty β] [linear_order β] [preorder γ] [no_max_order γ] {g : β → γ} {f : α → β} {l : filter α} (hg : antitone g) (hg' : tendsto g at_bot at_top) : is_bounded_under (≤) l (g ∘ f) ↔ is_bounded_under (≥) l f := hg.dual_right.is_bounded_under_ge_comp hg' lemma antitone.is_bounded_under_ge_comp [nonempty β] [linear_order β] [preorder γ] [no_min_order γ] {g : β → γ} {f : α → β} {l : filter α} (hg : antitone g) (hg' : tendsto g at_top at_bot) : is_bounded_under (≥) l (g ∘ f) ↔ is_bounded_under (≤) l f := hg.dual_right.is_bounded_under_le_comp hg' lemma galois_connection.l_limsup_le [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {v : α → β} {l : β → γ} {u : γ → β} (gc : galois_connection l u) (hlv : f.is_bounded_under (≤) (λ x, l (v x)) . is_bounded_default) (hv_co : f.is_cobounded_under (≤) v . is_bounded_default) : l (limsup v f) ≤ limsup (λ x, l (v x)) f := begin refine le_Limsup_of_le hlv (λ c hc, _), rw filter.eventually_map at hc, simp_rw (gc _ _) at hc ⊢, exact Limsup_le_of_le hv_co hc, end lemma order_iso.limsup_apply {γ} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {u : α → β} (g : β ≃o γ) (hu : f.is_bounded_under (≤) u . is_bounded_default) (hu_co : f.is_cobounded_under (≤) u . is_bounded_default) (hgu : f.is_bounded_under (≤) (λ x, g (u x)) . is_bounded_default) (hgu_co : f.is_cobounded_under (≤) (λ x, g (u x)) . is_bounded_default) : g (limsup u f) = limsup (λ x, g (u x)) f := begin refine le_antisymm (g.to_galois_connection.l_limsup_le hgu hu_co) _, rw [←(g.symm.symm_apply_apply $ limsup (λ x, g (u x)) f), g.symm_symm], refine g.monotone _, have hf : u = λ i, g.symm (g (u i)), from funext (λ i, (g.symm_apply_apply (u i)).symm), nth_rewrite 0 hf, refine g.symm.to_galois_connection.l_limsup_le _ hgu_co, simp_rw g.symm_apply_apply, exact hu, end lemma order_iso.liminf_apply {γ} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {u : α → β} (g : β ≃o γ) (hu : f.is_bounded_under (≥) u . is_bounded_default) (hu_co : f.is_cobounded_under (≥) u . is_bounded_default) (hgu : f.is_bounded_under (≥) (λ x, g (u x)) . is_bounded_default) (hgu_co : f.is_cobounded_under (≥) (λ x, g (u x)) . is_bounded_default) : g (liminf u f) = liminf (λ x, g (u x)) f := @order_iso.limsup_apply α βᵒᵈ γᵒᵈ _ _ f u g.dual hu hu_co hgu hgu_co end order
3b2e30d622f066ebd136eaf673c6d6cbc9f63b1d
0003047346476c031128723dfd16fe273c6bc605
/src/ring_theory/algebra.lean
232c77ff933885d8d510c76edeeb38bf4bf30261
[ "Apache-2.0" ]
permissive
ChandanKSingh/mathlib
d2bf4724ccc670bf24915c12c475748281d3fb73
d60d1616958787ccb9842dc943534f90ea0bab64
refs/heads/master
1,588,238,823,679
1,552,867,469,000
1,552,867,469,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,606
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Algebra over Commutative Ring (under category) -/ import data.polynomial data.multivariate_polynomial import linear_algebra.tensor_product import ring_theory.subring universes u v w u₁ v₁ open lattice local infix ` ⊗ `:100 := tensor_product /-- The category of R-algebras where R is a commutative ring is the under category R ↓ CRing. In the categorical setting we have a forgetful functor R-Alg ⥤ R-Mod. However here it extends module in order to preserve definitional equality in certain cases. -/ class algebra (R : Type u) (A : Type v) [comm_ring R] [ring A] extends module R A := (to_fun : R → A) [hom : is_ring_hom to_fun] (commutes' : ∀ r x, x * to_fun r = to_fun r * x) (smul_def' : ∀ r x, r • x = to_fun r * x) attribute [instance] algebra.hom def algebra_map {R : Type u} (A : Type v) [comm_ring R] [ring A] [algebra R A] (x : R) : A := algebra.to_fun A x namespace algebra variables {R : Type u} {S : Type v} {A : Type w} variables [comm_ring R] [comm_ring S] [ring A] [algebra R A] /-- The codomain of an algebra. -/ instance : module R A := infer_instance instance : has_scalar R A := infer_instance instance {F : Type u} {K : Type v} [discrete_field F] [ring K] [algebra F K] : vector_space F K := @vector_space.mk F _ _ _ algebra.module include R instance : is_ring_hom (algebra_map A : R → A) := algebra.hom _ A variables (A) @[simp] lemma map_add (r s : R) : algebra_map A (r + s) = algebra_map A r + algebra_map A s := is_ring_hom.map_add _ @[simp] lemma map_neg (r : R) : algebra_map A (-r) = -algebra_map A r := is_ring_hom.map_neg _ @[simp] lemma map_sub (r s : R) : algebra_map A (r - s) = algebra_map A r - algebra_map A s := is_ring_hom.map_sub _ @[simp] lemma map_mul (r s : R) : algebra_map A (r * s) = algebra_map A r * algebra_map A s := is_ring_hom.map_mul _ variables (R) @[simp] lemma map_zero : algebra_map A (0 : R) = 0 := is_ring_hom.map_zero _ @[simp] lemma map_one : algebra_map A (1 : R) = 1 := is_ring_hom.map_one _ variables {R A} /-- Creating an algebra from a morphism in CRing. -/ def of_ring_hom (i : R → S) (hom : is_ring_hom i) : algebra R S := { smul := λ c x, i c * x, smul_zero := λ x, mul_zero (i x), smul_add := λ r x y, mul_add (i r) x y, add_smul := λ r s x, show i (r + s) * x = _, by rw [hom.3, add_mul], mul_smul := λ r s x, show i (r * s) * x = _, by rw [hom.2, mul_assoc], one_smul := λ x, show i 1 * x = _, by rw [hom.1, one_mul], zero_smul := λ x, show i 0 * x = _, by rw [@@is_ring_hom.map_zero _ _ i hom, zero_mul], to_fun := i, commutes' := λ _ _, mul_comm _ _, smul_def' := λ c x, rfl } theorem smul_def (r : R) (x : A) : r • x = algebra_map A r * x := algebra.smul_def' r x theorem commutes (r : R) (x : A) : x * algebra_map A r = algebra_map A r * x := algebra.commutes' r x theorem left_comm (r : R) (x y : A) : x * (algebra_map A r * y) = algebra_map A r * (x * y) := by rw [← mul_assoc, commutes, mul_assoc] @[simp] lemma mul_smul_comm (s : R) (x y : A) : x * (s • y) = s • (x * y) := by rw [smul_def, smul_def, left_comm] @[simp] lemma smul_mul_assoc (r : R) (x y : A) : (r • x) * y = r • (x * y) := by rw [smul_def, smul_def, mul_assoc] /-- R[X] is the generator of the category R-Alg. -/ instance polynomial (R : Type u) [comm_ring R] [decidable_eq R] : algebra R (polynomial R) := { to_fun := polynomial.C, commutes' := λ _ _, mul_comm _ _, smul_def' := λ c p, (polynomial.C_mul' c p).symm, .. polynomial.module } /-- The algebra of multivariate polynomials. -/ instance mv_polynomial (R : Type u) [comm_ring R] [decidable_eq R] (ι : Type v) [decidable_eq ι] : algebra R (mv_polynomial ι R) := { to_fun := mv_polynomial.C, commutes' := λ _ _, mul_comm _ _, smul_def' := λ c p, (mv_polynomial.C_mul' c p).symm, .. mv_polynomial.module } /-- Creating an algebra from a subring. This is the dual of ring extension. -/ instance of_subring (S : set R) [is_subring S] : algebra S R := of_ring_hom subtype.val ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩ variables (R A) /-- The multiplication in an algebra is a bilinear map. -/ def lmul : A →ₗ A →ₗ A := linear_map.mk₂ R (*) (λ x y z, add_mul x y z) (λ c x y, by rw [smul_def, smul_def, mul_assoc _ x y]) (λ x y z, mul_add x y z) (λ c x y, by rw [smul_def, smul_def, left_comm]) set_option class.instance_max_depth 39 def lmul_left (r : A) : A →ₗ A := lmul R A r def lmul_right (r : A) : A →ₗ A := (lmul R A).flip r variables {R A} @[simp] lemma lmul_apply (p q : A) : lmul R A p q = p * q := rfl @[simp] lemma lmul_left_apply (p q : A) : lmul_left R A p q = p * q := rfl @[simp] lemma lmul_right_apply (p q : A) : lmul_right R A p q = q * p := rfl end algebra /-- Defining the homomorphism in the category R-Alg. -/ structure alg_hom {R : Type u} (A : Type v) (B : Type w) [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] := (to_fun : A → B) [hom : is_ring_hom to_fun] (commutes' : ∀ r : R, to_fun (algebra_map A r) = algebra_map B r) infixr ` →ₐ `:25 := alg_hom notation A ` →ₐ[`:25 R `] ` B := @alg_hom R A B _ _ _ _ _ namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁} variables [comm_ring R] [ring A] [ring B] [ring C] [ring D] variables [algebra R A] [algebra R B] [algebra R C] [algebra R D] include R instance : has_coe_to_fun (A →ₐ[R] B) := ⟨λ _, A → B, to_fun⟩ variables (φ : A →ₐ[R] B) instance : is_ring_hom ⇑φ := hom φ @[extensionality] theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := by cases φ₁; cases φ₂; congr' 1; ext; apply H theorem commutes (r : R) : φ (algebra_map A r) = algebra_map B r := φ.commutes' r @[simp] lemma map_add (r s : A) : φ (r + s) = φ r + φ s := is_ring_hom.map_add _ @[simp] lemma map_zero : φ 0 = 0 := is_ring_hom.map_zero _ @[simp] lemma map_neg (x) : φ (-x) = -φ x := is_ring_hom.map_neg _ @[simp] lemma map_sub (x y) : φ (x - y) = φ x - φ y := is_ring_hom.map_sub _ @[simp] lemma map_mul (x y) : φ (x * y) = φ x * φ y := is_ring_hom.map_mul _ @[simp] lemma map_one : φ 1 = 1 := is_ring_hom.map_one _ /-- R-Alg ⥤ R-Mod -/ def to_linear_map : A →ₗ B := { to_fun := φ, add := φ.map_add, smul := λ c x, by rw [algebra.smul_def, φ.map_mul, φ.commutes c, algebra.smul_def] } @[simp] lemma to_linear_map_apply (p : A) : φ.to_linear_map p = φ p := rfl theorem to_linear_map_inj {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁.to_linear_map = φ₂.to_linear_map) : φ₁ = φ₂ := ext $ λ x, show φ₁.to_linear_map x = φ₂.to_linear_map x, by rw H variables (R A) protected def id : A →ₐ[R] A := { to_fun := id, commutes' := λ _, rfl } variables {R A} @[simp] lemma id_apply (p : A) : alg_hom.id R A p = p := rfl def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ C := { to_fun := φ₁ ∘ φ₂, commutes' := λ r, by rw [function.comp_apply, φ₂.commutes, φ₁.commutes] } @[simp] lemma comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl @[simp] theorem comp_id : φ.comp (alg_hom.id R A) = φ := ext $ λ x, rfl @[simp] theorem id_comp : (alg_hom.id R B).comp φ = φ := ext $ λ x, rfl theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) : (φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) := ext $ λ x, rfl end alg_hom namespace algebra variables (R : Type u) (S : Type v) (A : Type w) variables [comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A] include R S A def comap : Type w := A def comap.to_comap : A → comap R S A := id def comap.of_comap : comap R S A → A := id omit R S A instance comap.ring : ring (comap R S A) := _inst_3 instance comap.comm_ring (R : Type u) (S : Type v) (A : Type w) [comm_ring R] [comm_ring S] [comm_ring A] [algebra R S] [algebra S A] : comm_ring (comap R S A) := _inst_8 instance comap.module : module S (comap R S A) := _inst_5.to_module instance comap.has_scalar : has_scalar S (comap R S A) := _inst_5.to_module.to_has_scalar /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ instance comap.algebra : algebra R (comap R S A) := { smul := λ r x, (algebra_map S r • x : A), smul_add := λ _ _ _, smul_add _ _ _, add_smul := λ _ _ _, by simp only [algebra.map_add]; from add_smul _ _ _, mul_smul := λ _ _ _, by simp only [algebra.map_mul]; from mul_smul _ _ _, one_smul := λ _, by simp only [algebra.map_one]; from one_smul _ _, zero_smul := λ _, by simp only [algebra.map_zero]; from zero_smul _ _, smul_zero := λ _, smul_zero _, to_fun := (algebra_map A : S → A) ∘ algebra_map S, hom := by letI : is_ring_hom (algebra_map A) := _inst_5.hom; apply_instance, commutes' := λ r x, algebra.commutes _ _, smul_def' := λ _ _, algebra.smul_def _ _ } def to_comap : S →ₐ[R] comap R S A := { to_fun := (algebra_map A : S → A), hom := _inst_5.hom, commutes' := λ r, rfl } theorem to_comap_apply (x) : to_comap R S A x = (algebra_map A : S → A) x := rfl end algebra namespace alg_hom variables {R : Type u} {S : Type v} {A : Type w} {B : Type u₁} variables [comm_ring R] [comm_ring S] [ring A] [ring B] variables [algebra R S] [algebra S A] [algebra S B] (φ : A →ₐ[S] B) include R /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def comap : algebra.comap R S A →ₐ[R] algebra.comap R S B := { to_fun := φ, hom := alg_hom.is_ring_hom _, commutes' := λ r, φ.commutes (algebra_map S r) } end alg_hom namespace polynomial variables (R : Type u) (A : Type v) variables [comm_ring R] [comm_ring A] [algebra R A] variables [decidable_eq R] (x : A) /-- A → Hom[R-Alg](R[X],A) -/ def aeval : polynomial R →ₐ[R] A := { to_fun := eval₂ (algebra_map A) x, hom := ⟨eval₂_one _ x, λ _ _, eval₂_mul _ x, λ _ _, eval₂_add _ x⟩, commutes' := λ r, eval₂_C _ _ } theorem aeval_def (p : polynomial R) : aeval R A x p = eval₂ (algebra_map A) x p := rfl instance aeval.is_ring_hom : is_ring_hom (aeval R A x) := alg_hom.hom _ theorem eval_unique (φ : polynomial R →ₐ[R] A) (p) : φ p = eval₂ (algebra_map A) (φ X) p := begin apply polynomial.induction_on p, { intro r, rw eval₂_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] }, { intros n r ih, rw [pow_succ', ← mul_assoc, is_ring_hom.map_mul φ, eval₂_mul (algebra_map A : R → A), eval₂_X, ih] } end end polynomial namespace mv_polynomial variables (R : Type u) (A : Type v) variables [comm_ring R] [comm_ring A] [algebra R A] variables [decidable_eq R] [decidable_eq A] (σ : set A) /-- (ι → A) → Hom[R-Alg](R[ι],A) -/ def aeval : mv_polynomial σ R →ₐ[R] A := { to_fun := eval₂ (algebra_map A) subtype.val, hom := ⟨eval₂_one _ _, λ _ _, eval₂_mul _ _, λ _ _, eval₂_add _ _⟩, commutes' := λ r, eval₂_C _ _ _ } theorem aeval_def (p : mv_polynomial σ R) : aeval R A σ p = eval₂ (algebra_map A) subtype.val p := rfl instance aeval.is_ring_hom : is_ring_hom (aeval R A σ) := alg_hom.hom _ variables (ι : Type w) [decidable_eq ι] theorem eval_unique (φ : mv_polynomial ι R →ₐ[R] A) (p) : φ p = eval₂ (algebra_map A) (φ ∘ X) p := begin apply mv_polynomial.induction_on p, { intro r, rw eval₂_C, exact φ.commutes r }, { intros f g ih1 ih2, rw [is_ring_hom.map_add φ, ih1, ih2, eval₂_add] }, { intros p j ih, rw [is_ring_hom.map_mul φ, eval₂_mul, eval₂_X, ih] } end end mv_polynomial structure subalgebra (R : Type u) (A : Type v) [comm_ring R] [ring A] [algebra R A] : Type v := (carrier : set A) [subring : is_subring carrier] (range_le : set.range (algebra_map A : R → A) ≤ carrier) attribute [instance] subalgebra.subring namespace subalgebra variables {R : Type u} {A : Type v} variables [comm_ring R] [ring A] [algebra R A] include R instance : has_coe (subalgebra R A) (set A) := ⟨λ S, S.carrier⟩ instance : has_mem A (subalgebra R A) := ⟨λ x S, x ∈ S.carrier⟩ variables {A} theorem mem_coe {x : A} {s : subalgebra R A} : x ∈ (s : set A) ↔ x ∈ s := iff.rfl @[extensionality] theorem ext {S T : subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := by cases S; cases T; congr; ext x; exact h x variables (S : subalgebra R A) instance : is_subring (S : set A) := S.subring instance : ring S := @@subtype.ring _ S.is_subring instance (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : comm_ring S := @@subtype.comm_ring _ S.is_subring instance algebra : algebra R S := { smul := λ (c:R) x, ⟨c • x.1, by rw algebra.smul_def; exact @@is_submonoid.mul_mem _ S.2.2 (S.3 ⟨c, rfl⟩) x.2⟩, smul_add := λ c x y, subtype.eq $ by apply _inst_3.1.1.2, add_smul := λ c x y, subtype.eq $ by apply _inst_3.1.1.3, mul_smul := λ c x y, subtype.eq $ by apply _inst_3.1.1.4, one_smul := λ x, subtype.eq $ by apply _inst_3.1.1.5, zero_smul := λ x, subtype.eq $ by apply _inst_3.1.1.6, smul_zero := λ x, subtype.eq $ by apply _inst_3.1.1.7, to_fun := λ r, ⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩, hom := ⟨subtype.eq $ algebra.map_one R A, λ x y, subtype.eq $ algebra.map_mul A x y, λ x y, subtype.eq $ algebra.map_add A x y⟩, commutes' := λ c x, subtype.eq $ by apply _inst_3.4, smul_def' := λ c x, subtype.eq $ by apply _inst_3.5 } instance to_algebra (R : Type u) (A : Type v) [comm_ring R] [comm_ring A] [algebra R A] (S : subalgebra R A) : algebra S A := algebra.of_subring _ def val : S →ₐ[R] A := { to_fun := subtype.val, hom := ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩, commutes' := λ r, rfl } def to_submodule : submodule R A := { carrier := S.carrier, zero := (0:S).2, add := λ x y hx hy, (⟨x, hx⟩ + ⟨y, hy⟩ : S).2, smul := λ c x hx, (algebra.smul_def c x).symm ▸ (⟨algebra_map A c, S.range_le ⟨c, rfl⟩⟩ * ⟨x, hx⟩:S).2 } instance to_submodule.is_subring : is_subring (S.to_submodule : set A) := S.2 instance : partial_order (subalgebra R A) := { le := λ S T, S.carrier ≤ T.carrier, le_refl := λ _, le_refl _, le_trans := λ _ _ _, le_trans, le_antisymm := λ S T hst hts, ext $ λ x, ⟨@hst x, @hts x⟩ } def comap {R : Type u} {S : Type v} {A : Type w} [comm_ring R] [comm_ring S] [ring A] [algebra R S] [algebra S A] (iSB : subalgebra S A) : subalgebra R (algebra.comap R S A) := { carrier := (iSB : set A), subring := iSB.is_subring, range_le := λ a ⟨r, hr⟩, hr ▸ iSB.range_le ⟨_, rfl⟩ } def under {R : Type u} {A : Type v} [comm_ring R] [comm_ring A] {i : algebra R A} (S : subalgebra R A) (T : subalgebra S A) : subalgebra R A := { carrier := T, range_le := (λ a ⟨r, hr⟩, hr ▸ T.range_le ⟨⟨algebra_map A r, S.range_le ⟨r, rfl⟩⟩, rfl⟩) } end subalgebra namespace alg_hom variables {R : Type u} {A : Type v} {B : Type w} variables [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] variables (φ : A →ₐ[R] B) protected def range : subalgebra R B := { carrier := set.range φ, subring := { one_mem := ⟨1, φ.map_one⟩, mul_mem := λ y₁ y₂ ⟨x₁, hx₁⟩ ⟨x₂, hx₂⟩, ⟨x₁ * x₂, hx₁ ▸ hx₂ ▸ φ.map_mul x₁ x₂⟩ }, range_le := λ y ⟨r, hr⟩, ⟨algebra_map A r, hr ▸ φ.commutes r⟩ } end alg_hom namespace algebra variables {R : Type u} (A : Type v) variables [comm_ring R] [ring A] [algebra R A] include R variables (R) instance id : algebra R R := algebra.of_ring_hom id $ by apply_instance def of_id : R →ₐ A := { to_fun := algebra_map A, commutes' := λ _, rfl } variables {R} theorem of_id_apply (r) : of_id R A r = algebra_map A r := rfl variables (R) {A} def adjoin (s : set A) : subalgebra R A := { carrier := ring.closure (set.range (algebra_map A : R → A) ∪ s), range_le := le_trans (set.subset_union_left _ _) ring.subset_closure } variables {R} protected def gc : galois_connection (adjoin R : set A → subalgebra R A) coe := λ s S, ⟨λ H, le_trans (le_trans (set.subset_union_right _ _) ring.subset_closure) H, λ H, ring.closure_subset $ set.union_subset S.range_le H⟩ protected def gi : galois_insertion (adjoin R : set A → subalgebra R A) coe := { choice := λ s hs, adjoin R s, gc := algebra.gc, le_l_u := λ S, (algebra.gc (S : set A) (adjoin R S)).1 $ le_refl _, choice_eq := λ _ _, rfl } instance : complete_lattice (subalgebra R A) := galois_insertion.lift_complete_lattice algebra.gi theorem mem_bot {x : A} : x ∈ (⊥ : subalgebra R A) ↔ x ∈ set.range (algebra_map A : R → A) := suffices (⊥ : subalgebra R A) = (of_id R A).range, by rw this; refl, le_antisymm bot_le $ subalgebra.range_le _ theorem mem_top {x : A} : x ∈ (⊤ : subalgebra R A) := ring.mem_closure $ or.inr trivial def to_top : A →ₐ[R] (⊤ : subalgebra R A) := { to_fun := λ x, ⟨x, mem_top⟩, hom := ⟨rfl, λ _ _, rfl, λ _ _, rfl⟩, commutes' := λ _, rfl } end algebra section int variables (R : Type*) [comm_ring R] /-- CRing ⥤ ℤ-Alg -/ def alg_hom_int {R : Type u} [comm_ring R] [algebra ℤ R] {S : Type v} [comm_ring S] [algebra ℤ S] (f : R → S) [is_ring_hom f] : R →ₐ[ℤ] S := { to_fun := f, hom := by apply_instance, commutes' := λ i, int.induction_on i (by rw [algebra.map_zero, algebra.map_zero, is_ring_hom.map_zero f]) (λ i ih, by rw [algebra.map_add, algebra.map_add, algebra.map_one, algebra.map_one]; rw [is_ring_hom.map_add f, is_ring_hom.map_one f, ih]) (λ i ih, by rw [algebra.map_sub, algebra.map_sub, algebra.map_one, algebra.map_one]; rw [is_ring_hom.map_sub f, is_ring_hom.map_one f, ih]) } /-- CRing ⥤ ℤ-Alg -/ instance algebra_int : algebra ℤ R := algebra.of_ring_hom coe $ by constructor; intros; simp variables {R} /-- CRing ⥤ ℤ-Alg -/ def subalgebra_of_subring (S : set R) [is_subring S] : subalgebra ℤ R := { carrier := S, range_le := λ x ⟨i, h⟩, h ▸ int.induction_on i (by rw algebra.map_zero; exact is_add_submonoid.zero_mem _) (λ i hi, by rw [algebra.map_add, algebra.map_one]; exact is_add_submonoid.add_mem hi (is_submonoid.one_mem _)) (λ i hi, by rw [algebra.map_sub, algebra.map_one]; exact is_add_subgroup.sub_mem _ _ _ hi (is_submonoid.one_mem _)) } @[simp] lemma mem_subalgebra_of_subring {x : R} {S : set R} [is_subring S] : x ∈ subalgebra_of_subring S ↔ x ∈ S := iff.rfl end int
85b0ebf6de8419cca5323c4b086ec98eda7a9e47
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/tactic/slim_check.lean
103c2cfb005f0fae37993da646f09155bdfc932b
[ "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
7,282
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import testing.slim_check.testable /-! ## Finding counterexamples automatically using `slim_check` A proposition can be tested by writing it out as: ```lean example (xs : list ℕ) (w : ∃ x ∈ xs, x < 3) : ∀ y ∈ xs, y < 5 := by slim_check -- =================== -- Found problems! -- xs := [0, 5] -- x := 0 -- y := 5 -- ------------------- example (x : ℕ) (h : 2 ∣ x) : x < 100 := by slim_check -- =================== -- Found problems! -- x := 258 -- ------------------- example (α : Type) (xs ys : list α) : xs ++ ys = ys ++ xs := by slim_check -- =================== -- Found problems! -- α := ℤ -- xs := [-4] -- ys := [1] -- ------------------- example : ∀ x ∈ [1,2,3], x < 4 := by slim_check -- Success ``` In the first example, `slim_check` is called on the following goal: ```lean xs : list ℕ, h : ∃ (x : ℕ) (H : x ∈ xs), x < 3 ⊢ ∀ (y : ℕ), y ∈ xs → y < 5 ``` The local constants are reverted and an instance is found for `testable (∀ (xs : list ℕ), (∃ x ∈ xs, x < 3) → (∀ y ∈ xs, y < 5))`. The `testable` instance is supported by instances of `sampleable (list ℕ)`, `decidable (x < 3)` and `decidable (y < 5)`. `slim_check` builds a `testable` instance step by step with: ``` - testable (∀ (xs : list ℕ), (∃ x ∈ xs, x < 3) → (∀ y ∈ xs, y < 5)) -: sampleable (list xs) - testable ((∃ x ∈ xs, x < 3) → (∀ y ∈ xs, y < 5)) - testable (∀ x ∈ xs, x < 3 → (∀ y ∈ xs, y < 5)) - testable (x < 3 → (∀ y ∈ xs, y < 5)) -: decidable (x < 3) - testable (∀ y ∈ xs, y < 5) -: decidable (y < 5) ``` `sampleable (list ℕ)` lets us create random data of type `list ℕ` in a way that helps find small counter-examples. Next, the test of the proposition hinges on `x < 3` and `y < 5` to both be decidable. The implication between the two could be tested as a whole but it would be less informative. Indeed, if we generate lists that only contain numbers greater than `3`, the implication will always trivially hold but we should conclude that we haven't found meaningful examples. Instead, when `x < 3` does not hold, we reject the example (i.e. we do not count it toward the 100 required positive examples) and we start over. Therefore, when `slim_check` prints `Success`, it means that a hundred suitable lists were found and successfully tested. If no counter-examples are found, `slim_check` behaves like `admit`. `slim_check` can also be invoked using `#eval`: ```lean #eval slim_check.testable.check (∀ (α : Type) (xs ys : list α), xs ++ ys = ys ++ xs) -- =================== -- Found problems! -- α := ℤ -- xs := [-4] -- ys := [1] -- ------------------- ``` For more information on writing your own `sampleable` and `testable` instances, see `testing.slim_check.testable`. -/ namespace tactic.interactive open tactic slim_check declare_trace slim_check.instance declare_trace slim_check.decoration declare_trace slim_check.discared open expr /-- Tree structure representing a `testable` instance. -/ meta inductive instance_tree | node : name → expr → list instance_tree → instance_tree /-- Gather information about a `testable` instance. Given an expression of type `testable ?p`, gather the name of the `testable` instances that it is built from and the proposition that they test. -/ meta def summarize_instance : expr → tactic instance_tree | (lam n bi d b) := do v ← mk_local' n bi d, summarize_instance $ b.instantiate_var v | e@(app f x) := do `(testable %%p) ← infer_type e, xs ← e.get_app_args.mmap_filter (try_core ∘ summarize_instance), pure $ instance_tree.node e.get_app_fn.const_name p xs | e := do failed /-- format a `instance_tree` -/ meta def instance_tree.to_format : instance_tree → tactic format | (instance_tree.node n p xs) := do xs ← format.join <$> (xs.mmap $ λ t, flip format.indent 2 <$> instance_tree.to_format t), ys ← pformat!"testable ({p})", pformat!"+ {n} :{format.indent ys 2}\n{xs}" meta instance instance_tree.has_to_tactic_format : has_to_tactic_format instance_tree := ⟨ instance_tree.to_format ⟩ /-- `slim_check` considers a proof goal and tries to generate examples that would contradict the statement. Let's consider the following proof goal. ```lean xs : list ℕ, h : ∃ (x : ℕ) (H : x ∈ xs), x < 3 ⊢ ∀ (y : ℕ), y ∈ xs → y < 5 ``` The local constants will be reverted and an instance will be found for `testable (∀ (xs : list ℕ), (∃ x ∈ xs, x < 3) → (∀ y ∈ xs, y < 5))`. The `testable` instance is supported by an instance of `sampleable (list ℕ)`, `decidable (x < 3)` and `decidable (y < 5)`. Examples will be created in ascending order of size (more or less) The first counter-examples found will be printed and will result in an error: ``` =================== Found problems! xs := [1, 28] x := 1 y := 28 ------------------- ``` If `slim_check` successfully tests 100 examples, it acts like admit. If it gives up or finds a counter-example, it reports an error. For more information on writing your own `sampleable` and `testable` instances, see `testing.slim_check.testable`. Optional arguments given with `slim_check_cfg` * num_inst (default 100): number of examples to test properties with * max_size (default 100): final size argument * enable_tracing (default ff): enable the printing of discarded samples Options: * `set_option trace.slim_check.decoration true`: print the proposition with quantifier annotations * `set_option trace.slim_check.discarded true`: print the examples discarded because they do not satisfy assumptions * `set_option trace.slim_check.instance true`: print the instances of `testable` being used to test the proposition -/ meta def slim_check (cfg : slim_check_cfg := {}) : tactic unit := do { tgt ← retrieve $ tactic.revert_all >> target, let tgt' := tactic.add_decorations tgt, let cfg := { cfg with enable_tracing := cfg.enable_tracing || is_trace_enabled_for `slim_check.discared }, inst ← mk_app ``testable [tgt'] >>= mk_instance <|> fail!"Failed to create a `testable` instance for `{tgt}`. What to do: 1. make sure that the types you are using have `sampleable` instances (you can use `#sample my_type` if you are unsure); 2. make sure that the relations and predicates that your proposition use are decidable; 3. make sure that instances of `testable` exist that, when combined, apply to your decorated proposition: ``` {tgt'} ``` Use `set_option trace.class_instances true` to understand what instances are missing. Try this: set_option trace.class_instances true #check (by apply_instance : testable ({tgt'}))", e ← mk_mapp ``testable.check [tgt, `(cfg), tgt', inst], when_tracing `slim_check.decoration trace!"[testable decoration]\n {tgt'}", when_tracing `slim_check.instance $ do { inst ← summarize_instance inst >>= pp, trace!"\n[testable instance]{format.indent inst 2}" }, code ← eval_expr (io punit) e, unsafe_run_io code, admit } end tactic.interactive
458f71a5a79783b9c667eef1f0058837719fd9f2
7cef822f3b952965621309e88eadf618da0c8ae9
/src/topology/basic.lean
70bfe9a95d65c4d017db3a41814a80c4dba6fbe4
[ "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
33,416
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, Jeremy Avigad -/ import order.filter /-! # Basic theory of topological spaces. The main definition is the type class `topological space α` which endows a type `α` with a topology. Then `set α` gets predicates `is_open`, `is_closed` and functions `interior`, `closure` and `frontier`. Each point `x` of `α` gets a neighborhood filter `𝓝 x`. This file also defines locally finite families of subsets of `α`. For topological spaces `α` and `β`, a function `f : α → β` and a point `a : α`, `continuous_at f a` means `f` is continuous at `a`, and global continuity is `continuous f`. There is also a version of continuity `pcontinuous` for partially defined functions. ## Implementation notes Topology in mathlib heavily uses filters (even more than in Bourbaki). See explanations in `docs/theories/topology.md`. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] * [I. M. James, *Topologies and Uniformities*][james1999] ## Tags topological space, interior, closure, frontier, neighborhood, continuity, continuous function -/ open set filter lattice classical open_locale classical universes u v w /-- A topology on `α`. -/ structure topological_space (α : Type u) := (is_open : set α → Prop) (is_open_univ : is_open univ) (is_open_inter : ∀s t, is_open s → is_open t → is_open (s ∩ t)) (is_open_sUnion : ∀s, (∀t∈s, is_open t) → is_open (⋃₀ s)) attribute [class] topological_space section topological_space variables {α : Type u} {β : Type v} {ι : Sort w} {a : α} {s s₁ s₂ : set α} {p p₁ p₂ : α → Prop} @[ext] lemma topological_space_eq : ∀ {f g : topological_space α}, f.is_open = g.is_open → f = g | ⟨a, _, _, _⟩ ⟨b, _, _, _⟩ rfl := rfl section variables [t : topological_space α] include t /-- `is_open s` means that `s` is open in the ambient topological space on `α` -/ def is_open (s : set α) : Prop := topological_space.is_open t s @[simp] lemma is_open_univ : is_open (univ : set α) := topological_space.is_open_univ t lemma is_open_inter (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∩ s₂) := topological_space.is_open_inter t s₁ s₂ h₁ h₂ lemma is_open_sUnion {s : set (set α)} (h : ∀t ∈ s, is_open t) : is_open (⋃₀ s) := topological_space.is_open_sUnion t s h end lemma is_open_fold {s : set α} {t : topological_space α} : t.is_open s = @is_open α t s := rfl variables [topological_space α] lemma is_open_Union {f : ι → set α} (h : ∀i, is_open (f i)) : is_open (⋃i, f i) := is_open_sUnion $ by rintro _ ⟨i, rfl⟩; exact h i lemma is_open_bUnion {s : set β} {f : β → set α} (h : ∀i∈s, is_open (f i)) : is_open (⋃i∈s, f i) := is_open_Union $ assume i, is_open_Union $ assume hi, h i hi lemma is_open_union (h₁ : is_open s₁) (h₂ : is_open s₂) : is_open (s₁ ∪ s₂) := by rw union_eq_Union; exact is_open_Union (bool.forall_bool.2 ⟨h₂, h₁⟩) @[simp] lemma is_open_empty : is_open (∅ : set α) := by rw ← sUnion_empty; exact is_open_sUnion (assume a, false.elim) lemma is_open_sInter {s : set (set α)} (hs : finite s) : (∀t ∈ s, is_open t) → is_open (⋂₀ s) := finite.induction_on hs (λ _, by rw sInter_empty; exact is_open_univ) $ λ a s has hs ih h, by rw sInter_insert; exact is_open_inter (h _ $ mem_insert _ _) (ih $ λ t, h t ∘ mem_insert_of_mem _) lemma is_open_bInter {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_open (f i)) → is_open (⋂i∈s, f i) := finite.induction_on hs (λ _, by rw bInter_empty; exact is_open_univ) (λ a s has hs ih h, by rw bInter_insert; exact is_open_inter (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_open_Inter [fintype β] {s : β → set α} (h : ∀ i, is_open (s i)) : is_open (⋂ i, s i) := suffices is_open (⋂ (i : β) (hi : i ∈ @univ β), s i), by simpa, is_open_bInter finite_univ (λ i _, h i) lemma is_open_Inter_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_open (s h)) : is_open (Inter s) := by by_cases p; simp * lemma is_open_const {p : Prop} : is_open {a : α | p} := by_cases (assume : p, begin simp only [this]; exact is_open_univ end) (assume : ¬ p, begin simp only [this]; exact is_open_empty end) lemma is_open_and : is_open {a | p₁ a} → is_open {a | p₂ a} → is_open {a | p₁ a ∧ p₂ a} := is_open_inter /-- A set is closed if its complement is open -/ def is_closed (s : set α) : Prop := is_open (-s) @[simp] lemma is_closed_empty : is_closed (∅ : set α) := by unfold is_closed; rw compl_empty; exact is_open_univ @[simp] lemma is_closed_univ : is_closed (univ : set α) := by unfold is_closed; rw compl_univ; exact is_open_empty lemma is_closed_union : is_closed s₁ → is_closed s₂ → is_closed (s₁ ∪ s₂) := λ h₁ h₂, by unfold is_closed; rw compl_union; exact is_open_inter h₁ h₂ lemma is_closed_sInter {s : set (set α)} : (∀t ∈ s, is_closed t) → is_closed (⋂₀ s) := by simp only [is_closed, compl_sInter, sUnion_image]; exact assume h, is_open_Union $ assume t, is_open_Union $ assume ht, h t ht lemma is_closed_Inter {f : ι → set α} (h : ∀i, is_closed (f i)) : is_closed (⋂i, f i ) := is_closed_sInter $ assume t ⟨i, (heq : f i = t)⟩, heq ▸ h i @[simp] lemma is_open_compl_iff {s : set α} : is_open (-s) ↔ is_closed s := iff.rfl @[simp] lemma is_closed_compl_iff {s : set α} : is_closed (-s) ↔ is_open s := by rw [←is_open_compl_iff, compl_compl] lemma is_open_diff {s t : set α} (h₁ : is_open s) (h₂ : is_closed t) : is_open (s \ t) := is_open_inter h₁ $ is_open_compl_iff.mpr h₂ lemma is_closed_inter (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (s₁ ∩ s₂) := by rw [is_closed, compl_inter]; exact is_open_union h₁ h₂ lemma is_closed_bUnion {s : set β} {f : β → set α} (hs : finite s) : (∀i∈s, is_closed (f i)) → is_closed (⋃i∈s, f i) := finite.induction_on hs (λ _, by rw bUnion_empty; exact is_closed_empty) (λ a s has hs ih h, by rw bUnion_insert; exact is_closed_union (h a (mem_insert _ _)) (ih (λ i hi, h i (mem_insert_of_mem _ hi)))) lemma is_closed_Union [fintype β] {s : β → set α} (h : ∀ i, is_closed (s i)) : is_closed (Union s) := suffices is_closed (⋃ (i : β) (hi : i ∈ @univ β), s i), by convert this; simp [set.ext_iff], is_closed_bUnion finite_univ (λ i _, h i) lemma is_closed_Union_prop {p : Prop} {s : p → set α} (h : ∀ h : p, is_closed (s h)) : is_closed (Union s) := by by_cases p; simp * lemma is_closed_imp {p q : α → Prop} (hp : is_open {x | p x}) (hq : is_closed {x | q x}) : is_closed {x | p x → q x} := have {x | p x → q x} = (- {x | p x}) ∪ {x | q x}, from set.ext $ λ x, imp_iff_not_or, by rw [this]; exact is_closed_union (is_closed_compl_iff.mpr hp) hq lemma is_open_neg : is_closed {a | p a} → is_open {a | ¬ p a} := is_open_compl_iff.mpr /-- The interior of a set `s` is the largest open subset of `s`. -/ def interior (s : set α) : set α := ⋃₀ {t | is_open t ∧ t ⊆ s} lemma mem_interior {s : set α} {x : α} : x ∈ interior s ↔ ∃ t ⊆ s, is_open t ∧ x ∈ t := by simp only [interior, mem_set_of_eq, exists_prop, and_assoc, and.left_comm] @[simp] lemma is_open_interior {s : set α} : is_open (interior s) := is_open_sUnion $ assume t ⟨h₁, h₂⟩, h₁ lemma interior_subset {s : set α} : interior s ⊆ s := sUnion_subset $ assume t ⟨h₁, h₂⟩, h₂ lemma interior_maximal {s t : set α} (h₁ : t ⊆ s) (h₂ : is_open t) : t ⊆ interior s := subset_sUnion_of_mem ⟨h₂, h₁⟩ lemma interior_eq_of_open {s : set α} (h : is_open s) : interior s = s := subset.antisymm interior_subset (interior_maximal (subset.refl s) h) lemma interior_eq_iff_open {s : set α} : interior s = s ↔ is_open s := ⟨assume h, h ▸ is_open_interior, interior_eq_of_open⟩ lemma subset_interior_iff_open {s : set α} : s ⊆ interior s ↔ is_open s := by simp only [interior_eq_iff_open.symm, subset.antisymm_iff, interior_subset, true_and] lemma subset_interior_iff_subset_of_open {s t : set α} (h₁ : is_open s) : s ⊆ interior t ↔ s ⊆ t := ⟨assume h, subset.trans h interior_subset, assume h₂, interior_maximal h₂ h₁⟩ lemma interior_mono {s t : set α} (h : s ⊆ t) : interior s ⊆ interior t := interior_maximal (subset.trans interior_subset h) is_open_interior @[simp] lemma interior_empty : interior (∅ : set α) = ∅ := interior_eq_of_open is_open_empty @[simp] lemma interior_univ : interior (univ : set α) = univ := interior_eq_of_open is_open_univ @[simp] lemma interior_interior {s : set α} : interior (interior s) = interior s := interior_eq_of_open is_open_interior @[simp] lemma interior_inter {s t : set α} : interior (s ∩ t) = interior s ∩ interior t := subset.antisymm (subset_inter (interior_mono $ inter_subset_left s t) (interior_mono $ inter_subset_right s t)) (interior_maximal (inter_subset_inter interior_subset interior_subset) $ is_open_inter is_open_interior is_open_interior) lemma interior_union_is_closed_of_interior_empty {s t : set α} (h₁ : is_closed s) (h₂ : interior t = ∅) : interior (s ∪ t) = interior s := have interior (s ∪ t) ⊆ s, from assume x ⟨u, ⟨(hu₁ : is_open u), (hu₂ : u ⊆ s ∪ t)⟩, (hx₁ : x ∈ u)⟩, classical.by_contradiction $ assume hx₂ : x ∉ s, have u \ s ⊆ t, from assume x ⟨h₁, h₂⟩, or.resolve_left (hu₂ h₁) h₂, have u \ s ⊆ interior t, by rwa subset_interior_iff_subset_of_open (is_open_diff hu₁ h₁), have u \ s ⊆ ∅, by rwa h₂ at this, this ⟨hx₁, hx₂⟩, subset.antisymm (interior_maximal this is_open_interior) (interior_mono $ subset_union_left _ _) lemma is_open_iff_forall_mem_open : is_open s ↔ ∀ x ∈ s, ∃ t ⊆ s, is_open t ∧ x ∈ t := by rw ← subset_interior_iff_open; simp only [subset_def, mem_interior] /-- The closure of `s` is the smallest closed set containing `s`. -/ def closure (s : set α) : set α := ⋂₀ {t | is_closed t ∧ s ⊆ t} @[simp] lemma is_closed_closure {s : set α} : is_closed (closure s) := is_closed_sInter $ assume t ⟨h₁, h₂⟩, h₁ lemma subset_closure {s : set α} : s ⊆ closure s := subset_sInter $ assume t ⟨h₁, h₂⟩, h₂ lemma closure_minimal {s t : set α} (h₁ : s ⊆ t) (h₂ : is_closed t) : closure s ⊆ t := sInter_subset_of_mem ⟨h₂, h₁⟩ lemma closure_eq_of_is_closed {s : set α} (h : is_closed s) : closure s = s := subset.antisymm (closure_minimal (subset.refl s) h) subset_closure lemma closure_eq_iff_is_closed {s : set α} : closure s = s ↔ is_closed s := ⟨assume h, h ▸ is_closed_closure, closure_eq_of_is_closed⟩ lemma closure_subset_iff_subset_of_is_closed {s t : set α} (h₁ : is_closed t) : closure s ⊆ t ↔ s ⊆ t := ⟨subset.trans subset_closure, assume h, closure_minimal h h₁⟩ lemma closure_mono {s t : set α} (h : s ⊆ t) : closure s ⊆ closure t := closure_minimal (subset.trans h subset_closure) is_closed_closure lemma is_closed_of_closure_subset {s : set α} (h : closure s ⊆ s) : is_closed s := by rw subset.antisymm subset_closure h; exact is_closed_closure @[simp] lemma closure_empty : closure (∅ : set α) = ∅ := closure_eq_of_is_closed is_closed_empty lemma closure_empty_iff (s : set α) : closure s = ∅ ↔ s = ∅ := begin split; intro h, { rw set.eq_empty_iff_forall_not_mem, intros x H, simpa only [h] using subset_closure H }, { exact (eq.symm h) ▸ closure_empty }, end @[simp] lemma closure_univ : closure (univ : set α) = univ := closure_eq_of_is_closed is_closed_univ @[simp] lemma closure_closure {s : set α} : closure (closure s) = closure s := closure_eq_of_is_closed is_closed_closure @[simp] lemma closure_union {s t : set α} : closure (s ∪ t) = closure s ∪ closure t := subset.antisymm (closure_minimal (union_subset_union subset_closure subset_closure) $ is_closed_union is_closed_closure is_closed_closure) (union_subset (closure_mono $ subset_union_left _ _) (closure_mono $ subset_union_right _ _)) lemma interior_subset_closure {s : set α} : interior s ⊆ closure s := subset.trans interior_subset subset_closure lemma closure_eq_compl_interior_compl {s : set α} : closure s = - interior (- s) := begin unfold interior closure is_closed, rw [compl_sUnion, compl_image_set_of], simp only [compl_subset_compl] end @[simp] lemma interior_compl {s : set α} : interior (- s) = - closure s := by simp [closure_eq_compl_interior_compl] @[simp] lemma closure_compl {s : set α} : closure (- s) = - interior s := by simp [closure_eq_compl_interior_compl] theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ o, is_open o → a ∈ o → o ∩ s ≠ ∅ := ⟨λ h o oo ao os, have s ⊆ -o, from λ x xs xo, @ne_empty_of_mem α (o∩s) x ⟨xo, xs⟩ os, closure_minimal this (is_closed_compl_iff.2 oo) h ao, λ H c ⟨h₁, h₂⟩, classical.by_contradiction $ λ nc, let ⟨x, hc, hs⟩ := exists_mem_of_ne_empty (H _ h₁ nc) in hc (h₂ hs)⟩ lemma dense_iff_inter_open {s : set α} : closure s = univ ↔ ∀ U, is_open U → U ≠ ∅ → U ∩ s ≠ ∅ := begin split ; intro h, { intros U U_op U_ne, cases exists_mem_of_ne_empty U_ne with x x_in, exact mem_closure_iff.1 (by simp only [h]) U U_op x_in }, { apply eq_univ_of_forall, intro x, rw mem_closure_iff, intros U U_op x_in, exact h U U_op (ne_empty_of_mem x_in) }, end lemma dense_of_subset_dense {s₁ s₂ : set α} (h : s₁ ⊆ s₂) (hd : closure s₁ = univ) : closure s₂ = univ := by { rw [← univ_subset_iff, ← hd], exact closure_mono h } /-- The frontier of a set is the set of points between the closure and interior. -/ def frontier (s : set α) : set α := closure s \ interior s lemma frontier_eq_closure_inter_closure {s : set α} : frontier s = closure s ∩ closure (- s) := by rw [closure_compl, frontier, diff_eq] /-- The complement of a set has the same frontier as the original set. -/ @[simp] lemma frontier_compl (s : set α) : frontier (-s) = frontier s := by simp only [frontier_eq_closure_inter_closure, lattice.neg_neg, inter_comm] /-- The frontier of a set is closed. -/ lemma is_closed_frontier {s : set α} : is_closed (frontier s) := by rw frontier_eq_closure_inter_closure; exact is_closed_inter is_closed_closure is_closed_closure /-- The frontier of a set has no interior point. -/ lemma interior_frontier {s : set α} (h : is_closed s) : interior (frontier s) = ∅ := begin have A : frontier s = s \ interior s, by rw [frontier, closure_eq_of_is_closed h], have B : interior (frontier s) ⊆ interior s, by rw A; exact interior_mono (diff_subset _ _), have C : interior (frontier s) ⊆ frontier s := interior_subset, have : interior (frontier s) ⊆ (interior s) ∩ (s \ interior s) := subset_inter B (by simpa [A] using C), rwa [inter_diff_self, subset_empty_iff] at this, end /-- neighbourhood filter -/ def nhds (a : α) : filter α := (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) localized "notation `𝓝` := nhds" in topological_space lemma nhds_def (a : α) : 𝓝 a = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal s) := rfl lemma le_nhds_iff {f a} : f ≤ 𝓝 a ↔ ∀ s : set α, a ∈ s → is_open s → s ∈ f := by simp [nhds_def] lemma nhds_le_of_le {f a} {s : set α} (h : a ∈ s) (o : is_open s) (sf : principal s ≤ f) : 𝓝 a ≤ f := by rw nhds_def; exact infi_le_of_le s (infi_le_of_le ⟨h, o⟩ sf) lemma nhds_sets {a : α} : (𝓝 a).sets = {s | ∃t⊆s, is_open t ∧ a ∈ t} := calc (𝓝 a).sets = (⋃s∈{s : set α| a ∈ s ∧ is_open s}, (principal s).sets) : binfi_sets_eq (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, le_principal_iff.2 (inter_subset_left _ _), le_principal_iff.2 (inter_subset_right _ _)⟩) ⟨univ, mem_univ _, is_open_univ⟩ ... = {s | ∃t⊆s, is_open t ∧ a ∈ t} : le_antisymm (supr_le $ assume i, supr_le $ assume ⟨hi₁, hi₂⟩ t ht, ⟨i, ht, hi₂, hi₁⟩) (assume t ⟨i, hi₁, hi₂, hi₃⟩, mem_Union.2 ⟨i, mem_Union.2 ⟨⟨hi₃, hi₂⟩, hi₁⟩⟩) lemma map_nhds {a : α} {f : α → β} : map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, principal (image f s)) := calc map f (𝓝 a) = (⨅ s ∈ {s : set α | a ∈ s ∧ is_open s}, map f (principal s)) : map_binfi_eq (assume x ⟨hx₁, hx₂⟩ y ⟨hy₁, hy₂⟩, ⟨x ∩ y, ⟨⟨hx₁, hy₁⟩, is_open_inter hx₂ hy₂⟩, le_principal_iff.2 (inter_subset_left _ _), le_principal_iff.2 (inter_subset_right _ _)⟩) ⟨univ, mem_univ _, is_open_univ⟩ ... = _ : by simp only [map_principal] attribute [irreducible] nhds lemma mem_nhds_sets_iff {a : α} {s : set α} : s ∈ 𝓝 a ↔ ∃t⊆s, is_open t ∧ a ∈ t := by simp only [nhds_sets, mem_set_of_eq, exists_prop] lemma mem_of_nhds {a : α} {s : set α} : s ∈ 𝓝 a → a ∈ s := λ H, let ⟨t, ht, _, hs⟩ := mem_nhds_sets_iff.1 H in ht hs lemma mem_nhds_sets {a : α} {s : set α} (hs : is_open s) (ha : a ∈ s) : s ∈ 𝓝 a := mem_nhds_sets_iff.2 ⟨s, subset.refl _, hs, ha⟩ theorem all_mem_nhds (x : α) (P : set α → Prop) (hP : ∀ s t, s ⊆ t → P s → P t) : (∀ s ∈ 𝓝 x, P s) ↔ (∀ s, is_open s → x ∈ s → P s) := iff.intro (λ h s os xs, h s (mem_nhds_sets os xs)) (λ h t, begin change t ∈ (𝓝 x).sets → P t, rw nhds_sets, rintros ⟨s, hs, opens, xs⟩, exact hP _ _ hs (h s opens xs), end) theorem all_mem_nhds_filter (x : α) (f : set α → set β) (hf : ∀ s t, s ⊆ t → f s ⊆ f t) (l : filter β) : (∀ s ∈ 𝓝 x, f s ∈ l) ↔ (∀ s, is_open s → x ∈ s → f s ∈ l) := all_mem_nhds _ _ (λ s t ssubt h, mem_sets_of_superset h (hf s t ssubt)) theorem rtendsto_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.core s ∈ l) := all_mem_nhds_filter _ _ (λ s t, id) _ theorem rtendsto'_nhds {r : rel β α} {l : filter β} {a : α} : rtendsto' r l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → r.preimage s ∈ l) := by { rw [rtendsto'_def], apply all_mem_nhds_filter, apply rel.preimage_mono } theorem ptendsto_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.core s ∈ l) := rtendsto_nhds theorem ptendsto'_nhds {f : β →. α} {l : filter β} {a : α} : ptendsto' f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f.preimage s ∈ l) := rtendsto'_nhds theorem tendsto_nhds {f : β → α} {l : filter β} {a : α} : tendsto f l (𝓝 a) ↔ (∀ s, is_open s → a ∈ s → f ⁻¹' s ∈ l) := all_mem_nhds_filter _ _ (λ s t h, preimage_mono h) _ lemma tendsto_const_nhds {a : α} {f : filter β} : tendsto (λb:β, a) f (𝓝 a) := tendsto_nhds.mpr $ assume s hs ha, univ_mem_sets' $ assume _, ha lemma pure_le_nhds : pure ≤ (𝓝 : α → filter α) := assume a, by rw nhds_def; exact le_infi (assume s, le_infi $ assume ⟨h₁, _⟩, principal_mono.mpr $ singleton_subset_iff.2 h₁) lemma tendsto_pure_nhds {α : Type*} [topological_space β] (f : α → β) (a : α) : tendsto f (pure a) (𝓝 (f a)) := begin rw [tendsto, filter.map_pure], exact pure_le_nhds (f a) end @[simp] lemma nhds_neq_bot {a : α} : 𝓝 a ≠ ⊥ := assume : 𝓝 a = ⊥, have pure a = (⊥ : filter α), from lattice.bot_unique $ this ▸ pure_le_nhds a, pure_neq_bot this lemma interior_eq_nhds {s : set α} : interior s = {a | 𝓝 a ≤ principal s} := set.ext $ λ x, by simp only [mem_interior, le_principal_iff, mem_nhds_sets_iff]; refl lemma mem_interior_iff_mem_nhds {s : set α} {a : α} : a ∈ interior s ↔ s ∈ 𝓝 a := by simp only [interior_eq_nhds, le_principal_iff]; refl lemma is_open_iff_nhds {s : set α} : is_open s ↔ ∀a∈s, 𝓝 a ≤ principal s := calc is_open s ↔ s ⊆ interior s : subset_interior_iff_open.symm ... ↔ (∀a∈s, 𝓝 a ≤ principal s) : by rw [interior_eq_nhds]; refl lemma is_open_iff_mem_nhds {s : set α} : is_open s ↔ ∀a∈s, s ∈ 𝓝 a := is_open_iff_nhds.trans $ forall_congr $ λ _, imp_congr_right $ λ _, le_principal_iff lemma closure_eq_nhds {s : set α} : closure s = {a | 𝓝 a ⊓ principal s ≠ ⊥} := calc closure s = - interior (- s) : closure_eq_compl_interior_compl ... = {a | ¬ 𝓝 a ≤ principal (-s)} : by rw [interior_eq_nhds]; refl ... = {a | 𝓝 a ⊓ principal s ≠ ⊥} : set.ext $ assume a, not_congr (inf_eq_bot_iff_le_compl (show principal s ⊔ principal (-s) = ⊤, by simp only [sup_principal, union_compl_self, principal_univ]) (by simp only [inf_principal, inter_compl_self, principal_empty])).symm theorem mem_closure_iff_nhds {s : set α} {a : α} : a ∈ closure s ↔ ∀ t ∈ 𝓝 a, t ∩ s ≠ ∅ := mem_closure_iff.trans ⟨λ H t ht, subset_ne_empty (inter_subset_inter_left _ interior_subset) (H _ is_open_interior (mem_interior_iff_mem_nhds.2 ht)), λ H o oo ao, H _ (mem_nhds_sets oo ao)⟩ /-- `x` belongs to the closure of `s` if and only if some ultrafilter supported on `s` converges to `x`. -/ lemma mem_closure_iff_ultrafilter {s : set α} {x : α} : x ∈ closure s ↔ ∃ (u : ultrafilter α), s ∈ u.val ∧ u.val ≤ 𝓝 x := begin rw closure_eq_nhds, change 𝓝 x ⊓ principal s ≠ ⊥ ↔ _, symmetry, convert exists_ultrafilter_iff _, ext u, rw [←le_principal_iff, inf_comm, le_inf_iff] end lemma is_closed_iff_nhds {s : set α} : is_closed s ↔ ∀a, 𝓝 a ⊓ principal s ≠ ⊥ → a ∈ s := calc is_closed s ↔ closure s = s : by rw [closure_eq_iff_is_closed] ... ↔ closure s ⊆ s : ⟨assume h, by rw h, assume h, subset.antisymm h subset_closure⟩ ... ↔ (∀a, 𝓝 a ⊓ principal s ≠ ⊥ → a ∈ s) : by rw [closure_eq_nhds]; refl lemma closure_inter_open {s t : set α} (h : is_open s) : s ∩ closure t ⊆ closure (s ∩ t) := assume a ⟨hs, ht⟩, have s ∈ 𝓝 a, from mem_nhds_sets h hs, have 𝓝 a ⊓ principal s = 𝓝 a, from inf_of_le_left $ by rwa le_principal_iff, have 𝓝 a ⊓ principal (s ∩ t) ≠ ⊥, from calc 𝓝 a ⊓ principal (s ∩ t) = 𝓝 a ⊓ (principal s ⊓ principal t) : by rw inf_principal ... = 𝓝 a ⊓ principal t : by rw [←inf_assoc, this] ... ≠ ⊥ : by rw [closure_eq_nhds] at ht; assumption, by rw [closure_eq_nhds]; assumption lemma closure_diff {s t : set α} : closure s - closure t ⊆ closure (s - t) := calc closure s \ closure t = (- closure t) ∩ closure s : by simp only [diff_eq, inter_comm] ... ⊆ closure (- closure t ∩ s) : closure_inter_open $ is_open_compl_iff.mpr $ is_closed_closure ... = closure (s \ closure t) : by simp only [diff_eq, inter_comm] ... ⊆ closure (s \ t) : closure_mono $ diff_subset_diff (subset.refl s) subset_closure lemma mem_of_closed_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (𝓝 a)) (hs : is_closed s) (h : f ⁻¹' s ∈ b) : a ∈ s := have b.map f ≤ 𝓝 a ⊓ principal s, from le_trans (le_inf (le_refl _) (le_principal_iff.mpr h)) (inf_le_inf hf (le_refl _)), is_closed_iff_nhds.mp hs a $ neq_bot_of_le_neq_bot (map_ne_bot hb) this lemma mem_of_closed_of_tendsto' {f : β → α} {x : filter β} {a : α} {s : set α} (hf : tendsto f x (𝓝 a)) (hs : is_closed s) (h : x ⊓ principal (f ⁻¹' s) ≠ ⊥) : a ∈ s := is_closed_iff_nhds.mp hs _ $ neq_bot_of_le_neq_bot (@map_ne_bot _ _ _ f h) $ le_inf (le_trans (map_mono $ inf_le_left) hf) $ le_trans (map_mono $ inf_le_right_of_le $ by simp only [comap_principal, le_principal_iff]; exact subset.refl _) (@map_comap_le _ _ _ f) lemma mem_closure_of_tendsto {f : β → α} {b : filter β} {a : α} {s : set α} (hb : b ≠ ⊥) (hf : tendsto f b (𝓝 a)) (h : f ⁻¹' s ∈ b) : a ∈ closure s := mem_of_closed_of_tendsto hb hf (is_closed_closure) $ filter.mem_sets_of_superset h (preimage_mono subset_closure) section lim variables [inhabited α] /-- If `f` is a filter, then `lim f` is a limit of the filter, if it exists. -/ noncomputable def lim (f : filter α) : α := epsilon $ λa, f ≤ 𝓝 a lemma lim_spec {f : filter α} (h : ∃a, f ≤ 𝓝 a) : f ≤ 𝓝 (lim f) := epsilon_spec h end lim /- locally finite family [General Topology (Bourbaki, 1995)] -/ section locally_finite /-- A family of sets in `set α` is locally finite if at every point `x:α`, there is a neighborhood of `x` which meets only finitely many sets in the family -/ def locally_finite (f : β → set α) := ∀x:α, ∃t ∈ 𝓝 x, finite {i | f i ∩ t ≠ ∅ } lemma locally_finite_of_finite {f : β → set α} (h : finite (univ : set β)) : locally_finite f := assume x, ⟨univ, univ_mem_sets, finite_subset h $ subset_univ _⟩ lemma locally_finite_subset {f₁ f₂ : β → set α} (hf₂ : locally_finite f₂) (hf : ∀b, f₁ b ⊆ f₂ b) : locally_finite f₁ := assume a, let ⟨t, ht₁, ht₂⟩ := hf₂ a in ⟨t, ht₁, finite_subset ht₂ $ assume i hi, neq_bot_of_le_neq_bot hi $ inter_subset_inter (hf i) $ subset.refl _⟩ lemma is_closed_Union_of_locally_finite {f : β → set α} (h₁ : locally_finite f) (h₂ : ∀i, is_closed (f i)) : is_closed (⋃i, f i) := is_open_iff_nhds.mpr $ assume a, assume h : a ∉ (⋃i, f i), have ∀i, a ∈ -f i, from assume i hi, h $ mem_Union.2 ⟨i, hi⟩, have ∀i, - f i ∈ (𝓝 a).sets, by rw [nhds_sets]; exact assume i, ⟨- f i, subset.refl _, h₂ i, this i⟩, let ⟨t, h_sets, (h_fin : finite {i | f i ∩ t ≠ ∅ })⟩ := h₁ a in calc 𝓝 a ≤ principal (t ∩ (⋂ i∈{i | f i ∩ t ≠ ∅ }, - f i)) : begin rw [le_principal_iff], apply @filter.inter_mem_sets _ (𝓝 a) _ _ h_sets, apply @filter.Inter_mem_sets _ (𝓝 a) _ _ _ h_fin, exact assume i h, this i end ... ≤ principal (- ⋃i, f i) : begin simp only [principal_mono, subset_def, mem_compl_eq, mem_inter_eq, mem_Inter, mem_set_of_eq, mem_Union, and_imp, not_exists, not_eq_empty_iff_exists, exists_imp_distrib, (≠)], exact assume x xt ht i xfi, ht i x xfi xt xfi end end locally_finite end topological_space section continuous variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] open_locale topological_space /-- A function between topological spaces is continuous if the preimage of every open set is open. -/ def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s) /-- A function between topological spaces is continuous at a point `x₀` if `f x` tends to `f x₀` when `x` tends to `x₀`. -/ def continuous_at (f : α → β) (x : α) := tendsto f (𝓝 x) (𝓝 (f x)) lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝 x := h ht lemma continuous_id : continuous (id : α → α) := assume s h, h lemma continuous.comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) : continuous (g ∘ f) := assume s h, hf _ (hg s h) lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α} (hg : continuous_at g (f x)) (hf : continuous_at f x) : continuous_at (g ∘ f) x := hg.comp hf lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (𝓝 x) (𝓝 (f x)) | s := show s ∈ 𝓝 (f x) → s ∈ map f (𝓝 x), by simp [nhds_sets]; exact assume t t_subset t_open fx_in_t, ⟨f ⁻¹' t, preimage_mono t_subset, hf t t_open, fx_in_t⟩ lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) : continuous_at f x := h.tendsto x lemma continuous_iff_continuous_at {f : α → β} : continuous f ↔ ∀ x, continuous_at f x := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (𝓝 x) (𝓝 (f x)), assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ 𝓝 (f a), by simp [nhds_sets]; exact assume a ha, ⟨s, subset.refl s, hs, ha⟩, show is_open (f ⁻¹' s), by simp [is_open_iff_nhds]; exact assume a ha, hf a (this a ha)⟩ lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_continuous_at.mpr $ assume a, tendsto_const_nhds lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, hf (-s) hs, assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma continuous_at_iff_ultrafilter {f : α → β} (x) : continuous_at f x ↔ ∀ g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) := tendsto_iff_ultrafilter f (𝓝 x) (𝓝 (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ 𝓝 x → g.map f ≤ 𝓝 (f x) := by simp only [continuous_iff_continuous_at, continuous_at_iff_ultrafilter] /-- A piecewise defined function `if p then f else g` is continuous, if both `f` and `g` are continuous, and they coincide on the frontier (boundary) of the set `{a | p a}`. -/ lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, classical.by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ - frontier {a | p a}, classical.by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure (- {a | p a}), from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) /- Continuity and partial functions -/ /-- Continuity of a partial function -/ def pcontinuous (f : α →. β) := ∀ s, is_open s → is_open (f.preimage s) lemma open_dom_of_pcontinuous {f : α →. β} (h : pcontinuous f) : is_open f.dom := by rw [←pfun.preimage_univ]; exact h _ is_open_univ lemma pcontinuous_iff' {f : α →. β} : pcontinuous f ↔ ∀ {x y} (h : y ∈ f x), ptendsto' f (𝓝 x) (𝓝 y) := begin split, { intros h x y h', rw [ptendsto'_def], change ∀ (s : set β), s ∈ (𝓝 y).sets → pfun.preimage f s ∈ (𝓝 x).sets, rw [nhds_sets, nhds_sets], rintros s ⟨t, tsubs, opent, yt⟩, exact ⟨f.preimage t, pfun.preimage_mono _ tsubs, h _ opent, ⟨y, yt, h'⟩⟩ }, intros hf s os, rw is_open_iff_nhds, rintros x ⟨y, ys, fxy⟩ t, rw [mem_principal_sets], assume h : f.preimage s ⊆ t, change t ∈ 𝓝 x, apply mem_sets_of_superset _ h, have h' : ∀ s ∈ 𝓝 y, f.preimage s ∈ 𝓝 x, { intros s hs, have : ptendsto' f (𝓝 x) (𝓝 y) := hf fxy, rw ptendsto'_def at this, exact this s hs }, show f.preimage s ∈ 𝓝 x, apply h', rw mem_nhds_sets_iff, exact ⟨s, set.subset.refl _, os, ys⟩ end lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := have ∀ (a : α), 𝓝 a ⊓ principal s ≠ ⊥ → 𝓝 (f a) ⊓ principal (f '' s) ≠ ⊥, from assume a ha, have h₁ : ¬ map f (𝓝 a ⊓ principal s) = ⊥, by rwa[map_eq_bot_iff], have h₂ : map f (𝓝 a ⊓ principal s) ≤ 𝓝 (f a) ⊓ principal (f '' s), from le_inf (le_trans (map_mono inf_le_left) $ by rw [continuous_iff_continuous_at] at h; exact h a) (le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _), neq_bot_of_le_neq_bot h₁ h₂, by simp [image_subset_iff, closure_eq_nhds]; assumption lemma mem_closure {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $ (mem_image_of_mem f ha) end continuous
3670ee2bcc4d112d59cb7420aa041d2eaa69f9c5
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/AuxRecursor.lean
3381a656c8764d065069d88a4b0e42ba2220cbcf
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach" ]
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
2,336
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Environment namespace Lean def casesOnSuffix := "casesOn" def recOnSuffix := "recOn" def brecOnSuffix := "brecOn" def binductionOnSuffix := "binductionOn" def belowSuffix := "below" def mkCasesOnName (indDeclName : Name) : Name := Name.mkStr indDeclName casesOnSuffix def mkRecOnName (indDeclName : Name) : Name := Name.mkStr indDeclName recOnSuffix def mkBRecOnName (indDeclName : Name) : Name := Name.mkStr indDeclName brecOnSuffix def mkBInductionOnName (indDeclName : Name) : Name := Name.mkStr indDeclName binductionOnSuffix def mkBelowName (indDeclName : Name) : Name := Name.mkStr indDeclName belowSuffix builtin_initialize auxRecExt : TagDeclarationExtension ← mkTagDeclarationExtension `auxRec @[export lean_mark_aux_recursor] def markAuxRecursor (env : Environment) (declName : Name) : Environment := auxRecExt.tag env declName @[export lean_is_aux_recursor] def isAuxRecursor (env : Environment) (declName : Name) : Bool := auxRecExt.isTagged env declName -- TODO: use `markAuxRecursor` when they are defined -- An attribute is not a good solution since we don't want users to control what is tagged as an auxiliary recursor. || declName == ``Eq.ndrec || declName == ``Eq.ndrecOn def isAuxRecursorWithSuffix (env : Environment) (declName : Name) (suffix : Name) : Bool := match declName with | .str _ s => s == suffix && isAuxRecursor env declName | _ => false def isCasesOnRecursor (env : Environment) (declName : Name) : Bool := isAuxRecursorWithSuffix env declName casesOnSuffix def isRecOnRecursor (env : Environment) (declName : Name) : Bool := isAuxRecursorWithSuffix env declName recOnSuffix def isBRecOnRecursor (env : Environment) (declName : Name) : Bool := isAuxRecursorWithSuffix env declName brecOnSuffix builtin_initialize noConfusionExt : TagDeclarationExtension ← mkTagDeclarationExtension `noConf @[export lean_mark_no_confusion] def markNoConfusion (env : Environment) (n : Name) : Environment := noConfusionExt.tag env n @[export lean_is_no_confusion] def isNoConfusion (env : Environment) (n : Name) : Bool := noConfusionExt.isTagged env n end Lean
6d78e1b050b865f5df3c78110ca3850bffa97af4
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/frontend1.lean
fb97e67bd723dd2506c111f86a315a78c7380201
[ "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
7,438
lean
import Lean.Elab new_frontend open Lean open Lean.Elab def runM (input : String) (failIff : Bool := true) : CoreM Unit := do let env ← getEnv; let opts ← getOptions; let (env, messages) ← liftIO $ process input env opts; messages.forM $ fun msg => (liftIO msg.toString) >>= IO.println; when (failIff && messages.hasErrors) $ throwError "errors have been found"; when (!failIff && !messages.hasErrors) $ throwError "there are no errors"; pure () def fail (input : String) : CoreM Unit := runM input false def M := IO Unit def zero := 0 def one := 1 def two := 2 def hello : String := "hello" def act1 : IO String := pure "hello" #eval runM "#check @HasAdd.add" #eval runM "#check [zero, one, two]" #eval runM "#check id $ Nat.succ one" #eval runM "#check HasAdd.add one two" #eval runM "#check one + two > one ∧ True" #eval runM "#check act1 >>= IO.println" #eval runM "#check one + two == one" #eval fail "#check one + one + hello == hello ++ one" #eval runM "#check Nat.add one $ Nat.add one two" #eval runM "universe u universe v export HasToString (toString) section namespace foo.bla end bla end foo variable (p q : Prop) variable (_ b : _) variable {α : Type} variable {m : Type → Type} variable [Monad m] #check m #check Type #check Prop #check toString zero #check id Nat.zero (α := Nat) #check id _ (α := Nat) #check id Nat.zero #check id (α := Nat) #check @id Nat #check p #check α #check Nat.succ #check Nat.add #check id #check forall (α : Type), α → α #check (α : Type) → α → α #check {α : Type} → {β : Type} → M → (α → β) → α → β #check () end" structure S1 := (x y : Nat := 0) structure S2 extends S1 := (z : Nat := 0) def fD {α} [HasToString α] (a b : α) : String := toString a ++ toString b structure S3 := (w : Nat := 0) (f : forall {α : Type} [HasToString α], α → α → String := @fD) structure S4 extends S2, S3 := (s : Nat := 0) def s4 : S4 := {} structure S (α : Type) := (field1 : S4 := {}) (field2 : S4 × S4 := ({}, {})) (field3 : α) (field4 : List α × Nat := ([], 0)) (vec : Array (α × α) := #[]) (map : Std.HashMap String α := {}) inductive D (α : Type) | mk (a : α) (s : S4) : D α def s : S Nat := { field3 := 0 } def d : D Nat := D.mk 10 {} def i : Nat := 10 def k : String := "hello" universes u class Monoid (α : Type u) := (one : α) (mul : α → α → α) def m : Monoid Nat := { one := 1, mul := Nat.mul } def f (x y z : Nat) : Nat := x + y + z #eval runM "#check s4.x" #eval runM "#check s.field1.x" #eval runM "#check s.field2.fst" #eval runM "#check s.field2.fst.w" #eval runM "#check s.1.x" #eval runM "#check s.2.1.x" #eval runM "#check d.1" #eval runM "#check d.2.x" #eval runM "#check s4.f s4.x" #eval runM "#check m.mul m.one" #eval runM "#check s.field4.1.length.succ" #eval runM "#check s.field4.1.map Nat.succ" #eval runM "#check s.vec[i].1" #eval runM "#check \"hello\"" #eval runM "#check 1" #eval runM "#check Nat.succ 1" #eval runM "#check fun _ a (x y : Int) => x + y + a" #eval runM "#check (one)" #eval runM "#check ()" #eval runM "#check (one, two, zero)" #eval runM "#check (one, two, zero)" #eval runM "#check (1 : Int)" #eval runM "#check ((1, 2) : Nat × Int)" #eval runM "#check (· + one)" #eval runM "#check (· + · : Nat → Nat → Nat)" #eval runM "#check (f one · zero)" #eval runM "#check (f · · zero)" #eval runM "#check fun (_ b : Nat) => b + 1" def foo {α β} (a : α) (b : β) (a' : α) : α := a def bla {α β} (f : α → β) (a : α) : β := f a -- #check fun x => foo x x.w s4 -- fails in old elaborator -- #check bla (fun x => x.w) s4 -- fails in the old elaborator -- #check #[1, 2, 3].foldl (fun r a => r.push a) #[] -- fails in the old elaborator #eval runM "#check fun x => foo x x.w s4" #eval runM "#check bla (fun x => x.w) s4" #eval runM "#check #[1, 2, 3].foldl (fun r a => r.push a) #[]" #eval runM "#check #[1, 2, 3].foldl (fun r a => (r.push a).push a) #[]" #eval runM "#check #[1, 2, 3].foldl (fun r a => ((r.push a).push a).push a) #[]" #eval runM "#check #[].push one $.push two $.push zero $.size.succ" #eval runM "#check #[1, 2].foldl (fun r a => r.push a $.push a $.push a) #[]" #eval runM "#check #[1, 2].foldl (init := #[]) $ fun r a => r.push a $.push a" #eval runM "#check let x := one + zero; x + x" -- set_option trace.Elab true #eval runM "#check (fun x => let v := x.w; v + v) s4" #eval runM "#check fun x => foo x (let v := x.w; v + one) s4" #eval runM "#check fun x => foo x (let v := x.w; let w := x.x; v + w + one) s4" #eval fail "#check id.{1,1}" #eval fail "#check @id.{0} Nat" #eval runM "#check @id.{1} Nat" #eval runM "universes u #check id.{u}" #eval fail "universes u #check id.{v}" #eval runM "universes u #check Type u" #eval runM "universes u #check Sort u" #eval runM "#check Type 1" #eval runM "#check Type 0" #eval runM "universes u v #check Sort (max u v)" #eval runM "universes u v #check Type (max u v)" #eval runM "#check 'a'" #eval fail "#check #['a', \"hello\"]" #eval runM "#check fun (a : Array Nat) => a.size" #eval runM "#check if 0 = 1 then 'a' else 'b'" #eval runM "#check fun (i : Nat) (a : Array Nat) => if h : i < a.size then a.get (Fin.mk i h) else i" #eval runM "#check { x : Nat // x > 0 }" #eval runM "#check { x // x > 0 }" #eval runM "#check fun (i : Nat) (a : Array Nat) => if h : i < a.size then a.get ⟨i, h⟩ else i" #eval runM "#check Prod.fst ⟨1, 2⟩" #eval runM "#check let x := ⟨1, 2⟩; Prod.fst x" #eval runM "#check show Nat from 1" #eval runM "#check show Int from 1" #eval runM "#check have Nat from one + zero; this + this" #eval runM "#check have x : Nat from one + zero; x + x" #eval runM "#check have Nat := one + zero; this + this" #eval runM "#check have x : Nat := one + zero; x + x" #eval runM "#check x + y where x := 1; where y := x + x" #eval runM "#check let z := 2; x + y where x := z + 1; where y := x + x" #eval runM "variables {α β} axiom x (n : Nat) : α → α #check x 1 0" #eval runM "#check HasToString.toString 0" #eval runM "@[instance] axiom newInst : HasToString Nat #check newInst #check HasToString.toString 0" #eval runM "variables {β σ} universes w1 w2 /-- Testing axiom -/ unsafe axiom Nat.aux (γ : Type w1) (v : Nat) : β → (α : Type _) → v = zero /- Nat.zero -/ → Array α #check @Nat.aux" #eval runM "def x : Nat := Nat.zero #check x" #eval runM "def x := Nat.zero #check x" #eval runM "open Lean.Parser def x := parser! symbol \"foo\" #check x" #eval runM "open Lean.Parser def x := parser!:50 symbol \"foo\" #check x" #eval runM "open Lean.Parser def x := tparser! symbol \"foo\" #check x" #eval runM "def x : Nat := 1 #check x" def g (x : Nat := zero) (y : Nat := one) (z : Nat := two) : Nat := x + y + z def three := 3 #eval runM "#check g" #eval runM "#check g (x := three) (z := zero)" #eval runM "#check g (y := three)" #eval runM "#check g (z := three)" #eval runM "#check g three (z := zero)" #eval runM "open Lean.Parser @[termParser] def myParser : Lean.Parser.Parser := parser! oldCoe \"hi\" #check myParser" #eval runM "#check (fun stx => if True then let e := stx; HasPure.pure e else HasPure.pure stx : Nat → Id Nat)" #eval runM "constant n : Nat #check n" #eval fail "#check Nat.succ 'a'" #eval runM "universes u v #check Type (max u v)" #eval runM "universes u v #check Type (imax u v)" #eval fail "namespace Boo def f (x : Nat) := x def s := 'a' #check (fun x => f x) s end Boo"
b076183c9b6645d59a86790575920455b38c71da
a721fe7446524f18ba361625fc01033d9c8b7a78
/src/principia/myint/basic.lean
806a35aa5f4e103b5a0f3b41ee846131b14232f3
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
7,972
lean
-- vim: ts=2 sw=0 sts=-1 et ai tw=70 import ..logic import ..mynat.basic import ..mynat.le import ..mynat.nat_sub import .int_pair import ..myring.integral_domain namespace hidden open mynat def myint := quotient int_pair.int_pair.setoid namespace myint instance: decidable_eq myint := quotient.decidable_eq instance: has_coe mynat myint := ⟨λ n, ⟦⟨n, 0⟩⟧⟩ theorem coe_nat_def (a: mynat): (↑a: myint) = ⟦⟨a, 0⟩⟧ := rfl instance: has_zero myint := ⟨(0: mynat)⟩ instance: has_one myint := ⟨(1: mynat)⟩ theorem int_zero: (0: myint) = ⟦0⟧ := rfl theorem int_one: (1: myint) = ⟦1⟧ := rfl theorem coe_zero : ↑(0 : mynat) = (0 : myint) := rfl theorem coe_one : ↑(1 : mynat) = (1 : myint) := rfl def neg: myint → myint := quotient.lift (λ n, ⟦-n⟧) int_pair.neg_well_defined instance: has_neg myint := ⟨neg⟩ instance has_neg2: has_neg (quotient int_pair.int_pair.setoid) := ⟨neg⟩ private theorem neg_eq_cls {x: int_pair.int_pair}: -⟦x⟧ = ⟦-x⟧ := rfl def add: myint → myint → myint := quotient.lift₂ (λ n m, ⟦n + m⟧) int_pair.add_well_defined instance: has_add myint := ⟨add⟩ -- so that lean knows what ⟦x⟧ + ⟦y⟧ is instance has_add2: has_add (quotient int_pair.int_pair.setoid) := ⟨add⟩ -- this theorem is a bit of an antique. Really -- it and theorems like it can just be replaced -- with invocations of `change`, but it can be -- nice (and stabler) sometimes to not have to write -- out the entire target all the time private theorem add_eq_cls {x y: int_pair.int_pair}: ⟦x⟧ + ⟦y⟧ = ⟦x + y⟧ := rfl def sub (n m: myint): myint := n + -m instance: has_sub myint := ⟨sub⟩ theorem sub_def (n m: myint): n - m = n + -m := rfl def mul: myint → myint → myint := quotient.lift₂ (λ n m, ⟦n * m⟧) int_pair.mul_well_defined instance: has_mul myint := ⟨mul⟩ instance has_mul2: has_mul (quotient int_pair.int_pair.setoid) := ⟨mul⟩ private theorem mul_eq_cls {x y: int_pair.int_pair}: ⟦x⟧ * ⟦y⟧ = ⟦x * y⟧ := rfl theorem nat_nat_mul {x y: mynat}: (↑x: myint) * ↑y = ↑(x * y) := begin apply quotient.sound, rw int_pair.setoid_equiv, simp, end universe u -- a decidable relation lifted to a quotient type is decidable -- This shouldn't be here... def quotient_decidable_rel {α : Sort u} {s : setoid α} (rel: α → α → Prop) (wd: ∀ n m x y: α, n ≈ x → m ≈ y → (rel n m = rel x y)) [dr : ∀ a b : α, decidable (rel a b)]: ∀ a b: quotient s, decidable (quotient.lift₂ rel wd a b) := λ q₁ q₂ : quotient s, quotient.rec_on_subsingleton₂ q₁ q₂ dr variables m n k: myint variables a b c: mynat lemma of_nat_zero: ↑(0: mynat) = (0: myint) := rfl lemma of_nat_one: ↑(1: mynat) = (1: myint) := rfl theorem zero_nat: (↑(0: mynat): myint) = 0 := rfl theorem one_nat: (↑(1:mynat):myint) = 1 := rfl @[simp] theorem of_nat_cancel: (↑a: myint) = ↑b ↔ a = b := begin repeat {rw coe_nat_def}, split, { assume hab, from quotient.exact hab, }, { assume hab, rw hab, }, end @[simp] theorem coe_succ: (↑(succ a): myint) = ↑a + 1 := rfl @[simp] theorem coe_coe_add: (↑a: myint) + ↑b = ↑(a + b) := rfl theorem add_one_succ: (↑a: myint) + 1 = ↑(succ a) := rfl @[simp] theorem coe_coe_mul : (↑a : myint) * ↑b = ↑(a * b) := begin repeat { rw coe_nat_def, }, rw mul_eq_cls, apply congr_arg, rw int_pair.eq_iff_split, simp, -- awsome :o end private lemma neq_iff_not_eq: m ≠ n ↔ ¬(m = n) := begin split; assume hneq heq, all_goals { contradiction }, end private lemma succ_times_succ_nzero: (succ a) * (succ b) ≠ 0 := begin assume h, have hsan0 : succ a ≠ 0, assume h₁, from mynat.no_confusion h₁, have hsbn0 : succ b ≠ 0, assume h₁, from mynat.no_confusion h₁, from hsbn0 (mynat.mul_integral hsan0 h), end -- hmmmmmm private lemma mul_integral_biased {m n : myint}: m ≠ 0 → m * n = 0 → n = 0 := begin cases quotient.exists_rep m with a ha, subst ha, cases quotient.exists_rep n with b hb, subst hb, repeat {rw mul_eq_cls <|> rw int_zero}, assume haneq0 hab0, rw int_pair.sound_exact_iff at hab0, rw int_pair.setoid_equiv at hab0, simp at hab0, repeat {rw int_pair.sound_exact_iff <|> rw int_pair.setoid_equiv}, simp, cases (le_total_order a.a a.b) with ha ha; cases ha with d hd, { rw hd at hab0, simp at hab0, rw ←mynat.add_assoc at hab0, rw mynat.add_comm (a.a * b.a) at hab0, rw mynat.add_assoc at hab0, have := mynat.add_cancel (mynat.add_cancel hab0), apply @mynat.mul_cancel d _ _, assume hd0, apply haneq0, apply quotient.sound, rw int_pair.setoid_equiv, rw hd, rw hd0, simp, symmetry, assumption, }, { rw hd at hab0, simp at hab0, conv at hab0 { to_rhs, rw mynat.add_comm, }, rw mynat.add_assoc at hab0, have hw := mynat.add_cancel hab0, rw mynat.add_comm at hw, have := mynat.add_cancel hw, apply @mynat.mul_cancel d _ _, assume hd0, apply haneq0, apply quotient.sound, rw int_pair.setoid_equiv, rw hd, rw hd0, simp, assumption, }, end theorem nontrivial: (0: myint) ≠ 1 := begin assume h01, rw int_zero at h01, rw int_one at h01, rw int_pair.sound_exact_iff at h01, rw int_pair.setoid_equiv at h01, cases h01, end -- theorem mul_nonzero_nonzero : m * n ≠ 0 ↔ m ≠ 0 ∧ n ≠ 0 := -- begin -- split; assume h, { -- have : 0 = (0 : myint) := rfl, -- split, all_goals { -- assume h0, -- subst h0, -- }, -- rw zero_mul at h, -- contradiction, -- rw mul_zero at h, -- contradiction, -- }, { -- assume hmn0, -- cases mul_integral hmn0 with hn0 hm0, -- from h.right hn0, -- from h.left hm0, -- }, -- end instance: myring myint := ⟨ by apply_instance, λ m n k: myint, begin cases quotient.exists_rep m with a ha, subst ha, cases quotient.exists_rep n with b hb, subst hb, cases quotient.exists_rep k with c hc, subst hc, apply congr_arg quotient.mk, rw int_pair.eq_iff_split, simp, split; ac_refl, end, λ m: myint, begin cases quotient.exists_rep m with a ha, subst ha, rw int_zero, apply congr_arg quotient.mk, rw int_pair.eq_iff_split, simp, end, λ m: myint, begin cases quotient.exists_rep m with a ha, subst ha, rw int_zero, rw neg_eq_cls, rw add_eq_cls, apply quotient.sound, rw int_pair.setoid_equiv, simp, rw mynat.add_comm, end, λ m n k: myint, begin cases quotient.exists_rep m with a ha, subst ha, cases quotient.exists_rep n with b hb, subst hb, cases quotient.exists_rep k with c hc, subst hc, apply congr_arg quotient.mk, rw int_pair.eq_iff_split, simp, split, { -- ac_refl takes too long without a little kick-start repeat {rw mynat.add_assoc <|> rw mynat.mul_assoc}, apply congr_arg, ac_refl, }, { repeat {rw mynat.add_assoc <|> rw mynat.mul_assoc}, apply congr_arg, ac_refl, }, end, λ m n: myint, begin cases quotient.exists_rep m with a ha, subst ha, cases quotient.exists_rep n with b hb, subst hb, apply congr_arg quotient.mk, rw int_pair.eq_iff_split, simp, split; ac_refl, end, λ m: myint, begin cases quotient.exists_rep m with a ha, subst ha, rw int_one, apply congr_arg quotient.mk, rw int_pair.eq_iff_split, simp, end, λ m n k: myint, begin cases quotient.exists_rep m with a ha, subst ha, cases quotient.exists_rep n with b hb, subst hb, cases quotient.exists_rep k with c hc, subst hc, apply congr_arg quotient.mk, rw int_pair.eq_iff_split, simp, split; ac_refl, end⟩ instance: integral_domain myint := ⟨begin intros a b ha h, apply mul_integral_biased ha, rwa myring.mul_comm, end⟩ end myint end hidden
f2acab20d2f2fdccf0d2e715fe75953269754ab9
94e33a31faa76775069b071adea97e86e218a8ee
/src/deprecated/ring.lean
7ef81059d04503c655154ffe8f640b8616da6a49
[ "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
4,991
lean
/- Copyright (c) 2020 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import deprecated.group /-! # Unbundled semiring and ring homomorphisms (deprecated) This file is deprecated, and is no longer imported by anything in mathlib other than other deprecated files, and test files. You should not need to import it. This file defines predicates for unbundled semiring and ring homomorphisms. Instead of using this file, please use `ring_hom`, defined in `algebra.hom.ring`, with notation `→+*`, for morphisms between semirings or rings. For example use `φ : A →+* B` to represent a ring homomorphism. ## Main Definitions `is_semiring_hom` (deprecated), `is_ring_hom` (deprecated) ## Tags is_semiring_hom, is_ring_hom -/ universes u v w variable {α : Type u} /-- Predicate for semiring homomorphisms (deprecated -- use the bundled `ring_hom` version). -/ structure is_semiring_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) : Prop := (map_zero [] : f 0 = 0) (map_one [] : f 1 = 1) (map_add [] : ∀ {x y}, f (x + y) = f x + f y) (map_mul [] : ∀ {x y}, f (x * y) = f x * f y) namespace is_semiring_hom variables {β : Type v} [semiring α] [semiring β] variables {f : α → β} (hf : is_semiring_hom f) {x y : α} /-- The identity map is a semiring homomorphism. -/ lemma id : is_semiring_hom (@id α) := by refine {..}; intros; refl /-- The composition of two semiring homomorphisms is a semiring homomorphism. -/ lemma comp (hf : is_semiring_hom f) {γ} [semiring γ] {g : β → γ} (hg : is_semiring_hom g) : is_semiring_hom (g ∘ f) := { map_zero := by simpa [map_zero hf] using map_zero hg, map_one := by simpa [map_one hf] using map_one hg, map_add := λ x y, by simp [map_add hf, map_add hg], map_mul := λ x y, by simp [map_mul hf, map_mul hg] } /-- A semiring homomorphism is an additive monoid homomorphism. -/ lemma to_is_add_monoid_hom (hf : is_semiring_hom f) : is_add_monoid_hom f := { ..‹is_semiring_hom f› } /-- A semiring homomorphism is a monoid homomorphism. -/ lemma to_is_monoid_hom (hf : is_semiring_hom f) : is_monoid_hom f := { ..‹is_semiring_hom f› } end is_semiring_hom /-- Predicate for ring homomorphisms (deprecated -- use the bundled `ring_hom` version). -/ structure is_ring_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) : Prop := (map_one [] : f 1 = 1) (map_mul [] : ∀ {x y}, f (x * y) = f x * f y) (map_add [] : ∀ {x y}, f (x + y) = f x + f y) namespace is_ring_hom variables {β : Type v} [ring α] [ring β] /-- A map of rings that is a semiring homomorphism is also a ring homomorphism. -/ lemma of_semiring {f : α → β} (H : is_semiring_hom f) : is_ring_hom f := {..H} variables {f : α → β} (hf : is_ring_hom f) {x y : α} /-- Ring homomorphisms map zero to zero. -/ lemma map_zero (hf : is_ring_hom f) : f 0 = 0 := calc f 0 = f (0 + 0) - f 0 : by rw [hf.map_add]; simp ... = 0 : by simp /-- Ring homomorphisms preserve additive inverses. -/ lemma map_neg (hf : is_ring_hom f) : f (-x) = -f x := calc f (-x) = f (-x + x) - f x : by rw [hf.map_add]; simp ... = -f x : by simp [hf.map_zero] /-- Ring homomorphisms preserve subtraction. -/ lemma map_sub (hf : is_ring_hom f) : f (x - y) = f x - f y := by simp [sub_eq_add_neg, hf.map_add, hf.map_neg] /-- The identity map is a ring homomorphism. -/ lemma id : is_ring_hom (@id α) := by refine {..}; intros; refl /-- The composition of two ring homomorphisms is a ring homomorphism. -/ -- see Note [no instance on morphisms] lemma comp (hf : is_ring_hom f) {γ} [ring γ] {g : β → γ} (hg : is_ring_hom g) : is_ring_hom (g ∘ f) := { map_add := λ x y, by simp [map_add hf]; rw map_add hg; refl, map_mul := λ x y, by simp [map_mul hf]; rw map_mul hg; refl, map_one := by simp [map_one hf]; exact map_one hg } /-- A ring homomorphism is also a semiring homomorphism. -/ lemma to_is_semiring_hom (hf : is_ring_hom f) : is_semiring_hom f := { map_zero := map_zero hf, ..‹is_ring_hom f› } lemma to_is_add_group_hom (hf : is_ring_hom f) : is_add_group_hom f := { map_add := hf.map_add } end is_ring_hom variables {β : Type v} {γ : Type w} [rα : semiring α] [rβ : semiring β] namespace ring_hom section include rα rβ /-- Interpret `f : α → β` with `is_semiring_hom f` as a ring homomorphism. -/ def of {f : α → β} (hf : is_semiring_hom f) : α →+* β := { to_fun := f, .. monoid_hom.of hf.to_is_monoid_hom, .. add_monoid_hom.of hf.to_is_add_monoid_hom } @[simp] lemma coe_of {f : α → β} (hf : is_semiring_hom f) : ⇑(of hf) = f := rfl lemma to_is_semiring_hom (f : α →+* β) : is_semiring_hom f := { map_zero := f.map_zero, map_one := f.map_one, map_add := f.map_add, map_mul := f.map_mul } end lemma to_is_ring_hom {α γ} [ring α] [ring γ] (g : α →+* γ) : is_ring_hom g := is_ring_hom.of_semiring g.to_is_semiring_hom end ring_hom
dc3323dd70b49102b8516f11b4f3eed9f500b1e7
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/meta/smt/smt_tactic.lean
79b84abc1d379addef716c426d360b32d78b22d2
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,080
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.simp_tactic import init.meta.smt.congruence_closure import init.meta.smt.ematch universe variables u run_command mk_simp_attr `pre_smt run_command mk_hinst_lemma_attr_set `ematch [] [`ematch_lhs] /-- Configuration for the smt tactic preprocessor. The preprocessor is applied whenever a new hypothesis is introduced. - simp_attr: is the attribute name for the simplification lemmas that are used during the preprocessing step. - max_steps: it is the maximum number of steps performed by the simplifier. - zeta: if tt, then zeta reduction (i.e., unfolding let-expressions) is used during preprocessing. -/ structure smt_pre_config := (simp_attr : name := `pre_smt) (max_steps : nat := 1000000) (zeta : bool := ff) /-- Configuration for the smt_state object. - em_attr: is the attribute name for the hinst_lemmas that are used for ematching -/ structure smt_config := (cc_cfg : cc_config := {}) (em_cfg : ematch_config := {}) (pre_cfg : smt_pre_config := {}) (em_attr : name := `ematch) meta def smt_config.set_classical (c : smt_config) (b : bool) : smt_config := {c with cc_cfg := { (c^.cc_cfg) with em := b}} meta constant smt_goal : Type meta def smt_state := list smt_goal meta constant smt_state.mk : smt_config → tactic smt_state meta constant smt_state.to_format : smt_state → tactic_state → format /-- Return tt iff classical excluded middle was enabled at smt_state.mk -/ meta constant smt_state.classical : smt_state → bool meta def smt_tactic := state_t smt_state tactic meta instance : has_append smt_state := list.has_append meta instance : monad smt_tactic := state_t.monad _ _ /- We don't use the default state_t lift operation because only tactics that do not change hypotheses can be automatically lifted to smt_tactic. -/ meta constant tactic_to_smt_tactic (α : Type) : tactic α → smt_tactic α meta instance : monad.has_monad_lift tactic smt_tactic := ⟨tactic_to_smt_tactic⟩ meta instance (α : Type) : has_coe (tactic α) (smt_tactic α) := ⟨monad.monad_lift⟩ meta def smt_tactic_orelse {α : Type} (t₁ t₂ : smt_tactic α) : smt_tactic α := λ ss ts, tactic_result.cases_on (t₁ ss ts) tactic_result.success (λ e₁ ref₁ s', tactic_result.cases_on (t₂ ss ts) tactic_result.success (tactic_result.exception (α × smt_state))) meta instance : alternative smt_tactic := {failure := λ α, @tactic.failed α, orelse := @smt_tactic_orelse, pure := @return _ _, seq := @fapp _ _, map := @fmap _ _} namespace smt_tactic open tactic (transparency) meta constant intros : smt_tactic unit meta constant intron : nat → smt_tactic unit meta constant intro_lst : list name → smt_tactic unit /-- Try to close main goal by using equalities implied by the congruence closure module. -/ meta constant close : smt_tactic unit /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from lemmas in the main goal with terms in the main goal. The set of lemmas is populated with theorems tagged with the attribute specified at smt_config.em_attr, and lemmas added using tactics such as `smt_tactic.add_lemmas`. The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`. Remark: the given predicate is applied to every new instance. The instance is only added to the state if the predicate returns tt. -/ meta constant ematch_core : (expr → bool) → smt_tactic unit /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from the given lemmas with terms in the main goal. -/ meta constant ematch_using : hinst_lemmas → smt_tactic unit meta constant mk_ematch_eqn_lemmas_for_core : transparency → name → smt_tactic hinst_lemmas meta constant to_cc_state : smt_tactic cc_state meta constant to_em_state : smt_tactic ematch_state meta constant get_config : smt_tactic cc_config /-- Preprocess the given term using the same simplifications rules used when we introduce a new hypothesis. The result is pair containing the resulting term and a proof that it is equal to the given one. -/ meta constant preprocess : expr → smt_tactic (expr × expr) meta constant get_lemmas : smt_tactic hinst_lemmas meta constant set_lemmas : hinst_lemmas → smt_tactic unit meta constant add_lemmas : hinst_lemmas → smt_tactic unit meta def add_ematch_lemma_core (md : transparency) (as_simp : bool) (e : expr) : smt_tactic unit := do h ← hinst_lemma.mk_core md e as_simp, add_lemmas (mk_hinst_singleton h) meta def add_ematch_lemma_from_decl_core (md : transparency) (as_simp : bool) (n : name) : smt_tactic unit := do h ← hinst_lemma.mk_from_decl_core md n as_simp, add_lemmas (mk_hinst_singleton h) meta def add_ematch_eqn_lemmas_for_core (md : transparency) (n : name) : smt_tactic unit := do hs ← mk_ematch_eqn_lemmas_for_core md n, add_lemmas hs meta def ematch : smt_tactic unit := ematch_core (λ _, tt) meta def failed : smt_tactic unit := tactic.failed meta def fail {α : Type} {β : Type u} [has_to_format β] (msg : β) : tactic α := tactic.fail msg meta def try {α : Type} (t : smt_tactic α) : smt_tactic unit := λ ss ts, tactic_result.cases_on (t ss ts) (λ ⟨a, new_ss⟩, tactic_result.success ((), new_ss)) (λ e ref s', tactic_result.success ((), ss) ts) /- (repeat_at_most n t): repeat the given tactic at most n times or until t fails -/ meta def repeat_at_most : nat → smt_tactic unit → smt_tactic unit | 0 t := return () | (n+1) t := (do t, repeat_at_most n t) <|> return () /-- (repeat_exactly n t) : execute t n times -/ meta def repeat_exactly : nat → smt_tactic unit → smt_tactic unit | 0 t := return () | (n+1) t := do t, repeat_exactly n t meta def repeat : smt_tactic unit → smt_tactic unit := repeat_at_most 100000 meta def eblast : smt_tactic unit := repeat (ematch >> try close) open tactic protected meta def read : smt_tactic (smt_state × tactic_state) := do s₁ ← state_t.read, s₂ ← tactic.read, return (s₁, s₂) private meta def mk_smt_goals_for (cfg : smt_config) : list expr → list smt_goal → list expr → tactic (list smt_goal × list expr) | [] sr tr := return (sr^.reverse, tr^.reverse) | (tg::tgs) sr tr := do tactic.set_goals [tg], [new_sg] ← smt_state.mk cfg | tactic.failed, [new_tg] ← get_goals | tactic.failed, mk_smt_goals_for tgs (new_sg::sr) (new_tg::tr) /-- This lift operation will restart the SMT state. It is useful for using tactics that change the set of hypotheses. -/ meta def slift {α : Type} (t : tactic α) : smt_tactic α := λ ss, do _::sgs ← return ss | fail "slift tactic failed, there no smt goals to be solved", cfg ← return {smt_config .}, -- TODO(Leo): use get_config tg::tgs ← tactic.get_goals | tactic.failed, tactic.set_goals [tg], a ← t, new_tgs ← tactic.get_goals, (new_sgs, new_tgs) ← mk_smt_goals_for cfg new_tgs [] [], tactic.set_goals (new_tgs ++ tgs), return (a, new_sgs ++ sgs) meta def trace_state : smt_tactic unit := do (s₁, s₂) ← smt_tactic.read, trace (smt_state.to_format s₁ s₂) meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit := tactic.trace a meta def classical : smt_tactic bool := do s ← state_t.read, return s^.classical /- Low level primitives for managing set of goals -/ meta def get_goals : smt_tactic (list smt_goal × list expr) := do (g₁, _) ← smt_tactic.read, g₂ ← tactic.get_goals, return (g₁, g₂) meta def set_goals : list smt_goal → list expr → smt_tactic unit := λ g₁ g₂ ss, tactic.set_goals g₂ >> return ((), g₁) private meta def all_goals_core (tac : smt_tactic unit) : list smt_goal → list expr → list smt_goal → list expr → smt_tactic unit | [] ts acs act := set_goals acs (ts ++ act) | (s :: ss) [] acs act := fail "ill-formed smt_state" | (s :: ss) (t :: ts) acs act := do set_goals [s] [t], tac, (new_ss, new_ts) ← get_goals, all_goals_core ss ts (acs ++ new_ss) (act ++ new_ts) /- Apply the given tactic to all goals. -/ meta def all_goals (tac : smt_tactic unit) : smt_tactic unit := do (ss, ts) ← get_goals, all_goals_core tac ss ts [] [] /- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/ meta def seq (tac1 : smt_tactic unit) (tac2 : smt_tactic unit) : smt_tactic unit := do (s::ss, t::ts) ← get_goals | failed, set_goals [s] [t], tac1, all_goals tac2, (new_ss, new_ts) ← get_goals, set_goals (new_ss ++ ss) (new_ts ++ ts) meta def solve1 (tac : smt_tactic unit) : smt_tactic unit := do (ss, gs) ← get_goals, match ss, gs with | [], _ := fail "focus tactic failed, there isn't any goal left to focus" | _, [] := fail "focus tactic failed, there isn't any smt goal left to focus" | s::ss, g::gs := do set_goals [s] [g], tac, (ss', gs') ← get_goals, match ss', gs' with | [], [] := set_goals ss gs | _, _ := fail "focus tactic failed, focused goal has not been solved" end end meta def swap : smt_tactic unit := do (ss, ts) ← get_goals, match ss, ts with | (s₁ :: s₂ :: ss), (t₁ :: t₂ :: ts) := set_goals (s₂ :: s₁ :: ss) (t₂ :: t₁ :: ts) | _, _ := failed end /-- Add a new goal for t, and the hypothesis (h : t) in the current goal. -/ meta def assert (h : name) (t : expr) : smt_tactic unit := tactic.assert_core h t >> swap >> intros >> swap >> try close /-- Add the hypothesis (h : t) in the current goal if v has type t. -/ meta def assertv (h : name) (t : expr) (v : expr) : smt_tactic unit := tactic.assertv_core h t v >> intros >> return () /-- Add a new goal for t, and the hypothesis (h : t := ?M) in the current goal. -/ meta def define (h : name) (t : expr) : smt_tactic unit := tactic.define_core h t >> swap >> intros >> swap >> try close /-- Add the hypothesis (h : t := v) in the current goal if v has type t. -/ meta def definev (h : name) (t : expr) (v : expr) : smt_tactic unit := tactic.definev_core h t v >> intros >> return () /-- Add (h : t := pr) to the current goal -/ meta def pose (h : name) (pr : expr) : smt_tactic unit := do t ← tactic.infer_type pr, definev h t pr /- Add (h : t) to the current goal, given a proof (pr : t) -/ meta def note (n : name) (pr : expr) : smt_tactic unit := do t ← tactic.infer_type pr, assertv n t pr meta def destruct (e : expr) : smt_tactic unit := smt_tactic.seq (tactic.destruct e) smt_tactic.intros meta def by_cases (e : expr) : smt_tactic unit := do c ← classical, if c then destruct (expr.app (expr.const `classical.em []) e) else do dec_e ← (mk_app `decidable [e] <|> fail "by_cases smt_tactic failed, type is not a proposition"), inst ← (mk_instance dec_e <|> fail "by_cases smt_tactic failed, type of given expression is not decidable"), em ← mk_app `decidable.em [e, inst], destruct em meta def by_contradiction : smt_tactic unit := do t ← target, c ← classical, if t^.is_false then skip else if c then do apply (expr.app (expr.const `classical.by_contradiction []) t), intros else do dec_t ← (mk_app `decidable [t] <|> fail "by_contradiction smt_tactic failed, target is not a proposition"), inst ← (mk_instance dec_t <|> fail "by_contradiction smt_tactic failed, target is not decidable"), a ← mk_mapp `decidable.by_contradiction [some t, some inst], apply a, intros /- Return a proof for e, if 'e' is a known fact in the main goal. -/ meta def proof_for (e : expr) : smt_tactic expr := do cc ← to_cc_state, cc^.proof_for e /- Return a refutation for e (i.e., a proof for (not e)), if 'e' has been refuted in the main goal. -/ meta def refutation_for (e : expr) : smt_tactic expr := do cc ← to_cc_state, cc^.refutation_for e meta def get_facts : smt_tactic (list expr) := do cc ← to_cc_state, return $ cc^.eqc_of expr.mk_true meta def get_refuted_facts : smt_tactic (list expr) := do cc ← to_cc_state, return $ cc^.eqc_of expr.mk_false meta def add_ematch_lemma : expr → smt_tactic unit := add_ematch_lemma_core reducible ff meta def add_ematch_lhs_lemma : expr → smt_tactic unit := add_ematch_lemma_core reducible tt meta def add_ematch_lemma_from_decl : name → smt_tactic unit := add_ematch_lemma_from_decl_core reducible ff meta def add_ematch_lhs_lemma_from_decl : name → smt_tactic unit := add_ematch_lemma_from_decl_core reducible ff meta def add_ematch_eqn_lemmas_for : name → smt_tactic unit := add_ematch_eqn_lemmas_for_core reducible meta def add_lemmas_from_facts_core : list expr → smt_tactic unit | [] := return () | (f::fs) := do try (is_prop f >> guard (f^.is_pi && bnot (f^.is_arrow)) >> proof_for f >>= add_ematch_lemma_core reducible ff), add_lemmas_from_facts_core fs meta def add_lemmas_from_facts : smt_tactic unit := get_facts >>= add_lemmas_from_facts_core meta def induction (e : expr) (rec : name) (ids : list name) : smt_tactic unit := slift (tactic.induction e rec ids) end smt_tactic open smt_tactic meta def using_smt_core (cfg : smt_config) (t : smt_tactic unit) : tactic unit := do ss ← smt_state.mk cfg, (t >> repeat close) ss, return () meta def using_smt : smt_tactic unit → tactic unit := using_smt_core {}
479c3d5458696ace1ea38bf1b7982dda971acd28
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/field_theory/splitting_field.lean
b43291a8b4f437c44b195b989d74a21324e440b0
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
7,590
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes Definition of splitting fields, and definition of homomorphism into any field that splits -/ import data.polynomial import ring_theory.principal_ideal_domain universes u v w variables {α : Type u} {β : Type v} {γ : Type w} namespace polynomial noncomputable theory open_locale classical variables [field α] [field β] [field γ] open polynomial section splits variables (i : α →+* β) /-- a polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1 -/ def splits (f : polynomial α) : Prop := f = 0 ∨ ∀ {g : polynomial β}, irreducible g → g ∣ f.map i → degree g = 1 @[simp] lemma splits_zero : splits i (0 : polynomial α) := or.inl rfl @[simp] lemma splits_C (a : α) : splits i (C a) := if ha : a = 0 then ha.symm ▸ (@C_0 α _).symm ▸ splits_zero i else have hia : i a ≠ 0, from mt ((is_add_group_hom.injective_iff i).1 i.injective _) ha, or.inr $ λ g hg ⟨p, hp⟩, absurd hg.1 (classical.not_not.2 (is_unit_iff_degree_eq_zero.2 $ by have := congr_arg degree hp; simp [degree_C hia, @eq_comm (with_bot ℕ) 0, nat.with_bot.add_eq_zero_iff] at this; clear _fun_match; tautology)) lemma splits_of_degree_eq_one {f : polynomial α} (hf : degree f = 1) : splits i f := or.inr $ λ g hg ⟨p, hp⟩, by have := congr_arg degree hp; simp [nat.with_bot.add_eq_one_iff, hf, @eq_comm (with_bot ℕ) 1, mt is_unit_iff_degree_eq_zero.2 hg.1] at this; clear _fun_match; tauto lemma splits_of_degree_le_one {f : polynomial α} (hf : degree f ≤ 1) : splits i f := begin cases h : degree f with n, { rw [degree_eq_bot.1 h]; exact splits_zero i }, { cases n with n, { rw [eq_C_of_degree_le_zero (trans_rel_right (≤) h (le_refl _))]; exact splits_C _ _ }, { have hn : n = 0, { rw h at hf, cases n, { refl }, { exact absurd hf dec_trivial } }, exact splits_of_degree_eq_one _ (by rw [h, hn]; refl) } } end lemma splits_mul {f g : polynomial α} (hf : splits i f) (hg : splits i g) : splits i (f * g) := if h : f * g = 0 then by simp [h] else or.inr $ λ p hp hpf, ((principal_ideal_domain.irreducible_iff_prime.1 hp).2.2 _ _ (show p ∣ map i f * map i g, by convert hpf; rw polynomial.map_mul)).elim (hf.resolve_left (λ hf, by simpa [hf] using h) hp) (hg.resolve_left (λ hg, by simpa [hg] using h) hp) lemma splits_of_splits_mul {f g : polynomial α} (hfg : f * g ≠ 0) (h : splits i (f * g)) : splits i f ∧ splits i g := ⟨or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact dvd.trans hg (dvd_mul_right _ _)), or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw map_mul; exact dvd.trans hg (dvd_mul_left _ _))⟩ lemma splits_map_iff (j : β →+* γ) {f : polynomial α} : splits j (f.map i) ↔ splits (j.comp i) f := by simp [splits, polynomial.map_map] lemma exists_root_of_splits {f : polynomial α} (hs : splits i f) (hf0 : degree f ≠ 0) : ∃ x, eval₂ i x f = 0 := if hf0 : f = 0 then ⟨37, by simp [hf0]⟩ else let ⟨g, hg⟩ := is_noetherian_ring.exists_irreducible_factor (show ¬ is_unit (f.map i), from mt is_unit_iff_degree_eq_zero.1 (by rwa degree_map)) (by rw [ne.def, map_eq_zero]; exact hf0) in let ⟨x, hx⟩ := exists_root_of_degree_eq_one (hs.resolve_left hf0 hg.1 hg.2) in let ⟨i, hi⟩ := hg.2 in ⟨x, by rw [← eval_map, hi, eval_mul, show _ = _, from hx, zero_mul]⟩ lemma exists_multiset_of_splits {f : polynomial α} : splits i f → ∃ (s : multiset β), f.map i = C (i f.leading_coeff) * (s.map (λ a : β, (X : polynomial β) - C a)).prod := suffices splits (ring_hom.id _) (f.map i) → ∃ s : multiset β, f.map i = (C (f.map i).leading_coeff) * (s.map (λ a : β, (X : polynomial β) - C a)).prod, by rwa [splits_map_iff, leading_coeff_map i] at this, is_noetherian_ring.irreducible_induction_on (f.map i) (λ _, ⟨{37}, by simp [i.map_zero]⟩) (λ u hu _, ⟨0, by conv_lhs { rw eq_C_of_degree_eq_zero (is_unit_iff_degree_eq_zero.1 hu) }; simp [leading_coeff, nat_degree_eq_of_degree_eq_some (is_unit_iff_degree_eq_zero.1 hu)]⟩) (λ f p hf0 hp ih hfs, have hpf0 : p * f ≠ 0, from mul_ne_zero hp.ne_zero hf0, let ⟨s, hs⟩ := ih (splits_of_splits_mul _ hpf0 hfs).2 in ⟨-(p * norm_unit p).coeff 0 :: s, have hp1 : degree p = 1, from hfs.resolve_left hpf0 hp (by simp), begin rw [multiset.map_cons, multiset.prod_cons, leading_coeff_mul, C_mul, mul_assoc, mul_left_comm (C f.leading_coeff), ← hs, ← mul_assoc, domain.mul_left_inj hf0], conv_lhs {rw eq_X_add_C_of_degree_eq_one hp1}, simp only [mul_add, coe_norm_unit hp.ne_zero, mul_comm p, coeff_neg, C_neg, sub_eq_add_neg, neg_neg, coeff_C_mul, (mul_assoc _ _ _).symm, C_mul.symm, mul_inv_cancel (show p.leading_coeff ≠ 0, from mt leading_coeff_eq_zero.1 hp.ne_zero), one_mul], end⟩) section UFD local attribute [instance, priority 10] principal_ideal_domain.to_unique_factorization_domain local infix ` ~ᵤ ` : 50 := associated open unique_factorization_domain associates lemma splits_of_exists_multiset {f : polynomial α} {s : multiset β} (hs : f.map i = C (i f.leading_coeff) * (s.map (λ a : β, (X : polynomial β) - C a)).prod) : splits i f := if hf0 : f = 0 then or.inl hf0 else or.inr $ λ p hp hdp, have ht : multiset.rel associated (factors (f.map i)) (s.map (λ a : β, (X : polynomial β) - C a)) := unique (λ p hp, irreducible_factors (mt (map_eq_zero i).1 hf0) _ hp) (λ p, by simp [@eq_comm _ _ p, -sub_eq_add_neg, irreducible_of_degree_eq_one (degree_X_sub_C _)] {contextual := tt}) (associated.symm $ calc _ ~ᵤ f.map i : ⟨(units.map' C : units β →* units (polynomial β)) (units.mk0 (f.map i).leading_coeff (mt leading_coeff_eq_zero.1 (mt (map_eq_zero i).1 hf0))), by conv_rhs {rw [hs, ← leading_coeff_map i, mul_comm]}; refl⟩ ... ~ᵤ _ : associated.symm (unique_factorization_domain.factors_prod (by simpa using hf0))), let ⟨q, hq, hpq⟩ := exists_mem_factors_of_dvd (by simpa) hp hdp in let ⟨q', hq', hqq'⟩ := multiset.exists_mem_of_rel_of_mem ht hq in let ⟨a, ha⟩ := multiset.mem_map.1 hq' in by rw [← degree_X_sub_C a, ha.2]; exact degree_eq_degree_of_associated (hpq.trans hqq') lemma splits_of_splits_id {f : polynomial α} : splits (ring_hom.id _) f → splits i f := unique_factorization_domain.induction_on_prime f (λ _, splits_zero _) (λ _ hu _, splits_of_degree_le_one _ ((is_unit_iff_degree_eq_zero.1 hu).symm ▸ dec_trivial)) (λ a p ha0 hp ih hfi, splits_mul _ (splits_of_degree_eq_one _ ((splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).1.resolve_left hp.1 (irreducible_of_prime hp) (by rw map_id))) (ih (splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).2)) end UFD lemma splits_iff_exists_multiset {f : polynomial α} : splits i f ↔ ∃ (s : multiset β), f.map i = C (i f.leading_coeff) * (s.map (λ a : β, (X : polynomial β) - C a)).prod := ⟨exists_multiset_of_splits i, λ ⟨s, hs⟩, splits_of_exists_multiset i hs⟩ lemma splits_comp_of_splits (j : β →+* γ) {f : polynomial α} (h : splits i f) : splits (j.comp i) f := begin change i with ((ring_hom.id _).comp i) at h, rw [← splits_map_iff], rw [← splits_map_iff i] at h, exact splits_of_splits_id _ h end end splits end polynomial
a5b646829a4233f9241cb946fc5ad94542886868
7bc35d4fbdda0c01e9b22a949940ee5cbb9800d0
/manifold/manifold.lean
2ec5167d4093872f61141e20e4c7da7d53bae137
[]
no_license
truonghoangle/manifolds
e6c2534dd46579f56ba99a48e2eb7ce51640e7c0
dcf4815b29ad363ec9712fd00b7756c36cfa7c1c
refs/heads/main
1,638,501,090,139
1,636,918,550,000
1,636,918,550,000
185,779,631
0
0
null
null
null
null
UTF-8
Lean
false
false
2,578
lean
import manifold.differentiable open topological_space noncomputable theory universes u v w variables {α : Type} {β : Type} {γ : Type w} {n : ℕ} variable [normed_field α] variables {E : euclidean α } variables {F : euclidean α } structure chart (X : Top) (E : euclidean α ) := (iso : X ≃ₜ. E.to_Top) (h1 : is_open iso.to_fun.dom) (h2 : is_open iso.inv_fun.dom) namespace chart variable {X : Top} def to_fun (c : chart X E) : X →. E := c.iso.to_fun def inv_fun (c :chart X E) : E →. X := c.iso.inv_fun def domain (c : chart X E) : set X := c.to_fun.dom def codomain (c : chart X E) : set E := c.inv_fun.dom end chart def differentiable_compatible_charts {X : Top} (c₁ c₂ : chart X E) : Prop := differentiable.is_differentiable_map (c₂.to_fun ∘. c₁.inv_fun) ∧ differentiable.is_differentiable_map (c₁.to_fun ∘. c₂.inv_fun) def C_compatible_charts {X : Top} (n:ℕ) (c₁ c₂ : chart X E) : Prop := differentiable.𝒞_n n (c₂.to_fun ∘. c₁.inv_fun) ∧ differentiable.𝒞_n n (c₁.to_fun ∘. c₂.inv_fun) def C_infinity_compatible_charts {X : Top} (c₁ c₂ : chart X E) : Prop := differentiable.𝒞_infinity (c₂.to_fun ∘. c₁.inv_fun) ∧ differentiable.𝒞_infinity (c₁.to_fun ∘. c₂.inv_fun) structure manifold (E : euclidean α ) := (carrier : Top) (struct2 : t2_space carrier) (struct3 : second_countable_topology carrier) (charts : set (chart carrier E)) (cover : ⋃₀ (chart.domain '' charts) = set.univ) structure differentiable_manifold (E : euclidean α ) extends manifold (E) := (compatible : ∀{{c₁ c₂}}, c₁ ∈ charts → c₂ ∈ charts → differentiable_compatible_charts c₁ c₂) structure C_infinity_manifold (E : euclidean α ) extends manifold (E) := (compatible : ∀{{c₁ c₂}}, c₁ ∈ charts → c₂ ∈ charts → C_infinity_compatible_charts c₁ c₂) structure C_manifold (n:ℕ) (E : euclidean α ) extends manifold (E) := (compatible : ∀{{c₁ c₂}}, c₁ ∈ charts → c₂ ∈ charts → C_compatible_charts n c₁ c₂) def real_manifold (E : euclidean ℝ ) := differentiable_manifold (E : euclidean ℝ ) def complex_manifold (E : euclidean ℂ ) := differentiable_manifold (E : euclidean ℂ ) namespace differentiable_manifold def dim (M:differentiable_manifold E) :ℕ := E.dim def curve (M:differentiable_manifold E):Prop := dim M==1 def surface (M:differentiable_manifold E):Prop := dim M==2 def threefold (M:differentiable_manifold E):Prop := dim M==3 end differentiable_manifold
c4897340545798ff402990488dbd1c5486402f68
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/smt_ematch2.lean
7549aee7c72e905b8a2ea7f4207d38faf988b8db
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
2,728
lean
universe variables u namespace foo variables {α : Type u} open smt_tactic meta def no_ac : smt_config := { cc_cfg := { ac := ff }} meta def blast : tactic unit := using_smt_with no_ac $ intros >> repeat (ematch >> try close) section add_comm_monoid variables [add_comm_monoid α] attribute [ematch] add_comm add_assoc theorem add_comm_three (a b c : α) : a + b + c = c + b + a := by blast theorem add.comm4 : ∀ (n m k l : α), n + m + (k + l) = n + k + (m + l) := by blast end add_comm_monoid section group variable [group α] attribute [ematch] mul_assoc mul_left_inv one_mul theorem inv_mul_cancel_left (a b : α) : a⁻¹ * (a * b) = b := by blast end group namespace subt constant subt : nat → nat → Prop axiom subt_trans {a b c : nat} : subt a b → subt b c → subt a c attribute [ematch] subt_trans lemma ex (a b c d : nat) : subt a b → subt b c → subt c d → subt a d := by blast end subt section ring variables [ring α] (a b : α) attribute [ematch] zero_mul lemma ex2 : a = 0 → a * b = 0 := by blast definition ex1 (a b : int) : a = 0 → a * b = 0 := by blast end ring namespace cast1 constant C : nat → Type constant f : ∀ n, C n → C n axiom fax (n : nat) (a : C (2*n)) : (: f (2*n) a :) = a attribute [ematch] fax lemma ex3 (n m : nat) (a : C n) : n = 2*m → f n a = a := by blast end cast1 namespace cast2 constant C : nat → Type constant f : ∀ n, C n → C n constant g : ∀ n, C n → C n → C n axiom gffax (n : nat) (a b : C n) : (: g n (f n a) (f n b) :) = a attribute [ematch] gffax lemma ex4 (n m : nat) (a c : C n) (b : C m) : n = m → a == f m b → g n a (f n c) == b := by blast end cast2 namespace cast3 constant C : nat → Type constant f : ∀ n, C n → C n constant g : ∀ n, C n → C n → C n axiom gffax (n : nat) (a b : C n) : (: g n a b :) = a attribute [ematch] gffax lemma ex5 (n m : nat) (a c : C n) (b : C m) (e : m = n) : a == b → g n a a == b := by blast end cast3 namespace tuple constant {α} tuple: Type α → nat → Type α constant nil {α : Type u} : tuple α 0 constant append {α : Type u} {n m : nat} : tuple α n → tuple α m → tuple α (n + m) infix ` ++ ` := append axiom append_assoc {α : Type u} {n₁ n₂ n₃ : nat} (v₁ : tuple α n₁) (v₂ : tuple α n₂) (v₃ : tuple α n₃) : (v₁ ++ v₂) ++ v₃ == v₁ ++ (v₂ ++ v₃) attribute [ematch] append_assoc variables {p m n q : nat} variables {xs : tuple α m} variables {ys : tuple α n} variables {zs : tuple α p} variables {ws : tuple α q} lemma ex6 : p = m + n → zs == xs ++ ys → zs ++ ws == xs ++ (ys ++ ws) := by blast def ex : p = n + m → zs == xs ++ ys → zs ++ ws == xs ++ (ys ++ ws) := by blast end tuple end foo
a5f87e7e21da358cf2404cdd1f2cc8efaaeaf8b4
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/mean_inequalities.lean
af5cfd68579f8fc1cefa905eb66775039d16d4a1
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
38,265
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Sébastien Gouëzel, Rémy Degenne -/ import analysis.convex.specific_functions import data.real.conjugate_exponents /-! # Mean value inequalities In this file we prove several inequalities for finite sums, including AM-GM inequality, Young's inequality, Hölder inequality, and Minkowski inequality. Versions for integrals of some of these inequalities are available in `measure_theory.mean_inequalities`. ## Main theorems ### AM-GM inequality: The inequality says that the geometric mean of a tuple of non-negative numbers is less than or equal to their arithmetic mean. We prove the weighted version of this inequality: if $w$ and $z$ are two non-negative vectors and $\sum_{i\in s} w_i=1$, then $$ \prod_{i\in s} z_i^{w_i} ≤ \sum_{i\in s} w_iz_i. $$ The classical version is a special case of this inequality for $w_i=\frac{1}{n}$. We prove a few versions of this inequality. Each of the following lemmas comes in two versions: a version for real-valued non-negative functions is in the `real` namespace, and a version for `nnreal`-valued functions is in the `nnreal` namespace. - `geom_mean_le_arith_mean_weighted` : weighted version for functions on `finset`s; - `geom_mean_le_arith_mean2_weighted` : weighted version for two numbers; - `geom_mean_le_arith_mean3_weighted` : weighted version for three numbers; - `geom_mean_le_arith_mean4_weighted` : weighted version for four numbers. ### Young's inequality Young's inequality says that for non-negative numbers `a`, `b`, `p`, `q` such that $\frac{1}{p}+\frac{1}{q}=1$ we have $$ ab ≤ \frac{a^p}{p} + \frac{b^q}{q}. $$ This inequality is a special case of the AM-GM inequality. It is then used to prove Hölder's inequality (see below). ### Hölder's inequality The inequality says that for two conjugate exponents `p` and `q` (i.e., for two positive numbers such that $\frac{1}{p}+\frac{1}{q}=1$) and any two non-negative vectors their inner product is less than or equal to the product of the $L_p$ norm of the first vector and the $L_q$ norm of the second vector: $$ \sum_{i\in s} a_ib_i ≤ \sqrt[p]{\sum_{i\in s} a_i^p}\sqrt[q]{\sum_{i\in s} b_i^q}. $$ We give versions of this result in `ℝ`, `ℝ≥0` and `ℝ≥0∞`. There are at least two short proofs of this inequality. In our proof we prenormalize both vectors, then apply Young's inequality to each $a_ib_i$. Another possible proof would be to deduce this inequality from the generalized mean inequality for well-chosen vectors and weights. ### Minkowski's inequality The inequality says that for `p ≥ 1` the function $$ \|a\|_p=\sqrt[p]{\sum_{i\in s} a_i^p} $$ satisfies the triangle inequality $\|a+b\|_p\le \|a\|_p+\|b\|_p$. We give versions of this result in `real`, `ℝ≥0` and `ℝ≥0∞`. We deduce this inequality from Hölder's inequality. Namely, Hölder inequality implies that $\|a\|_p$ is the maximum of the inner product $\sum_{i\in s}a_ib_i$ over `b` such that $\|b\|_q\le 1$. Now Minkowski's inequality follows from the fact that the maximum value of the sum of two functions is less than or equal to the sum of the maximum values of the summands. ## TODO - each inequality `A ≤ B` should come with a theorem `A = B ↔ _`; one of the ways to prove them is to define `strict_convex_on` functions. - generalized mean inequality with any `p ≤ q`, including negative numbers; - prove that the power mean tends to the geometric mean as the exponent tends to zero. -/ universes u v open finset open_locale classical big_operators nnreal ennreal noncomputable theory variables {ι : Type u} (s : finset ι) section geom_mean_le_arith_mean /-! ### AM-GM inequality -/ namespace real /-- AM-GM inequality: the **geometric mean is less than or equal to the arithmetic mean**, weighted version for real-valued nonnegative functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) : (∏ i in s, (z i) ^ (w i)) ≤ ∑ i in s, w i * z i := begin -- If some number `z i` equals zero and has non-zero weight, then LHS is 0 and RHS is nonnegative. by_cases A : ∃ i ∈ s, z i = 0 ∧ w i ≠ 0, { rcases A with ⟨i, his, hzi, hwi⟩, rw [prod_eq_zero his], { exact sum_nonneg (λ j hj, mul_nonneg (hw j hj) (hz j hj)) }, { rw hzi, exact zero_rpow hwi } }, -- If all numbers `z i` with non-zero weight are positive, then we apply Jensen's inequality -- for `exp` and numbers `log (z i)` with weights `w i`. { simp only [not_exists, not_and, ne.def, not_not] at A, have := convex_on_exp.map_sum_le hw hw' (λ i _, set.mem_univ $ log (z i)), simp only [exp_sum, (∘), smul_eq_mul, mul_comm (w _) (log _)] at this, convert this using 1; [apply prod_congr rfl, apply sum_congr rfl]; intros i hi, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { exact rpow_def_of_pos hz _ } }, { cases eq_or_lt_of_le (hz i hi) with hz hz, { simp [A i hi hz.symm] }, { rw [exp_log hz] } } } end theorem geom_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : (∏ i in s, (z i) ^ (w i)) = x := calc (∏ i in s, (z i) ^ (w i)) = ∏ i in s, x ^ w i : begin refine prod_congr rfl (λ i hi, _), cases eq_or_ne (w i) 0 with h₀ h₀, { rw [h₀, rpow_zero, rpow_zero] }, { rw hx i hi h₀ } end ... = x : begin rw [← rpow_sum_of_nonneg _ hw, hw', rpow_one], have : (∑ i in s, w i) ≠ 0, { rw hw', exact one_ne_zero }, obtain ⟨i, his, hi⟩ := exists_ne_zero_of_sum_ne_zero this, rw ← hx i his hi, exact hz i his end theorem arith_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw' : ∑ i in s, w i = 1) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : ∑ i in s, w i * z i = x := calc ∑ i in s, w i * z i = ∑ i in s, w i * x : begin refine sum_congr rfl (λ i hi, _), cases eq_or_ne (w i) 0 with hwi hwi, { rw [hwi, zero_mul, zero_mul] }, { rw hx i hi hwi }, end ... = x : by rw [←sum_mul, hw', one_mul] theorem geom_mean_eq_arith_mean_weighted_of_constant (w z : ι → ℝ) (x : ℝ) (hw : ∀ i ∈ s, 0 ≤ w i) (hw' : ∑ i in s, w i = 1) (hz : ∀ i ∈ s, 0 ≤ z i) (hx : ∀ i ∈ s, w i ≠ 0 → z i = x) : (∏ i in s, (z i) ^ (w i)) = ∑ i in s, w i * z i := by rw [geom_mean_weighted_of_constant, arith_mean_weighted_of_constant]; assumption end real namespace nnreal /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for `nnreal`-valued functions. -/ theorem geom_mean_le_arith_mean_weighted (w z : ι → ℝ≥0) (hw' : ∑ i in s, w i = 1) : (∏ i in s, (z i) ^ (w i:ℝ)) ≤ ∑ i in s, w i * z i := by exact_mod_cast real.geom_mean_le_arith_mean_weighted _ _ _ (λ i _, (w i).coe_nonneg) (by assumption_mod_cast) (λ i _, (z i).coe_nonneg) /-- The geometric mean is less than or equal to the arithmetic mean, weighted version for two `nnreal` numbers. -/ theorem geom_mean_le_arith_mean2_weighted (w₁ w₂ p₁ p₂ : ℝ≥0) : w₁ + w₂ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) ≤ w₁ * p₁ + w₂ * p₂ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one] using geom_mean_le_arith_mean_weighted univ ![w₁, w₂] ![p₁, p₂] theorem geom_mean_le_arith_mean3_weighted (w₁ w₂ w₃ p₁ p₂ p₃ : ℝ≥0) : w₁ + w₂ + w₃ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) * p₃ ^ (w₃:ℝ) ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted univ ![w₁, w₂, w₃] ![p₁, p₂, p₃] theorem geom_mean_le_arith_mean4_weighted (w₁ w₂ w₃ w₄ p₁ p₂ p₃ p₄ : ℝ≥0) : w₁ + w₂ + w₃ + w₄ = 1 → p₁ ^ (w₁:ℝ) * p₂ ^ (w₂:ℝ) * p₃ ^ (w₃:ℝ)* p₄ ^ (w₄:ℝ) ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ + w₄ * p₄ := by simpa only [fin.prod_univ_succ, fin.sum_univ_succ, finset.prod_empty, finset.sum_empty, fintype.univ_of_is_empty, fin.cons_succ, fin.cons_zero, add_zero, mul_one, ← add_assoc, mul_assoc] using geom_mean_le_arith_mean_weighted univ ![w₁, w₂, w₃, w₄] ![p₁, p₂, p₃, p₄] end nnreal namespace real theorem geom_mean_le_arith_mean2_weighted {w₁ w₂ p₁ p₂ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hw : w₁ + w₂ = 1) : p₁ ^ w₁ * p₂ ^ w₂ ≤ w₁ * p₁ + w₂ * p₂ := nnreal.geom_mean_le_arith_mean2_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ $ nnreal.coe_eq.1 $ by assumption theorem geom_mean_le_arith_mean3_weighted {w₁ w₂ w₃ p₁ p₂ p₃ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hw₃ : 0 ≤ w₃) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hp₃ : 0 ≤ p₃) (hw : w₁ + w₂ + w₃ = 1) : p₁ ^ w₁ * p₂ ^ w₂ * p₃ ^ w₃ ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ := nnreal.geom_mean_le_arith_mean3_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨w₃, hw₃⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ ⟨p₃, hp₃⟩ $ nnreal.coe_eq.1 hw theorem geom_mean_le_arith_mean4_weighted {w₁ w₂ w₃ w₄ p₁ p₂ p₃ p₄ : ℝ} (hw₁ : 0 ≤ w₁) (hw₂ : 0 ≤ w₂) (hw₃ : 0 ≤ w₃) (hw₄ : 0 ≤ w₄) (hp₁ : 0 ≤ p₁) (hp₂ : 0 ≤ p₂) (hp₃ : 0 ≤ p₃) (hp₄ : 0 ≤ p₄) (hw : w₁ + w₂ + w₃ + w₄ = 1) : p₁ ^ w₁ * p₂ ^ w₂ * p₃ ^ w₃ * p₄ ^ w₄ ≤ w₁ * p₁ + w₂ * p₂ + w₃ * p₃ + w₄ * p₄ := nnreal.geom_mean_le_arith_mean4_weighted ⟨w₁, hw₁⟩ ⟨w₂, hw₂⟩ ⟨w₃, hw₃⟩ ⟨w₄, hw₄⟩ ⟨p₁, hp₁⟩ ⟨p₂, hp₂⟩ ⟨p₃, hp₃⟩ ⟨p₄, hp₄⟩ $ nnreal.coe_eq.1 $ by assumption end real end geom_mean_le_arith_mean section young /-! ### Young's inequality -/ namespace real /-- Young's inequality, a version for nonnegative real numbers. -/ theorem young_inequality_of_nonneg {a b p q : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) (hpq : p.is_conjugate_exponent q) : a * b ≤ a^p / p + b^q / q := by simpa [← rpow_mul, ha, hb, hpq.ne_zero, hpq.symm.ne_zero, div_eq_inv_mul] using geom_mean_le_arith_mean2_weighted hpq.one_div_nonneg hpq.symm.one_div_nonneg (rpow_nonneg_of_nonneg ha p) (rpow_nonneg_of_nonneg hb q) hpq.inv_add_inv_conj /-- Young's inequality, a version for arbitrary real numbers. -/ theorem young_inequality (a b : ℝ) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ |a|^p / p + |b|^q / q := calc a * b ≤ |a * b| : le_abs_self (a * b) ... = |a| * |b| : abs_mul a b ... ≤ |a|^p / p + |b|^q / q : real.young_inequality_of_nonneg (abs_nonneg a) (abs_nonneg b) hpq end real namespace nnreal /-- Young's inequality, `ℝ≥0` version. We use `{p q : ℝ≥0}` in order to avoid constructing witnesses of `0 ≤ p` and `0 ≤ q` for the denominators. -/ theorem young_inequality (a b : ℝ≥0) {p q : ℝ≥0} (hp : 1 < p) (hpq : 1 / p + 1 / q = 1) : a * b ≤ a^(p:ℝ) / p + b^(q:ℝ) / q := real.young_inequality_of_nonneg a.coe_nonneg b.coe_nonneg ⟨hp, nnreal.coe_eq.2 hpq⟩ /-- Young's inequality, `ℝ≥0` version with real conjugate exponents. -/ theorem young_inequality_real (a b : ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ a ^ p / real.to_nnreal p + b ^ q / real.to_nnreal q := begin nth_rewrite 0 ← real.coe_to_nnreal p hpq.nonneg, nth_rewrite 0 ← real.coe_to_nnreal q hpq.symm.nonneg, exact young_inequality a b hpq.one_lt_nnreal hpq.inv_add_inv_conj_nnreal, end end nnreal namespace ennreal /-- Young's inequality, `ℝ≥0∞` version with real conjugate exponents. -/ theorem young_inequality (a b : ℝ≥0∞) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : a * b ≤ a ^ p / ennreal.of_real p + b ^ q / ennreal.of_real q := begin by_cases h : a = ⊤ ∨ b = ⊤, { refine le_trans le_top (le_of_eq _), repeat { rw div_eq_mul_inv }, cases h; rw h; simp [h, hpq.pos, hpq.symm.pos], }, push_neg at h, -- if a ≠ ⊤ and b ≠ ⊤, use the nnreal version: nnreal.young_inequality_real rw [←coe_to_nnreal h.left, ←coe_to_nnreal h.right, ←coe_mul, coe_rpow_of_nonneg _ hpq.nonneg, coe_rpow_of_nonneg _ hpq.symm.nonneg, ennreal.of_real, ennreal.of_real, ←@coe_div (real.to_nnreal p) _ (by simp [hpq.pos]), ←@coe_div (real.to_nnreal q) _ (by simp [hpq.symm.pos]), ←coe_add, coe_le_coe], exact nnreal.young_inequality_real a.to_nnreal b.to_nnreal hpq, end end ennreal end young section holder_minkowski /-! ### Hölder's and Minkowski's inequalities -/ namespace nnreal private lemma inner_le_Lp_mul_Lp_of_norm_le_one (f g : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : ∑ i in s, (f i) ^ p ≤ 1) (hg : ∑ i in s, (g i) ^ q ≤ 1) : ∑ i in s, f i * g i ≤ 1 := begin have hp_ne_zero : real.to_nnreal p ≠ 0, from (zero_lt_one.trans hpq.one_lt_nnreal).ne.symm, have hq_ne_zero : real.to_nnreal q ≠ 0, from (zero_lt_one.trans hpq.symm.one_lt_nnreal).ne.symm, calc ∑ i in s, f i * g i ≤ ∑ i in s, ((f i) ^ p / real.to_nnreal p + (g i) ^ q / real.to_nnreal q) : finset.sum_le_sum (λ i his, young_inequality_real (f i) (g i) hpq) ... = (∑ i in s, (f i) ^ p) / real.to_nnreal p + (∑ i in s, (g i) ^ q) / real.to_nnreal q : by rw [sum_add_distrib, sum_div, sum_div] ... ≤ 1 / real.to_nnreal p + 1 / real.to_nnreal q : by { refine add_le_add _ _, { rwa [div_le_iff hp_ne_zero, div_mul_cancel _ hp_ne_zero], }, { rwa [div_le_iff hq_ne_zero, div_mul_cancel _ hq_ne_zero], }, } ... = 1 : hpq.inv_add_inv_conj_nnreal, end private lemma inner_le_Lp_mul_Lp_of_norm_eq_zero (f g : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : ∑ i in s, (f i) ^ p = 0) : ∑ i in s, f i * g i ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) * (∑ i in s, (g i) ^ q) ^ (1 / q) := begin simp only [hf, hpq.ne_zero, one_div, sum_eq_zero_iff, zero_rpow, zero_mul, inv_eq_zero, ne.def, not_false_iff, le_zero_iff, mul_eq_zero], intros i his, left, rw sum_eq_zero_iff at hf, exact (rpow_eq_zero_iff.mp (hf i his)).left, end /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝ≥0`-valued functions. -/ theorem inner_le_Lp_mul_Lq (f g : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : ∑ i in s, f i * g i ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) * (∑ i in s, (g i) ^ q) ^ (1 / q) := begin by_cases hF_zero : ∑ i in s, (f i) ^ p = 0, { exact inner_le_Lp_mul_Lp_of_norm_eq_zero s f g hpq hF_zero, }, by_cases hG_zero : ∑ i in s, (g i) ^ q = 0, { calc ∑ i in s, f i * g i = ∑ i in s, g i * f i : by { congr' with i, rw mul_comm, } ... ≤ (∑ i in s, (g i) ^ q) ^ (1 / q) * (∑ i in s, (f i) ^ p) ^ (1 / p) : inner_le_Lp_mul_Lp_of_norm_eq_zero s g f hpq.symm hG_zero ... = (∑ i in s, (f i) ^ p) ^ (1 / p) * (∑ i in s, (g i) ^ q) ^ (1 / q) : mul_comm _ _, }, let f' := λ i, (f i) / (∑ i in s, (f i) ^ p) ^ (1 / p), let g' := λ i, (g i) / (∑ i in s, (g i) ^ q) ^ (1 / q), suffices : ∑ i in s, f' i * g' i ≤ 1, { simp_rw [f', g', div_mul_div_comm, ← sum_div] at this, rwa [div_le_iff, one_mul] at this, refine mul_ne_zero _ _, { rw [ne.def, rpow_eq_zero_iff, not_and_distrib], exact or.inl hF_zero, }, { rw [ne.def, rpow_eq_zero_iff, not_and_distrib], exact or.inl hG_zero, }, }, refine inner_le_Lp_mul_Lp_of_norm_le_one s f' g' hpq (le_of_eq _) (le_of_eq _), { simp_rw [f', div_rpow, ← sum_div, ← rpow_mul, one_div, inv_mul_cancel hpq.ne_zero, rpow_one, div_self hF_zero], }, { simp_rw [g', div_rpow, ← sum_div, ← rpow_mul, one_div, inv_mul_cancel hpq.symm.ne_zero, rpow_one, div_self hG_zero], }, end /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are already expressed as `p`-th powers, see `inner_le_Lp_mul_Lq_has_sum`. -/ theorem inner_le_Lp_mul_Lq_tsum {f g : ι → ℝ≥0} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ q)) : summable (λ i, f i * g i) ∧ ∑' i, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q) := begin have H₁ : ∀ s : finset ι, ∑ i in s, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q), { intros s, refine le_trans (inner_le_Lp_mul_Lq s f g hpq) (mul_le_mul _ _ bot_le bot_le), { rw nnreal.rpow_le_rpow_iff (one_div_pos.mpr hpq.pos), exact sum_le_tsum _ (λ _ _, zero_le _) hf }, { rw nnreal.rpow_le_rpow_iff (one_div_pos.mpr hpq.symm.pos), exact sum_le_tsum _ (λ _ _, zero_le _) hg } }, have bdd : bdd_above (set.range (λ s, ∑ i in s, f i * g i)), { refine ⟨(∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q), _⟩, rintros a ⟨s, rfl⟩, exact H₁ s }, have H₂ : summable _ := (has_sum_of_is_lub _ (is_lub_csupr bdd)).summable, exact ⟨H₂, tsum_le_of_sum_le H₂ H₁⟩, end theorem summable_mul_of_Lp_Lq {f g : ι → ℝ≥0} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ q)) : summable (λ i, f i * g i) := (inner_le_Lp_mul_Lq_tsum hpq hf hg).1 theorem inner_le_Lp_mul_Lq_tsum' {f g : ι → ℝ≥0} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ q)) : ∑' i, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q) := (inner_le_Lp_mul_Lq_tsum hpq hf hg).2 /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are not already expressed as `p`-th powers, see `inner_le_Lp_mul_Lq_tsum`. -/ theorem inner_le_Lp_mul_Lq_has_sum {f g : ι → ℝ≥0} {A B : ℝ≥0} {p q : ℝ} (hpq : p.is_conjugate_exponent q) (hf : has_sum (λ i, (f i) ^ p) (A ^ p)) (hg : has_sum (λ i, (g i) ^ q) (B ^ q)) : ∃ C, C ≤ A * B ∧ has_sum (λ i, f i * g i) C := begin obtain ⟨H₁, H₂⟩ := inner_le_Lp_mul_Lq_tsum hpq hf.summable hg.summable, have hA : A = (∑' (i : ι), f i ^ p) ^ (1 / p), { rw [hf.tsum_eq, rpow_inv_rpow_self hpq.ne_zero] }, have hB : B = (∑' (i : ι), g i ^ q) ^ (1 / q), { rw [hg.tsum_eq, rpow_inv_rpow_self hpq.symm.ne_zero] }, refine ⟨∑' i, f i * g i, _, _⟩, { simpa [hA, hB] using H₂ }, { simpa only [rpow_self_rpow_inv hpq.ne_zero] using H₁.has_sum } end /-- For `1 ≤ p`, the `p`-th power of the sum of `f i` is bounded above by a constant times the sum of the `p`-th powers of `f i`. Version for sums over finite sets, with `ℝ≥0`-valued functions. -/ theorem rpow_sum_le_const_mul_sum_rpow (f : ι → ℝ≥0) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, f i) ^ p ≤ (card s) ^ (p - 1) * ∑ i in s, (f i) ^ p := begin cases eq_or_lt_of_le hp with hp hp, { simp [← hp] }, let q : ℝ := p / (p - 1), have hpq : p.is_conjugate_exponent q, { rw real.is_conjugate_exponent_iff hp }, have hp₁ : 1 / p * p = 1 := one_div_mul_cancel hpq.ne_zero, have hq : 1 / q * p = (p - 1), { rw [← hpq.div_conj_eq_sub_one], ring }, simpa only [nnreal.mul_rpow, ← nnreal.rpow_mul, hp₁, hq, one_mul, one_rpow, rpow_one, pi.one_apply, sum_const, nat.smul_one_eq_coe] using nnreal.rpow_le_rpow (inner_le_Lp_mul_Lq s 1 f hpq.symm) hpq.nonneg, end /-- The `L_p` seminorm of a vector `f` is the greatest value of the inner product `∑ i in s, f i * g i` over functions `g` of `L_q` seminorm less than or equal to one. -/ theorem is_greatest_Lp (f : ι → ℝ≥0) {p q : ℝ} (hpq : p.is_conjugate_exponent q) : is_greatest ((λ g : ι → ℝ≥0, ∑ i in s, f i * g i) '' {g | ∑ i in s, (g i)^q ≤ 1}) ((∑ i in s, (f i)^p) ^ (1 / p)) := begin split, { use λ i, ((f i) ^ p / f i / (∑ i in s, (f i) ^ p) ^ (1 / q)), by_cases hf : ∑ i in s, (f i)^p = 0, { simp [hf, hpq.ne_zero, hpq.symm.ne_zero] }, { have A : p + q - q ≠ 0, by simp [hpq.ne_zero], have B : ∀ y : ℝ≥0, y * y^p / y = y^p, { refine λ y, mul_div_cancel_left_of_imp (λ h, _), simpa [h, hpq.ne_zero] }, simp only [set.mem_set_of_eq, div_rpow, ← sum_div, ← rpow_mul, div_mul_cancel _ hpq.symm.ne_zero, rpow_one, div_le_iff hf, one_mul, hpq.mul_eq_add, ← rpow_sub' _ A, _root_.add_sub_cancel, le_refl, true_and, ← mul_div_assoc, B], rw [div_eq_iff, ← rpow_add hf, hpq.inv_add_inv_conj, rpow_one], simpa [hpq.symm.ne_zero] using hf } }, { rintros _ ⟨g, hg, rfl⟩, apply le_trans (inner_le_Lp_mul_Lq s f g hpq), simpa only [mul_one] using mul_le_mul_left' (nnreal.rpow_le_one hg (le_of_lt hpq.symm.one_div_pos)) _ } end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `nnreal`-valued functions. -/ theorem Lp_add_le (f g : ι → ℝ≥0) {p : ℝ} (hp : 1 ≤ p) : (∑ i in s, (f i + g i) ^ p) ^ (1 / p) ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) + (∑ i in s, (g i) ^ p) ^ (1 / p) := begin -- The result is trivial when `p = 1`, so we can assume `1 < p`. rcases eq_or_lt_of_le hp with rfl|hp, { simp [finset.sum_add_distrib] }, have hpq := real.is_conjugate_exponent_conjugate_exponent hp, have := is_greatest_Lp s (f + g) hpq, simp only [pi.add_apply, add_mul, sum_add_distrib] at this, rcases this.1 with ⟨φ, hφ, H⟩, rw ← H, exact add_le_add ((is_greatest_Lp s f hpq).2 ⟨φ, hφ, rfl⟩) ((is_greatest_Lp s g hpq).2 ⟨φ, hφ, rfl⟩) end /-- Minkowski inequality: the `L_p` seminorm of the infinite sum of two vectors is less than or equal to the infinite sum of the `L_p`-seminorms of the summands, if these infinite sums both exist. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are already expressed as `p`-th powers, see `Lp_add_le_has_sum_of_nonneg`. -/ theorem Lp_add_le_tsum {f g : ι → ℝ≥0} {p : ℝ} (hp : 1 ≤ p) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ p)) : summable (λ i, (f i + g i) ^ p) ∧ (∑' i, (f i + g i) ^ p) ^ (1 / p) ≤ (∑' i, (f i) ^ p) ^ (1 / p) + (∑' i, (g i) ^ p) ^ (1 / p) := begin have pos : 0 < p := lt_of_lt_of_le zero_lt_one hp, have H₁ : ∀ s : finset ι, ∑ i in s, (f i + g i) ^ p ≤ ((∑' i, (f i)^p) ^ (1/p) + (∑' i, (g i)^p) ^ (1/p)) ^ p, { intros s, rw ← nnreal.rpow_one_div_le_iff pos, refine le_trans (Lp_add_le s f g hp) (add_le_add _ _); rw nnreal.rpow_le_rpow_iff (one_div_pos.mpr pos); refine sum_le_tsum _ (λ _ _, zero_le _) _, exacts [hf, hg] }, have bdd : bdd_above (set.range (λ s, ∑ i in s, (f i + g i) ^ p)), { refine ⟨((∑' i, (f i)^p) ^ (1/p) + (∑' i, (g i)^p) ^ (1/p)) ^ p, _⟩, rintros a ⟨s, rfl⟩, exact H₁ s }, have H₂ : summable _ := (has_sum_of_is_lub _ (is_lub_csupr bdd)).summable, refine ⟨H₂, _⟩, rw nnreal.rpow_one_div_le_iff pos, refine tsum_le_of_sum_le H₂ H₁, end theorem summable_Lp_add {f g : ι → ℝ≥0} {p : ℝ} (hp : 1 ≤ p) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ p)) : summable (λ i, (f i + g i) ^ p) := (Lp_add_le_tsum hp hf hg).1 theorem Lp_add_le_tsum' {f g : ι → ℝ≥0} {p : ℝ} (hp : 1 ≤ p) (hf : summable (λ i, (f i) ^ p)) (hg : summable (λ i, (g i) ^ p)) : (∑' i, (f i + g i) ^ p) ^ (1 / p) ≤ (∑' i, (f i) ^ p) ^ (1 / p) + (∑' i, (g i) ^ p) ^ (1 / p) := (Lp_add_le_tsum hp hf hg).2 /-- Minkowski inequality: the `L_p` seminorm of the infinite sum of two vectors is less than or equal to the infinite sum of the `L_p`-seminorms of the summands, if these infinite sums both exist. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are not already expressed as `p`-th powers, see `Lp_add_le_tsum_of_nonneg`. -/ theorem Lp_add_le_has_sum {f g : ι → ℝ≥0} {A B : ℝ≥0} {p : ℝ} (hp : 1 ≤ p) (hf : has_sum (λ i, (f i) ^ p) (A ^ p)) (hg : has_sum (λ i, (g i) ^ p) (B ^ p)) : ∃ C, C ≤ A + B ∧ has_sum (λ i, (f i + g i) ^ p) (C ^ p) := begin have hp' : p ≠ 0 := (lt_of_lt_of_le zero_lt_one hp).ne', obtain ⟨H₁, H₂⟩ := Lp_add_le_tsum hp hf.summable hg.summable, have hA : A = (∑' (i : ι), f i ^ p) ^ (1 / p) := by rw [hf.tsum_eq, rpow_inv_rpow_self hp'], have hB : B = (∑' (i : ι), g i ^ p) ^ (1 / p) := by rw [hg.tsum_eq, rpow_inv_rpow_self hp'], refine ⟨(∑' i, (f i + g i) ^ p) ^ (1 / p), _, _⟩, { simpa [hA, hB] using H₂ }, { simpa only [rpow_self_rpow_inv hp'] using H₁.has_sum } end end nnreal namespace real variables (f g : ι → ℝ) {p q : ℝ} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : is_conjugate_exponent p q) : ∑ i in s, f i * g i ≤ (∑ i in s, |f i| ^ p) ^ (1 / p) * (∑ i in s, |g i| ^ q) ^ (1 / q) := begin have := nnreal.coe_le_coe.2 (nnreal.inner_le_Lp_mul_Lq s (λ i, ⟨_, abs_nonneg (f i)⟩) (λ i, ⟨_, abs_nonneg (g i)⟩) hpq), push_cast at this, refine le_trans (sum_le_sum $ λ i hi, _) this, simp only [← abs_mul, le_abs_self] end /-- For `1 ≤ p`, the `p`-th power of the sum of `f i` is bounded above by a constant times the sum of the `p`-th powers of `f i`. Version for sums over finite sets, with `ℝ`-valued functions. -/ theorem rpow_sum_le_const_mul_sum_rpow (hp : 1 ≤ p) : (∑ i in s, |f i|) ^ p ≤ (card s) ^ (p - 1) * ∑ i in s, |f i| ^ p := begin have := nnreal.coe_le_coe.2 (nnreal.rpow_sum_le_const_mul_sum_rpow s (λ i, ⟨_, abs_nonneg (f i)⟩) hp), push_cast at this, exact this, -- for some reason `exact_mod_cast` can't replace this argument end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `real`-valued functions. -/ theorem Lp_add_le (hp : 1 ≤ p) : (∑ i in s, |f i + g i| ^ p) ^ (1 / p) ≤ (∑ i in s, |f i| ^ p) ^ (1 / p) + (∑ i in s, |g i| ^ p) ^ (1 / p) := begin have := nnreal.coe_le_coe.2 (nnreal.Lp_add_le s (λ i, ⟨_, abs_nonneg (f i)⟩) (λ i, ⟨_, abs_nonneg (g i)⟩) hp), push_cast at this, refine le_trans (rpow_le_rpow _ (sum_le_sum $ λ i hi, _) _) this; simp [sum_nonneg, rpow_nonneg_of_nonneg, abs_nonneg, le_trans zero_le_one hp, abs_add, rpow_le_rpow] end variables {f g} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with real-valued nonnegative functions. -/ theorem inner_le_Lp_mul_Lq_of_nonneg (hpq : is_conjugate_exponent p q) (hf : ∀ i ∈ s, 0 ≤ f i) (hg : ∀ i ∈ s, 0 ≤ g i) : ∑ i in s, f i * g i ≤ (∑ i in s, (f i)^p) ^ (1 / p) * (∑ i in s, (g i)^q) ^ (1 / q) := by convert inner_le_Lp_mul_Lq s f g hpq using 3; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi] /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. A version for `ℝ`-valued functions. For an alternative version, convenient if the infinite sums are already expressed as `p`-th powers, see `inner_le_Lp_mul_Lq_has_sum_of_nonneg`. -/ theorem inner_le_Lp_mul_Lq_tsum_of_nonneg (hpq : p.is_conjugate_exponent q) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ q)) : summable (λ i, f i * g i) ∧ ∑' i, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q) := begin lift f to (ι → ℝ≥0) using hf, lift g to (ι → ℝ≥0) using hg, norm_cast at *, exact nnreal.inner_le_Lp_mul_Lq_tsum hpq hf_sum hg_sum, end theorem summable_mul_of_Lp_Lq_of_nonneg (hpq : p.is_conjugate_exponent q) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ q)) : summable (λ i, f i * g i) := (inner_le_Lp_mul_Lq_tsum_of_nonneg hpq hf hg hf_sum hg_sum).1 theorem inner_le_Lp_mul_Lq_tsum_of_nonneg' (hpq : p.is_conjugate_exponent q) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ q)) : ∑' i, f i * g i ≤ (∑' i, (f i) ^ p) ^ (1 / p) * (∑' i, (g i) ^ q) ^ (1 / q) := (inner_le_Lp_mul_Lq_tsum_of_nonneg hpq hf hg hf_sum hg_sum).2 /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. A version for `nnreal`-valued functions. For an alternative version, convenient if the infinite sums are not already expressed as `p`-th powers, see `inner_le_Lp_mul_Lq_tsum_of_nonneg`. -/ theorem inner_le_Lp_mul_Lq_has_sum_of_nonneg (hpq : p.is_conjugate_exponent q) {A B : ℝ} (hA : 0 ≤ A) (hB : 0 ≤ B) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : has_sum (λ i, (f i) ^ p) (A ^ p)) (hg_sum : has_sum (λ i, (g i) ^ q) (B ^ q)) : ∃ C : ℝ, 0 ≤ C ∧ C ≤ A * B ∧ has_sum (λ i, f i * g i) C := begin lift f to (ι → ℝ≥0) using hf, lift g to (ι → ℝ≥0) using hg, lift A to ℝ≥0 using hA, lift B to ℝ≥0 using hB, norm_cast at hf_sum hg_sum, obtain ⟨C, hC, H⟩ := nnreal.inner_le_Lp_mul_Lq_has_sum hpq hf_sum hg_sum, refine ⟨C, C.prop, hC, _⟩, norm_cast, exact H end /-- For `1 ≤ p`, the `p`-th power of the sum of `f i` is bounded above by a constant times the sum of the `p`-th powers of `f i`. Version for sums over finite sets, with nonnegative `ℝ`-valued functions. -/ theorem rpow_sum_le_const_mul_sum_rpow_of_nonneg (hp : 1 ≤ p) (hf : ∀ i ∈ s, 0 ≤ f i) : (∑ i in s, f i) ^ p ≤ (card s) ^ (p - 1) * ∑ i in s, f i ^ p := by convert rpow_sum_le_const_mul_sum_rpow s f hp using 2; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi] /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `ℝ`-valued nonnegative functions. -/ theorem Lp_add_le_of_nonneg (hp : 1 ≤ p) (hf : ∀ i ∈ s, 0 ≤ f i) (hg : ∀ i ∈ s, 0 ≤ g i) : (∑ i in s, (f i + g i) ^ p) ^ (1 / p) ≤ (∑ i in s, (f i) ^ p) ^ (1 / p) + (∑ i in s, (g i) ^ p) ^ (1 / p) := by convert Lp_add_le s f g hp using 2 ; [skip, congr' 1, congr' 1]; apply sum_congr rfl; intros i hi; simp only [abs_of_nonneg, hf i hi, hg i hi, add_nonneg] /-- Minkowski inequality: the `L_p` seminorm of the infinite sum of two vectors is less than or equal to the infinite sum of the `L_p`-seminorms of the summands, if these infinite sums both exist. A version for `ℝ`-valued functions. For an alternative version, convenient if the infinite sums are already expressed as `p`-th powers, see `Lp_add_le_has_sum_of_nonneg`. -/ theorem Lp_add_le_tsum_of_nonneg (hp : 1 ≤ p) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ p)) : summable (λ i, (f i + g i) ^ p) ∧ (∑' i, (f i + g i) ^ p) ^ (1 / p) ≤ (∑' i, (f i) ^ p) ^ (1 / p) + (∑' i, (g i) ^ p) ^ (1 / p) := begin lift f to (ι → ℝ≥0) using hf, lift g to (ι → ℝ≥0) using hg, norm_cast at *, exact nnreal.Lp_add_le_tsum hp hf_sum hg_sum, end theorem summable_Lp_add_of_nonneg (hp : 1 ≤ p) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ p)) : summable (λ i, (f i + g i) ^ p) := (Lp_add_le_tsum_of_nonneg hp hf hg hf_sum hg_sum).1 theorem Lp_add_le_tsum_of_nonneg' (hp : 1 ≤ p) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) (hf_sum : summable (λ i, (f i) ^ p)) (hg_sum : summable (λ i, (g i) ^ p)) : (∑' i, (f i + g i) ^ p) ^ (1 / p) ≤ (∑' i, (f i) ^ p) ^ (1 / p) + (∑' i, (g i) ^ p) ^ (1 / p) := (Lp_add_le_tsum_of_nonneg hp hf hg hf_sum hg_sum).2 /-- Minkowski inequality: the `L_p` seminorm of the infinite sum of two vectors is less than or equal to the infinite sum of the `L_p`-seminorms of the summands, if these infinite sums both exist. A version for `ℝ`-valued functions. For an alternative version, convenient if the infinite sums are not already expressed as `p`-th powers, see `Lp_add_le_tsum_of_nonneg`. -/ theorem Lp_add_le_has_sum_of_nonneg (hp : 1 ≤ p) (hf : ∀ i, 0 ≤ f i) (hg : ∀ i, 0 ≤ g i) {A B : ℝ} (hA : 0 ≤ A) (hB : 0 ≤ B) (hfA : has_sum (λ i, (f i) ^ p) (A ^ p)) (hgB : has_sum (λ i, (g i) ^ p) (B ^ p)) : ∃ C, 0 ≤ C ∧ C ≤ A + B ∧ has_sum (λ i, (f i + g i) ^ p) (C ^ p) := begin lift f to (ι → ℝ≥0) using hf, lift g to (ι → ℝ≥0) using hg, lift A to ℝ≥0 using hA, lift B to ℝ≥0 using hB, norm_cast at hfA hgB, obtain ⟨C, hC₁, hC₂⟩ := nnreal.Lp_add_le_has_sum hp hfA hgB, use C, norm_cast, exact ⟨zero_le _, hC₁, hC₂⟩, end end real namespace ennreal variables (f g : ι → ℝ≥0∞) {p q : ℝ} /-- Hölder inequality: the scalar product of two functions is bounded by the product of their `L^p` and `L^q` norms when `p` and `q` are conjugate exponents. Version for sums over finite sets, with `ℝ≥0∞`-valued functions. -/ theorem inner_le_Lp_mul_Lq (hpq : p.is_conjugate_exponent q) : (∑ i in s, f i * g i) ≤ (∑ i in s, (f i)^p) ^ (1/p) * (∑ i in s, (g i)^q) ^ (1/q) := begin by_cases H : (∑ i in s, (f i)^p) ^ (1/p) = 0 ∨ (∑ i in s, (g i)^q) ^ (1/q) = 0, { replace H : (∀ i ∈ s, f i = 0) ∨ (∀ i ∈ s, g i = 0), by simpa [ennreal.rpow_eq_zero_iff, hpq.pos, hpq.symm.pos, asymm hpq.pos, asymm hpq.symm.pos, sum_eq_zero_iff_of_nonneg] using H, have : ∀ i ∈ s, f i * g i = 0 := λ i hi, by cases H; simp [H i hi], have : (∑ i in s, f i * g i) = (∑ i in s, 0) := sum_congr rfl this, simp [this] }, push_neg at H, by_cases H' : (∑ i in s, (f i)^p) ^ (1/p) = ⊤ ∨ (∑ i in s, (g i)^q) ^ (1/q) = ⊤, { cases H'; simp [H', -one_div, H] }, replace H' : (∀ i ∈ s, f i ≠ ⊤) ∧ (∀ i ∈ s, g i ≠ ⊤), by simpa [ennreal.rpow_eq_top_iff, asymm hpq.pos, asymm hpq.symm.pos, hpq.pos, hpq.symm.pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.inner_le_Lp_mul_Lq _ s (λ i, ennreal.to_nnreal (f i)) (λ i, ennreal.to_nnreal (g i)) _ _ hpq), simp [← ennreal.coe_rpow_of_nonneg, le_of_lt (hpq.pos), le_of_lt (hpq.one_div_pos), le_of_lt (hpq.symm.pos), le_of_lt (hpq.symm.one_div_pos)] at this, convert this using 1; [skip, congr' 2]; [skip, skip, simp, skip, simp]; { apply finset.sum_congr rfl (λ i hi, _), simp [H'.1 i hi, H'.2 i hi, -with_zero.coe_mul, with_top.coe_mul.symm] }, end /-- For `1 ≤ p`, the `p`-th power of the sum of `f i` is bounded above by a constant times the sum of the `p`-th powers of `f i`. Version for sums over finite sets, with `ℝ≥0∞`-valued functions. -/ theorem rpow_sum_le_const_mul_sum_rpow (hp : 1 ≤ p) : (∑ i in s, f i) ^ p ≤ (card s) ^ (p - 1) * ∑ i in s, (f i) ^ p := begin cases eq_or_lt_of_le hp with hp hp, { simp [← hp] }, let q : ℝ := p / (p - 1), have hpq : p.is_conjugate_exponent q, { rw real.is_conjugate_exponent_iff hp }, have hp₁ : 1 / p * p = 1 := one_div_mul_cancel hpq.ne_zero, have hq : 1 / q * p = (p - 1), { rw [← hpq.div_conj_eq_sub_one], ring }, simpa only [ennreal.mul_rpow_of_nonneg _ _ hpq.nonneg, ← ennreal.rpow_mul, hp₁, hq, coe_one, one_mul, one_rpow, rpow_one, pi.one_apply, sum_const, nat.smul_one_eq_coe] using ennreal.rpow_le_rpow (inner_le_Lp_mul_Lq s 1 f hpq.symm) hpq.nonneg, end /-- Minkowski inequality: the `L_p` seminorm of the sum of two vectors is less than or equal to the sum of the `L_p`-seminorms of the summands. A version for `ℝ≥0∞` valued nonnegative functions. -/ theorem Lp_add_le (hp : 1 ≤ p) : (∑ i in s, (f i + g i) ^ p)^(1/p) ≤ (∑ i in s, (f i)^p) ^ (1/p) + (∑ i in s, (g i)^p) ^ (1/p) := begin by_cases H' : (∑ i in s, (f i)^p) ^ (1/p) = ⊤ ∨ (∑ i in s, (g i)^p) ^ (1/p) = ⊤, { cases H'; simp [H', -one_div] }, have pos : 0 < p := lt_of_lt_of_le zero_lt_one hp, replace H' : (∀ i ∈ s, f i ≠ ⊤) ∧ (∀ i ∈ s, g i ≠ ⊤), by simpa [ennreal.rpow_eq_top_iff, asymm pos, pos, ennreal.sum_eq_top_iff, not_or_distrib] using H', have := ennreal.coe_le_coe.2 (@nnreal.Lp_add_le _ s (λ i, ennreal.to_nnreal (f i)) (λ i, ennreal.to_nnreal (g i)) _ hp), push_cast [← ennreal.coe_rpow_of_nonneg, le_of_lt (pos), le_of_lt (one_div_pos.2 pos)] at this, convert this using 2; [skip, congr' 1, congr' 1]; { apply finset.sum_congr rfl (λ i hi, _), simp [H'.1 i hi, H'.2 i hi] } end end ennreal end holder_minkowski
e9c366abca601a7775179a65f331c6ab27f1764c
4727251e0cd73359b15b664c3170e5d754078599
/src/data/set/opposite.lean
f2866a025d0995f8655a32538dc593ac551ca88d
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
2,093
lean
/- Copyright (c) 2022 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import data.opposite import data.set.basic /-! # The opposite of a set The opposite of a set `s` is simply the set obtained by taking the opposite of each member of `s`. -/ variables {α : Type*} open opposite namespace set /-- The opposite of a set `s` is the set obtained by taking the opposite of each member of `s`. -/ protected def op (s : set α) : set αᵒᵖ := unop ⁻¹' s /-- The unop of a set `s` is the set obtained by taking the unop of each member of `s`. -/ protected def unop (s : set αᵒᵖ) : set α := op ⁻¹' s @[simp] lemma mem_op {s : set α} {a : αᵒᵖ} : a ∈ s.op ↔ unop a ∈ s := iff.rfl @[simp] lemma op_mem_op {s : set α} {a : α} : op a ∈ s.op ↔ a ∈ s := by rw [mem_op, unop_op] @[simp] lemma mem_unop {s : set αᵒᵖ} {a : α} : a ∈ s.unop ↔ op a ∈ s := iff.rfl @[simp] lemma unop_mem_unop {s : set αᵒᵖ} {a : αᵒᵖ} : unop a ∈ s.unop ↔ a ∈ s := by rw [mem_unop, op_unop] @[simp] lemma op_unop (s : set α) : s.op.unop = s := ext (by simp only [mem_unop, op_mem_op, iff_self, implies_true_iff]) @[simp] lemma unop_op (s : set αᵒᵖ) : s.unop.op = s := ext (by simp only [mem_op, unop_mem_unop, iff_self, implies_true_iff]) /-- Taking opposites as an equivalence of powersets. -/ @[simps] def op_equiv : set α ≃ set αᵒᵖ := ⟨set.op, set.unop, op_unop, unop_op⟩ @[simp] lemma singleton_op (x : α) : ({x} : set α).op = {op x} := ext $ λ y, by simpa only [mem_op, mem_singleton_iff] using unop_eq_iff_eq_op @[simp] lemma singleton_unop (x : αᵒᵖ) : ({x} : set αᵒᵖ).unop = {unop x} := ext $ λ y, by simpa only [mem_unop, mem_singleton_iff] using op_eq_iff_eq_unop @[simp] lemma singleton_op_unop (x : α) : ({op x} : set αᵒᵖ).unop = {x} := by simp only [singleton_unop, opposite.unop_op] @[simp] lemma singleton_unop_op (x : αᵒᵖ) : ({unop x} : set α).op = {x} := by simp only [singleton_op, opposite.op_unop] end set
f93098dbe9f61ee8f99288d86cc7845a44da9d84
9a0192b31a07f48502dacee8122e0b8beda7b110
/lista4.lean
24c5cbeb9ab6a67e7c2a96394613caebb4a006cc
[]
no_license
ana-/Lean
8a5b9f2e13685bd43efd72c6665401c0851157cb
4527da83de947fa1df368b2f9bcf322a4d14e121
refs/heads/master
1,593,588,619,552
1,565,196,720,000
1,565,196,720,000
201,090,102
0
0
null
null
null
null
UTF-8
Lean
false
false
631
lean
/- Lista 3 do EAD Questão 3- Existem duas caixas, A e B. Um aviso na caixa A diz “O aviso na caixa B é falso e o ouro está na caixa B”. Um aviso na caixa B diz “O aviso na caixa A é verdadeiro e o ouro está na caixa B”. Assumindo que existe ouro em uma das caixas, qual delas contém o ouro? Justifique sua resposta em termos lógicos. A : caixa A B: caixa B v : O aviso na caixa é verdadeiro O : ouro na caixa -/ --resposta variable U : Type variable O : U variable V : U variables A B : U → Prop #check ((A V ↔ (¬ B V ∧ B O)) ∧ (B V ↔ (A V ∧ B O )) ∧ (A O ∨ B O)) → A O
060dbdf700ff47ae8f9f8700491b37a38d1740bf
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/tactic/abel.lean
dbd6a874b02a6bd73deefd97c1e17a9d771658c1
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
13,361
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.group_power tactic.norm_num /-! # The `abel` tactic Evaluate expressions in the language of additive, commutative monoids and groups. -/ namespace tactic namespace abel meta structure cache := (α : expr) (univ : level) (α0 : expr) (is_group : bool) (inst : expr) meta def mk_cache (e : expr) : tactic cache := do α ← infer_type e, c ← mk_app ``add_comm_monoid [α] >>= mk_instance, cg ← try_core (mk_app ``add_comm_group [α] >>= mk_instance), u ← mk_meta_univ, infer_type α >>= unify (expr.sort (level.succ u)), u ← get_univ_assignment u, α0 ← expr.of_nat α 0, match cg with | (some cg) := return ⟨α, u, α0, tt, cg⟩ | _ := return ⟨α, u, α0, ff, c⟩ end meta def cache.app (c : cache) (n : name) (inst : expr) : list expr → expr := (@expr.const tt n [c.univ] c.α inst).mk_app meta def cache.mk_app (c : cache) (n inst : name) (l : list expr) : tactic expr := do m ← mk_instance ((expr.const inst [c.univ] : expr) c.α), return $ c.app n m l meta def add_g : name → name | (name.mk_string s p) := name.mk_string (s ++ "g") p | n := n meta def cache.iapp (c : cache) (n : name) : list expr → expr := c.app (if c.is_group then add_g n else n) c.inst def term {α} [add_comm_monoid α] (n : ℕ) (x a : α) : α := add_monoid.smul n x + a def termg {α} [add_comm_group α] (n : ℤ) (x a : α) : α := gsmul n x + a meta def cache.mk_term (c : cache) (n x a : expr) : expr := c.iapp ``term [n, x, a] meta def cache.int_to_expr (c : cache) (n : ℤ) : tactic expr := expr.of_int (if c.is_group then `(ℤ) else `(ℕ)) n meta inductive normal_expr : Type | zero (e : expr) : normal_expr | nterm (e : expr) (n : expr × ℤ) (x : expr) (a : normal_expr) : normal_expr meta def normal_expr.e : normal_expr → expr | (normal_expr.zero e) := e | (normal_expr.nterm e _ _ _) := e meta instance : has_coe normal_expr expr := ⟨normal_expr.e⟩ meta def normal_expr.term' (c : cache) (n : expr × ℤ) (x : expr) (a : normal_expr) : normal_expr := normal_expr.nterm (c.mk_term n.1 x a) n x a meta def normal_expr.zero' (c : cache) : normal_expr := normal_expr.zero c.α0 meta def normal_expr.to_list : normal_expr → list (ℤ × expr) | (normal_expr.zero _) := [] | (normal_expr.nterm _ (_, n) x a) := (n, x) :: a.to_list open normal_expr meta def normal_expr.to_string (e : normal_expr) : string := " + ".intercalate $ (to_list e).map $ λ ⟨n, e⟩, to_string n ++ " • (" ++ to_string e ++ ")" meta def normal_expr.pp (e : normal_expr) : tactic format := do l ← (to_list e).mmap (λ ⟨n, e⟩, do pe ← pp e, return (to_fmt n ++ " • (" ++ pe ++ ")")), return $ format.join $ l.intersperse ↑" + " meta instance : has_to_tactic_format normal_expr := ⟨normal_expr.pp⟩ meta def normal_expr.refl_conv (e : normal_expr) : tactic (normal_expr × expr) := do p ← mk_eq_refl e, return (e, p) theorem const_add_term {α} [add_comm_monoid α] (k n x a a') (h : k + a = a') : k + @term α _ n x a = term n x a' := by simp [h.symm, term]; ac_refl theorem const_add_termg {α} [add_comm_group α] (k n x a a') (h : k + a = a') : k + @termg α _ n x a = termg n x a' := by simp [h.symm, termg]; ac_refl theorem term_add_const {α} [add_comm_monoid α] (n x a k a') (h : a + k = a') : @term α _ n x a + k = term n x a' := by simp [h.symm, term] theorem term_add_constg {α} [add_comm_group α] (n x a k a') (h : a + k = a') : @termg α _ n x a + k = termg n x a' := by simp [h.symm, termg] theorem term_add_term {α} [add_comm_monoid α] (n₁ x a₁ n₂ a₂ n' a') (h₁ : n₁ + n₂ = n') (h₂ : a₁ + a₂ = a') : @term α _ n₁ x a₁ + @term α _ n₂ x a₂ = term n' x a' := by simp [h₁.symm, h₂.symm, term, add_monoid.add_smul]; ac_refl theorem term_add_termg {α} [add_comm_group α] (n₁ x a₁ n₂ a₂ n' a') (h₁ : n₁ + n₂ = n') (h₂ : a₁ + a₂ = a') : @termg α _ n₁ x a₁ + @termg α _ n₂ x a₂ = termg n' x a' := by simp [h₁.symm, h₂.symm, termg, add_gsmul]; ac_refl theorem zero_term {α} [add_comm_monoid α] (x a) : @term α _ 0 x a = a := by simp [term] theorem zero_termg {α} [add_comm_group α] (x a) : @termg α _ 0 x a = a := by simp [termg] meta def eval_add (c : cache) : normal_expr → normal_expr → tactic (normal_expr × expr) | (zero _) e₂ := do p ← mk_app ``zero_add [e₂], return (e₂, p) | e₁ (zero _) := do p ← mk_app ``add_zero [e₁], return (e₁, p) | he₁@(nterm e₁ n₁ x₁ a₁) he₂@(nterm e₂ n₂ x₂ a₂) := if expr.lex_lt x₁ x₂ then do (a', h) ← eval_add a₁ he₂, return (term' c n₁ x₁ a', c.iapp ``term_add_const [n₁.1, x₁, a₁, e₂, a', h]) else if x₁ ≠ x₂ then do (a', h) ← eval_add he₁ a₂, return (term' c n₂ x₂ a', c.iapp ``const_add_term [e₁, n₂.1, x₂, a₂, a', h]) else do (n', h₁) ← mk_app ``has_add.add [n₁.1, n₂.1] >>= norm_num, (a', h₂) ← eval_add a₁ a₂, let k := n₁.2 + n₂.2, let p₁ := c.iapp ``term_add_term [n₁.1, x₁, a₁, n₂.1, a₂, n', a', h₁, h₂], if k = 0 then do p ← mk_eq_trans p₁ (c.iapp ``zero_term [x₁, a']), return (a', p) else return (term' c (n', k) x₁ a', p₁) theorem term_neg {α} [add_comm_group α] (n x a n' a') (h₁ : -n = n') (h₂ : -a = a') : -@termg α _ n x a = termg n' x a' := by simp [h₂.symm, h₁.symm, termg]; ac_refl meta def eval_neg (c : cache) : normal_expr → tactic (normal_expr × expr) | (zero e) := do p ← c.mk_app ``neg_zero ``add_group [], return (zero' c, p) | (nterm e n x a) := do (n', h₁) ← mk_app ``has_neg.neg [n.1] >>= norm_num, (a', h₂) ← eval_neg a, return (term' c (n', -n.2) x a', c.app ``term_neg c.inst [n.1, x, a, n', a', h₁, h₂]) def smul {α} [add_comm_monoid α] (n : ℕ) (x : α) : α := add_monoid.smul n x def smulg {α} [add_comm_group α] (n : ℤ) (x : α) : α := gsmul n x theorem zero_smul {α} [add_comm_monoid α] (c) : smul c (0 : α) = 0 := by simp [smul] theorem zero_smulg {α} [add_comm_group α] (c) : smulg c (0 : α) = 0 := by simp [smulg] theorem term_smul {α} [add_comm_monoid α] (c n x a n' a') (h₁ : c * n = n') (h₂ : smul c a = a') : smul c (@term α _ n x a) = term n' x a' := by simp [h₂.symm, h₁.symm, term, smul, add_monoid.smul_add, add_monoid.mul_smul] theorem term_smulg {α} [add_comm_group α] (c n x a n' a') (h₁ : c * n = n') (h₂ : smulg c a = a') : smulg c (@termg α _ n x a) = termg n' x a' := by simp [h₂.symm, h₁.symm, termg, smulg, gsmul_add, gsmul_mul] meta def eval_smul (c : cache) (k : expr × ℤ) : normal_expr → tactic (normal_expr × expr) | (zero _) := return (zero' c, c.iapp ``zero_smul [k.1]) | (nterm e n x a) := do (n', h₁) ← mk_app ``has_mul.mul [k.1, n.1] >>= norm_num, (a', h₂) ← eval_smul a, return (term' c (n', k.2 * n.2) x a', c.iapp ``term_smul [k.1, n.1, x, a, n', a', h₁, h₂]) theorem term_atom {α} [add_comm_monoid α] (x : α) : x = term 1 x 0 := by simp [term] theorem term_atomg {α} [add_comm_group α] (x : α) : x = termg 1 x 0 := by simp [termg] meta def eval_atom (c : cache) (e : expr) : tactic (normal_expr × expr) := do n1 ← c.int_to_expr 1, return (term' c (n1, 1) e (zero' c), c.iapp ``term_atom [e]) lemma unfold_sub {α} [add_group α] (a b c : α) (h : a + -b = c) : a - b = c := h theorem unfold_smul {α} [add_comm_monoid α] (n) (x y : α) (h : smul n x = y) : add_monoid.smul n x = y := h theorem unfold_smulg {α} [add_comm_group α] (n : ℕ) (x y : α) (h : smulg (int.of_nat n) x = y) : add_monoid.smul n x = y := h theorem unfold_gsmul {α} [add_comm_group α] (n : ℤ) (x y : α) (h : smulg n x = y) : gsmul n x = y := h lemma subst_into_smul {α} [add_comm_monoid α] (l r tl tr t) (prl : l = tl) (prr : r = tr) (prt : @smul α _ tl tr = t) : smul l r = t := by simp [prl, prr, prt] lemma subst_into_smulg {α} [add_comm_group α] (l r tl tr t) (prl : l = tl) (prr : r = tr) (prt : @smulg α _ tl tr = t) : smulg l r = t := by simp [prl, prr, prt] meta def eval (c : cache) : expr → tactic (normal_expr × expr) | `(%%e₁ + %%e₂) := do (e₁', p₁) ← eval e₁, (e₂', p₂) ← eval e₂, (e', p') ← eval_add c e₁' e₂', p ← c.mk_app ``norm_num.subst_into_sum ``has_add [e₁, e₂, e₁', e₂', e', p₁, p₂, p'], return (e', p) | `(%%e₁ - %%e₂) := do e₂' ← mk_app ``has_neg.neg [e₂], e ← mk_app ``has_add.add [e₁, e₂'], (e', p) ← eval e, p' ← c.mk_app ``unfold_sub ``add_group [e₁, e₂, e', p], return (e', p') | `(- %%e) := do (e₁, p₁) ← eval e, (e₂, p₂) ← eval_neg c e₁, p ← c.mk_app ``norm_num.subst_into_neg ``has_neg [e, e₁, e₂, p₁, p₂], return (e₂, p) | `(add_monoid.smul %%e₁ %%e₂) := do n ← if c.is_group then mk_app ``int.of_nat [e₁] else return e₁, (e', p) ← eval $ c.iapp ``smul [n, e₂], return (e', c.iapp ``unfold_smul [e₁, e₂, e', p]) | `(gsmul %%e₁ %%e₂) := do guardb c.is_group, (e', p) ← eval $ c.iapp ``smul [e₁, e₂], return (e', c.app ``unfold_gsmul c.inst [e₁, e₂, e', p]) | `(smul %%e₁ %%e₂) := do guard (¬ c.is_group), (e₁', p₁) ← norm_num.derive e₁ <|> refl_conv e₁, n ← e₁'.to_nat, (e₂', p₂) ← eval e₂, (e', p) ← eval_smul c (e₁', n) e₂', return (e', c.iapp ``subst_into_smul [e₁, e₂, e₁', e₂', e', p₁, p₂, p]) | `(smulg %%e₁ %%e₂) := do guardb c.is_group, (e₁', p₁) ← norm_num.derive e₁ <|> refl_conv e₁, n ← e₁'.to_int, (e₂', p₂) ← eval e₂, (e', p) ← eval_smul c (e₁', n) e₂', return (e', c.iapp ``subst_into_smul [e₁, e₂, e₁', e₂', e', p₁, p₂, p]) | e := eval_atom c e meta def eval' (c : cache) (e : expr) : tactic (expr × expr) := do (e', p) ← eval c e, return (e', p) @[derive has_reflect] inductive normalize_mode | raw | term instance : inhabited normalize_mode := ⟨normalize_mode.term⟩ meta def normalize (mode := normalize_mode.term) (e : expr) : tactic (expr × expr) := do pow_lemma ← simp_lemmas.mk.add_simp ``pow_one, let lemmas := match mode with | normalize_mode.term := [``term.equations._eqn_1, ``termg.equations._eqn_1, ``add_zero, ``add_monoid.one_smul, ``one_gsmul] | _ := [] end, lemmas ← lemmas.mfoldl simp_lemmas.add_simp simp_lemmas.mk, (_, e', pr) ← ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ e, do c ← mk_cache e, (new_e, pr) ← match mode with | normalize_mode.raw := eval' c | normalize_mode.term := trans_conv (eval' c) (simplify lemmas []) end e, guard (¬ new_e =ₐ e), return ((), new_e, some pr, ff)) (λ _ _ _ _ _, failed) `eq e, return (e', pr) end abel namespace interactive open interactive interactive.types lean.parser open tactic.abel local postfix `?`:9001 := optional /-- Tactic for solving equations in the language of *additive*, commutative monoids and groups. This version of `abel` fails if the target is not an equality that is provable by the axioms of commutative monoids/groups. -/ meta def abel1 : tactic unit := do `(%%e₁ = %%e₂) ← target, c ← mk_cache e₁, (e₁', p₁) ← eval c e₁, (e₂', p₂) ← eval c e₂, is_def_eq e₁' e₂', p ← mk_eq_symm p₂ >>= mk_eq_trans p₁, tactic.exact p meta def abel.mode : lean.parser abel.normalize_mode := with_desc "(raw|term)?" $ do mode ← ident?, match mode with | none := return abel.normalize_mode.term | some `term := return abel.normalize_mode.term | some `raw := return abel.normalize_mode.raw | _ := failed end /-- Tactic for solving equations in the language of *additive*, commutative monoids and groups. Attempts to prove the goal outright if there is no `at` specifier and the target is an equality, but if this fails it falls back to rewriting all monoid expressions into a normal form. --- Evaluate expressions in the language of *additive*, commutative monoids and groups. It attempts to prove the goal outright if there is no `at` specifier and the target is an equality, but if this fails, it falls back to rewriting all monoid expressions into a normal form. If there is an `at` specifier, it rewrites the given target into a normal form. ```lean example {α : Type*} {a b : α} [add_comm_monoid α] : a + (b + a) = a + a + b := by abel example {α : Type*} {a b : α} [add_comm_group α] : (a + b) - ((b + a) + a) = -a := by abel example {α : Type*} {a b : α} [add_comm_group α] (hyp : a + a - a = b - b) : a = 0 := by { abel at hyp, exact hyp } ``` -/ meta def abel (SOP : parse abel.mode) (loc : parse location) : tactic unit := match loc with | interactive.loc.ns [none] := abel1 | _ := failed end <|> do ns ← loc.get_locals, tt ← tactic.replace_at (normalize SOP) ns loc.include_goal | fail "abel failed to simplify", when loc.include_goal $ try tactic.reflexivity add_tactic_doc { name := "abel", category := doc_category.tactic, decl_names := [`tactic.interactive.abel], tags := ["arithmetic", "decision procedure"] } end interactive end tactic
2ef000b255dfeb9744faf95fc0248ad0edc0bdd7
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Compiler/IR/NormIds.lean
13de2760c3e5db6a2fcc44029c56b1cfd40c7371
[ "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
7,366
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.IR.Basic namespace Lean namespace IR namespace UniqueIds abbrev M := StateT IndexSet Id def checkId (id : Index) : M Bool := modifyGet $ fun s => if s.contains id then (false, s) else (true, s.insert id) def checkParams (ps : Array Param) : M Bool := ps.allM $ fun p => checkId p.x.idx partial def checkFnBody : FnBody → M Bool | FnBody.vdecl x _ _ b => checkId x.idx <&&> checkFnBody b | FnBody.jdecl j ys _ b => checkId j.idx <&&> checkParams ys <&&> checkFnBody b | FnBody.case _ _ _ alts => alts.allM $ fun alt => checkFnBody alt.body | b => if b.isTerminal then pure true else checkFnBody b.body partial def checkDecl : Decl → M Bool | Decl.fdecl _ xs _ b => checkParams xs <&&> checkFnBody b | Decl.extern _ xs _ _ => checkParams xs end UniqueIds /- Return true if variable, parameter and join point ids are unique -/ def Decl.uniqueIds (d : Decl) : Bool := (UniqueIds.checkDecl d).run' {} namespace NormalizeIds abbrev M := ReaderT IndexRenaming Id def normIndex (x : Index) : M Index := fun m => match m.find? x with | some y => y | none => x def normVar (x : VarId) : M VarId := VarId.mk <$> normIndex x.idx def normJP (x : JoinPointId) : M JoinPointId := JoinPointId.mk <$> normIndex x.idx def normArg : Arg → M Arg | Arg.var x => Arg.var <$> normVar x | other => pure other def normArgs (as : Array Arg) : M (Array Arg) := fun m => as.map $ fun a => normArg a m def normExpr : Expr → M Expr | Expr.ctor c ys, m => Expr.ctor c (normArgs ys m) | Expr.reset n x, m => Expr.reset n (normVar x m) | Expr.reuse x c u ys, m => Expr.reuse (normVar x m) c u (normArgs ys m) | Expr.proj i x, m => Expr.proj i (normVar x m) | Expr.uproj i x, m => Expr.uproj i (normVar x m) | Expr.sproj n o x, m => Expr.sproj n o (normVar x m) | Expr.fap c ys, m => Expr.fap c (normArgs ys m) | Expr.pap c ys, m => Expr.pap c (normArgs ys m) | Expr.ap x ys, m => Expr.ap (normVar x m) (normArgs ys m) | Expr.box t x, m => Expr.box t (normVar x m) | Expr.unbox x, m => Expr.unbox (normVar x m) | Expr.isShared x, m => Expr.isShared (normVar x m) | Expr.isTaggedPtr x, m => Expr.isTaggedPtr (normVar x m) | e@(Expr.lit v), m => e abbrev N := ReaderT IndexRenaming (StateM Nat) @[inline] def withVar {α : Type} (x : VarId) (k : VarId → N α) : N α := fun m => do n ← getModify (fun n => n + 1); k { idx := n } (m.insert x.idx n) @[inline] def withJP {α : Type} (x : JoinPointId) (k : JoinPointId → N α) : N α := fun m => do n ← getModify (fun n => n + 1); k { idx := n } (m.insert x.idx n) @[inline] def withParams {α : Type} (ps : Array Param) (k : Array Param → N α) : N α := fun m => do m ← ps.foldlM (fun (m : IndexRenaming) p => do n ← getModify (fun n => n + 1); pure $ m.insert p.x.idx n) m; let ps := ps.map $ fun p => { p with x := normVar p.x m }; k ps m instance MtoN {α} : HasCoe (M α) (N α) := ⟨fun x m => pure $ x m⟩ partial def normFnBody : FnBody → N FnBody | FnBody.vdecl x t v b => do v ← normExpr v; withVar x $ fun x => FnBody.vdecl x t v <$> normFnBody b | FnBody.jdecl j ys v b => do (ys, v) ← withParams ys $ fun ys => do { v ← normFnBody v; pure (ys, v) }; withJP j $ fun j => FnBody.jdecl j ys v <$> normFnBody b | FnBody.set x i y b => do x ← normVar x; y ← normArg y; FnBody.set x i y <$> normFnBody b | FnBody.uset x i y b => do x ← normVar x; y ← normVar y; FnBody.uset x i y <$> normFnBody b | FnBody.sset x i o y t b => do x ← normVar x; y ← normVar y; FnBody.sset x i o y t <$> normFnBody b | FnBody.setTag x i b => do x ← normVar x; FnBody.setTag x i <$> normFnBody b | FnBody.inc x n c p b => do x ← normVar x; FnBody.inc x n c p <$> normFnBody b | FnBody.dec x n c p b => do x ← normVar x; FnBody.dec x n c p <$> normFnBody b | FnBody.del x b => do x ← normVar x; FnBody.del x <$> normFnBody b | FnBody.mdata d b => FnBody.mdata d <$> normFnBody b | FnBody.case tid x xType alts => do x ← normVar x; alts ← alts.mapM $ fun alt => alt.mmodifyBody normFnBody; pure $ FnBody.case tid x xType alts | FnBody.jmp j ys => FnBody.jmp <$> normJP j <*> normArgs ys | FnBody.ret x => FnBody.ret <$> normArg x | FnBody.unreachable => pure FnBody.unreachable def normDecl : Decl → N Decl | Decl.fdecl f xs t b => withParams xs $ fun xs => Decl.fdecl f xs t <$> normFnBody b | other => pure other end NormalizeIds /- Create a declaration equivalent to `d` s.t. `d.normalizeIds.uniqueIds == true` -/ def Decl.normalizeIds (d : Decl) : Decl := (NormalizeIds.normDecl d {}).run' 1 /- Apply a function `f : VarId → VarId` to variable occurrences. The following functions assume the IR code does not have variable shadowing. -/ namespace MapVars @[inline] def mapArg (f : VarId → VarId) : Arg → Arg | Arg.var x => Arg.var (f x) | a => a @[specialize] def mapArgs (f : VarId → VarId) (as : Array Arg) : Array Arg := as.map (mapArg f) @[specialize] def mapExpr (f : VarId → VarId) : Expr → Expr | Expr.ctor c ys => Expr.ctor c (mapArgs f ys) | Expr.reset n x => Expr.reset n (f x) | Expr.reuse x c u ys => Expr.reuse (f x) c u (mapArgs f ys) | Expr.proj i x => Expr.proj i (f x) | Expr.uproj i x => Expr.uproj i (f x) | Expr.sproj n o x => Expr.sproj n o (f x) | Expr.fap c ys => Expr.fap c (mapArgs f ys) | Expr.pap c ys => Expr.pap c (mapArgs f ys) | Expr.ap x ys => Expr.ap (f x) (mapArgs f ys) | Expr.box t x => Expr.box t (f x) | Expr.unbox x => Expr.unbox (f x) | Expr.isShared x => Expr.isShared (f x) | Expr.isTaggedPtr x => Expr.isTaggedPtr (f x) | e@(Expr.lit v) => e @[specialize] partial def mapFnBody (f : VarId → VarId) : FnBody → FnBody | FnBody.vdecl x t v b => FnBody.vdecl x t (mapExpr f v) (mapFnBody b) | FnBody.jdecl j ys v b => FnBody.jdecl j ys (mapFnBody v) (mapFnBody b) | FnBody.set x i y b => FnBody.set (f x) i (mapArg f y) (mapFnBody b) | FnBody.setTag x i b => FnBody.setTag (f x) i (mapFnBody b) | FnBody.uset x i y b => FnBody.uset (f x) i (f y) (mapFnBody b) | FnBody.sset x i o y t b => FnBody.sset (f x) i o (f y) t (mapFnBody b) | FnBody.inc x n c p b => FnBody.inc (f x) n c p (mapFnBody b) | FnBody.dec x n c p b => FnBody.dec (f x) n c p (mapFnBody b) | FnBody.del x b => FnBody.del (f x) (mapFnBody b) | FnBody.mdata d b => FnBody.mdata d (mapFnBody b) | FnBody.case tid x xType alts => FnBody.case tid (f x) xType (alts.map (fun alt => alt.modifyBody mapFnBody)) | FnBody.jmp j ys => FnBody.jmp j (mapArgs f ys) | FnBody.ret x => FnBody.ret (mapArg f x) | FnBody.unreachable => FnBody.unreachable end MapVars @[inline] def FnBody.mapVars (f : VarId → VarId) (b : FnBody) : FnBody := MapVars.mapFnBody f b /- Replace `x` with `y` in `b`. This function assumes `b` does not shadow `x` -/ def FnBody.replaceVar (x y : VarId) (b : FnBody) : FnBody := b.mapVars $ fun z => if x == z then y else z end IR end Lean
905acbae90928ea5f8ae04bb0cfc0fdd102885f7
690889011852559ee5ac4dfea77092de8c832e7e
/src/category_theory/products/associator.lean
30fe3623f92d6055eeacf78fb974a9c803f63353
[ "Apache-2.0" ]
permissive
williamdemeo/mathlib
f6df180148f8acc91de9ba5e558976ab40a872c7
1fa03c29f9f273203bbffb79d10d31f696b3d317
refs/heads/master
1,584,785,260,929
1,572,195,914,000
1,572,195,913,000
138,435,193
0
0
Apache-2.0
1,529,789,739,000
1,529,789,739,000
null
UTF-8
Lean
false
false
1,956
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison -/ import category_theory.products.basic /-# The associator functor `((C × D) × E) ⥤ (C × (D × E))` and its inverse form an equivalence. -/ universes v₁ v₂ v₃ u₁ u₂ u₃ open category_theory namespace category_theory.prod variables (C : Type u₁) [𝒞 : category.{v₁} C] (D : Type u₂) [𝒟 : category.{v₂} D] (E : Type u₃) [ℰ : category.{v₃} E] include 𝒞 𝒟 ℰ def associator : ((C × D) × E) ⥤ (C × (D × E)) := { obj := λ X, (X.1.1, (X.1.2, X.2)), map := λ _ _ f, (f.1.1, (f.1.2, f.2)) } @[simp] lemma associator_obj (X) : (associator C D E).obj X = (X.1.1, (X.1.2, X.2)) := rfl @[simp] lemma associator_map {X Y} (f : X ⟶ Y) : (associator C D E).map f = (f.1.1, (f.1.2, f.2)) := rfl def inverse_associator : (C × (D × E)) ⥤ ((C × D) × E) := { obj := λ X, ((X.1, X.2.1), X.2.2), map := λ _ _ f, ((f.1, f.2.1), f.2.2) } @[simp] lemma inverse_associator_obj (X) : (inverse_associator C D E).obj X = ((X.1, X.2.1), X.2.2) := rfl @[simp] lemma inverse_associator_map {X Y} (f : X ⟶ Y) : (inverse_associator C D E).map f = ((f.1, f.2.1), f.2.2) := rfl def associativity : (C × D) × E ≌ C × (D × E) := equivalence.mk (associator C D E) (inverse_associator C D E) (nat_iso.of_components (λ X, eq_to_iso (by simp)) (by tidy)) (nat_iso.of_components (λ X, eq_to_iso (by simp)) (by tidy)) instance associator_is_equivalence : is_equivalence (associator C D E) := (by apply_instance : is_equivalence (associativity C D E).functor) instance inverse_associator_is_equivalence : is_equivalence (inverse_associator C D E) := (by apply_instance : is_equivalence (associativity C D E).inverse) -- TODO unitors? -- TODO pentagon natural transformation? ...satisfying? end category_theory.prod
b985eb1358d5137f0dc1d2fc45fa13cd26671a00
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/group_theory/perm/list.lean
6afbf9fe5248f8996acd1b489862ee5fc07f186f
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,389
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 data.list.rotate import group_theory.perm.support /-! # Permutations from a list A list `l : list α` can be interpreted as a `equiv.perm α` where each element in the list is permuted to the next one, defined as `form_perm`. When we have that `nodup l`, we prove that `equiv.perm.support (form_perm l) = l.to_finset`, and that `form_perm l` is rotationally invariant, in `form_perm_rotate`. When there are duplicate elements in `l`, how and in what arrangement with respect to the other elements they appear in the list determines the formed permutation. This is because `list.form_perm` is implemented as a product of `equiv.swap`s. That means that presence of a sublist of two adjacent duplicates like `[..., x, x, ...]` will produce the same permutation as if the adjacent duplicates were not present. The `list.form_perm` definition is meant to primarily be used with `nodup l`, so that the resulting permutation is cyclic (if `l` has at least two elements). The presence of duplicates in a particular placement can lead `list.form_perm` to produce a nontrivial permutation that is noncyclic. -/ namespace list variables {α β : Type*} section form_perm variables [decidable_eq α] (l : list α) open equiv equiv.perm /-- A list `l : list α` can be interpreted as a `equiv.perm α` where each element in the list is permuted to the next one, defined as `form_perm`. When we have that `nodup l`, we prove that `equiv.perm.support (form_perm l) = l.to_finset`, and that `form_perm l` is rotationally invariant, in `form_perm_rotate`. -/ def form_perm : equiv.perm α := (zip_with equiv.swap l l.tail).prod @[simp] lemma form_perm_nil : form_perm ([] : list α) = 1 := rfl @[simp] lemma form_perm_singleton (x : α) : form_perm [x] = 1 := rfl @[simp] lemma form_perm_cons_cons (x y : α) (l : list α) : form_perm (x :: y :: l) = swap x y * form_perm (y :: l) := prod_cons lemma form_perm_pair (x y : α) : form_perm [x, y] = swap x y := rfl lemma form_perm_apply_of_not_mem (x : α) (l : list α) (h : x ∉ l) : form_perm l x = x := begin cases l with y l, { simp }, induction l with z l IH generalizing x y, { simp }, { specialize IH x z (mt (mem_cons_of_mem y) h), simp only [not_or_distrib, mem_cons_iff] at h, simp [IH, swap_apply_of_ne_of_ne, h] } end lemma form_perm_apply_mem_of_mem (x : α) (l : list α) (h : x ∈ l) : form_perm l x ∈ l := begin cases l with y l, { simpa }, induction l with z l IH generalizing x y, { simpa using h }, { by_cases hx : x ∈ z :: l, { rw [form_perm_cons_cons, mul_apply, swap_apply_def], split_ifs; simp [IH _ _ hx] }, { replace h : x = y := or.resolve_right h hx, simp [form_perm_apply_of_not_mem _ _ hx, ←h] } } end @[simp] lemma form_perm_cons_concat_apply_last (x y : α) (xs : list α) : form_perm (x :: (xs ++ [y])) y = x := begin induction xs with z xs IH generalizing x y, { simp }, { simp [IH] } end @[simp] lemma form_perm_apply_last (x : α) (xs : list α) : form_perm (x :: xs) ((x :: xs).last (cons_ne_nil x xs)) = x := begin induction xs using list.reverse_rec_on with xs y IH generalizing x; simp end @[simp] lemma form_perm_apply_nth_le_length (x : α) (xs : list α) : form_perm (x :: xs) ((x :: xs).nth_le xs.length (by simp)) = x := by rw [nth_le_cons_length, form_perm_apply_last] lemma form_perm_apply_head (x y : α) (xs : list α) (h : nodup (x :: y :: xs)) : form_perm (x :: y :: xs) x = y := by simp [form_perm_apply_of_not_mem _ _ (not_mem_of_nodup_cons h)] lemma form_perm_apply_nth_le_zero (l : list α) (h : nodup l) (hl : 1 < l.length) : form_perm l (l.nth_le 0 (zero_lt_one.trans hl)) = l.nth_le 1 hl := begin rcases l with (_|⟨x, _|⟨y, tl⟩⟩), { simp }, { simp }, { simpa using form_perm_apply_head _ _ _ h } end lemma form_perm_eq_head_iff_eq_last (x y : α) : form_perm (y :: l) x = y ↔ x = last (y :: l) (cons_ne_nil _ _) := iff.trans (by rw form_perm_apply_last) (form_perm (y :: l)).injective.eq_iff lemma zip_with_swap_prod_support' (l l' : list α) : {x | (zip_with swap l l').prod x ≠ x} ≤ l.to_finset ⊔ l'.to_finset := begin simp only [set.sup_eq_union, set.le_eq_subset], induction l with y l hl generalizing l', { simp }, { cases l' with z l', { simp }, { intro x, simp only [set.union_subset_iff, mem_cons_iff, zip_with_cons_cons, foldr, prod_cons, mul_apply], intro hx, by_cases h : x ∈ {x | (zip_with swap l l').prod x ≠ x}, { specialize hl l' h, refine set.mem_union.elim hl (λ hm, _) (λ hm, _); { simp only [finset.coe_insert, set.mem_insert_iff, finset.mem_coe, to_finset_cons, mem_to_finset] at hm ⊢, simp [hm] } }, { simp only [not_not, set.mem_set_of_eq] at h, simp only [h, set.mem_set_of_eq] at hx, rw swap_apply_ne_self_iff at hx, rcases hx with ⟨hyz, rfl|rfl⟩; simp } } } end lemma zip_with_swap_prod_support [fintype α] (l l' : list α) : (zip_with swap l l').prod.support ≤ l.to_finset ⊔ l'.to_finset := begin intros x hx, have hx' : x ∈ {x | (zip_with swap l l').prod x ≠ x} := by simpa using hx, simpa using zip_with_swap_prod_support' _ _ hx' end lemma support_form_perm_le' : {x | form_perm l x ≠ x} ≤ l.to_finset := begin refine (zip_with_swap_prod_support' l l.tail).trans _, simpa [finset.subset_iff] using tail_subset l end lemma support_form_perm_le [fintype α] : support (form_perm l) ≤ l.to_finset := begin intros x hx, have hx' : x ∈ {x | form_perm l x ≠ x} := by simpa using hx, simpa using support_form_perm_le' _ hx' end lemma form_perm_apply_lt (xs : list α) (h : nodup xs) (n : ℕ) (hn : n + 1 < xs.length) : form_perm xs (xs.nth_le n ((nat.lt_succ_self n).trans hn)) = xs.nth_le (n + 1) hn := begin induction n with n IH generalizing xs, { simpa using form_perm_apply_nth_le_zero _ h _ }, { rcases xs with (_|⟨x, _|⟨y, l⟩⟩), { simp }, { simp }, { specialize IH (y :: l) (nodup_of_nodup_cons h) _, { simpa [nat.succ_lt_succ_iff] using hn }, simp only [swap_apply_eq_iff, coe_mul, form_perm_cons_cons, nth_le], generalize_proofs at IH, rw [IH, swap_apply_of_ne_of_ne, nth_le]; { rintro rfl, simpa [nth_le_mem _ _ _] using h } } } end lemma form_perm_apply_nth_le (xs : list α) (h : nodup xs) (n : ℕ) (hn : n < xs.length) : form_perm xs (xs.nth_le n hn) = xs.nth_le ((n + 1) % xs.length) (nat.mod_lt _ (n.zero_le.trans_lt hn)) := begin cases xs with x xs, { simp }, { have : n ≤ xs.length, { refine nat.le_of_lt_succ _, simpa using hn }, rcases this.eq_or_lt with rfl|hn', { simp }, { simp [form_perm_apply_lt, h, nat.mod_eq_of_lt, nat.succ_lt_succ hn'] } } end lemma support_form_perm_of_nodup' (l : list α) (h : nodup l) (h' : ∀ (x : α), l ≠ [x]) : {x | form_perm l x ≠ x} = l.to_finset := begin apply le_antisymm, { exact support_form_perm_le' l }, { intros x hx, simp only [finset.mem_coe, mem_to_finset] at hx, obtain ⟨n, hn, rfl⟩ := nth_le_of_mem hx, rw [set.mem_set_of_eq, form_perm_apply_nth_le _ h], intro H, rw nodup_iff_nth_le_inj at h, specialize h _ _ _ _ H, cases (nat.succ_le_of_lt hn).eq_or_lt with hn' hn', { simp only [←hn', nat.mod_self] at h, refine not_exists.mpr h' _, simpa [←h, eq_comm, length_eq_one] using hn' }, { simpa [nat.mod_eq_of_lt hn'] using h } } end lemma support_form_perm_of_nodup [fintype α] (l : list α) (h : nodup l) (h' : ∀ (x : α), l ≠ [x]) : support (form_perm l) = l.to_finset := begin rw ←finset.coe_inj, convert support_form_perm_of_nodup' _ h h', simp [set.ext_iff] end lemma form_perm_rotate_one (l : list α) (h : nodup l) : form_perm (l.rotate 1) = form_perm l := begin have h' : nodup (l.rotate 1), { simpa using h }, by_cases hl : ∀ (x : α), l ≠ [x], { have hl' : ∀ (x : α), l.rotate 1 ≠ [x], { intro, rw [ne.def, rotate_eq_iff], simpa using hl _ }, ext x, by_cases hx : x ∈ l.rotate 1, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, rw [form_perm_apply_nth_le _ h', nth_le_rotate l, nth_le_rotate l, form_perm_apply_nth_le _ h], simp }, { rw [form_perm_apply_of_not_mem _ _ hx, form_perm_apply_of_not_mem], simpa using hx } }, { push_neg at hl, obtain ⟨x, rfl⟩ := hl, simp } end lemma form_perm_rotate (l : list α) (h : nodup l) (n : ℕ) : form_perm (l.rotate n) = form_perm l := begin induction n with n hn, { simp }, { rw [nat.succ_eq_add_one, ←rotate_rotate, form_perm_rotate_one, hn], rwa is_rotated.nodup_iff, exact is_rotated.forall l n } end lemma form_perm_eq_of_is_rotated {l l' : list α} (hd : nodup l) (h : l ~r l') : form_perm l = form_perm l' := begin obtain ⟨n, rfl⟩ := h, exact (form_perm_rotate l hd n).symm end lemma form_perm_reverse (l : list α) (h : nodup l) : form_perm l.reverse = (form_perm l)⁻¹ := begin -- Let's show `form_perm l` is an inverse to `form_perm l.reverse`. rw [eq_comm, inv_eq_iff_mul_eq_one], ext x, -- We only have to check for `x ∈ l` that `form_perm l (form_perm l.reverse x)` rw [mul_apply, one_apply], by_cases hx : x ∈ l, swap, { rw [form_perm_apply_of_not_mem x l.reverse, form_perm_apply_of_not_mem _ _ hx], simpa using hx }, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem (mem_reverse.mpr hx), rw [form_perm_apply_nth_le l.reverse (nodup_reverse.mpr h), nth_le_reverse', form_perm_apply_nth_le _ h, nth_le_reverse'], { congr, rw [length_reverse, ←nat.succ_le_iff, nat.succ_eq_add_one] at hk, cases hk.eq_or_lt with hk' hk', { simp [←hk'] }, { rw [length_reverse, nat.mod_eq_of_lt hk', tsub_add_eq_add_tsub (nat.le_pred_of_lt hk'), nat.mod_eq_of_lt], { simp }, { rw tsub_add_cancel_of_le, refine tsub_lt_self _ (nat.zero_lt_succ _), all_goals { simpa using (nat.zero_le _).trans_lt hk' } } } }, all_goals { rw [← tsub_add_eq_tsub_tsub, ←length_reverse], refine tsub_lt_self _ (zero_lt_one.trans_le (le_add_right le_rfl)), exact k.zero_le.trans_lt hk } }, end lemma form_perm_pow_apply_nth_le (l : list α) (h : nodup l) (n k : ℕ) (hk : k < l.length) : (form_perm l ^ n) (l.nth_le k hk) = l.nth_le ((k + n) % l.length) (nat.mod_lt _ (k.zero_le.trans_lt hk)) := begin induction n with n hn, { simp [nat.mod_eq_of_lt hk] }, { simp [pow_succ, mul_apply, hn, form_perm_apply_nth_le _ h, nat.succ_eq_add_one, ←nat.add_assoc] } end lemma form_perm_pow_apply_head (x : α) (l : list α) (h : nodup (x :: l)) (n : ℕ) : (form_perm (x :: l) ^ n) x = (x :: l).nth_le (n % (x :: l).length) (nat.mod_lt _ (nat.zero_lt_succ _)) := by { convert form_perm_pow_apply_nth_le _ h n 0 _; simp } lemma form_perm_ext_iff {x y x' y' : α} {l l' : list α} (hd : nodup (x :: y :: l)) (hd' : nodup (x' :: y' :: l')) : form_perm (x :: y :: l) = form_perm (x' :: y' :: l') ↔ (x :: y :: l) ~r (x' :: y' :: l') := begin refine ⟨λ h, _, λ hr, form_perm_eq_of_is_rotated hd hr⟩, rw equiv.perm.ext_iff at h, have hx : x' ∈ (x :: y :: l), { have : x' ∈ {z | form_perm (x :: y :: l) z ≠ z}, { rw [set.mem_set_of_eq, h x', form_perm_apply_head _ _ _ hd'], simp only [mem_cons_iff, nodup_cons] at hd', push_neg at hd', exact hd'.left.left.symm }, simpa using support_form_perm_le' _ this }, obtain ⟨n, hn, hx'⟩ := nth_le_of_mem hx, have hl : (x :: y :: l).length = (x' :: y' :: l').length, { rw [←erase_dup_eq_self.mpr hd, ←erase_dup_eq_self.mpr hd', ←card_to_finset, ←card_to_finset], refine congr_arg finset.card _, rw [←finset.coe_inj, ←support_form_perm_of_nodup' _ hd (by simp), ←support_form_perm_of_nodup' _ hd' (by simp)], simp only [h] }, use n, apply list.ext_le, { rw [length_rotate, hl] }, { intros k hk hk', rw nth_le_rotate, induction k with k IH, { simp_rw [nat.zero_add, nat.mod_eq_of_lt hn], simpa }, { have : k.succ = (k + 1) % (x' :: y' :: l').length, { rw [←nat.succ_eq_add_one, nat.mod_eq_of_lt hk'] }, simp_rw this, rw [←form_perm_apply_nth_le _ hd' k (k.lt_succ_self.trans hk'), ←IH (k.lt_succ_self.trans hk), ←h, form_perm_apply_nth_le _ hd], congr' 1, have h1 : 1 = 1 % (x' :: y' :: l').length := by simp, rw [hl, nat.mod_eq_of_lt hk', h1, ←nat.add_mod, nat.succ_add] } } end lemma form_perm_apply_mem_eq_self_iff (hl : nodup l) (x : α) (hx : x ∈ l) : form_perm l x = x ↔ length l ≤ 1 := begin obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, rw [form_perm_apply_nth_le _ hl, hl.nth_le_inj_iff], cases hn : l.length, { exact absurd k.zero_le (hk.trans_le hn.le).not_le }, { rw hn at hk, cases (nat.le_of_lt_succ hk).eq_or_lt with hk' hk', { simp [←hk', nat.succ_le_succ_iff, eq_comm] }, { simpa [nat.mod_eq_of_lt (nat.succ_lt_succ hk'), nat.succ_lt_succ_iff] using k.zero_le.trans_lt hk' } } end lemma form_perm_apply_mem_ne_self_iff (hl : nodup l) (x : α) (hx : x ∈ l) : form_perm l x ≠ x ↔ 2 ≤ l.length := begin rw [ne.def, form_perm_apply_mem_eq_self_iff _ hl x hx, not_le], exact ⟨nat.succ_le_of_lt, nat.lt_of_succ_le⟩ end lemma mem_of_form_perm_ne_self (l : list α) (x : α) (h : form_perm l x ≠ x) : x ∈ l := begin suffices : x ∈ {y | form_perm l y ≠ y}, { rw ←mem_to_finset, exact support_form_perm_le' _ this }, simpa using h end lemma form_perm_eq_self_of_not_mem (l : list α) (x : α) (h : x ∉ l) : form_perm l x = x := by_contra (λ H, h $ mem_of_form_perm_ne_self _ _ H) lemma form_perm_eq_one_iff (hl : nodup l) : form_perm l = 1 ↔ l.length ≤ 1 := begin cases l with hd tl, { simp }, { rw ←form_perm_apply_mem_eq_self_iff _ hl hd (mem_cons_self _ _), split, { simp {contextual := tt} }, { intro h, simp only [(hd :: tl).form_perm_apply_mem_eq_self_iff hl hd (mem_cons_self hd tl), add_le_iff_nonpos_left, length, nonpos_iff_eq_zero, length_eq_zero] at h, simp [h] } } end lemma form_perm_eq_form_perm_iff {l l' : list α} (hl : l.nodup) (hl' : l'.nodup) : l.form_perm = l'.form_perm ↔ l ~r l' ∨ l.length ≤ 1 ∧ l'.length ≤ 1 := begin rcases l with (_ | ⟨x, _ | ⟨y, l⟩⟩), { suffices : l'.length ≤ 1 ↔ l' = nil ∨ l'.length ≤ 1, { simpa [eq_comm, form_perm_eq_one_iff, hl, hl', length_eq_zero] }, refine ⟨λ h, or.inr h, _⟩, rintro (rfl | h), { simp }, { exact h } }, { suffices : l'.length ≤ 1 ↔ [x] ~r l' ∨ l'.length ≤ 1, { simpa [eq_comm, form_perm_eq_one_iff, hl, hl', length_eq_zero, le_rfl] }, refine ⟨λ h, or.inr h, _⟩, rintro (h | h), { simp [←h.perm.length_eq] }, { exact h } }, { rcases l' with (_ | ⟨x', _ | ⟨y', l'⟩⟩), { simp [form_perm_eq_one_iff, hl, -form_perm_cons_cons] }, { suffices : ¬ (x :: y :: l) ~r [x'], { simp [form_perm_eq_one_iff, hl, -form_perm_cons_cons] }, intro h, simpa using h.perm.length_eq }, { simp [-form_perm_cons_cons, form_perm_ext_iff hl hl'] } } end lemma form_perm_zpow_apply_mem_imp_mem (l : list α) (x : α) (hx : x ∈ l) (n : ℤ) : ((form_perm l) ^ n) x ∈ l := begin by_cases h : (l.form_perm ^ n) x = x, { simpa [h] using hx }, { have : x ∈ {x | (l.form_perm ^ n) x ≠ x} := h, rw ←set_support_apply_mem at this, replace this := set_support_zpow_subset _ _ this, simpa using support_form_perm_le' _ this } end lemma form_perm_pow_length_eq_one_of_nodup (hl : nodup l) : (form_perm l) ^ (length l) = 1 := begin ext x, by_cases hx : x ∈ l, { obtain ⟨k, hk, rfl⟩ := nth_le_of_mem hx, simp [form_perm_pow_apply_nth_le _ hl, nat.mod_eq_of_lt hk] }, { have : x ∉ {x | (l.form_perm ^ l.length) x ≠ x}, { intros H, refine hx _, replace H := set_support_zpow_subset l.form_perm l.length H, simpa using support_form_perm_le' _ H }, simpa } end end form_perm end list
7e6b3a92e24e7400b7ea4d95d8b1919cc85f6955
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Init/Data/Nat/Basic.lean
0be29b289e79147b7d2fae9a41348455f38bebb5
[ "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
14,877
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 /- Helper "packing" theorems -/ @[simp] theorem zero_eq : Nat.zero = 0 := rfl @[simp] theorem add_eq : Nat.add x y = x + y := rfl @[simp] theorem mul_eq : Nat.mul x y = x * y := rfl @[simp] theorem lt_eq : Nat.lt x y = (x < y) := rfl @[simp] theorem le_eq : Nat.le x y = (x ≤ y) := rfl /- Nat.add theorems -/ @[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 zero_lt_of_lt : {a b : Nat} → a < b → 0 < b | 0, _, h => h | a+1, b, h => have : a < b := Nat.lt_trans (Nat.lt_succ_self _) h zero_lt_of_lt this 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
3641068540a5006cd818cac183a6d57df664ec1a
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/topology/compacts.lean
9c0a9caa962d42864e219cd95439ffecf771f326
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
4,467
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import topology.homeomorph /-! # Compact sets ## Summary We define the subtype of compact sets in a topological space. ## Main Definitions - `closeds α` is the type of closed subsets of a topological space `α`. - `compacts α` is the type of compact subsets of a topological space `α`. - `nonempty_compacts α` is the type of non-empty compact subsets. - `positive_compacts α` is the type of compact subsets with non-empty interior. -/ open set variables (α : Type*) {β : Type*} [topological_space α] [topological_space β] namespace topological_space /-- The type of closed subsets of a topological space. -/ def closeds := {s : set α // is_closed s} /-- The type of closed subsets is inhabited, with default element the empty set. -/ instance : inhabited (closeds α) := ⟨⟨∅, is_closed_empty ⟩⟩ /-- The compact sets of a topological space. See also `nonempty_compacts`. -/ def compacts : Type* := { s : set α // is_compact s } /-- The type of non-empty compact subsets of a topological space. The non-emptiness will be useful in metric spaces, as we will be able to put a distance (and not merely an edistance) on this space. -/ def nonempty_compacts := {s : set α // s.nonempty ∧ is_compact s} /-- In an inhabited space, the type of nonempty compact subsets is also inhabited, with default element the singleton set containing the default element. -/ instance nonempty_compacts_inhabited [inhabited α] : inhabited (nonempty_compacts α) := ⟨⟨{default α}, singleton_nonempty (default α), is_compact_singleton ⟩⟩ /-- The compact sets with nonempty interior of a topological space. See also `compacts` and `nonempty_compacts`. -/ @[nolint has_inhabited_instance] def positive_compacts: Type* := { s : set α // is_compact s ∧ (interior s).nonempty } variables {α} namespace compacts instance : semilattice_sup_bot (compacts α) := subtype.semilattice_sup_bot is_compact_empty (λ K₁ K₂, is_compact.union) instance [t2_space α]: semilattice_inf_bot (compacts α) := subtype.semilattice_inf_bot is_compact_empty (λ K₁ K₂, is_compact.inter) instance [t2_space α] : lattice (compacts α) := subtype.lattice (λ K₁ K₂, is_compact.union) (λ K₁ K₂, is_compact.inter) @[simp] lemma bot_val : (⊥ : compacts α).1 = ∅ := rfl @[simp] lemma sup_val {K₁ K₂ : compacts α} : (K₁ ⊔ K₂).1 = K₁.1 ∪ K₂.1 := rfl @[ext] protected lemma ext {K₁ K₂ : compacts α} (h : K₁.1 = K₂.1) : K₁ = K₂ := subtype.eq h @[simp] lemma finset_sup_val {β} {K : β → compacts α} {s : finset β} : (s.sup K).1 = s.sup (λ x, (K x).1) := finset.sup_coe _ _ instance : inhabited (compacts α) := ⟨⊥⟩ /-- The image of a compact set under a continuous function. -/ protected def map (f : α → β) (hf : continuous f) (K : compacts α) : compacts β := ⟨f '' K.1, K.2.image hf⟩ @[simp] lemma map_val {f : α → β} (hf : continuous f) (K : compacts α) : (K.map f hf).1 = f '' K.1 := rfl /-- A homeomorphism induces an equivalence on compact sets, by taking the image. -/ @[simp] protected def equiv (f : α ≃ₜ β) : compacts α ≃ compacts β := { to_fun := compacts.map f f.continuous, inv_fun := compacts.map _ f.symm.continuous, left_inv := by { intro K, ext1, simp only [map_val, ← image_comp, f.symm_comp_self, image_id] }, right_inv := by { intro K, ext1, simp only [map_val, ← image_comp, f.self_comp_symm, image_id] } } /-- The image of a compact set under a homeomorphism can also be expressed as a preimage. -/ lemma equiv_to_fun_val (f : α ≃ₜ β) (K : compacts α) : (compacts.equiv f K).1 = f.symm ⁻¹' K.1 := congr_fun (image_eq_preimage_of_inverse f.left_inv f.right_inv) K.1 end compacts section nonempty_compacts open topological_space set variable {α} instance nonempty_compacts.to_compact_space {p : nonempty_compacts α} : compact_space p.val := ⟨is_compact_iff_is_compact_univ.1 p.property.2⟩ instance nonempty_compacts.to_nonempty {p : nonempty_compacts α} : nonempty p.val := p.property.1.to_subtype /-- Associate to a nonempty compact subset the corresponding closed subset -/ def nonempty_compacts.to_closeds [t2_space α] : nonempty_compacts α → closeds α := set.inclusion $ λ s hs, hs.2.is_closed end nonempty_compacts end topological_space
ea8399b20dfcb1b013641ef0d5c6fb76582c0b3c
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/equiv/encodable/basic.lean
aca7245fa3b847151438519fa7bda6248fae7293
[ "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
18,130
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.equiv.nat import order.rel_iso import order.directed /-! # Encodable types This file defines encodable (constructively countable) types as a typeclass. This is used to provide explicit encode/decode functions from and to `ℕ`, with the information that those functions are inverses of each other. The difference with `denumerable` is that finite types are encodable. For infinite types, `encodable` and `denumerable` agree. ## Main declarations * `encodable α`: States that there exists an explicit encoding function `encode : α → ℕ` with a partial inverse `decode : ℕ → option α`. * `decode₂`: Version of `decode` that is equal to `none` outside of the range of `encode`. Useful as we do not require this in the definition of `decode`. * `ulower α`: Any encodable type has an equivalent type living in the lowest universe, namely a subtype of `ℕ`. `ulower α` finds it. ## Implementation notes The point of asking for an explicit partial inverse `decode : ℕ → option α` to `encode : α → ℕ` is to make the range of `encode` decidable even when the finiteness of `α` is not. -/ open option list nat function /-- Constructively countable type. Made from an explicit injection `encode : α → ℕ` and a partial inverse `decode : ℕ → option α`. Note that finite types *are* countable. See `denumerable` if you wish to enforce infiniteness. -/ class encodable (α : Type*) := (encode : α → ℕ) (decode [] : ℕ → option α) (encodek : ∀ a, decode (encode a) = some a) attribute [simp] encodable.encodek namespace encodable variables {α : Type*} {β : Type*} universe u theorem encode_injective [encodable α] : function.injective (@encode α _) | x y e := option.some.inj $ by rw [← encodek, e, encodek] lemma surjective_decode_iget (α : Type*) [encodable α] [inhabited α] : surjective (λ n, (encodable.decode α n).iget) := λ x, ⟨encodable.encode x, by simp_rw [encodable.encodek]⟩ /-- An encodable type has decidable equality. Not set as an instance because this is usually not the best way to infer decidability. -/ def decidable_eq_of_encodable (α) [encodable α] : decidable_eq α | a b := decidable_of_iff _ encode_injective.eq_iff /-- If `α` is encodable and there is an injection `f : β → α`, then `β` is encodable as well. -/ def of_left_injection [encodable α] (f : β → α) (finv : α → option β) (linv : ∀ b, finv (f b) = some b) : encodable β := ⟨λ b, encode (f b), λ n, (decode α n).bind finv, λ b, by simp [encodable.encodek, linv]⟩ /-- If `α` is encodable and `f : β → α` is invertible, then `β` is encodable as well. -/ def of_left_inverse [encodable α] (f : β → α) (finv : α → β) (linv : ∀ b, finv (f b) = b) : encodable β := of_left_injection f (some ∘ finv) (λ b, congr_arg some (linv b)) /-- Encodability is preserved by equivalence. -/ def of_equiv (α) [encodable α] (e : β ≃ α) : encodable β := of_left_inverse e e.symm e.left_inv @[simp] theorem encode_of_equiv {α β} [encodable α] (e : β ≃ α) (b : β) : @encode _ (of_equiv _ e) b = encode (e b) := rfl @[simp] theorem decode_of_equiv {α β} [encodable α] (e : β ≃ α) (n : ℕ) : @decode _ (of_equiv _ e) n = (decode α n).map e.symm := rfl instance nat : encodable ℕ := ⟨id, some, λ a, rfl⟩ @[simp] theorem encode_nat (n : ℕ) : encode n = n := rfl @[simp] theorem decode_nat (n : ℕ) : decode ℕ n = some n := rfl instance empty : encodable empty := ⟨λ a, a.rec _, λ n, none, λ a, a.rec _⟩ instance unit : encodable punit := ⟨λ_, zero, λ n, nat.cases_on n (some punit.star) (λ _, none), λ _, by simp⟩ @[simp] theorem encode_star : encode punit.star = 0 := rfl @[simp] theorem decode_unit_zero : decode punit 0 = some punit.star := rfl @[simp] theorem decode_unit_succ (n) : decode punit (succ n) = none := rfl /-- If `α` is encodable, then so is `option α`. -/ instance option {α : Type*} [h : encodable α] : encodable (option α) := ⟨λ o, option.cases_on o nat.zero (λ a, succ (encode a)), λ n, nat.cases_on n (some none) (λ m, (decode α m).map some), λ o, by cases o; dsimp; simp [encodek, nat.succ_ne_zero]⟩ @[simp] theorem encode_none [encodable α] : encode (@none α) = 0 := rfl @[simp] theorem encode_some [encodable α] (a : α) : encode (some a) = succ (encode a) := rfl @[simp] theorem decode_option_zero [encodable α] : decode (option α) 0 = some none := rfl @[simp] theorem decode_option_succ [encodable α] (n) : decode (option α) (succ n) = (decode α n).map some := rfl /-- Failsafe variant of `decode`. `decode₂ α n` returns the preimage of `n` under `encode` if it exists, and returns `none` if it doesn't. This requirement could be imposed directly on `decode` but is not to help make the definition easier to use. -/ def decode₂ (α) [encodable α] (n : ℕ) : option α := (decode α n).bind (option.guard (λ a, encode a = n)) theorem mem_decode₂' [encodable α] {n : ℕ} {a : α} : a ∈ decode₂ α n ↔ a ∈ decode α n ∧ encode a = n := by simp [decode₂]; exact ⟨λ ⟨_, h₁, rfl, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨_, h₁, rfl, h₂⟩⟩ theorem mem_decode₂ [encodable α] {n : ℕ} {a : α} : a ∈ decode₂ α n ↔ encode a = n := mem_decode₂'.trans (and_iff_right_of_imp $ λ e, e ▸ encodek _) theorem decode₂_ne_none_iff [encodable α] {n : ℕ} : decode₂ α n ≠ none ↔ n ∈ set.range (encode : α → ℕ) := by simp_rw [set.range, set.mem_set_of_eq, ne.def, option.eq_none_iff_forall_not_mem, encodable.mem_decode₂, not_forall, not_not] theorem decode₂_is_partial_inv [encodable α] : is_partial_inv encode (decode₂ α) := λ a n, mem_decode₂ theorem decode₂_inj [encodable α] {n : ℕ} {a₁ a₂ : α} (h₁ : a₁ ∈ decode₂ α n) (h₂ : a₂ ∈ decode₂ α n) : a₁ = a₂ := encode_injective $ (mem_decode₂.1 h₁).trans (mem_decode₂.1 h₂).symm theorem encodek₂ [encodable α] (a : α) : decode₂ α (encode a) = some a := mem_decode₂.2 rfl /-- The encoding function has decidable range. -/ def decidable_range_encode (α : Type*) [encodable α] : decidable_pred (∈ set.range (@encode α _)) := λ x, decidable_of_iff (option.is_some (decode₂ α x)) ⟨λ h, ⟨option.get h, by rw [← decode₂_is_partial_inv (option.get h), option.some_get]⟩, λ ⟨n, hn⟩, by rw [← hn, encodek₂]; exact rfl⟩ /-- An encodable type is equivalent to the range of its encoding function. -/ def equiv_range_encode (α : Type*) [encodable α] : α ≃ set.range (@encode α _) := { to_fun := λ a : α, ⟨encode a, set.mem_range_self _⟩, inv_fun := λ n, option.get (show is_some (decode₂ α n.1), by cases n.2 with x hx; rw [← hx, encodek₂]; exact rfl), left_inv := λ a, by dsimp; rw [← option.some_inj, option.some_get, encodek₂], right_inv := λ ⟨n, x, hx⟩, begin apply subtype.eq, dsimp, conv {to_rhs, rw ← hx}, rw [encode_injective.eq_iff, ← option.some_inj, option.some_get, ← hx, encodek₂], end } /-- A type with unique element is encodable. This is not an instance to avoid diamonds. -/ def _root_.unique.encodable [unique α] : encodable α := ⟨λ _, 0, λ _, some (default α), unique.forall_iff.2 rfl⟩ section sum variables [encodable α] [encodable β] /-- Explicit encoding function for the sum of two encodable types. -/ def encode_sum : α ⊕ β → ℕ | (sum.inl a) := bit0 $ encode a | (sum.inr b) := bit1 $ encode b /-- Explicit decoding function for the sum of two encodable types. -/ def decode_sum (n : ℕ) : option (α ⊕ β) := match bodd_div2 n with | (ff, m) := (decode α m).map sum.inl | (tt, m) := (decode β m).map sum.inr end /-- If `α` and `β` are encodable, then so is their sum. -/ instance sum : encodable (α ⊕ β) := ⟨encode_sum, decode_sum, λ s, by cases s; simp [encode_sum, decode_sum, encodek]; refl⟩ @[simp] theorem encode_inl (a : α) : @encode (α ⊕ β) _ (sum.inl a) = bit0 (encode a) := rfl @[simp] theorem encode_inr (b : β) : @encode (α ⊕ β) _ (sum.inr b) = bit1 (encode b) := rfl @[simp] theorem decode_sum_val (n : ℕ) : decode (α ⊕ β) n = decode_sum n := rfl end sum instance bool : encodable bool := of_equiv (unit ⊕ unit) equiv.bool_equiv_punit_sum_punit @[simp] theorem encode_tt : encode tt = 1 := rfl @[simp] theorem encode_ff : encode ff = 0 := rfl @[simp] theorem decode_zero : decode bool 0 = some ff := rfl @[simp] theorem decode_one : decode bool 1 = some tt := rfl theorem decode_ge_two (n) (h : 2 ≤ n) : decode bool n = none := begin suffices : decode_sum n = none, { change (decode_sum n).map _ = none, rw this, refl }, have : 1 ≤ div2 n, { rw [div2_val, nat.le_div_iff_mul_le], exacts [h, dec_trivial] }, cases exists_eq_succ_of_ne_zero (ne_of_gt this) with m e, simp [decode_sum]; cases bodd n; simp [decode_sum]; rw e; refl end noncomputable instance «Prop» : encodable Prop := of_equiv bool equiv.Prop_equiv_bool section sigma variables {γ : α → Type*} [encodable α] [∀ a, encodable (γ a)] /-- Explicit encoding function for `sigma γ` -/ def encode_sigma : sigma γ → ℕ | ⟨a, b⟩ := mkpair (encode a) (encode b) /-- Explicit decoding function for `sigma γ` -/ def decode_sigma (n : ℕ) : option (sigma γ) := let (n₁, n₂) := unpair n in (decode α n₁).bind $ λ a, (decode (γ a) n₂).map $ sigma.mk a instance sigma : encodable (sigma γ) := ⟨encode_sigma, decode_sigma, λ ⟨a, b⟩, by simp [encode_sigma, decode_sigma, unpair_mkpair, encodek]⟩ @[simp] theorem decode_sigma_val (n : ℕ) : decode (sigma γ) n = (decode α n.unpair.1).bind (λ a, (decode (γ a) n.unpair.2).map $ sigma.mk a) := show decode_sigma._match_1 _ = _, by cases n.unpair; refl @[simp] theorem encode_sigma_val (a b) : @encode (sigma γ) _ ⟨a, b⟩ = mkpair (encode a) (encode b) := rfl end sigma section prod variables [encodable α] [encodable β] /-- If `α` and `β` are encodable, then so is their product. -/ instance prod : encodable (α × β) := of_equiv _ (equiv.sigma_equiv_prod α β).symm @[simp] theorem decode_prod_val (n : ℕ) : decode (α × β) n = (decode α n.unpair.1).bind (λ a, (decode β n.unpair.2).map $ prod.mk a) := show (decode (sigma (λ _, β)) n).map (equiv.sigma_equiv_prod α β) = _, by simp; cases decode α n.unpair.1; simp; cases decode β n.unpair.2; refl @[simp] theorem encode_prod_val (a b) : @encode (α × β) _ (a, b) = mkpair (encode a) (encode b) := rfl end prod section subtype open subtype decidable variables {P : α → Prop} [encA : encodable α] [decP : decidable_pred P] include encA /-- Explicit encoding function for a decidable subtype of an encodable type -/ def encode_subtype : {a : α // P a} → ℕ | ⟨v, h⟩ := encode v include decP /-- Explicit decoding function for a decidable subtype of an encodable type -/ def decode_subtype (v : ℕ) : option {a : α // P a} := (decode α v).bind $ λ a, if h : P a then some ⟨a, h⟩ else none /-- A decidable subtype of an encodable type is encodable. -/ instance subtype : encodable {a : α // P a} := ⟨encode_subtype, decode_subtype, λ ⟨v, h⟩, by simp [encode_subtype, decode_subtype, encodek, h]⟩ lemma subtype.encode_eq (a : subtype P) : encode a = encode a.val := by cases a; refl end subtype instance fin (n) : encodable (fin n) := of_equiv _ (equiv.fin_equiv_subtype _) instance int : encodable ℤ := of_equiv _ equiv.int_equiv_nat instance pnat : encodable ℕ+ := of_equiv _ equiv.pnat_equiv_nat /-- The lift of an encodable type is encodable. -/ instance ulift [encodable α] : encodable (ulift α) := of_equiv _ equiv.ulift /-- The lift of an encodable type is encodable. -/ instance plift [encodable α] : encodable (plift α) := of_equiv _ equiv.plift /-- If `β` is encodable and there is an injection `f : α → β`, then `α` is encodable as well. -/ noncomputable def of_inj [encodable β] (f : α → β) (hf : injective f) : encodable α := of_left_injection f (partial_inv f) (λ x, (partial_inv_of_injective hf _ _).2 rfl) end encodable section ulower local attribute [instance, priority 100] encodable.decidable_range_encode /-- `ulower α : Type` is an equivalent type in the lowest universe, given `encodable α`. -/ @[derive decidable_eq, derive encodable] def ulower (α : Type*) [encodable α] : Type := set.range (encodable.encode : α → ℕ) end ulower namespace ulower variables (α : Type*) [encodable α] /-- The equivalence between the encodable type `α` and `ulower α : Type`. -/ def equiv : α ≃ ulower α := encodable.equiv_range_encode α variables {α} /-- Lowers an `a : α` into `ulower α`. -/ def down (a : α) : ulower α := equiv α a instance [inhabited α] : inhabited (ulower α) := ⟨down (default _)⟩ /-- Lifts an `a : ulower α` into `α`. -/ def up (a : ulower α) : α := (equiv α).symm a @[simp] lemma down_up {a : ulower α} : down a.up = a := equiv.right_inv _ _ @[simp] lemma up_down {a : α} : (down a).up = a := equiv.left_inv _ _ @[simp] lemma up_eq_up {a b : ulower α} : a.up = b.up ↔ a = b := equiv.apply_eq_iff_eq _ @[simp] lemma down_eq_down {a b : α} : down a = down b ↔ a = b := equiv.apply_eq_iff_eq _ @[ext] protected lemma ext {a b : ulower α} : a.up = b.up → a = b := up_eq_up.1 end ulower /- Choice function for encodable types and decidable predicates. We provide the following API choose {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] : (∃ x, p x) → α := choose_spec {α : Type*} {p : α → Prop} [c : encodable α] [d : decidable_pred p] (ex : ∃ x, p x) : p (choose ex) := -/ namespace encodable section find_a variables {α : Type*} (p : α → Prop) [encodable α] [decidable_pred p] private def good : option α → Prop | (some a) := p a | none := false private def decidable_good : decidable_pred (good p) | n := by cases n; unfold good; apply_instance local attribute [instance] decidable_good open encodable variable {p} /-- Constructive choice function for a decidable subtype of an encodable type. -/ def choose_x (h : ∃ x, p x) : {a : α // p a} := have ∃ n, good p (decode α n), from let ⟨w, pw⟩ := h in ⟨encode w, by simp [good, encodek, pw]⟩, match _, nat.find_spec this : ∀ o, good p o → {a // p a} with | some a, h := ⟨a, h⟩ end /-- Constructive choice function for a decidable predicate over an encodable type. -/ def choose (h : ∃ x, p x) : α := (choose_x h).1 lemma choose_spec (h : ∃ x, p x) : p (choose h) := (choose_x h).2 end find_a theorem axiom_of_choice {α : Type*} {β : α → Type*} {R : Π x, β x → Prop} [Π a, encodable (β a)] [∀ x y, decidable (R x y)] (H : ∀ x, ∃ y, R x y) : ∃ f : Π a, β a, ∀ x, R x (f x) := ⟨λ x, choose (H x), λ x, choose_spec (H x)⟩ theorem skolem {α : Type*} {β : α → Type*} {P : Π x, β x → Prop} [c : Π a, encodable (β a)] [d : ∀ x y, decidable (P x y)] : (∀ x, ∃ y, P x y) ↔ ∃ f : Π a, β a, (∀ x, P x (f x)) := ⟨axiom_of_choice, λ ⟨f, H⟩ x, ⟨_, H x⟩⟩ /- There is a total ordering on the elements of an encodable type, induced by the map to ℕ. -/ /-- The `encode` function, viewed as an embedding. -/ def encode' (α) [encodable α] : α ↪ ℕ := ⟨encodable.encode, encodable.encode_injective⟩ instance {α} [encodable α] : is_trans _ (encode' α ⁻¹'o (≤)) := (rel_embedding.preimage _ _).is_trans instance {α} [encodable α] : is_antisymm _ (encodable.encode' α ⁻¹'o (≤)) := (rel_embedding.preimage _ _).is_antisymm instance {α} [encodable α] : is_total _ (encodable.encode' α ⁻¹'o (≤)) := (rel_embedding.preimage _ _).is_total end encodable namespace directed open encodable variables {α : Type*} {β : Type*} [encodable α] [inhabited α] /-- Given a `directed r` function `f : α → β` defined on an encodable inhabited type, construct a noncomputable sequence such that `r (f (x n)) (f (x (n + 1)))` and `r (f a) (f (x (encode a + 1))`. -/ protected noncomputable def sequence {r : β → β → Prop} (f : α → β) (hf : directed r f) : ℕ → α | 0 := default α | (n + 1) := let p := sequence n in match decode α n with | none := classical.some (hf p p) | (some a) := classical.some (hf p a) end lemma sequence_mono_nat {r : β → β → Prop} {f : α → β} (hf : directed r f) (n : ℕ) : r (f (hf.sequence f n)) (f (hf.sequence f (n+1))) := begin dsimp [directed.sequence], generalize eq : hf.sequence f n = p, cases h : decode α n with a, { exact (classical.some_spec (hf p p)).1 }, { exact (classical.some_spec (hf p a)).1 } end lemma rel_sequence {r : β → β → Prop} {f : α → β} (hf : directed r f) (a : α) : r (f a) (f (hf.sequence f (encode a + 1))) := begin simp only [directed.sequence, encodek], exact (classical.some_spec (hf _ a)).2 end variables [preorder β] {f : α → β} (hf : directed (≤) f) lemma sequence_mono : monotone (f ∘ (hf.sequence f)) := monotone_nat_of_le_succ $ hf.sequence_mono_nat lemma le_sequence (a : α) : f a ≤ f (hf.sequence f (encode a + 1)) := hf.rel_sequence a end directed section quotient open encodable quotient variables {α : Type*} {s : setoid α} [@decidable_rel α (≈)] [encodable α] /-- Representative of an equivalence class. This is a computable version of `quot.out` for a setoid on an encodable type. -/ def quotient.rep (q : quotient s) : α := choose (exists_rep q) theorem quotient.rep_spec (q : quotient s) : ⟦q.rep⟧ = q := choose_spec (exists_rep q) /-- The quotient of an encodable space by a decidable equivalence relation is encodable. -/ def encodable_quotient : encodable (quotient s) := ⟨λ q, encode q.rep, λ n, quotient.mk <$> decode α n, by rintros ⟨l⟩; rw encodek; exact congr_arg some ⟦l⟧.rep_spec⟩ end quotient
141802d5cbcab1aa1dc8da0a56e5b05abedc3587
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/tutorial/4.lean
02b53b4a92de38807574e60226e442dc495529a1
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
91
lean
lemma add_succ_zero (a : mynat) : a + succ(0) = succ(a) := rwa [add_succ, add_zero], end
4bb2b58fadaccc6e1a44bfb54162fb6ac93a4fcc
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/fp/basic.lean
3936873bc42a4b3e8d785333d0ebcaa308ea5ebc
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,487
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.semiquot import data.rat.floor /-! # Implementation of floating-point numbers (experimental). > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ def int.shift2 (a b : ℕ) : ℤ → ℕ × ℕ | (int.of_nat e) := (a.shiftl e, b) | -[1+ e] := (a, b.shiftl e.succ) namespace fp @[derive inhabited] inductive rmode | NE -- round to nearest even class float_cfg := (prec emax : ℕ) (prec_pos : 0 < prec) (prec_max : prec ≤ emax) variable [C : float_cfg] include C def prec := C.prec def emax := C.emax def emin : ℤ := 1 - C.emax def valid_finite (e : ℤ) (m : ℕ) : Prop := emin ≤ e + prec - 1 ∧ e + prec - 1 ≤ emax ∧ e = max (e + m.size - prec) emin instance dec_valid_finite (e m) : decidable (valid_finite e m) := by unfold valid_finite; apply_instance inductive float | inf : bool → float | nan : float | finite : bool → Π e m, valid_finite e m → float def float.is_finite : float → bool | (float.finite s e m f) := tt | _ := ff def to_rat : Π (f : float), f.is_finite → ℚ | (float.finite s e m f) _ := let (n, d) := int.shift2 m 1 e, r := rat.mk_nat n d in if s then -r else r theorem float.zero.valid : valid_finite emin 0 := ⟨begin rw add_sub_assoc, apply le_add_of_nonneg_right, apply sub_nonneg_of_le, apply int.coe_nat_le_coe_nat_of_le, exact C.prec_pos end, suffices prec ≤ 2 * emax, begin rw ← int.coe_nat_le at this, rw ← sub_nonneg at *, simp only [emin, emax] at *, ring_nf, assumption end, le_trans C.prec_max (nat.le_mul_of_pos_left dec_trivial), by rw max_eq_right; simp [sub_eq_add_neg]⟩ def float.zero (s : bool) : float := float.finite s emin 0 float.zero.valid instance : inhabited float := ⟨float.zero tt⟩ protected def float.sign' : float → semiquot bool | (float.inf s) := pure s | float.nan := ⊤ | (float.finite s e m f) := pure s protected def float.sign : float → bool | (float.inf s) := s | float.nan := ff | (float.finite s e m f) := s protected def float.is_zero : float → bool | (float.finite s e 0 f) := tt | _ := ff protected def float.neg : float → float | (float.inf s) := float.inf (bnot s) | float.nan := float.nan | (float.finite s e m f) := float.finite (bnot s) e m f def div_nat_lt_two_pow (n d : ℕ) : ℤ → bool | (int.of_nat e) := n < d.shiftl e | -[1+ e] := n.shiftl e.succ < d -- TODO(Mario): Prove these and drop 'meta' meta def of_pos_rat_dn (n : ℕ+) (d : ℕ+) : float × bool := begin let e₁ : ℤ := n.1.size - d.1.size - prec, cases h₁ : int.shift2 d.1 n.1 (e₁ + prec) with d₁ n₁, let e₂ := if n₁ < d₁ then e₁ - 1 else e₁, let e₃ := max e₂ emin, cases h₂ : int.shift2 d.1 n.1 (e₃ + prec) with d₂ n₂, let r := rat.mk_nat n₂ d₂, let m := r.floor, refine (float.finite ff e₃ (int.to_nat m) _, r.denom = 1), { exact undefined } end meta def next_up_pos (e m) (v : valid_finite e m) : float := let m' := m.succ in if ss : m'.size = m.size then float.finite ff e m' (by unfold valid_finite at *; rw ss; exact v) else if h : e = emax then float.inf ff else float.finite ff e.succ (nat.div2 m') undefined meta def next_dn_pos (e m) (v : valid_finite e m) : float := match m with | 0 := next_up_pos _ _ float.zero.valid | nat.succ m' := if ss : m'.size = m.size then float.finite ff e m' (by unfold valid_finite at *; rw ss; exact v) else if h : e = emin then float.finite ff emin m' undefined else float.finite ff e.pred (bit1 m') undefined end meta def next_up : float → float | (float.finite ff e m f) := next_up_pos e m f | (float.finite tt e m f) := float.neg $ next_dn_pos e m f | f := f meta def next_dn : float → float | (float.finite ff e m f) := next_dn_pos e m f | (float.finite tt e m f) := float.neg $ next_up_pos e m f | f := f meta def of_rat_up : ℚ → float | ⟨0, _, _, _⟩ := float.zero ff | ⟨nat.succ n, d, h, _⟩ := let (f, exact) := of_pos_rat_dn n.succ_pnat ⟨d, h⟩ in if exact then f else next_up f | ⟨-[1+n], d, h, _⟩ := float.neg (of_pos_rat_dn n.succ_pnat ⟨d, h⟩).1 meta def of_rat_dn (r : ℚ) : float := float.neg $ of_rat_up (-r) meta def of_rat : rmode → ℚ → float | rmode.NE r := let low := of_rat_dn r, high := of_rat_up r in if hf : high.is_finite then if r = to_rat _ hf then high else if lf : low.is_finite then if r - to_rat _ lf > to_rat _ hf - r then high else if r - to_rat _ lf < to_rat _ hf - r then low else match low, lf with float.finite s e m f, _ := if 2 ∣ m then low else high end else float.inf tt else float.inf ff namespace float instance : has_neg float := ⟨float.neg⟩ meta def add (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf tt) (inf ff) := nan | (inf ff) (inf tt) := nan | (inf s₁) _ := inf s₁ | _ (inf s₂) := inf s₂ | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in of_rat mode (to_rat f₁ rfl + to_rat f₂ rfl) meta instance : has_add float := ⟨float.add rmode.NE⟩ meta def sub (mode : rmode) (f1 f2 : float) : float := add mode f1 (-f2) meta instance : has_sub float := ⟨float.sub rmode.NE⟩ meta def mul (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf s₁) f₂ := if f₂.is_zero then nan else inf (bxor s₁ f₂.sign) | f₁ (inf s₂) := if f₁.is_zero then nan else inf (bxor f₁.sign s₂) | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in of_rat mode (to_rat f₁ rfl * to_rat f₂ rfl) meta def div (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf s₁) (inf s₂) := nan | (inf s₁) f₂ := inf (bxor s₁ f₂.sign) | f₁ (inf s₂) := zero (bxor f₁.sign s₂) | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in if f₂.is_zero then inf (bxor s₁ s₂) else of_rat mode (to_rat f₁ rfl / to_rat f₂ rfl) end float end fp
56a0e278a70acf03e8ec025f0a715f50ed8a98ed
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Syntax/Translate/Tactic/Mathlib/Converter.lean
23f074a7d94ddd3ca06ed32db15f8da8b0d61fc8
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
1,478
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathport.Syntax.Translate.Tactic.Basic import Mathport.Syntax.Translate.Tactic.Lean3 open Lean namespace Mathport.Translate.Tactic open Mathport.Translate.Parser -- # tactic.converter @[tr_tactic old_conv] def trOldConv : TacM Syntax.Tactic := do warn! "unsupported tactic old_conv" -- unattested @[tr_tactic find] def trFindTac : TacM Syntax.Tactic := do warn! "unsupported tactic find" -- unattested @[tr_tactic conv_lhs] def trConvLHS : TacM Syntax.Tactic := do `(tactic| conv_lhs $[at $((← parse (tk "at" *> ident)?).map mkIdent)]? $[in $(← liftM $ (← parse (tk "in" *> pExpr)?).mapM trExpr):term]? => $(← trConvBlock (← itactic)):convSeq) @[tr_tactic conv_rhs] def trConvRHS : TacM Syntax.Tactic := do `(tactic| conv_rhs $[at $((← parse (tk "at" *> ident)?).map mkIdent)]? $[in $(← liftM $ (← parse (tk "in" *> pExpr)?).mapM trExpr):term]? => $(← trConvBlock (← itactic)):convSeq) @[tr_conv erw] def trERwConv : TacM Syntax.Conv := do let q ← liftM $ (← parse rwRules).mapM trRwRule if let some cfg ← expr? then warn! "warning: unsupported: erw with cfg: {repr cfg}" `(conv| erw [$q,*]) @[tr_conv apply_congr] def trApplyCongr : TacM Syntax.Conv := do `(conv| apply_congr $[$(← liftM $ (← parse (pExpr)?).mapM trExpr)]?)
d2d3bd46bf85f1ceb8bbbe485e49b6b7af827536
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/08_Building_Theories_and_Proofs.org.36.lean
08d92a1d77593df4bc81fabd2995b254cd1a35cf
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
54
lean
import standard import data.nat open nat (hiding add)
fac8248bf99943794ae69e5a83a8f154a2ff67c2
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/category_theory/natural_isomorphism.lean
ce931c5699a6197542baec4f599b39a76e6676f7
[ "Apache-2.0" ]
permissive
anrddh/mathlib
6a374da53c7e3a35cb0298b0cd67824efef362b4
a4266a01d2dcb10de19369307c986d038c7bb6a6
refs/heads/master
1,656,710,827,909
1,589,560,456,000
1,589,560,456,000
264,271,800
0
0
Apache-2.0
1,589,568,062,000
1,589,568,061,000
null
UTF-8
Lean
false
false
5,359
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.functor_category import category_theory.isomorphism open category_theory universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory open nat_trans /-- The application of a natural isomorphism to an object. We put this definition in a different namespace, so that we can use α.app -/ @[simp, reducible] def iso.app {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] {F G : C ⥤ D} (α : F ≅ G) (X : C) : F.obj X ≅ G.obj X := { hom := α.hom.app X, inv := α.inv.app X, hom_inv_id' := begin rw [← comp_app, iso.hom_inv_id], refl end, inv_hom_id' := begin rw [← comp_app, iso.inv_hom_id], refl end } namespace nat_iso open category_theory.category category_theory.functor variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] {E : Type u₃} [category.{v₃} E] @[simp] lemma trans_app {F G H : C ⥤ D} (α : F ≅ G) (β : G ≅ H) (X : C) : (α ≪≫ β).app X = α.app X ≪≫ β.app X := rfl lemma app_hom {F G : C ⥤ D} (α : F ≅ G) (X : C) : (α.app X).hom = α.hom.app X := rfl lemma app_inv {F G : C ⥤ D} (α : F ≅ G) (X : C) : (α.app X).inv = α.inv.app X := rfl @[simp] lemma hom_inv_id_app {F G : C ⥤ D} (α : F ≅ G) (X : C) : α.hom.app X ≫ α.inv.app X = 𝟙 (F.obj X) := congr_fun (congr_arg app α.hom_inv_id) X @[simp] lemma inv_hom_id_app {F G : C ⥤ D} (α : F ≅ G) (X : C) : α.inv.app X ≫ α.hom.app X = 𝟙 (G.obj X) := congr_fun (congr_arg app α.inv_hom_id) X variables {F G : C ⥤ D} instance hom_app_is_iso (α : F ≅ G) (X : C) : is_iso (α.hom.app X) := { inv := α.inv.app X, hom_inv_id' := begin rw [←comp_app, iso.hom_inv_id, ←id_app] end, inv_hom_id' := begin rw [←comp_app, iso.inv_hom_id, ←id_app] end } instance inv_app_is_iso (α : F ≅ G) (X : C) : is_iso (α.inv.app X) := { inv := α.hom.app X, hom_inv_id' := begin rw [←comp_app, iso.inv_hom_id, ←id_app] end, inv_hom_id' := begin rw [←comp_app, iso.hom_inv_id, ←id_app] end } lemma hom_app_inv_app_id (α : F ≅ G) (X : C) : α.hom.app X ≫ α.inv.app X = 𝟙 _ := hom_inv_id_app _ _ lemma inv_app_hom_app_id (α : F ≅ G) (X : C) : α.inv.app X ≫ α.hom.app X = 𝟙 _ := inv_hom_id_app _ _ variables {X Y : C} lemma naturality_1 (α : F ≅ G) (f : X ⟶ Y) : (α.inv.app X) ≫ (F.map f) ≫ (α.hom.app Y) = G.map f := begin erw [naturality, ←category.assoc, is_iso.hom_inv_id, category.id_comp] end lemma naturality_2 (α : F ≅ G) (f : X ⟶ Y) : (α.hom.app X) ≫ (G.map f) ≫ (α.inv.app Y) = F.map f := begin erw [naturality, ←category.assoc, is_iso.hom_inv_id, category.id_comp] end def is_iso_of_is_iso_app (α : F ⟶ G) [∀ X : C, is_iso (α.app X)] : is_iso α := { inv := { app := λ X, inv (α.app X), naturality' := λ X Y f, begin have h := congr_arg (λ f, inv (α.app X) ≫ (f ≫ inv (α.app Y))) (α.naturality f).symm, simp only [is_iso.inv_hom_id_assoc, is_iso.hom_inv_id, assoc, comp_id, cancel_mono] at h, exact h end } } instance is_iso_of_is_iso_app' (α : F ⟶ G) [H : ∀ X : C, is_iso (nat_trans.app α X)] : is_iso α := @nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ α H -- TODO can we make this an instance? def is_iso_app_of_is_iso (α : F ⟶ G) [is_iso α] (X) : is_iso (α.app X) := { inv := (inv α).app X, hom_inv_id' := congr_fun (congr_arg nat_trans.app (is_iso.hom_inv_id α)) X, inv_hom_id' := congr_fun (congr_arg nat_trans.app (is_iso.inv_hom_id α)) X } def of_components (app : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality : ∀ {X Y : C} (f : X ⟶ Y), (F.map f) ≫ ((app Y).hom) = ((app X).hom) ≫ (G.map f)) : F ≅ G := as_iso { app := λ X, (app X).hom } @[simp] lemma of_components.app (app' : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) : (of_components app' naturality).app X = app' X := by tidy @[simp] lemma of_components.hom_app (app : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) : (of_components app naturality).hom.app X = (app X).hom := rfl @[simp] lemma of_components.inv_app (app : ∀ X : C, (F.obj X) ≅ (G.obj X)) (naturality) (X) : (of_components app naturality).inv.app X = (app X).inv := rfl def hcomp {F G : C ⥤ D} {H I : D ⥤ E} (α : F ≅ G) (β : H ≅ I) : F ⋙ H ≅ G ⋙ I := begin refine ⟨α.hom ◫ β.hom, α.inv ◫ β.inv, _, _⟩, { ext, rw [←nat_trans.exchange], simp, refl }, ext, rw [←nat_trans.exchange], simp, refl end -- declare local notation for nat_iso.hcomp localized "infix ` ■ `:80 := category_theory.nat_iso.hcomp" in category end nat_iso namespace functor variables {C : Type u₁} [category.{v₁} C] def ulift_down_up : ulift_down.{v₁} C ⋙ ulift_up C ≅ 𝟭 (ulift.{u₂} C) := { hom := { app := λ X, @category_struct.id (ulift.{u₂} C) _ X }, inv := { app := λ X, @category_struct.id (ulift.{u₂} C) _ X } } def ulift_up_down : ulift_up.{v₁} C ⋙ ulift_down C ≅ 𝟭 C := { hom := { app := λ X, 𝟙 X }, inv := { app := λ X, 𝟙 X } } end functor end category_theory
b771555d1a027eb60b37334a1dcc3608646c37c0
b147e1312077cdcfea8e6756207b3fa538982e12
/data/finset.lean
6262d43351a2323eb20a1fb534d78e1a9f7b403c
[ "Apache-2.0" ]
permissive
SzJS/mathlib
07836ee708ca27cd18347e1e11ce7dd5afb3e926
23a5591fca0d43ee5d49d89f6f0ee07a24a6ca29
refs/heads/master
1,584,980,332,064
1,532,063,841,000
1,532,063,841,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
57,237
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro Finite sets. -/ import logic.embedding order.boolean_algebra algebra.order_functions data.multiset data.sigma.basic data.set.lattice open multiset subtype nat lattice variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ h := by congr; assumption @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 := erase_dup_eq_self.2 s.2 end finset namespace finset instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /- membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /- set coercion -/ /-- Convert a finset to a set in the natural way. -/ def to_set (s : finset α) : set α := {x | x ∈ s} instance : has_lift (finset α) (set α) := ⟨to_set⟩ @[simp] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl /- extensionality -/ theorem ext {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[extensionality] theorem ext' {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext.2 @[simp] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ := (set.ext_iff _ _).trans ext.symm /- subset -/ instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩ theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext.2 $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp] theorem coe_subset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } @[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff /- empty -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ | e := not_mem_empty a $ e ▸ h @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero theorem exists_mem_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a : α, a ∈ s := exists_mem_of_ne_zero (mt val_eq_zero.1 h) @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := by simp [set.ext_iff] /-- `singleton a` is the set `{a}` containing `a` and nothing else. -/ def singleton (a : α) : finset α := ⟨_, nodup_singleton a⟩ local prefix `ι`:90 := singleton @[simp] theorem singleton_val (a : α) : (ι a).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ι a ↔ b = a := by simp [singleton] theorem not_mem_singleton {a b : α} : a ∉ ι b ↔ a ≠ b := by simp theorem mem_singleton_self (a : α) : a ∈ ι a := by simp theorem singleton_inj {a b : α} : ι a = ι b ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ @[simp] theorem singleton_ne_empty (a : α) : ι a ≠ ∅ := ne_empty_of_mem (mem_singleton_self _) @[simp] lemma coe_singleton (a : α) : ↑(ι a) = ({a} : set α) := by simp [set.ext_iff] /- insert -/ section decidable_eq variables [decidable_eq α] /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ @[simp] theorem has_insert_eq_insert (a : α) (s : finset α) : has_insert.insert a s = insert a s := rfl theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) := by simp [erase_dup_cons] theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := by simp theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := by simp * theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left @[simp] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) := by simp [set.ext_iff] @[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext.2 $ by simp [or.left_comm] @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext.2 $ by simp @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := ne_empty_of_mem (mem_insert_self a s) theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp [subset_iff, or_imp_distrib, forall_and_distrib] theorem subset_insert [h : decidable_eq α] (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a, a ∉ s ∧ insert a s ⊆ t) := iff.intro (assume ⟨h₁, h₂⟩, have ∃a, a ∈ t ∧ a ∉ s, by simpa [finset.subset_iff, classical.not_forall] using h₂, let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, h₁⟩⟩) (assume ⟨a, hat, has⟩, let ⟨h₁, h₂⟩ := insert_subset.mp has in ⟨h₂, assume h, hat $ h h₁⟩) lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[recursor 6] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [insert_val, ndinsert_of_not_mem m] } end) nd @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s @[simp] theorem singleton_eq_singleton (a : α) : _root_.singleton a = ι a := rfl @[simp] theorem insert_empty_eq_singleton (a : α) : {a} = ι a := rfl @[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = ι a := by simp [singleton] /- union -/ /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩ theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl @[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := by simp * theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := by simp * theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ := by simp [not_or_distrib] @[simp] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := by simp [set.ext_iff] theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩) theorem subset_union_left {s₁ s₂ : finset α} : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right {s₁ s₂ : finset α} : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ @[simp] theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := by simp [ext, or_comm] instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := by simp [ext, or_comm, or.left_comm] instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext.2 $ by simp instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext.2 $ by simp [or_comm, or.left_comm] theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by simp @[simp] theorem union_self (s : finset α) : s ∪ s = s := by simp @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := by simp [ext] @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := by simp [ext] theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := by simp [ext, or_comm, or.left_comm] @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp [ext, or_comm, or.left_comm] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp [ext, or.left_comm] theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp [ext] /- inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left {s₁ s₂ : finset α} : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right {s₁ s₂ : finset α} : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ := by simp [subset_iff] {contextual:=tt}; finish @[simp] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := by simp [set.ext_iff] @[simp] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext.2 $ by simp [and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext.2 $ by simp [and_comm, and.left_comm] @[simp] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext.2 $ by simp [and.left_comm] @[simp] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext.2 $ by simp [and.left_comm] @[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext.2 $ by simp @[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext.2 $ by simp @[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext.2 $ by simp @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext.2 $ by simp; intro x; constructor; finish @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext.2 $ assume a', by by_cases h' : a' = a; simp [mem_inter, mem_insert, h, h', and_comm] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} : a ∈ s → ι a ∩ s = ι a := show a ∈ s → insert a ∅ ∩ s = insert a ∅, by simp {contextual := tt} @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} : a ∉ s → ι a ∩ s = ∅ := show a ∉ s → insert a ∅ ∩ s = ∅, by simp {contextual := tt} @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ ι a = ι a := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ ι a = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] /- lattice laws -/ instance : lattice (finset α) := { sup := (∪), sup_le := assume a b c, union_subset, le_sup_left := assume a b, subset_union_left, le_sup_right := assume a b, subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := assume a b, inter_subset_left, inf_le_right := assume a b, inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl instance : semilattice_inf_bot (finset α) := { bot := ∅, bot_le := empty_subset, ..finset.lattice.lattice } instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp [subset_iff, and_imp, or_imp_distrib] {contextual:=tt}, ..finset.lattice.lattice } theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext.2 $ by simp [mem_inter, mem_union]; intro; split; finish /- erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := by simp @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a := by simp {contextual:=tt} theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b := by simp {contextual:=tt} theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := ext.2 $ assume x, by simp; constructor; finish theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext.2 $ assume x, by simp; constructor; finish theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ @[simp] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) := by simp [set.ext_iff, and_comm] lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp [subset_iff, or_iff_not_imp_left]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ /- sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩ @[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} : a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2 @[simp] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := ext.2 $ λ a, by simpa [or_and_distrib_left, dec_em] using or_iff_right_of_imp (@h a) @[simp] theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := ext.2 $ by simp {contextual := tt} @[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := by simp theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ := by simpa [subset_iff] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂) @[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := by simp [set.ext_iff] end decidable_eq /- attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∀a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] : decidable_eq (Πa∈s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∃a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /- filter -/ section filter variables {p q : α → Prop} [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext.2 $ assume a, by simp [and_comm, and.left_comm] @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext.2 $ assume a, by simp variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext.2 $ by simp [or_and_distrib_right] theorem filter_or (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext.2 $ by simp [and_or_distrib_left] theorem filter_and (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext.2 $ by simp [and_comm, and.left_comm] theorem filter_not (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext.2 $ by simpa [and_comm] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext.2 $ by simp theorem filter_union_filter_neg_eq (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := by simp [filter_not] theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ := by simp [filter_not] @[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := by simp [set.ext_iff] end filter /- range -/ section range variables {n m l : ℕ} /-- `range n` is the set of integers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ by simp @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := finset.induction_on s ⟨0, by simp⟩ $ λ a s ha ⟨n, hn⟩, ⟨max (a + 1) n, insert_subset.2 ⟨by simpa using le_max_left (a+1) n, subset.trans hn (by simp [le_max_right])⟩⟩ end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp theorem exists_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) := by simp [or_and_distrib_right, exists_or_distrib] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := by simp theorem forall_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) := by simp [or_imp_distrib, forall_and_distrib] end finset /- erase_dup on list and multiset -/ namespace multiset variable [decidable_eq α] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 (erase_dup_eq_self.2 n).symm @[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a :: s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons end multiset namespace list variable [decidable_eq α] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] end list namespace finset section map open function def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : α ↪ β) (s : finset α) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := by simp [mem_def] @[simp] theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.map f := mem_map.2 ⟨_, h, rfl⟩ theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext.2 $ by simp theorem map_refl : s.map (embedding.refl _) = s := ext.2 $ by simp [embedding.refl] theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp [erase_dup_map_erase_dup_eq] theorem map_subset_map {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.map f ⊆ s₂.map f := by simp [subset_def, map_subset_map h] theorem map_filter {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext.2 $ λ b, by simp; rw ← exists_and_distrib_right; refine exists_congr (λ a, (and_congr_right $ λ e, _).trans and.right_comm); simp [e.2.symm] theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := ext.2 $ by simp [mem_map, or_and_distrib_right, exists_or_distrib] theorem map_inter [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f := ext.2 $ by simp [mem_map]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, f.2 (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem map_singleton (f : α ↪ β) (a : α) : (singleton a).map f = singleton (f a) := ext.2 $ by simp [mem_map, eq_comm] @[simp] theorem map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) : (insert a s).map f = insert (f a) (s.map f) := by simp [insert_eq, map_union] @[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by simp [embedding.subtype]; rw attach_val; simp [multiset.attach_map_val] end map section image variables [decidable_eq β] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset @[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl variables {f : α → β} {s : finset α} @[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp [mem_def] @[simp] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ @[simp] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := by simp [set.ext_iff] theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext.2 $ by simp @[simp] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f := multiset.erase_dup_eq_self.2 (nodup_map_on H s.2) theorem image_id [decidable_eq α] : s.image id = s := ext.2 $ by simp theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp [erase_dup_map_erase_dup_eq] theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp [subset_def, multiset.map_subset_map h] theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext.2 $ λ b, by simp [and_comm]; rw ← exists_and_distrib_left; exact exists_congr (λ a, and.left_comm.trans $ and_congr_right $ λ e, by simp [e.symm]) theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext.2 $ by simp [mem_image, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f := ext.2 $ by simp [mem_image]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem image_singleton [decidable_eq α] (f : α → β) (a : α) : (singleton a).image f = singleton (f a) := ext.2 $ by simp [mem_image, eq_comm] @[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) : (insert a s).image f = insert (f a) (s.image f) := by simp [insert_eq, image_union] @[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s := eq_of_veq $ by simp [multiset.attach_map_val] @[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) := begin apply eq_of_veq, dsimp, rw [attach_ndinsert, multiset.erase_dup_eq_self.2], { refl }, apply nodup_map_on, exact assume ⟨a', _⟩ _ ⟨b', _⟩ _ h, by simp at h; simp [h], exact multiset.nodup_attach.2 s.2 end theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f := eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm end image /- card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset α) : nat := s.1.card theorem card_def (s : finset α) : s.card = s.1.card := rfl @[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl @[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset α} : 0 < card s ↔ s ≠ ∅ := pos_iff_ne_zero.trans $ not_congr card_eq_zero @[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 := by simpa [card] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 := by by_cases a ∈ s; simp [h, nat.le_add_right] @[simp] theorem card_singleton (a : α) : card (singleton a) = 1 := card_singleton _ theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem theorem card_range (n : ℕ) : card (range n) = n := card_range n theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α} (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s := by simp [card, image_val_of_inj_on H] theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α) (H : function.injective f) : card (image f s) = card s := card_image_of_inj_on $ λ x _ y _ h, H h lemma card_eq_of_bijective [decidable_eq α] {s : finset α} {n : ℕ} (f : ∀i, i < n → α) (hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s) (f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : card s = n := have ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩, have s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)), by simpa [ext], calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n lemma card_eq_succ [decidable_eq α] {s : finset α} {a : α} {n : ℕ} : s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have card s > 0, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := finset.exists_mem_of_ne_empty $ card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp [eq, card_erase_of_mem has]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat) theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t := multiset.card_le_of_le ∘ val_le_iff.mpr theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma card_lt_card [decidable_eq α] {s t : finset α} (h : s ⊂ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on [decidable_eq α] [decidable_eq β] {s : finset α} {t : finset β} (f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) : card s ≤ card t := calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≤ card t : card_le_of_subset $ assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end lemma card_le_of_inj_on [decidable_eq α] {n} {s : finset α} (f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s := calc n = card (range n) : (card_range n).symm ... ≤ card s : card_le_card_of_inj_on f (by simp; assumption) (by simp; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂) @[elab_as_eliminator] lemma strong_induction_on {p : finset α → Sort*} : ∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s | ⟨s, nd⟩ ih := multiset.strong_induction_on s (λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop} (s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s := finset.strong_induction_on s $ λ s, finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $ λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) end card section bind variables [decidable_eq β] {s : finset α} {t : α → finset β} /-- `bind s t` is the union of `t x` over `x ∈ s` -/ protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bind_val (s : finset α) (t : α → finset β) : (s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl @[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl @[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a := by simp [mem_def] @[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t := ext.2 $ by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind [decidable_eq α] {a : α} : (singleton a).bind t = t a := show (insert a ∅ : finset α).bind t = t a, by simp theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} : (s.image f).bind t = s.bind (λa, t (f a)) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp {contextual := tt}) theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} : (s.bind t).image f = s.bind (λa, (t a).image f) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [image_union] {contextual := tt}) theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) := ext.2 $ by simp lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ := have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa [finset.subset_iff] lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := finset.ext.mpr $ by simp [eq_comm] end bind section prod variables {s : finset α} {t : finset β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bind (λa, t.image $ λb, (a, b)) := ext.2 $ by simp [and.left_comm] @[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ end prod section sigma variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) := ⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)} : s₁ ⊆ s₂ → (∀a, t₁ a ⊆ t₂ a) → s₁.sigma t₁ ⊆ s₂.sigma t₂ := by simp [subset_iff, mem_sigma] {contextual := tt} theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) : s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) := ext.2 $ by simp [and.left_comm] end sigma section pi variables {δ : α → Type*} [decidable_eq α] def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) := ⟨s.1.pi (λ a, (t a).1), nodup_pi s.2 (λ a _, (t a).2)⟩ @[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) : (s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl @[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} : f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) := mem_pi _ _ _ def pi.empty (β : α → Sort*) [decidable_eq α] (a : α) (h : a ∈ (∅ : finset α)) : β a := multiset.pi.empty β a h def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' := multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h) @[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) : pi.cons s a b f a h = b := multiset.pi.cons_same _ lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') : pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) := multiset.pi.cons_ne _ _ lemma injective_pi_cons {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) : function.injective (pi.cons s a b) := assume e₁ e₂ eq, @multiset.injective_pi_cons α _ δ a b s.1 hs _ _ $ funext $ assume e, funext $ assume h, have pi.cons s a b e₁ e (by simpa using h) = pi.cons s a b e₂ e (by simpa using h), by rw [eq], this @[simp] lemma pi_empty {t : Πa:α, finset (δ a)} : pi (∅ : finset α) t = singleton (pi.empty δ) := rfl @[simp] lemma pi_insert [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) : pi (insert a s) t = (t a).bind (λb, (pi s t).image (pi.cons s a b)) := begin apply eq_of_veq, rw ← multiset.erase_dup_eq_self.2 (pi (insert a s) t).2, refine (λ s' (h : s' = a :: s.1), (_ : erase_dup (multiset.pi s' (λ a, (t a).1)) = erase_dup ((t a).1.bind $ λ b, erase_dup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $ λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha), subst s', rw pi_cons, congr, funext b, rw multiset.erase_dup_eq_self.2, exact multiset.nodup_map (multiset.injective_pi_cons ha) (pi s t).2, end end pi section powerset def powerset (s : finset α) : finset (finset α) := ⟨s.1.powerset.pmap finset.mk (λ t h, nodup_of_le (mem_powerset.1 h) s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset.2 s.2)⟩ @[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t := by cases s; simp [powerset]; rw ← val_le_iff @[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s := mem_powerset.2 (empty_subset _) @[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 (subset.refl _) @[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t := ⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _), λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩ @[simp] theorem card_powerset (s : finset α) : card (powerset s) = 2 ^ card s := (card_pmap _ _ _).trans (card_powerset s.1) end powerset section fold variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op] local notation a * b := op a b include hc ha /-- `fold op b f s` folds the commutative associative operation `op` over the `f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/ def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b variables {op} {f : α → β} {b : β} {s : finset α} {a : α} @[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl @[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f := by simp [fold, ndinsert_of_not_mem h] @[simp] theorem fold_singleton : (singleton a).fold op b f = f a * b := by simp [fold] @[simp] theorem fold_image [decidable_eq α] [decidable_eq γ] {g : γ → α} {s : finset γ} (H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) := by simp [fold, image_val_of_inj_on H, map_map] @[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g := by rw [fold, fold, map_congr H] theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} : s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g := by simp [fold, fold_distrib] theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op'] {m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) : s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) := by rw [fold, fold, ← fold_hom op hm, multiset.map_map] theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} : (s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f := by unfold fold; rw [← fold_add op, ← map_add, union_val, inter_val, union_add_inter, map_add, hc.comm, fold_add] @[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] : (insert a s).fold op b f = f a * s.fold op b f := by haveI := classical.prop_decidable; rw [fold, insert_val', ← fold_erase_dup_idem op, erase_dup_map_erase_dup_eq, fold_erase_dup_idem op]; simp [fold] end fold section sup variables [semilattice_sup_bot α] [decidable_eq α] [decidable_eq β] /-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/ def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f variables {s s₁ s₂ : finset β} {f : β → α} lemma sup_val : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ := fold_empty @[simp] lemma sup_insert {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f := fold_insert_idem @[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b := calc _ = f b ⊔ (∅:finset β).sup f : sup_insert ... = f b : by simp lemma sup_union : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by simp) (by simp {contextual := tt}; cc) lemma sup_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.sup f ≤ s.sup g := finset.induction_on s (by simp) (by simp [-sup_le_iff, sup_le_sup] {contextual := tt}) lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := calc f b ≤ f b ⊔ s.sup f : le_sup_left ... = (insert b s).sup f : by simp ... = s.sup f : by simp [hb] lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := finset.induction_on s (by simp) (by simp {contextual := tt}) lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) end sup section inf variables [semilattice_inf_top α] [decidable_eq α] [decidable_eq β] /-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/ def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f variables {s s₁ s₂ : finset β} {f : β → α} lemma inf_val : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ := fold_empty @[simp] lemma inf_insert {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f := fold_insert_idem @[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b := calc _ = f b ⊓ (∅:finset β).inf f : inf_insert ... = f b : by simp lemma inf_union : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := finset.induction_on s₁ (by simp) (by simp {contextual := tt}; cc) lemma inf_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.inf f ≤ s.inf g := finset.induction_on s (by simp) (by simp [inf_le_inf] {contextual := tt}) lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := calc f b ≥ f b ⊓ s.inf f : inf_le_left ... = (insert b s).inf f : by simp ... = s.inf f : by simp [hb] lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := finset.induction_on s (by simp) (by simp {contextual := tt}) lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f := le_inf $ assume b hb, inf_le (h hb) end inf /- max and min of finite sets -/ section max_min variables [decidable_linear_order α] protected def max : finset α → option α := fold (option.lift_or_get max) none some theorem max_eq_sup_with_bot (s : finset α) : s.max = @sup (with_bot α) α _ _ _ s some := rfl @[simp] theorem max_empty : (∅ : finset α).max = none := by simp [finset.max] @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = option.lift_or_get max (some a) s.max := by simp [finset.max, fold_insert_idem] @[simp] theorem max_singleton {a : α} : finset.max {a} = some a := by simp [finset.max, option.lift_or_get] theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max := (@le_sup (with_bot α) _ _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, by_contradiction (λ hs, let ⟨a, ha⟩ := exists_mem_of_ne_empty hs in let ⟨b, hb⟩ := max_of_mem ha in by simpa [h] using hb), λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s := finset.induction_on s (by simp) $ λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice max_choice (some b) s.max with q q; simp [q] at h, { exact absurd h p }, { exact mem_insert_of_mem (ih h) } } end theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b := by rcases @le_sup (with_bot α) _ _ _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption protected def min : finset α → option α := fold (option.lift_or_get min) none some theorem min_eq_inf_with_top (s : finset α) : s.min = @inf (with_top α) α _ _ _ s some := rfl @[simp] theorem min_empty : (∅ : finset α).min = none := by simp [finset.min] @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = option.lift_or_get min (some a) s.min := by simp [finset.min, fold_insert_idem] @[simp] theorem min_singleton {a : α} : finset.min {a} = some a := by simp [finset.min, option.lift_or_get] theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min := (@inf_le (with_top α) _ _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, by_contradiction (λ hs, let ⟨a, ha⟩ := exists_mem_of_ne_empty hs in let ⟨b, hb⟩ := min_of_mem ha in by simpa [h] using hb), λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s := finset.induction_on s (by simp) $ λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice min_choice (some b) s.min with q q; simp [q] at h, { exact absurd h p }, { exact mem_insert_of_mem (ih h) } } end theorem le_min_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b := by rcases @inf_le (with_top α) _ _ _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption end max_min section sort variables (r : α → α → Prop) [decidable_rel r] [is_trans α r] [is_antisymm α r] [is_total α r] /-- `sort s` constructs a sorted list from the unordered set `s`. (Uses merge sort algorithm.) -/ def sort (s : finset α) : list α := sort r s.1 @[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) := sort_sorted _ _ @[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 := sort_eq _ _ @[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup := (by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s)) @[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s := list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s) end sort section disjoint variable [decidable_eq α] theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := by simp [_root_.disjoint, subset_iff]; refl theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp [disjoint_left, imp_not_comm] theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s := by simp [disjoint_left]; refl @[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s := by rw disjoint.comm; simp @[simp] theorem disjoint_insert_left {a : α} {s t : finset α} : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp [disjoint_left, or_imp_distrib, forall_and_distrib]; refl @[simp] theorem disjoint_insert_right {a : α} {s t : finset α} : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp [disjoint_left, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp [disjoint_right, or_imp_distrib, forall_and_distrib] @[simp] theorem card_disjoint_union {s t : finset α} : disjoint s t → card (s ∪ t) = card s + card t := finset.induction_on s (by simp) $ by simp {contextual := tt} end disjoint theorem sort_sorted_lt [decidable_linear_order α] (s : finset α) : list.sorted (<) (sort (≤) s) := (sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _) instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩ end finset namespace list variable [decidable_eq α] theorem to_finset_card_of_nodup {l : list α} : l.nodup → l.to_finset.card = l.length := begin induction l, case list.nil { simp }, case list.cons : _ _ ih { intros nd, simp at nd, simp [finset.card_insert_of_not_mem ((not_iff_not_of_iff mem_to_finset).mpr nd.1), ih nd.2] } end end list
3dfc56a505363484d77c634d3c50b0f209aee905
dc15192b741b5d1c22cea8d65d6eb38bce3d838d
/src/circuit.lean
39ccf51f5e4b2523bc17929e2b0d84b96dc6e678
[]
no_license
VArtem/lean-matroids
86910241ac8d1a5ec7b35adb77c1cc9969480fb9
a8969b1cb2456820ccbdce65e2e168c48c30d9bf
refs/heads/main
1,678,556,999,525
1,614,537,008,000
1,614,537,008,000
338,915,729
0
1
null
null
null
null
UTF-8
Lean
false
false
5,104
lean
import data.nat.basic import data.finset.basic import tactic import matroid import finset import base_of variables {α : Type*} [fintype α] [decidable_eq α] {m : matroid α} {A B : finset α} open finset namespace matroid lemma circuit_sdiff : m.circuit A → B ⊆ A → B ≠ ∅ → m.ind (A \ B) := begin rintro ⟨Anind, Aerase⟩ Bsubset Bnonempty, rw ← nonempty_iff_ne_empty at Bnonempty, rcases Bnonempty with ⟨x, xB⟩, have xA := Bsubset xB, refine m.ind_subset _ (A.erase x) _ (Aerase x xA), exact sdiff_subset_erase_of_mem xB, end lemma circuit_ssubset : m.circuit A → B ⊂ A → m.ind B := begin rintro ⟨Anind, Aerase⟩ Bsubset, rw ssubset_iff at Bsubset, rcases Bsubset with ⟨x, xB, x_insert⟩, rw insert_subset at x_insert, cases x_insert with xA h_subset, apply m.ind_subset _ (A.erase x) _ (Aerase x xA), rw ← erase_eq_of_not_mem xB, exact erase_subset_erase x h_subset, end lemma ind_iff_not_contains_circuit (A) : m.ind A ↔ ∀ C, m.circuit C → ¬ C ⊆ A := begin split, { intros Aind C Ccirc hCA, refine dep_superset hCA (Ccirc.1) Aind, }, { intros h, by_contradiction Aind, suffices subsets_dep : ∀ k ≤ A.card, ∃ B ⊆ A, B.card = k ∧ ¬ m.ind B, { obtain ⟨B, Bsub, Bcard, Bdep⟩ := subsets_dep 0 (zero_le _), rw [card_eq_zero] at Bcard, subst Bcard, exact Bdep m.ind_empty, }, intros k k_le_card, refine nat.decreasing_induction (λ n, _) k_le_card _, { rintro ⟨B, hBA, Bcard, Bdep⟩, specialize h B, rw imp_not_comm at h, specialize h hBA, rw circuit at h, push_neg at h, obtain ⟨x, xAB, h_dep_erase⟩ := h Bdep, refine ⟨B.erase x, subset.trans (erase_subset _ _) hBA, _, h_dep_erase⟩, rw ← card_erase_of_mem' xAB at Bcard, exact nat.succ_inj'.1 Bcard, }, { use [A, subset.refl _, rfl], } } end lemma dep_iff_contains_circuit (A) : ¬ m.ind A ↔ ∃ C, m.circuit C ∧ C ⊆ A := begin have h := not_congr (ind_iff_not_contains_circuit A), push_neg at h, exact h, end theorem empty_not_circuit : ¬ m.circuit ∅ := λ ⟨empty_dep, _⟩, empty_dep m.ind_empty theorem circuit_not_subset {A B} : m.circuit A → m.circuit B → A ⊆ B → A = B := begin rintro Acirc Bcirc h_subset, cases eq_or_ssubset_of_subset h_subset, { exact h, }, { exfalso, exact Acirc.1 (circuit_ssubset Bcirc h), }, end theorem circuit_common_element {x} (Acirc : m.circuit A) (Bcirc : m.circuit B): A ≠ B → x ∈ A → x ∈ B → ∃ C, m.circuit C ∧ C ⊆ ((A ∪ B).erase x) := begin intros A_neq_B xA xB, rw ← dep_iff_contains_circuit, intro union_ind, have inter_ind : m.ind (A ∩ B), from by { rw ind_iff_not_contains_circuit, intros C Ccirc Csub, have hAC := circuit_not_subset Ccirc Acirc (subset.trans Csub (inter_subset_left _ _)), have hBC := circuit_not_subset Ccirc Bcirc (subset.trans Csub (inter_subset_right _ _)), exact A_neq_B (eq.trans hAC.symm hBC), }, have tmp : A ∩ B ⊆ A ∪ B := subset.trans (inter_subset_left _ _) (subset_union_left _ _), obtain ⟨X, hABX, hXbase_of⟩ := ind_subset_base_of tmp (inter_ind), have tmp2 := ind_card_le_base_of_card (erase_subset _ _) union_ind hXbase_of, replace tmp2 := nat.add_le_add_right tmp2 1, rw [card_erase_of_mem' (mem_union_left _ xA)] at tmp2, have tmp3 := card_le_of_subset hXbase_of.1, cases eq_or_eq_succ_of_le_and_le_succ tmp3 tmp2, { replace h := eq_of_subset_of_card_le (hXbase_of.1) (ge_of_eq h), subst h, refine (dep_iff_contains_circuit (A ∪ B)).2 _ hXbase_of.2.1, use [A, Acirc, subset_union_left _ _], }, { cases very_important_lemma hABX hXbase_of.1 h.symm, { refine (dep_iff_contains_circuit X).2 _ hXbase_of.2.1, use [A, Acirc, h_1], }, { refine (dep_iff_contains_circuit X).2 _ hXbase_of.2.1, use [B, Bcirc, h_1], } } end lemma circuit_subset_ind_insert {x} (Aind : m.ind A) (hx : x ∉ A) (Bcirc : m.circuit B) : B ⊆ insert x A → x ∈ B := begin intro Bsub, by_contradiction, rw [subset_insert_iff, erase_eq_of_not_mem h] at Bsub, exact (circuit_dep Bcirc) (ind_subset_def Bsub Aind), end theorem base_insert_unique_circuit {x} (Abase : m.base A) (hx : x ∉ A) : ∃! C, C ⊆ (insert x A) ∧ m.circuit C := begin have tmp : ¬ m.ind (insert x A) := Abase.2 x hx, obtain ⟨C, Ccirc, Csub⟩ := (dep_iff_contains_circuit _).1 tmp, use [C, Csub, Ccirc], rintro D ⟨Dsub, Dcirc⟩, have Cmem := circuit_subset_ind_insert (Abase.1) hx Ccirc Csub, have Dmem := circuit_subset_ind_insert (Abase.1) hx Dcirc Dsub, by_contradiction, rcases circuit_common_element Dcirc Ccirc h Dmem Cmem with ⟨U, Ucirc, Usub⟩, suffices hUA : U ⊆ A, { exact (circuit_dep Ucirc) (ind_subset_def hUA Abase.1), }, { have tmp : (D ∪ C) ⊆ (insert x A) := union_subset Dsub Csub, replace tmp := erase_subset_erase x tmp, rw erase_insert hx at tmp, exact subset.trans Usub tmp, } end end matroid
27fc71b06a2bdcc944698c27bdd04c38fe228604
8e6cad62ec62c6c348e5faaa3c3f2079012bdd69
/src/topology/algebra/infinite_sum.lean
a3bd35220256758c7e68f276b4ba68fd37de38d7
[ "Apache-2.0" ]
permissive
benjamindavidson/mathlib
8cc81c865aa8e7cf4462245f58d35ae9a56b150d
fad44b9f670670d87c8e25ff9cdf63af87ad731e
refs/heads/master
1,679,545,578,362
1,615,343,014,000
1,615,343,014,000
312,926,983
0
0
Apache-2.0
1,615,360,301,000
1,605,399,418,000
Lean
UTF-8
Lean
false
false
46,513
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import algebra.big_operators.intervals import topology.instances.real import topology.algebra.module import data.indicator_function import data.equiv.encodable.lattice import order.filter.at_top_bot /-! # Infinite sum over a topological monoid This sum is known as unconditionally convergent, as it sums to the same value under all possible permutations. For Euclidean spaces (finite dimensional Banach spaces) this is equivalent to absolute convergence. Note: There are summable sequences which are not unconditionally convergent! The other way holds generally, see `has_sum.tendsto_sum_nat`. ## References * Bourbaki: General Topology (1995), Chapter 3 §5 (Infinite sums in commutative groups) -/ noncomputable theory open finset filter function classical open_locale topological_space classical big_operators nnreal variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section has_sum variables [add_comm_monoid α] [topological_space α] /-- Infinite sum on a topological monoid The `at_top` filter on `finset β` is the limit of all finite sets towards the entire type. So we sum up bigger and bigger sets. This sum operation is invariant under reordering. In particular, the function `ℕ → ℝ` sending `n` to `(-1)^n / (n+1)` does not have a sum for this definition, but a series which is absolutely convergent will have the correct sum. This is based on Mario Carneiro's [infinite sum `df-tsms` in Metamath](http://us.metamath.org/mpeuni/df-tsms.html). For the definition or many statements, `α` does not need to be a topological monoid. We only add this assumption later, for the lemmas where it is relevant. -/ def has_sum (f : β → α) (a : α) : Prop := tendsto (λs:finset β, ∑ b in s, f b) at_top (𝓝 a) /-- `summable f` means that `f` has some (infinite) sum. Use `tsum` to get the value. -/ def summable (f : β → α) : Prop := ∃a, has_sum f a /-- `∑' i, f i` is the sum of `f` it exists, or 0 otherwise -/ @[irreducible] def tsum {β} (f : β → α) := if h : summable f then classical.some h else 0 -- see Note [operator precedence of big operators] notation `∑'` binders `, ` r:(scoped:67 f, tsum f) := r variables {f g : β → α} {a b : α} {s : finset β} lemma summable.has_sum (ha : summable f) : has_sum f (∑'b, f b) := by simp [ha, tsum]; exact some_spec ha lemma has_sum.summable (h : has_sum f a) : summable f := ⟨a, h⟩ /-- Constant zero function has sum `0` -/ lemma has_sum_zero : has_sum (λb, 0 : β → α) 0 := by simp [has_sum, tendsto_const_nhds] lemma summable_zero : summable (λb, 0 : β → α) := has_sum_zero.summable lemma tsum_eq_zero_of_not_summable (h : ¬ summable f) : ∑'b, f b = 0 := by simp [tsum, h] lemma has_sum.has_sum_of_sum_eq {g : γ → α} (h_eq : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∑ x in u', g x = ∑ b in v', f b) (hf : has_sum g a) : has_sum f a := le_trans (map_at_top_finset_sum_le_of_sum_eq h_eq) hf lemma has_sum_iff_has_sum {g : γ → α} (h₁ : ∀u:finset γ, ∃v:finset β, ∀v', v ⊆ v' → ∃u', u ⊆ u' ∧ ∑ x in u', g x = ∑ b in v', f b) (h₂ : ∀v:finset β, ∃u:finset γ, ∀u', u ⊆ u' → ∃v', v ⊆ v' ∧ ∑ b in v', f b = ∑ x in u', g x) : has_sum f a ↔ has_sum g a := ⟨has_sum.has_sum_of_sum_eq h₂, has_sum.has_sum_of_sum_eq h₁⟩ lemma function.injective.has_sum_iff {g : γ → β} (hg : injective g) (hf : ∀ x ∉ set.range g, f x = 0) : has_sum (f ∘ g) a ↔ has_sum f a := by simp only [has_sum, tendsto, hg.map_at_top_finset_sum_eq hf] lemma function.injective.summable_iff {g : γ → β} (hg : injective g) (hf : ∀ x ∉ set.range g, f x = 0) : summable (f ∘ g) ↔ summable f := exists_congr $ λ _, hg.has_sum_iff hf lemma has_sum_subtype_iff_of_support_subset {s : set β} (hf : support f ⊆ s) : has_sum (f ∘ coe : s → α) a ↔ has_sum f a := subtype.coe_injective.has_sum_iff $ by simpa using support_subset_iff'.1 hf lemma has_sum_subtype_iff_indicator {s : set β} : has_sum (f ∘ coe : s → α) a ↔ has_sum (s.indicator f) a := by rw [← set.indicator_range_comp, subtype.range_coe, has_sum_subtype_iff_of_support_subset set.support_indicator_subset] @[simp] lemma has_sum_subtype_support : has_sum (f ∘ coe : support f → α) a ↔ has_sum f a := has_sum_subtype_iff_of_support_subset $ set.subset.refl _ lemma has_sum_fintype [fintype β] (f : β → α) : has_sum f (∑ b, f b) := order_top.tendsto_at_top_nhds _ protected lemma finset.has_sum (s : finset β) (f : β → α) : has_sum (f ∘ coe : (↑s : set β) → α) (∑ b in s, f b) := by { rw ← sum_attach, exact has_sum_fintype _ } protected lemma finset.summable (s : finset β) (f : β → α) : summable (f ∘ coe : (↑s : set β) → α) := (s.has_sum f).summable protected lemma set.finite.summable {s : set β} (hs : s.finite) (f : β → α) : summable (f ∘ coe : s → α) := by convert hs.to_finset.summable f; simp only [hs.coe_to_finset] /-- If a function `f` vanishes outside of a finite set `s`, then it `has_sum` `∑ b in s, f b`. -/ lemma has_sum_sum_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : has_sum f (∑ b in s, f b) := (has_sum_subtype_iff_of_support_subset $ support_subset_iff'.2 hf).1 $ s.has_sum f lemma summable_of_ne_finset_zero (hf : ∀b∉s, f b = 0) : summable f := (has_sum_sum_of_ne_finset_zero hf).summable lemma has_sum_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) : has_sum f (f b) := suffices has_sum f (∑ b' in {b}, f b'), by simpa using this, has_sum_sum_of_ne_finset_zero $ by simpa [hf] lemma has_sum_ite_eq (b : β) [decidable_pred (= b)] (a : α) : has_sum (λb', if b' = b then a else 0) a := begin convert has_sum_single b _, { exact (if_pos rfl).symm }, assume b' hb', exact if_neg hb' end lemma equiv.has_sum_iff (e : γ ≃ β) : has_sum (f ∘ e) a ↔ has_sum f a := e.injective.has_sum_iff $ by simp lemma equiv.summable_iff (e : γ ≃ β) : summable (f ∘ e) ↔ summable f := exists_congr $ λ a, e.has_sum_iff lemma summable.prod_symm {f : β × γ → α} (hf : summable f) : summable (λ p : γ × β, f p.swap) := (equiv.prod_comm γ β).summable_iff.2 hf lemma equiv.has_sum_iff_of_support {g : γ → α} (e : support f ≃ support g) (he : ∀ x : support f, g (e x) = f x) : has_sum f a ↔ has_sum g a := have (g ∘ coe) ∘ e = f ∘ coe, from funext he, by rw [← has_sum_subtype_support, ← this, e.has_sum_iff, has_sum_subtype_support] lemma has_sum_iff_has_sum_of_ne_zero_bij {g : γ → α} (i : support g → β) (hi : ∀ ⦃x y⦄, i x = i y → (x : γ) = y) (hf : support f ⊆ set.range i) (hfg : ∀ x, f (i x) = g x) : has_sum f a ↔ has_sum g a := iff.symm $ equiv.has_sum_iff_of_support (equiv.of_bijective (λ x, ⟨i x, λ hx, x.coe_prop $ hfg x ▸ hx⟩) ⟨λ x y h, subtype.ext $ hi $ subtype.ext_iff.1 h, λ y, (hf y.coe_prop).imp $ λ x hx, subtype.ext hx⟩) hfg lemma equiv.summable_iff_of_support {g : γ → α} (e : support f ≃ support g) (he : ∀ x : support f, g (e x) = f x) : summable f ↔ summable g := exists_congr $ λ _, e.has_sum_iff_of_support he protected lemma has_sum.map [add_comm_monoid γ] [topological_space γ] (hf : has_sum f a) (g : α →+ γ) (hg : continuous g) : has_sum (g ∘ f) (g a) := have g ∘ (λs:finset β, ∑ b in s, f b) = (λs:finset β, ∑ b in s, g (f b)), from funext $ g.map_sum _, show tendsto (λs:finset β, ∑ b in s, g (f b)) at_top (𝓝 (g a)), from this ▸ (hg.tendsto a).comp hf protected lemma summable.map [add_comm_monoid γ] [topological_space γ] (hf : summable f) (g : α →+ γ) (hg : continuous g) : summable (g ∘ f) := (hf.has_sum.map g hg).summable /-- If `f : ℕ → α` has sum `a`, then the partial sums `∑_{i=0}^{n-1} f i` converge to `a`. -/ lemma has_sum.tendsto_sum_nat {f : ℕ → α} (h : has_sum f a) : tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) := h.comp tendsto_finset_range lemma has_sum.unique {a₁ a₂ : α} [t2_space α] : has_sum f a₁ → has_sum f a₂ → a₁ = a₂ := tendsto_nhds_unique lemma summable.has_sum_iff_tendsto_nat [t2_space α] {f : ℕ → α} {a : α} (hf : summable f) : has_sum f a ↔ tendsto (λn:ℕ, ∑ i in range n, f i) at_top (𝓝 a) := begin refine ⟨λ h, h.tendsto_sum_nat, λ h, _⟩, rw tendsto_nhds_unique h hf.has_sum.tendsto_sum_nat, exact hf.has_sum end lemma equiv.summable_iff_of_has_sum_iff {α' : Type*} [add_comm_monoid α'] [topological_space α'] (e : α' ≃ α) {f : β → α} {g : γ → α'} (he : ∀ {a}, has_sum f (e a) ↔ has_sum g a) : summable f ↔ summable g := ⟨λ ⟨a, ha⟩, ⟨e.symm a, he.1 $ by rwa [e.apply_symm_apply]⟩, λ ⟨a, ha⟩, ⟨e a, he.2 ha⟩⟩ variable [has_continuous_add α] lemma has_sum.add (hf : has_sum f a) (hg : has_sum g b) : has_sum (λb, f b + g b) (a + b) := by simp only [has_sum, sum_add_distrib]; exact hf.add hg lemma summable.add (hf : summable f) (hg : summable g) : summable (λb, f b + g b) := (hf.has_sum.add hg.has_sum).summable lemma has_sum_sum {f : γ → β → α} {a : γ → α} {s : finset γ} : (∀i∈s, has_sum (f i) (a i)) → has_sum (λb, ∑ i in s, f i b) (∑ i in s, a i) := finset.induction_on s (by simp only [has_sum_zero, sum_empty, forall_true_iff]) (by simp only [has_sum.add, sum_insert, mem_insert, forall_eq_or_imp, forall_2_true_iff, not_false_iff, forall_true_iff] {contextual := tt}) lemma summable_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, summable (f i)) : summable (λb, ∑ i in s, f i b) := (has_sum_sum $ assume i hi, (hf i hi).has_sum).summable lemma has_sum.add_compl {s : set β} (ha : has_sum (f ∘ coe : s → α) a) (hb : has_sum (f ∘ coe : sᶜ → α) b) : has_sum f (a + b) := by simpa using (has_sum_subtype_iff_indicator.1 ha).add (has_sum_subtype_iff_indicator.1 hb) lemma summable.add_compl {s : set β} (hs : summable (f ∘ coe : s → α)) (hsc : summable (f ∘ coe : sᶜ → α)) : summable f := (hs.has_sum.add_compl hsc.has_sum).summable lemma has_sum.compl_add {s : set β} (ha : has_sum (f ∘ coe : sᶜ → α) a) (hb : has_sum (f ∘ coe : s → α) b) : has_sum f (a + b) := by simpa using (has_sum_subtype_iff_indicator.1 ha).add (has_sum_subtype_iff_indicator.1 hb) lemma summable.compl_add {s : set β} (hs : summable (f ∘ coe : sᶜ → α)) (hsc : summable (f ∘ coe : s → α)) : summable f := (hs.has_sum.compl_add hsc.has_sum).summable lemma has_sum.sigma [regular_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α} (ha : has_sum f a) (hf : ∀b, has_sum (λc, f ⟨b, c⟩) (g b)) : has_sum g a := begin refine (at_top_basis.tendsto_iff (closed_nhds_basis a)).mpr _, rintros s ⟨hs, hsc⟩, rcases mem_at_top_sets.mp (ha hs) with ⟨u, hu⟩, use [u.image sigma.fst, trivial], intros bs hbs, simp only [set.mem_preimage, ge_iff_le, finset.le_iff_subset] at hu, have : tendsto (λ t : finset (Σ b, γ b), ∑ p in t.filter (λ p, p.1 ∈ bs), f p) at_top (𝓝 $ ∑ b in bs, g b), { simp only [← sigma_preimage_mk, sum_sigma], refine tendsto_finset_sum _ (λ b hb, _), change tendsto (λ t, (λ t, ∑ s in t, f ⟨b, s⟩) (preimage t (sigma.mk b) _)) at_top (𝓝 (g b)), exact tendsto.comp (hf b) (tendsto_finset_preimage_at_top_at_top _) }, refine hsc.mem_of_tendsto this (eventually_at_top.2 ⟨u, λ t ht, hu _ (λ x hx, _)⟩), exact mem_filter.2 ⟨ht hx, hbs $ mem_image_of_mem _ hx⟩ end /-- If a series `f` on `β × γ` has sum `a` and for each `b` the restriction of `f` to `{b} × γ` has sum `g b`, then the series `g` has sum `a`. -/ lemma has_sum.prod_fiberwise [regular_space α] {f : β × γ → α} {g : β → α} {a : α} (ha : has_sum f a) (hf : ∀b, has_sum (λc, f (b, c)) (g b)) : has_sum g a := has_sum.sigma ((equiv.sigma_equiv_prod β γ).has_sum_iff.2 ha) hf lemma summable.sigma' [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) (hf : ∀b, summable (λc, f ⟨b, c⟩)) : summable (λb, ∑'c, f ⟨b, c⟩) := (ha.has_sum.sigma (assume b, (hf b).has_sum)).summable lemma has_sum.sigma_of_has_sum [regular_space α] {γ : β → Type*} {f : (Σ b:β, γ b) → α} {g : β → α} {a : α} (ha : has_sum g a) (hf : ∀b, has_sum (λc, f ⟨b, c⟩) (g b)) (hf' : summable f) : has_sum f a := by simpa [(hf'.has_sum.sigma hf).unique ha] using hf'.has_sum end has_sum section tsum variables [add_comm_monoid α] [topological_space α] [t2_space α] variables {f g : β → α} {a a₁ a₂ : α} lemma has_sum.tsum_eq (ha : has_sum f a) : ∑'b, f b = a := (summable.has_sum ⟨a, ha⟩).unique ha lemma summable.has_sum_iff (h : summable f) : has_sum f a ↔ ∑'b, f b = a := iff.intro has_sum.tsum_eq (assume eq, eq ▸ h.has_sum) @[simp] lemma tsum_zero : ∑'b:β, (0:α) = 0 := has_sum_zero.tsum_eq lemma tsum_eq_sum {f : β → α} {s : finset β} (hf : ∀b∉s, f b = 0) : ∑' b, f b = ∑ b in s, f b := (has_sum_sum_of_ne_finset_zero hf).tsum_eq lemma tsum_congr {α β : Type*} [add_comm_monoid α] [topological_space α] {f g : β → α} (hfg : ∀ b, f b = g b) : ∑' b, f b = ∑' b, g b := congr_arg tsum (funext hfg) lemma tsum_fintype [fintype β] (f : β → α) : ∑'b, f b = ∑ b, f b := (has_sum_fintype f).tsum_eq @[simp] lemma finset.tsum_subtype (s : finset β) (f : β → α) : ∑' x : {x // x ∈ s}, f x = ∑ x in s, f x := (s.has_sum f).tsum_eq @[simp] lemma finset.tsum_subtype' (s : finset β) (f : β → α) : ∑' x : (s : set β), f x = ∑ x in s, f x := s.tsum_subtype f lemma tsum_eq_single {f : β → α} (b : β) (hf : ∀b' ≠ b, f b' = 0) : ∑'b, f b = f b := (has_sum_single b hf).tsum_eq @[simp] lemma tsum_ite_eq (b : β) [decidable_pred (= b)] (a : α) : ∑' b', (if b' = b then a else 0) = a := (has_sum_ite_eq b a).tsum_eq lemma tsum_dite_right (P : Prop) [decidable P] (x : β → ¬ P → α) : ∑' (b : β), (if h : P then (0 : α) else x b h) = if h : P then (0 : α) else ∑' (b : β), x b h := by by_cases hP : P; simp [hP] lemma tsum_dite_left (P : Prop) [decidable P] (x : β → P → α) : ∑' (b : β), (if h : P then x b h else 0) = if h : P then (∑' (b : β), x b h) else 0 := by by_cases hP : P; simp [hP] lemma equiv.tsum_eq_tsum_of_has_sum_iff_has_sum {α' : Type*} [add_comm_monoid α'] [topological_space α'] (e : α' ≃ α) (h0 : e 0 = 0) {f : β → α} {g : γ → α'} (h : ∀ {a}, has_sum f (e a) ↔ has_sum g a) : ∑' b, f b = e (∑' c, g c) := by_cases (assume : summable g, (h.mpr this.has_sum).tsum_eq) (assume hg : ¬ summable g, have hf : ¬ summable f, from mt (e.summable_iff_of_has_sum_iff @h).1 hg, by simp [tsum, hf, hg, h0]) lemma tsum_eq_tsum_of_has_sum_iff_has_sum {f : β → α} {g : γ → α} (h : ∀{a}, has_sum f a ↔ has_sum g a) : ∑'b, f b = ∑'c, g c := (equiv.refl α).tsum_eq_tsum_of_has_sum_iff_has_sum rfl @h lemma equiv.tsum_eq (j : γ ≃ β) (f : β → α) : ∑'c, f (j c) = ∑'b, f b := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ a, j.has_sum_iff lemma equiv.tsum_eq_tsum_of_support {f : β → α} {g : γ → α} (e : support f ≃ support g) (he : ∀ x, g (e x) = f x) : (∑' x, f x) = ∑' y, g y := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, e.has_sum_iff_of_support he lemma tsum_eq_tsum_of_ne_zero_bij {g : γ → α} (i : support g → β) (hi : ∀ ⦃x y⦄, i x = i y → (x : γ) = y) (hf : support f ⊆ set.range i) (hfg : ∀ x, f (i x) = g x) : ∑' x, f x = ∑' y, g y := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, has_sum_iff_has_sum_of_ne_zero_bij i hi hf hfg lemma tsum_subtype (s : set β) (f : β → α) : ∑' x:s, f x = ∑' x, s.indicator f x := tsum_eq_tsum_of_has_sum_iff_has_sum $ λ _, has_sum_subtype_iff_indicator section has_continuous_add variable [has_continuous_add α] lemma tsum_add (hf : summable f) (hg : summable g) : ∑'b, (f b + g b) = (∑'b, f b) + (∑'b, g b) := (hf.has_sum.add hg.has_sum).tsum_eq lemma tsum_sum {f : γ → β → α} {s : finset γ} (hf : ∀i∈s, summable (f i)) : ∑'b, ∑ i in s, f i b = ∑ i in s, ∑'b, f i b := (has_sum_sum $ assume i hi, (hf i hi).has_sum).tsum_eq lemma tsum_sigma' [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (h₁ : ∀b, summable (λc, f ⟨b, c⟩)) (h₂ : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := (h₂.has_sum.sigma (assume b, (h₁ b).has_sum)).tsum_eq.symm lemma tsum_prod' [regular_space α] {f : β × γ → α} (h : summable f) (h₁ : ∀b, summable (λc, f (b, c))) : ∑'p, f p = ∑'b c, f (b, c) := (h.has_sum.prod_fiberwise (assume b, (h₁ b).has_sum)).tsum_eq.symm lemma tsum_comm' [regular_space α] {f : β → γ → α} (h : summable (function.uncurry f)) (h₁ : ∀b, summable (f b)) (h₂ : ∀ c, summable (λ b, f b c)) : ∑' c b, f b c = ∑' b c, f b c := begin erw [← tsum_prod' h h₁, ← tsum_prod' h.prod_symm h₂, ← (equiv.prod_comm β γ).tsum_eq], refl, assumption end end has_continuous_add section encodable open encodable variable [encodable γ] /-- You can compute a sum over an encodably type by summing over the natural numbers and taking a supremum. This is useful for outer measures. -/ theorem tsum_supr_decode2 [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (s : γ → β) : ∑' i : ℕ, m (⨆ b ∈ decode2 γ i, s b) = ∑' b : γ, m (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, { refine (h $ by simp [m0]).elim }, { exact rfl } }, symmetry, refine tsum_eq_tsum_of_ne_zero_bij (λ a, option.get (H a.1 a.2)) _ _ _, { rintros ⟨m, hm⟩ ⟨n, 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, _⟩, _⟩, { simp only [mem_support, encodek2] at h ⊢, convert h, simp [set.ext_iff, encodek2] }, { exact option.get_of_mem _ (encodek2 _) } }, { rintros ⟨n, h⟩, dsimp only [subtype.coe_mk], transitivity, swap, rw [show decode2 γ n = _, from option.get_mem (H n h)], congr, simp [ext_iff, -option.some_get] } end /-- `tsum_supr_decode2` specialized to the complete lattice of sets. -/ theorem tsum_Union_decode2 (m : set β → α) (m0 : m ∅ = 0) (s : γ → set β) : ∑' i, m (⋃ b ∈ decode2 γ i, s b) = ∑' b, m (s b) := tsum_supr_decode2 m m0 s /-! Some properties about measure-like functions. These could also be functions defined on complete sublattices of sets, with the property that they are countably sub-additive. `R` will probably be instantiated with `(≤)` in all applications. -/ /-- If a function is countably sub-additive then it is sub-additive on encodable types -/ theorem rel_supr_tsum [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) ∑' i, m (s i)) (s : γ → β) : R (m (⨆ b : γ, s b)) ∑' b : γ, m (s b) := by { rw [← supr_decode2, ← tsum_supr_decode2 _ m0 s], exact m_supr _ } /-- If a function is countably sub-additive then it is sub-additive on finite sets -/ theorem rel_supr_sum [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) (∑' i, m (s i))) (s : δ → β) (t : finset δ) : R (m (⨆ d ∈ t, s d)) (∑ d in t, m (s d)) := by { cases t.nonempty_encodable, rw [supr_subtype'], convert rel_supr_tsum m m0 R m_supr _, rw [← finset.tsum_subtype], assumption } /-- If a function is countably sub-additive then it is binary sub-additive -/ theorem rel_sup_add [complete_lattice β] (m : β → α) (m0 : m ⊥ = 0) (R : α → α → Prop) (m_supr : ∀(s : ℕ → β), R (m (⨆ i, s i)) (∑' i, m (s i))) (s₁ s₂ : β) : R (m (s₁ ⊔ s₂)) (m s₁ + m s₂) := begin convert rel_supr_tsum m m0 R m_supr (λ b, cond b s₁ s₂), { simp only [supr_bool_eq, cond] }, { rw [tsum_fintype, fintype.sum_bool, cond, cond] } end end encodable end tsum section pi variables {ι : Type*} {π : α → Type*} [∀ x, add_comm_monoid (π x)] [∀ x, topological_space (π x)] lemma pi.has_sum {f : ι → ∀ x, π x} {g : ∀ x, π x} : has_sum f g ↔ ∀ x, has_sum (λ i, f i x) (g x) := by simp [has_sum, tendsto_pi] lemma pi.summable {f : ι → ∀ x, π x} : summable f ↔ ∀ x, summable (λ i, f i x) := by simp [summable, pi.has_sum, classical.skolem] lemma tsum_apply [∀ x, t2_space (π x)] {f : ι → ∀ x, π x}{x : α} (hf : summable f) : (∑' i, f i) x = ∑' i, f i x := (pi.has_sum.mp hf.has_sum x).tsum_eq.symm end pi section topological_group variables [add_comm_group α] [topological_space α] [topological_add_group α] variables {f g : β → α} {a a₁ a₂ : α} -- `by simpa using` speeds up elaboration. Why? lemma has_sum.neg (h : has_sum f a) : has_sum (λb, - f b) (- a) := by simpa only using h.map (-add_monoid_hom.id α) continuous_neg lemma summable.neg (hf : summable f) : summable (λb, - f b) := hf.has_sum.neg.summable lemma summable.of_neg (hf : summable (λb, - f b)) : summable f := by simpa only [neg_neg] using hf.neg lemma summable_neg_iff : summable (λ b, - f b) ↔ summable f := ⟨summable.of_neg, summable.neg⟩ lemma has_sum.sub (hf : has_sum f a₁) (hg : has_sum g a₂) : has_sum (λb, f b - g b) (a₁ - a₂) := by { simp [sub_eq_add_neg], exact hf.add hg.neg } lemma summable.sub (hf : summable f) (hg : summable g) : summable (λb, f b - g b) := (hf.has_sum.sub hg.has_sum).summable lemma has_sum.update (hf : has_sum f a₁) (b : β) [decidable_eq β] (a : α) : has_sum (update f b a) (a - f b + a₁) := begin convert ((has_sum_ite_eq b _).add hf), ext b', by_cases h : b' = b, { rw h, simp, }, { simp [h] }, end lemma summable.update (hf : summable f) (b : β) [decidable_eq β] (a : α) : summable (update f b a) := (hf.has_sum.update b a).summable lemma has_sum.has_sum_compl_iff {s : set β} (hf : has_sum (f ∘ coe : s → α) a₁) : has_sum (f ∘ coe : sᶜ → α) a₂ ↔ has_sum f (a₁ + a₂) := begin refine ⟨λ h, hf.add_compl h, λ h, _⟩, rw [has_sum_subtype_iff_indicator] at hf ⊢, rw [set.indicator_compl], simpa only [add_sub_cancel'] using h.sub hf end lemma has_sum.has_sum_iff_compl {s : set β} (hf : has_sum (f ∘ coe : s → α) a₁) : has_sum f a₂ ↔ has_sum (f ∘ coe : sᶜ → α) (a₂ - a₁) := iff.symm $ hf.has_sum_compl_iff.trans $ by rw [add_sub_cancel'_right] lemma summable.summable_compl_iff {s : set β} (hf : summable (f ∘ coe : s → α)) : summable (f ∘ coe : sᶜ → α) ↔ summable f := ⟨λ ⟨a, ha⟩, (hf.has_sum.has_sum_compl_iff.1 ha).summable, λ ⟨a, ha⟩, (hf.has_sum.has_sum_iff_compl.1 ha).summable⟩ protected lemma finset.has_sum_compl_iff (s : finset β) : has_sum (λ x : {x // x ∉ s}, f x) a ↔ has_sum f (a + ∑ i in s, f i) := (s.has_sum f).has_sum_compl_iff.trans $ by rw [add_comm] protected lemma finset.has_sum_iff_compl (s : finset β) : has_sum f a ↔ has_sum (λ x : {x // x ∉ s}, f x) (a - ∑ i in s, f i) := (s.has_sum f).has_sum_iff_compl protected lemma finset.summable_compl_iff (s : finset β) : summable (λ x : {x // x ∉ s}, f x) ↔ summable f := (s.summable f).summable_compl_iff lemma set.finite.summable_compl_iff {s : set β} (hs : s.finite) : summable (f ∘ coe : sᶜ → α) ↔ summable f := (hs.summable f).summable_compl_iff section tsum variables [t2_space α] lemma tsum_neg (hf : summable f) : ∑'b, - f b = - ∑'b, f b := hf.has_sum.neg.tsum_eq lemma tsum_sub (hf : summable f) (hg : summable g) : ∑'b, (f b - g b) = ∑'b, f b - ∑'b, g b := (hf.has_sum.sub hg.has_sum).tsum_eq lemma tsum_add_tsum_compl {s : set β} (hs : summable (f ∘ coe : s → α)) (hsc : summable (f ∘ coe : sᶜ → α)) : (∑' x : s, f x) + (∑' x : sᶜ, f x) = ∑' x, f x := (hs.has_sum.add_compl hsc.has_sum).tsum_eq.symm lemma sum_add_tsum_compl {s : finset β} (hf : summable f) : (∑ x in s, f x) + (∑' x : (↑s : set β)ᶜ, f x) = ∑' x, f x := ((s.has_sum f).add_compl (s.summable_compl_iff.2 hf).has_sum).tsum_eq.symm end tsum /-! ### Sums on subtypes If `s` is a finset of `α`, we show that the summability of `f` in the whole space and on the subtype `univ - s` are equivalent, and relate their sums. For a function defined on `ℕ`, we deduce the formula `(∑ i in range k, f i) + (∑' i, f (i + k)) = (∑' i, f i)`, in `sum_add_tsum_nat_add`. -/ section subtype lemma has_sum_nat_add_iff {f : ℕ → α} (k : ℕ) {a : α} : has_sum (λ n, f (n + k)) a ↔ has_sum f (a + ∑ i in range k, f i) := begin refine iff.trans _ ((range k).has_sum_compl_iff), rw [← (not_mem_range_equiv k).symm.has_sum_iff], refl end lemma summable_nat_add_iff {f : ℕ → α} (k : ℕ) : summable (λ n, f (n + k)) ↔ summable f := iff.symm $ (equiv.add_right (∑ i in range k, f i)).summable_iff_of_has_sum_iff $ λ a, (has_sum_nat_add_iff k).symm lemma has_sum_nat_add_iff' {f : ℕ → α} (k : ℕ) {a : α} : has_sum (λ n, f (n + k)) (a - ∑ i in range k, f i) ↔ has_sum f a := by simp [has_sum_nat_add_iff] lemma sum_add_tsum_nat_add [t2_space α] {f : ℕ → α} (k : ℕ) (h : summable f) : (∑ i in range k, f i) + (∑' i, f (i + k)) = ∑' i, f i := by simpa [add_comm] using ((has_sum_nat_add_iff k).1 ((summable_nat_add_iff k).2 h).has_sum).unique h.has_sum lemma tsum_eq_zero_add [t2_space α] {f : ℕ → α} (hf : summable f) : ∑'b, f b = f 0 + ∑'b, f (b + 1) := by simpa only [sum_range_one] using (sum_add_tsum_nat_add 1 hf).symm /-- For `f : ℕ → α`, then `∑' k, f (k + i)` tends to zero. This does not require a summability assumption on `f`, as otherwise all sums are zero. -/ lemma tendsto_sum_nat_add [t2_space α] (f : ℕ → α) : tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) := begin by_cases hf : summable f, { have h₀ : (λ i, (∑' i, f i) - ∑ j in range i, f j) = λ i, ∑' (k : ℕ), f (k + i), { ext1 i, rw [sub_eq_iff_eq_add, add_comm, sum_add_tsum_nat_add i hf] }, have h₁ : tendsto (λ i : ℕ, ∑' i, f i) at_top (𝓝 (∑' i, f i)) := tendsto_const_nhds, simpa only [h₀, sub_self] using tendsto.sub h₁ hf.has_sum.tendsto_sum_nat }, { convert tendsto_const_nhds, ext1 i, rw ← summable_nat_add_iff i at hf, { exact tsum_eq_zero_of_not_summable hf }, { apply_instance } } end end subtype end topological_group section topological_semiring variables [semiring α] [topological_space α] [topological_semiring α] variables {f g : β → α} {a a₁ a₂ : α} lemma has_sum.mul_left (a₂) (h : has_sum f a₁) : has_sum (λb, a₂ * f b) (a₂ * a₁) := by simpa only using h.map (add_monoid_hom.mul_left a₂) (continuous_const.mul continuous_id) lemma has_sum.mul_right (a₂) (hf : has_sum f a₁) : has_sum (λb, f b * a₂) (a₁ * a₂) := by simpa only using hf.map (add_monoid_hom.mul_right a₂) (continuous_id.mul continuous_const) lemma summable.mul_left (a) (hf : summable f) : summable (λb, a * f b) := (hf.has_sum.mul_left _).summable lemma summable.mul_right (a) (hf : summable f) : summable (λb, f b * a) := (hf.has_sum.mul_right _).summable section tsum variables [t2_space α] lemma summable.tsum_mul_left (a) (hf : summable f) : ∑'b, a * f b = a * ∑'b, f b := (hf.has_sum.mul_left _).tsum_eq lemma summable.tsum_mul_right (a) (hf : summable f) : (∑'b, f b * a) = (∑'b, f b) * a := (hf.has_sum.mul_right _).tsum_eq end tsum end topological_semiring section topological_semimodule variables {R : Type*} [semiring R] [topological_space R] [topological_space α] [add_comm_monoid α] [semimodule R α] [topological_semimodule R α] {f : β → α} lemma has_sum.smul {a : α} {r : R} (hf : has_sum f a) : has_sum (λ z, r • f z) (r • a) := hf.map (const_smul_hom α r) (continuous_const.smul continuous_id) lemma summable.smul {r : R} (hf : summable f) : summable (λ z, r • f z) := hf.has_sum.smul.summable lemma tsum_smul [t2_space α] {r : R} (hf : summable f) : ∑' z, r • f z = r • ∑' z, f z := hf.has_sum.smul.tsum_eq end topological_semimodule section division_ring variables [division_ring α] [topological_space α] [topological_semiring α] {f g : β → α} {a a₁ a₂ : α} lemma has_sum.div_const (h : has_sum f a) (b : α) : has_sum (λ x, f x / b) (a / b) := by simp only [div_eq_mul_inv, h.mul_right b⁻¹] lemma has_sum_mul_left_iff (h : a₂ ≠ 0) : has_sum f a₁ ↔ has_sum (λb, a₂ * f b) (a₂ * a₁) := ⟨has_sum.mul_left _, λ H, by simpa only [inv_mul_cancel_left' h] using H.mul_left a₂⁻¹⟩ lemma has_sum_mul_right_iff (h : a₂ ≠ 0) : has_sum f a₁ ↔ has_sum (λb, f b * a₂) (a₁ * a₂) := ⟨has_sum.mul_right _, λ H, by simpa only [mul_inv_cancel_right' h] using H.mul_right a₂⁻¹⟩ lemma summable_mul_left_iff (h : a ≠ 0) : summable f ↔ summable (λb, a * f b) := ⟨λ H, H.mul_left _, λ H, by simpa only [inv_mul_cancel_left' h] using H.mul_left a⁻¹⟩ lemma summable_mul_right_iff (h : a ≠ 0) : summable f ↔ summable (λb, f b * a) := ⟨λ H, H.mul_right _, λ H, by simpa only [mul_inv_cancel_right' h] using H.mul_right a⁻¹⟩ lemma tsum_mul_left [t2_space α] : (∑' x, a * f x) = a * ∑' x, f x := if hf : summable f then hf.tsum_mul_left a else if ha : a = 0 then by simp [ha] else by rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt (summable_mul_left_iff ha).2 hf), mul_zero] lemma tsum_mul_right [t2_space α] : (∑' x, f x * a) = (∑' x, f x) * a := if hf : summable f then hf.tsum_mul_right a else if ha : a = 0 then by simp [ha] else by rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable (mt (summable_mul_right_iff ha).2 hf), zero_mul] end division_ring section order_topology variables [ordered_add_comm_monoid α] [topological_space α] [order_closed_topology α] variables {f g : β → α} {a a₁ a₂ : α} lemma has_sum_le (h : ∀b, f b ≤ g b) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ ≤ a₂ := le_of_tendsto_of_tendsto' hf hg $ assume s, sum_le_sum $ assume b _, h b @[mono] lemma has_sum_mono (hf : has_sum f a₁) (hg : has_sum g a₂) (h : f ≤ g) : a₁ ≤ a₂ := has_sum_le h hf hg lemma has_sum_le_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c) (h : ∀b, f b ≤ g (i b)) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ ≤ a₂ := have has_sum (λc, (partial_inv i c).cases_on' 0 f) a₁, begin refine (has_sum_iff_has_sum_of_ne_zero_bij (i ∘ coe) _ _ _).2 hf, { exact assume c₁ c₂ eq, hi eq }, { intros c hc, rw [mem_support] at hc, cases eq : partial_inv i c with b; rw eq at hc, { contradiction }, { rw [partial_inv_of_injective hi] at eq, exact ⟨⟨b, hc⟩, eq⟩ } }, { assume c, simp [partial_inv_left hi, option.cases_on'] } end, begin refine has_sum_le (assume c, _) this hg, by_cases c ∈ set.range i, { rcases h with ⟨b, rfl⟩, rw [partial_inv_left hi, option.cases_on'], exact h _ }, { have : partial_inv i c = none := dif_neg h, rw [this, option.cases_on'], exact hs _ h } end lemma tsum_le_tsum_of_inj {g : γ → α} (i : β → γ) (hi : injective i) (hs : ∀c∉set.range i, 0 ≤ g c) (h : ∀b, f b ≤ g (i b)) (hf : summable f) (hg : summable g) : tsum f ≤ tsum g := has_sum_le_inj i hi hs h hf.has_sum hg.has_sum lemma sum_le_has_sum {f : β → α} (s : finset β) (hs : ∀ b∉s, 0 ≤ f b) (hf : has_sum f a) : ∑ b in s, f b ≤ a := ge_of_tendsto hf (eventually_at_top.2 ⟨s, λ t hst, sum_le_sum_of_subset_of_nonneg hst $ λ b hbt hbs, hs b hbs⟩) lemma le_has_sum (hf : has_sum f a) (b : β) (hb : ∀ b' ≠ b, 0 ≤ f b') : f b ≤ a := calc f b = ∑ b in {b}, f b : finset.sum_singleton.symm ... ≤ a : sum_le_has_sum _ (by { convert hb, simp }) hf lemma sum_le_tsum {f : β → α} (s : finset β) (hs : ∀ b∉s, 0 ≤ f b) (hf : summable f) : ∑ b in s, f b ≤ tsum f := sum_le_has_sum s hs hf.has_sum lemma le_tsum (hf : summable f) (b : β) (hb : ∀ b' ≠ b, 0 ≤ f b') : f b ≤ ∑' b, f b := le_has_sum (summable.has_sum hf) b hb lemma tsum_le_tsum (h : ∀b, f b ≤ g b) (hf : summable f) (hg : summable g) : ∑'b, f b ≤ ∑'b, g b := has_sum_le h hf.has_sum hg.has_sum @[mono] lemma tsum_mono (hf : summable f) (hg : summable g) (h : f ≤ g) : ∑' n, f n ≤ ∑' n, g n := tsum_le_tsum h hf hg lemma has_sum.nonneg (h : ∀ b, 0 ≤ g b) (ha : has_sum g a) : 0 ≤ a := has_sum_le h has_sum_zero ha lemma has_sum.nonpos (h : ∀ b, g b ≤ 0) (ha : has_sum g a) : a ≤ 0 := has_sum_le h ha has_sum_zero lemma tsum_nonneg (h : ∀ b, 0 ≤ g b) : 0 ≤ ∑'b, g b := begin by_cases hg : summable g, { exact hg.has_sum.nonneg h }, { simp [tsum_eq_zero_of_not_summable hg] } end lemma tsum_nonpos (h : ∀ b, f b ≤ 0) : ∑'b, f b ≤ 0 := begin by_cases hf : summable f, { exact hf.has_sum.nonpos h }, { simp [tsum_eq_zero_of_not_summable hf] } end end order_topology section ordered_topological_group variables [ordered_add_comm_group α] [topological_space α] [topological_add_group α] [order_closed_topology α] {f g : β → α} {a₁ a₂ : α} lemma has_sum_lt {i : β} (h : ∀ (b : β), f b ≤ g b) (hi : f i < g i) (hf : has_sum f a₁) (hg : has_sum g a₂) : a₁ < a₂ := have update f i 0 ≤ update g i 0 := update_le_update_iff.mpr ⟨rfl.le, λ i _, h i⟩, have 0 - f i + a₁ ≤ 0 - g i + a₂ := has_sum_le this (hf.update i 0) (hg.update i 0), by simpa only [zero_sub, add_neg_cancel_left] using add_lt_add_of_lt_of_le hi this @[mono] lemma has_sum_strict_mono (hf : has_sum f a₁) (hg : has_sum g a₂) (h : f < g) : a₁ < a₂ := let ⟨hle, i, hi⟩ := pi.lt_def.mp h in has_sum_lt hle hi hf hg lemma tsum_lt_tsum {i : β} (h : ∀ (b : β), f b ≤ g b) (hi : f i < g i) (hf : summable f) (hg : summable g) : ∑' n, f n < ∑' n, g n := has_sum_lt h hi hf.has_sum hg.has_sum @[mono] lemma tsum_strict_mono (hf : summable f) (hg : summable g) (h : f < g) : ∑' n, f n < ∑' n, g n := let ⟨hle, i, hi⟩ := pi.lt_def.mp h in tsum_lt_tsum hle hi hf hg end ordered_topological_group section canonically_ordered variables [canonically_ordered_add_monoid α] [topological_space α] [order_closed_topology α] variables {f : β → α} {a : α} lemma le_has_sum' (hf : has_sum f a) (b : β) : f b ≤ a := le_has_sum hf b $ λ _ _, zero_le _ lemma le_tsum' (hf : summable f) (b : β) : f b ≤ ∑' b, f b := le_tsum hf b $ λ _ _, zero_le _ lemma has_sum_zero_iff : has_sum f 0 ↔ ∀ x, f x = 0 := begin refine ⟨_, λ h, _⟩, { contrapose!, exact λ ⟨x, hx⟩ h, irrefl _ (lt_of_lt_of_le (pos_iff_ne_zero.2 hx) (le_has_sum' h x)) }, { convert has_sum_zero, exact funext h } end lemma tsum_eq_zero_iff (hf : summable f) : ∑' i, f i = 0 ↔ ∀ x, f x = 0 := by rw [←has_sum_zero_iff, hf.has_sum_iff] lemma tsum_ne_zero_iff (hf : summable f) : ∑' i, f i ≠ 0 ↔ ∃ x, f x ≠ 0 := by rw [ne.def, tsum_eq_zero_iff hf, not_forall] end canonically_ordered section uniform_group variables [add_comm_group α] [uniform_space α] lemma summable_iff_cauchy_seq_finset [complete_space α] {f : β → α} : summable f ↔ cauchy_seq (λ (s : finset β), ∑ b in s, f b) := cauchy_map_iff_exists_tendsto.symm variables [uniform_add_group α] {f g : β → α} {a a₁ a₂ : α} lemma cauchy_seq_finset_iff_vanishing : cauchy_seq (λ (s : finset β), ∑ b in s, f b) ↔ ∀ e ∈ 𝓝 (0:α), (∃s:finset β, ∀t, disjoint t s → ∑ b in t, f b ∈ e) := begin simp only [cauchy_seq, cauchy_map_iff, and_iff_right at_top_ne_bot, prod_at_top_at_top_eq, uniformity_eq_comap_nhds_zero α, tendsto_comap_iff, (∘)], rw [tendsto_at_top'], split, { assume h e he, rcases h e he with ⟨⟨s₁, s₂⟩, h⟩, use [s₁ ∪ s₂], assume t ht, specialize h (s₁ ∪ s₂, (s₁ ∪ s₂) ∪ t) ⟨le_sup_left, le_sup_left_of_le le_sup_right⟩, simpa only [finset.sum_union ht.symm, add_sub_cancel'] using h }, { assume h e he, rcases exists_nhds_half_neg he with ⟨d, hd, hde⟩, rcases h d hd with ⟨s, h⟩, use [(s, s)], rintros ⟨t₁, t₂⟩ ⟨ht₁, ht₂⟩, have : ∑ b in t₂, f b - ∑ b in t₁, f b = ∑ b in t₂ \ s, f b - ∑ b in t₁ \ s, f b, { simp only [(finset.sum_sdiff ht₁).symm, (finset.sum_sdiff ht₂).symm, add_sub_add_right_eq_sub] }, simp only [this], exact hde _ (h _ finset.sdiff_disjoint) _ (h _ finset.sdiff_disjoint) } end variable [complete_space α] lemma summable_iff_vanishing : summable f ↔ ∀ e ∈ 𝓝 (0:α), (∃s:finset β, ∀t, disjoint t s → ∑ b in t, f b ∈ e) := by rw [summable_iff_cauchy_seq_finset, cauchy_seq_finset_iff_vanishing] /- TODO: generalize to monoid with a uniform continuous subtraction operator: `(a + b) - b = a` -/ lemma summable.summable_of_eq_zero_or_self (hf : summable f) (h : ∀b, g b = 0 ∨ g b = f b) : summable g := summable_iff_vanishing.2 $ assume e he, let ⟨s, hs⟩ := summable_iff_vanishing.1 hf e he in ⟨s, assume t ht, have eq : ∑ b in t.filter (λb, g b = f b), f b = ∑ b in t, g b := calc ∑ b in t.filter (λb, g b = f b), f b = ∑ b in t.filter (λb, g b = f b), g b : finset.sum_congr rfl (assume b hb, (finset.mem_filter.1 hb).2.symm) ... = ∑ b in t, g b : begin refine finset.sum_subset (finset.filter_subset _ _) _, assume b hbt hb, simp only [(∉), finset.mem_filter, and_iff_right hbt] at hb, exact (h b).resolve_right hb end, eq ▸ hs _ $ finset.disjoint_of_subset_left (finset.filter_subset _ _) ht⟩ protected lemma summable.indicator (hf : summable f) (s : set β) : summable (s.indicator f) := hf.summable_of_eq_zero_or_self $ set.indicator_eq_zero_or_self _ _ lemma summable.comp_injective {i : γ → β} (hf : summable f) (hi : injective i) : summable (f ∘ i) := begin simpa only [set.indicator_range_comp] using (hi.summable_iff _).2 (hf.indicator (set.range i)), exact λ x hx, set.indicator_of_not_mem hx _ end lemma summable.subtype (hf : summable f) (s : set β) : summable (f ∘ coe : s → α) := hf.comp_injective subtype.coe_injective lemma summable_subtype_and_compl {s : set β} : summable (λ x : s, f x) ∧ summable (λ x : sᶜ, f x) ↔ summable f := ⟨and_imp.2 summable.add_compl, λ h, ⟨h.subtype s, h.subtype sᶜ⟩⟩ lemma summable.sigma_factor {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) (b : β) : summable (λc, f ⟨b, c⟩) := ha.comp_injective sigma_mk_injective lemma summable.sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) : summable (λb, ∑'c, f ⟨b, c⟩) := ha.sigma' (λ b, ha.sigma_factor b) lemma summable.prod_factor {f : β × γ → α} (h : summable f) (b : β) : summable (λ c, f (b, c)) := h.comp_injective $ λ c₁ c₂ h, (prod.ext_iff.1 h).2 lemma tsum_sigma [regular_space α] {γ : β → Type*} {f : (Σb:β, γ b) → α} (ha : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := tsum_sigma' (λ b, ha.sigma_factor b) ha lemma tsum_prod [regular_space α] {f : β × γ → α} (h : summable f) : ∑'p, f p = ∑'b c, f ⟨b, c⟩ := tsum_prod' h h.prod_factor lemma tsum_comm [regular_space α] {f : β → γ → α} (h : summable (function.uncurry f)) : ∑' c b, f b c = ∑' b c, f b c := tsum_comm' h h.prod_factor h.prod_symm.prod_factor /-- Let `f : ℕ → ℝ` be a sequence with summable series and let `i ∈ ℕ` be an index. Lemma `tsum_ite_eq_extract` writes `Σ f n` as the sum of `f i` plus the series of the remaining terms. TODO: generalize this to `f : β → α` with appropriate typeclass assumptions -/ lemma tsum_ite_eq_extract {f : ℕ → ℝ} (hf : summable f) (i : ℕ) : ∑' n, f n = f i + ∑' n, ite (n = i) 0 (f n) := begin refine ((tsum_congr _).trans $ tsum_add (hf.summable_of_eq_zero_or_self _) $ hf.summable_of_eq_zero_or_self _).trans (add_right_cancel_iff.mpr (tsum_ite_eq i (f i))); exact λ j, by { by_cases ji : j = i; simp [ji] } end end uniform_group section topological_group variables {G : Type*} [topological_space G] [add_comm_group G] [topological_add_group G] {f : α → G} lemma summable.vanishing (hf : summable f) ⦃e : set G⦄ (he : e ∈ 𝓝 (0 : G)) : ∃ s : finset α, ∀ t, disjoint t s → ∑ k in t, f k ∈ e := begin letI : uniform_space G := topological_add_group.to_uniform_space G, letI : uniform_add_group G := topological_add_group_is_uniform, rcases hf with ⟨y, hy⟩, exact cauchy_seq_finset_iff_vanishing.1 hy.cauchy_seq e he end /-- Series divergence test: if `f` is a convergent series, then `f x` tends to zero along `cofinite`. -/ lemma summable.tendsto_cofinite_zero (hf : summable f) : tendsto f cofinite (𝓝 0) := begin intros e he, rw [filter.mem_map], rcases hf.vanishing he with ⟨s, hs⟩, refine s.eventually_cofinite_nmem.mono (λ x hx, _), by simpa using hs {x} (singleton_disjoint.2 hx) end lemma summable.tendsto_at_top_zero {f : ℕ → G} (hf : summable f) : tendsto f at_top (𝓝 0) := by { rw ←nat.cofinite_eq_at_top, exact hf.tendsto_cofinite_zero } end topological_group lemma summable_abs_iff [linear_ordered_add_comm_group β] [uniform_space β] [uniform_add_group β] [complete_space β] {f : α → β} : summable (λ x, abs (f x)) ↔ summable f := have h1 : ∀ x : {x | 0 ≤ f x}, abs (f x) = f x := λ x, abs_of_nonneg x.2, have h2 : ∀ x : {x | 0 ≤ f x}ᶜ, abs (f x) = -f x := λ x, abs_of_neg (not_le.1 x.2), calc summable (λ x, abs (f x)) ↔ summable (λ x : {x | 0 ≤ f x}, abs (f x)) ∧ summable (λ x : {x | 0 ≤ f x}ᶜ, abs (f x)) : summable_subtype_and_compl.symm ... ↔ summable (λ x : {x | 0 ≤ f x}, f x) ∧ summable (λ x : {x | 0 ≤ f x}ᶜ, -f x) : by simp only [h1, h2] ... ↔ _ : by simp only [summable_neg_iff, summable_subtype_and_compl] alias summable_abs_iff ↔ summable.of_abs summable.abs section cauchy_seq open finset.Ico filter /-- If the extended distance between consequent points of a sequence is estimated by a summable series of `nnreal`s, then the original sequence is a Cauchy sequence. -/ lemma cauchy_seq_of_edist_le_of_summable [emetric_space α] {f : ℕ → α} (d : ℕ → ℝ≥0) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : summable d) : cauchy_seq f := begin refine emetric.cauchy_seq_iff_nnreal.2 (λ ε εpos, _), -- Actually we need partial sums of `d` to be a Cauchy sequence replace hd : cauchy_seq (λ (n : ℕ), ∑ x in range n, d x) := let ⟨_, H⟩ := hd in H.tendsto_sum_nat.cauchy_seq, -- Now we take the same `N` as in one of the definitions of a Cauchy sequence refine (metric.cauchy_seq_iff'.1 hd ε (nnreal.coe_pos.2 εpos)).imp (λ N hN n hn, _), have hsum := hN n hn, -- We simplify the known inequality rw [dist_nndist, nnreal.nndist_eq, ← sum_range_add_sum_Ico _ hn, nnreal.add_sub_cancel'] at hsum, norm_cast at hsum, replace hsum := lt_of_le_of_lt (le_max_left _ _) hsum, rw edist_comm, -- Then use `hf` to simplify the goal to the same form apply lt_of_le_of_lt (edist_le_Ico_sum_of_edist_le hn (λ k _ _, hf k)), assumption_mod_cast end /-- If the distance between consequent points of a sequence is estimated by a summable series, then the original sequence is a Cauchy sequence. -/ lemma cauchy_seq_of_dist_le_of_summable [metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) : cauchy_seq f := begin refine metric.cauchy_seq_iff'.2 (λε εpos, _), replace hd : cauchy_seq (λ (n : ℕ), ∑ x in range n, d x) := let ⟨_, H⟩ := hd in H.tendsto_sum_nat.cauchy_seq, refine (metric.cauchy_seq_iff'.1 hd ε εpos).imp (λ N hN n hn, _), have hsum := hN n hn, rw [real.dist_eq, ← sum_Ico_eq_sub _ hn] at hsum, calc dist (f n) (f N) = dist (f N) (f n) : dist_comm _ _ ... ≤ ∑ x in Ico N n, d x : dist_le_Ico_sum_of_dist_le hn (λ k _ _, hf k) ... ≤ abs (∑ x in Ico N n, d x) : le_abs_self _ ... < ε : hsum end lemma cauchy_seq_of_summable_dist [metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) : cauchy_seq f := cauchy_seq_of_dist_le_of_summable _ (λ _, le_refl _) h lemma dist_le_tsum_of_dist_le_of_tendsto [metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : dist (f n) a ≤ ∑' m, d (n + m) := begin refine le_of_tendsto (tendsto_const_nhds.dist ha) (eventually_at_top.2 ⟨n, λ m hnm, _⟩), refine le_trans (dist_le_Ico_sum_of_dist_le hnm (λ k _ _, hf k)) _, rw [sum_Ico_eq_sum_range], refine sum_le_tsum (range _) (λ _ _, le_trans dist_nonneg (hf _)) _, exact hd.comp_injective (add_right_injective n) end lemma dist_le_tsum_of_dist_le_of_tendsto₀ [metric_space α] {f : ℕ → α} (d : ℕ → ℝ) (hf : ∀ n, dist (f n) (f n.succ) ≤ d n) (hd : summable d) {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ tsum d := by simpa only [zero_add] using dist_le_tsum_of_dist_le_of_tendsto d hf hd ha 0 lemma dist_le_tsum_dist_of_tendsto [metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) {a : α} (ha : tendsto f at_top (𝓝 a)) (n) : dist (f n) a ≤ ∑' m, dist (f (n+m)) (f (n+m).succ) := show dist (f n) a ≤ ∑' m, (λx, dist (f x) (f x.succ)) (n + m), from dist_le_tsum_of_dist_le_of_tendsto (λ n, dist (f n) (f n.succ)) (λ _, le_refl _) h ha n lemma dist_le_tsum_dist_of_tendsto₀ [metric_space α] {f : ℕ → α} (h : summable (λn, dist (f n) (f n.succ))) {a : α} (ha : tendsto f at_top (𝓝 a)) : dist (f 0) a ≤ ∑' n, dist (f n) (f n.succ) := by simpa only [zero_add] using dist_le_tsum_dist_of_tendsto h ha 0 end cauchy_seq
fe5832fd072cd25e5a8effd48e03e68390f79235
54d7e71c3616d331b2ec3845d31deb08f3ff1dea
/tests/lean/run/tc_inout1.lean
858511ce0d91b40fc888bcac3490553782e34ea1
[ "Apache-2.0" ]
permissive
pachugupta/lean
6f3305c4292288311cc4ab4550060b17d49ffb1d
0d02136a09ac4cf27b5c88361750e38e1f485a1a
refs/heads/master
1,611,110,653,606
1,493,130,117,000
1,493,167,649,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,141
lean
universe variables u v /- Type class parameter can be annotated with out_param. Given (C a_1 ... a_n), we replace a_i with a temporary metavariable ?m_i IF 1- a_i is an out_param and it contains metavariables. 3- a_i depends on a_j for j < i, and a_j was replaced with a temporary metavariable ?m_j. This case is needed to make sure the new C-application is type correct. Then, we execute type class resolution as usual. If it succeeds, and metavariables ?m_i have been assigned, we solve the unification constraints ?m_i =?= a_i. If we succeed, we return the result. Otherwise, we fail. We also fail if ?m_i is not assigned. Remark: we do not cache results when temporary metavariables ?m_i are used. -/ class is_monoid (α : Type) (op : out_param (α → α → α)) (e : out_param α) := (op_assoc : associative op) (left_neutral : ∀ a : α, op e a = a) (right_neutral : ∀ a : α, op a e = a) lemma assoc {α : Type} {op : α → α → α} {e : α} [is_monoid α op e] : ∀ a b c : α, op (op a b) c = op a (op b c) := @is_monoid.op_assoc α op e _ instance nat_add_monoid : is_monoid nat nat.add 0 := sorry instance nat_mul_monoid : is_monoid nat nat.mul 1 := sorry instance int_mul_monoid : is_monoid int int.mul 1 := sorry open tactic run_cmd do M ← to_expr `(is_monoid nat), m₁ ← mk_mvar, m₂ ← mk_mvar, i ← mk_instance (M m₁ m₂), /- found nat_mul_monoid -/ trace i, instantiate_mvars (M m₁ m₂) >>= trace run_cmd do M ← to_expr `(is_monoid nat nat.add), m₁ ← mk_mvar, i ← mk_instance (M m₁), /- found nat_add_monoid -/ trace i, instantiate_mvars (M m₁) >>= trace section local infix + := nat.add example (a b c : nat) : (a + b) + c = a + (b + c) := assoc a b c end section class has_mem2 (α : out_param (Type u)) (γ : Type v) := (mem : α → γ → Prop) def mem2 {α : Type u} {γ : Type v} [has_mem2 α γ] : α → γ → Prop := has_mem2.mem local infix ∈ := mem2 instance (α : Type u) : has_mem2 α (list α) := ⟨list.mem⟩ #check λ a (s : list nat), a ∈ s set_option pp.notation false #check ∀ a ∈ [1, 2, 3], a > 0 end
0effb21674e934fd37b341ce069b0e29d6de1759
df561f413cfe0a88b1056655515399c546ff32a5
/4-power-world/l8.lean
3e41a378d84d062938bd306d08c56cab71b765c4
[]
no_license
nicholaspun/natural-number-game-solutions
31d5158415c6f582694680044c5c6469032c2a06
1e2aed86d2e76a3f4a275c6d99e795ad30cf6df0
refs/heads/main
1,675,123,625,012
1,607,633,548,000
1,607,633,548,000
318,933,860
3
1
null
null
null
null
UTF-8
Lean
false
false
481
lean
lemma add_squared (a b : mynat) : (a + b) ^ (2 : mynat) = a ^ (2 : mynat) + b ^ (2 : mynat) + 2 * a * b := begin rw two_eq_succ_one, rw one_eq_succ_zero, repeat { rw pow_succ }, repeat { rw pow_zero }, repeat { rw one_mul }, rw mul_add, repeat { rw add_mul }, rw mul_comm b a, rw add_assoc, rw ← add_assoc (a * b) _ _, rw ← one_mul (a * b), rw ← add_mul, rw ← one_eq_succ_zero, rw ← succ_eq_add_one, rw add_comm _ (b * b), rw ← add_assoc, rw ← mul_assoc, refl, end
129e110ba9081894d06b46110fff4136f382b5c5
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/bundle.lean
b916ce0d124578e28a65effbf07beab8ff0ae9ce
[ "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,515
lean
/- Copyright © 2021 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri -/ import algebra.module.basic /-! # Bundle > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Basic data structure to implement fiber bundles, vector bundles (maybe fibrations?), etc. This file should contain all possible results that do not involve any topology. We represent a bundle `E` over a base space `B` as a dependent type `E : B → Type*`. We define `bundle.total_space F E` to be the type of pairs `⟨b, x⟩`, where `b : B` and `x : E x`. This type is isomorphic to `Σ x, E x` and uses an extra argument `F` for reasons explained below. In general, the constructions of fiber bundles we will make will be of this form. ## Main Definitions * `bundle.total_space` the total space of a bundle. * `bundle.total_space.proj` the projection from the total space to the base space. * `bundle.total_space.mk` the constructor for the total space. ## Implementation Notes - We use a custom structure for the total space of a bundle instead of using a type synonym for the canonical disjoint union `Σ x, E x` because the total space usually has a different topology and Lean 4 `simp` fails to apply lemmas about `Σ x, E x` to elements of the total space. - The definition of `bundle.total_space` has an unused argument `F`. The reason is that in some constructions (e.g., `bundle.continuous_linear_map.vector_bundle`) we need access to the atlas of trivializations of original fiber bundles to construct the topology on the total space of the new fiber bundle. ## References - https://en.wikipedia.org/wiki/Bundle_(mathematics) -/ namespace bundle variables {B F : Type*} (E : B → Type*) /-- `bundle.total_space E` is the total space of the bundle. It consists of pairs `(proj : B, snd : E proj)`. -/ @[ext] structure total_space (F : Type*) (E : B → Type*) := (proj : B) (snd : E proj) instance [inhabited B] [inhabited (E default)] : inhabited (total_space F E) := ⟨⟨default, default⟩⟩ variables {E} /-- `bundle.total_space.proj` is the canonical projection `bundle.total_space E → B` from the total space to the base space. -/ add_decl_doc total_space.proj -- this notation won't be used in the pretty-printer localized "notation `π` := @bundle.total_space.proj _" in bundle -- TODO: try `abbrev` in Lean 4 localized "notation `total_space.mk'` F:max := @bundle.total_space.mk _ F _" in bundle lemma total_space.mk_cast {x x' : B} (h : x = x') (b : E x) : total_space.mk' F x' (cast (congr_arg E h) b) = total_space.mk x b := by { subst h, refl } instance {x : B} : has_coe_t (E x) (total_space F E) := ⟨total_space.mk x⟩ @[simp] lemma total_space.coe_proj (x : B) (v : E x) : (v : total_space F E).proj = x := rfl @[simp] lemma total_space.coe_snd {x : B} {y : E x} : (y : total_space F E).snd = y := rfl lemma total_space.coe_eq_mk {x : B} (v : E x) : (v : total_space F E) = total_space.mk x v := rfl lemma total_space.eta (z : total_space F E) : total_space.mk z.proj z.2 = z := by cases z; refl -- notation for the direct sum of two bundles over the same base notation E₁ ` ×ᵇ `:100 E₂ := λ x, E₁ x × E₂ x /-- `bundle.trivial B F` is the trivial bundle over `B` of fiber `F`. -/ @[reducible, nolint unused_arguments] def trivial (B : Type*) (F : Type*) : B → Type* := λ _, F /-- The trivial bundle, unlike other bundles, has a canonical projection on the fiber. -/ def total_space.trivial_snd (B : Type*) (F : Type*) : total_space F (bundle.trivial B F) → F := total_space.snd /-- A trivial bundle is equivalent to the product `B × F`. -/ @[simps { attrs := [`simp, `mfld_simps] }] def total_space.to_prod (B F : Type*) : total_space F (λ _ : B, F) ≃ B × F := { to_fun := λ x, (x.1, x.2), inv_fun := λ x, ⟨x.1, x.2⟩, left_inv := λ ⟨_, _⟩, rfl, right_inv := λ ⟨_, _⟩, rfl } section pullback variable {B' : Type*} /-- The pullback of a bundle `E` over a base `B` under a map `f : B' → B`, denoted by `pullback f E` or `f *ᵖ E`, is the bundle over `B'` whose fiber over `b'` is `E (f b')`. -/ def pullback (f : B' → B) (E : B → Type*) : B' → Type* := λ x, E (f x) notation f ` *ᵖ ` E:max := pullback f E instance {f : B' → B} {x : B'} [nonempty (E (f x))] : nonempty (f *ᵖ E x) := ‹nonempty (E (f x))› /-- Natural embedding of the total space of `f *ᵖ E` into `B' × total_space E`. -/ @[simp] def pullback_total_space_embedding (f : B' → B) : total_space F (f *ᵖ E) → B' × total_space F E := λ z, (z.proj, total_space.mk (f z.proj) z.2) /-- The base map `f : B' → B` lifts to a canonical map on the total spaces. -/ @[simps { attrs := [`simp, `mfld_simps] }] def pullback.lift (f : B' → B) : total_space F (f *ᵖ E) → total_space F E := λ z, ⟨f z.proj, z.2⟩ @[simp, mfld_simps] lemma pullback.lift_mk (f : B' → B) (x : B') (y : E (f x)) : pullback.lift f (total_space.mk' F x y) = ⟨f x, y⟩ := rfl end pullback section fiber_structures @[simp] lemma coe_snd_map_apply [∀ x, has_add (E x)] (x : B) (v w : E x) : (↑(v + w) : total_space F E).snd = (v : total_space F E).snd + (w : total_space F E).snd := rfl @[simp] lemma coe_snd_map_smul {R} [∀ x, has_smul R (E x)] (x : B) (r : R) (v : E x) : (↑(r • v) : total_space F E).snd = r • (v : total_space F E).snd := rfl end fiber_structures end bundle
bb4eb30856cf923421cd639952760bc34bde32d1
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/discrTreeSimp.lean
1e2c1b377fc2df0d6508e6babe09955f05b05ab9
[ "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
694
lean
prelude import Init.Data.List.Basic @[simp] theorem map_comp_map (f : α → β) (g : β → γ) : List.map g ∘ List.map f = List.map (g ∘ f) := sorry theorem map_map (f : α → β) (g : β → γ) (xs : List α) : (xs.map f |>.map g) = xs.map (g ∘ f) := sorry theorem ex1 (f : Nat → Nat) (xs : List Nat) : (xs.map f |>.map f) = xs.map (f ∘ f) := by fail_if_success simp simp [map_map] done theorem ex2 (f : Nat → Nat) : List.map f ∘ List.map f ∘ List.map f = List.map (f ∘ f ∘ f) := by simp attribute [simp] map_map theorem ex3 (f : Nat → Nat) (xs : List Nat) : (xs.map f |>.map f |>.map f) = xs.map (fun x => f (f (f x))) := by simp [Function.comp]
679c7bbd26eaa3655f1acbb09d5c5c778f8ecac9
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/src/Lean/Elab/DeclModifiers.lean
3a332d96302a72adeea2f24d903e52bbf8f6ad04
[ "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
8,011
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Modifiers import Lean.DocString import Lean.Elab.Attributes import Lean.Elab.Exception import Lean.Elab.DeclUtil namespace Lean.Elab def checkNotAlreadyDeclared {m} [Monad m] [MonadEnv m] [MonadError m] (declName : Name) : m Unit := do let env ← getEnv if env.contains declName then match privateToUserName? declName with | none => throwError "'{declName}' has already been declared" | some declName => throwError "private declaration '{declName}' has already been declared" if env.contains (mkPrivateName env declName) then throwError "a private declaration '{declName}' has already been declared" match privateToUserName? declName with | none => pure () | some declName => if env.contains declName then throwError "a non-private declaration '{declName}' has already been declared" inductive Visibility where | regular | «protected» | «private» deriving Inhabited instance : ToString Visibility := ⟨fun | Visibility.regular => "regular" | Visibility.«private» => "private" | Visibility.«protected» => "protected"⟩ inductive RecKind where | «partial» | «nonrec» | default deriving Inhabited structure Modifiers where docString? : Option String := none visibility : Visibility := Visibility.regular isNoncomputable : Bool := false recKind : RecKind := RecKind.default isUnsafe : Bool := false attrs : Array Attribute := #[] deriving Inhabited def Modifiers.isPrivate : Modifiers → Bool | { visibility := Visibility.private, .. } => true | _ => false def Modifiers.isProtected : Modifiers → Bool | { visibility := Visibility.protected, .. } => true | _ => false def Modifiers.isPartial : Modifiers → Bool | { recKind := RecKind.partial, .. } => true | _ => false def Modifiers.isNonrec : Modifiers → Bool | { recKind := RecKind.nonrec, .. } => true | _ => false def Modifiers.addAttribute (modifiers : Modifiers) (attr : Attribute) : Modifiers := { modifiers with attrs := modifiers.attrs.push attr } instance : ToFormat Modifiers := ⟨fun m => let components : List Format := (match m.docString? with | some str => [f!"/--{str}-/"] | none => []) ++ (match m.visibility with | Visibility.regular => [] | Visibility.protected => [f!"protected"] | Visibility.private => [f!"private"]) ++ (if m.isNoncomputable then [f!"noncomputable"] else []) ++ (match m.recKind with | RecKind.partial => [f!"partial"] | RecKind.nonrec => [f!"nonrec"] | _ => []) ++ (if m.isUnsafe then [f!"unsafe"] else []) ++ m.attrs.toList.map (fun attr => format attr) Format.bracket "{" (Format.joinSep components ("," ++ Format.line)) "}"⟩ instance : ToString Modifiers := ⟨toString ∘ format⟩ def expandOptDocComment? [Monad m] [MonadError m] (optDocComment : Syntax) : m (Option String) := match optDocComment.getOptional? with | none => pure none | some s => match s[1] with | Syntax.atom _ val => pure (some (val.extract 0 (val.bsize - 2))) | _ => throwErrorAt s "unexpected doc string{indentD s[1]}" section Methods variable [Monad m] [MonadEnv m] [MonadResolveName m] [MonadError m] [MonadMacroAdapter m] [MonadRecDepth m] [MonadTrace m] [MonadOptions m] [AddMessageContext m] def elabModifiers (stx : Syntax) : m Modifiers := do let docCommentStx := stx[0] let attrsStx := stx[1] let visibilityStx := stx[2] let noncompStx := stx[3] let unsafeStx := stx[4] let recKind := if stx[5].isNone then RecKind.default else if stx[5][0].getKind == ``Parser.Command.partial then RecKind.partial else RecKind.nonrec let docString? ← match docCommentStx.getOptional? with | none => pure none | some s => match s[1] with | Syntax.atom _ val => pure (some (val.extract 0 (val.bsize - 2))) | _ => throwErrorAt s "unexpected doc string{indentD s[1]}" let visibility ← match visibilityStx.getOptional? with | none => pure Visibility.regular | some v => let kind := v.getKind if kind == `Lean.Parser.Command.private then pure Visibility.private else if kind == `Lean.Parser.Command.protected then pure Visibility.protected else throwErrorAt v "unexpected visibility modifier" let attrs ← match attrsStx.getOptional? with | none => pure #[] | some attrs => elabDeclAttrs attrs return { docString?, visibility, recKind, attrs, isUnsafe := !unsafeStx.isNone isNoncomputable := !noncompStx.isNone } def applyVisibility (visibility : Visibility) (declName : Name) : m Name := do match visibility with | Visibility.private => let env ← getEnv let declName := mkPrivateName env declName checkNotAlreadyDeclared declName pure declName | Visibility.protected => checkNotAlreadyDeclared declName let env ← getEnv let env := addProtected env declName setEnv env pure declName | _ => checkNotAlreadyDeclared declName pure declName def checkIfShadowingStructureField (declName : Name) : m Unit := do match declName with | Name.str pre .. => if isStructure (← getEnv) pre then let fieldNames := getStructureFieldsFlattened (← getEnv) pre for fieldName in fieldNames do if pre ++ fieldName == declName then throwError "invalid declaration name '{declName}', structure '{pre}' has field '{fieldName}'" | _ => pure () def mkDeclName (currNamespace : Name) (modifiers : Modifiers) (shortName : Name) : m (Name × Name) := do let name := (extractMacroScopes shortName).name unless name.isAtomic || isFreshInstanceName name do throwError "atomic identifier expected '{shortName}'" let declName := currNamespace ++ shortName checkIfShadowingStructureField declName let declName ← applyVisibility modifiers.visibility declName match modifiers.visibility with | Visibility.protected => match currNamespace with | Name.str _ s _ => pure (declName, Name.mkSimple s ++ shortName) | _ => throwError "protected declarations must be in a namespace" | _ => pure (declName, shortName) /- `declId` is of the form ``` leading_parser ident >> optional (".{" >> sepBy1 ident ", " >> "}") ``` but we also accept a single identifier to users to make macro writing more convenient . -/ def expandDeclIdCore (declId : Syntax) : Name × Syntax := if declId.isIdent then (declId.getId, mkNullNode) else let id := declId[0].getId let optUnivDeclStx := declId[1] (id, optUnivDeclStx) structure ExpandDeclIdResult where shortName : Name declName : Name levelNames : List Name def expandDeclId (currNamespace : Name) (currLevelNames : List Name) (declId : Syntax) (modifiers : Modifiers) : m ExpandDeclIdResult := do -- ident >> optional (".{" >> sepBy1 ident ", " >> "}") let (shortName, optUnivDeclStx) := expandDeclIdCore declId let levelNames ← if optUnivDeclStx.isNone then pure currLevelNames else let extraLevels := optUnivDeclStx[1].getArgs.getEvenElems extraLevels.foldlM (fun levelNames idStx => let id := idStx.getId if levelNames.elem id then withRef idStx $ throwAlreadyDeclaredUniverseLevel id else pure (id :: levelNames)) currLevelNames let (declName, shortName) ← withRef declId $ mkDeclName currNamespace modifiers shortName addDocString' declName modifiers.docString? pure { shortName := shortName, declName := declName, levelNames := levelNames } end Methods end Lean.Elab
cf8a8537e5e835b27bf01dbf8127d6447d39ba1d
abd85493667895c57a7507870867b28124b3998f
/src/ring_theory/localization.lean
074ccb788473f27101645e10808cc6dbb573ce0a
[ "Apache-2.0" ]
permissive
pechersky/mathlib
d56eef16bddb0bfc8bc552b05b7270aff5944393
f1df14c2214ee114c9738e733efd5de174deb95d
refs/heads/master
1,666,714,392,571
1,591,747,567,000
1,591,747,567,000
270,557,274
0
0
Apache-2.0
1,591,597,975,000
1,591,597,974,000
null
UTF-8
Lean
false
false
33,931
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston -/ import data.equiv.ring import tactic.ring_exp import ring_theory.ideal_operations import group_theory.monoid_localization /-! # Localizations of commutative rings We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R →+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R × M` such that `z * f y = f x`; 3. For all `x, y : R`, `f x = f y` iff there exists `c ∈ M` such that `x * c = y * c`. Given such a localization map `f : R →+* S`, we can define the surjection `localization_map.mk'` sending `(x, y) : R × M` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `S` induced by a homomorphism from `R` which maps elements of `M` to invertible elements of the codomain. Similarly, given commutative rings `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : R →+* P` such that `g(M) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `S` to `Q`. We show the localization as a quotient type, defined in `group_theory.monoid_localization` as `submonoid.localization`, is a `comm_ring` and that the natural ring hom `of : R →+* localization M` is a localization map. We prove some lemmas about the `R`-algebra structure of `S`. When `R` is an integral domain, we define `fraction_map R K` as an abbreviation for `localization (non_zero_divisors R) K`, the natural map to `R`'s field of fractions. We show that a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field. We use this to show the field of fractions as a quotient type, `fraction_ring`, is a field. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A ring localization map is defined to be a localization map of the underlying `comm_monoid` (a `submonoid.localization_map`) which is also a ring hom. To prove most lemmas about a `localization_map` `f` in this file we invoke the corresponding proof for the underlying `comm_monoid` localization map `f.to_localization_map`, which can be found in `group_theory.monoid_localization` and the namespace `submonoid.localization_map`. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R → M → localization M` equals the surjection `localization_map.mk'` induced by the map `of : localization_map M (localization M)` (where `of` establishes the localization as a quotient type satisfies the characteristic predicate). The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. We use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `R`-algebra instance on `S` can 'know' the map needed to induce the `R`-algebra structure. The proof that "a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[field K]` instead of just `[comm_ring K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ variables {R : Type*} [comm_ring R] (M : submonoid R) (S : Type*) [comm_ring S] {P : Type*} [comm_ring P] open function set_option old_structure_cmd true /-- The type of ring homomorphisms satisfying the characteristic predicate: if `f : R →+* S` satisfies this predicate, then `S` is isomorphic to the localization of `R` at `M`. We later define an instance coercing a localization map `f` to its codomain `S` so that the `R`-algebra instance on `S` can 'know' the map needed to induce the `R`-algebra structure. -/ @[nolint has_inhabited_instance] structure localization_map extends ring_hom R S, submonoid.localization_map M S /-- The ring hom underlying a `localization_map`. -/ add_decl_doc localization_map.to_ring_hom /-- The `comm_monoid` `localization_map` underlying a `comm_ring` `localization_map`. See `group_theory.monoid_localization` for its definition. -/ add_decl_doc localization_map.to_localization_map variables {M S} namespace ring_hom /-- Makes a localization map from a `comm_ring` hom satisfying the characteristic predicate. -/ def to_localization_map (f : R →+* S) (H1 : ∀ y : M, is_unit (f y)) (H2 : ∀ z, ∃ x : R × M, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : M, x * c = y * c) : localization_map M S := { map_units' := H1, surj' := H2, eq_iff_exists' := H3, .. f } end ring_hom /-- Makes a `comm_ring` localization map from an additive `comm_monoid` localization map of `comm_ring`s. -/ def submonoid.localization_map.to_ring_localization (f : submonoid.localization_map M S) (h : ∀ x y, f.to_map (x + y) = f.to_map x + f.to_map y) : localization_map M S := { ..ring_hom.mk' f.to_monoid_hom h, ..f } namespace localization_map variables (f : localization_map M S) /-- Short for `to_ring_hom`; used for applying a localization map as a function. -/ abbreviation to_map := f.to_ring_hom lemma map_units (y : M) : is_unit (f.to_map y) := f.6 y lemma surj (z) : ∃ x : R × M, z * f.to_map x.2 = f.to_map x.1 := f.7 z lemma eq_iff_exists {x y} : f.to_map x = f.to_map y ↔ ∃ c : M, x * c = y * c := f.8 x y @[ext] lemma ext {f g : localization_map M S} (h : ∀ x, f.to_map x = g.to_map x) : f = g := begin cases f, cases g, simp only [] at *, exact funext h end lemma ext_iff {f g : localization_map M S} : f = g ↔ ∀ x, f.to_map x = g.to_map x := ⟨λ h x, h ▸ rfl, ext⟩ lemma to_map_injective : injective (@localization_map.to_map _ _ M S _) := λ _ _ h, ext $ ring_hom.ext_iff.1 h /-- Given `a : S`, `S` a localization of `R`, `is_integer a` iff `a` is in the image of the localization map from `R` to `S`. -/ def is_integer (a : S) : Prop := a ∈ set.range f.to_map variables {f} lemma is_integer_add {a b} (ha : f.is_integer a) (hb : f.is_integer b) : f.is_integer (a + b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' + b', rw [f.to_map.map_add, ha, hb] end lemma is_integer_mul {a b} (ha : f.is_integer a) (hb : f.is_integer b) : f.is_integer (a * b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' * b', rw [f.to_map.map_mul, ha, hb] end lemma is_integer_smul {a : R} {b} (hb : f.is_integer b) : f.is_integer (f.to_map a * b) := begin rcases hb with ⟨b', hb⟩, use a * b', rw [←hb, f.to_map.map_mul] end variables (f) /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the right, matching the argument order in `localization_map.surj`. -/ lemma exists_integer_multiple' (a : S) : ∃ (b : M), is_integer f (a * f.to_map b) := let ⟨⟨num, denom⟩, h⟩ := f.surj a in ⟨denom, set.mem_range.mpr ⟨num, h.symm⟩⟩ /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the left, matching the argument order in the `has_scalar` instance. -/ lemma exists_integer_multiple (a : S) : ∃ (b : M), is_integer f (f.to_map b * a) := by { simp_rw mul_comm _ a, apply exists_integer_multiple' } /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x` (so this lemma is true by definition). -/ lemma sec_spec {f : localization_map M S} (z : S) : z * f.to_map (f.to_localization_map.sec z).2 = f.to_map (f.to_localization_map.sec z).1 := classical.some_spec $ f.surj z /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x`, so this lemma is just an application of `S`'s commutativity. -/ lemma sec_spec' {f : localization_map M S} (z : S) : f.to_map (f.to_localization_map.sec z).1 = f.to_map (f.to_localization_map.sec z).2 * z := by rw [mul_comm, sec_spec] lemma map_right_cancel {x y} {c : M} (h : f.to_map (c * x) = f.to_map (c * y)) : f.to_map x = f.to_map y := f.to_localization_map.map_right_cancel h lemma map_left_cancel {x y} {c : M} (h : f.to_map (x * c) = f.to_map (y * c)) : f.to_map x = f.to_map y := f.to_localization_map.map_left_cancel h lemma eq_zero_of_fst_eq_zero {z x} {y : M} (h : z * f.to_map y = f.to_map x) (hx : x = 0) : z = 0 := by rw [hx, f.to_map.map_zero] at h; exact (is_unit.mul_left_eq_zero_iff_eq_zero (f.map_units y)).1 h /-- Given a localization map `f : R →+* S`, the surjection sending `(x, y) : R × M` to `f x * (f y)⁻¹`. -/ noncomputable def mk' (f : localization_map M S) (x : R) (y : M) : S := f.to_localization_map.mk' x y @[simp] lemma mk'_sec (z : S) : f.mk' (f.to_localization_map.sec z).1 (f.to_localization_map.sec z).2 = z := f.to_localization_map.mk'_sec _ lemma mk'_mul (x₁ x₂ : R) (y₁ y₂ : M) : f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ := f.to_localization_map.mk'_mul _ _ _ _ lemma mk'_one (x) : f.mk' x (1 : M) = f.to_map x := f.to_localization_map.mk'_one _ lemma mk'_spec (x) (y : M) : f.mk' x y * f.to_map y = f.to_map x := f.to_localization_map.mk'_spec _ _ lemma mk'_spec' (x) (y : M) : f.to_map y * f.mk' x y = f.to_map x := f.to_localization_map.mk'_spec' _ _ theorem eq_mk'_iff_mul_eq {x} {y : M} {z} : z = f.mk' x y ↔ z * f.to_map y = f.to_map x := f.to_localization_map.eq_mk'_iff_mul_eq theorem mk'_eq_iff_eq_mul {x} {y : M} {z} : f.mk' x y = z ↔ f.to_map x = z * f.to_map y := f.to_localization_map.mk'_eq_iff_eq_mul lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : M} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) := f.to_localization_map.mk'_eq_iff_eq protected lemma eq {a₁ b₁} {a₂ b₂ : M} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : M, a₁ * b₂ * c = b₁ * a₂ * c := f.to_localization_map.eq lemma eq_iff_eq (g : localization_map M P) {x y} : f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y := f.to_localization_map.eq_iff_eq g.to_localization_map lemma mk'_eq_iff_mk'_eq (g : localization_map M P) {x₁ x₂} {y₁ y₂ : M} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ := f.to_localization_map.mk'_eq_iff_mk'_eq g.to_localization_map lemma mk'_eq_of_eq {a₁ b₁ : R} {a₂ b₂ : M} (H : b₁ * a₂ = a₁ * b₂) : f.mk' a₁ a₂ = f.mk' b₁ b₂ := f.to_localization_map.mk'_eq_of_eq H @[simp] lemma mk'_self {x : R} {hx : x ∈ M} : f.mk' x ⟨x, hx⟩ = 1 := f.to_localization_map.mk'_self' _ hx @[simp] lemma mk'_self' {x : M} : f.mk' x x = 1 := f.to_localization_map.mk'_self _ @[simp] lemma mk'_self'' {x : M} : f.mk' x.1 x = 1 := f.mk'_self' lemma mul_mk'_eq_mk'_of_mul (x y : R) (z : M) : f.to_map x * f.mk' y z = f.mk' (x * y) z := f.to_localization_map.mul_mk'_eq_mk'_of_mul _ _ _ lemma mk'_eq_mul_mk'_one (x : R) (y : M) : f.mk' x y = f.to_map x * f.mk' 1 y := (f.to_localization_map.mul_mk'_one_eq_mk' _ _).symm @[simp] lemma mk'_mul_cancel_left (x : R) (y : M) : f.mk' (y * x) y = f.to_map x := f.to_localization_map.mk'_mul_cancel_left _ _ lemma mk'_mul_cancel_right (x : R) (y : M) : f.mk' (x * y) y = f.to_map x := f.to_localization_map.mk'_mul_cancel_right _ _ lemma is_unit_comp (j : S →+* P) (y : M) : is_unit (j.comp f.to_map y) := f.to_localization_map.is_unit_comp j.to_monoid_hom _ /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g(M) ⊆ units P`, `f x = f y → g x = g y` for all `x y : R`. -/ lemma eq_of_eq {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) : g x = g y := @submonoid.localization_map.eq_of_eq _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg _ _ h lemma mk'_add (x₁ x₂ : R) (y₁ y₂ : M) : f.mk' (x₁ * y₂ + x₂ * y₁) (y₁ * y₂) = f.mk' x₁ y₁ + f.mk' x₂ y₂ := f.mk'_eq_iff_eq_mul.2 $ eq.symm begin rw [mul_comm (_ + _), mul_add, mul_mk'_eq_mk'_of_mul, ←eq_sub_iff_add_eq, mk'_eq_iff_eq_mul, mul_comm _ (f.to_map _), mul_sub, eq_sub_iff_add_eq, ←eq_sub_iff_add_eq', ←mul_assoc, ←f.to_map.map_mul, mul_mk'_eq_mk'_of_mul, mk'_eq_iff_eq_mul], simp only [f.to_map.map_add, submonoid.coe_mul, f.to_map.map_mul], ring_exp, end /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` sending `z : S` to `g x * (g y)⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) : S →+* P := ring_hom.mk' (@submonoid.localization_map.lift _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg) $ begin intros x y, rw [f.to_localization_map.lift_spec, mul_comm, add_mul, ←sub_eq_iff_eq_add, eq_comm, f.to_localization_map.lift_spec_mul, mul_comm _ (_ - _), sub_mul, eq_sub_iff_add_eq', ←eq_sub_iff_add_eq, mul_assoc, f.to_localization_map.lift_spec_mul], show g _ * (g _ * g _) = g _ * (g _ * g _ - g _ * g _), repeat {rw ←g.map_mul}, rw [←g.map_sub, ←g.map_mul], apply f.eq_of_eq hg, erw [f.to_map.map_mul, sec_spec', mul_sub, f.to_map.map_sub], simp only [f.to_map.map_mul, sec_spec'], ring_exp, end variables {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : R, y ∈ M`. -/ lemma lift_mk' (x y) : f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.to_monoid_hom.mrestrict M) hg y)⁻¹ := f.to_localization_map.lift_mk' _ _ _ lemma lift_mk'_spec (x v) (y : M) : f.lift hg (f.mk' x y) = v ↔ g x = g y * v := f.to_localization_map.lift_mk'_spec _ _ _ _ @[simp] lemma lift_eq (x : R) : f.lift hg (f.to_map x) = g x := f.to_localization_map.lift_eq _ _ lemma lift_eq_iff {x y : R × M} : f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := f.to_localization_map.lift_eq_iff _ @[simp] lemma lift_comp : (f.lift hg).comp f.to_map = g := ring_hom.ext $ monoid_hom.ext_iff.1 $ f.to_localization_map.lift_comp _ @[simp] lemma lift_of_comp (j : S →+* P) : f.lift (f.is_unit_comp j) = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ f.to_localization_map.lift_of_comp j.to_monoid_hom lemma epic_of_localization_map {j k : S →+* P} (h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.epic_of_localization_map _ _ _ _ _ _ _ f.to_localization_map j.to_monoid_hom k.to_monoid_hom h lemma lift_unique {j : S →+* P} (hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.lift_unique _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg j.to_monoid_hom hj @[simp] lemma lift_id (x) : f.lift f.map_units x = x := f.to_localization_map.lift_id _ /-- Given two localization maps `f : R →+* S, k : R →+* P` for a submonoid `M ⊆ R`, the hom from `P` to `S` induced by `f` is left inverse to the hom from `S` to `P` induced by `k`. -/ @[simp] lemma lift_left_inverse {k : localization_map M S} (z : S) : k.lift f.map_units (f.lift k.map_units z) = z := f.to_localization_map.lift_left_inverse _ lemma lift_surjective_iff : surjective (f.lift hg) ↔ ∀ v : P, ∃ x : R × M, v * g x.2 = g x.1 := f.to_localization_map.lift_surjective_iff hg lemma lift_injective_iff : injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y := f.to_localization_map.lift_injective_iff hg variables {T : submonoid P} (hy : ∀ y : M, g y ∈ T) {Q : Type*} [comm_ring Q] (k : localization_map T Q) /-- Given a `comm_ring` homomorphism `g : R →+* P` where for submonoids `M ⊆ R, T ⊆ P` we have `g(M) ⊆ T`, the induced ring homomorphism from the localization of `R` at `M` to the localization of `P` at `T`: if `f : R →+* S` and `k : P →+* Q` are localization maps for `M` and `T` respectively, we send `z : S` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map : S →+* Q := @lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩ variables {k} lemma map_eq (x) : f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x @[simp] lemma map_comp : (f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩ lemma map_mk' (x) (y : M) : f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ := @submonoid.localization_map.map_mk' _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom _ hy _ _ k.to_localization_map _ _ @[simp] lemma map_id (z : S) : f.map (λ y, show ring_hom.id R y ∈ M, from y.2) f z = z := f.lift_id _ /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_comp_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ w : T, l w ∈ U) : (k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.map_comp_map _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom _ hy _ _ k.to_localization_map _ _ _ _ _ j.to_localization_map l.to_monoid_hom hl /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ w : T, l w ∈ U) (x) : k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x := by rw ←f.map_comp_map hy j hl; refl /-- Given localization maps `f : R →+* S, k : P →+* Q` for submonoids `M, T` respectively, an isomorphism `j : R ≃+* P` such that `j(M) = T` induces an isomorphism of localizations `S ≃+* Q`. -/ noncomputable def ring_equiv_of_ring_equiv (k : localization_map T Q) (h : R ≃+* P) (H : M.map h.to_monoid_hom = T) : S ≃+* Q := (f.to_localization_map.mul_equiv_of_mul_equiv k.to_localization_map H).to_ring_equiv $ (@lift _ _ _ _ _ _ _ f (k.to_map.comp h.to_ring_hom) (λ y, k.map_units ⟨(h y), H ▸ set.mem_image_of_mem h y.2⟩)).map_add @[simp] lemma ring_equiv_of_ring_equiv_eq_map_apply {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : f.ring_equiv_of_ring_equiv k j H x = f.map (λ y : M, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl lemma ring_equiv_of_ring_equiv_eq_map {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) : (f.ring_equiv_of_ring_equiv k j H).to_monoid_hom = f.map (λ y : M, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl @[simp] lemma ring_equiv_of_ring_equiv_eq {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : f.ring_equiv_of_ring_equiv k j H (f.to_map x) = k.to_map (j x) := f.to_localization_map.mul_equiv_of_mul_equiv_eq H _ lemma ring_equiv_of_ring_equiv_mk' {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x y) : f.ring_equiv_of_ring_equiv k j H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ := f.to_localization_map.mul_equiv_of_mul_equiv_mk' H _ _ end localization_map namespace localization variables {M} instance : has_add (localization M) := ⟨λ z w, con.lift_on₂ z w (λ x y : R × M, mk ((x.2 : R) * y.1 + y.2 * x.1) (x.2 * y.2)) $ λ r1 r2 r3 r4 h1 h2, (con.eq _).2 begin rw r_eq_r' at h1 h2 ⊢, cases h1 with t₅ ht₅, cases h2 with t₆ ht₆, use t₆ * t₅, calc ((r1.2 : R) * r2.1 + r2.2 * r1.1) * (r3.2 * r4.2) * (t₆ * t₅) = (r2.1 * r4.2 * t₆) * (r1.2 * r3.2 * t₅) + (r1.1 * r3.2 * t₅) * (r2.2 * r4.2 * t₆) : by ring ... = (r3.2 * r4.1 + r4.2 * r3.1) * (r1.2 * r2.2) * (t₆ * t₅) : by rw [ht₆, ht₅]; ring end⟩ instance : has_neg (localization M) := ⟨λ z, con.lift_on z (λ x : R × M, mk (-x.1) x.2) $ λ r1 r2 h, (con.eq _).2 begin rw r_eq_r' at h ⊢, cases h with t ht, use t, rw [neg_mul_eq_neg_mul_symm, neg_mul_eq_neg_mul_symm, ht], ring, end⟩ instance : has_zero (localization M) := ⟨mk 0 1⟩ private meta def tac := `[{ intros, refine quotient.sound' (r_of_eq _), simp only [prod.snd_mul, prod.fst_mul, submonoid.coe_mul], ring }] instance : comm_ring (localization M) := { zero := 0, one := 1, add := (+), mul := (*), add_assoc := λ m n k, quotient.induction_on₃' m n k (by tac), zero_add := λ y, quotient.induction_on' y (by tac), add_zero := λ y, quotient.induction_on' y (by tac), neg := has_neg.neg, add_left_neg := λ y, quotient.induction_on' y (by tac), add_comm := λ y z, quotient.induction_on₂' z y (by tac), left_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), right_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), ..localization.comm_monoid M } variables (M) /-- Natural hom sending `x : R`, `R` a `comm_ring`, to the equivalence class of `(x, 1)` in the localization of `R` at a submonoid. -/ def of : localization_map M (localization M) := (localization.monoid_of M).to_ring_localization $ λ x y, (con.eq _).2 $ r_of_eq $ by simp [add_comm] variables {M} lemma monoid_of_eq_of (x) : (monoid_of M).to_map x = (of M).to_map x := rfl lemma mk_one_eq_of (x) : mk x 1 = (of M).to_map x := rfl lemma mk_eq_mk'_apply (x y) : mk x y = (of M).mk' x y := mk_eq_monoid_of_mk'_apply _ _ @[simp] lemma mk_eq_mk' : mk = (of M).mk' := mk_eq_monoid_of_mk' variables (f : localization_map M S) /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ noncomputable def ring_equiv_of_quotient : localization M ≃+* S := (mul_equiv_of_quotient f.to_localization_map).to_ring_equiv $ ((of M).lift f.map_units).map_add variables {f} @[simp] lemma ring_equiv_of_quotient_apply (x) : ring_equiv_of_quotient f x = (of M).lift f.map_units x := rfl @[simp] lemma ring_equiv_of_quotient_mk' (x y) : ring_equiv_of_quotient f ((of M).mk' x y) = f.mk' x y := mul_equiv_of_quotient_mk' _ _ lemma ring_equiv_of_quotient_mk (x y) : ring_equiv_of_quotient f (mk x y) = f.mk' x y := mul_equiv_of_quotient_mk _ _ @[simp] lemma ring_equiv_of_quotient_of (x) : ring_equiv_of_quotient f ((of M).to_map x) = f.to_map x := mul_equiv_of_quotient_monoid_of _ @[simp] lemma ring_equiv_of_quotient_symm_mk' (x y) : (ring_equiv_of_quotient f).symm (f.mk' x y) = (of M).mk' x y := mul_equiv_of_quotient_symm_mk' _ _ lemma ring_equiv_of_quotient_symm_mk (x y) : (ring_equiv_of_quotient f).symm (f.mk' x y) = mk x y := mul_equiv_of_quotient_symm_mk _ _ @[simp] lemma ring_equiv_of_quotient_symm_of (x) : (ring_equiv_of_quotient f).symm (f.to_map x) = (of M).to_map x := mul_equiv_of_quotient_symm_monoid_of _ end localization variables {M} namespace localization_map /-! ### `algebra` section Defines the `R`-algebra instance on a copy of `S` carrying the data of the localization map `f` needed to induce the `R`-algebra structure. -/ variables (f : localization_map M S) /-- We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `R`-algebra instance on `S` can 'know' the map needed to induce the `R`-algebra structure. -/ @[reducible, nolint unused_arguments] def codomain (f : localization_map M S) := S /-- We use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `R`-algebra instance on `S` can 'know' the map needed to induce the `R`-algebra structure. -/ instance : algebra R f.codomain := f.to_map.to_algebra @[simp] lemma of_id (a : R) : (algebra.of_id R f.codomain) a = f.to_map a := rfl variables (f) /-- Localization map `f` from `R` to `S` as an `R`-linear map. -/ def lin_coe : R →ₗ[R] f.codomain := { to_fun := f.to_map, add := f.to_map.map_add, smul := f.to_map.map_mul } variables {f} instance coe_submodules : has_coe (ideal R) (submodule R f.codomain) := ⟨λ I, submodule.map f.lin_coe I⟩ lemma mem_coe (I : ideal R) {x : S} : x ∈ (I : submodule R f.codomain) ↔ ∃ y : R, y ∈ I ∧ f.to_map y = x := iff.rfl @[simp] lemma lin_coe_apply {x} : f.lin_coe x = f.to_map x := rfl end localization_map variables (R) /-- The submonoid of non-zero-divisors of a `comm_ring` `R`. -/ def non_zero_divisors : submonoid R := { carrier := {x | ∀ z, z * x = 0 → z = 0}, one_mem' := λ z hz, by rwa mul_one at hz, mul_mem' := λ x₁ x₂ hx₁ hx₂ z hz, have z * x₁ * x₂ = 0, by rwa mul_assoc, hx₁ z $ hx₂ (z * x₁) this } variables {A : Type*} [integral_domain A] lemma eq_zero_of_ne_zero_of_mul_eq_zero {x y : A} (hnx : x ≠ 0) (hxy : y * x = 0) : y = 0 := or.resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero hxy) hnx lemma mem_non_zero_divisors_iff_ne_zero {x : A} : x ∈ non_zero_divisors A ↔ x ≠ 0 := ⟨λ hm hz, zero_ne_one (hm 1 $ by rw [hz, one_mul]).symm, λ hnx z, eq_zero_of_ne_zero_of_mul_eq_zero hnx⟩ lemma map_ne_zero_of_mem_non_zero_divisors {B : Type*} [ring B] {g : A →+* B} (hg : injective g) {x : non_zero_divisors A} : g x ≠ 0 := λ h0, mem_non_zero_divisors_iff_ne_zero.1 x.2 $ g.injective_iff.1 hg x h0 lemma map_mem_non_zero_divisors {B : Type*} [integral_domain B] {g : A →+* B} (hg : injective g) {x : non_zero_divisors A} : g x ∈ non_zero_divisors B := λ z hz, eq_zero_of_ne_zero_of_mul_eq_zero (map_ne_zero_of_mem_non_zero_divisors hg) hz variables (K : Type*) /-- Localization map from an integral domain `R` to its field of fractions. -/ @[reducible] def fraction_map [comm_ring K] := localization_map (non_zero_divisors R) K namespace fraction_map open localization_map variables {R K} lemma to_map_eq_zero_iff [comm_ring K] (φ : fraction_map R K) {x : R} : x = 0 ↔ φ.to_map x = 0 := begin rw ← φ.to_map.map_zero, split; intro h, { rw h }, { cases φ.eq_iff_exists.mp h with c hc, rw zero_mul at hc, exact c.2 x hc } end protected theorem injective [comm_ring K] (φ : fraction_map R K) : injective φ.to_map := φ.to_map.injective_iff.2 (λ _ h, φ.to_map_eq_zero_iff.mpr h) local attribute [instance] classical.dec_eq /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is an integral domain. -/ def to_integral_domain [comm_ring K] (φ : fraction_map A K) : integral_domain K := { eq_zero_or_eq_zero_of_mul_eq_zero := begin intros z w h, cases φ.surj z with x hx, cases φ.surj w with y hy, have : z * w * φ.to_map y.2 * φ.to_map x.2 = φ.to_map x.1 * φ.to_map y.1, by rw [mul_assoc z, hy, ←hx]; ac_refl, erw h at this, rw [zero_mul, zero_mul, ←φ.to_map.map_mul] at this, cases eq_zero_or_eq_zero_of_mul_eq_zero (φ.to_map_eq_zero_iff.mpr this.symm) with H H, { exact or.inl (φ.eq_zero_of_fst_eq_zero hx H) }, { exact or.inr (φ.eq_zero_of_fst_eq_zero hy H) }, end, zero_ne_one := by erw [←φ.to_map.map_zero, ←φ.to_map.map_one]; exact λ h, zero_ne_one (φ.injective h), ..(infer_instance : comm_ring K) } /-- The inverse of an element in the field of fractions of an integral domain. -/ protected noncomputable def inv [comm_ring K] (φ : fraction_map A K) (z : K) : K := if h : z = 0 then 0 else φ.mk' (φ.to_localization_map.sec z).2 ⟨(φ.to_localization_map.sec z).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, h $ φ.eq_zero_of_fst_eq_zero (sec_spec z) h0⟩ protected lemma mul_inv_cancel [comm_ring K] (φ : fraction_map A K) (x : K) (hx : x ≠ 0) : x * φ.inv x = 1 := show x * dite _ _ _ = 1, by rw [dif_neg hx, ←is_unit.mul_left_inj (φ.map_units ⟨(φ.to_localization_map.sec x).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, hx $ φ.eq_zero_of_fst_eq_zero (sec_spec x) h0⟩), one_mul, mul_assoc, mk'_spec, ←eq_mk'_iff_mul_eq]; exact (φ.mk'_sec x).symm /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is a field. -/ noncomputable def to_field [comm_ring K] (φ : fraction_map A K) : field K := { inv := φ.inv, mul_inv_cancel := φ.mul_inv_cancel, inv_zero := dif_pos rfl, ..φ.to_integral_domain } variables {B : Type*} [integral_domain B] [field K] {L : Type*} [field L] (f : fraction_map A K) {g : A →+* L} lemma mk'_eq_div {r s} : f.mk' r s = f.to_map r / f.to_map s := f.mk'_eq_iff_eq_mul.2 $ (div_mul_cancel _ (map_ne_zero_of_mem_non_zero_divisors f.injective)).symm lemma is_unit_map_of_injective (hg : injective g) (y : non_zero_divisors A) : is_unit (g y) := is_unit.mk0 (g y) $ map_ne_zero_of_mem_non_zero_divisors hg /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, we get a field hom sending `z : K` to `g x * (g y)⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift (hg : injective g) : K →+* L := f.lift $ is_unit_map_of_injective hg /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, field hom induced from `K` to `L` maps `f x / f y` to `g x / g y` for all `x : A, y ∈ non_zero_divisors A`. -/ @[simp] lemma lift_mk' (hg : injective g) (x y) : f.lift hg (f.mk' x y) = g x / g y := begin erw f.lift_mk' (is_unit_map_of_injective hg), erw submonoid.localization_map.mul_inv_left (λ y : non_zero_divisors A, show is_unit (g.to_monoid_hom y), from is_unit_map_of_injective hg y), exact (mul_div_cancel' _ (map_ne_zero_of_mem_non_zero_divisors hg)).symm, end /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L` and an injective ring hom `j : A →+* B`, we get a field hom sending `z : K` to `g (j x) * (g (j y))⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map (g : fraction_map B L) {j : A →+* B} (hj : injective j) : K →+* L := f.map (λ y, mem_non_zero_divisors_iff_ne_zero.2 $ map_ne_zero_of_mem_non_zero_divisors hj) g /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L`, an isomorphism `j : A ≃+* B` induces an isomorphism of fields of fractions `K ≃+* L`. -/ noncomputable def field_equiv_of_ring_equiv (g : fraction_map B L) (h : A ≃+* B) : K ≃+* L := f.ring_equiv_of_ring_equiv g h begin ext b, show b ∈ h.to_equiv '' _ ↔ _, erw [h.to_equiv.image_eq_preimage, set.preimage, set.mem_set_of_eq, mem_non_zero_divisors_iff_ne_zero, mem_non_zero_divisors_iff_ne_zero], exact h.symm.map_ne_zero_iff end /-- The cast from `int` to `rat` as a `fraction_map`. -/ def int.fraction_map : fraction_map ℤ ℚ := { to_fun := coe, map_units' := begin rintro ⟨x, hx⟩, rw [submonoid.mem_carrier, mem_non_zero_divisors_iff_ne_zero] at hx, simpa only [is_unit_iff_ne_zero, int.cast_eq_zero, ne.def, subtype.coe_mk] using hx, end, surj' := begin rintro ⟨n, d, hd, h⟩, refine ⟨⟨n, ⟨d, _⟩⟩, rat.mul_denom_eq_num⟩, rwa [submonoid.mem_carrier, mem_non_zero_divisors_iff_ne_zero, int.coe_nat_ne_zero_iff_pos] end, eq_iff_exists' := begin intros x y, rw [int.cast_inj], refine ⟨by { rintro rfl, use 1 }, _⟩, rintro ⟨⟨c, hc⟩, h⟩, apply int.eq_of_mul_eq_mul_right _ h, rwa [submonoid.mem_carrier, mem_non_zero_divisors_iff_ne_zero] at hc, end, ..int.cast_ring_hom ℚ } end fraction_map variables (A) /-- The fraction field of an integral domain as a quotient type. -/ @[reducible] def fraction_ring := localization (non_zero_divisors A) /-- Natural hom sending `x : A`, `A` an integral domain, to the equivalence class of `(x, 1)` in the field of fractions of `A`. -/ def of : fraction_map A (localization (non_zero_divisors A)) := localization.of (non_zero_divisors A) namespace fraction_ring variables {A} noncomputable instance : field (fraction_ring A) := (of A).to_field @[simp] lemma mk_eq_div {r s} : (localization.mk r s : fraction_ring A) = ((of A).to_map r / (of A).to_map s : fraction_ring A) := by erw [localization.mk_eq_mk', (of A).mk'_eq_div] /-- Given an integral domain `A` and a localization map to a field of fractions `f : A →+* K`, we get an isomorphism between the field of fractions of `A` as a quotient type and `K`. -/ noncomputable def field_equiv_of_quotient {K : Type*} [field K] (f : fraction_map A K) : fraction_ring A ≃+* K := localization.ring_equiv_of_quotient f end fraction_ring
03fef68ff07414000aecc75ee8e963b4e5b1ca86
6f4750ff03c8be1d6c0b1b809ff6ac541b9d5a22
/WhileSyntax.lean
d084844ac0ec59584a229993e06b1284cd9dae1b
[]
no_license
benating/Lean_Test
1809b5e15df9edcdf4a3388f53d2205f29c89ac2
c07099804a86347b4ec836f54530f62e22534bdb
refs/heads/master
1,628,468,905,337
1,510,426,597,000
1,510,426,597,000
108,762,297
0
0
null
null
null
null
UTF-8
Lean
false
false
1,033
lean
namespace whileSyntax inductive wExpr : Type | ident : string → wExpr | num : ℕ → wExpr | add : wExpr → wExpr → wExpr | mul : wExpr → wExpr → wExpr namespace wExpr infixl `+` := add infixl `*` := mul end wExpr inductive wBool : Type | true : wBool | false : wBool | eq : wBool → wBool → wBool | lt : wBool → wBool → wBool | and : wBool → wBool → wBool | or : wBool → wBool → wBool | not : wBool → wBool → wBool namespace wBool infixl `=` := eq infixl `<` := lt precedence `:∧` :1 infixl `:∧` := and precedence `:∨` :2 infixl `:∨` := or infixl `¬` := not end wBool inductive wComm : Type | skip : wComm | assign : string → wExpr → wComm | seq : wComm → wComm → wComm | ifThenElse : wBool → wComm → wComm → wComm | whileDo : wBool → wComm → wComm namespace wComm infixl `:=` := assign infixl `;` := seq end wComm def state := string → ℕ inductive configExpr : Type | configE : wExpr × state → configExpr end whileSyntax
c1f77eec7369b25888139e8b519bcdfca782c8be
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Std/Data/HashMap.lean
f2f981a5b7db3f9652bc371c7e365e5d78d66c8f
[ "Apache-2.0" ]
permissive
banksonian/lean4
3a2e6b0f1eb63aa56ff95b8d07b2f851072d54dc
78da6b3aa2840693eea354a41e89fc5b212a5011
refs/heads/master
1,673,703,624,165
1,605,123,551,000
1,605,123,551,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,220
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Std.Data.AssocList namespace Std universes u v w def HashMapBucket (α : Type u) (β : Type v) := { b : Array (AssocList α β) // b.size > 0 } def HashMapBucket.update {α : Type u} {β : Type v} (data : HashMapBucket α β) (i : USize) (d : AssocList α β) (h : i.toNat < data.val.size) : HashMapBucket α β := ⟨ data.val.uset i d h, by rw [Array.szFSetEq]; exact data.property ⟩ structure HashMapImp (α : Type u) (β : Type v) := (size : Nat) (buckets : HashMapBucket α β) def mkHashMapImp {α : Type u} {β : Type v} (nbuckets := 8) : HashMapImp α β := let n := if nbuckets = 0 then 8 else nbuckets; { size := 0, buckets := ⟨ mkArray n AssocList.nil, by rw [Array.sizeMkArrayEq]; cases nbuckets; decide!; apply Nat.zeroLtSucc; done ⟩ } namespace HashMapImp variables {α : Type u} {β : Type v} def mkIdx {n : Nat} (h : n > 0) (u : USize) : { u : USize // u.toNat < n } := ⟨u %ₙ n, USize.modnLt _ h⟩ @[inline] def reinsertAux (hashFn : α → USize) (data : HashMapBucket α β) (a : α) (b : β) : HashMapBucket α β := let ⟨i, h⟩ := mkIdx data.property (hashFn a) data.update i (AssocList.cons a b (data.val.uget i h)) h @[inline] def foldBucketsM {δ : Type w} {m : Type w → Type w} [Monad m] (data : HashMapBucket α β) (d : δ) (f : δ → α → β → m δ) : m δ := data.val.foldlM (init := d) fun d b => b.foldlM f d @[inline] def foldBuckets {δ : Type w} (data : HashMapBucket α β) (d : δ) (f : δ → α → β → δ) : δ := Id.run $ foldBucketsM data d f @[inline] def foldM {δ : Type w} {m : Type w → Type w} [Monad m] (f : δ → α → β → m δ) (d : δ) (h : HashMapImp α β) : m δ := foldBucketsM h.buckets d f @[inline] def fold {δ : Type w} (f : δ → α → β → δ) (d : δ) (m : HashMapImp α β) : δ := foldBuckets m.buckets d f def findEntry? [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Option (α × β) := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a) (buckets.val.uget i h).findEntry? a def find? [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Option β := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a) (buckets.val.uget i h).find? a def contains [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : Bool := match m with | ⟨_, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a) (buckets.val.uget i h).contains a -- TODO: remove `partial` by using well-founded recursion partial def moveEntries [Hashable α] (i : Nat) (source : Array (AssocList α β)) (target : HashMapBucket α β) : HashMapBucket α β := if h : i < source.size then let idx : Fin source.size := ⟨i, h⟩ let es : AssocList α β := source.get idx -- We remove `es` from `source` to make sure we can reuse its memory cells when performing es.foldl let source := source.set idx AssocList.nil let target := es.foldl (reinsertAux hash) target moveEntries (i+1) source target else target def expand [Hashable α] (size : Nat) (buckets : HashMapBucket α β) : HashMapImp α β := let nbuckets := buckets.val.size * 2 have nbuckets > 0 from Nat.mulPos buckets.property (decide! : 2 > 0) let new_buckets : HashMapBucket α β := ⟨mkArray nbuckets AssocList.nil, by rw [Array.sizeMkArrayEq]; assumption⟩ { size := size, buckets := moveEntries 0 buckets.val new_buckets } def insert [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) (b : β) : HashMapImp α β := match m with | ⟨size, buckets⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a) let bkt := buckets.val.uget i h if bkt.contains a then ⟨size, buckets.update i (bkt.replace a b) h⟩ else let size' := size + 1 let buckets' := buckets.update i (AssocList.cons a b bkt) h if size' ≤ buckets.val.size then { size := size', buckets := buckets' } else expand size' buckets' def erase [BEq α] [Hashable α] (m : HashMapImp α β) (a : α) : HashMapImp α β := match m with | ⟨ size, buckets ⟩ => let ⟨i, h⟩ := mkIdx buckets.property (hash a) let bkt := buckets.val.uget i h if bkt.contains a then ⟨size - 1, buckets.update i (bkt.erase a) h⟩ else m inductive WellFormed [BEq α] [Hashable α] : HashMapImp α β → Prop | mkWff : ∀ n, WellFormed (mkHashMapImp n) | insertWff : ∀ m a b, WellFormed m → WellFormed (insert m a b) | eraseWff : ∀ m a, WellFormed m → WellFormed (erase m a) end HashMapImp def HashMap (α : Type u) (β : Type v) [BEq α] [Hashable α] := { m : HashMapImp α β // m.WellFormed } open Std.HashMapImp def mkHashMap {α : Type u} {β : Type v} [BEq α] [Hashable α] (nbuckets := 8) : HashMap α β := ⟨ mkHashMapImp nbuckets, WellFormed.mkWff nbuckets ⟩ namespace HashMap variables {α : Type u} {β : Type v} [BEq α] [Hashable α] instance : Inhabited (HashMap α β) := ⟨mkHashMap⟩ instance : EmptyCollection (HashMap α β) := ⟨mkHashMap⟩ @[inline] def insert (m : HashMap α β) (a : α) (b : β) : HashMap α β := match m with | ⟨ m, hw ⟩ => ⟨ m.insert a b, WellFormed.insertWff m a b hw ⟩ @[inline] def erase (m : HashMap α β) (a : α) : HashMap α β := match m with | ⟨ m, hw ⟩ => ⟨ m.erase a, WellFormed.eraseWff m a hw ⟩ @[inline] def findEntry? (m : HashMap α β) (a : α) : Option (α × β) := match m with | ⟨ m, _ ⟩ => m.findEntry? a @[inline] def find? (m : HashMap α β) (a : α) : Option β := match m with | ⟨ m, _ ⟩ => m.find? a @[inline] def findD (m : HashMap α β) (a : α) (b₀ : β) : β := (m.find? a).getD b₀ @[inline] def find! [Inhabited β] (m : HashMap α β) (a : α) : β := match m.find? a with | some b => b | none => panic! "key is not in the map" @[inline] def getOp (self : HashMap α β) (idx : α) : Option β := self.find? idx @[inline] def contains (m : HashMap α β) (a : α) : Bool := match m with | ⟨ m, _ ⟩ => m.contains a @[inline] def foldM {δ : Type w} {m : Type w → Type w} [Monad m] (f : δ → α → β → m δ) (init : δ) (h : HashMap α β) : m δ := match h with | ⟨ h, _ ⟩ => h.foldM f init @[inline] def fold {δ : Type w} (f : δ → α → β → δ) (init : δ) (m : HashMap α β) : δ := match m with | ⟨ m, _ ⟩ => m.fold f init @[inline] def size (m : HashMap α β) : Nat := match m with | ⟨ {size := sz, ..}, _ ⟩ => sz @[inline] def isEmpty (m : HashMap α β) : Bool := m.size = 0 @[inline] def empty : HashMap α β := mkHashMap def toList (m : HashMap α β) : List (α × β) := m.fold (init := []) fun r k v => (k, v)::r def toArray (m : HashMap α β) : Array (α × β) := m.fold (init := #[]) fun r k v => r.push (k, v) def numBuckets (m : HashMap α β) : Nat := m.val.buckets.val.size end HashMap end Std