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
4dc524b5641a8e1b325fd0a76374fb05ea3b5cf6
64874bd1010548c7f5a6e3e8902efa63baaff785
/tests/lean/run/eq1.lean
5ba3494ce3ae5889a034800440938227006ea76c
[ "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
438
lean
inductive day := monday, tuesday, wednesday, thursday, friday, saturday, sunday open day definition next_weekday : day → day, next_weekday monday := tuesday, next_weekday tuesday := wednesday, next_weekday wednesday := thursday, next_weekday thursday := friday, next_weekday friday := monday, next_weekday saturday := monday, next_weekday sunday := monday example : next_weekday (next_weekday monday) = wednesday := rfl
7847c5bdb8592fbdcf636d1503438f8e2ee6593b
ae9f8bf05de0928a4374adc7d6b36af3411d3400
/src/formal_ml/set.lean
f3fa1879bed37381ee7991fd20b8d006b54903dd
[ "Apache-2.0" ]
permissive
NeoTim/formal-ml
bc42cf6beba9cd2ed56c1cd054ab4eb5402ed445
c9cbad2837104160a9832a29245471468748bb8d
refs/heads/master
1,671,549,160,900
1,601,362,989,000
1,601,362,989,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,122
lean
/- Copyright 2020 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ import data.set.disjointed data.set.countable import data.set.lattice data.set.finite import formal_ml.nat /- Theorems about sets. The best repository of theorems about sets is /mathlib/src/data/set/basic.lean. -/ --Novel, used: move to mathlib? --Alternately, look into topological_space.lean, and see what is necessary. lemma set.preimage_fst_def {α β:Type*} {Bα:set (set α)}: (@set.preimage (α × β) α (@prod.fst α β) '' Bα) = {U : set (α × β) | ∃ (A : set α) (H : A ∈ Bα), U = @set.prod α β A (@set.univ β)} := begin ext,split;intros A1A, { simp at A1A, cases A1A with A A1A, cases A1A with A1B A1C, subst x, split, simp, split, apply A1B, unfold set.prod, simp, refl, }, { simp at A1A, cases A1A with A A1A, cases A1A with A1B A1C, subst x, split, split, apply A1B, unfold set.prod, simp, refl } end --Novel, used. lemma set.preimage_snd_def {α β:Type*} {Bβ:set (set β)}: (@set.preimage (α × β) β (@prod.snd α β) '' Bβ) = {U : set (α × β) | ∃ (B : set β) (H : B ∈ Bβ), U = @set.prod α β (@set.univ α) B} := begin ext,split;intros A1A, { simp at A1A, cases A1A with A A1A, cases A1A with A1B A1C, subst x, split, simp, split, apply A1B, unfold set.prod, simp, refl, }, { simp at A1A, cases A1A with A A1A, cases A1A with A1B A1C, subst x, split, split, apply A1B, unfold set.prod, simp, refl } end --Novel, used. --Similar, but not identical to set.preimage_sUnion. --Could probably replace it. lemma set.preimage_sUnion' {α β:Type*} (f:α → β) (T:set (set β)): (f ⁻¹' ⋃₀ T)=⋃₀ (set.image (set.preimage f) T) := begin rw set.preimage_sUnion, ext,split;intros A1;simp;simp at A1;apply A1, end --Novel, used. lemma set.prod_sUnion_right {α:Type*} (A:set α) {β:Type*} (B:set (set β)): (set.prod A (⋃₀ B)) = ⋃₀ {C:set (α× β)|∃ b∈ B, C=(set.prod A b)} := begin ext,split;intro A1, { cases A1 with A2 A3, cases A3 with b A4, cases A4 with A5 A6, simp, apply exists.intro (set.prod A b), split, { apply exists.intro b, split, exact A5, refl, }, { split;assumption, } }, { cases A1 with Ab A2, cases A2 with A3 A4, cases A3 with b A5, cases A5 with A6 A7, subst Ab, cases A4 with A8 A9, split, { exact A8, }, { simp, apply exists.intro b, split;assumption, } } end --Novel, used. lemma set.prod_sUnion_left {α:Type*} (A:set (set α)) {β:Type*} (B:set β): (set.prod (⋃₀ A) B) = ⋃₀ {C:set (α× β)|∃ a∈ A, C=(set.prod a B)} := begin ext,split;intro A1, { cases A1 with A2 A3, cases A2 with a A4, cases A4 with A5 A6, simp, apply exists.intro (set.prod a B), split, { apply exists.intro a, split, exact A5, refl, }, { split;assumption, } }, { cases A1 with Ab A2, cases A2 with A3 A4, cases A3 with b A5, cases A5 with A6 A7, subst Ab, cases A4 with A8 A9, split, { simp, apply exists.intro b, split;assumption, }, { exact A9, } } end --Novel, used. lemma union_trichotomy {β:Type*} [decidable_eq β] {b:β} {S S2:finset β}: b∈ (S ∪ S2) ↔ ( ((b∈ S) ∧ (b∉ S2)) ∨ ((b∈ S) ∧ (b ∈ S2)) ∨ ((b∉ S) ∧ (b∈ S2))) := begin have B1:(b∈ S)∨ (b∉ S), { apply classical.em, }, have B2:(b∈ S2)∨ (b∉ S2), { apply classical.em, }, split;intro A1, { simp at A1, cases A1, { cases B2, { right,left, apply and.intro A1 B2, }, { left, apply and.intro A1 B2, }, }, { right, cases B1, { left, apply and.intro B1 A1, }, { right, apply and.intro B1 A1, }, }, }, { simp, cases A1, { left, apply A1.left, }, cases A1, { left, apply A1.left, }, { right, apply A1.right, }, }, end /- There are theorems about disjoint properties, but not about disjoint sets. It would be good to figure out a long-term strategy of dealing with disjointedness, as it is pervasive through measure theory and probability theory. Disjoint is defined on lattices, and sets are basically the canonical complete lattice. However, the relationship between complementary sets and disjointedness is lost, as complementarity doesn't exist in a generic complete lattice. -/ lemma set.disjoint.symm {α:Type*} {A B:set α}:disjoint A B → disjoint B A := begin rw set.disjoint_iff_inter_eq_empty, rw set.disjoint_iff_inter_eq_empty, rw set.inter_comm, simp, end --Unused, but there are parallels in mathlib for finset and list. lemma set.disjoint_comm {α:Type*} {A B:set α}:disjoint A B ↔ disjoint B A := begin split;intros A1;apply set.disjoint.symm A1, end lemma set.disjoint_inter_compl {α:Type*} (A B C:set α):disjoint (A ∩ B) (C∩ Bᶜ) := begin apply set.disjoint_of_subset_left (set.inter_subset_right A B), apply set.disjoint_of_subset_right (set.inter_subset_right C Bᶜ), apply set.disjoint_compl_right, end --In general, disjoint A C → disjoint (A ⊓ B) C lemma set.disjoint_inter_left {α:Type*} {A B C:set α}: disjoint A C → disjoint (A ∩ B) (C) := begin intros A1, apply set.disjoint_of_subset_left _ A1, apply set.inter_subset_left, end --In general, disjoint A C → disjoint A (B ⊓ C) lemma set.disjoint_inter_right {α:Type*} {A B C:set α}: disjoint A C → disjoint A (B ∩ C) := begin intros A1, apply set.disjoint_of_subset_right _ A1, apply set.inter_subset_right, end /- The connection between Union and supremum comes in useful in measure theory. The next three theorems do this directly. -/ lemma set.le_Union {α:Type*} {f:ℕ → set α} {n:ℕ}: f n ≤ set.Union f := begin rw set.le_eq_subset, rw set.subset_def, intros a A3, simp, apply exists.intro n A3, end lemma set.Union_le {α:Type*} {f:ℕ → set α} {S:set α}: (∀ i, f i ≤ S) → set.Union f ≤ S := begin intro A1, rw set.le_eq_subset, rw set.subset_def, intros x A2, simp at A2, cases A2 with n A2, apply A1 n A2, end lemma supr_eq_Union {α:Type*} {f:ℕ → set α}: supr f = set.Union f := begin apply le_antisymm, { apply @supr_le (set α) _ _, intro i, apply set.le_Union, }, { apply set.Union_le, intros n, apply @le_supr (set α) _ _, }, end lemma empty_of_subset_empty {α:Type*} (X:set α): X ⊆ ∅ → X = ∅ := begin have A1:(∅:set α) = ⊥ := rfl, rw A1, rw ← set.le_eq_subset, intro A2, rw le_bot_iff at A2, apply A2, end lemma subset_empty_iff {α:Type*} (X:set α): X ⊆ ∅ ↔ X = ∅ := begin have A1:(∅:set α) = ⊥ := rfl, rw A1, rw ← set.le_eq_subset, apply le_bot_iff, end lemma set.eq_univ_iff_univ_subset {α:Type*} {S:set α}: set.univ ⊆ S ↔ S = set.univ := begin have A1:@set.univ α = ⊤ := rfl, rw A1, rw ← set.le_eq_subset, apply top_le_iff, end lemma preimage_if {α β:Type*} {E:set α} {D:decidable_pred E} {X Y:α → β} {S:set β}: set.preimage (λ a:α, if (E a) then (X a) else (Y a)) S = (E ∩ set.preimage X S) ∪ (Eᶜ ∩ set.preimage Y S) := begin ext a;split;intros A1, { cases (classical.em (a∈ E)) with A2 A2, { rw set.mem_preimage at A1, rw if_pos at A1, apply set.mem_union_left, apply set.mem_inter A2, rw set.mem_preimage, apply A1, rw set.mem_def at A2, apply A2, }, { rw set.mem_preimage at A1, rw if_neg at A1, apply set.mem_union_right, apply set.mem_inter, apply set.mem_compl, apply A2, rw set.mem_preimage, apply A1, rw set.mem_def at A2, apply A2, }, }, { rw set.mem_preimage, rw set.mem_union at A1, cases A1 with A1 A1; rw set.mem_inter_eq at A1; cases A1 with A2 A3; rw set.mem_preimage at A3; rw set.mem_def at A2, { rw if_pos, apply A3, apply A2, }, { rw if_neg, apply A3, apply A2, }, }, end
00ea703b5045ef11f48372f462d3b87c5bd30dee
94e33a31faa76775069b071adea97e86e218a8ee
/src/topology/metric_space/metrizable.lean
70dcfdee9c335aee5075e21e5f7eda46df799515
[ "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
11,141
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.urysohns_lemma import topology.continuous_function.bounded /-! # Metrizability of a T₃ topological space with second countable topology In this file we define metrizable topological spaces, i.e., topological spaces for which there exists a metric space structure that generates the same topology. We also show that a T₃ topological space with second countable topology `X` is metrizable. First we prove that `X` can be embedded into `l^∞`, then use this embedding to pull back the metric space structure. -/ open set filter metric open_locale bounded_continuous_function filter topological_space namespace topological_space variables {ι X Y : Type*} {π : ι → Type*} [topological_space X] [topological_space Y] [fintype ι] [Π i, topological_space (π i)] /-- A topological space is *pseudo metrizable* if there exists a pseudo metric space structure compatible with the topology. To endow such a space with a compatible distance, use `letI : pseudo_metric_space X := topological_space.pseudo_metrizable_space_pseudo_metric X`. -/ class pseudo_metrizable_space (X : Type*) [t : topological_space X] : Prop := (exists_pseudo_metric : ∃ (m : pseudo_metric_space X), m.to_uniform_space.to_topological_space = t) @[priority 100] instance _root_.pseudo_metric_space.to_pseudo_metrizable_space {X : Type*} [m : pseudo_metric_space X] : pseudo_metrizable_space X := ⟨⟨m, rfl⟩⟩ /-- Construct on a metrizable space a metric compatible with the topology. -/ noncomputable def pseudo_metrizable_space_pseudo_metric (X : Type*) [topological_space X] [h : pseudo_metrizable_space X] : pseudo_metric_space X := h.exists_pseudo_metric.some.replace_topology h.exists_pseudo_metric.some_spec.symm instance pseudo_metrizable_space_prod [pseudo_metrizable_space X] [pseudo_metrizable_space Y] : pseudo_metrizable_space (X × Y) := begin letI : pseudo_metric_space X := pseudo_metrizable_space_pseudo_metric X, letI : pseudo_metric_space Y := pseudo_metrizable_space_pseudo_metric Y, apply_instance end /-- Given an inducing map of a topological space into a pseudo metrizable space, the source space is also pseudo metrizable. -/ lemma _root_.inducing.pseudo_metrizable_space [pseudo_metrizable_space Y] {f : X → Y} (hf : inducing f) : pseudo_metrizable_space X := begin letI : pseudo_metric_space Y := pseudo_metrizable_space_pseudo_metric Y, exact ⟨⟨hf.comap_pseudo_metric_space, rfl⟩⟩ end instance pseudo_metrizable_space.subtype [pseudo_metrizable_space X] (s : set X) : pseudo_metrizable_space s := inducing_coe.pseudo_metrizable_space instance pseudo_metrizable_space_pi [Π i, pseudo_metrizable_space (π i)] : pseudo_metrizable_space (Π i, π i) := by { letI := λ i, pseudo_metrizable_space_pseudo_metric (π i), apply_instance } /-- A topological space is metrizable if there exists a metric space structure compatible with the topology. To endow such a space with a compatible distance, use `letI : metric_space X := topological_space.metrizable_space_metric X` -/ class metrizable_space (X : Type*) [t : topological_space X] : Prop := (exists_metric : ∃ (m : metric_space X), m.to_uniform_space.to_topological_space = t) @[priority 100] instance _root_.metric_space.to_metrizable_space {X : Type*} [m : metric_space X] : metrizable_space X := ⟨⟨m, rfl⟩⟩ @[priority 100] instance metrizable_space.to_pseudo_metrizable_space [h : metrizable_space X] : pseudo_metrizable_space X := ⟨let ⟨m, hm⟩ := h.1 in ⟨m.to_pseudo_metric_space, hm⟩⟩ /-- Construct on a metrizable space a metric compatible with the topology. -/ noncomputable def metrizable_space_metric (X : Type*) [topological_space X] [h : metrizable_space X] : metric_space X := h.exists_metric.some.replace_topology h.exists_metric.some_spec.symm @[priority 100] instance t2_space_of_metrizable_space [metrizable_space X] : t2_space X := by { letI : metric_space X := metrizable_space_metric X, apply_instance } instance metrizable_space_prod [metrizable_space X] [metrizable_space Y] : metrizable_space (X × Y) := begin letI : metric_space X := metrizable_space_metric X, letI : metric_space Y := metrizable_space_metric Y, apply_instance end /-- Given an embedding of a topological space into a metrizable space, the source space is also metrizable. -/ lemma _root_.embedding.metrizable_space [metrizable_space Y] {f : X → Y} (hf : embedding f) : metrizable_space X := begin letI : metric_space Y := metrizable_space_metric Y, exact ⟨⟨hf.comap_metric_space f, rfl⟩⟩ end instance metrizable_space.subtype [metrizable_space X] (s : set X) : metrizable_space s := embedding_subtype_coe.metrizable_space instance metrizable_space_pi [Π i, metrizable_space (π i)] : metrizable_space (Π i, π i) := by { letI := λ i, metrizable_space_metric (π i), apply_instance } variables (X) [t3_space X] [second_countable_topology X] /-- A T₃ topological space with second countable topology can be embedded into `l^∞ = ℕ →ᵇ ℝ`. -/ lemma exists_embedding_l_infty : ∃ f : X → (ℕ →ᵇ ℝ), embedding f := begin haveI : normal_space X := normal_space_of_t3_second_countable X, -- Choose a countable basis, and consider the set `s` of pairs of set `(U, V)` such that `U ∈ B`, -- `V ∈ B`, and `closure U ⊆ V`. rcases exists_countable_basis X with ⟨B, hBc, -, hB⟩, set s : set (set X × set X) := {UV ∈ B ×ˢ B| closure UV.1 ⊆ UV.2}, -- `s` is a countable set. haveI : encodable s := ((hBc.prod hBc).mono (inter_subset_left _ _)).to_encodable, -- We don't have the space of bounded (possibly discontinuous) functions, so we equip `s` -- with the discrete topology and deal with `s →ᵇ ℝ` instead. letI : topological_space s := ⊥, haveI : discrete_topology s := ⟨rfl⟩, suffices : ∃ f : X → (s →ᵇ ℝ), embedding f, { rcases this with ⟨f, hf⟩, exact ⟨λ x, (f x).extend (encodable.encode' s) 0, (bounded_continuous_function.isometry_extend (encodable.encode' s) (0 : ℕ →ᵇ ℝ)).embedding.comp hf⟩ }, have hd : ∀ UV : s, disjoint (closure UV.1.1) (UV.1.2ᶜ) := λ UV, disjoint_compl_right.mono_right (compl_subset_compl.2 UV.2.2), -- Choose a sequence of `εₙ > 0`, `n : s`, that is bounded above by `1` and tends to zero -- along the `cofinite` filter. obtain ⟨ε, ε01, hε⟩ : ∃ ε : s → ℝ, (∀ UV, ε UV ∈ Ioc (0 : ℝ) 1) ∧ tendsto ε cofinite (𝓝 0), { rcases pos_sum_of_encodable zero_lt_one s with ⟨ε, ε0, c, hεc, hc1⟩, refine ⟨ε, λ UV, ⟨ε0 UV, _⟩, hεc.summable.tendsto_cofinite_zero⟩, exact (le_has_sum hεc UV $ λ _ _, (ε0 _).le).trans hc1 }, /- For each `UV = (U, V) ∈ s` we use Urysohn's lemma to choose a function `f UV` that is equal to zero on `U` and is equal to `ε UV` on the complement to `V`. -/ have : ∀ UV : s, ∃ f : C(X, ℝ), eq_on f 0 UV.1.1 ∧ eq_on f (λ _, ε UV) UV.1.2ᶜ ∧ ∀ x, f x ∈ Icc 0 (ε UV), { intro UV, rcases exists_continuous_zero_one_of_closed is_closed_closure (hB.is_open UV.2.1.2).is_closed_compl (hd UV) with ⟨f, hf₀, hf₁, hf01⟩, exact ⟨ε UV • f, λ x hx, by simp [hf₀ (subset_closure hx)], λ x hx, by simp [hf₁ hx], λ x, ⟨mul_nonneg (ε01 _).1.le (hf01 _).1, mul_le_of_le_one_right (ε01 _).1.le (hf01 _).2⟩⟩ }, choose f hf0 hfε hf0ε, have hf01 : ∀ UV x, f UV x ∈ Icc (0 : ℝ) 1, from λ UV x, Icc_subset_Icc_right (ε01 _).2 (hf0ε _ _), /- The embedding is given by `F x UV = f UV x`. -/ set F : X → s →ᵇ ℝ := λ x, ⟨⟨λ UV, f UV x, continuous_of_discrete_topology⟩, 1, λ UV₁ UV₂, real.dist_le_of_mem_Icc_01 (hf01 _ _) (hf01 _ _)⟩, have hF : ∀ x UV, F x UV = f UV x := λ _ _, rfl, refine ⟨F, embedding.mk' _ (λ x y hxy, _) (λ x, le_antisymm _ _)⟩, { /- First we prove that `F` is injective. Indeed, if `F x = F y` and `x ≠ y`, then we can find `(U, V) ∈ s` such that `x ∈ U` and `y ∉ V`, hence `F x UV = 0 ≠ ε UV = F y UV`. -/ refine not_not.1 (λ Hne, _), -- `by_contra Hne` timeouts rcases hB.mem_nhds_iff.1 (is_open_ne.mem_nhds Hne) with ⟨V, hVB, hxV, hVy⟩, rcases hB.exists_closure_subset (hB.mem_nhds hVB hxV) with ⟨U, hUB, hxU, hUV⟩, set UV : ↥s := ⟨(U, V), ⟨hUB, hVB⟩, hUV⟩, apply (ε01 UV).1.ne, calc (0 : ℝ) = F x UV : (hf0 UV hxU).symm ... = F y UV : by rw hxy ... = ε UV : hfε UV (λ h : y ∈ V, hVy h rfl) }, { /- Now we prove that each neighborhood `V` of `x : X` include a preimage of a neighborhood of `F x` under `F`. Without loss of generality, `V` belongs to `B`. Choose `U ∈ B` such that `x ∈ V` and `closure V ⊆ U`. Then the preimage of the `(ε (U, V))`-neighborhood of `F x` is included by `V`. -/ refine ((nhds_basis_ball.comap _).le_basis_iff hB.nhds_has_basis).2 _, rintro V ⟨hVB, hxV⟩, rcases hB.exists_closure_subset (hB.mem_nhds hVB hxV) with ⟨U, hUB, hxU, hUV⟩, set UV : ↥s := ⟨(U, V), ⟨hUB, hVB⟩, hUV⟩, refine ⟨ε UV, (ε01 UV).1, λ y (hy : dist (F y) (F x) < ε UV), _⟩, replace hy : dist (F y UV) (F x UV) < ε UV, from (bounded_continuous_function.dist_coe_le_dist _).trans_lt hy, contrapose! hy, rw [hF, hF, hfε UV hy, hf0 UV hxU, pi.zero_apply, dist_zero_right], exact le_abs_self _ }, { /- Finally, we prove that `F` is continuous. Given `δ > 0`, consider the set `T` of `(U, V) ∈ s` such that `ε (U, V) ≥ δ`. Since `ε` tends to zero, `T` is finite. Since each `f` is continuous, we can choose a neighborhood such that `dist (F y (U, V)) (F x (U, V)) ≤ δ` for any `(U, V) ∈ T`. For `(U, V) ∉ T`, the same inequality is true because both `F y (U, V)` and `F x (U, V)` belong to the interval `[0, ε (U, V)]`. -/ refine (nhds_basis_closed_ball.comap _).ge_iff.2 (λ δ δ0, _), have h_fin : {UV : s | δ ≤ ε UV}.finite, by simpa only [← not_lt] using hε (gt_mem_nhds δ0), have : ∀ᶠ y in 𝓝 x, ∀ UV, δ ≤ ε UV → dist (F y UV) (F x UV) ≤ δ, { refine (eventually_all_finite h_fin).2 (λ UV hUV, _), exact (f UV).continuous.tendsto x (closed_ball_mem_nhds _ δ0) }, refine this.mono (λ y hy, (bounded_continuous_function.dist_le δ0.le).2 $ λ UV, _), cases le_total δ (ε UV) with hle hle, exacts [hy _ hle, (real.dist_le_of_mem_Icc (hf0ε _ _) (hf0ε _ _)).trans (by rwa sub_zero)] } end /-- *Urysohn's metrization theorem* (Tychonoff's version): a T₃ topological space with second countable topology `X` is metrizable, i.e., there exists a metric space structure that generates the same topology. -/ lemma metrizable_space_of_t3_second_countable : metrizable_space X := let ⟨f, hf⟩ := exists_embedding_l_infty X in hf.metrizable_space instance : metrizable_space ennreal := metrizable_space_of_t3_second_countable ennreal end topological_space
a348c565372b6ed059814199565fe41b67fa20b4
82b86ba2ae0d5aed0f01f49c46db5afec0eb2bd7
/src/Lean/Level.lean
777174ef6c3395566b9f57344256fb9ce8f755a0
[ "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
16,841
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Std.Data.HashMap import Std.Data.HashSet import Std.Data.PersistentHashMap import Std.Data.PersistentHashSet import Lean.Data.Name import Lean.Data.Format def Nat.imax (n m : Nat) : Nat := if m = 0 then 0 else Nat.max n m namespace Lean /-- Cached hash code, cached results, and other data for `Level`. hash : 32-bits hasMVar : 1-bit hasParam : 1-bit depth : 24-bits -/ def Level.Data := UInt64 instance : Inhabited Level.Data := inferInstanceAs (Inhabited UInt64) def Level.Data.hash (c : Level.Data) : USize := c.toUInt32.toUSize instance : BEq Level.Data := ⟨fun (a b : UInt64) => a == b⟩ def Level.Data.depth (c : Level.Data) : UInt32 := (c.shiftRight 40).toUInt32 def Level.Data.hasMVar (c : Level.Data) : Bool := ((c.shiftRight 32).land 1) == 1 def Level.Data.hasParam (c : Level.Data) : Bool := ((c.shiftRight 33).land 1) == 1 def Level.mkData (h : USize) (depth : Nat) (hasMVar hasParam : Bool) : Level.Data := if depth > Nat.pow 2 24 - 1 then panic! "universe level depth is too big" else let r : UInt64 := h.toUInt32.toUInt64 + hasMVar.toUInt64.shiftLeft 32 + hasParam.toUInt64.shiftLeft 33 + depth.toUInt64.shiftLeft 40 r open Level inductive Level | zero : Data → Level | succ : Level → Data → Level | max : Level → Level → Data → Level | imax : Level → Level → Data → Level | param : Name → Data → Level | mvar : Name → Data → Level namespace Level @[inline] def data : Level → Data | zero d => d | mvar _ d => d | param _ d => d | succ _ d => d | max _ _ d => d | imax _ _ d => d protected def hash (u : Level) : USize := u.data.hash instance : Hashable Level := ⟨Level.hash⟩ def depth (u : Level) : Nat := u.data.depth.toNat def hasMVar (u : Level) : Bool := u.data.hasMVar def hasParam (u : Level) : Bool := u.data.hasParam @[export lean_level_hash] def hashEx : Level → USize := Level.hash @[export lean_level_has_mvar] def hasMVarEx : Level → Bool := hasMVar @[export lean_level_has_param] def hasParamEx : Level → Bool := hasParam @[export lean_level_depth] def depthEx (u : Level) : UInt32 := u.data.depth end Level def levelZero := Level.zero $ mkData 2221 0 false false def mkLevelMVar (mvarId : Name) := Level.mvar mvarId $ mkData (mixHash 2237 $ hash mvarId) 0 true false def mkLevelParam (name : Name) := Level.param name $ mkData (mixHash 2239 $ hash name) 0 false true def mkLevelSucc (u : Level) := Level.succ u $ mkData (mixHash 2243 $ hash u) (u.depth + 1) u.hasMVar u.hasParam def mkLevelMax (u v : Level) := Level.max u v $ mkData (mixHash 2251 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1) (u.hasMVar || v.hasMVar) (u.hasParam || v.hasParam) def mkLevelIMax (u v : Level) := Level.imax u v $ mkData (mixHash 2267 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1) (u.hasMVar || v.hasMVar) (u.hasParam || v.hasParam) def levelOne := mkLevelSucc levelZero @[export lean_level_mk_zero] def mkLevelZeroEx : Unit → Level := fun _ => levelZero @[export lean_level_mk_succ] def mkLevelSuccEx : Level → Level := mkLevelSucc @[export lean_level_mk_mvar] def mkLevelMVarEx : Name → Level := mkLevelMVar @[export lean_level_mk_param] def mkLevelParamEx : Name → Level := mkLevelParam @[export lean_level_mk_max] def mkLevelMaxEx : Level → Level → Level := mkLevelMax @[export lean_level_mk_imax] def mkLevelIMaxEx : Level → Level → Level := mkLevelIMax namespace Level instance : Inhabited Level := ⟨levelZero⟩ def isZero : Level → Bool | zero _ => true | _ => false def isSucc : Level → Bool | succ .. => true | _ => false def isMax : Level → Bool | max .. => true | _ => false def isIMax : Level → Bool | imax .. => true | _ => false def isMaxIMax : Level → Bool | max .. => true | imax .. => true | _ => false def isParam : Level → Bool | param .. => true | _ => false def isMVar : Level → Bool | mvar .. => true | _ => false def mvarId! : Level → Name | mvar mvarId _ => mvarId | _ => panic! "metavariable expected" /-- If result is true, then forall assignments `A` which assigns all parameters and metavariables occuring in `l`, `l[A] != zero` -/ def isNeverZero : Level → Bool | zero _ => false | param .. => false | mvar .. => false | succ .. => true | max l₁ l₂ _ => isNeverZero l₁ || isNeverZero l₂ | imax l₁ l₂ _ => isNeverZero l₂ def ofNat : Nat → Level | 0 => levelZero | n+1 => mkLevelSucc (ofNat n) def addOffsetAux : Nat → Level → Level | 0, u => u | (n+1), u => addOffsetAux n (mkLevelSucc u) def addOffset (u : Level) (n : Nat) : Level := u.addOffsetAux n def isExplicit : Level → Bool | zero _ => true | succ u _ => !u.hasMVar && !u.hasParam && isExplicit u | _ => false def getOffsetAux : Level → Nat → Nat | succ u _, r => getOffsetAux u (r+1) | _, r => r def getOffset (lvl : Level) : Nat := getOffsetAux lvl 0 def getLevelOffset : Level → Level | succ u _ => getLevelOffset u | u => u def toNat (lvl : Level) : Option Nat := match lvl.getLevelOffset with | zero _ => lvl.getOffset | _ => none @[extern "lean_level_eq"] protected constant beq (a : @& Level) (b : @& Level) : Bool := arbitrary _ instance : BEq Level := ⟨Level.beq⟩ /-- `occurs u l` return `true` iff `u` occurs in `l`. -/ def occurs : Level → Level → Bool | u, v@(succ v₁ _) => u == v || occurs u v₁ | u, v@(max v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂ | u, v@(imax v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂ | u, v => u == v def ctorToNat : Level → Nat | zero .. => 0 | param .. => 1 | mvar .. => 2 | succ .. => 3 | max .. => 4 | imax .. => 5 /- TODO: use well founded recursion. -/ partial def normLtAux : Level → Nat → Level → Nat → Bool | succ l₁ _, k₁, l₂, k₂ => normLtAux l₁ (k₁+1) l₂ k₂ | l₁, k₁, succ l₂ _, k₂ => normLtAux l₁ k₁ l₂ (k₂+1) | l₁@(max l₁₁ l₁₂ _), k₁, l₂@(max l₂₁ l₂₂ _), k₂ => if l₁ == l₂ then k₁ < k₂ else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0 else normLtAux l₁₂ 0 l₂₂ 0 | l₁@(imax l₁₁ l₁₂ _), k₁, l₂@(imax l₂₁ l₂₂ _), k₂ => if l₁ == l₂ then k₁ < k₂ else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0 else normLtAux l₁₂ 0 l₂₂ 0 | param n₁ _, k₁, param n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.lt n₁ n₂ -- use Name.lt because it is lexicographical | mvar n₁ _, k₁, mvar n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.quickLt n₁ n₂ -- metavariables are temporary, the actual order doesn't matter | l₁, k₁, l₂, k₂ => if l₁ == l₂ then k₁ < k₂ else ctorToNat l₁ < ctorToNat l₂ /-- A total order on level expressions that has the following properties - `succ l` is an immediate successor of `l`. - `zero` is the minimal element. This total order is used in the normalization procedure. -/ def normLt (l₁ l₂ : Level) : Bool := normLtAux l₁ 0 l₂ 0 private def isAlreadyNormalizedCheap : Level → Bool | zero _ => true | param _ _ => true | mvar _ _ => true | succ u _ => isAlreadyNormalizedCheap u | _ => false /- Auxiliary function used at `normalize` -/ private def mkIMaxAux : Level → Level → Level | _, u@(zero _) => u | zero _, u => u | u₁, u₂ => if u₁ == u₂ then u₁ else mkLevelIMax u₁ u₂ /- Auxiliary function used at `normalize` -/ @[specialize] private partial def getMaxArgsAux (normalize : Level → Level) : Level → Bool → Array Level → Array Level | max l₁ l₂ _, alreadyNormalized, lvls => getMaxArgsAux normalize l₂ alreadyNormalized (getMaxArgsAux normalize l₁ alreadyNormalized lvls) | l, false, lvls => getMaxArgsAux normalize (normalize l) true lvls | l, true, lvls => lvls.push l private def accMax (result : Level) (prev : Level) (offset : Nat) : Level := if result.isZero then prev.addOffset offset else mkLevelMax result (prev.addOffset offset) /- Auxiliary function used at `normalize`. Remarks: - `lvls` are sorted using `normLt` - `extraK` is the outter offset of the `max` term. We will push it inside. - `i` is the current array index - `prev + prevK` is the "previous" level that has not been added to `result` yet. - `result` is the accumulator -/ private partial def mkMaxAux (lvls : Array Level) (extraK : Nat) (i : Nat) (prev : Level) (prevK : Nat) (result : Level) : Level := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ let curr := lvl.getLevelOffset let currK := lvl.getOffset if curr == prev then mkMaxAux lvls extraK (i+1) curr currK result else mkMaxAux lvls extraK (i+1) curr currK (accMax result prev (extraK + prevK)) else accMax result prev (extraK + prevK) /- Auxiliary function for `normalize`. It assumes `lvls` has been sorted using `normLt`. It finds the first position that is not an explicit universe. -/ private partial def skipExplicit (lvls : Array Level) (i : Nat) : Nat := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ if lvl.getLevelOffset.isZero then skipExplicit lvls (i+1) else i else i /- Auxiliary function for `normalize`. `maxExplicit` is the maximum explicit universe level at `lvls`. Return true if it finds a level with offset ≥ maxExplicit. `i` starts at the first non explict level. It assumes `lvls` has been sorted using `normLt`. -/ private partial def isExplicitSubsumedAux (lvls : Array Level) (maxExplicit : Nat) (i : Nat) : Bool := if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩ if lvl.getOffset ≥ maxExplicit then true else isExplicitSubsumedAux lvls maxExplicit (i+1) else false /- Auxiliary function for `normalize`. See `isExplicitSubsumedAux` -/ private def isExplicitSubsumed (lvls : Array Level) (firstNonExplicit : Nat) : Bool := if firstNonExplicit == 0 then false else let max := (lvls.get! (firstNonExplicit - 1)).getOffset; isExplicitSubsumedAux lvls max firstNonExplicit partial def normalize (l : Level) : Level := if isAlreadyNormalizedCheap l then l else let k := l.getOffset let u := l.getLevelOffset match u with | max l₁ l₂ _ => let lvls := getMaxArgsAux normalize l₁ false #[] let lvls := getMaxArgsAux normalize l₂ false lvls let lvls := lvls.qsort normLt let firstNonExplicit := skipExplicit lvls 0 let i := if isExplicitSubsumed lvls firstNonExplicit then firstNonExplicit else firstNonExplicit - 1 let lvl₁ := lvls[i] let prev := lvl₁.getLevelOffset let prevK := lvl₁.getOffset mkMaxAux lvls k (i+1) prev prevK levelZero | imax l₁ l₂ _ => if l₂.isNeverZero then addOffset (normalize (mkLevelMax l₁ l₂)) k else let l₁ := normalize l₁ let l₂ := normalize l₂ addOffset (mkIMaxAux l₁ l₂) k | _ => unreachable! /- Return true if `u` and `v` denote the same level. Check is currently incomplete. -/ def isEquiv (u v : Level) : Bool := u == v || u.normalize == v.normalize /-- Reduce (if possible) universe level by 1 -/ def dec : Level → Option Level | zero _ => none | param _ _ => none | mvar _ _ => none | succ l _ => l | max l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂ /- Remark: `mkLevelMax` in the following line is not a typo. If `dec l₂` succeeds, then `imax l₁ l₂` is equivalent to `max l₁ l₂`. -/ | imax l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂ /- Level to Format -/ namespace LevelToFormat inductive Result | leaf : Format → Result | num : Nat → Result | offset : Result → Nat → Result | maxNode : List Result → Result | imaxNode : List Result → Result def Result.succ : Result → Result | Result.offset f k => Result.offset f (k+1) | Result.num k => Result.num (k+1) | f => Result.offset f 1 def Result.max : Result → Result → Result | f, Result.maxNode Fs => Result.maxNode (f::Fs) | f₁, f₂ => Result.maxNode [f₁, f₂] def Result.imax : Result → Result → Result | f, Result.imaxNode Fs => Result.imaxNode (f::Fs) | f₁, f₂ => Result.imaxNode [f₁, f₂] def parenIfFalse : Format → Bool → Format | f, true => f | f, false => f.paren @[specialize] private def formatLst (fmt : Result → Format) : List Result → Format | [] => Format.nil | r::rs => Format.line ++ fmt r ++ formatLst fmt rs partial def Result.format : Result → Bool → Format | Result.leaf f, _ => f | Result.num k, _ => toString k | Result.offset f 0, r => format f r | Result.offset f (k+1), r => let f' := format f false; parenIfFalse (f' ++ "+" ++ fmt (k+1)) r | Result.maxNode fs, r => parenIfFalse (Format.group $ "max" ++ formatLst (fun r => format r false) fs) r | Result.imaxNode fs, r => parenIfFalse (Format.group $ "imax" ++ formatLst (fun r => format r false) fs) r def toResult : Level → Result | zero _ => Result.num 0 | succ l _ => Result.succ (toResult l) | max l₁ l₂ _ => Result.max (toResult l₁) (toResult l₂) | imax l₁ l₂ _ => Result.imax (toResult l₁) (toResult l₂) | param n _ => Result.leaf (fmt n) | mvar n _ => let n := n.replacePrefix `_uniq `u; Result.leaf ("?" ++ fmt n) end LevelToFormat protected def format (l : Level) : Format := (LevelToFormat.toResult l).format true instance : ToFormat Level := ⟨Level.format⟩ instance : ToString Level := ⟨Format.pretty ∘ Level.format⟩ /- The update functions here are defined using C code. They will try to avoid allocating new values using pointer equality. The hypotheses `(h : e.is... = true)` are used to ensure Lean will not crash at runtime. The `update*!` functions are inlined and provide a convenient way of using the update proofs without providing proofs. Note that if they are used under a match-expression, the compiler will eliminate the double-match. -/ @[extern "lean_level_update_succ"] def updateSucc (lvl : Level) (newLvl : Level) (h : lvl.isSucc = true) : Level := mkLevelSucc newLvl @[inline] def updateSucc! (lvl : Level) (newLvl : Level) : Level := match lvl with | succ lvl d => updateSucc (succ lvl d) newLvl rfl | _ => panic! "succ level expected" @[extern "lean_level_update_max"] def updateMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isMax = true) : Level := mkLevelMax newLhs newRhs @[inline] def updateMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level := match lvl with | max lhs rhs d => updateMax (max lhs rhs d) newLhs newRhs rfl | _ => panic! "max level expected" @[extern "lean_level_update_imax"] def updateIMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isIMax = true) : Level := mkLevelIMax newLhs newRhs @[inline] def updateIMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level := match lvl with | imax lhs rhs d => updateIMax (imax lhs rhs d) newLhs newRhs rfl | _ => panic! "imax level expected" def mkNaryMax : List Level → Level | [] => levelZero | [u] => u | u::us => mkLevelMax u (mkNaryMax us) /- Level to Format -/ @[specialize] def instantiateParams (s : Name → Option Level) : Level → Level | u@(zero _) => u | u@(succ v _) => if u.hasParam then u.updateSucc! (instantiateParams s v) else u | u@(max v₁ v₂ _) => if u.hasParam then u.updateMax! (instantiateParams s v₁) (instantiateParams s v₂) else u | u@(imax v₁ v₂ _) => if u.hasParam then u.updateIMax! (instantiateParams s v₁) (instantiateParams s v₂) else u | u@(param n _) => match s n with | some u' => u' | none => u | u => u end Level open Std (HashMap HashSet PHashMap PHashSet) abbrev LevelMap (α : Type) := HashMap Level α abbrev PersistentLevelMap (α : Type) := PHashMap Level α abbrev LevelSet := HashSet Level abbrev PersistentLevelSet := PHashSet Level abbrev PLevelSet := PersistentLevelSet end Lean abbrev Nat.toLevel (n : Nat) : Lean.Level := Lean.Level.ofNat n
9f9db81c7d9a37d9d61934b39e19dbbd23d036e1
6b9565799c5c4b76978fc138a9aa7becedecd541
/library/standard/kernel.lean
61f7811f0725a19f6fdff2150a8af175ce783ed6
[ "Apache-2.0" ]
permissive
leanparikshit/libraries
657c9c09723523592b1d2021496f6f8041659130
cbe08140e93fbde4cc81ef212fc65fbe357c5b59
refs/heads/master
1,586,305,043,220
1,401,535,624,000
1,401,535,624,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
47,753
lean
-- Copyright (c) 2014 Microsoft Corporation. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Author: Leonardo de Moura import macros tactic universe U ≥ 1 definition TypeU := (Type U) -- create default rewrite rule set (* mk_rewrite_rule_set() *) variable Bool : Type -- Reflexivity for heterogeneous equality -- We use universe U+1 in heterogeneous equality axioms because we want to be able -- to state the equality between A and B of (Type U) axiom hrefl {A : (Type U+1)} (a : A) : a == a -- Homogeneous equality definition eq {A : (Type U)} (a b : A) := a == b infix 50 = : eq theorem refl {A : (Type U)} (a : A) : a = a := hrefl a theorem heq_eq {A : (Type U)} (a b : A) : (a == b) = (a = b) := refl (a == b) definition true : Bool := (λ x : Bool, x) = (λ x : Bool, x) theorem trivial : true := refl (λ x : Bool, x) set_opaque true true definition false : Bool := ∀ x : Bool, x alias ⊤ : true alias ⊥ : false definition not (a : Bool) := a → false notation 40 ¬ _ : not definition or (a b : Bool) := ∀ c : Bool, (a → c) → (b → c) → c infixr 30 || : or infixr 30 \/ : or infixr 30 ∨ : or definition and (a b : Bool) := ∀ c : Bool, (a → b → c) → c infixr 35 && : and infixr 35 /\ : and infixr 35 ∧ : and definition implies (a b : Bool) := a → b definition ne {A : (Type U)} (a b : A) := ¬ (a = b) infix 50 ≠ : ne theorem ne_intro {A : (Type U)} {a b : A} (H : (a = b) → false) : a ≠ b := H theorem ne_elim {A : (Type U)} {a b : A} (H1 : (a ≠ b)) (H2 : (a = b)) : false := H1 H2 theorem ne_irrefl {A : (Type U)} {a : A} (H : a ≠ a) : false := H (refl a) definition iff (a b : Bool) := a = b infixr 25 <-> : iff infixr 25 ↔ : iff theorem not_intro {a : Bool} (H : a → false) : ¬ a := H theorem not_elim {a : Bool} (H1: ¬ a) (H2 : a) : false := H1 H2 theorem absurd {a : Bool} (H1 : a) (H2 : ¬ a) : false := H2 H1 theorem false_elim (a : Bool) (H : false) : a := H a set_opaque false true theorem mt {a b : Bool} (H1 : a → b) (H2 : ¬ b) : ¬ a := assume Ha : a, absurd (H1 Ha) H2 theorem contrapos {a b : Bool} (H : a → b) : ¬ b → ¬ a := assume Hnb : ¬ b, mt H Hnb theorem absurd_elim {a : Bool} (b : Bool) (H1 : a) (H2 : ¬ a) : b := false_elim b (absurd H1 H2) theorem or_intro_left {a : Bool} (b : Bool) (H : a) : a ∨ b := take c : Bool, assume (H1 : a → c) (H2 : b → c), H1 H theorem or_intro_right (a : Bool) {b : Bool} (H : b) : a ∨ b := take c : Bool, assume (H1 : a → c) (H2 : b → c), H2 H theorem or_elim {a b c : Bool} (H1 : a ∨ b) (H2 : a → c) (H3 : b → c) : c := H1 c H2 H3 theorem resolve_right {a b : Bool} (H1 : a ∨ b) (H2 : ¬ a) : b := H1 b (assume Ha : a, absurd_elim b Ha H2) (assume Hb : b, Hb) theorem resolve_left {a b : Bool} (H1 : a ∨ b) (H2 : ¬ b) : a := H1 a (assume Ha : a, Ha) (assume Hb : b, absurd_elim a Hb H2) theorem or_flip {a b : Bool} (H : a ∨ b) : b ∨ a := take c : Bool, assume (H1 : b → c) (H2 : a → c), H c H2 H1 theorem and_intro {a b : Bool} (H1 : a) (H2 : b) : a ∧ b := take c : Bool, assume H : a → b → c, H H1 H2 theorem and_elim_left {a b : Bool} (H : a ∧ b) : a := H a (assume (Ha : a) (Hb : b), Ha) theorem and_elim_right {a b : Bool} (H : a ∧ b) : b := H b (assume (Ha : a) (Hb : b), Hb) axiom subst {A : (Type U)} {a b : A} {P : A → Bool} (H1 : P a) (H2 : a = b) : P b -- Alias for subst where we provide P explicitly, but keep A,a,b implicit theorem substp {A : (Type U)} {a b : A} (P : A → Bool) (H1 : P a) (H2 : a = b) : P b := subst H1 H2 theorem symm {A : (Type U)} {a b : A} (H : a = b) : b = a := subst (refl a) H theorem trans {A : (Type U)} {a b c : A} (H1 : a = b) (H2 : b = c) : a = c := subst H1 H2 theorem hcongr1 {A : (Type U)} {B : A → (Type U)} {f g : ∀ x, B x} (H : f = g) (a : A) : f a = g a := substp (fun h, f a = h a) (refl (f a)) H theorem congr1 {A B : (Type U)} {f g : A → B} (H : f = g) (a : A) : f a = g a := hcongr1 H a theorem congr2 {A B : (Type U)} {a b : A} (f : A → B) (H : a = b) : f a = f b := substp (fun x : A, f a = f x) (refl (f a)) H theorem congr {A B : (Type U)} {f g : A → B} {a b : A} (H1 : f = g) (H2 : a = b) : f a = g b := subst (congr2 f H2) (congr1 H1 b) theorem true_ne_false : ¬ true = false := assume H : true = false, subst trivial H theorem absurd_not_true (H : ¬ true) : false := absurd trivial H theorem not_false_trivial : ¬ false := assume H : false, H -- "equality modus pones" theorem eqmp {a b : Bool} (H1 : a = b) (H2 : a) : b := subst H2 H1 infixl 100 <| : eqmp infixl 100 ◂ : eqmp theorem eqmpr {a b : Bool} (H1 : a = b) (H2 : b) : a := (symm H1) ◂ H2 theorem imp_trans {a b c : Bool} (H1 : a → b) (H2 : b → c) : a → c := assume Ha, H2 (H1 Ha) theorem imp_eq_trans {a b c : Bool} (H1 : a → b) (H2 : b = c) : a → c := assume Ha, H2 ◂ (H1 Ha) theorem eq_imp_trans {a b c : Bool} (H1 : a = b) (H2 : b → c) : a → c := assume Ha, H2 (H1 ◂ Ha) theorem to_eq {A : (Type U)} {a b : A} (H : a == b) : a = b := (heq_eq a b) ◂ H theorem to_heq {A : (Type U)} {a b : A} (H : a = b) : a == b := (symm (heq_eq a b)) ◂ H theorem iff_elim_left {a b : Bool} (H : a ↔ b) : a → b := (λ Ha : a, eqmp H Ha) theorem iff_elim_right {a b : Bool} (H : a ↔ b) : b → a := (λ Hb : b, eqmpr H Hb) theorem ne_symm {A : (Type U)} {a b : A} (H : a ≠ b) : b ≠ a := assume H1 : b = a, H (symm H1) theorem eq_ne_trans {A : (Type U)} {a b c : A} (H1 : a = b) (H2 : b ≠ c) : a ≠ c := subst H2 (symm H1) theorem ne_eq_trans {A : (Type U)} {a b c : A} (H1 : a ≠ b) (H2 : b = c) : a ≠ c := subst H1 H2 theorem eqt_elim {a : Bool} (H : a = true) : a := (symm H) ◂ trivial theorem eqf_elim {a : Bool} (H : a = false) : ¬ a := not_intro (λ Ha : a, H ◂ Ha) theorem heqt_elim {a : Bool} (H : a == true) : a := eqt_elim (to_eq H) axiom boolcomplete (a : Bool) : a = true ∨ a = false theorem case (P : Bool → Bool) (H1 : P true) (H2 : P false) (a : Bool) : P a := or_elim (boolcomplete a) (assume Ht : a = true, subst H1 (symm Ht)) (assume Hf : a = false, subst H2 (symm Hf)) theorem em (a : Bool) : a ∨ ¬ a := or_elim (boolcomplete a) (assume Ht : a = true, or_intro_left (¬ a) (eqt_elim Ht)) (assume Hf : a = false, or_intro_right a (eqf_elim Hf)) theorem boolcomplete_swapped (a : Bool) : a = false ∨ a = true := case (λ x, x = false ∨ x = true) (or_intro_right (true = false) (refl true)) (or_intro_left (false = true) (refl false)) a theorem not_true : (¬ true) = false := let aux : ¬ (¬ true) = true := assume H : (¬ true) = true, absurd_not_true (subst trivial (symm H)) in resolve_right (boolcomplete (¬ true)) aux theorem not_false : (¬ false) = true := let aux : ¬ (¬ false) = false := assume H : (¬ false) = false, subst not_false_trivial H in resolve_right (boolcomplete_swapped (¬ false)) aux add_rewrite not_true not_false theorem not_not_eq (a : Bool) : (¬ ¬ a) = a := case (λ x, (¬ ¬ x) = x) (calc (¬ ¬ true) = (¬ false) : { not_true } ... = true : not_false) (calc (¬ ¬ false) = (¬ true) : { not_false } ... = false : not_true) a add_rewrite not_not_eq theorem not_ne {A : (Type U)} (a b : A) : ¬ (a ≠ b) ↔ a = b := not_not_eq (a = b) add_rewrite not_ne theorem not_ne_elim {A : (Type U)} {a b : A} (H : ¬ (a ≠ b)) : a = b := (not_ne a b) ◂ H theorem not_not_elim {a : Bool} (H : ¬ ¬ a) : a := (not_not_eq a) ◂ H theorem not_imp_elim_left {a b : Bool} (Hnab : ¬ (a → b)) : a := not_not_elim (show ¬ ¬ a, from assume Hna : ¬ a, absurd (assume Ha : a, absurd_elim b Ha Hna) Hnab) theorem not_imp_elimr {a b : Bool} (H : ¬ (a → b)) : ¬ b := assume Hb : b, absurd (assume Ha : a, Hb) H theorem boolext {a b : Bool} (Hab : a → b) (Hba : b → a) : a = b := or_elim (boolcomplete a) (λ Hat : a = true, or_elim (boolcomplete b) (λ Hbt : b = true, trans Hat (symm Hbt)) (λ Hbf : b = false, false_elim (a = b) (subst (Hab (eqt_elim Hat)) Hbf))) (λ Haf : a = false, or_elim (boolcomplete b) (λ Hbt : b = true, false_elim (a = b) (subst (Hba (eqt_elim Hbt)) Haf)) (λ Hbf : b = false, trans Haf (symm Hbf))) -- Another name for boolext theorem iff_intro {a b : Bool} (Hab : a → b) (Hba : b → a) : a ↔ b := boolext Hab Hba theorem eqt_intro {a : Bool} (H : a) : a = true := boolext (assume H1 : a, trivial) (assume H2 : true, H) theorem eqf_intro {a : Bool} (H : ¬ a) : a = false := boolext (assume H1 : a, absurd H1 H) (assume H2 : false, false_elim a H2) theorem by_contradiction {a : Bool} (H : ¬ a → false) : a := or_elim (em a) (λ H1 : a, H1) (λ H1 : ¬ a, false_elim a (H H1)) theorem ne_id {A : (Type U)} (a : A) : (a ≠ a) ↔ false := boolext (assume H, ne_irrefl H) (assume H, false_elim (a ≠ a) H) theorem eq_id {A : (Type U)} (a : A) : (a = a) ↔ true := eqt_intro (refl a) theorem iff_id (a : Bool) : (a ↔ a) ↔ true := eqt_intro (refl a) theorem heq_id (A : (Type U+1)) (a : A) : (a == a) ↔ true := eqt_intro (hrefl a) theorem ne_eq_iff_false {A : (Type U)} {a b : A} (H : a ≠ b) : a = b ↔ false := eqf_intro H theorem ne_to_not_eq {A : (Type U)} {a b : A} : a ≠ b ↔ ¬ a = b := refl (a ≠ b) add_rewrite eq_id iff_id ne_to_not_eq -- Remark: ordered rewriting + assoc + comm + left_comm sorts a term lexicographically theorem left_comm {A : (Type U)} {R : A -> A -> A} (comm : ∀ x y, R x y = R y x) (assoc : ∀ x y z, R (R x y) z = R x (R y z)) : ∀ x y z, R x (R y z) = R y (R x z) := take x y z, calc R x (R y z) = R (R x y) z : symm (assoc x y z) ... = R (R y x) z : { comm x y } ... = R y (R x z) : assoc y x z theorem right_comm {A : (Type U)} {R : A -> A -> A} (comm : ∀ x y, R x y = R y x) (assoc : ∀ x y z, R (R x y) z = R x (R y z)) : ∀ x y z, R (R x y) z = R (R x z) y := take x y z, calc R (R x y) z = R x (R y z) : assoc x y z ... = R x (R z y) : { comm y z } ... = R (R x z) y : symm (assoc x z y) theorem or_comm (a b : Bool) : (a ∨ b) = (b ∨ a) := boolext (assume H, or_elim H (λ H1, or_intro_right b H1) (λ H2, or_intro_left a H2)) (assume H, or_elim H (λ H1, or_intro_right a H1) (λ H2, or_intro_left b H2)) theorem or_assoc (a b c : Bool) : (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) := boolext (assume H : (a ∨ b) ∨ c, or_elim H (λ H1 : a ∨ b, or_elim H1 (λ Ha : a, or_intro_left (b ∨ c) Ha) (λ Hb : b, or_intro_right a (or_intro_left c Hb))) (λ Hc : c, or_intro_right a (or_intro_right b Hc))) (assume H : a ∨ (b ∨ c), or_elim H (λ Ha : a, (or_intro_left c (or_intro_left b Ha))) (λ H1 : b ∨ c, or_elim H1 (λ Hb : b, or_intro_left c (or_intro_right a Hb)) (λ Hc : c, or_intro_right (a ∨ b) Hc))) theorem or_id (a : Bool) : a ∨ a ↔ a := boolext (assume H, or_elim H (λ H1, H1) (λ H2, H2)) (assume H, or_intro_left a H) theorem or_false_left (a : Bool) : a ∨ false ↔ a := boolext (assume H, or_elim H (λ H1, H1) (λ H2, false_elim a H2)) (assume H, or_intro_left false H) theorem or_false_right (a : Bool) : false ∨ a ↔ a := trans (or_comm false a) (or_false_left a) theorem or_true_left (a : Bool) : true ∨ a ↔ true := boolext (assume H : true ∨ a, trivial) (assume H : true, or_intro_left a trivial) theorem or_true_right (a : Bool) : a ∨ true ↔ true := trans (or_comm a true) (or_true_left a) theorem or_tauto (a : Bool) : a ∨ ¬ a ↔ true := eqt_intro (em a) theorem or_left_comm (a b c : Bool) : a ∨ (b ∨ c) ↔ b ∨ (a ∨ c) --suggestion: rename to or_comm_left := left_comm or_comm or_assoc a b c theorem or_imp_or {a b c d : Bool} (H1 : a ∨ b) (H2 : a → c) (H3 : b → d) : c ∨ d := or_elim H1 (assume Ha : a, or_intro_left d (H2 Ha)) (assume Hb : b, or_intro_right c (H3 Hb)) add_rewrite or_comm or_assoc or_id or_false_left or_false_right or_true_left or_true_right or_tauto or_left_comm theorem and_comm (a b : Bool) : a ∧ b ↔ b ∧ a := boolext (assume H, and_intro (and_elim_right H) (and_elim_left H)) (assume H, and_intro (and_elim_right H) (and_elim_left H)) theorem and_id (a : Bool) : a ∧ a ↔ a := boolext (assume H, and_elim_left H) (assume H, and_intro H H) theorem and_assoc (a b c : Bool) : (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) := boolext (assume H, and_intro (and_elim_left (and_elim_left H)) (and_intro (and_elim_right (and_elim_left H)) (and_elim_right H))) (assume H, and_intro (and_intro (and_elim_left H) (and_elim_left (and_elim_right H))) (and_elim_right (and_elim_right H))) theorem and_true_right (a : Bool) : a ∧ true ↔ a := boolext (assume H : a ∧ true, and_elim_left H) (assume H : a, and_intro H trivial) theorem and_true_left (a : Bool) : true ∧ a ↔ a := trans (and_comm true a) (and_true_right a) theorem and_false_left (a : Bool) : a ∧ false ↔ false := boolext (assume H, and_elim_right H) (assume H, false_elim (a ∧ false) H) theorem and_false_right (a : Bool) : false ∧ a ↔ false := trans (and_comm false a) (and_false_left a) theorem and_absurd (a : Bool) : a ∧ ¬ a ↔ false := boolext (assume H, absurd (and_elim_left H) (and_elim_right H)) (assume H, false_elim (a ∧ ¬ a) H) theorem and_left_comm (a b c : Bool) : a ∧ (b ∧ c) ↔ b ∧ (a ∧ c) --suggestion: rename to and_comm_left := left_comm and_comm and_assoc a b c add_rewrite and_comm and_assoc and_id and_false_left and_false_right and_true_left and_true_right and_absurd and_left_comm theorem imp_true_right (a : Bool) : (a → true) ↔ true := boolext (assume H, trivial) (assume H Ha, trivial) theorem imp_true_left (a : Bool) : (true → a) ↔ a := boolext (assume H : true → a, H trivial) (assume Ha H, Ha) theorem imp_false_right (a : Bool) : (a → false) ↔ ¬ a := refl _ theorem imp_false_left (a : Bool) : (false → a) ↔ true := boolext (assume H, trivial) (assume H Hf, false_elim a Hf) theorem imp_id (a : Bool) : (a → a) ↔ true := eqt_intro (λ H : a, H) add_rewrite imp_true_right imp_true_left imp_false_right imp_false_left imp_id theorem imp_or (a b : Bool) : (a → b) ↔ ¬ a ∨ b := boolext (assume H : a → b, (or_elim (em a) (λ Ha : a, or_intro_right (¬ a) (H Ha)) (λ Hna : ¬ a, or_intro_left b Hna))) (assume H : ¬ a ∨ b, assume Ha : a, resolve_right H ((symm (not_not_eq a)) ◂ Ha)) theorem or_imp (a b : Bool) : a ∨ b ↔ (¬ a → b) := boolext (assume H : a ∨ b, (or_elim H (assume (Ha : a) (Hna : ¬ a), absurd_elim b Ha Hna) (assume (Hb : b) (Hna : ¬ a), Hb))) (assume H : ¬ a → b, (or_elim (em a) (assume Ha : a, or_intro_left b Ha) (assume Hna : ¬ a, or_intro_right a (H Hna)))) theorem not_congr {a b : Bool} (H : a ↔ b) : ¬ a ↔ ¬ b := congr2 not H -- The Lean parser has special treatment for the constant exists. -- It allows us to write -- exists x y : A, P x y and ∃ x y : A, P x y -- as syntax sugar for -- exists A (fun x : A, exists A (fun y : A, P x y)) -- That is, it treats the exists as an extra binder such as fun and forall. -- It also provides an alias (Exists) that should be used when we -- want to treat exists as a constant. definition Exists (A : (Type U)) (P : A → Bool) := ¬ (∀ x, ¬ (P x)) definition exists_unique {A : (Type U)} (p : A → Bool) := ∃ x, p x ∧ ∀ y, y ≠ x → ¬ p y theorem exists_elim {A : (Type U)} {P : A → Bool} {B : Bool} (H1 : Exists A P) (H2 : ∀ (a : A) (H : P a), B) : B := by_contradiction (assume R : ¬ B, absurd (take a : A, mt (assume H : P a, H2 a H) R) H1) theorem exists_intro {A : (Type U)} {P : A → Bool} (a : A) (H : P a) : Exists A P := assume H1 : (∀ x : A, ¬ P x), absurd H (H1 a) theorem not_exists (A : (Type U)) (P : A → Bool) : ¬ (∃ x : A, P x) ↔ (∀ x : A, ¬ P x) := calc (¬ ∃ x : A, P x) = ¬ ¬ ∀ x : A, ¬ P x : refl (¬ ∃ x : A, P x) ... = ∀ x : A, ¬ P x : not_not_eq (∀ x : A, ¬ P x) theorem not_exists_elim {A : (Type U)} {P : A → Bool} (H : ¬ ∃ x : A, P x) : ∀ x : A, ¬ P x := (not_exists A P) ◂ H theorem exists_unfold1 {A : (Type U)} {P : A → Bool} (a : A) (H : ∃ x : A, P x) : P a ∨ (∃ x : A, x ≠ a ∧ P x) := exists_elim H (λ (w : A) (H1 : P w), or_elim (em (w = a)) (λ Heq : w = a, or_intro_left (∃ x : A, x ≠ a ∧ P x) (subst H1 Heq)) (λ Hne : w ≠ a, or_intro_right (P a) (exists_intro w (and_intro Hne H1)))) theorem exists_unfold2 {A : (Type U)} {P : A → Bool} (a : A) (H : P a ∨ (∃ x : A, x ≠ a ∧ P x)) : ∃ x : A, P x := or_elim H (λ H1 : P a, exists_intro a H1) (λ H2 : (∃ x : A, x ≠ a ∧ P x), exists_elim H2 (λ (w : A) (Hw : w ≠ a ∧ P w), exists_intro w (and_elim_right Hw))) theorem exists_unfold {A : (Type U)} (P : A → Bool) (a : A) : (∃ x : A, P x) ↔ (P a ∨ (∃ x : A, x ≠ a ∧ P x)) := boolext (assume H : (∃ x : A, P x), exists_unfold1 a H) (assume H : (P a ∨ (∃ x : A, x ≠ a ∧ P x)), exists_unfold2 a H) definition inhabited (A : (Type U)) := ∃ x : A, true -- If we have an element of type A, then A is inhabited theorem inhabited_intro {A : (Type U)} (a : A) : inhabited A := assume H : (∀ x, ¬ true), absurd_not_true (H a) theorem inhabited_elim {A : (Type U)} (H1 : inhabited A) {B : Bool} (H2 : A → B) : B := obtain (w : A) (Hw : true), from H1, H2 w theorem inhabited_ex_intro {A : (Type U)} {P : A → Bool} (H : ∃ x, P x) : inhabited A := obtain (w : A) (Hw : P w), from H, exists_intro w trivial -- If a function space is non-empty, then for every 'a' in the domain, the range (B a) is not empty theorem inhabited_range {A : (Type U)} {B : A → (Type U)} (H : inhabited (∀ x, B x)) (a : A) : inhabited (B a) := by_contradiction (assume N : ¬ inhabited (B a), let s1 : ¬ ∃ x : B a, true := N, s2 : ∀ x : B a, false := take x : B a, absurd_not_true (not_exists_elim s1 x), s3 : ∃ y : (∀ x, B x), true := H in obtain (w : (∀ x, B x)) (Hw : true), from s3, let s4 : B a := w a in s2 s4) theorem exists_rem {A : (Type U)} (H : inhabited A) (p : Bool) : (∃ x : A, p) ↔ p := iff_intro (assume Hl : (∃ x : A, p), obtain (w : A) (Hw : p), from Hl, Hw) (assume Hr : p, inhabited_elim H (λ w, exists_intro w Hr)) theorem forall_rem {A : (Type U)} (H : inhabited A) (p : Bool) : (∀ x : A, p) ↔ p := iff_intro (assume Hl : (∀ x : A, p), inhabited_elim H (λ w, Hl w)) (assume Hr : p, take x, Hr) theorem not_and (a b : Bool) : ¬ (a ∧ b) ↔ ¬ a ∨ ¬ b := boolext (assume H, or_elim (em a) (assume Ha, or_elim (em b) (assume Hb, absurd_elim (¬ a ∨ ¬ b) (and_intro Ha Hb) H) (assume Hnb, or_intro_right (¬ a) Hnb)) (assume Hna, or_intro_left (¬ b) Hna)) (assume (H : ¬ a ∨ ¬ b) (N : a ∧ b), or_elim H (assume Hna, absurd (and_elim_left N) Hna) (assume Hnb, absurd (and_elim_right N) Hnb)) theorem not_and_elim {a b : Bool} (H : ¬ (a ∧ b)) : ¬ a ∨ ¬ b := (not_and a b) ◂ H theorem not_or (a b : Bool) : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := boolext (assume H, or_elim (em a) (assume Ha, absurd_elim (¬ a ∧ ¬ b) (or_intro_left b Ha) H) (assume Hna, or_elim (em b) (assume Hb, absurd_elim (¬ a ∧ ¬ b) (or_intro_right a Hb) H) (assume Hnb, and_intro Hna Hnb))) (assume (H : ¬ a ∧ ¬ b) (N : a ∨ b), or_elim N (assume Ha, absurd Ha (and_elim_left H)) (assume Hb, absurd Hb (and_elim_right H))) theorem not_or_elim {a b : Bool} (H : ¬ (a ∨ b)) : ¬ a ∧ ¬ b := (not_or a b) ◂ H theorem not_implies (a b : Bool) : ¬ (a → b) ↔ a ∧ ¬ b := calc (¬ (a → b)) = ¬ (¬ a ∨ b) : { imp_or a b } ... = ¬ ¬ a ∧ ¬ b : not_or (¬ a) b ... = a ∧ ¬ b : congr2 (λ x, x ∧ ¬ b) (not_not_eq a) theorem and_imp (a b : Bool) : a ∧ b ↔ ¬ (a → ¬ b) := have H1 : a ∧ ¬ ¬ b ↔ ¬ (a → ¬ b), from symm (not_implies a (¬ b)), subst H1 (not_not_eq b) theorem not_implies_elim {a b : Bool} (H : ¬ (a → b)) : a ∧ ¬ b := (not_implies a b) ◂ H theorem eq_not_self (a : Bool) : (a = ¬ a) ↔ false := boolext (λ H, or_elim (em a) (λ Ha, absurd Ha (subst Ha H)) (λ Hna, absurd (subst Hna (symm H)) Hna)) (λ H, false_elim (a = ¬ a) H) theorem iff_not_self (a : Bool) : (a ↔ ¬ a) ↔ false := eq_not_self a theorem true_eq_false : (true = false) ↔ false := subst (eq_not_self true) not_true theorem true_iff_false : (true ↔ false) ↔ false := true_eq_false theorem false_eq_true : (false = true) ↔ false := subst (eq_not_self false) not_false theorem false_iff_true : (false ↔ true) ↔ false := false_eq_true theorem iff_true_right (a : Bool) : (a ↔ true) ↔ a := boolext (λ H, eqt_elim H) (λ H, eqt_intro H) theorem iff_false_right (a : Bool) : (a ↔ false) ↔ ¬ a := boolext (λ H, eqf_elim H) (λ H, eqf_intro H) add_rewrite eq_not_self iff_not_self true_eq_false true_iff_false false_eq_true false_iff_true iff_true_right iff_false_right theorem not_iff (a b : Bool) : ¬ (a ↔ b) ↔ (¬ a ↔ b) := or_elim (em b) (λ Hb, calc (¬ (a ↔ b)) = (¬ (a ↔ true)) : { eqt_intro Hb } ... = ¬ a : { iff_true_right a } ... = ¬ a ↔ true : { symm (iff_true_right (¬ a)) } ... = ¬ a ↔ b : { symm (eqt_intro Hb) }) (λ Hnb, calc (¬ (a ↔ b)) = (¬ (a ↔ false)) : { eqf_intro Hnb } ... = ¬ ¬ a : { iff_false_right a } ... = ¬ a ↔ false : { symm (iff_false_right (¬ a)) } ... = ¬ a ↔ b : { symm (eqf_intro Hnb) }) theorem not_iff_elim {a b : Bool} (H : ¬ (a ↔ b)) : (¬ a) ↔ b := (not_iff a b) ◂ H -- Congruence theorems for contextual simplification -- Simplify a → b, by first simplifying a to c using the fact that ¬ b is true, and then -- b to d using the fact that c is true theorem imp_congr_right {a b c d : Bool} (H_ac : ∀ (H_nb : ¬ b), a = c) (H_bd : ∀ (H_c : c), b = d) : (a → b) = (c → d) := or_elim (em b) (λ H_b : b, or_elim (em c) (λ H_c : c, calc (a → b) = (a → true) : { eqt_intro H_b } ... = true : imp_true_right a ... = (c → true) : symm (imp_true_right c) ... = (c → b) : { symm (eqt_intro H_b) } ... = (c → d) : { H_bd H_c }) (λ H_nc : ¬ c, calc (a → b) = (a → true) : { eqt_intro H_b } ... = true : imp_true_right a ... = (false → d) : symm (imp_false_left d) ... = (c → d) : { symm (eqf_intro H_nc) })) (λ H_nb : ¬ b, or_elim (em c) (λ H_c : c, calc (a → b) = (c → b) : { H_ac H_nb } ... = (c → d) : { H_bd H_c }) (λ H_nc : ¬ c, calc (a → b) = (c → b) : { H_ac H_nb } ... = (false → b) : { eqf_intro H_nc } ... = true : imp_false_left b ... = (false → d) : symm (imp_false_left d) ... = (c → d) : { symm (eqf_intro H_nc) })) -- Simplify a → b, by first simplifying b to d using the fact that a is true, and then -- b to d using the fact that ¬ d is true. -- This kind of congruence seems to be useful in very rare cases. theorem imp_congr_left {a b c d : Bool} (H_bd : ∀ (H_a : a), b = d) (H_ac : ∀ (H_nd : ¬ d), a = c) : (a → b) = (c → d) := or_elim (em a) (λ H_a : a, or_elim (em d) (λ H_d : d, calc (a → b) = (a → d) : { H_bd H_a } ... = (a → true) : { eqt_intro H_d } ... = true : imp_true_right a ... = (c → true) : symm (imp_true_right c) ... = (c → d) : { symm (eqt_intro H_d) }) (λ H_nd : ¬ d, calc (a → b) = (c → b) : { H_ac H_nd } ... = (c → d) : { H_bd H_a })) (λ H_na : ¬ a, or_elim (em d) (λ H_d : d, calc (a → b) = (false → b) : { eqf_intro H_na } ... = true : imp_false_left b ... = (c → true) : symm (imp_true_right c) ... = (c → d) : { symm (eqt_intro H_d) }) (λ H_nd : ¬ d, calc (a → b) = (false → b) : { eqf_intro H_na } ... = true : imp_false_left b ... = (false → d) : symm (imp_false_left d) ... = (a → d) : { symm (eqf_intro H_na) } ... = (c → d) : { H_ac H_nd })) -- (Common case) simplify a to c, and then b to d using the fact that c is true theorem imp_congr {a b c d : Bool} (H_ac : a = c) (H_bd : ∀ (H_c : c), b = d) : (a → b) = (c → d) := imp_congr_right (λ H, H_ac) H_bd theorem or_congr_right {a b c d : Bool} (H_ac : ∀ (H_nb : ¬ b), a = c) (H_bd : ∀ (H_nc : ¬ c), b = d) : a ∨ b ↔ c ∨ d := have H1 : (¬ a → b) ↔ (¬ c → d), from imp_congr_right (λ H_nb : ¬ b, congr2 not (H_ac H_nb)) H_bd, calc (a ∨ b) = (¬ a → b) : or_imp _ _ ... = (¬ c → d) : H1 ... = c ∨ d : symm (or_imp _ _) theorem or_congr_left {a b c d : Bool} (H_bd : ∀ (H_na : ¬ a), b = d) (H_ac : ∀ (H_nd : ¬ d), a = c) : a ∨ b ↔ c ∨ d := have H1 : (¬ a → b) ↔ (¬ c → d), from imp_congr_left H_bd (λ H_nd : ¬ d, congr2 not (H_ac H_nd)), calc (a ∨ b) = (¬ a → b) : or_imp _ _ ... = (¬ c → d) : H1 ... = c ∨ d : symm (or_imp _ _) -- (Common case) simplify a to c, and then b to d using the fact that ¬ c is true theorem or_congr {a b c d : Bool} (H_ac : a = c) (H_bd : ∀ (H_nc : ¬ c), b = d) : a ∨ b ↔ c ∨ d := or_congr_right (λ H, H_ac) H_bd theorem and_congr_right {a b c d : Bool} (H_ac : ∀ (H_b : b), a = c) (H_bd : ∀ (H_c : c), b = d) : a ∧ b ↔ c ∧ d := have H1 : ¬ (a → ¬ b) ↔ ¬ (c → ¬ d), from congr2 not (imp_congr_right (λ (H_nnb : ¬ ¬ b), H_ac (not_not_elim H_nnb)) (λ H_c : c, congr2 not (H_bd H_c))), calc (a ∧ b) = ¬ (a → ¬ b) : and_imp _ _ ... = ¬ (c → ¬ d) : H1 ... = c ∧ d : symm (and_imp _ _) theorem and_congr_left {a b c d : Bool} (H_bd : ∀ (H_a : a), b = d) (H_ac : ∀ (H_d : d), a = c) : a ∧ b ↔ c ∧ d := have H1 : ¬ (a → ¬ b) ↔ ¬ (c → ¬ d), from congr2 not (imp_congr_left (λ H_a : a, congr2 not (H_bd H_a)) (λ (H_nnd : ¬ ¬ d), H_ac (not_not_elim H_nnd))), calc (a ∧ b) = ¬ (a → ¬ b) : and_imp _ _ ... = ¬ (c → ¬ d) : H1 ... = c ∧ d : symm (and_imp _ _) -- (Common case) simplify a to c, and then b to d using the fact that c is true theorem and_congr {a b c d : Bool} (H_ac : a = c) (H_bd : ∀ (H_c : c), b = d) : a ∧ b ↔ c ∧ d := and_congr_right (λ H, H_ac) H_bd theorem forall_or_distribute_right {A : (Type U)} (p : Bool) (φ : A → Bool) : (∀ x, p ∨ φ x) = (p ∨ ∀ x, φ x) := boolext (assume H : (∀ x, p ∨ φ x), or_elim (em p) (λ Hp : p, or_intro_left (∀ x, φ x) Hp) (λ Hnp : ¬ p, or_intro_right p (take x, resolve_right (H x) Hnp))) (assume H : (p ∨ ∀ x, φ x), take x, or_elim H (λ H1 : p, or_intro_left (φ x) H1) (λ H2 : (∀ x, φ x), or_intro_right p (H2 x))) theorem forall_or_distribute_left {A : Type} (p : Bool) (φ : A → Bool) : (∀ x, φ x ∨ p) = ((∀ x, φ x) ∨ p) := boolext (assume H : (∀ x, φ x ∨ p), or_elim (em p) (λ Hp : p, or_intro_right (∀ x, φ x) Hp) (λ Hnp : ¬ p, or_intro_left p (take x, resolve_left (H x) Hnp))) (assume H : (∀ x, φ x) ∨ p, take x, or_elim H (λ H1 : (∀ x, φ x), or_intro_left p (H1 x)) (λ H2 : p, or_intro_right (φ x) H2)) theorem forall_and_distribute {A : (Type U)} (φ ψ : A → Bool) : (∀ x, φ x ∧ ψ x) ↔ (∀ x, φ x) ∧ (∀ x, ψ x) := boolext (assume H : (∀ x, φ x ∧ ψ x), and_intro (take x, and_elim_left (H x)) (take x, and_elim_right (H x))) (assume H : (∀ x, φ x) ∧ (∀ x, ψ x), take x, and_intro (and_elim_left H x) (and_elim_right H x)) theorem exists_and_distribute_right {A : (Type U)} (p : Bool) (φ : A → Bool) : (∃ x, p ∧ φ x) ↔ p ∧ ∃ x, φ x := boolext (assume H : (∃ x, p ∧ φ x), obtain (w : A) (Hw : p ∧ φ w), from H, and_intro (and_elim_left Hw) (exists_intro w (and_elim_right Hw))) (assume H : (p ∧ ∃ x, φ x), obtain (w : A) (Hw : φ w), from (and_elim_right H), exists_intro w (and_intro (and_elim_left H) Hw)) theorem exists_or_distribute {A : (Type U)} (φ ψ : A → Bool) : (∃ x, φ x ∨ ψ x) ↔ (∃ x, φ x) ∨ (∃ x, ψ x) := boolext (assume H : (∃ x, φ x ∨ ψ x), obtain (w : A) (Hw : φ w ∨ ψ w), from H, or_elim Hw (λ Hw1 : φ w, or_intro_left (∃ x, ψ x) (exists_intro w Hw1)) (λ Hw2 : ψ w, or_intro_right (∃ x, φ x) (exists_intro w Hw2))) (assume H : (∃ x, φ x) ∨ (∃ x, ψ x), or_elim H (λ H1 : (∃ x, φ x), obtain (w : A) (Hw : φ w), from H1, exists_intro w (or_intro_left (ψ w) Hw)) (λ H2 : (∃ x, ψ x), obtain (w : A) (Hw : ψ w), from H2, exists_intro w (or_intro_right (φ w) Hw))) theorem eq_exists_intro {A : (Type U)} {P Q : A → Bool} (H : ∀ x : A, P x ↔ Q x) : (∃ x : A, P x) ↔ (∃ x : A, Q x) := boolext (assume Hex, obtain w Pw, from Hex, exists_intro w ((H w) ◂ Pw)) (assume Hex, obtain w Qw, from Hex, exists_intro w ((symm (H w)) ◂ Qw)) theorem not_forall (A : (Type U)) (P : A → Bool) : ¬ (∀ x : A, P x) ↔ (∃ x : A, ¬ P x) := boolext (assume H, by_contradiction (assume N : ¬ (∃ x, ¬ P x), absurd (take x, not_not_elim (not_exists_elim N x)) H)) (assume (H : ∃ x, ¬ P x) (N : ∀ x, P x), obtain w Hw, from H, absurd (N w) Hw) theorem not_forall_elim {A : (Type U)} {P : A → Bool} (H : ¬ (∀ x : A, P x)) : ∃ x : A, ¬ P x := (not_forall A P) ◂ H theorem exists_and_distribute_left {A : (Type U)} (p : Bool) (φ : A → Bool) : (∃ x, φ x ∧ p) ↔ (∃ x, φ x) ∧ p := calc (∃ x, φ x ∧ p) = (∃ x, p ∧ φ x) : eq_exists_intro (λ x, and_comm (φ x) p) ... = (p ∧ (∃ x, φ x)) : exists_and_distribute_right p φ ... = ((∃ x, φ x) ∧ p) : and_comm p (∃ x, φ x) theorem exists_imp_distribute {A : (Type U)} (φ ψ : A → Bool) : (∃ x, φ x → ψ x) ↔ ((∀ x, φ x) → (∃ x, ψ x)) := calc (∃ x, φ x → ψ x) = (∃ x, ¬ φ x ∨ ψ x) : eq_exists_intro (λ x, imp_or (φ x) (ψ x)) ... = (∃ x, ¬ φ x) ∨ (∃ x, ψ x) : exists_or_distribute _ _ ... = ¬ (∀ x, φ x) ∨ (∃ x, ψ x) : { symm (not_forall A φ) } ... = (∀ x, φ x) → (∃ x, ψ x) : symm (imp_or _ _) theorem forall_uninhabited {A : (Type U)} {B : A → Bool} (H : ¬ inhabited A) : ∀ x, B x := by_contradiction (assume N : ¬ (∀ x, B x), obtain w Hw, from not_forall_elim N, absurd (inhabited_intro w) H) theorem allext {A : (Type U)} {B C : A → Bool} (H : ∀ x : A, B x = C x) : (∀ x : A, B x) = (∀ x : A, C x) := boolext (assume Hl, take x, (H x) ◂ (Hl x)) (assume Hr, take x, (symm (H x)) ◂ (Hr x)) theorem proj1_congr {A : (Type U)} {B : A → (Type U)} {a b : sig x, B x} (H : a = b) : proj1 a = proj1 b := subst (refl (proj1 a)) H theorem proj2_congr {A B : (Type U)} {a b : A # B} (H : a = b) : proj2 a = proj2 b := subst (refl (proj2 a)) H theorem hproj2_congr {A : (Type U)} {B : A → (Type U)} {a b : sig x, B x} (H : a = b) : proj2 a == proj2 b := subst (hrefl (proj2 a)) H -- Up to this point, we proved all theorems using just reflexivity, substitution and case (proof by cases) -- Function extensionality axiom funext {A : (Type U)} {B : A → (Type U)} {f g : ∀ x : A, B x} (H : ∀ x : A, f x = g x) : f = g -- Eta is a consequence of function extensionality theorem eta {A : (Type U)} {B : A → (Type U)} (f : ∀ x : A, B x) : (λ x : A, f x) = f := funext (λ x : A, refl (f x)) -- Epsilon (Hilbert's operator) variable eps {A : (Type U)} (H : inhabited A) (P : A → Bool) : A alias ε : eps axiom eps_ax {A : (Type U)} (H : inhabited A) {P : A → Bool} (a : A) : P a → P (ε H P) theorem eps_th {A : (Type U)} {P : A → Bool} (a : A) : P a → P (ε (inhabited_intro a) P) := assume H : P a, @eps_ax A (inhabited_intro a) P a H theorem eps_singleton {A : (Type U)} (H : inhabited A) (a : A) : ε H (λ x, x = a) = a := let P := λ x, x = a, Ha : P a := refl a in eps_ax H a Ha -- A function space (∀ x : A, B x) is inhabited if forall a : A, we have inhabited (B a) theorem inhabited_dfun {A : (Type U)} {B : A → (Type U)} (Hn : ∀ a, inhabited (B a)) : inhabited (∀ x, B x) := inhabited_intro (λ x, ε (Hn x) (λ y, true)) theorem inhabited_fun (A : (Type U)) {B : (Type U)} (H : inhabited B) : inhabited (A → B) := inhabited_intro (λ x, ε H (λ y, true)) theorem exists_to_eps {A : (Type U)} {P : A → Bool} (H : ∃ x, P x) : P (ε (inhabited_ex_intro H) P) := obtain (w : A) (Hw : P w), from H, @eps_ax _ (inhabited_ex_intro H) P w Hw theorem axiom_of_choice {A : (Type U)} {B : A → (Type U)} {R : ∀ x : A, B x → Bool} (H : ∀ x, ∃ y, R x y) : ∃ f, ∀ x, R x (f x) := exists_intro (λ x, ε (inhabited_ex_intro (H x)) (λ y, R x y)) -- witness for f (λ x, exists_to_eps (H x)) -- proof that witness satisfies ∀ x, R x (f x) theorem skolem_th {A : (Type U)} {B : A → (Type U)} {P : ∀ x : A, B x → Bool} : (∀ x, ∃ y, P x y) ↔ ∃ f, (∀ x, P x (f x)) := iff_intro (λ H : (∀ x, ∃ y, P x y), @axiom_of_choice _ _ P H) (λ H : (∃ f, (∀ x, P x (f x))), take x, obtain (fw : ∀ x, B x) (Hw : ∀ x, P x (fw x)), from H, exists_intro (fw x) (Hw x)) -- if-then-else expression, we define it using Hilbert's operator definition ite {A : (Type U)} (c : Bool) (a b : A) : A := ε (inhabited_intro a) (λ r, (c → r = a) ∧ (¬ c → r = b)) notation 45 if _ then _ else _ : ite theorem if_true {A : (Type U)} (a b : A) : (if true then a else b) = a := calc (if true then a else b) = ε (inhabited_intro a) (λ r, (true → r = a) ∧ (¬ true → r = b)) : refl (if true then a else b) ... = ε (inhabited_intro a) (λ r, r = a) : by simp ... = a : eps_singleton (inhabited_intro a) a theorem if_false {A : (Type U)} (a b : A) : (if false then a else b) = b := calc (if false then a else b) = ε (inhabited_intro a) (λ r, (false → r = a) ∧ (¬ false → r = b)) : refl (if false then a else b) ... = ε (inhabited_intro a) (λ r, r = b) : by simp ... = b : eps_singleton (inhabited_intro a) b theorem if_same {A : (Type U)} (c : Bool) (a: A) : (if c then a else a) = a := or_elim (em c) (λ H : c, calc (if c then a else a) = (if true then a else a) : { eqt_intro H } ... = a : if_true a a) (λ H : ¬ c, calc (if c then a else a) = (if false then a else a) : { eqf_intro H } ... = a : if_false a a) add_rewrite if_true if_false if_same theorem if_congr {A : (Type U)} {b c : Bool} {x y u v : A} (H_bc : b = c) (H_xu : ∀ (H_c : c), x = u) (H_yv : ∀ (H_nc : ¬ c), y = v) : (if b then x else y) = if c then u else v := or_elim (em c) (λ H_c : c, calc (if b then x else y) = if c then x else y : { H_bc } ... = if true then x else y : { eqt_intro H_c } ... = x : if_true _ _ ... = u : H_xu H_c ... = if true then u else v : symm (if_true _ _) ... = if c then u else v : { symm (eqt_intro H_c) }) (λ H_nc : ¬ c, calc (if b then x else y) = if c then x else y : { H_bc } ... = if false then x else y : { eqf_intro H_nc } ... = y : if_false _ _ ... = v : H_yv H_nc ... = if false then u else v : symm (if_false _ _) ... = if c then u else v : { symm (eqf_intro H_nc) }) theorem if_imp_then {a b c : Bool} (H : if a then b else c) : a → b := assume Ha : a, eqt_elim (calc b = if true then b else c : symm (if_true b c) ... = if a then b else c : { symm (eqt_intro Ha) } ... = true : eqt_intro H) theorem if_imp_else {a b c : Bool} (H : if a then b else c) : ¬ a → c := assume Hna : ¬ a, eqt_elim (calc c = if false then b else c : symm (if_false b c) ... = if a then b else c : { symm (eqf_intro Hna) } ... = true : eqt_intro H) theorem app_if_distribute {A B : (Type U)} (c : Bool) (f : A → B) (a b : A) : f (if c then a else b) = if c then f a else f b := or_elim (em c) (λ Hc : c , calc f (if c then a else b) = f (if true then a else b) : { eqt_intro Hc } ... = f a : { if_true a b } ... = if true then f a else f b : symm (if_true (f a) (f b)) ... = if c then f a else f b : { symm (eqt_intro Hc) }) (λ Hnc : ¬ c, calc f (if c then a else b) = f (if false then a else b) : { eqf_intro Hnc } ... = f b : { if_false a b } ... = if false then f a else f b : symm (if_false (f a) (f b)) ... = if c then f a else f b : { symm (eqf_intro Hnc) }) theorem eq_if_distribute_right {A : (Type U)} (c : Bool) (a b v : A) : (v = (if c then a else b)) = if c then v = a else v = b := app_if_distribute c (eq v) a b theorem eq_if_distribute_left {A : (Type U)} (c : Bool) (a b v : A) : ((if c then a else b) = v) = if c then a = v else b = v := app_if_distribute c (λ x, x = v) a b set_opaque exists true set_opaque not true set_opaque or true set_opaque and true set_opaque implies true set_opaque ite true set_opaque eq true definition injective {A B : (Type U)} (f : A → B) := ∀ x1 x2, f x1 = f x2 → x1 = x2 definition non_surjective {A B : (Type U)} (f : A → B) := ∃ y, ∀ x, ¬ f x = y -- The set of individuals, we need to assert the existence of one infinite set variable ind : Type -- ind is infinite, i.e., there is a function f s.t. f is injective, and not surjective axiom infinity : ∃ f : ind → ind, injective f ∧ non_surjective f -- Pair extensionality axiom pairext {A : (Type U)} {B : A → (Type U)} (a b : sig x, B x) (H1 : proj1 a = proj1 b) (H2 : proj2 a == proj2 b) : a = b theorem pair_proj_eq {A : (Type U)} {B : A → (Type U)} (a : sig x, B x) : pair (proj1 a) (proj2 a) = a := have Heq1 : proj1 (pair (proj1 a) (proj2 a)) = proj1 a, from refl (proj1 a), have Heq2 : proj2 (pair (proj1 a) (proj2 a)) == proj2 a, from hrefl (proj2 a), show pair (proj1 a) (proj2 a) = a, from pairext (pair (proj1 a) (proj2 a)) a Heq1 Heq2 theorem pair_congr {A : (Type U)} {B : A → (Type U)} {a a' : A} {b : B a} {b' : B a'} (Ha : a = a') (Hb : b == b') : (pair a b) = (pair a' b') := have Heq1 : proj1 (pair a b) = proj1 (pair a' b'), from Ha, have Heq2 : proj2 (pair a b) == proj2 (pair a' b'), from Hb, show (pair a b) = (pair a' b'), from pairext (pair a b) (pair a' b') Heq1 Heq2 theorem pairext_proj {A B : (Type U)} {p : A # B} {a : A} {b : B} (H1 : proj1 p = a) (H2 : proj2 p = b) : p = (pair a b) := pairext p (pair a b) H1 (to_heq H2) theorem hpairext_proj {A : (Type U)} {B : A → (Type U)} {p : sig x, B x} {a : A} {b : B a} (H1 : proj1 p = a) (H2 : proj2 p == b) : p = (pair a b) := pairext p (pair a b) H1 H2 -- Heterogeneous equality axioms and theorems -- We can "type-cast" an A expression into a B expression, if we can prove that A == B -- Remark: we use A == B instead of A = B, because A = B would be type incorrect. -- A = B is actually (@eq (Type U) A B), which is type incorrect because -- the first argument of eq must have type (Type U) and the type of (Type U) is (Type U+1) variable cast {A B : (Type U+1)} : A == B → A → B axiom cast_heq {A B : (Type U+1)} (H : A == B) (a : A) : cast H a == a -- Heterogeneous equality satisfies the usual properties: symmetry, transitivity, congruence, function extensionality, ... -- Heterogeneous version of subst axiom hsubst {A B : (Type U+1)} {a : A} {b : B} (P : ∀ T : (Type U+1), T → Bool) : P A a → a == b → P B b theorem hsymm {A B : (Type U+1)} {a : A} {b : B} (H : a == b) : b == a := hsubst (λ (T : (Type U+1)) (x : T), x == a) (hrefl a) H theorem htrans {A B C : (Type U+1)} {a : A} {b : B} {c : C} (H1 : a == b) (H2 : b == c) : a == c := hsubst (λ (T : (Type U+1)) (x : T), a == x) H1 H2 axiom hcongr {A A' : (Type U+1)} {B : A → (Type U+1)} {B' : A' → (Type U+1)} {f : ∀ x, B x} {f' : ∀ x, B' x} {a : A} {a' : A'} : f == f' → a == a' → f a == f' a' axiom hfunext {A A' : (Type U+1)} {B : A → (Type U+1)} {B' : A' → (Type U+1)} {f : ∀ x, B x} {f' : ∀ x, B' x} : A == A' → (∀ x x', x == x' → f x == f' x') → f == f' axiom hpiext {A A' : (Type U+1)} {B : A → (Type U+1)} {B' : A' → (Type U+1)} : A == A' → (∀ x x', x == x' → B x == B' x') → (∀ x, B x) == (∀ x, B' x) axiom hsigext {A A' : (Type U+1)} {B : A → (Type U+1)} {B' : A' → (Type U+1)} : A == A' → (∀ x x', x == x' → B x == B' x') → (sig x, B x) == (sig x, B' x) -- Heterogeneous version of the allext theorem theorem hallext {A A' : (Type U+1)} {B : A → Bool} {B' : A' → Bool} (Ha : A == A') (Hb : ∀ x x', x == x' → B x = B' x') : (∀ x, B x) = (∀ x, B' x) := to_eq (hpiext Ha (λ x x' Heq, to_heq (Hb x x' Heq))) -- Simpler version of hfunext axiom, we use it to build proofs theorem hsfunext {A : (Type U)} {B B' : A → (Type U)} {f : ∀ x, B x} {f' : ∀ x, B' x} : (∀ x, f x == f' x) → f == f' := λ Hb, hfunext (hrefl A) (λ (x x' : A) (Heq : x == x'), let s1 : f x == f' x := Hb x, s2 : f' x == f' x' := hcongr (hrefl f') Heq in htrans s1 s2) theorem heq_congr {A B : (Type U)} {a a' : A} {b b' : B} (H1 : a = a') (H2 : b = b') : (a == b) = (a' == b') := calc (a == b) = (a' == b) : { H1 } ... = (a' == b') : { H2 } theorem hheq_congr {A A' B B' : (Type U+1)} {a : A} {a' : A'} {b : B} {b' : B'} (H1 : a == a') (H2 : b == b') : (a == b) = (a' == b') := have Heq1 : (a == b) = (a' == b), from (hsubst (λ (T : (Type U+1)) (x : T), (a == b) = (x == b)) (refl (a == b)) H1), have Heq2 : (a' == b) = (a' == b'), from (hsubst (λ (T : (Type U+1)) (x : T), (a' == b) = (a' == x)) (refl (a' == b)) H2), show (a == b) = (a' == b'), from trans Heq1 Heq2 theorem type_eq {A B : (Type U)} {a : A} {b : B} (H : a == b) : A == B := hsubst (λ (T : (Type U+1)) (x : T), A == T) (hrefl A) H -- Some theorems that are useful for applying simplifications. theorem cast_eq {A : (Type U)} (H : A == A) (a : A) : cast H a = a := to_eq (cast_heq H a) theorem cast_trans {A B C : (Type U)} (Hab : A == B) (Hbc : B == C) (a : A) : cast Hbc (cast Hab a) = cast (htrans Hab Hbc) a := have Heq1 : cast Hbc (cast Hab a) == cast Hab a, from cast_heq Hbc (cast Hab a), have Heq2 : cast Hab a == a, from cast_heq Hab a, have Heq3 : cast (htrans Hab Hbc) a == a, from cast_heq (htrans Hab Hbc) a, show cast Hbc (cast Hab a) = cast (htrans Hab Hbc) a, from to_eq (htrans (htrans Heq1 Heq2) (hsymm Heq3)) theorem cast_pull {A : (Type U)} {B B' : A → (Type U)} (f : ∀ x, B x) (a : A) (Hb : (∀ x, B x) == (∀ x, B' x)) (Hba : (B a) == (B' a)) : cast Hb f a = cast Hba (f a) := have s1 : cast Hb f a == f a, from hcongr (cast_heq Hb f) (hrefl a), have s2 : cast Hba (f a) == f a, from cast_heq Hba (f a), show cast Hb f a = cast Hba (f a), from to_eq (htrans s1 (hsymm s2)) -- Proof irrelevance is true in the set theoretic model we have for Lean. axiom proof_irrel {a : Bool} (H1 H2 : a) : H1 = H2 -- A more general version of proof_irrel that can be be derived using proof_irrel, heq axioms and boolext/iff_intro theorem hproof_irrel {a b : Bool} (H1 : a) (H2 : b) : H1 == H2 := let Hab : a == b := to_heq (iff_intro (assume Ha, H2) (assume Hb, H1)), H1b : b := cast Hab H1, H1_eq_H1b : H1 == H1b := hsymm (cast_heq Hab H1), H1b_eq_H2 : H1b == H2 := to_heq (proof_irrel H1b H2) in htrans H1_eq_H1b H1b_eq_H2
d52e621d42158b10a512eec10b70cb05b7eeae14
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/algebra/monoid_algebra.lean
3c92c0b7b2bd97f5ca25026d07f1e20b1ccc5b4d
[ "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
45,135
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, Yury G. Kudryashov, Scott Morrison -/ import algebra.algebra.basic import linear_algebra.finsupp /-! # Monoid algebras When the domain of a `finsupp` has a multiplicative or additive structure, we can define a convolution product. To mathematicians this structure is known as the "monoid algebra", i.e. the finite formal linear combinations over a given semiring of elements of the monoid. The "group ring" ℤ[G] or the "group algebra" k[G] are typical uses. In this file we define `monoid_algebra k G := G →₀ k`, and `add_monoid_algebra k G` in the same way, and then define the convolution product on these. When the domain is additive, this is used to define polynomials: ``` polynomial α := add_monoid_algebra ℕ α mv_polynomial σ α := add_monoid_algebra (σ →₀ ℕ) α ``` When the domain is multiplicative, e.g. a group, this will be used to define the group ring. ## Implementation note Unfortunately because additive and multiplicative structures both appear in both cases, it doesn't appear to be possible to make much use of `to_additive`, and we just settle for saying everything twice. Similarly, I attempted to just define `add_monoid_algebra k G := monoid_algebra k (multiplicative G)`, but the definitional equality `multiplicative G = G` leaks through everywhere, and seems impossible to use. -/ noncomputable theory open_locale classical big_operators open finset finsupp universes u₁ u₂ u₃ variables (k : Type u₁) (G : Type u₂) /-! ### Multiplicative monoids -/ section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid, has_coe_to_fun]] def monoid_algebra : Type (max u₁ u₂) := G →₀ k end namespace monoid_algebra variables {k G} section has_mul variables [semiring k] [has_mul G] /-- The product of `f g : monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x * y = a`. (Think of the group ring of a group.) -/ instance : has_mul (monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)⟩ lemma mul_def {f g : monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ * a₂) (b₁ * b₂)) := rfl instance : distrib (monoid_algebra k G) := { mul := (*), add := (+), left_distrib := assume f g h, by simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add], right_distrib := assume f g h, by simp only [mul_def, sum_add_index, add_mul, mul_zero, zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add], .. finsupp.add_comm_monoid } instance : mul_zero_class (monoid_algebra k G) := { zero := 0, mul := (*), zero_mul := assume f, by simp only [mul_def, sum_zero_index], mul_zero := assume f, by simp only [mul_def, sum_zero_index, sum_zero] } end has_mul section semigroup variables [semiring k] [semigroup G] instance : semigroup_with_zero (monoid_algebra k G) := { mul := (*), mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], .. monoid_algebra.mul_zero_class } end semigroup section has_one variables [semiring k] [has_one G] /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `1` and zero elsewhere. -/ instance : has_one (monoid_algebra k G) := ⟨single 1 1⟩ lemma one_def : (1 : monoid_algebra k G) = single 1 1 := rfl end has_one section mul_one_class variables [semiring k] [mul_one_class G] instance : mul_zero_one_class (monoid_algebra k G) := { one := 1, mul := (*), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], ..monoid_algebra.mul_zero_class } variables {R : Type*} [semiring R] /-- A non-commutative version of `monoid_algebra.lift`: given a additive homomorphism `f : k →+ R` and a multiplicative monoid homomorphism `g : G →* R`, returns the additive homomorphism from `monoid_algebra k G` such that `lift_nc f g (single a b) = f b * g a`. If `f` is a ring homomorphism and the range of either `f` or `g` is in center of `R`, then the result is a ring homomorphism. If `R` is a `k`-algebra and `f = algebra_map k R`, then the result is an algebra homomorphism called `monoid_algebra.lift`. -/ def lift_nc (f : k →+ R) (g : G →* R) : monoid_algebra k G →+ R := lift_add_hom (λ x : G, (add_monoid_hom.mul_right (g x)).comp f) @[simp] lemma lift_nc_single (f : k →+ R) (g : G →* R) (a : G) (b : k) : lift_nc f g (single a b) = f b * g a := lift_add_hom_apply_single _ _ _ @[simp] lemma lift_nc_one (f : k →+* R) (g : G →* R) : lift_nc (f : k →+ R) g 1 = 1 := by simp [one_def] lemma lift_nc_mul (f : k →+* R) (g : G →* R) (a b : monoid_algebra k G) (h_comm : ∀ {x y}, y ∈ a.support → commute (f (b x)) (g y)) : lift_nc (f : k →+ R) g (a * b) = lift_nc (f : k →+ R) g a * lift_nc (f : k →+ R) g b := begin conv_rhs { rw [← sum_single a, ← sum_single b] }, simp_rw [mul_def, (lift_nc _ g).map_finsupp_sum, lift_nc_single, finsupp.sum_mul, finsupp.mul_sum], refine finset.sum_congr rfl (λ y hy, finset.sum_congr rfl (λ x hx, _)), simp [mul_assoc, (h_comm hy).left_comm] end end mul_one_class /-! #### Semiring structure -/ section semiring variables [semiring k] [monoid G] instance : semiring (monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), .. monoid_algebra.mul_zero_one_class, .. monoid_algebra.semigroup_with_zero, .. monoid_algebra.distrib, .. finsupp.add_comm_monoid } variables {R : Type*} [semiring R] /-- `lift_nc` as a `ring_hom`, for when `f x` and `g y` commute -/ def lift_nc_ring_hom (f : k →+* R) (g : G →* R) (h_comm : ∀ x y, commute (f x) (g y)) : monoid_algebra k G →+* R := { to_fun := lift_nc (f : k →+ R) g, map_one' := lift_nc_one _ _, map_mul' := λ a b, lift_nc_mul _ _ _ _ $ λ _ _ _, h_comm _ _, ..(lift_nc (f : k →+ R) g)} end semiring instance [comm_semiring k] [comm_monoid G] : comm_semiring (monoid_algebra k G) := { mul_comm := assume f g, begin simp only [mul_def, finsupp.sum, mul_comm], rw [finset.sum_comm], simp only [mul_comm] end, .. monoid_algebra.semiring } instance [semiring k] [nontrivial k] [nonempty G]: nontrivial (monoid_algebra k G) := finsupp.nontrivial /-! #### Derived instances -/ section derived_instances instance [semiring k] [subsingleton k] : unique (monoid_algebra k G) := finsupp.unique_of_right instance [ring k] : add_group (monoid_algebra k G) := finsupp.add_group instance [ring k] [monoid G] : ring (monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, .. monoid_algebra.semiring } instance [comm_ring k] [comm_monoid G] : comm_ring (monoid_algebra k G) := { mul_comm := mul_comm, .. monoid_algebra.ring} variables {R S : Type*} instance [semiring R] [semiring k] [module R k] : has_scalar R (monoid_algebra k G) := finsupp.has_scalar instance [semiring R] [semiring k] [module R k] : module R (monoid_algebra k G) := finsupp.module G k instance [semiring R] [semiring S] [semiring k] [module R k] [module S k] [has_scalar R S] [is_scalar_tower R S k] : is_scalar_tower R S (monoid_algebra k G) := finsupp.is_scalar_tower G k instance [semiring R] [semiring S] [semiring k] [module R k] [module S k] [smul_comm_class R S k] : smul_comm_class R S (monoid_algebra k G) := finsupp.smul_comm_class G k instance [group G] [semiring k] : distrib_mul_action G (monoid_algebra k G) := finsupp.comap_distrib_mul_action_self end derived_instances section misc_theorems variables [semiring k] local attribute [reducible] monoid_algebra lemma mul_apply [has_mul G] (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ * a₂ = x then b₁ * b₂ else 0) := begin rw [mul_def], simp only [finsupp.sum_apply, single_apply], end lemma mul_apply_antidiagonal [has_mul G] (f g : monoid_algebra k G) (x : G) (s : finset (G × G)) (hs : ∀ {p : G × G}, p ∈ s ↔ p.1 * p.2 = x) : (f * g) x = ∑ p in s, (f p.1 * g p.2) := let F : G × G → k := λ p, if p.1 * p.2 = x then f p.1 * g p.2 else 0 in calc (f * g) x = (∑ a₁ in f.support, ∑ a₂ in g.support, F (a₁, a₂)) : mul_apply f g x ... = ∑ p in f.support.product g.support, F p : finset.sum_product.symm ... = ∑ p in (f.support.product g.support).filter (λ p : G × G, p.1 * p.2 = x), f p.1 * g p.2 : (finset.sum_filter _ _).symm ... = ∑ p in s.filter (λ p : G × G, p.1 ∈ f.support ∧ p.2 ∈ g.support), f p.1 * g p.2 : sum_congr (by { ext, simp only [mem_filter, mem_product, hs, and_comm] }) (λ _ _, rfl) ... = ∑ p in s, f p.1 * g p.2 : sum_subset (filter_subset _ _) $ λ p hps hp, begin simp only [mem_filter, mem_support_iff, not_and, not_not] at hp ⊢, by_cases h1 : f p.1 = 0, { rw [h1, zero_mul] }, { rw [hp hps h1, mul_zero] } end lemma support_mul [has_mul G] (a b : monoid_algebra k G) : (a * b).support ⊆ a.support.bUnion (λa₁, b.support.bUnion $ λa₂, {a₁ * a₂}) := subset.trans support_sum $ bUnion_mono $ assume a₁ _, subset.trans support_sum $ bUnion_mono $ assume a₂ _, support_single_subset @[simp] lemma single_mul_single [has_mul G] {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ : monoid_algebra k G) * single a₂ b₂ = single (a₁ * a₂) (b₁ * b₂) := (sum_single_index (by simp only [zero_mul, single_zero, sum_zero])).trans (sum_single_index (by rw [mul_zero, single_zero])) @[simp] lemma single_pow [monoid G] {a : G} {b : k} : ∀ n : ℕ, (single a b : monoid_algebra k G)^n = single (a^n) (b ^ n) | 0 := by { simp only [pow_zero], refl } | (n+1) := by simp only [pow_succ, single_pow n, single_mul_single] section /-- Like `finsupp.map_domain_add`, but for the convolutive multiplication we define in this file -/ lemma map_domain_mul {α : Type*} {β : Type*} {α₂ : Type*} [semiring β] [has_mul α] [has_mul α₂] {x y : monoid_algebra β α} (f : mul_hom α α₂) : (map_domain f (x * y : monoid_algebra β α) : monoid_algebra β α₂) = (map_domain f x * map_domain f y : monoid_algebra β α₂) := begin simp_rw [mul_def, map_domain_sum, map_domain_single, f.map_mul], rw finsupp.sum_map_domain_index, { congr, ext a b, rw finsupp.sum_map_domain_index, { simp }, { simp [mul_add] } }, { simp }, { simp [add_mul] } end variables (k G) [mul_one_class G] /-- Embedding of a monoid into its monoid algebra. -/ def of : G →* monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, map_mul' := λ a b, by rw [single_mul_single, one_mul] } end @[simp] lemma of_apply [mul_one_class G] (a : G) : of k G a = single a 1 := rfl lemma of_injective [mul_one_class G] [nontrivial k] : function.injective (of k G) := λ a b h, by simpa using (single_eq_single_iff _ _ _ _).mp h lemma mul_single_apply_aux [has_mul G] (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, a * x = z ↔ a = y) : (f * single x r) z = f y * r := have A : ∀ a₁ b₁, (single x r).sum (λ a₂ b₂, ite (a₁ * a₂ = z) (b₁ * b₂) 0) = ite (a₁ * x = z) (b₁ * r) 0, from λ a₁ b₁, sum_single_index $ by simp, calc (f * single x r) z = sum f (λ a b, if (a = y) then (b * r) else 0) : -- different `decidable` instances make it not trivial by { simp only [mul_apply, A, H], congr, funext, split_ifs; refl } ... = if y ∈ f.support then f y * r else 0 : f.support.sum_ite_eq' _ _ ... = f y * r : by split_ifs with h; simp at h; simp [h] lemma mul_single_one_apply [mul_one_class G] (f : monoid_algebra k G) (r : k) (x : G) : (f * single 1 r) x = f x * r := f.mul_single_apply_aux $ λ a, by rw [mul_one] lemma single_mul_apply_aux [has_mul G] (f : monoid_algebra k G) {r : k} {x y z : G} (H : ∀ a, x * a = y ↔ a = z) : (single x r * f) y = r * f z := have f.sum (λ a b, ite (x * a = y) (0 * b) 0) = 0, by simp, calc (single x r * f) y = sum f (λ a b, ite (x * a = y) (r * b) 0) : (mul_apply _ _ _).trans $ sum_single_index this ... = f.sum (λ a b, ite (a = z) (r * b) 0) : by { simp only [H], congr' with g s, split_ifs; refl } ... = if z ∈ f.support then (r * f z) else 0 : f.support.sum_ite_eq' _ _ ... = _ : by split_ifs with h; simp at h; simp [h] lemma single_one_mul_apply [mul_one_class G] (f : monoid_algebra k G) (r : k) (x : G) : (single 1 r * f) x = r * f x := f.single_mul_apply_aux $ λ a, by rw [one_mul] lemma lift_nc_smul [mul_one_class G] {R : Type*} [semiring R] (f : k →+* R) (g : G →* R) (c : k) (φ : monoid_algebra k G) : lift_nc (f : k →+ R) g (c • φ) = f c * lift_nc (f : k →+ R) g φ := begin suffices : (lift_nc ↑f g).comp (smul_add_hom k (monoid_algebra k G) c) = (add_monoid_hom.mul_left (f c)).comp (lift_nc ↑f g), from add_monoid_hom.congr_fun this φ, ext a b, simp [mul_assoc] end end misc_theorems /-! #### Algebra structure -/ section algebra local attribute [reducible] monoid_algebra lemma single_one_comm [comm_semiring k] [mul_one_class G] (r : k) (f : monoid_algebra k G) : single 1 r * f = f * single 1 r := by { ext, rw [single_one_mul_apply, mul_single_one_apply, mul_comm] } /-- `finsupp.single 1` as a `ring_hom` -/ @[simps] def single_one_ring_hom [semiring k] [monoid G] : k →+* monoid_algebra k G := { map_one' := rfl, map_mul' := λ x y, by rw [single_add_hom, single_mul_single, one_mul], ..finsupp.single_add_hom 1} /-- If two ring homomorphisms from `monoid_algebra k G` are equal on all `single a 1` and `single 1 b`, then they are equal. -/ lemma ring_hom_ext {R} [semiring k] [monoid G] [semiring R] {f g : monoid_algebra k G →+* R} (h₁ : ∀ b, f (single 1 b) = g (single 1 b)) (h_of : ∀ a, f (single a 1) = g (single a 1)) : f = g := ring_hom.coe_add_monoid_hom_injective $ add_hom_ext $ λ a b, by rw [← one_mul a, ← mul_one b, ← single_mul_single, f.coe_add_monoid_hom, g.coe_add_monoid_hom, f.map_mul, g.map_mul, h₁, h_of] /-- If two ring homomorphisms from `monoid_algebra k G` are equal on all `single a 1` and `single 1 b`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma ring_hom_ext' {R} [semiring k] [monoid G] [semiring R] {f g : monoid_algebra k G →+* R} (h₁ : f.comp single_one_ring_hom = g.comp single_one_ring_hom) (h_of : (f : monoid_algebra k G →* R).comp (of k G) = (g : monoid_algebra k G →* R).comp (of k G)) : f = g := ring_hom_ext (ring_hom.congr_fun h₁) (monoid_hom.congr_fun h_of) /-- The instance `algebra k (monoid_algebra A G)` whenever we have `algebra k A`. In particular this provides the instance `algebra k (monoid_algebra k G)`. -/ instance {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : algebra k (monoid_algebra A G) := { smul_def' := λ r a, by { ext, simp [single_one_mul_apply, algebra.smul_def'', pi.smul_apply], }, commutes' := λ r f, by { ext, simp [single_one_mul_apply, mul_single_one_apply, algebra.commutes], }, ..single_one_ring_hom.comp (algebra_map k A) } /-- `finsupp.single 1` as a `alg_hom` -/ @[simps] def single_one_alg_hom {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : A →ₐ[k] monoid_algebra A G := { commutes' := λ r, by { ext, simp, refl, }, ..single_one_ring_hom} @[simp] lemma coe_algebra_map {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] : ⇑(algebra_map k (monoid_algebra A G)) = single 1 ∘ (algebra_map k A) := rfl lemma single_eq_algebra_map_mul_of [comm_semiring k] [monoid G] (a : G) (b : k) : single a b = algebra_map k (monoid_algebra k G) b * of k G a := by simp lemma single_algebra_map_eq_algebra_map_mul_of {A : Type*} [comm_semiring k] [semiring A] [algebra k A] [monoid G] (a : G) (b : k) : single a (algebra_map k A b) = algebra_map k (monoid_algebra A G) b * of A G a := by simp lemma induction_on [semiring k] [monoid G] {p : monoid_algebra k G → Prop} (f : monoid_algebra k G) (hM : ∀ g, p (of k G g)) (hadd : ∀ f g : monoid_algebra k G, p f → p g → p (f + g)) (hsmul : ∀ (r : k) f, p f → p (r • f)) : p f := begin refine finsupp.induction_linear f _ (λ f g hf hg, hadd f g hf hg) (λ g r, _), { simpa using hsmul 0 (of k G 1) (hM 1) }, { convert hsmul r (of k G g) (hM g), simp only [mul_one, smul_single', of_apply] }, end end algebra section lift variables {k G} [comm_semiring k] [monoid G] variables {A : Type u₃} [semiring A] [algebra k A] {B : Type*} [semiring B] [algebra k B] /-- `lift_nc_ring_hom` as a `alg_hom`, for when `f` is an `alg_hom` -/ def lift_nc_alg_hom (f : A →ₐ[k] B) (g : G →* B) (h_comm : ∀ x y, commute (f x) (g y)) : monoid_algebra A G →ₐ[k] B := { to_fun := lift_nc_ring_hom (f : A →+* B) g h_comm, commutes' := by simp [lift_nc_ring_hom], ..(lift_nc_ring_hom (f : A →+* B) g h_comm)} /-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma alg_hom_ext ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := alg_hom.to_linear_map_inj $ finsupp.lhom_ext' $ λ a, linear_map.ext_ring (h a) /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma alg_hom_ext' ⦃φ₁ φ₂ : monoid_algebra k G →ₐ[k] A⦄ (h : (φ₁ : monoid_algebra k G →* A).comp (of k G) = (φ₂ : monoid_algebra k G →* A).comp (of k G)) : φ₁ = φ₂ := alg_hom_ext $ monoid_hom.congr_fun h variables (k G A) /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `monoid_algebra k G →ₐ[k] A`. -/ def lift : (G →* A) ≃ (monoid_algebra k G →ₐ[k] A) := { inv_fun := λ f, (f : monoid_algebra k G →* A).comp (of k G), to_fun := λ F, lift_nc_alg_hom (algebra.of_id k A) F $ λ _ _, algebra.commutes _ _, left_inv := λ f, by { ext, simp [lift_nc_alg_hom, lift_nc_ring_hom] }, right_inv := λ F, by { ext, simp [lift_nc_alg_hom, lift_nc_ring_hom] } } variables {k G A} lemma lift_apply' (F : G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, (algebra_map k A b) * F a) := rfl lemma lift_apply (F : G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, b • F a) := by simp only [lift_apply', algebra.smul_def] lemma lift_def (F : G →* A) : ⇑(lift k G A F) = lift_nc ((algebra_map k A : k →+* A) : k →+ A) F := rfl @[simp] lemma lift_symm_apply (F : monoid_algebra k G →ₐ[k] A) (x : G) : (lift k G A).symm F x = F (single x 1) := rfl lemma lift_of (F : G →* A) (x) : lift k G A F (of k G x) = F x := by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply] @[simp] lemma lift_single (F : G →* A) (a b) : lift k G A F (single a b) = b • F a := by rw [lift_def, lift_nc_single, algebra.smul_def, ring_hom.coe_add_monoid_hom] lemma lift_unique' (F : monoid_algebra k G →ₐ[k] A) : F = lift k G A ((F : monoid_algebra k G →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by its values on `F (single a 1)`. -/ lemma lift_unique (F : monoid_algebra k G →ₐ[k] A) (f : monoid_algebra k G) : F f = f.sum (λ a b, b • F (single a 1)) := by conv_lhs { rw lift_unique' F, simp [lift_apply] } end lift section local attribute [reducible] monoid_algebra variables (k) /-- When `V` is a `k[G]`-module, multiplication by a group element `g` is a `k`-linear map. -/ def group_smul.linear_map [monoid G] [comm_semiring k] (V : Type u₃) [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] (g : G) : V →ₗ[k] V := { to_fun := λ v, (single g (1 : k) • v : V), map_add' := λ x y, smul_add (single g (1 : k)) x y, map_smul' := λ c x, smul_algebra_smul_comm _ _ _ } @[simp] lemma group_smul.linear_map_apply [monoid G] [comm_semiring k] (V : Type u₃) [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] (g : G) (v : V) : (group_smul.linear_map k V g) v = (single g (1 : k) • v : V) := rfl section variables {k} variables [monoid G] [comm_semiring k] {V W : Type u₃} [add_comm_monoid V] [module k V] [module (monoid_algebra k G) V] [is_scalar_tower k (monoid_algebra k G) V] [add_comm_monoid W] [module k W] [module (monoid_algebra k G) W] [is_scalar_tower k (monoid_algebra k G) W] (f : V →ₗ[k] W) (h : ∀ (g : G) (v : V), f (single g (1 : k) • v : V) = (single g (1 : k) • (f v) : W)) include h /-- Build a `k[G]`-linear map from a `k`-linear map and evidence that it is `G`-equivariant. -/ def equivariant_of_linear_of_comm : V →ₗ[monoid_algebra k G] W := { to_fun := f, map_add' := λ v v', by simp, map_smul' := λ c v, begin apply finsupp.induction c, { simp, }, { intros g r c' nm nz w, simp only [add_smul, f.map_add, w, add_left_inj, single_eq_algebra_map_mul_of, ← smul_smul], erw [algebra_map_smul (monoid_algebra k G) r, algebra_map_smul (monoid_algebra k G) r, f.map_smul, h g v, of_apply], all_goals { apply_instance } } end, } @[simp] lemma equivariant_of_linear_of_comm_apply (v : V) : (equivariant_of_linear_of_comm f h) v = f v := rfl end end section universe ui variable {ι : Type ui} local attribute [reducible] monoid_algebra lemma prod_single [comm_semiring k] [comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : (∏ i in s, single (a i) (b i)) = single (∏ i in s, a i) (∏ i in s, b i) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, prod_insert has, prod_insert has] end section -- We now prove some additional statements that hold for group algebras. variables [semiring k] [group G] local attribute [reducible] monoid_algebra @[simp] lemma mul_single_apply (f : monoid_algebra k G) (r : k) (x y : G) : (f * single x r) y = f (y * x⁻¹) * r := f.mul_single_apply_aux $ λ a, eq_mul_inv_iff_mul_eq.symm @[simp] lemma single_mul_apply (r : k) (x : G) (f : monoid_algebra k G) (y : G) : (single x r * f) y = r * f (x⁻¹ * y) := f.single_mul_apply_aux $ λ z, eq_inv_mul_iff_mul_eq.symm lemma mul_apply_left (f g : monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λ a b, b * (g (a⁻¹ * x))) := calc (f * g) x = sum f (λ a b, (single a b * g) x) : by rw [← finsupp.sum_apply, ← finsupp.sum_mul, f.sum_single] ... = _ : by simp only [single_mul_apply, finsupp.sum] -- If we'd assumed `comm_semiring`, we could deduce this from `mul_apply_left`. lemma mul_apply_right (f g : monoid_algebra k G) (x : G) : (f * g) x = (g.sum $ λa b, (f (x * a⁻¹)) * b) := calc (f * g) x = sum g (λ a b, (f * single a b) x) : by rw [← finsupp.sum_apply, ← finsupp.mul_sum, g.sum_single] ... = _ : by simp only [mul_single_apply, finsupp.sum] end section span variables [semiring k] [mul_one_class G] /-- An element of `monoid_algebra R M` is in the subalgebra generated by its support. -/ lemma mem_span_support (f : monoid_algebra k G) : f ∈ submodule.span k (of k G '' (f.support : set G)) := by rw [of, monoid_hom.coe_mk, ← finsupp.supported_eq_span_single, finsupp.mem_supported] end span end monoid_algebra /-! ### Additive monoids -/ section variables [semiring k] /-- The monoid algebra over a semiring `k` generated by the additive monoid `G`. It is the type of finite formal `k`-linear combinations of terms of `G`, endowed with the convolution product. -/ @[derive [inhabited, add_comm_monoid, has_coe_to_fun]] def add_monoid_algebra := G →₀ k end namespace add_monoid_algebra variables {k G} section has_mul variables [semiring k] [has_add G] /-- The product of `f g : add_monoid_algebra k G` is the finitely supported function whose value at `a` is the sum of `f x * g y` over all pairs `x, y` such that `x + y = a`. (Think of the product of multivariate polynomials where `α` is the additive monoid of monomial exponents.) -/ instance : has_mul (add_monoid_algebra k G) := ⟨λf g, f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)⟩ lemma mul_def {f g : add_monoid_algebra k G} : f * g = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, single (a₁ + a₂) (b₁ * b₂)) := rfl instance : distrib (add_monoid_algebra k G) := { mul := (*), add := (+), left_distrib := assume f g h, by simp only [mul_def, sum_add_index, mul_add, mul_zero, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_add], right_distrib := assume f g h, by simp only [mul_def, sum_add_index, add_mul, mul_zero, zero_mul, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, sum_zero, sum_add], } instance : mul_zero_class (add_monoid_algebra k G) := { zero := 0, mul := (*), zero_mul := assume f, by simp only [mul_def, sum_zero_index], mul_zero := assume f, by simp only [mul_def, sum_zero_index, sum_zero] } end has_mul section has_one variables [semiring k] [has_zero G] /-- The unit of the multiplication is `single 1 1`, i.e. the function that is `1` at `0` and zero elsewhere. -/ instance : has_one (add_monoid_algebra k G) := ⟨single 0 1⟩ lemma one_def : (1 : add_monoid_algebra k G) = single 0 1 := rfl end has_one section semigroup variables [semiring k] [add_semigroup G] instance : semigroup_with_zero (add_monoid_algebra k G) := { mul := (*), mul_assoc := assume f g h, by simp only [mul_def, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, add_mul, mul_add, add_assoc, mul_assoc, zero_mul, mul_zero, sum_zero, sum_add], .. add_monoid_algebra.mul_zero_class } end semigroup section mul_one_class variables [semiring k] [add_zero_class G] instance : mul_zero_one_class (add_monoid_algebra k G) := { one := 1, mul := (*), one_mul := assume f, by simp only [mul_def, one_def, sum_single_index, zero_mul, single_zero, sum_zero, zero_add, one_mul, sum_single], mul_one := assume f, by simp only [mul_def, one_def, sum_single_index, mul_zero, single_zero, sum_zero, add_zero, mul_one, sum_single], .. add_monoid_algebra.mul_zero_class } variables {R : Type*} [semiring R] /-- A non-commutative version of `add_monoid_algebra.lift`: given a additive homomorphism `f : k →+ R` and a multiplicative monoid homomorphism `g : multiplicative G →* R`, returns the additive homomorphism from `add_monoid_algebra k G` such that `lift_nc f g (single a b) = f b * g a`. If `f` is a ring homomorphism and the range of either `f` or `g` is in center of `R`, then the result is a ring homomorphism. If `R` is a `k`-algebra and `f = algebra_map k R`, then the result is an algebra homomorphism called `add_monoid_algebra.lift`. -/ def lift_nc (f : k →+ R) (g : multiplicative G →* R) : add_monoid_algebra k G →+ R := lift_add_hom (λ x : G, (add_monoid_hom.mul_right (g $ multiplicative.of_add x)).comp f) @[simp] lemma lift_nc_single (f : k →+ R) (g : multiplicative G →* R) (a : G) (b : k) : lift_nc f g (single a b) = f b * g (multiplicative.of_add a) := lift_add_hom_apply_single _ _ _ @[simp] lemma lift_nc_one (f : k →+* R) (g : multiplicative G →* R) : lift_nc (f : k →+ R) g 1 = 1 := @monoid_algebra.lift_nc_one k (multiplicative G) _ _ _ _ f g lemma lift_nc_mul (f : k →+* R) (g : multiplicative G →* R) (a b : add_monoid_algebra k G) (h_comm : ∀ {x y}, y ∈ a.support → commute (f (b x)) (g $ multiplicative.of_add y)) : lift_nc (f : k →+ R) g (a * b) = lift_nc (f : k →+ R) g a * lift_nc (f : k →+ R) g b := @monoid_algebra.lift_nc_mul k (multiplicative G) _ _ _ _ f g a b @h_comm end mul_one_class /-! #### Semiring structure -/ section semiring instance {R : Type*} [semiring R] [semiring k] [module R k] : has_scalar R (add_monoid_algebra k G) := finsupp.has_scalar variables [semiring k] [add_monoid G] instance : semiring (add_monoid_algebra k G) := { one := 1, mul := (*), zero := 0, add := (+), nsmul := λ n f, n • f, nsmul_zero' := by { intros, ext, simp [-nsmul_eq_mul, add_smul] }, nsmul_succ' := by { intros, ext, simp [-nsmul_eq_mul, nat.succ_eq_one_add, add_smul] }, .. add_monoid_algebra.mul_zero_one_class, .. add_monoid_algebra.semigroup_with_zero, .. add_monoid_algebra.distrib, .. finsupp.add_comm_monoid } variables {R : Type*} [semiring R] /-- `lift_nc` as a `ring_hom`, for when `f` and `g` commute -/ def lift_nc_ring_hom (f : k →+* R) (g : multiplicative G →* R) (h_comm : ∀ x y, commute (f x) (g y)) : add_monoid_algebra k G →+* R := { to_fun := lift_nc (f : k →+ R) g, map_one' := lift_nc_one _ _, map_mul' := λ a b, lift_nc_mul _ _ _ _ $ λ _ _ _, h_comm _ _, ..(lift_nc (f : k →+ R) g)} end semiring instance [comm_semiring k] [add_comm_monoid G] : comm_semiring (add_monoid_algebra k G) := { mul_comm := @mul_comm (monoid_algebra k $ multiplicative G) _, .. add_monoid_algebra.semiring } instance [semiring k] [nontrivial k] [nonempty G] : nontrivial (add_monoid_algebra k G) := finsupp.nontrivial /-! #### Derived instances -/ section derived_instances instance [semiring k] [subsingleton k] : unique (add_monoid_algebra k G) := finsupp.unique_of_right instance [ring k] : add_group (add_monoid_algebra k G) := finsupp.add_group instance [ring k] [add_monoid G] : ring (add_monoid_algebra k G) := { neg := has_neg.neg, add_left_neg := add_left_neg, sub := has_sub.sub, sub_eq_add_neg := finsupp.add_group.sub_eq_add_neg, .. add_monoid_algebra.semiring } instance [comm_ring k] [add_comm_monoid G] : comm_ring (add_monoid_algebra k G) := { mul_comm := mul_comm, .. add_monoid_algebra.ring} variables {R S : Type*} instance [semiring R] [semiring k] [module R k] : module R (add_monoid_algebra k G) := finsupp.module G k instance [semiring R] [semiring S] [semiring k] [module R k] [module S k] [has_scalar R S] [is_scalar_tower R S k] : is_scalar_tower R S (add_monoid_algebra k G) := finsupp.is_scalar_tower G k instance [semiring R] [semiring S] [semiring k] [module R k] [module S k] [smul_comm_class R S k] : smul_comm_class R S (add_monoid_algebra k G) := finsupp.smul_comm_class G k /-! It is hard to state the equivalent of `distrib_mul_action G (add_monoid_algebra k G)` because we've never discussed actions of additive groups. -/ end derived_instances section misc_theorems variables [semiring k] lemma mul_apply [has_add G] (f g : add_monoid_algebra k G) (x : G) : (f * g) x = (f.sum $ λa₁ b₁, g.sum $ λa₂ b₂, if a₁ + a₂ = x then b₁ * b₂ else 0) := @monoid_algebra.mul_apply k (multiplicative G) _ _ _ _ _ lemma mul_apply_antidiagonal [has_add G] (f g : add_monoid_algebra k G) (x : G) (s : finset (G × G)) (hs : ∀ {p : G × G}, p ∈ s ↔ p.1 + p.2 = x) : (f * g) x = ∑ p in s, (f p.1 * g p.2) := @monoid_algebra.mul_apply_antidiagonal k (multiplicative G) _ _ _ _ _ s @hs lemma support_mul [has_add G] (a b : add_monoid_algebra k G) : (a * b).support ⊆ a.support.bUnion (λa₁, b.support.bUnion $ λa₂, {a₁ + a₂}) := @monoid_algebra.support_mul k (multiplicative G) _ _ _ _ lemma single_mul_single [has_add G] {a₁ a₂ : G} {b₁ b₂ : k} : (single a₁ b₁ * single a₂ b₂ : add_monoid_algebra k G) = single (a₁ + a₂) (b₁ * b₂) := @monoid_algebra.single_mul_single k (multiplicative G) _ _ _ _ _ _ -- This should be a `@[simp]` lemma, but the simp_nf linter times out if we add this. -- Probably the correct fix is to make a `[add_]monoid_algebra.single` with the correct type, -- instead of relying on `finsupp.single`. lemma single_pow [add_monoid G] {a : G} {b : k} : ∀ n : ℕ, ((single a b)^n : add_monoid_algebra k G) = single (n • a) (b ^ n) | 0 := by { simp only [pow_zero, zero_nsmul], refl } | (n+1) := by rw [pow_succ, pow_succ, single_pow n, single_mul_single, add_comm, add_nsmul, one_nsmul] /-- Like `finsupp.map_domain_add`, but for the convolutive multiplication we define in this file -/ lemma map_domain_mul {α : Type*} {β : Type*} {α₂ : Type*} [semiring β] [has_add α] [has_add α₂] {x y : add_monoid_algebra β α} (f : add_hom α α₂) : (map_domain f (x * y : add_monoid_algebra β α) : add_monoid_algebra β α₂) = (map_domain f x * map_domain f y : add_monoid_algebra β α₂) := begin simp_rw [mul_def, map_domain_sum, map_domain_single, f.map_add], rw finsupp.sum_map_domain_index, { congr, ext a b, rw finsupp.sum_map_domain_index, { simp }, { simp [mul_add] } }, { simp }, { simp [add_mul] } end section variables (k G) /-- Embedding of a monoid into its monoid algebra. -/ def of [add_zero_class G] : multiplicative G →* add_monoid_algebra k G := { to_fun := λ a, single a 1, map_one' := rfl, map_mul' := λ a b, by { rw [single_mul_single, one_mul], refl } } /-- Embedding of a monoid into its monoid algebra, having `G` as source. -/ def of' : G → add_monoid_algebra k G := λ a, single a 1 end @[simp] lemma of_apply [add_zero_class G] (a : multiplicative G) : of k G a = single a.to_add 1 := rfl @[simp] lemma of'_apply (a : G) : of' k G a = single a 1 := rfl lemma of'_eq_of [add_zero_class G] (a : G) : of' k G a = of k G a := rfl lemma of_injective [nontrivial k] [add_zero_class G] : function.injective (of k G) := λ a b h, by simpa using (single_eq_single_iff _ _ _ _).mp h lemma mul_single_apply_aux [has_add G] (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, a + x = z ↔ a = y) : (f * single x r) z = f y * r := @monoid_algebra.mul_single_apply_aux k (multiplicative G) _ _ _ _ _ _ _ H lemma mul_single_zero_apply [add_zero_class G] (f : add_monoid_algebra k G) (r : k) (x : G) : (f * single 0 r) x = f x * r := f.mul_single_apply_aux r _ _ _ $ λ a, by rw [add_zero] lemma single_mul_apply_aux [has_add G] (f : add_monoid_algebra k G) (r : k) (x y z : G) (H : ∀ a, x + a = y ↔ a = z) : (single x r * f : add_monoid_algebra k G) y = r * f z := @monoid_algebra.single_mul_apply_aux k (multiplicative G) _ _ _ _ _ _ _ H lemma single_zero_mul_apply [add_zero_class G] (f : add_monoid_algebra k G) (r : k) (x : G) : (single 0 r * f : add_monoid_algebra k G) x = r * f x := f.single_mul_apply_aux r _ _ _ $ λ a, by rw [zero_add] lemma lift_nc_smul {R : Type*} [add_zero_class G] [semiring R] (f : k →+* R) (g : multiplicative G →* R) (c : k) (φ : monoid_algebra k G) : lift_nc (f : k →+ R) g (c • φ) = f c * lift_nc (f : k →+ R) g φ := @monoid_algebra.lift_nc_smul k (multiplicative G) _ _ _ _ f g c φ variables {k G} lemma induction_on [add_monoid G] {p : add_monoid_algebra k G → Prop} (f : add_monoid_algebra k G) (hM : ∀ g, p (of k G (multiplicative.of_add g))) (hadd : ∀ f g : add_monoid_algebra k G, p f → p g → p (f + g)) (hsmul : ∀ (r : k) f, p f → p (r • f)) : p f := begin refine finsupp.induction_linear f _ (λ f g hf hg, hadd f g hf hg) (λ g r, _), { simpa using hsmul 0 (of k G (multiplicative.of_add 0)) (hM 0) }, { convert hsmul r (of k G (multiplicative.of_add g)) (hM g), simp only [mul_one, to_add_of_add, smul_single', of_apply] }, end end misc_theorems section span variables [semiring k] /-- An element of `add_monoid_algebra R M` is in the submodule generated by its support. -/ lemma mem_span_support [add_zero_class G] (f : add_monoid_algebra k G) : f ∈ submodule.span k (of k G '' (f.support : set G)) := by rw [of, monoid_hom.coe_mk, ← finsupp.supported_eq_span_single, finsupp.mem_supported] /-- An element of `add_monoid_algebra R M` is in the subalgebra generated by its support, using unbundled inclusion. -/ lemma mem_span_support' (f : add_monoid_algebra k G) : f ∈ submodule.span k (of' k G '' (f.support : set G)) := by rw [of', ← finsupp.supported_eq_span_single, finsupp.mem_supported] end span end add_monoid_algebra /-! #### Conversions between `add_monoid_algebra` and `monoid_algebra` While we were not able to define `add_monoid_algebra k G = monoid_algebra k (multiplicative G)` due to definitional inconveniences, we can still show the types are isomorphic. TODO: with the new definitional `nsmul`, there is a direct equality here, so this paragraph could be improved. -/ /-- The equivalence between `add_monoid_algebra` and `monoid_algebra` in terms of `multiplicative` -/ protected def add_monoid_algebra.to_multiplicative [semiring k] [has_add G] : add_monoid_algebra k G ≃+* monoid_algebra k (multiplicative G) := { map_mul' := λ x y, by convert add_monoid_algebra.map_domain_mul (add_hom.id G), ..finsupp.dom_congr multiplicative.of_add } /-- The equivalence between `monoid_algebra` and `add_monoid_algebra` in terms of `additive` -/ protected def monoid_algebra.to_additive [semiring k] [has_mul G] : monoid_algebra k G ≃+* add_monoid_algebra k (additive G) := { map_mul' := λ x y, by convert monoid_algebra.map_domain_mul (mul_hom.id G), ..finsupp.dom_congr additive.of_mul } namespace add_monoid_algebra variables {k G} /-! #### Algebra structure -/ section algebra variables {R : Type*} local attribute [reducible] add_monoid_algebra /-- `finsupp.single 0` as a `ring_hom` -/ @[simps] def single_zero_ring_hom [semiring k] [add_monoid G] : k →+* add_monoid_algebra k G := { map_one' := rfl, map_mul' := λ x y, by rw [single_add_hom, single_mul_single, zero_add], ..finsupp.single_add_hom 0} /-- If two ring homomorphisms from `add_monoid_algebra k G` are equal on all `single a 1` and `single 0 b`, then they are equal. -/ lemma ring_hom_ext {R} [semiring k] [add_monoid G] [semiring R] {f g : add_monoid_algebra k G →+* R} (h₀ : ∀ b, f (single 0 b) = g (single 0 b)) (h_of : ∀ a, f (single a 1) = g (single a 1)) : f = g := @monoid_algebra.ring_hom_ext k (multiplicative G) R _ _ _ _ _ h₀ h_of /-- If two ring homomorphisms from `add_monoid_algebra k G` are equal on all `single a 1` and `single 0 b`, then they are equal. See note [partially-applied ext lemmas]. -/ @[ext] lemma ring_hom_ext' {R} [semiring k] [add_monoid G] [semiring R] {f g : add_monoid_algebra k G →+* R} (h₁ : f.comp single_zero_ring_hom = g.comp single_zero_ring_hom) (h_of : (f : add_monoid_algebra k G →* R).comp (of k G) = (g : add_monoid_algebra k G →* R).comp (of k G)) : f = g := ring_hom_ext (ring_hom.congr_fun h₁) (monoid_hom.congr_fun h_of) /-- The instance `algebra R (add_monoid_algebra k G)` whenever we have `algebra R k`. In particular this provides the instance `algebra k (add_monoid_algebra k G)`. -/ instance [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : algebra R (add_monoid_algebra k G) := { smul_def' := λ r a, by { ext, simp [single_zero_mul_apply, algebra.smul_def'', pi.smul_apply], }, commutes' := λ r f, by { ext, simp [single_zero_mul_apply, mul_single_zero_apply, algebra.commutes], }, ..single_zero_ring_hom.comp (algebra_map R k) } /-- `finsupp.single 0` as a `alg_hom` -/ @[simps] def single_zero_alg_hom [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : k →ₐ[R] add_monoid_algebra k G := { commutes' := λ r, by { ext, simp, refl, }, ..single_zero_ring_hom} @[simp] lemma coe_algebra_map [comm_semiring R] [semiring k] [algebra R k] [add_monoid G] : (algebra_map R (add_monoid_algebra k G) : R → add_monoid_algebra k G) = single 0 ∘ (algebra_map R k) := rfl end algebra section lift variables {k G} [comm_semiring k] [add_monoid G] variables {A : Type u₃} [semiring A] [algebra k A] {B : Type*} [semiring B] [algebra k B] /-- `lift_nc_ring_hom` as a `alg_hom`, for when `f` is an `alg_hom` -/ def lift_nc_alg_hom (f : A →ₐ[k] B) (g : multiplicative G →* B) (h_comm : ∀ x y, commute (f x) (g y)) : add_monoid_algebra A G →ₐ[k] B := { to_fun := lift_nc_ring_hom (f : A →+* B) g h_comm, commutes' := by simp [lift_nc_ring_hom], ..(lift_nc_ring_hom (f : A →+* B) g h_comm)} /-- A `k`-algebra homomorphism from `monoid_algebra k G` is uniquely defined by its values on the functions `single a 1`. -/ lemma alg_hom_ext ⦃φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A⦄ (h : ∀ x, φ₁ (single x 1) = φ₂ (single x 1)) : φ₁ = φ₂ := @monoid_algebra.alg_hom_ext k (multiplicative G) _ _ _ _ _ _ _ h /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma alg_hom_ext' ⦃φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A⦄ (h : (φ₁ : add_monoid_algebra k G →* A).comp (of k G) = (φ₂ : add_monoid_algebra k G →* A).comp (of k G)) : φ₁ = φ₂ := alg_hom_ext $ monoid_hom.congr_fun h variables (k G A) /-- Any monoid homomorphism `G →* A` can be lifted to an algebra homomorphism `monoid_algebra k G →ₐ[k] A`. -/ def lift : (multiplicative G →* A) ≃ (add_monoid_algebra k G →ₐ[k] A) := { inv_fun := λ f, (f : add_monoid_algebra k G →* A).comp (of k G), to_fun := λ F, { to_fun := lift_nc_alg_hom (algebra.of_id k A) F $ λ _ _, algebra.commutes _ _, .. @monoid_algebra.lift k (multiplicative G) _ _ A _ _ F}, .. @monoid_algebra.lift k (multiplicative G) _ _ A _ _ } variables {k G A} lemma lift_apply' (F : multiplicative G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, (algebra_map k A b) * F (multiplicative.of_add a)) := rfl lemma lift_apply (F : multiplicative G →* A) (f : monoid_algebra k G) : lift k G A F f = f.sum (λ a b, b • F (multiplicative.of_add a)) := by simp only [lift_apply', algebra.smul_def] lemma lift_def (F : multiplicative G →* A) : ⇑(lift k G A F) = lift_nc ((algebra_map k A : k →+* A) : k →+ A) F := rfl @[simp] lemma lift_symm_apply (F : add_monoid_algebra k G →ₐ[k] A) (x : multiplicative G) : (lift k G A).symm F x = F (single x.to_add 1) := rfl lemma lift_of (F : multiplicative G →* A) (x : multiplicative G) : lift k G A F (of k G x) = F x := by rw [of_apply, ← lift_symm_apply, equiv.symm_apply_apply] @[simp] lemma lift_single (F : multiplicative G →* A) (a b) : lift k G A F (single a b) = b • F (multiplicative.of_add a) := by rw [lift_def, lift_nc_single, algebra.smul_def, ring_hom.coe_add_monoid_hom] lemma lift_unique' (F : add_monoid_algebra k G →ₐ[k] A) : F = lift k G A ((F : add_monoid_algebra k G →* A).comp (of k G)) := ((lift k G A).apply_symm_apply F).symm /-- Decomposition of a `k`-algebra homomorphism from `monoid_algebra k G` by its values on `F (single a 1)`. -/ lemma lift_unique (F : add_monoid_algebra k G →ₐ[k] A) (f : monoid_algebra k G) : F f = f.sum (λ a b, b • F (single a 1)) := by conv_lhs { rw lift_unique' F, simp [lift_apply] } lemma alg_hom_ext_iff {φ₁ φ₂ : add_monoid_algebra k G →ₐ[k] A} : (∀ x, φ₁ (finsupp.single x 1) = φ₂ (finsupp.single x 1)) ↔ φ₁ = φ₂ := ⟨λ h, alg_hom_ext h, by rintro rfl _; refl⟩ end lift section local attribute [reducible] add_monoid_algebra universe ui variable {ι : Type ui} lemma prod_single [comm_semiring k] [add_comm_monoid G] {s : finset ι} {a : ι → G} {b : ι → k} : (∏ i in s, single (a i) (b i)) = single (∑ i in s, a i) (∏ i in s, b i) := finset.induction_on s rfl $ λ a s has ih, by rw [prod_insert has, ih, single_mul_single, sum_insert has, prod_insert has] end end add_monoid_algebra variables {R : Type*} [comm_semiring R] (k G) /-- The algebra equivalence between `add_monoid_algebra` and `monoid_algebra` in terms of `multiplicative`. -/ def add_monoid_algebra.to_multiplicative_alg_equiv [semiring k] [algebra R k] [add_monoid G] : add_monoid_algebra k G ≃ₐ[R] monoid_algebra k (multiplicative G) := { commutes' := λ r, by simp [add_monoid_algebra.to_multiplicative], ..add_monoid_algebra.to_multiplicative k G } /-- The algebra equivalence between `monoid_algebra` and `add_monoid_algebra` in terms of `additive`. -/ def monoid_algebra.to_additive_alg_equiv [semiring k] [algebra R k] [monoid G] : monoid_algebra k G ≃ₐ[R] add_monoid_algebra k (additive G) := { commutes' := λ r, by simp [monoid_algebra.to_additive], ..monoid_algebra.to_additive k G }
63a1c40c54226f25b76105b6bc5114f8cf69ac20
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/algebra/pointwise.lean
6a039c4813002a69defd1f663ca8adc7fd859dc0
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
10,353
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin Pointwise addition and multiplication of sets -/ import data.set.finite data.set.lattice group_theory.group_action namespace set open function variables {α : Type*} {β : Type*} (f : α → β) @[to_additive set.pointwise_zero] def pointwise_one [has_one α] : has_one (set α) := ⟨{1}⟩ local attribute [instance] pointwise_one @[simp, to_additive set.mem_pointwise_zero] lemma mem_pointwise_one [has_one α] (a : α) : a ∈ (1 : set α) ↔ a = 1 := mem_singleton_iff @[to_additive set.pointwise_add] def pointwise_mul [has_mul α] : has_mul (set α) := ⟨λ s t, {a | ∃ x ∈ s, ∃ y ∈ t, a = x * y}⟩ local attribute [instance] pointwise_one pointwise_mul pointwise_add @[to_additive set.mem_pointwise_add] lemma mem_pointwise_mul [has_mul α] {s t : set α} {a : α} : a ∈ s * t ↔ ∃ x ∈ s, ∃ y ∈ t, a = x * y := iff.rfl @[to_additive set.add_mem_pointwise_add] lemma mul_mem_pointwise_mul [has_mul α] {s t : set α} {a b : α} (ha : a ∈ s) (hb : b ∈ t) : a * b ∈ s * t := ⟨_, ha, _, hb, rfl⟩ @[to_additive set.pointwise_add_eq_image] lemma pointwise_mul_eq_image [has_mul α] {s t : set α} : s * t = (λ x : α × α, x.fst * x.snd) '' s.prod t := set.ext $ λ a, ⟨ by { rintros ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr ⟨‹_›, ‹_›⟩, rfl⟩ }, by { rintros ⟨_, _, rfl⟩, exact ⟨_, (mem_prod.mp ‹_›).1, _, (mem_prod.mp ‹_›).2, rfl⟩ }⟩ @[to_additive set.pointwise_add_finite] lemma pointwise_mul_finite [has_mul α] {s t : set α} (hs : finite s) (ht : finite t) : finite (s * t) := by { rw pointwise_mul_eq_image, apply set.finite_image, exact set.finite_prod hs ht } def pointwise_mul_semigroup [semigroup α] : semigroup (set α) := { mul_assoc := λ _ _ _, set.ext $ λ _, begin split, { rintros ⟨_, ⟨_, _, _, _, rfl⟩, _, _, rfl⟩, exact ⟨_, ‹_›, _, ⟨_, ‹_›, _, ‹_›, rfl⟩, mul_assoc _ _ _⟩ }, { rintros ⟨_, _, _, ⟨_, _, _, _, rfl⟩, rfl⟩, exact ⟨_, ⟨_, ‹_›, _, ‹_›, rfl⟩, _, ‹_›, (mul_assoc _ _ _).symm⟩ } end, ..pointwise_mul } def pointwise_add_add_semigroup [add_semigroup α] : add_semigroup (set α) := { add_assoc := λ _ _ _, set.ext $ λ _, begin split, { rintros ⟨_, ⟨_, _, _, _, rfl⟩, _, _, rfl⟩, exact ⟨_, ‹_›, _, ⟨_, ‹_›, _, ‹_›, rfl⟩, add_assoc _ _ _⟩ }, { rintros ⟨_, _, _, ⟨_, _, _, _, rfl⟩, rfl⟩, exact ⟨_, ⟨_, ‹_›, _, ‹_›, rfl⟩, _, ‹_›, (add_assoc _ _ _).symm⟩ } end, ..pointwise_add } attribute [to_additive set.pointwise_add_add_semigroup._proof_1] pointwise_mul_semigroup._proof_1 attribute [to_additive set.pointwise_add_add_semigroup] pointwise_mul_semigroup def pointwise_mul_monoid [monoid α] : monoid (set α) := { one_mul := λ s, set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, λ h, ⟨1, mem_singleton 1, a, h, (one_mul a).symm⟩⟩, mul_one := λ s, set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, λ h, ⟨a, h, 1, mem_singleton 1, (mul_one a).symm⟩⟩, ..pointwise_mul_semigroup, ..pointwise_one } def pointwise_add_add_monoid [add_monoid α] : add_monoid (set α) := { zero_add := λ s, set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, λ h, ⟨0, mem_singleton 0, a, h, (zero_add a).symm⟩⟩, add_zero := λ s, set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, simp * at *}, λ h, ⟨a, h, 0, mem_singleton 0, (add_zero a).symm⟩⟩, ..pointwise_add_add_semigroup, ..pointwise_zero } attribute [to_additive set.pointwise_add_add_monoid._proof_1] pointwise_mul_monoid._proof_1 attribute [to_additive set.pointwise_add_add_monoid._proof_2] pointwise_mul_monoid._proof_2 attribute [to_additive set.pointwise_add_add_monoid._proof_3] pointwise_mul_monoid._proof_3 attribute [to_additive set.pointwise_add_add_monoid] pointwise_mul_monoid local attribute [instance] pointwise_mul_monoid @[to_additive set.singleton.is_add_hom] def singleton.is_mul_hom [has_mul α] : is_mul_hom (singleton : α → set α) := { map_mul := λ x y, set.ext $ λ a, by simp [mem_singleton_iff, mem_pointwise_mul] } @[to_additive set.singleton.is_add_monoid_hom] def singleton.is_monoid_hom [monoid α] : is_monoid_hom (singleton : α → set α) := { map_one := rfl, ..singleton.is_mul_hom } @[to_additive set.pointwise_neg] def pointwise_inv [has_inv α] : has_inv (set α) := ⟨λ s, {a | a⁻¹ ∈ s}⟩ @[simp, to_additive set.pointwise_add_empty] lemma pointwise_mul_empty [has_mul α] (s : set α) : s * ∅ = ∅ := set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, tauto}, false.elim⟩ @[simp, to_additive set.empty_pointwise_add] lemma empty_pointwise_mul [has_mul α] (s : set α) : ∅ * s = ∅ := set.ext $ λ a, ⟨by {rintros ⟨_, _, _, _, rfl⟩, tauto}, false.elim⟩ @[to_additive set.pointwise_add_subset_add] lemma pointwise_mul_subset_mul [has_mul α] {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ * s₂ ⊆ t₁ * t₂ := by {rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, h₁ ‹_›, _, h₂ ‹_›, rfl⟩ } @[to_additive set.pointwise_add_union] lemma pointwise_mul_union [has_mul α] (s t u : set α) : s * (t ∪ u) = (s * t) ∪ (s * u) := begin ext a, split, { rintros ⟨_, _, _, H, rfl⟩, cases H; [left, right]; exact ⟨_, ‹_›, _, ‹_›, rfl⟩ }, { intro H, cases H with H H; { rcases H with ⟨_, _, _, _, rfl⟩, refine ⟨_, ‹_›, _, _, rfl⟩, erw mem_union, simp * } } end @[to_additive set.union_pointwise_add] lemma union_pointwise_mul [has_mul α] (s t u : set α) : (s ∪ t) * u = (s * u) ∪ (t * u) := begin ext a, split, { rintros ⟨_, H, _, _, rfl⟩, cases H; [left, right]; exact ⟨_, ‹_›, _, ‹_›, rfl⟩ }, { intro H, cases H with H H; { rcases H with ⟨_, _, _, _, rfl⟩; refine ⟨_, _, _, ‹_›, rfl⟩; erw mem_union; simp * } } end @[to_additive set.pointwise_add_eq_Union_add_left] lemma pointwise_mul_eq_Union_mul_left [has_mul α] {s t : set α} : s * t = ⋃a∈s, (λx, a * x) '' t := by { ext y; split; simp only [mem_Union]; rintros ⟨a, ha, x, hx, ax⟩; exact ⟨a, ha, x, hx, ax.symm⟩ } @[to_additive set.pointwise_add_eq_Union_add_right] lemma pointwise_mul_eq_Union_mul_right [has_mul α] {s t : set α} : s * t = ⋃a∈t, (λx, x * a) '' s := by { ext y; split; simp only [mem_Union]; rintros ⟨a, ha, x, hx, ax⟩; exact ⟨x, hx, a, ha, ax.symm⟩ } @[to_additive set.pointwise_add_ne_empty] lemma pointwise_mul_ne_empty [has_mul α] {s t : set α} : s ≠ ∅ → t ≠ ∅ → s * t ≠ ∅ := begin simp only [ne_empty_iff_exists_mem], rintros ⟨x, hx⟩ ⟨y, hy⟩, exact ⟨x * y, mul_mem_pointwise_mul hx hy⟩ end @[simp, to_additive univ_add_univ] lemma univ_pointwise_mul_univ [monoid α] : (univ : set α) * univ = univ := begin have : ∀x, ∃a b : α, x = a * b := λx, ⟨x, ⟨1, (mul_one x).symm⟩⟩, show {a | ∃ x ∈ univ, ∃ y ∈ univ, a = x * y} = univ, simpa [eq_univ_iff_forall] end def pointwise_mul_fintype [has_mul α] [decidable_eq α] (s t : set α) [hs : fintype s] [ht : fintype t] : fintype (s * t : set α) := by { rw pointwise_mul_eq_image, apply set.fintype_image } def pointwise_add_fintype [has_add α] [decidable_eq α] (s t : set α) [hs : fintype s] [ht : fintype t] : fintype (s + t : set α) := by { rw pointwise_add_eq_image, apply set.fintype_image } attribute [to_additive set.pointwise_add_fintype] set.pointwise_mul_fintype section monoid def pointwise_mul_semiring [monoid α] : semiring (set α) := { add := (⊔), zero := ∅, add_assoc := set.union_assoc, zero_add := set.empty_union, add_zero := set.union_empty, add_comm := set.union_comm, zero_mul := empty_pointwise_mul, mul_zero := pointwise_mul_empty, left_distrib := pointwise_mul_union, right_distrib := union_pointwise_mul, ..pointwise_mul_monoid } def pointwise_mul_comm_semiring [comm_monoid α] : comm_semiring (set α) := { mul_comm := λ s t, set.ext $ λ a, by split; { rintros ⟨_, _, _, _, rfl⟩, exact ⟨_, ‹_›, _, ‹_›, mul_comm _ _⟩ }, ..pointwise_mul_semiring } local attribute [instance] pointwise_mul_semiring section is_mul_hom open is_mul_hom variables [has_mul α] [has_mul β] (m : α → β) [is_mul_hom m] @[to_additive is_add_hom.image_add] lemma image_pointwise_mul (s t : set α) : m '' (s * t) = m '' s * m '' t := set.ext $ assume y, begin split, { rintros ⟨_, ⟨_, _, _, _, rfl⟩, rfl⟩, refine ⟨_, mem_image_of_mem _ ‹_›, _, mem_image_of_mem _ ‹_›, map_mul _ _ _⟩ }, { rintros ⟨_, ⟨_, _, rfl⟩, _, ⟨_, _, rfl⟩, rfl⟩, refine ⟨_, ⟨_, ‹_›, _, ‹_›, rfl⟩, map_mul _ _ _⟩ } end @[to_additive is_add_hom.preimage_add_preimage_subset] lemma preimage_pointwise_mul_preimage_subset (s t : set β) : m ⁻¹' s * m ⁻¹' t ⊆ m ⁻¹' (s * t) := begin rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, ‹_›, _, ‹_›, map_mul _ _ _⟩, end end is_mul_hom variables [monoid α] [monoid β] [is_monoid_hom f] def pointwise_mul_image_is_semiring_hom : is_semiring_hom (image f) := { map_zero := image_empty _, map_one := by erw [image_singleton, is_monoid_hom.map_one f]; refl, map_add := image_union _, map_mul := image_pointwise_mul _ } local attribute [instance] singleton.is_monoid_hom def pointwise_mul_action : mul_action α (set α) := { smul := λ a s, ({a} : set α) * s, one_smul := one_mul, mul_smul := λ _ _ _, show {_} * _ = _, by { erw is_monoid_hom.map_mul (singleton : α → set α), apply mul_assoc } } local attribute [instance] pointwise_mul_action lemma mem_smul_set {a : α} {s : set α} {x : α} : x ∈ a • s ↔ ∃ y ∈ s, x = a * y := by { erw mem_pointwise_mul, simp } lemma smul_set_eq_image {a : α} {s : set α} : a • s = (λ b, a * b) '' s := set.ext $ λ x, begin simp only [mem_smul_set, exists_prop, mem_image], apply exists_congr, intro y, apply and_congr iff.rfl, split; exact eq.symm end end monoid end set
d671270fa01ec520153b5a5117fe3645bee47dde
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebraic_topology/dold_kan/functor_gamma.lean
6cee23ab0cb2532fc928eab8a0dfed8c0c45659a
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
6,169
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import algebraic_topology.dold_kan.split_simplicial_object /-! # Construction of the inverse functor of the Dold-Kan equivalence @TODO @joelriou: construct the functor `Γ₀ : chain_complex C ℕ ⥤ simplicial_object C` which shall be the inverse functor of the Dold-Kan equivalence in the case of abelian categories, and more generally pseudoabelian categories. Extend this functor `Γ₀` as a functor `Γ₂ : karoubi (chain_complex C ℕ) ⥤ karoubi (simplicial_object C)` on the idempotent completion, show that this functor shall be an equivalence of categories when `C` is any additive category. Currently, this file contains the definition of `Γ₀.obj.obj₂ K Δ` for `K : chain_complex C ℕ` and `Δ : simplex_categoryᵒᵖ`. By definition, `Γ₀.obj.obj₂ K Δ` is a certain coproduct indexed by the set `splitting.index_set Δ` whose elements consists of epimorphisms `e : Δ.unop ⟶ Δ'.unop` (with `Δ' : simplex_categoryᵒᵖ`). Some morphisms between the summands of these coproducts are also studied. When the simplicial operations are defined using the epi-mono factorisations in `simplex_category`, the simplicial object `Γ₀.obj K` we get will be a split simplicial object. -/ noncomputable theory open category_theory category_theory.category category_theory.limits simplex_category simplicial_object open_locale simplicial namespace algebraic_topology namespace dold_kan variables {C : Type*} [category C] [preadditive C] (K K' : chain_complex C ℕ) (f : K ⟶ K') {Δ'' Δ' Δ : simplex_category} (i' : Δ'' ⟶ Δ') [mono i'] (i : Δ' ⟶ Δ) [mono i] /-- `is_δ₀ i` is a simple condition used to check whether a monomorphism `i` in `simplex_category` identifies to the coface map `δ 0`. -/ @[nolint unused_arguments] def is_δ₀ {Δ Δ' : simplex_category} (i : Δ' ⟶ Δ) [mono i] : Prop := (Δ.len = Δ'.len+1) ∧ (i.to_order_hom 0 ≠ 0) namespace is_δ₀ lemma iff {j : ℕ} {i : fin (j+2)} : is_δ₀ (simplex_category.δ i) ↔ i = 0 := begin split, { rintro ⟨h₁, h₂⟩, by_contradiction, exact h₂ (fin.succ_above_ne_zero_zero h), }, { rintro rfl, exact ⟨rfl, fin.succ_ne_zero _⟩, }, end lemma eq_δ₀ {n : ℕ} {i : [n] ⟶ [n+1]} [mono i] (hi : is_δ₀ i) : i = simplex_category.δ 0 := begin unfreezingI { obtain ⟨j, rfl⟩ := simplex_category.eq_δ_of_mono i, }, rw iff at hi, rw hi, end end is_δ₀ namespace Γ₀ namespace obj /-- In the definition of `(Γ₀.obj K).obj Δ` as a direct sum indexed by `A : splitting.index_set Δ`, the summand `summand K Δ A` is `K.X A.1.len`. -/ def summand (Δ : simplex_categoryᵒᵖ) (A : splitting.index_set Δ) : C := K.X A.1.unop.len /-- The functor `Γ₀` sends a chain complex `K` to the simplicial object which sends `Δ` to the direct sum of the objects `summand K Δ A` for all `A : splitting.index_set Δ` -/ def obj₂ (K : chain_complex C ℕ) (Δ : simplex_categoryᵒᵖ) [has_finite_coproducts C] : C := ∐ (λ (A : splitting.index_set Δ), summand K Δ A) namespace termwise /-- A monomorphism `i : Δ' ⟶ Δ` induces a morphism `K.X Δ.len ⟶ K.X Δ'.len` which is the identity if `Δ = Δ'`, the differential on the complex `K` if `i = δ 0`, and zero otherwise. -/ def map_mono (K : chain_complex C ℕ) {Δ' Δ : simplex_category} (i : Δ' ⟶ Δ) [mono i] : K.X Δ.len ⟶ K.X Δ'.len := begin by_cases Δ = Δ', { exact eq_to_hom (by congr'), }, { by_cases is_δ₀ i, { exact K.d Δ.len Δ'.len, }, { exact 0, }, }, end variable (Δ) lemma map_mono_id : map_mono K (𝟙 Δ) = 𝟙 _ := by { unfold map_mono, simp only [eq_self_iff_true, eq_to_hom_refl, dite_eq_ite, if_true], } variable {Δ} lemma map_mono_δ₀' (hi : is_δ₀ i) : map_mono K i = K.d Δ.len Δ'.len := begin unfold map_mono, classical, rw [dif_neg, dif_pos hi], unfreezingI { rintro rfl, }, simpa only [self_eq_add_right, nat.one_ne_zero] using hi.1, end @[simp] lemma map_mono_δ₀ {n : ℕ} : map_mono K (δ (0 : fin (n+2))) = K.d (n+1) n := map_mono_δ₀' K _ (by rw is_δ₀.iff) lemma map_mono_eq_zero (h₁ : Δ ≠ Δ') (h₂ : ¬is_δ₀ i) : map_mono K i = 0 := by { unfold map_mono, rw ne.def at h₁, split_ifs, refl, } variables {K K'} @[simp, reassoc] lemma map_mono_naturality : map_mono K i ≫ f.f Δ'.len = f.f Δ.len ≫ map_mono K' i := begin unfold map_mono, split_ifs, { unfreezingI { subst h, }, simp only [id_comp, eq_to_hom_refl, comp_id], }, { rw homological_complex.hom.comm, }, { rw [zero_comp, comp_zero], } end variable (K) @[simp, reassoc] lemma map_mono_comp : map_mono K i ≫ map_mono K i' = map_mono K (i' ≫ i) := begin /- case where i : Δ' ⟶ Δ is the identity -/ by_cases h₁ : Δ = Δ', { unfreezingI { subst h₁, }, simp only [simplex_category.eq_id_of_mono i, comp_id, id_comp, map_mono_id K, eq_to_hom_refl], }, /- case where i' : Δ'' ⟶ Δ' is the identity -/ by_cases h₂ : Δ' = Δ'', { unfreezingI { subst h₂, }, simp only [simplex_category.eq_id_of_mono i', comp_id, id_comp, map_mono_id K, eq_to_hom_refl], }, /- then the RHS is always zero -/ obtain ⟨k, hk⟩ := nat.exists_eq_add_of_lt (len_lt_of_mono i h₁), obtain ⟨k', hk'⟩ := nat.exists_eq_add_of_lt (len_lt_of_mono i' h₂), have eq : Δ.len = Δ''.len + (k+k'+2) := by linarith, rw map_mono_eq_zero K (i' ≫ i) _ _, rotate, { by_contradiction, simpa only [self_eq_add_right, h] using eq, }, { by_contradiction, simp only [h.1, add_right_inj] at eq, linarith, }, /- in all cases, the LHS is also zero, either by definition, or because d ≫ d = 0 -/ by_cases h₃ : is_δ₀ i, { by_cases h₄ : is_δ₀ i', { rw [map_mono_δ₀' K i h₃, map_mono_δ₀' K i' h₄, homological_complex.d_comp_d], }, { simp only [map_mono_eq_zero K i' h₂ h₄, comp_zero], }, }, { simp only [map_mono_eq_zero K i h₁ h₃, zero_comp], }, end end termwise end obj end Γ₀ end dold_kan end algebraic_topology
4948a095c6e5652c685024b2e094d30970932442
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/typeclass_outparam.lean
4606348775692c649ad1a4ec69a1be728ce2670e
[ "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
176
lean
new_frontend class OPClass (α : outParam Type) (β : Type) : Type := (u : Unit := ()) instance op₁ : OPClass Nat Nat := {} set_option pp.all true #synth OPClass Nat Nat
643b601b01e0b19abf59d319f9ab21b27220e902
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/interactive/autoBoundIssue.lean
a2d2377324fac28871a7fb784419928db19558c4
[ "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
867
lean
inductive Vector (α : Type u) : Nat → Type u | nil : Vector α 0 | cons : α → Vector α n → Vector α (n+1) infix:67 " :: " => Vector.cons inductive Ty where | int | bool | fn (a r : Ty) inductive HasType : Fin n → Vector Ty n → Ty → Type where | stop : HasType 0 (ty :: ctx) ty | pop : HasType k ctx ty → HasType k.succ (u :: ctx) ty --^ $/lean/plainTermGoal inductive Foo : HasType k ctx ty → Prop --^ $/lean/plainTermGoal def foo : HasType k ctx ty → Prop --^ $/lean/plainTermGoal | _ => True structure Ex where aux : HasType k ctx ty --^ $/lean/plainTermGoal boo : HasType k ctx ty → Prop := fun _ => True --^ $/lean/plainTermGoal variable (x : HasType k ctx ty) --^ $/lean/plainTermGoal #check x
10b123ce734ed30ab13c7648426eff78a5cb32f3
69b400b14d8b9f598d16e57d334c8a68910f3584
/prep/alg-geom/src/skyscraper.lean
879af05d6e8d3218519231e27ba719d5e4bc3434
[]
no_license
TwoFX/partiii
e8cf563d9f7074a45eb35928bf7eb7dfd38ea29e
b949b1f7599ef35a1c1856ba6e9d1fddb245bcd9
refs/heads/master
1,683,178,340,068
1,622,049,944,000
1,622,049,944,000
304,524,313
0
0
null
null
null
null
UTF-8
Lean
false
false
563
lean
import topology.sheaves.sheaf import category_theory.abelian.basic open_locale classical noncomputable theory universes v u open category_theory open category_theory.limits open opposite variables {C : Type u} [category.{v} C] [has_products C] [abelian C] variables {X : Top.{v}} local attribute [instance] has_zero_object.has_zero def skyscraper (p : X) (A : C) : Top.sheaf C X := { presheaf := { obj := λ U, if p ∈ unop U then A else 0, map := λ V U hVU, begin end, map_id' := _, map_comp' := _ }, sheaf_condition := _ }
2a9963c51d2677c965e8a11b349616ee34acdbf3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/monad/equiv_mon.lean
c6904a81c14521308c93a0094b329865a5a9f5ff
[]
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,215
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.monad.bundled import Mathlib.category_theory.monoidal.End import Mathlib.category_theory.monoidal.Mon_ import Mathlib.category_theory.category.Cat import Mathlib.PostPort universes u v namespace Mathlib /-! # The equivalence between `Monad C` and `Mon_ (C ⥤ C)`. A monad "is just" a monoid in the category of endofunctors. # Definitions/Theorems 1. `to_Mon` associates a monoid object in `C ⥤ C` to any monad on `C`. 2. `Monad_to_Mon` is the functorial version of `to_Mon`. 3. `of_Mon` associates a monad on `C` to any monoid object in `C ⥤ C`. 4. `Monad_Mon_equiv` is the equivalence between `Monad C` and `Mon_ (C ⥤ C)`. -/ namespace category_theory namespace Monad /-- To every `Monad C` we associated a monoid object in `C ⥤ C`.-/ @[simp] theorem to_Mon_mul {C : Type u} [category C] : ∀ (ᾰ : Monad C), Mon_.mul (to_Mon ᾰ) = μ_ := fun (ᾰ : Monad C) => Eq.refl (Mon_.mul (to_Mon ᾰ)) /-- Passing from `Monad C` to `Mon_ (C ⥤ C)` is functorial. -/ @[simp] theorem Monad_to_Mon_obj (C : Type u) [category C] : ∀ (ᾰ : Monad C), functor.obj (Monad_to_Mon C) ᾰ = to_Mon ᾰ := fun (ᾰ : Monad C) => Eq.refl (functor.obj (Monad_to_Mon C) ᾰ) /-- To every monoid object in `C ⥤ C` we associate a `Monad C`. -/ def of_Mon {C : Type u} [category C] : Mon_ (C ⥤ C) → Monad C := fun (M : Mon_ (C ⥤ C)) => mk (Mon_.X M) /-- Passing from `Mon_ (C ⥤ C)` to `Monad C` is functorial. -/ def Mon_to_Monad (C : Type u) [category C] : Mon_ (C ⥤ C) ⥤ Monad C := functor.mk of_Mon fun (_x _x_1 : Mon_ (C ⥤ C)) (f : _x ⟶ _x_1) => monad_hom.mk (nat_trans.mk (nat_trans.app (Mon_.hom.hom f))) namespace Monad_Mon_equiv /-- Isomorphism of functors used in `Monad_Mon_equiv` -/ @[simp] theorem counit_iso_hom_app_hom {C : Type u} [category C] (_x : Mon_ (C ⥤ C)) : Mon_.hom.hom (nat_trans.app (iso.hom counit_iso) _x) = 𝟙 := Eq.refl (Mon_.hom.hom (nat_trans.app (iso.hom counit_iso) _x)) /-- Auxilliary definition for `Monad_Mon_equiv` -/ def unit_iso_hom {C : Type u} [category C] : 𝟭 ⟶ Monad_to_Mon C ⋙ Mon_to_Monad C := nat_trans.mk fun (_x : Monad C) => monad_hom.mk (nat_trans.mk fun (_x_1 : C) => 𝟙) /-- Auxilliary definition for `Monad_Mon_equiv` -/ @[simp] theorem unit_iso_inv_app_to_nat_trans_app {C : Type u} [category C] (_x : Monad C) : ∀ (_x_1 : C), nat_trans.app (monad_hom.to_nat_trans (nat_trans.app unit_iso_inv _x)) _x_1 = 𝟙 := fun (_x_1 : C) => Eq.refl (nat_trans.app (monad_hom.to_nat_trans (nat_trans.app unit_iso_inv _x)) _x_1) /-- Isomorphism of functors used in `Monad_Mon_equiv` -/ def unit_iso {C : Type u} [category C] : 𝟭 ≅ Monad_to_Mon C ⋙ Mon_to_Monad C := iso.mk unit_iso_hom unit_iso_inv end Monad_Mon_equiv /-- Oh, monads are just monoids in the category of endofunctors (equivalence of categories). -/ def Monad_Mon_equiv (C : Type u) [category C] : Monad C ≌ Mon_ (C ⥤ C) := equivalence.mk' (Monad_to_Mon C) (Mon_to_Monad C) sorry sorry
49325a26f2ed7e2f037eeb563c317d4df95df83d
e2c1ee9c02c59b832eb48536755242ce5f9b2c3e
/src/FinalProject.lean
21a14effe5544c88f237d700ab666e299e1da5ad
[]
no_license
yairgueta/Lean
cb5bb16d81674e8a07f85c9dbeb56dc2f543d4eb
af8a4fa24f76edfdd0dd33f013db194e611e6a86
refs/heads/master
1,676,275,529,938
1,610,289,661,000
1,610,289,661,000
328,405,309
0
0
null
null
null
null
UTF-8
Lean
false
false
1,731
lean
import data.finset import data.set inductive word (S : Type*) | ε {} : word | concat : S → word → word namespace word open list variable {S : Type*} notation σ ⬝ w := concat σ w def concat_words : word S → word S → word S | ε w := w | (σ⬝w) w2 := σ⬝ (concat_words w w2) notation w1 ⬝ w2 := concat_words w1 w2 def word.mk : list S → word S | nil := ε | (h :: t) := h ⬝ (word.mk t) end word structure language {S : Type} := (l : set (word S)) namespace language end language structure automaton (states_type : Type*) := (S : Type) (Q : finset states_type) (q₀ : states_type) (δ : (states_type × S) → states_type) (F : finset states_type) (hq_0 : q₀ ∈ Q) (hF : F ⊆ Q) namespace automaton inductive S1 | a : S1 | b : S1 open S1 open word def A1 : automaton ℤ := { S := S1, Q := {0,1,2,3}, q₀ := 0, δ := λ x, match x with | (0, a) := 1 | (0, b) := 0 | (1, a) := 2 | (1, b) := 0 | (2, a) := 2 | (2, b) := 3 | (3, _) := 3 | _ := 0 end, F := {3}, hq_0 := by simp, hF := by simp, } def read_word_from_state {states_type : Type*} (A : automaton states_type) : states_type → word A.S → states_type | qt ε := qt | qt (σ ⬝ w) := read_word_from_state (A.δ ⟨qt, σ⟩) w #reduce read_word_from_state A1 0 ( word.mk [a,a,b,b,b,a]) def read_word {states_type : Type* } (A : automaton states_type) (w : word A.S) : states_type := read_word_from_state A A.q₀ w #reduce read_word A1 (word.mk [a,a,a,a,b,a,a,b,a]) def is_word_accepted {states_type : Type* } (A : automaton states_type) (w : word A.S) : bool := if ((read_word A w) ∈ A.F) then tt else ff #reduce is_word_accepted A1 (word.mk [a,a,a,b,a,a,b,a]) end automaton
af29003110dfb3d26149411dbeea642fa41e9bd9
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/tests/lean/run/coe_to_fn.lean
2d5c02cd4e9f738b2a5582662a8c7c4088bc936a
[ "Apache-2.0" ]
permissive
kbuzzard/lean
ae3c3db4bb462d750dbf7419b28bafb3ec983ef7
ed1788fd674bb8991acffc8fca585ec746711928
refs/heads/master
1,620,983,366,617
1,618,937,600,000
1,618,937,600,000
359,886,396
1
0
Apache-2.0
1,618,936,987,000
1,618,936,987,000
null
UTF-8
Lean
false
false
688
lean
universes u v w structure equiv (α : Type u) (β : Type v) := (f : α → β) (g : β → α) infix ` ≃ `:50 := equiv variables {α : Type u} {β : Type v} {γ : Type w} instance: has_coe_to_fun (α ≃ β) := { F := λ _, α → β, coe := equiv.f, } @[symm] def equiv.inv : α ≃ β → β ≃ α | ⟨f,g⟩ := ⟨g,f⟩ local postfix `⁻¹` := equiv.inv -- coe_fn should be applied at function arguments def equiv.trans (f : α ≃ β) (g : β ≃ γ) : α ≃ γ := ⟨g ∘ f, f⁻¹ ∘ g⁻¹⟩ example (f : α ≃ β) := function.bijective f example (f : α ≃ β) (a : α) := f a example (f : (α ≃ β) ≃ (β ≃ α)) (g : α ≃ β) (b : β) := f g b
14ada75fc45b2aceaa81a333801277386d264ea0
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/tst6.lean
dc6d3eb6631ad0d577d32e1bd8afafa00e7af36e
[ "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
2,192
lean
variable N : Type variable h : N -> N -> N theorem congrH {a1 a2 b1 b2 : N} (H1 : a1 = b1) (H2 : a2 = b2) : (h a1 a2) = (h b1 b2) := congr (congr (refl h) H1) H2 -- Display the theorem showing implicit arguments set_option lean::pp::implicit true print environment 2 -- Display the theorem hiding implicit arguments set_option lean::pp::implicit false print environment 2 theorem Example1 (a b c d : N) (H: (a = b ∧ b = c) ∨ (a = d ∧ d = c)) : (h a b) = (h c b) := or_elim H (fun H1 : a = b ∧ b = c, congrH (trans (and_eliml H1) (and_elimr H1)) (refl b)) (fun H1 : a = d ∧ d = c, congrH (trans (and_eliml H1) (and_elimr H1)) (refl b)) -- print proof of the last theorem with all implicit arguments set_option lean::pp::implicit true print environment 1 -- Using placeholders to hide the type of H1 theorem Example2 (a b c d : N) (H: (a = b ∧ b = c) ∨ (a = d ∧ d = c)) : (h a b) = (h c b) := or_elim H (fun H1 : _, congrH (trans (and_eliml H1) (and_elimr H1)) (refl b)) (fun H1 : _, congrH (trans (and_eliml H1) (and_elimr H1)) (refl b)) set_option lean::pp::implicit true print environment 1 -- Same example but the first conjuct has unnecessary stuff theorem Example3 (a b c d e : N) (H: (a = b ∧ b = e ∧ b = c) ∨ (a = d ∧ d = c)) : (h a b) = (h c b) := or_elim H (fun H1 : _, congrH (trans (and_eliml H1) (and_elimr (and_elimr H1))) (refl b)) (fun H1 : _, congrH (trans (and_eliml H1) (and_elimr H1)) (refl b)) set_option lean::pp::implicit false print environment 1 theorem Example4 (a b c d e : N) (H: (a = b ∧ b = e ∧ b = c) ∨ (a = d ∧ d = c)) : (h a c) = (h c a) := or_elim H (fun H1 : _, let AeqC := trans (and_eliml H1) (and_elimr (and_elimr H1)) in congrH AeqC (symm AeqC)) (fun H1 : _, let AeqC := trans (and_eliml H1) (and_elimr H1) in congrH AeqC (symm AeqC)) set_option lean::pp::implicit false print environment 1
690eca8c2f90ff6fcfec687349e696d64c883144
b147e1312077cdcfea8e6756207b3fa538982e12
/logic/relation.lean
467e5e34f34724df576a6e5f6d9d1d00a27dfbeb
[ "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
13,071
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Transitive reflexive as well as reflexive closure of relations. -/ import tactic logic.relator variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} namespace relation section comp variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop} def comp (r : α → β → Prop) (p : β → γ → Prop) (a : α) (c : γ) : Prop := ∃b, r a b ∧ p b c local infixr ` ∘r ` : 80 := relation.comp lemma comp_eq : r ∘r (=) = r := funext $ assume a, funext $ assume b, propext $ iff.intro (assume ⟨c, h, eq⟩, eq ▸ h) (assume h, ⟨b, h, rfl⟩) lemma eq_comp : (=) ∘r r = r := funext $ assume a, funext $ assume b, propext $ iff.intro (assume ⟨c, eq, h⟩, eq.symm ▸ h) (assume h, ⟨a, rfl, h⟩) lemma iff_comp {r : Prop → α → Prop} : (↔) ∘r r = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, eq_comp] lemma comp_iff {r : α → Prop → Prop} : r ∘r (↔) = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, comp_eq] lemma comp_assoc : (r ∘r p) ∘r q = r ∘r p ∘r q := begin funext a d, apply propext, split, exact assume ⟨c, ⟨b, hab, hbc⟩, hcd⟩, ⟨b, hab, c, hbc, hcd⟩, exact assume ⟨b, hab, c, hbc, hcd⟩, ⟨c, ⟨b, hab, hbc⟩, hcd⟩ end lemma flip_comp : flip (r ∘r p) = (flip p) ∘r (flip r) := begin funext c a, apply propext, split, exact assume ⟨b, hab, hbc⟩, ⟨b, hbc, hab⟩, exact assume ⟨b, hbc, hab⟩, ⟨b, hab, hbc⟩ end end comp protected def map (r : α → β → Prop) (f : α → γ) (g : β → δ) : γ → δ → Prop := λc d, ∃a b, r a b ∧ f a = c ∧ g b = d variables {r : α → α → Prop} {a b c d : α} /-- `refl_trans_gen r`: reflexive transitive closure of `r` -/ inductive refl_trans_gen (r : α → α → Prop) (a : α) : α → Prop | refl {} : refl_trans_gen a | tail {b c} : refl_trans_gen b → r b c → refl_trans_gen c attribute [refl] refl_trans_gen.refl run_cmd tactic.mk_iff_of_inductive_prop `relation.refl_trans_gen `relation.refl_trans_gen.cases_tail_iff /-- `refl_gen r`: reflexive closure of `r` -/ inductive refl_gen (r : α → α → Prop) (a : α) : α → Prop | refl {} : refl_gen a | single {b} : r a b → refl_gen b run_cmd tactic.mk_iff_of_inductive_prop `relation.refl_gen `relation.refl_gen_iff /-- `trans_gen r`: transitive closure of `r` -/ inductive trans_gen (r : α → α → Prop) (a : α) : α → Prop | single {b} : r a b → trans_gen b | tail {b c} : trans_gen b → r b c → trans_gen c run_cmd tactic.mk_iff_of_inductive_prop `relation.trans_gen `relation.trans_gen_iff attribute [refl] refl_gen.refl lemma refl_gen.to_refl_trans_gen : ∀{a b}, refl_gen r a b → refl_trans_gen r a b | a _ refl_gen.refl := by refl | a b (refl_gen.single h) := refl_trans_gen.tail refl_trans_gen.refl h namespace refl_trans_gen @[trans] lemma trans (hab : refl_trans_gen r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma single (hab : r a b) : refl_trans_gen r a b := refl.tail hab lemma head (hab : r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { exact refl.tail hab }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma cases_tail : refl_trans_gen r a b → b = a ∨ (∃c, refl_trans_gen r a c ∧ r c b) := (cases_tail_iff r a b).1 lemma head_induction_on {P : ∀(a:α), refl_trans_gen r a b → Prop} {a : α} (h : refl_trans_gen r a b) (refl : P b refl) (head : ∀{a c} (h' : r a c) (h : refl_trans_gen r c b), P c h → P a (h.head h')) : P a h := begin induction h generalizing P, case refl_trans_gen.refl { exact refl }, case refl_trans_gen.tail : b c hab hbc ih { apply ih, show P b _, from head hbc _ refl, show ∀a a', r a a' → refl_trans_gen r a' b → P a' _ → P a _, from assume a a' hab hbc, head hab _ } end lemma trans_induction_on {P : ∀{a b : α}, refl_trans_gen r a b → Prop} {a b : α} (h : refl_trans_gen r a b) (ih₁ : ∀a, @P a a refl) (ih₂ : ∀{a b} (h : r a b), P (single h)) (ih₃ : ∀{a b c} (h₁ : refl_trans_gen r a b) (h₂ : refl_trans_gen r b c), P h₁ → P h₂ → P (h₁.trans h₂)) : P h := begin induction h, case refl_trans_gen.refl { exact ih₁ a }, case refl_trans_gen.tail : b c hab hbc ih { exact ih₃ hab (single hbc) ih (ih₂ hbc) } end lemma cases_head (h : refl_trans_gen r a b) : a = b ∨ (∃c, r a c ∧ refl_trans_gen r c b) := begin induction h using relation.refl_trans_gen.head_induction_on, { left, refl }, { right, existsi _, split; assumption } end lemma cases_head_iff : refl_trans_gen r a b ↔ a = b ∨ (∃c, r a c ∧ refl_trans_gen r c b) := begin split, { exact cases_head }, { assume h, rcases h with rfl | ⟨c, hac, hcb⟩, { refl }, { exact head hac hcb } } end lemma total_of_right_unique (U : relator.right_unique r) (ab : refl_trans_gen r a b) (ac : refl_trans_gen r a c) : refl_trans_gen r b c ∨ refl_trans_gen r c b := begin induction ab with b d ab bd IH, { exact or.inl ac }, { rcases IH with IH | IH, { rcases cases_head IH with rfl | ⟨e, be, ec⟩, { exact or.inr (single bd) }, { cases U bd be, exact or.inl ec } }, { exact or.inr (IH.tail bd) } } end end refl_trans_gen namespace trans_gen lemma to_refl {a b} (h : trans_gen r a b) : refl_trans_gen r a b := begin induction h with b h b c _ bc ab, exact refl_trans_gen.single h, exact refl_trans_gen.tail ab bc end @[trans] lemma trans_left (hab : trans_gen r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := begin induction hbc, case refl_trans_gen.refl : c hab { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end @[trans] lemma trans (hab : trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := trans_left hab hbc.to_refl lemma head' (hab : r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := trans_left (single hab) hbc lemma tail' (hab : refl_trans_gen r a b) (hbc : r b c) : trans_gen r a c := begin induction hab generalizing c, case refl_trans_gen.refl : c hac { exact single hac }, case refl_trans_gen.tail : d b hab hdb IH { exact tail (IH hdb) hbc } end @[trans] lemma trans_right (hab : refl_trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := begin induction hbc, case trans_gen.single : c hbc { exact tail' hab hbc }, case trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma head (hab : r a b) (hbc : trans_gen r b c) : trans_gen r a c := head' hab hbc.to_refl lemma tail'_iff : trans_gen r a c ↔ ∃ b, refl_trans_gen r a b ∧ r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, tail' hab hbc⟩, cases h with _ hac b _ hab hbc, { exact ⟨_, by refl, hac⟩ }, { exact ⟨_, hab.to_refl, hbc⟩ } end lemma head'_iff : trans_gen r a c ↔ ∃ b, r a b ∧ refl_trans_gen r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, head' hab hbc⟩, induction h, case trans_gen.single : c hac { exact ⟨_, hac, by refl⟩ }, case trans_gen.tail : b c hab hbc IH { rcases IH with ⟨d, had, hdb⟩, exact ⟨_, had, hdb.tail hbc⟩ } end end trans_gen section refl_trans_gen open refl_trans_gen lemma refl_trans_gen_iff_eq (h : ∀b, ¬ r a b) : refl_trans_gen r a b ↔ b = a := by rw [cases_head_iff]; simp [h, eq_comm] lemma refl_trans_gen_iff_eq_or_trans_gen : refl_trans_gen r a b ↔ b = a ∨ trans_gen r a b := begin refine ⟨λ h, _, λ h, _⟩, { cases h with c _ hac hcb, { exact or.inl rfl }, { exact or.inr (trans_gen.tail' hac hcb) } }, { rcases h with rfl | h, {refl}, {exact h.to_refl} } end lemma refl_trans_gen_lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀a b, r a b → p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := hab.trans_induction_on (assume a, refl) (assume a b, refl_trans_gen.single ∘ h _ _) (assume a b c _ _, trans) lemma refl_trans_gen_mono {p : α → α → Prop} : (∀a b, r a b → p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen_lift id lemma refl_trans_gen_eq_self (refl : reflexive r) (trans : transitive r) : refl_trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h with b c h₁ h₂ IH, {apply refl}, exact trans IH h₂, end, single⟩ lemma reflexive_refl_trans_gen : reflexive (refl_trans_gen r) := assume a, refl lemma transitive_refl_trans_gen : transitive (refl_trans_gen r) := assume a b c, trans lemma refl_trans_gen_idem : refl_trans_gen (refl_trans_gen r) = refl_trans_gen r := refl_trans_gen_eq_self reflexive_refl_trans_gen transitive_refl_trans_gen lemma refl_trans_gen_lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀a b, r a b → refl_trans_gen p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := by simpa [refl_trans_gen_idem] using refl_trans_gen_lift f h hab lemma refl_trans_gen_closed {p : α → α → Prop} : (∀ a b, r a b → refl_trans_gen p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen_lift' id end refl_trans_gen def join (r : α → α → Prop) : α → α → Prop := λa b, ∃c, r a c ∧ r b c section join open refl_trans_gen refl_gen lemma church_rosser (h : ∀a b c, r a b → r a c → ∃d, refl_gen r b d ∧ refl_trans_gen r c d) (hab : refl_trans_gen r a b) (hac : refl_trans_gen r a c) : join (refl_trans_gen r) b c := begin induction hab, case refl_trans_gen.refl { exact ⟨c, hac, refl⟩ }, case refl_trans_gen.tail : d e had hde ih { clear hac had a, rcases ih with ⟨b, hdb, hcb⟩, have : ∃a, refl_trans_gen r e a ∧ refl_gen r b a, { clear hcb, induction hdb, case refl_trans_gen.refl { exact ⟨e, refl, refl_gen.single hde⟩ }, case refl_trans_gen.tail : f b hdf hfb ih { rcases ih with ⟨a, hea, hfa⟩, cases hfa with _ hfa, { exact ⟨b, hea.tail hfb, refl_gen.refl⟩ }, { rcases h _ _ _ hfb hfa with ⟨c, hbc, hac⟩, exact ⟨c, hea.trans hac, hbc⟩ } } }, rcases this with ⟨a, hea, hba⟩, cases hba with _ hba, { exact ⟨b, hea, hcb⟩ }, { exact ⟨a, hea, hcb.tail hba⟩ } } end lemma join_of_single (h : reflexive r) (hab : r a b) : join r a b := ⟨b, hab, h b⟩ lemma symmetric_join : symmetric (join r) := assume a b ⟨c, hac, hcb⟩, ⟨c, hcb, hac⟩ lemma reflexive_join (h : reflexive r) : reflexive (join r) := assume a, ⟨a, h a, h a⟩ lemma transitive_join (ht : transitive r) (h : ∀a b c, r a b → r a c → join r b c) : transitive (join r) := assume a b c ⟨x, hax, hbx⟩ ⟨y, hby, hcy⟩, let ⟨z, hxz, hyz⟩ := h b x y hbx hby in ⟨z, ht hax hxz, ht hcy hyz⟩ lemma equivalence_join (hr : reflexive r) (ht : transitive r) (h : ∀a b c, r a b → r a c → join r b c) : equivalence (join r) := ⟨reflexive_join hr, symmetric_join, transitive_join ht h⟩ lemma equivalence_join_refl_trans_gen (h : ∀a b c, r a b → r a c → ∃d, refl_gen r b d ∧ refl_trans_gen r c d) : equivalence (join (refl_trans_gen r)) := equivalence_join reflexive_refl_trans_gen transitive_refl_trans_gen (assume a b c, church_rosser h) lemma join_of_equivalence {r' : α → α → Prop} (hr : equivalence r) (h : ∀a b, r' a b → r a b) : join r' a b → r a b | ⟨c, hac, hbc⟩ := hr.2.2 (h _ _ hac) (hr.2.1 $ h _ _ hbc) lemma refl_trans_gen_of_transitive_reflexive {r' : α → α → Prop} (hr : reflexive r) (ht : transitive r) (h : ∀a b, r' a b → r a b) (h' : refl_trans_gen r' a b) : r a b := begin induction h' with b c hab hbc ih, { exact hr _ }, { exact ht ih (h _ _ hbc) } end lemma refl_trans_gen_of_equivalence {r' : α → α → Prop} (hr : equivalence r) : (∀a b, r' a b → r a b) → refl_trans_gen r' a b → r a b := refl_trans_gen_of_transitive_reflexive hr.1 hr.2.2 end join section eqv_gen lemma eqv_gen_iff_of_equivalence (h : equivalence r) : eqv_gen r a b ↔ r a b := iff.intro begin assume h, induction h, case eqv_gen.rel { assumption }, case eqv_gen.refl { exact h.1 _ }, case eqv_gen.symm { apply h.2.1, assumption }, case eqv_gen.trans : a b c _ _ hab hbc { exact h.2.2 hab hbc } end (eqv_gen.rel a b) lemma eqv_gen_mono {r p : α → α → Prop} (hrp : ∀a b, r a b → p a b) (h : eqv_gen r a b) : eqv_gen p a b := begin induction h, case eqv_gen.rel : a b h { exact eqv_gen.rel _ _ (hrp _ _ h) }, case eqv_gen.refl : { exact eqv_gen.refl _ }, case eqv_gen.symm : a b h ih { exact eqv_gen.symm _ _ ih }, case eqv_gen.trans : a b c ih1 ih2 hab hbc { exact eqv_gen.trans _ _ _ hab hbc } end end eqv_gen end relation
505499f91d88c0628bc00854a67ba05f186af2ef
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/scc_auto.lean
cb5231190acadcdd5808b4ab18bd5d855fbbe337
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,577
lean
/- Copyright (c) 2018 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Tactics based on the strongly connected components (SCC) of a graph where the vertices are propositions and the edges are implications found in the context. They are used for finding the sets of equivalent propositions in a set of implications. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.tauto import Mathlib.data.sum import Mathlib.PostPort namespace Mathlib /-! # Strongly Connected Components This file defines tactics to construct proofs of equivalences between a set of mutually equivalent propositions. The tactics use implications transitively to find sets of equivalent propositions. ## Implementation notes The tactics use a strongly connected components algorithm on a graph where propositions are vertices and edges are proofs that the source implies the target. The strongly connected components are therefore sets of propositions that are pairwise equivalent to each other. The resulting strongly connected components are encoded in a disjoint set data structure to facilitate the construction of equivalence proofs between two arbitrary members of an equivalence class. ## Possible generalizations Instead of reasoning about implications and equivalence, we could generalize the machinery to reason about arbitrary partial orders. ## References * Tarjan, R. E. (1972), "Depth-first search and linear graph algorithms", SIAM Journal on Computing, 1 (2): 146–160, doi:10.1137/0201010 * Dijkstra, Edsger (1976), A Discipline of Programming, NJ: Prentice Hall, Ch. 25. * <https://en.wikipedia.org/wiki/Disjoint-set_data_structure> ## Tags graphs, tactic, strongly connected components, disjoint sets -/ namespace tactic /-- `closure` implements a disjoint set data structure using path compression optimization. For the sake of the scc algorithm, it also stores the preorder numbering of the equivalence graph of the local assumptions. The `expr_map` encodes a directed forest by storing for every non-root node, a reference to its parent and a proof of equivalence between that node's expression and its parent's expression. Given that data structure, checking that two nodes belong to the same tree is easy and fast by repeatedly following the parent references until a root is reached. If both nodes have the same root, they belong to the same tree, i.e. their expressions are equivalent. The proof of equivalence can be formed by composing the proofs along the edges of the paths to the root. More concretely, if we ignore preorder numbering, the set `{ {e₀,e₁,e₂,e₃}, {e₄,e₅} }` is represented as: ``` e₀ → ⊥ -- no parent, i.e. e₀ is a root e₁ → e₀, p₁ -- with p₁ : e₁ ↔ e₀ e₂ → e₁, p₂ -- with p₂ : e₂ ↔ e₁ e₃ → e₀, p₃ -- with p₃ : e₃ ↔ e₀ e₄ → ⊥ -- no parent, i.e. e₄ is a root e₅ → e₄, p₅ -- with p₅ : e₅ ↔ e₄ ``` We can check that `e₂` and `e₃` are equivalent by seeking the root of the tree of each. The parent of `e₂` is `e₁`, the parent of `e₁` is `e₀` and `e₀` does not have a parent, and thus, this is the root of its tree. The parent of `e₃` is `e₀` and it's also the root, the same as for `e₂` and they are therefore equivalent. We can build a proof of that equivalence by using transitivity on `p₂`, `p₁` and `p₃.symm` in that order. Similarly, we can discover that `e₂` and `e₅` aren't equivalent. A description of the path compression optimization can be found at: <https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Path_compression> -/ namespace closure /-- `with_new_closure f` creates an empty `closure` `c`, executes `f` on `c`, and then deletes `c`, returning the output of `f`. -/ /-- `to_tactic_format cl` pretty-prints the `closure` `cl` as a list. Assuming `cl` was built by `dfs_at`, each element corresponds to a node `pᵢ : expr` and is one of the folllowing: - if `pᵢ` is a root: `"pᵢ ⇐ i"`, where `i` is the preorder number of `pᵢ`, - otherwise: `"(pᵢ, pⱼ) : P"`, where `P` is `pᵢ ↔ pⱼ`. Useful for debugging. -/ /-- `(n,r,p) ← root cl e` returns `r` the root of the tree that `e` is a part of (which might be itself) along with `p` a proof of `e ↔ r` and `n`, the preorder numbering of the root. -/ /-- (Implementation of `merge`.) -/ /-- `merge cl p`, with `p` a proof of `e₀ ↔ e₁` for some `e₀` and `e₁`, merges the trees of `e₀` and `e₁` and keeps the root with the smallest preorder number as the root. This ensures that, in the depth-first traversal of the graph, when encountering an edge going into a vertex whose equivalence class includes a vertex that originated the current search, that vertex will be the root of the corresponding tree. -/ /-- Sequentially assign numbers to the nodes of the graph as they are being visited. -/ /-- `prove_eqv cl e₀ e₁` constructs a proof of equivalence of `e₀` and `e₁` if they are equivalent. -/ /-- `prove_impl cl e₀ e₁` constructs a proof of `e₀ -> e₁` if they are equivalent. -/ /-- `is_eqv cl e₀ e₁` checks whether `e₀` and `e₁` are equivalent without building a proof. -/ end closure /-- mutable graphs between local propositions that imply each other with the proof of implication -/ /-- `with_impl_graph f` creates an empty `impl_graph` `g`, executes `f` on `g`, and then deletes `g`, returning the output of `f`. -/ namespace impl_graph /-- `add_edge g p`, with `p` a proof of `v₀ → v₁` or `v₀ ↔ v₁`, adds an edge to the implication graph `g`. -/ /-- `merge_path path e`, where `path` and `e` forms a cycle with proofs of implication between consecutive vertices. The proofs are compiled into proofs of equivalences and added to the closure structure. `e` and the first vertex of `path` do not have to be the same but they have to be in the same equivalence class. -/ /-- (implementation of `collapse`) -/ /-- `collapse path v`, where `v` is a vertex that originated the current search (or a vertex in the same equivalence class as the one that originated the current search). It or its equivalent should be found in `path`. Since the vertices following `v` in the path form a cycle with `v`, they can all be added to an equivalence class. -/ /-- Strongly connected component algorithm inspired by Tarjan's and Dijkstra's scc algorithm. Whereas they return strongly connected components by enumerating them, this algorithm returns a disjoint set data structure using path compression. This is a compact representation that allows us, after the fact, to construct a proof of equivalence between any two members of an equivalence class. * Tarjan, R. E. (1972), "Depth-first search and linear graph algorithms", SIAM Journal on Computing, 1 (2): 146–160, doi:10.1137/0201010 * Dijkstra, Edsger (1976), A Discipline of Programming, NJ: Prentice Hall, Ch. 25. -/ /-- Use the local assumptions to create a set of equivalence classes. -/ end impl_graph /-- `scc` uses the available equivalences and implications to prove a goal of the form `p ↔ q`. ```lean example (p q r : Prop) (hpq : p → q) (hqr : q ↔ r) (hrp : r → p) : p ↔ r := by scc ``` -/ /-- Collect all the available equivalences and implications and add assumptions for every equivalence that can be proven using the strongly connected components technique. Mostly useful for testing. -/ /-- `scc` uses the available equivalences and implications to prove end Mathlib
533622d6c99941fb819a1e8f9057705009c99811
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/algebra/char_p.lean
118d11262780640e0f4f94c8fc67cf1c373eb49e
[ "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
10,691
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kenny Lau, Joey van Langen, Casper Putz -/ import data.fintype.basic import data.nat.choose import data.int.modeq import algebra.module import algebra.iterate_hom /-! # Characteristic of semirings -/ universes u v /-- The generator of the kernel of the unique homomorphism ℕ → α for a semiring α -/ class char_p (α : Type u) [semiring α] (p : ℕ) : Prop := (cast_eq_zero_iff [] : ∀ x:ℕ, (x:α) = 0 ↔ p ∣ x) theorem char_p.cast_eq_zero (α : Type u) [semiring α] (p : ℕ) [char_p α p] : (p:α) = 0 := (char_p.cast_eq_zero_iff α p p).2 (dvd_refl p) lemma char_p.int_cast_eq_zero_iff (R : Type u) [ring R] (p : ℕ) [char_p R p] (a : ℤ) : (a : R) = 0 ↔ (p:ℤ) ∣ a := begin rcases lt_trichotomy a 0 with h|rfl|h, { rw [← neg_eq_zero, ← int.cast_neg, ← dvd_neg], lift -a to ℕ using neg_nonneg.mpr (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] }, { simp only [int.cast_zero, eq_self_iff_true, dvd_zero] }, { lift a to ℕ using (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] } end lemma char_p.int_coe_eq_int_coe_iff (R : Type*) [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = (b : R) ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd] theorem char_p.eq (α : Type u) [semiring α] {p q : ℕ} (c1 : char_p α p) (c2 : char_p α q) : p = q := nat.dvd_antisymm ((char_p.cast_eq_zero_iff α p q).1 (char_p.cast_eq_zero _ _)) ((char_p.cast_eq_zero_iff α q p).1 (char_p.cast_eq_zero _ _)) instance char_p.of_char_zero (α : Type u) [semiring α] [char_zero α] : char_p α 0 := ⟨λ x, by rw [zero_dvd_iff, ← nat.cast_zero, nat.cast_inj]⟩ theorem char_p.exists (α : Type u) [semiring α] : ∃ p, char_p α p := by letI := classical.dec_eq α; exact classical.by_cases (assume H : ∀ p:ℕ, (p:α) = 0 → p = 0, ⟨0, ⟨λ x, by rw [zero_dvd_iff]; exact ⟨H x, by rintro rfl; refl⟩⟩⟩) (λ H, ⟨nat.find (classical.not_forall.1 H), ⟨λ x, ⟨λ H1, nat.dvd_of_mod_eq_zero (by_contradiction $ λ H2, nat.find_min (classical.not_forall.1 H) (nat.mod_lt x $ nat.pos_of_ne_zero $ not_of_not_imp $ nat.find_spec (classical.not_forall.1 H)) (not_imp_of_and_not ⟨by rwa [← nat.mod_add_div x (nat.find (classical.not_forall.1 H)), nat.cast_add, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (classical.not_forall.1 H)), zero_mul, add_zero] at H1, H2⟩)), λ H1, by rw [← nat.mul_div_cancel' H1, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (classical.not_forall.1 H)), zero_mul]⟩⟩⟩) theorem char_p.exists_unique (α : Type u) [semiring α] : ∃! p, char_p α p := let ⟨c, H⟩ := char_p.exists α in ⟨c, H, λ y H2, char_p.eq α H2 H⟩ /-- Noncomuptable function that outputs the unique characteristic of a semiring. -/ noncomputable def ring_char (α : Type u) [semiring α] : ℕ := classical.some (char_p.exists_unique α) theorem ring_char.spec (α : Type u) [semiring α] : ∀ x:ℕ, (x:α) = 0 ↔ ring_char α ∣ x := by letI := (classical.some_spec (char_p.exists_unique α)).1; unfold ring_char; exact char_p.cast_eq_zero_iff α (ring_char α) theorem ring_char.eq (α : Type u) [semiring α] {p : ℕ} (C : char_p α p) : p = ring_char α := (classical.some_spec (char_p.exists_unique α)).2 p C theorem add_pow_char (α : Type u) [comm_ring α] {p : ℕ} (hp : nat.prime p) [char_p α p] (x y : α) : (x + y)^p = x^p + y^p := begin rw [add_pow, finset.sum_range_succ, nat.sub_self, pow_zero, nat.choose_self], rw [nat.cast_one, mul_one, mul_one, add_right_inj], transitivity, { refine finset.sum_eq_single 0 _ _, { intros b h1 h2, have := nat.prime.dvd_choose_self (nat.pos_of_ne_zero h2) (finset.mem_range.1 h1) hp, rw [← nat.div_mul_cancel this, nat.cast_mul, char_p.cast_eq_zero α p], simp only [mul_zero] }, { intro H, exfalso, apply H, exact finset.mem_range.2 hp.pos } }, rw [pow_zero, nat.sub_zero, one_mul, nat.choose_zero_right, nat.cast_one, mul_one] end lemma eq_iff_modeq_int (R : Type*) [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = b ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd] lemma char_p.neg_one_ne_one (R : Type*) [ring R] (p : ℕ) [char_p R p] [fact (2 < p)] : (-1 : R) ≠ (1 : R) := begin suffices : (2 : R) ≠ 0, { symmetry, rw [ne.def, ← sub_eq_zero, sub_neg_eq_add], exact this }, assume h, rw [show (2 : R) = (2 : ℕ), by norm_cast] at h, have := (char_p.cast_eq_zero_iff R p 2).mp h, have := nat.le_of_dvd dec_trivial this, rw fact at *, linarith, end section frobenius variables (R : Type u) [comm_ring R] {S : Type v} [comm_ring S] (f : R →* S) (g : R →+* S) (p : ℕ) [fact p.prime] [char_p R p] [char_p S p] (x y : R) /-- The frobenius map that sends x to x^p -/ def frobenius : R →+* R := { to_fun := λ x, x^p, map_one' := one_pow p, map_mul' := λ x y, mul_pow x y p, map_zero' := zero_pow (lt_trans zero_lt_one ‹nat.prime p›.one_lt), map_add' := add_pow_char R ‹nat.prime p› } variable {R} theorem frobenius_def : frobenius R p x = x ^ p := rfl theorem frobenius_mul : frobenius R p (x * y) = frobenius R p x * frobenius R p y := (frobenius R p).map_mul x y theorem frobenius_one : frobenius R p 1 = 1 := one_pow _ variable {R} theorem monoid_hom.map_frobenius : f (frobenius R p x) = frobenius S p (f x) := f.map_pow x p theorem ring_hom.map_frobenius : g (frobenius R p x) = frobenius S p (g x) := g.map_pow x p theorem monoid_hom.map_iterate_frobenius (n : ℕ) : f (frobenius R p^[n] x) = (frobenius S p^[n] (f x)) := function.semiconj.iterate_right (f.map_frobenius p) n x theorem ring_hom.map_iterate_frobenius (n : ℕ) : g (frobenius R p^[n] x) = (frobenius S p^[n] (g x)) := g.to_monoid_hom.map_iterate_frobenius p x n theorem monoid_hom.iterate_map_frobenius (f : R →* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ theorem ring_hom.iterate_map_frobenius (f : R →+* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ variable (R) theorem frobenius_zero : frobenius R p 0 = 0 := (frobenius R p).map_zero theorem frobenius_add : frobenius R p (x + y) = frobenius R p x + frobenius R p y := (frobenius R p).map_add x y theorem frobenius_neg : frobenius R p (-x) = -frobenius R p x := (frobenius R p).map_neg x theorem frobenius_sub : frobenius R p (x - y) = frobenius R p x - frobenius R p y := (frobenius R p).map_sub x y theorem frobenius_nat_cast (n : ℕ) : frobenius R p n = n := (frobenius R p).map_nat_cast n end frobenius theorem frobenius_inj (α : Type u) [integral_domain α] (p : ℕ) [fact p.prime] [char_p α p] : function.injective (frobenius α p) := λ x h H, by { rw ← sub_eq_zero at H ⊢, rw ← frobenius_sub at H, exact pow_eq_zero H } namespace char_p section variables (α : Type u) [ring α] lemma char_p_to_char_zero [char_p α 0] : char_zero α := add_group.char_zero_of_inj_zero $ λ n h0, eq_zero_of_zero_dvd ((cast_eq_zero_iff α 0 n).mp h0) lemma cast_eq_mod (p : ℕ) [char_p α p] (k : ℕ) : (k : α) = (k % p : ℕ) := calc (k : α) = ↑(k % p + p * (k / p)) : by rw [nat.mod_add_div] ... = ↑(k % p) : by simp[cast_eq_zero] theorem char_ne_zero_of_fintype (p : ℕ) [hc : char_p α p] [fintype α] : p ≠ 0 := assume h : p = 0, have char_zero α := @char_p_to_char_zero α _ (h ▸ hc), absurd (@nat.cast_injective α _ _ this) (not_injective_infinite_fintype coe) end section integral_domain open nat variables (α : Type u) [integral_domain α] theorem char_ne_one (p : ℕ) [hc : char_p α p] : p ≠ 1 := assume hp : p = 1, have ( 1 : α) = 0, by simpa using (cast_eq_zero_iff α p 1).mpr (hp ▸ dvd_refl p), absurd this one_ne_zero theorem char_is_prime_of_ge_two (p : ℕ) [hc : char_p α p] (hp : p ≥ 2) : nat.prime p := suffices ∀d ∣ p, d = 1 ∨ d = p, from ⟨hp, this⟩, assume (d : ℕ) (hdvd : ∃ e, p = d * e), let ⟨e, hmul⟩ := hdvd in have (p : α) = 0, from (cast_eq_zero_iff α p p).mpr (dvd_refl p), have (d : α) * e = 0, from (@cast_mul α _ d e) ▸ (hmul ▸ this), or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this) (assume hd : (d : α) = 0, have p ∣ d, from (cast_eq_zero_iff α p d).mp hd, show d = 1 ∨ d = p, from or.inr (dvd_antisymm ⟨e, hmul⟩ this)) (assume he : (e : α) = 0, have p ∣ e, from (cast_eq_zero_iff α p e).mp he, have e ∣ p, from dvd_of_mul_left_eq d (eq.symm hmul), have e = p, from dvd_antisymm ‹e ∣ p› ‹p ∣ e›, have h₀ : p > 0, from gt_of_ge_of_gt hp (nat.zero_lt_succ 1), have d * p = 1 * p, by rw ‹e = p› at hmul; rw [one_mul]; exact eq.symm hmul, show d = 1 ∨ d = p, from or.inl (eq_of_mul_eq_mul_right h₀ this)) theorem char_is_prime_or_zero (p : ℕ) [hc : char_p α p] : nat.prime p ∨ p = 0 := match p, hc with | 0, _ := or.inr rfl | 1, hc := absurd (eq.refl (1 : ℕ)) (@char_ne_one α _ (1 : ℕ) hc) | (m+2), hc := or.inl (@char_is_prime_of_ge_two α _ (m+2) hc (nat.le_add_left 2 m)) end lemma char_is_prime_of_pos (p : ℕ) [h : fact (0 < p)] [char_p α p] : fact p.prime := (char_p.char_is_prime_or_zero α _).resolve_right (nat.pos_iff_ne_zero.1 h) theorem char_is_prime [fintype α] (p : ℕ) [char_p α p] : p.prime := or.resolve_right (char_is_prime_or_zero α p) (char_ne_zero_of_fintype α p) end integral_domain section char_one variables {R : Type*} section prio set_option default_priority 100 -- see Note [default priority] instance [semiring R] [char_p R 1] : subsingleton R := subsingleton.intro $ suffices ∀ (r : R), r = 0, from assume a b, show a = b, by rw [this a, this b], assume r, calc r = 1 * r : by rw one_mul ... = (1 : ℕ) * r : by rw nat.cast_one ... = 0 * r : by rw char_p.cast_eq_zero ... = 0 : by rw zero_mul end prio lemma false_of_nonzero_of_char_one [semiring R] [nonzero R] [char_p R 1] : false := zero_ne_one $ show (0:R) = 1, from subsingleton.elim 0 1 lemma ring_char_ne_one [semiring R] [nonzero R] : ring_char R ≠ 1 := by { intros h, apply @zero_ne_one R, symmetry, rw [←nat.cast_one, ring_char.spec, h], } end char_one end char_p
42959d6e8c7d298b867d317b8b164932276de00d
7cef822f3b952965621309e88eadf618da0c8ae9
/src/topology/category/Top/open_nhds.lean
f0501bf1995aedaa374d36b7f5dbb4d0607c1f6a
[ "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
1,807
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import topology.category.Top.opens import category_theory.full_subcategory open category_theory open topological_space open opposite universe u variables {X Y : Top.{u}} (f : X ⟶ Y) namespace topological_space def open_nhds (x : X.α) := { U : opens X // x ∈ U } namespace open_nhds instance open_nhds_category (x : X.α) : category.{u} (open_nhds x) := by {unfold open_nhds, apply_instance} def inclusion (x : X.α) : open_nhds x ⥤ opens X := full_subcategory_inclusion _ @[simp] lemma inclusion_obj (x : X.α) (U) (p) : (inclusion x).obj ⟨U,p⟩ = U := rfl def map (x : X) : open_nhds (f x) ⥤ open_nhds x := { obj := λ U, ⟨(opens.map f).obj U.1, by tidy⟩, map := λ U V i, (opens.map f).map i } @[simp] lemma map_obj (x : X) (U) (q) : (map f x).obj ⟨U, q⟩ = ⟨(opens.map f).obj U, by tidy⟩ := rfl @[simp] lemma map_id_obj' (x : X) (U) (p) (q) : (map (𝟙 X) x).obj ⟨⟨U, p⟩, q⟩ = ⟨⟨U, p⟩, q⟩ := rfl @[simp] lemma map_id_obj (x : X) (U) : (map (𝟙 X) x).obj U = U := by tidy @[simp] lemma map_id_obj_unop (x : X) (U : (open_nhds x)ᵒᵖ) : (map (𝟙 X) x).obj (unop U) = unop U := by simp @[simp] lemma op_map_id_obj (x : X) (U : (open_nhds x)ᵒᵖ) : (map (𝟙 X) x).op.obj U = U := by simp def inclusion_map_iso (x : X) : inclusion (f x) ⋙ opens.map f ≅ map f x ⋙ inclusion x := nat_iso.of_components (λ U, begin split, exact 𝟙 _, exact 𝟙 _ end) (by tidy) @[simp] lemma inclusion_map_iso_hom (x : X) : (inclusion_map_iso f x).hom = 𝟙 _ := rfl @[simp] lemma inclusion_map_iso_inv (x : X) : (inclusion_map_iso f x).inv = 𝟙 _ := rfl end open_nhds end topological_space
a1317666eaf0e5a0edfbce2d9c4d57cde6a49457
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/mutualdef1.lean
131073a388082c18984559df768b46a357a27e46
[ "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
472
lean
new_frontend mutual def f (x : Nat) : Nat → Nat | 0 => 1 | x+1 => g x theorem g (x : Nat) : Nat → Nat -- cannot mix theorems and definitions | 0 => 2 | x+1 => f x end mutual def f : Nat → Nat | 0 => 1 | x+1 => g x example : Nat := -- cannot mix examples and definitions g 10 end mutual def f (x : Nat) : Nat → Nat | 0 => 1 | x+1 => g x unsafe def g (x : Nat) : Nat → Nat -- cannot mix safe and unsafe definitions | 0 => 2 | x+1 => f x end
fdf1221e5587f65dd83814ef775c76552111baef
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/revert1.lean
620b77bbf9b51f59bfc4709f95b26b563bc10c5b
[ "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
304
lean
new_frontend theorem tst1 (x y z : Nat) : y = z → x = x → x = y → x = z := by { intros h1 h2 h3; revert h2; intro h2; exact Eq.trans h3 h1 } theorem tst2 (x y z : Nat) : y = z → x = x → x = y → x = z := by { intros h1 h2 h3; revert y; intros y hb ha; exact Eq.trans ha hb }
98127491994e3daa2c7ea9d423e1e172ba96fdc6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/gluing.lean
525390c4c3be2c49bfe33c92098bfd427b1aca6f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
18,729
lean
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import category_theory.glue_data import category_theory.concrete_category.elementwise import topology.category.Top.limits.pullbacks import topology.category.Top.opens /-! # Gluing Topological spaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Given a family of gluing data (see `category_theory/glue_data`), we can then glue them together. The construction should be "sealed" and considered as a black box, while only using the API provided. ## Main definitions * `Top.glue_data`: A structure containing the family of gluing data. * `category_theory.glue_data.glued`: The glued topological space. This is defined as the multicoequalizer of `∐ V i j ⇉ ∐ U i`, so that the general colimit API can be used. * `category_theory.glue_data.ι`: The immersion `ι i : U i ⟶ glued` for each `i : ι`. * `Top.glue_data.rel`: A relation on `Σ i, D.U i` defined by `⟨i, x⟩ ~ ⟨j, y⟩` iff `⟨i, x⟩ = ⟨j, y⟩` or `t i j x = y`. See `Top.glue_data.ι_eq_iff_rel`. * `Top.glue_data.mk`: A constructor of `glue_data` whose conditions are stated in terms of elements rather than subobjects and pullbacks. * `Top.glue_data.of_open_subsets`: Given a family of open sets, we may glue them into a new topological space. This new space embeds into the original space, and is homeomorphic to it if the given family is an open cover (`Top.glue_data.open_cover_glue_homeo`). ## Main results * `Top.glue_data.is_open_iff`: A set in `glued` is open iff its preimage along each `ι i` is open. * `Top.glue_data.ι_jointly_surjective`: The `ι i`s are jointly surjective. * `Top.glue_data.rel_equiv`: `rel` is an equivalence relation. * `Top.glue_data.ι_eq_iff_rel`: `ι i x = ι j y ↔ ⟨i, x⟩ ~ ⟨j, y⟩`. * `Top.glue_data.image_inter`: The intersection of the images of `U i` and `U j` in `glued` is `V i j`. * `Top.glue_data.preimage_range`: The preimage of the image of `U i` in `U j` is `V i j`. * `Top.glue_data.preimage_image_eq_preimage_f`: The preimage of the image of some `U ⊆ U i` is given by the preimage along `f j i`. * `Top.glue_data.ι_open_embedding`: Each of the `ι i`s are open embeddings. -/ noncomputable theory open topological_space category_theory universes v u open category_theory.limits namespace Top /-- A family of gluing data consists of 1. An index type `J` 2. An object `U i` for each `i : J`. 3. An object `V i j` for each `i j : J`. (Note that this is `J × J → Top` rather than `J → J → Top` to connect to the limits library easier.) 4. An open embedding `f i j : V i j ⟶ U i` for each `i j : ι`. 5. A transition map `t i j : V i j ⟶ V j i` for each `i j : ι`. such that 6. `f i i` is an isomorphism. 7. `t i i` is the identity. 8. `V i j ×[U i] V i k ⟶ V i j ⟶ V j i` factors through `V j k ×[U j] V j i ⟶ V j i` via some `t' : V i j ×[U i] V i k ⟶ V j k ×[U j] V j i`. (This merely means that `V i j ∩ V i k ⊆ t i j ⁻¹' (V j i ∩ V j k)`.) 9. `t' i j k ≫ t' j k i ≫ t' k i j = 𝟙 _`. We can then glue the topological spaces `U i` together by identifying `V i j` with `V j i`, such that the `U i`'s are open subspaces of the glued space. Most of the times it would be easier to use the constructor `Top.glue_data.mk'` where the conditions are stated in a less categorical way. -/ @[nolint has_nonempty_instance] structure glue_data extends glue_data Top := (f_open : ∀ i j, open_embedding (f i j)) (f_mono := λ i j, (Top.mono_iff_injective _).mpr (f_open i j).to_embedding.inj) namespace glue_data variable (D : glue_data.{u}) local notation `𝖣` := D.to_glue_data lemma π_surjective : function.surjective 𝖣 .π := (Top.epi_iff_surjective 𝖣 .π).mp infer_instance lemma is_open_iff (U : set 𝖣 .glued) : is_open U ↔ ∀ i, is_open (𝖣 .ι i ⁻¹' U) := begin delta category_theory.glue_data.ι, simp_rw ← multicoequalizer.ι_sigma_π 𝖣 .diagram, rw ← (homeo_of_iso (multicoequalizer.iso_coequalizer 𝖣 .diagram).symm).is_open_preimage, rw [coequalizer_is_open_iff, colimit_is_open_iff.{u}], split, { intros h j, exact h ⟨j⟩, }, { intros h j, cases j, exact h j, }, end lemma ι_jointly_surjective (x : 𝖣 .glued) : ∃ i (y : D.U i), 𝖣 .ι i y = x := 𝖣 .ι_jointly_surjective (forget Top) x /-- An equivalence relation on `Σ i, D.U i` that holds iff `𝖣 .ι i x = 𝖣 .ι j y`. See `Top.glue_data.ι_eq_iff_rel`. -/ def rel (a b : Σ i, ((D.U i : Top) : Type*)) : Prop := a = b ∨ ∃ (x : D.V (a.1, b.1)) , D.f _ _ x = a.2 ∧ D.f _ _ (D.t _ _ x) = b.2 lemma rel_equiv : equivalence D.rel := ⟨ λ x, or.inl (refl x), begin rintros a b (⟨⟨⟩⟩|⟨x,e₁,e₂⟩), exacts [or.inl rfl, or.inr ⟨D.t _ _ x, by simp [e₁, e₂]⟩] end, begin rintros ⟨i,a⟩ ⟨j,b⟩ ⟨k,c⟩ (⟨⟨⟩⟩|⟨x,e₁,e₂⟩), exact id, rintro (⟨⟨⟩⟩|⟨y,e₃,e₄⟩), exact or.inr ⟨x,e₁,e₂⟩, let z := (pullback_iso_prod_subtype (D.f j i) (D.f j k)).inv ⟨⟨_,_⟩, e₂.trans e₃.symm⟩, have eq₁ : (D.t j i) ((pullback.fst : _ ⟶ D.V _) z) = x := by simp, have eq₂ : (pullback.snd : _ ⟶ D.V _) z = y := pullback_iso_prod_subtype_inv_snd_apply _ _ _, clear_value z, right, use (pullback.fst : _ ⟶ D.V (i, k)) (D.t' _ _ _ z), dsimp only at *, substs e₁ e₃ e₄ eq₁ eq₂, have h₁ : D.t' j i k ≫ pullback.fst ≫ D.f i k = pullback.fst ≫ D.t j i ≫ D.f i j, { rw ← 𝖣 .t_fac_assoc, congr' 1, exact pullback.condition }, have h₂ : D.t' j i k ≫ pullback.fst ≫ D.t i k ≫ D.f k i = pullback.snd ≫ D.t j k ≫ D.f k j, { rw ← 𝖣 .t_fac_assoc, apply @epi.left_cancellation _ _ _ _ (D.t' k j i), rw [𝖣 .cocycle_assoc, 𝖣 .t_fac_assoc, 𝖣 .t_inv_assoc], exact pullback.condition.symm }, exact ⟨continuous_map.congr_fun h₁ z, continuous_map.congr_fun h₂ z⟩ end⟩ open category_theory.limits.walking_parallel_pair lemma eqv_gen_of_π_eq {x y : ∐ D.U} (h : 𝖣 .π x = 𝖣 .π y) : eqv_gen (types.coequalizer_rel 𝖣 .diagram.fst_sigma_map 𝖣 .diagram.snd_sigma_map) x y := begin delta glue_data.π multicoequalizer.sigma_π at h, simp_rw comp_app at h, replace h := (Top.mono_iff_injective (multicoequalizer.iso_coequalizer 𝖣 .diagram).inv).mp _ h, let diagram := parallel_pair 𝖣 .diagram.fst_sigma_map 𝖣 .diagram.snd_sigma_map ⋙ forget _, have : colimit.ι diagram one x = colimit.ι diagram one y, { rw ←ι_preserves_colimits_iso_hom, simp [h] }, have : (colimit.ι diagram _ ≫ colim.map _ ≫ (colimit.iso_colimit_cocone _).hom) _ = (colimit.ι diagram _ ≫ colim.map _ ≫ (colimit.iso_colimit_cocone _).hom) _ := (congr_arg (colim.map (diagram_iso_parallel_pair diagram).hom ≫ (colimit.iso_colimit_cocone (types.coequalizer_colimit _ _)).hom) this : _), simp only [eq_to_hom_refl, types_comp_apply, colimit.ι_map_assoc, diagram_iso_parallel_pair_hom_app, colimit.iso_colimit_cocone_ι_hom, types_id_apply] at this, exact quot.eq.1 this, apply_instance end lemma ι_eq_iff_rel (i j : D.J) (x : D.U i) (y : D.U j) : 𝖣 .ι i x = 𝖣 .ι j y ↔ D.rel ⟨i, x⟩ ⟨j, y⟩ := begin split, { delta glue_data.ι, simp_rw ← multicoequalizer.ι_sigma_π, intro h, rw ← (show _ = sigma.mk i x, from concrete_category.congr_hom (sigma_iso_sigma.{u} D.U).inv_hom_id _), rw ← (show _ = sigma.mk j y, from concrete_category.congr_hom (sigma_iso_sigma.{u} D.U).inv_hom_id _), change inv_image D.rel (sigma_iso_sigma.{u} D.U).hom _ _, simp only [Top.sigma_iso_sigma_inv_apply], rw ← (inv_image.equivalence _ _ D.rel_equiv).eqv_gen_iff, refine eqv_gen.mono _ (D.eqv_gen_of_π_eq h : _), rintros _ _ ⟨x⟩, rw ← (show (sigma_iso_sigma.{u} _).inv _ = x, from concrete_category.congr_hom (sigma_iso_sigma.{u} _).hom_inv_id x), generalize : (sigma_iso_sigma.{u} D.V).hom x = x', obtain ⟨⟨i,j⟩,y⟩ := x', unfold inv_image multispan_index.fst_sigma_map multispan_index.snd_sigma_map, simp only [opens.inclusion_apply, Top.comp_app, sigma_iso_sigma_inv_apply, category_theory.limits.colimit.ι_desc_apply, cofan.mk_ι_app, sigma_iso_sigma_hom_ι_apply, continuous_map.to_fun_eq_coe], erw [sigma_iso_sigma_hom_ι_apply, sigma_iso_sigma_hom_ι_apply], exact or.inr ⟨y, by { dsimp [glue_data.diagram], simp }⟩ }, { rintro (⟨⟨⟩⟩|⟨z,e₁,e₂⟩), refl, dsimp only at *, subst e₁, subst e₂, simp } end lemma ι_injective (i : D.J) : function.injective (𝖣 .ι i) := begin intros x y h, rcases (D.ι_eq_iff_rel _ _ _ _).mp h with (⟨⟨⟩⟩|⟨_,e₁,e₂⟩), { refl }, { dsimp only at *, cases e₁, cases e₂, simp } end instance ι_mono (i : D.J) : mono (𝖣 .ι i) := (Top.mono_iff_injective _).mpr (D.ι_injective _) lemma image_inter (i j : D.J) : set.range (𝖣 .ι i) ∩ set.range (𝖣 .ι j) = set.range (D.f i j ≫ 𝖣 .ι _) := begin ext x, split, { rintro ⟨⟨x₁, eq₁⟩, ⟨x₂, eq₂⟩⟩, obtain (⟨⟨⟩⟩|⟨y,e₁,e₂⟩) := (D.ι_eq_iff_rel _ _ _ _).mp (eq₁.trans eq₂.symm), { exact ⟨inv (D.f i i) x₁, by simp [eq₁]⟩ }, { dsimp only at *, substs e₁ eq₁, exact ⟨y, by simp⟩ } }, { rintro ⟨x, hx⟩, exact ⟨⟨D.f i j x, hx⟩, ⟨D.f j i (D.t _ _ x), by simp [← hx]⟩⟩ } end lemma preimage_range (i j : D.J) : 𝖣 .ι j ⁻¹' (set.range (𝖣 .ι i)) = set.range (D.f j i) := by rw [← set.preimage_image_eq (set.range (D.f j i)) (D.ι_injective j), ← set.image_univ, ← set.image_univ, ←set.image_comp, ←coe_comp, set.image_univ,set.image_univ, ← image_inter, set.preimage_range_inter] lemma preimage_image_eq_image (i j : D.J) (U : set (𝖣 .U i)) : 𝖣 .ι j ⁻¹' (𝖣 .ι i '' U) = D.f _ _ '' ((D.t j i ≫ D.f _ _) ⁻¹' U) := begin have : D.f _ _ ⁻¹' (𝖣 .ι j ⁻¹' (𝖣 .ι i '' U)) = (D.t j i ≫ D.f _ _) ⁻¹' U, { ext x, conv_rhs { rw ← set.preimage_image_eq U (D.ι_injective _) }, generalize : 𝖣 .ι i '' U = U', simp }, rw [← this, set.image_preimage_eq_inter_range], symmetry, apply set.inter_eq_self_of_subset_left, rw ← D.preimage_range i j, exact set.preimage_mono (set.image_subset_range _ _), end lemma preimage_image_eq_image' (i j : D.J) (U : set (𝖣 .U i)) : 𝖣 .ι j ⁻¹' (𝖣 .ι i '' U) = (D.t i j ≫ D.f _ _) '' ((D.f _ _) ⁻¹' U) := begin convert D.preimage_image_eq_image i j U using 1, rw [coe_comp, coe_comp, ← set.image_image], congr' 1, rw [← set.eq_preimage_iff_image_eq, set.preimage_preimage], change _ = (D.t i j ≫ D.t j i ≫ _) ⁻¹' _, rw 𝖣 .t_inv_assoc, rw ← is_iso_iff_bijective, apply (forget Top).map_is_iso end lemma open_image_open (i : D.J) (U : opens (𝖣 .U i)) : is_open (𝖣 .ι i '' U) := begin rw is_open_iff, intro j, rw preimage_image_eq_image, apply (D.f_open _ _).is_open_map, apply (D.t j i ≫ D.f i j).continuous_to_fun.is_open_preimage, exact U.is_open end lemma ι_open_embedding (i : D.J) : open_embedding (𝖣 .ι i) := open_embedding_of_continuous_injective_open (𝖣 .ι i).continuous_to_fun (D.ι_injective i) (λ U h, D.open_image_open i ⟨U, h⟩) /-- A family of gluing data consists of 1. An index type `J` 2. A bundled topological space `U i` for each `i : J`. 3. An open set `V i j ⊆ U i` for each `i j : J`. 4. A transition map `t i j : V i j ⟶ V j i` for each `i j : ι`. such that 6. `V i i = U i`. 7. `t i i` is the identity. 8. For each `x ∈ V i j ∩ V i k`, `t i j x ∈ V j k`. 9. `t j k (t i j x) = t i k x`. We can then glue the topological spaces `U i` together by identifying `V i j` with `V j i`. -/ @[nolint has_nonempty_instance] structure mk_core := {J : Type u} (U : J → Top.{u}) (V : Π i, J → opens (U i)) (t : Π i j, (opens.to_Top _).obj (V i j) ⟶ (opens.to_Top _).obj (V j i)) (V_id : ∀ i, V i i = ⊤) (t_id : ∀ i, ⇑(t i i) = id) (t_inter : ∀ ⦃i j⦄ k (x : V i j), ↑x ∈ V i k → @coe (V j i) (U j) _ (t i j x) ∈ V j k) (cocycle : ∀ i j k (x : V i j) (h : ↑x ∈ V i k), @coe (V k j) (U k) _ (t j k ⟨↑(t i j x), t_inter k x h⟩) = @coe (V k i) (U k) _ (t i k ⟨x, h⟩)) lemma mk_core.t_inv (h : mk_core) (i j : h.J) (x : h.V j i) : h.t i j ((h.t j i) x) = x := begin have := h.cocycle j i j x _, rw h.t_id at this, convert subtype.eq this, { ext, refl }, all_goals { rw h.V_id, trivial } end instance (h : mk_core.{u}) (i j : h.J) : is_iso (h.t i j) := by { use h.t j i, split; ext1, exacts [h.t_inv _ _ _, h.t_inv _ _ _] } /-- (Implementation) the restricted transition map to be fed into `glue_data`. -/ def mk_core.t' (h : mk_core.{u}) (i j k : h.J) : pullback (h.V i j).inclusion (h.V i k).inclusion ⟶ pullback (h.V j k).inclusion (h.V j i).inclusion := begin refine (pullback_iso_prod_subtype _ _).hom ≫ ⟨_, _⟩ ≫ (pullback_iso_prod_subtype _ _).inv, { intro x, refine ⟨⟨⟨(h.t i j x.1.1).1, _⟩, h.t i j x.1.1⟩, rfl⟩, rcases x with ⟨⟨⟨x, hx⟩, ⟨x', hx'⟩⟩, (rfl : x = x')⟩, exact h.t_inter _ ⟨x, hx⟩ hx' }, continuity, end /-- This is a constructor of `Top.glue_data` whose arguments are in terms of elements and intersections rather than subobjects and pullbacks. Please refer to `Top.glue_data.mk_core` for details. -/ def mk' (h : mk_core.{u}) : Top.glue_data := { J := h.J, U := h.U, V := λ i, (opens.to_Top _).obj (h.V i.1 i.2), f := λ i j, (h.V i j).inclusion , f_id := λ i, (h.V_id i).symm ▸ is_iso.of_iso (opens.inclusion_top_iso (h.U i)), f_open := λ (i j : h.J), (h.V i j).open_embedding, t := h.t, t_id := λ i, by { ext, rw h.t_id, refl }, t' := h.t', t_fac := λ i j k, begin delta mk_core.t', rw [category.assoc, category.assoc, pullback_iso_prod_subtype_inv_snd, ← iso.eq_inv_comp, pullback_iso_prod_subtype_inv_fst_assoc], ext ⟨⟨⟨x, hx⟩, ⟨x', hx'⟩⟩, (rfl : x = x')⟩, refl, end, cocycle := λ i j k, begin delta mk_core.t', simp_rw ← category.assoc, rw iso.comp_inv_eq, simp only [iso.inv_hom_id_assoc, category.assoc, category.id_comp], rw [← iso.eq_inv_comp, iso.inv_hom_id], ext1 ⟨⟨⟨x, hx⟩, ⟨x', hx'⟩⟩, (rfl : x = x')⟩, simp only [Top.comp_app, continuous_map.coe_mk, prod.mk.inj_iff, Top.id_app, subtype.mk_eq_mk, subtype.coe_mk], rw [← subtype.coe_injective.eq_iff, subtype.val_eq_coe, subtype.coe_mk, and_self], convert congr_arg coe (h.t_inv k i ⟨x, hx'⟩) using 3, ext, exact h.cocycle i j k ⟨x, hx⟩ hx', end } . variables {α : Type u} [topological_space α] {J : Type u} (U : J → opens α) include U /-- We may construct a glue data from a family of open sets. -/ @[simps to_glue_data_J to_glue_data_U to_glue_data_V to_glue_data_t to_glue_data_f] def of_open_subsets : Top.glue_data.{u} := mk'.{u} { J := J, U := λ i, (opens.to_Top $ Top.of α).obj (U i), V := λ i j, (opens.map $ opens.inclusion _).obj (U j), t := λ i j, ⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, by continuity⟩, V_id := λ i, by { ext, cases U i, simp }, t_id := λ i, by { ext, refl }, t_inter := λ i j k x hx, hx, cocycle := λ i j k x h, rfl } /-- The canonical map from the glue of a family of open subsets `α` into `α`. This map is an open embedding (`from_open_subsets_glue_open_embedding`), and its range is `⋃ i, (U i : set α)` (`range_from_open_subsets_glue`). -/ def from_open_subsets_glue : (of_open_subsets U).to_glue_data.glued ⟶ Top.of α := multicoequalizer.desc _ _ (λ x, opens.inclusion _) (by { rintro ⟨i, j⟩, ext x, refl }) @[simp, elementwise] lemma ι_from_open_subsets_glue (i : J) : (of_open_subsets U).to_glue_data.ι i ≫ from_open_subsets_glue U = opens.inclusion _ := multicoequalizer.π_desc _ _ _ _ _ lemma from_open_subsets_glue_injective : function.injective (from_open_subsets_glue U) := begin intros x y e, obtain ⟨i, ⟨x, hx⟩, rfl⟩ := (of_open_subsets U).ι_jointly_surjective x, obtain ⟨j, ⟨y, hy⟩, rfl⟩ := (of_open_subsets U).ι_jointly_surjective y, rw [ι_from_open_subsets_glue_apply, ι_from_open_subsets_glue_apply] at e, change x = y at e, subst e, rw (of_open_subsets U).ι_eq_iff_rel, right, exact ⟨⟨⟨x, hx⟩, hy⟩, rfl, rfl⟩, end lemma from_open_subsets_glue_is_open_map : is_open_map (from_open_subsets_glue U) := begin intros s hs, rw (of_open_subsets U).is_open_iff at hs, rw is_open_iff_forall_mem_open, rintros _ ⟨x, hx, rfl⟩, obtain ⟨i, ⟨x, hx'⟩, rfl⟩ := (of_open_subsets U).ι_jointly_surjective x, use from_open_subsets_glue U '' s ∩ set.range (@opens.inclusion (Top.of α) (U i)), use set.inter_subset_left _ _, split, { erw ← set.image_preimage_eq_inter_range, apply (@opens.open_embedding (Top.of α) (U i)).is_open_map, convert hs i using 1, rw [← ι_from_open_subsets_glue, coe_comp, set.preimage_comp], congr' 1, refine set.preimage_image_eq _ (from_open_subsets_glue_injective U) }, { refine ⟨set.mem_image_of_mem _ hx, _⟩, rw ι_from_open_subsets_glue_apply, exact set.mem_range_self _ }, end lemma from_open_subsets_glue_open_embedding : open_embedding (from_open_subsets_glue U) := open_embedding_of_continuous_injective_open (continuous_map.continuous_to_fun _) (from_open_subsets_glue_injective U) (from_open_subsets_glue_is_open_map U) lemma range_from_open_subsets_glue : set.range (from_open_subsets_glue U) = ⋃ i, (U i : set α) := begin ext, split, { rintro ⟨x, rfl⟩, obtain ⟨i, ⟨x, hx'⟩, rfl⟩ := (of_open_subsets U).ι_jointly_surjective x, rw ι_from_open_subsets_glue_apply, exact set.subset_Union _ i hx' }, { rintro ⟨_, ⟨i, rfl⟩, hx⟩, refine ⟨(of_open_subsets U).to_glue_data.ι i ⟨x, hx⟩, ι_from_open_subsets_glue_apply _ _ _⟩ } end /-- The gluing of an open cover is homeomomorphic to the original space. -/ def open_cover_glue_homeo (h : (⋃ i, (U i : set α)) = set.univ) : (of_open_subsets U).to_glue_data.glued ≃ₜ α := homeomorph.homeomorph_of_continuous_open (equiv.of_bijective (from_open_subsets_glue U) ⟨from_open_subsets_glue_injective U, set.range_iff_surjective.mp ((range_from_open_subsets_glue U).symm ▸ h)⟩) (from_open_subsets_glue U).2 (from_open_subsets_glue_is_open_map U) end glue_data end Top
55a5713f82b041c45b08b16a6eaf54fb399f18ae
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/filtered_colimit_commutes_finite_limit.lean
d320a23a66c89c0e253beb113fd2adf92236c4bf
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
17,150
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 category_theory.limits.colimit_limit import category_theory.limits.preserves.functor_category import category_theory.limits.preserves.finite import category_theory.limits.shapes.finite_limits import category_theory.limits.preserves.filtered import category_theory.concrete_category.basic /-! # Filtered colimits commute with finite limits. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We show that for a functor `F : J × K ⥤ Type v`, when `J` is finite and `K` is filtered, the universal morphism `colimit_limit_to_limit_colimit F` comparing the colimit (over `K`) of the limits (over `J`) with the limit of the colimits is an isomorphism. (In fact, to prove that it is injective only requires that `J` has finitely many objects.) ## References * Borceux, Handbook of categorical algebra 1, Theorem 2.13.4 * [Stacks: Filtered colimits](https://stacks.math.columbia.edu/tag/002W) -/ universes v u open category_theory open category_theory.category open category_theory.limits.types open category_theory.limits.types.filtered_colimit namespace category_theory.limits variables {J K : Type v} [small_category J] [small_category K] variables (F : J × K ⥤ Type v) open category_theory.prod variables [is_filtered K] section /-! Injectivity doesn't need that we have finitely many morphisms in `J`, only that there are finitely many objects. -/ variables [finite J] /-- This follows this proof from * Borceux, Handbook of categorical algebra 1, Theorem 2.13.4 -/ lemma colimit_limit_to_limit_colimit_injective : function.injective (colimit_limit_to_limit_colimit F) := begin classical, casesI nonempty_fintype J, -- Suppose we have two terms `x y` in the colimit (over `K`) of the limits (over `J`), -- and that these have the same image under `colimit_limit_to_limit_colimit F`. intros x y h, -- These elements of the colimit have representatives somewhere: obtain ⟨kx, x, rfl⟩ := jointly_surjective'.{v v} x, obtain ⟨ky, y, rfl⟩ := jointly_surjective'.{v v} y, dsimp at x y, -- Since the images of `x` and `y` are equal in a limit, they are equal componentwise -- (indexed by `j : J`), replace h := λ j, congr_arg (limit.π ((curry.obj F) ⋙ colim) j) h, -- and they are equations in a filtered colimit, -- so for each `j` we have some place `k j` to the right of both `kx` and `ky` simp [colimit_eq_iff.{v v}] at h, let k := λ j, (h j).some, let f : Π j, kx ⟶ k j := λ j, (h j).some_spec.some, let g : Π j, ky ⟶ k j := λ j, (h j).some_spec.some_spec.some, -- where the images of the components of the representatives become equal: have w : Π j, F.map ((𝟙 j, f j) : (j, kx) ⟶ (j, k j)) (limit.π ((curry.obj (swap K J ⋙ F)).obj kx) j x) = F.map ((𝟙 j, g j) : (j, ky) ⟶ (j, k j)) (limit.π ((curry.obj (swap K J ⋙ F)).obj ky) j y) := λ j, (h j).some_spec.some_spec.some_spec, -- We now use that `K` is filtered, picking some point to the right of all these -- morphisms `f j` and `g j`. let O : finset K := finset.univ.image k ∪ {kx, ky}, have kxO : kx ∈ O := finset.mem_union.mpr (or.inr (by simp)), have kyO : ky ∈ O := finset.mem_union.mpr (or.inr (by simp)), have kjO : ∀ j, k j ∈ O := λ j, finset.mem_union.mpr (or.inl (by simp)), let H : finset (Σ' (X Y : K) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y) := (finset.univ).image (λ j : J, ⟨kx, k j, kxO, finset.mem_union.mpr (or.inl (by simp)), f j⟩) ∪ (finset.univ).image (λ j : J, ⟨ky, k j, kyO, finset.mem_union.mpr (or.inl (by simp)), g j⟩), obtain ⟨S, T, W⟩ := is_filtered.sup_exists O H, have fH : ∀ j, (⟨kx, k j, kxO, kjO j, f j⟩ : (Σ' (X Y : K) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y)) ∈ H := λ j, (finset.mem_union.mpr (or.inl begin simp only [true_and, finset.mem_univ, eq_self_iff_true, exists_prop_of_true, finset.mem_image, heq_iff_eq], refine ⟨j, rfl, _⟩, simp only [heq_iff_eq], exact ⟨rfl, rfl, rfl⟩, end)), have gH : ∀ j, (⟨ky, k j, kyO, kjO j, g j⟩ : (Σ' (X Y : K) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y)) ∈ H := λ j, (finset.mem_union.mpr (or.inr begin simp only [true_and, finset.mem_univ, eq_self_iff_true, exists_prop_of_true, finset.mem_image, heq_iff_eq], refine ⟨j, rfl, _⟩, simp only [heq_iff_eq], exact ⟨rfl, rfl, rfl⟩, end)), -- Our goal is now an equation between equivalence classes of representatives of a colimit, -- and so it suffices to show those representative become equal somewhere, in particular at `S`. apply colimit_sound'.{v v} (T kxO) (T kyO), -- We can check if two elements of a limit (in `Type`) are equal by comparing them componentwise. ext, -- Now it's just a calculation using `W` and `w`. simp only [functor.comp_map, limit.map_π_apply, curry_obj_map_app, swap_map], rw ←W _ _ (fH j), rw ←W _ _ (gH j), simp [w], end end variables [fin_category J] /-- This follows this proof from * Borceux, Handbook of categorical algebra 1, Theorem 2.13.4 although with different names. -/ lemma colimit_limit_to_limit_colimit_surjective : function.surjective (colimit_limit_to_limit_colimit F) := begin classical, -- We begin with some element `x` in the limit (over J) over the colimits (over K), intro x, -- This consists of some coherent family of elements in the various colimits, -- and so our first task is to pick representatives of these elements. have z := λ j, jointly_surjective'.{v v} (limit.π (curry.obj F ⋙ limits.colim) j x), -- `k : J ⟶ K` records where the representative of the element in the `j`-th element of `x` lives let k : J → K := λ j, (z j).some, -- `y j : F.obj (j, k j)` is the representative let y : Π j, F.obj (j, k j) := λ j, (z j).some_spec.some, -- and we record that these representatives, when mapped back into the relevant colimits, -- are actually the components of `x`. have e : ∀ j, colimit.ι ((curry.obj F).obj j) (k j) (y j) = limit.π (curry.obj F ⋙ limits.colim) j x := λ j, (z j).some_spec.some_spec, clear_value k y, -- A little tidying up of things we no longer need. clear z, -- As a first step, we use that `K` is filtered to pick some point `k' : K` above all the `k j` let k' : K := is_filtered.sup (finset.univ.image k) ∅, -- and name the morphisms as `g j : k j ⟶ k'`. have g : Π j, k j ⟶ k' := λ j, is_filtered.to_sup (finset.univ.image k) ∅ (by simp), clear_value k', -- Recalling that the components of `x`, which are indexed by `j : J`, are "coherent", -- in other words preserved by morphisms in the `J` direction, -- we see that for any morphism `f : j ⟶ j'` in `J`, -- the images of `y j` and `y j'`, when mapped to `F.obj (j', k')` respectively by -- `(f, g j)` and `(𝟙 j', g j')`, both represent the same element in the colimit. have w : ∀ {j j' : J} (f : j ⟶ j'), colimit.ι ((curry.obj F).obj j') k' (F.map ((𝟙 j', g j') : (j', k j') ⟶ (j', k')) (y j')) = colimit.ι ((curry.obj F).obj j') k' (F.map ((f, g j) : (j, k j) ⟶ (j', k')) (y j)), { intros j j' f, have t : (f, g j) = (((f, 𝟙 (k j)) : (j, k j) ⟶ (j', k j)) ≫ (𝟙 j', g j) : (j, k j) ⟶ (j', k')), { simp only [id_comp, comp_id, prod_comp], }, erw [colimit.w_apply', t, functor_to_types.map_comp_apply, colimit.w_apply', e, ←limit.w_apply' f, ←e], simp, }, -- Because `K` is filtered, we can restate this as saying that -- for each such `f`, there is some place to the right of `k'` -- where these images of `y j` and `y j'` become equal. simp_rw colimit_eq_iff.{v v} at w, -- We take a moment to restate `w` more conveniently. let kf : Π {j j'} (f : j ⟶ j'), K := λ _ _ f, (w f).some, let gf : Π {j j'} (f : j ⟶ j'), k' ⟶ kf f := λ _ _ f, (w f).some_spec.some, let hf : Π {j j'} (f : j ⟶ j'), k' ⟶ kf f := λ _ _ f, (w f).some_spec.some_spec.some, have wf : Π {j j'} (f : j ⟶ j'), F.map ((𝟙 j', g j' ≫ gf f) : (j', k j') ⟶ (j', kf f)) (y j') = F.map ((f, g j ≫ hf f) : (j, k j) ⟶ (j', kf f)) (y j) := λ j j' f, begin have q : ((curry.obj F).obj j').map (gf f) (F.map _ (y j')) = ((curry.obj F).obj j').map (hf f) (F.map _ (y j)) := (w f).some_spec.some_spec.some_spec, dsimp at q, simp_rw ←functor_to_types.map_comp_apply at q, convert q; simp only [comp_id], end, clear_value kf gf hf, -- and clean up some things that are no longer needed. clear w, -- We're now ready to use the fact that `K` is filtered a second time, -- picking some place to the right of all of -- the morphisms `gf f : k' ⟶ kh f` and `hf f : k' ⟶ kf f`. -- At this point we're relying on there being only finitely morphisms in `J`. let O := finset.univ.bUnion (λ j, finset.univ.bUnion (λ j', finset.univ.image (@kf j j'))) ∪ {k'}, have kfO : ∀ {j j'} (f : j ⟶ j'), kf f ∈ O := λ j j' f, finset.mem_union.mpr (or.inl ( begin rw [finset.mem_bUnion], refine ⟨j, finset.mem_univ j, _⟩, rw [finset.mem_bUnion], refine ⟨j', finset.mem_univ j', _⟩, rw [finset.mem_image], refine ⟨f, finset.mem_univ _, _⟩, refl, end)), have k'O : k' ∈ O := finset.mem_union.mpr (or.inr (finset.mem_singleton.mpr rfl)), let H : finset (Σ' (X Y : K) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y) := finset.univ.bUnion (λ j : J, finset.univ.bUnion (λ j' : J, finset.univ.bUnion (λ f : j ⟶ j', {⟨k', kf f, k'O, kfO f, gf f⟩, ⟨k', kf f, k'O, kfO f, hf f⟩}))), obtain ⟨k'', i', s'⟩ := is_filtered.sup_exists O H, -- We then restate this slightly more conveniently, as a family of morphism `i f : kf f ⟶ k''`, -- satisfying `gf f ≫ i f = hf f' ≫ i f'`. let i : Π {j j'} (f : j ⟶ j'), kf f ⟶ k'' := λ j j' f, i' (kfO f), have s : ∀ {j₁ j₂ j₃ j₄} (f : j₁ ⟶ j₂) (f' : j₃ ⟶ j₄), gf f ≫ i f = hf f' ≫ i f' := begin intros, rw [s', s'], swap 2, exact k'O, swap 2, { rw [finset.mem_bUnion], refine ⟨j₁, finset.mem_univ _, _⟩, rw [finset.mem_bUnion], refine ⟨j₂, finset.mem_univ _, _⟩, rw [finset.mem_bUnion], refine ⟨f, finset.mem_univ _, _⟩, simp only [true_or, eq_self_iff_true, and_self, finset.mem_insert, heq_iff_eq], }, { rw [finset.mem_bUnion], refine ⟨j₃, finset.mem_univ _, _⟩, rw [finset.mem_bUnion], refine ⟨j₄, finset.mem_univ _, _⟩, rw [finset.mem_bUnion], refine ⟨f', finset.mem_univ _, _⟩, simp only [eq_self_iff_true, or_true, and_self, finset.mem_insert, finset.mem_singleton, heq_iff_eq], } end, clear_value i, clear s' i' H kfO k'O O, -- We're finally ready to construct the pre-image, and verify it really maps to `x`. fsplit, { -- We construct the pre-image (which, recall is meant to be a point -- in the colimit (over `K`) of the limits (over `J`)) via a representative at `k''`. apply colimit.ι (curry.obj (swap K J ⋙ F) ⋙ limits.lim) k'' _, dsimp, -- This representative is meant to be an element of a limit, -- so we need to construct a family of elements in `F.obj (j, k'')` for varying `j`, -- then show that are coherent with respect to morphisms in the `j` direction. apply limit.mk.{v v}, swap, { -- We construct the elements as the images of the `y j`. exact λ j, F.map (⟨𝟙 j, g j ≫ gf (𝟙 j) ≫ i (𝟙 j)⟩ : (j, k j) ⟶ (j, k'')) (y j), }, { -- After which it's just a calculation, using `s` and `wf`, to see they are coherent. dsimp, intros j j' f, simp only [←functor_to_types.map_comp_apply, prod_comp, id_comp, comp_id], calc F.map ((f, g j ≫ gf (𝟙 j) ≫ i (𝟙 j)) : (j, k j) ⟶ (j', k'')) (y j) = F.map ((f, g j ≫ hf f ≫ i f) : (j, k j) ⟶ (j', k'')) (y j) : by rw s (𝟙 j) f ... = F.map ((𝟙 j', i f) : (j', kf f) ⟶ (j', k'')) (F.map ((f, g j ≫ hf f) : (j, k j) ⟶ (j', kf f)) (y j)) : by rw [←functor_to_types.map_comp_apply, prod_comp, comp_id, assoc] ... = F.map ((𝟙 j', i f) : (j', kf f) ⟶ (j', k'')) (F.map ((𝟙 j', g j' ≫ gf f) : (j', k j') ⟶ (j', kf f)) (y j')) : by rw ←wf f ... = F.map ((𝟙 j', g j' ≫ gf f ≫ i f) : (j', k j') ⟶ (j', k'')) (y j') : by rw [←functor_to_types.map_comp_apply, prod_comp, id_comp, assoc] ... = F.map ((𝟙 j', g j' ≫ gf (𝟙 j') ≫ i (𝟙 j')) : (j', k j') ⟶ (j', k'')) (y j') : by rw [s f (𝟙 j'), ←s (𝟙 j') (𝟙 j')], }, }, -- Finally we check that this maps to `x`. { -- We can do this componentwise: apply limit_ext', intro j, -- and as each component is an equation in a colimit, we can verify it by -- pointing out the morphism which carries one representative to the other: simp only [←e, colimit_eq_iff.{v v}, curry_obj_obj_map, limit.π_mk', bifunctor.map_id_comp, id.def, types_comp_apply, limits.ι_colimit_limit_to_limit_colimit_π_apply], refine ⟨k'', 𝟙 k'', g j ≫ gf (𝟙 j) ≫ i (𝟙 j), _⟩, simp only [bifunctor.map_id_comp, types_comp_apply, bifunctor.map_id, types_id_apply], }, end instance colimit_limit_to_limit_colimit_is_iso : is_iso (colimit_limit_to_limit_colimit F) := (is_iso_iff_bijective _).mpr ⟨colimit_limit_to_limit_colimit_injective F, colimit_limit_to_limit_colimit_surjective F⟩ instance colimit_limit_to_limit_colimit_cone_iso (F : J ⥤ K ⥤ Type v) : is_iso (colimit_limit_to_limit_colimit_cone F) := begin haveI : is_iso (colimit_limit_to_limit_colimit_cone F).hom, { dsimp only [colimit_limit_to_limit_colimit_cone], apply_instance }, apply cones.cone_iso_of_hom_iso, end noncomputable instance filtered_colim_preserves_finite_limits_of_types : preserves_finite_limits (colim : (K ⥤ Type v) ⥤ _) := begin apply preserves_finite_limits_of_preserves_finite_limits_of_size.{v}, intros J _ _, resetI, constructor, intro F, constructor, intros c hc, apply is_limit.of_iso_limit (limit.is_limit _), symmetry, transitivity (colim.map_cone (limit.cone F)), exact functor.map_iso _ (hc.unique_up_to_iso (limit.is_limit F)), exact as_iso (colimit_limit_to_limit_colimit_cone.{v (v + 1)} F), end variables {C : Type u} [category.{v} C] [concrete_category.{v} C] section variables [has_limits_of_shape J C] [has_colimits_of_shape K C] variables [reflects_limits_of_shape J (forget C)] [preserves_colimits_of_shape K (forget C)] variables [preserves_limits_of_shape J (forget C)] noncomputable instance filtered_colim_preserves_finite_limits : preserves_limits_of_shape J (colim : (K ⥤ C) ⥤ _) := begin haveI : preserves_limits_of_shape J ((colim : (K ⥤ C) ⥤ _) ⋙ forget C) := preserves_limits_of_shape_of_nat_iso (preserves_colimit_nat_iso _).symm, exactI preserves_limits_of_shape_of_reflects_of_preserves _ (forget C) end end local attribute [instance] reflects_limits_of_shape_of_reflects_isomorphisms noncomputable instance [preserves_finite_limits (forget C)] [preserves_filtered_colimits (forget C)] [has_finite_limits C] [has_colimits_of_shape K C] [reflects_isomorphisms (forget C)] : preserves_finite_limits (colim : (K ⥤ C) ⥤ _) := begin apply preserves_finite_limits_of_preserves_finite_limits_of_size.{v}, intros J _ _, resetI, apply_instance end section variables [has_limits_of_shape J C] [has_colimits_of_shape K C] variables [reflects_limits_of_shape J (forget C)] [preserves_colimits_of_shape K (forget C)] variables [preserves_limits_of_shape J (forget C)] /-- A curried version of the fact that filtered colimits commute with finite limits. -/ noncomputable def colimit_limit_iso (F : J ⥤ K ⥤ C) : colimit (limit F) ≅ limit (colimit F.flip) := (is_limit_of_preserves colim (limit.is_limit _)).cone_point_unique_up_to_iso (limit.is_limit _) ≪≫ (has_limit.iso_of_nat_iso (colimit_flip_iso_comp_colim _).symm) @[simp, reassoc] lemma ι_colimit_limit_iso_limit_π (F : J ⥤ K ⥤ C) (a) (b) : colimit.ι (limit F) a ≫ (colimit_limit_iso F).hom ≫ limit.π (colimit F.flip) b = (limit.π F b).app a ≫ (colimit.ι F.flip a).app b := begin dsimp [colimit_limit_iso], simp only [functor.map_cone_π_app, iso.symm_hom, limits.limit.cone_point_unique_up_to_iso_hom_comp_assoc, limits.limit.cone_π, limits.colimit.ι_map_assoc, limits.colimit_flip_iso_comp_colim_inv_app, assoc, limits.has_limit.iso_of_nat_iso_hom_π], congr' 1, simp only [← category.assoc, iso.comp_inv_eq, limits.colimit_obj_iso_colimit_comp_evaluation_ι_app_hom, limits.has_colimit.iso_of_nat_iso_ι_hom, nat_iso.of_components_hom_app], dsimp, simp, end end end category_theory.limits
920ea2368b09f4bf9873fcccc88b1e45c120d8fa
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/hott/876.hlean
a42bd53684abf991c19e0a3e625d94085ff30da4
[ "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
131
hlean
example : Σ(X : Type₀), X → unit := begin fapply sigma.mk, { exact unit}, { intro x, induction x, exact unit.star } end
df081814a1dc6cf76a5775e48c47b12dc8c6efc3
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/order/closure.lean
573c7c77c2546dbb976de449c8ae21afab538fcb
[ "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
17,376
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Yaël Dillies -/ import data.set.lattice import data.set_like.basic import order.galois_connection import order.hom.basic import tactic.monotonicity /-! # Closure operators between preorders We define (bundled) closure operators on a preorder as monotone (increasing), extensive (inflationary) and idempotent functions. We define closed elements for the operator as elements which are fixed by it. Lower adjoints to a function between preorders `u : β → α` allow to generalise closure operators to situations where the closure operator we are dealing with naturally decomposes as `u ∘ l` where `l` is a worthy function to have on its own. Typical examples include `l : set G → subgroup G := subgroup.closure`, `u : subgroup G → set G := coe`, where `G` is a group. This shows there is a close connection between closure operators, lower adjoints and Galois connections/insertions: every Galois connection induces a lower adjoint which itself induces a closure operator by composition (see `galois_connection.lower_adjoint` and `lower_adjoint.closure_operator`), and every closure operator on a partial order induces a Galois insertion from the set of closed elements to the underlying type (see `closure_operator.gi`). ## Main definitions * `closure_operator`: A closure operator is a monotone function `f : α → α` such that `∀ x, x ≤ f x` and `∀ x, f (f x) = f x`. * `lower_adjoint`: A lower adjoint to `u : β → α` is a function `l : α → β` such that `l` and `u` form a Galois connection. ## Implementation details Although `lower_adjoint` is technically a generalisation of `closure_operator` (by defining `to_fun := id`), it is desirable to have both as otherwise `id`s would be carried all over the place when using concrete closure operators such as `convex_hull`. `lower_adjoint` really is a semibundled `structure` version of `galois_connection`. ## References * https://en.wikipedia.org/wiki/Closure_operator#Closure_operators_on_partially_ordered_sets -/ universe u /-! ### Closure operator -/ variable (α : Type*) /-- A closure operator on the preorder `α` is a monotone function which is extensive (every `x` is less than its closure) and idempotent. -/ structure closure_operator [preorder α] extends α →o α := (le_closure' : ∀ x, x ≤ to_fun x) (idempotent' : ∀ x, to_fun (to_fun x) = to_fun x) namespace closure_operator instance [preorder α] : has_coe_to_fun (closure_operator α) (λ _, α → α) := ⟨λ c, c.to_fun⟩ /-- See Note [custom simps projection] -/ def simps.apply [preorder α] (f : closure_operator α) : α → α := f initialize_simps_projections closure_operator (to_order_hom_to_fun → apply, -to_order_hom) section partial_order variable [partial_order α] /-- The identity function as a closure operator. -/ @[simps] def id : closure_operator α := { to_order_hom := order_hom.id, le_closure' := λ _, le_rfl, idempotent' := λ _, rfl } instance : inhabited (closure_operator α) := ⟨id α⟩ variables {α} (c : closure_operator α) @[ext] lemma ext : ∀ (c₁ c₂ : closure_operator α), (c₁ : α → α) = (c₂ : α → α) → c₁ = c₂ | ⟨⟨c₁, _⟩, _, _⟩ ⟨⟨c₂, _⟩, _, _⟩ h := by { congr, exact h } /-- Constructor for a closure operator using the weaker idempotency axiom: `f (f x) ≤ f x`. -/ @[simps] def mk' (f : α → α) (hf₁ : monotone f) (hf₂ : ∀ x, x ≤ f x) (hf₃ : ∀ x, f (f x) ≤ f x) : closure_operator α := { to_fun := f, monotone' := hf₁, le_closure' := hf₂, idempotent' := λ x, (hf₃ x).antisymm (hf₁ (hf₂ x)) } /-- Convenience constructor for a closure operator using the weaker minimality axiom: `x ≤ f y → f x ≤ f y`, which is sometimes easier to prove in practice. -/ @[simps] def mk₂ (f : α → α) (hf : ∀ x, x ≤ f x) (hmin : ∀ ⦃x y⦄, x ≤ f y → f x ≤ f y) : closure_operator α := { to_fun := f, monotone' := λ x y hxy, hmin (hxy.trans (hf y)), le_closure' := hf, idempotent' := λ x, (hmin le_rfl).antisymm (hf _) } /-- Expanded out version of `mk₂`. `p` implies being closed. This constructor should be used when you already know a sufficient condition for being closed and using `mem_mk₃_closed` will avoid you the (slight) hassle of having to prove it both inside and outside the constructor. -/ @[simps] def mk₃ (f : α → α) (p : α → Prop) (hf : ∀ x, x ≤ f x) (hfp : ∀ x, p (f x)) (hmin : ∀ ⦃x y⦄, x ≤ y → p y → f x ≤ y) : closure_operator α := mk₂ f hf (λ x y hxy, hmin hxy (hfp y)) /-- This lemma shows that the image of `x` of a closure operator built from the `mk₃` constructor respects `p`, the property that was fed into it. -/ lemma closure_mem_mk₃ {f : α → α} {p : α → Prop} {hf : ∀ x, x ≤ f x} {hfp : ∀ x, p (f x)} {hmin : ∀ ⦃x y⦄, x ≤ y → p y → f x ≤ y} (x : α) : p (mk₃ f p hf hfp hmin x) := hfp x /-- Analogue of `closure_le_closed_iff_le` but with the `p` that was fed into the `mk₃` constructor. -/ lemma closure_le_mk₃_iff {f : α → α} {p : α → Prop} {hf : ∀ x, x ≤ f x} {hfp : ∀ x, p (f x)} {hmin : ∀ ⦃x y⦄, x ≤ y → p y → f x ≤ y} {x y : α} (hxy : x ≤ y) (hy : p y) : mk₃ f p hf hfp hmin x ≤ y := hmin hxy hy @[mono] lemma monotone : monotone c := c.monotone' /-- Every element is less than its closure. This property is sometimes referred to as extensivity or inflationarity. -/ lemma le_closure (x : α) : x ≤ c x := c.le_closure' x @[simp] lemma idempotent (x : α) : c (c x) = c x := c.idempotent' x lemma le_closure_iff (x y : α) : x ≤ c y ↔ c x ≤ c y := ⟨λ h, c.idempotent y ▸ c.monotone h, λ h, (c.le_closure x).trans h⟩ /-- An element `x` is closed for the closure operator `c` if it is a fixed point for it. -/ def closed : set α := λ x, c x = x lemma mem_closed_iff (x : α) : x ∈ c.closed ↔ c x = x := iff.rfl lemma mem_closed_iff_closure_le (x : α) : x ∈ c.closed ↔ c x ≤ x := ⟨le_of_eq, λ h, h.antisymm (c.le_closure x)⟩ lemma closure_eq_self_of_mem_closed {x : α} (h : x ∈ c.closed) : c x = x := h @[simp] lemma closure_is_closed (x : α) : c x ∈ c.closed := c.idempotent x /-- The set of closed elements for `c` is exactly its range. -/ lemma closed_eq_range_close : c.closed = set.range c := set.ext $ λ x, ⟨λ h, ⟨x, h⟩, by { rintro ⟨y, rfl⟩, apply c.idempotent }⟩ /-- Send an `x` to an element of the set of closed elements (by taking the closure). -/ def to_closed (x : α) : c.closed := ⟨c x, c.closure_is_closed x⟩ @[simp] lemma closure_le_closed_iff_le (x : α) {y : α} (hy : c.closed y) : c x ≤ y ↔ x ≤ y := by rw [←c.closure_eq_self_of_mem_closed hy, ←le_closure_iff] /-- A closure operator is equal to the closure operator obtained by feeding `c.closed` into the `mk₃` constructor. -/ lemma eq_mk₃_closed (c : closure_operator α) : c = mk₃ c c.closed c.le_closure c.closure_is_closed (λ x y hxy hy, (c.closure_le_closed_iff_le x hy).2 hxy) := by { ext, refl } /-- The property `p` fed into the `mk₃` constructor implies being closed. -/ lemma mem_mk₃_closed {f : α → α} {p : α → Prop} {hf : ∀ x, x ≤ f x} {hfp : ∀ x, p (f x)} {hmin : ∀ ⦃x y⦄, x ≤ y → p y → f x ≤ y} {x : α} (hx : p x) : x ∈ (mk₃ f p hf hfp hmin).closed := (hmin le_rfl hx).antisymm (hf _) end partial_order variable {α} section order_top variables [partial_order α] [order_top α] (c : closure_operator α) @[simp] lemma closure_top : c ⊤ = ⊤ := le_top.antisymm (c.le_closure _) lemma top_mem_closed : ⊤ ∈ c.closed := c.closure_top end order_top lemma closure_inf_le [semilattice_inf α] (c : closure_operator α) (x y : α) : c (x ⊓ y) ≤ c x ⊓ c y := c.monotone.map_inf_le _ _ section semilattice_sup variables [semilattice_sup α] (c : closure_operator α) lemma closure_sup_closure_le (x y : α) : c x ⊔ c y ≤ c (x ⊔ y) := c.monotone.le_map_sup _ _ lemma closure_sup_closure_left (x y : α) : c (c x ⊔ y) = c (x ⊔ y) := ((c.le_closure_iff _ _).1 (sup_le (c.monotone le_sup_left) (le_sup_right.trans (c.le_closure _)))).antisymm (c.monotone (sup_le_sup_right (c.le_closure _) _)) lemma closure_sup_closure_right (x y : α) : c (x ⊔ c y) = c (x ⊔ y) := by rw [sup_comm, closure_sup_closure_left, sup_comm] lemma closure_sup_closure (x y : α) : c (c x ⊔ c y) = c (x ⊔ y) := by rw [closure_sup_closure_left, closure_sup_closure_right] end semilattice_sup section complete_lattice variables [complete_lattice α] (c : closure_operator α) lemma closure_supr_closure {ι : Type u} (x : ι → α) : c (⨆ i, c (x i)) = c (⨆ i, x i) := le_antisymm ((c.le_closure_iff _ _).1 (supr_le (λ i, c.monotone (le_supr x i)))) (c.monotone (supr_le_supr (λ i, c.le_closure _))) lemma closure_bsupr_closure (p : α → Prop) : c (⨆ x (H : p x), c x) = c (⨆ x (H : p x), x) := le_antisymm ((c.le_closure_iff _ _).1 (bsupr_le (λ x hx, c.monotone (le_bsupr_of_le x hx (le_refl x))))) (c.monotone (bsupr_le_bsupr (λ x hx, c.le_closure x))) end complete_lattice end closure_operator /-! ### Lower adjoint -/ variables {α} {β : Type*} /-- A lower adjoint of `u` on the preorder `α` is a function `l` such that `l` and `u` form a Galois connection. It allows us to define closure operators whose output does not match the input. In practice, `u` is often `coe : β → α`. -/ structure lower_adjoint [preorder α] [preorder β] (u : β → α) := (to_fun : α → β) (gc' : galois_connection to_fun u) namespace lower_adjoint variable (α) /-- The identity function as a lower adjoint to itself. -/ @[simps] protected def id [preorder α] : lower_adjoint (id : α → α) := { to_fun := λ x, x, gc' := galois_connection.id } variable {α} instance [preorder α] : inhabited (lower_adjoint (id : α → α)) := ⟨lower_adjoint.id α⟩ section preorder variables [preorder α] [preorder β] {u : β → α} (l : lower_adjoint u) instance : has_coe_to_fun (lower_adjoint u) (λ _, α → β) := { coe := to_fun } /-- See Note [custom simps projection] -/ def simps.apply : α → β := l lemma gc : galois_connection l u := l.gc' @[ext] lemma ext : ∀ (l₁ l₂ : lower_adjoint u), (l₁ : α → β) = (l₂ : α → β) → l₁ = l₂ | ⟨l₁, _⟩ ⟨l₂, _⟩ h := by { congr, exact h } @[mono] lemma monotone : monotone (u ∘ l) := l.gc.monotone_u.comp l.gc.monotone_l /-- Every element is less than its closure. This property is sometimes referred to as extensivity or inflationarity. -/ lemma le_closure (x : α) : x ≤ u (l x) := l.gc.le_u_l _ end preorder section partial_order variables [partial_order α] [preorder β] {u : β → α} (l : lower_adjoint u) /-- Every lower adjoint induces a closure operator given by the composition. This is the partial order version of the statement that every adjunction induces a monad. -/ @[simps] def closure_operator : closure_operator α := { to_fun := λ x, u (l x), monotone' := l.monotone, le_closure' := l.le_closure, idempotent' := λ x, l.gc.u_l_u_eq_u (l x) } lemma idempotent (x : α) : u (l (u (l x))) = u (l x) := l.closure_operator.idempotent _ lemma le_closure_iff (x y : α) : x ≤ u (l y) ↔ u (l x) ≤ u (l y) := l.closure_operator.le_closure_iff _ _ end partial_order section preorder variables [preorder α] [preorder β] {u : β → α} (l : lower_adjoint u) /-- An element `x` is closed for `l : lower_adjoint u` if it is a fixed point: `u (l x) = x` -/ def closed : set α := λ x, u (l x) = x lemma mem_closed_iff (x : α) : x ∈ l.closed ↔ u (l x) = x := iff.rfl lemma closure_eq_self_of_mem_closed {x : α} (h : x ∈ l.closed) : u (l x) = x := h end preorder section partial_order variables [partial_order α] [partial_order β] {u : β → α} (l : lower_adjoint u) lemma mem_closed_iff_closure_le (x : α) : x ∈ l.closed ↔ u (l x) ≤ x := l.closure_operator.mem_closed_iff_closure_le _ @[simp] lemma closure_is_closed (x : α) : u (l x) ∈ l.closed := l.idempotent x /-- The set of closed elements for `l` is the range of `u ∘ l`. -/ lemma closed_eq_range_close : l.closed = set.range (u ∘ l) := l.closure_operator.closed_eq_range_close /-- Send an `x` to an element of the set of closed elements (by taking the closure). -/ def to_closed (x : α) : l.closed := ⟨u (l x), l.closure_is_closed x⟩ @[simp] lemma closure_le_closed_iff_le (x : α) {y : α} (hy : l.closed y) : u (l x) ≤ y ↔ x ≤ y := l.closure_operator.closure_le_closed_iff_le x hy end partial_order lemma closure_top [partial_order α] [order_top α] [preorder β] {u : β → α} (l : lower_adjoint u) : u (l ⊤) = ⊤ := l.closure_operator.closure_top lemma closure_inf_le [semilattice_inf α] [preorder β] {u : β → α} (l : lower_adjoint u) (x y : α) : u (l (x ⊓ y)) ≤ u (l x) ⊓ u (l y) := l.closure_operator.closure_inf_le x y section semilattice_sup variables [semilattice_sup α] [preorder β] {u : β → α} (l : lower_adjoint u) lemma closure_sup_closure_le (x y : α) : u (l x) ⊔ u (l y) ≤ u (l (x ⊔ y)) := l.closure_operator.closure_sup_closure_le x y lemma closure_sup_closure_left (x y : α) : u (l (u (l x) ⊔ y)) = u (l (x ⊔ y)) := l.closure_operator.closure_sup_closure_left x y lemma closure_sup_closure_right (x y : α) : u (l (x ⊔ u (l y))) = u (l (x ⊔ y)) := l.closure_operator.closure_sup_closure_right x y lemma closure_sup_closure (x y : α) : u (l (u (l x) ⊔ u (l y))) = u (l (x ⊔ y)) := l.closure_operator.closure_sup_closure x y end semilattice_sup section complete_lattice variables [complete_lattice α] [preorder β] {u : β → α} (l : lower_adjoint u) lemma closure_supr_closure {ι : Type u} (x : ι → α) : u (l (⨆ i, u (l (x i)))) = u (l (⨆ i, x i)) := l.closure_operator.closure_supr_closure x lemma closure_bsupr_closure (p : α → Prop) : u (l (⨆ x (H : p x), u (l x))) = u (l (⨆ x (H : p x), x)) := l.closure_operator.closure_bsupr_closure p end complete_lattice /- Lemmas for `lower_adjoint (coe : α → set β)`, where `set_like α β` -/ section coe_to_set variables [set_like α β] (l : lower_adjoint (coe : α → set β)) lemma subset_closure (s : set β) : s ⊆ l s := l.le_closure s lemma not_mem_of_not_mem_closure {s : set β} {P : β} (hP : P ∉ l s) : P ∉ s := λ h, hP (subset_closure _ s h) lemma le_iff_subset (s : set β) (S : α) : l s ≤ S ↔ s ⊆ S := l.gc s S lemma mem_iff (s : set β) (x : β) : x ∈ l s ↔ ∀ S : α, s ⊆ S → x ∈ S := by { simp_rw [←set_like.mem_coe, ←set.singleton_subset_iff, ←l.le_iff_subset], exact ⟨λ h S, h.trans, λ h, h _ le_rfl⟩ } lemma eq_of_le {s : set β} {S : α} (h₁ : s ⊆ S) (h₂ : S ≤ l s) : l s = S := ((l.le_iff_subset _ _).2 h₁).antisymm h₂ lemma closure_union_closure_subset (x y : α) : (l x : set β) ∪ (l y) ⊆ l (x ∪ y) := l.closure_sup_closure_le x y @[simp] lemma closure_union_closure_left (x y : α) : (l ((l x) ∪ y) : set β) = l (x ∪ y) := l.closure_sup_closure_left x y @[simp] lemma closure_union_closure_right (x y : α) : l (x ∪ (l y)) = l (x ∪ y) := set_like.coe_injective (l.closure_sup_closure_right x y) @[simp] lemma closure_union_closure (x y : α) : l ((l x) ∪ (l y)) = l (x ∪ y) := set_like.coe_injective (l.closure_operator.closure_sup_closure x y) @[simp] lemma closure_Union_closure {ι : Type u} (x : ι → α) : l (⋃ i, l (x i)) = l (⋃ i, x i) := set_like.coe_injective (l.closure_supr_closure (coe ∘ x)) @[simp] lemma closure_bUnion_closure (p : set β → Prop) : l (⋃ x (H : p x), l x) = l (⋃ x (H : p x), x) := set_like.coe_injective (l.closure_bsupr_closure p) end coe_to_set end lower_adjoint /-! ### Translations between `galois_connection`, `lower_adjoint`, `closure_operator` -/ variable {α} /-- Every Galois connection induces a lower adjoint. -/ @[simps] def galois_connection.lower_adjoint [preorder α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) : lower_adjoint u := { to_fun := l, gc' := gc } /-- Every Galois connection induces a closure operator given by the composition. This is the partial order version of the statement that every adjunction induces a monad. -/ @[simps] def galois_connection.closure_operator [partial_order α] [preorder β] {l : α → β} {u : β → α} (gc : galois_connection l u) : closure_operator α := gc.lower_adjoint.closure_operator /-- The set of closed elements has a Galois insertion to the underlying type. -/ def closure_operator.gi [partial_order α] (c : closure_operator α) : galois_insertion c.to_closed coe := { choice := λ x hx, ⟨x, hx.antisymm (c.le_closure x)⟩, gc := λ x y, (c.closure_le_closed_iff_le _ y.2), le_l_u := λ x, c.le_closure _, choice_eq := λ x hx, le_antisymm (c.le_closure x) hx } /-- The Galois insertion associated to a closure operator can be used to reconstruct the closure operator. Note that the inverse in the opposite direction does not hold in general. -/ @[simp] lemma closure_operator_gi_self [partial_order α] (c : closure_operator α) : c.gi.gc.closure_operator = c := by { ext x, refl }
71cd8367510a709245274463d8a246a5f63a8c88
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Util/PPGoal.lean
1207f044b1cd4f56e8e8127d5dfdd1ba205a4711
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
2,184
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ prelude import Init.Lean.Util.PPExt namespace Lean def ppGoal (env : Environment) (mctx : MetavarContext) (opts : Options) (mvarId : MVarId) : Format := match mctx.findDecl? mvarId with | none => "unknown goal" | some mvarDecl => let indent := 2; -- Use option let lctx := mvarDecl.lctx; let pp (e : Expr) : Format := ppExpr env mctx lctx opts e; let instMVars (e : Expr) : Expr := (mctx.instantiateMVars e).1; let addLine (fmt : Format) : Format := if fmt.isNil then fmt else fmt ++ Format.line; let pushPending (ids : List Name) (type? : Option Expr) (fmt : Format) : Format := let fmt := addLine fmt; match ids, type? with | [], _ => fmt | _, none => fmt | _, some type => fmt ++ (Format.joinSep ids.reverse " " ++ " :" ++ Format.nest indent (Format.line ++ pp type)).group; let (varNames, type?, fmt) := mvarDecl.lctx.foldl (fun (acc : List Name × Option Expr × Format) (localDecl : LocalDecl) => let (varNames, prevType?, fmt) := acc; match localDecl with | LocalDecl.cdecl _ _ varName type _ => let type := instMVars type; if prevType? == none || prevType? == some type then (varName :: varNames, some type, fmt) else let fmt := pushPending varNames prevType? fmt; ([varName], some type, fmt) | LocalDecl.ldecl _ _ varName type val => let fmt := pushPending varNames prevType? fmt; let fmt := addLine fmt; let type := instMVars type; let val := instMVars val; let fmt := fmt ++ (format varName ++ " : " ++ pp type ++ " :=" ++ Format.nest indent (Format.line ++ pp val)).group; ([], none, fmt)) ([], none, Format.nil); let fmt := pushPending varNames type? fmt; let fmt := addLine fmt; let fmt := fmt ++ "⊢" ++ " " ++ Format.nest indent (pp mvarDecl.type); match mvarDecl.userName with | Name.anonymous => fmt | name => "case " ++ format name ++ Format.line ++ fmt end Lean
0703b2c7f538ceabfa978add9992c8f4ad775dd6
76ce87faa6bc3c2aa9af5962009e01e04f2a074a
/05_Functions/functions.lean
3d701593f26677cc5c38234eedf98fd66067f9ae
[]
no_license
Mnormansell/Discrete-Notes
db423dd9206bbe7080aecb84b4c2d275b758af97
61f13b98be590269fc4822be7b47924a6ddc1261
refs/heads/master
1,585,412,435,424
1,540,919,483,000
1,540,919,483,000
148,684,638
0
0
null
null
null
null
UTF-8
Lean
false
false
5,889
lean
/- If P and Q are of type Prop, we think of P → Q as a proposition in the form of an implication. Moreover, as we've seen, the way that we give a proof of P → Q in the logic of Lean is to produce a function that, if given a proof of P returns a proof of Q. On the other hand, nothing in Lean will prevent our using types such as nat and bool as P and Q. In this case we'd say that P → Q, or nat → bool, is a type: a type of functions from P → Q, e.g., from nat to bool. EXERCISE: Give two or three examples of interesting functions from nat to bool. A program that takes any natural number as an argument and always returns a Boolean value as a result is a value (a "function", or it would be better to say, a lambda expression) of this type, ℕ → bool. -/ def zero_nat (n : ℕ) : nat := 0 -- explicit return type #check zero_nat #check zero_nat 5 #reduce zero_nat 5 def inc (n : ℕ) : ℕ := n + 1 #reduce inc 5 def zero_nat': ℕ → ℕ := λ n : nat, (0 : nat) def zero_nat'': ℕ → ℕ := λ n, 0 def zero_nat''' := λ n : nat, 0 /- EXERCISE: define a function, one_nat that takes any natural number as an argument and always returns the natural number 1. Write it in traditional function definition notation and using lambda expressions and type inference. -/ def one_nat : ℕ → ℕ := λ n: nat, 1 #check one_nat #reduce one_nat 5 def identity_nat (n : ℕ) : nat := n --explicit return type #check identity_nat #check identity_nat 5 #reduce identity_nat 5 /- EXERCISE: define the same function, call it identity_nat' and write it using a lambda expression. -/ def double (n : ℕ) := 2 * n -- return type inferred #check double #check double 3 #reduce double 3 def double' := λ n: ℕ , (2 * n : ℕ) /- EXERCISE: Write it using a lambda expression. -/ def square (n: ℕ) := n * n -- return type inferred #check square #check square 3 #reduce square 3 def square' : ℤ → ℤ := λ n: ℤ, (n * n: ℤ) #reduce square' (-3) -- #reduce square (-3) -- Nope /- EXERCISE: Write it using a lambda expression. -/ def isPositive (n: nat) : bool := if n > 0 then tt else ff #check isPositive #check isPositive 0 #check isPositive 1 #check isPositive 2 #reduce isPositive 0 #reduce isPositive 1 #reduce isPositive 2 /- EXERCISE: Write it using a lambda expression. -/ /- Here's a function that returns boolean true (tt) if a given natural number n is greater than or equal to zero and less than 2^32. It is given using lambda notation. Write it with a prime on its name using ordinary function definition notation (as the functions above are written). -/ def uint32: ℕ → bool := λ n, if n >= 0 ∧ n < 2^32 then tt else ff #check uint32 /- An equivalent function definition with a type declaration using arrow notation, and an explicit lambda expression as a value. -/ def positive' : nat → bool := λ n : nat, (if n > 0 then tt else ff : bool) #check positive' #check λ n : nat, (if n > 0 then tt else ff : bool) #check λ n, (if n > 0 then tt else ff) #check λ n, n > 0 /- Read the first line as saying positive' is a proof of (a value of type) nat → bool. There are many values of this type: they are all the functions taking nat arguments and returning bool results. The specific value that we use here to "prove the type" is the function that returns true if the argument is strictly greater than zero, otherwise is returns false. Read the lambda expression as follows: A function that takes an argument, n, of type nat, and that returns the result of evaluating the expression, involving n, after the comma: here, true if n > 0, and false otherwise. Here's an equivalent expression using type inference to skip declaring the type of n. -/ def positive'' : nat → bool := λ n, if n > 0 then tt else ff /- Here we use type inference to skip declaring the function type, while still using a lambda expression for the program. -/ def positive''' := λ n, if n > 0 then tt else ff #check positive''' #reduce positive''' 3 #reduce positive''' 0 theorem pos_is_pos : isPositive = positive''' := rfl def my_pow (x: nat) (y: nat) := x^y #eval my_pow 2 16 /- EXERCISE: Give two or three examples of interesting functions from nat to nat. A program that takes any natural number as an argument and always returns a natural as a result is a value (a "function", or it would be better to say, a lambda expression) of this type, ℕ → ℕ -/ /- We can also pass functions as arguments to other functions! -/ def compose (f: ℕ → ℕ) (g: ℕ → ℕ) (x: ℕ) : ℕ := f (g x) #check compose #check compose square double #reduce compose double double 3 #reduce compose square double 3 #reduce square (double 3) def do_twice (f : ℕ → ℕ) (x: ℕ) : ℕ := f (f x) #check do_twice #reduce do_twice square 3 /- Let's write this as a lambda expression and see what we get. -/ def do_twice' : (ℕ → ℕ) → ℕ → ℕ := λ (f: ℕ → ℕ) (x: ℕ), f (f x) /- And that's a shorthand for this! -/ def do_twice'' : (ℕ → ℕ) → ℕ → ℕ := λ (f : ℕ → ℕ), λ (x : ℕ), f (f x) def sub_do_twice (f: ℕ → ℕ) := λ (x : ℕ), f (f x) #check sub_do_twice def call_sub_do_twice := sub_do_twice (λ (x: ℕ), x + 1) #check call_sub_do_twice #reduce call_sub_do_twice 2 theorem dt_eq_dt : do_twice = do_twice'' := rfl /- Inception! Here we define a function that takes as an argument a function that takes as an argument a function (and a natural number) -/ def do_twice''' (f: (ℕ → ℕ) → ℕ → ℕ) (x: (ℕ → ℕ)) : (ℕ → ℕ) := f (f x) #check do_twice''' #check do_twice''' do_twice #reduce do_twice''' /- Use #eval instead of #reduce because #reduce will have recursion problems unless we increase our stack space. -/ #eval (do_twice''' do_twice) square 2 #eval 2^16
70a3939a0c6323f6f65e7483027b558f6e017c19
4fa161becb8ce7378a709f5992a594764699e268
/src/analysis/calculus/deriv.lean
5604a1a4d2132ae51ea7fe1afd572514be32ec21
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
69,586
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Sébastien Gouëzel -/ import analysis.calculus.fderiv import data.polynomial /-! # One-dimensional derivatives This file defines the derivative of a function `f : 𝕜 → F` where `𝕜` is a normed field and `F` is a normed space over this field. The derivative of such a function `f` at a point `x` is given by an element `f' : F`. The theory is developed analogously to the [Fréchet derivatives](./fderiv.lean). We first introduce predicates defined in terms of the corresponding predicates for Fréchet derivatives: - `has_deriv_at_filter f f' x L` states that the function `f` has the derivative `f'` at the point `x` as `x` goes along the filter `L`. - `has_deriv_within_at f f' s x` states that the function `f` has the derivative `f'` at the point `x` within the subset `s`. - `has_deriv_at f f' x` states that the function `f` has the derivative `f'` at the point `x`. - `has_strict_deriv_at f f' x` states that the function `f` has the derivative `f'` at the point `x` in the sense of strict differentiability, i.e., `f y - f z = (y - z) • f' + o (y - z)` as `y, z → x`. For the last two notions we also define a functional version: - `deriv_within f s x` is a derivative of `f` at `x` within `s`. If the derivative does not exist, then `deriv_within f s x` equals zero. - `deriv f x` is a derivative of `f` at `x`. If the derivative does not exist, then `deriv f x` equals zero. The theorems `fderiv_within_deriv_within` and `fderiv_deriv` show that the one-dimensional derivatives coincide with the general Fréchet derivatives. We also show the existence and compute the derivatives of: - constants - the identity function - linear maps - addition - sum of finitely many functions - negation - subtraction - multiplication - inverse `x → x⁻¹` - multiplication of two functions in `𝕜 → 𝕜` - multiplication of a function in `𝕜 → 𝕜` and of a function in `𝕜 → E` - composition of a function in `𝕜 → F` with a function in `𝕜 → 𝕜` - composition of a function in `F → E` with a function in `𝕜 → F` - inverse function (assuming that it exists; the inverse function theorem is in `inverse.lean`) - division - polynomials For most binary operations we also define `const_op` and `op_const` theorems for the cases when the first or second argument is a constant. This makes writing chains of `has_deriv_at`'s easier, and they more frequently lead to the desired result. We set up the simplifier so that it can compute the derivative of simple functions. For instance, ```lean example (x : ℝ) : deriv (λ x, cos (sin x) * exp x) x = (cos(sin(x))-sin(sin(x))*cos(x))*exp(x) := by { simp, ring } ``` ## Implementation notes Most of the theorems are direct restatements of the corresponding theorems for Fréchet derivatives. The strategy to construct simp lemmas that give the simplifier the possibility to compute derivatives is the same as the one for differentiability statements, as explained in `fderiv.lean`. See the explanations there. -/ universes u v w noncomputable theory open_locale classical topological_space big_operators open filter asymptotics set open continuous_linear_map (smul_right smul_right_one_eq_iff) variables {𝕜 : Type u} [nondiscrete_normed_field 𝕜] section variables {F : Type v} [normed_group F] [normed_space 𝕜 F] variables {E : Type w} [normed_group E] [normed_space 𝕜 E] /-- `f` has the derivative `f'` at the point `x` as `x` goes along the filter `L`. That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges along the filter `L`. -/ def has_deriv_at_filter (f : 𝕜 → F) (f' : F) (x : 𝕜) (L : filter 𝕜) := has_fderiv_at_filter f (smul_right 1 f' : 𝕜 →L[𝕜] F) x L /-- `f` has the derivative `f'` at the point `x` within the subset `s`. That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges to `x` inside `s`. -/ def has_deriv_within_at (f : 𝕜 → F) (f' : F) (s : set 𝕜) (x : 𝕜) := has_deriv_at_filter f f' x (nhds_within x s) /-- `f` has the derivative `f'` at the point `x`. That is, `f x' = f x + (x' - x) • f' + o(x' - x)` where `x'` converges to `x`. -/ def has_deriv_at (f : 𝕜 → F) (f' : F) (x : 𝕜) := has_deriv_at_filter f f' x (𝓝 x) /-- `f` has the derivative `f'` at the point `x` in the sense of strict differentiability. That is, `f y - f z = (y - z) • f' + o(y - z)` as `y, z → x`. -/ def has_strict_deriv_at (f : 𝕜 → F) (f' : F) (x : 𝕜) := has_strict_fderiv_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) x /-- Derivative of `f` at the point `x` within the set `s`, if it exists. Zero otherwise. If the derivative exists (i.e., `∃ f', has_deriv_within_at f f' s x`), then `f x' = f x + (x' - x) • deriv_within f s x + o(x' - x)` where `x'` converges to `x` inside `s`. -/ def deriv_within (f : 𝕜 → F) (s : set 𝕜) (x : 𝕜) := (fderiv_within 𝕜 f s x : 𝕜 →L[𝕜] F) 1 /-- Derivative of `f` at the point `x`, if it exists. Zero otherwise. If the derivative exists (i.e., `∃ f', has_deriv_at f f' x`), then `f x' = f x + (x' - x) • deriv f x + o(x' - x)` where `x'` converges to `x`. -/ def deriv (f : 𝕜 → F) (x : 𝕜) := (fderiv 𝕜 f x : 𝕜 →L[𝕜] F) 1 variables {f f₀ f₁ g : 𝕜 → F} variables {f' f₀' f₁' g' : F} variables {x : 𝕜} variables {s t : set 𝕜} variables {L L₁ L₂ : filter 𝕜} /-- Expressing `has_fderiv_at_filter f f' x L` in terms of `has_deriv_at_filter` -/ lemma has_fderiv_at_filter_iff_has_deriv_at_filter {f' : 𝕜 →L[𝕜] F} : has_fderiv_at_filter f f' x L ↔ has_deriv_at_filter f (f' 1) x L := by simp [has_deriv_at_filter] lemma has_fderiv_at_filter.has_deriv_at_filter {f' : 𝕜 →L[𝕜] F} : has_fderiv_at_filter f f' x L → has_deriv_at_filter f (f' 1) x L := has_fderiv_at_filter_iff_has_deriv_at_filter.mp /-- Expressing `has_fderiv_within_at f f' s x` in terms of `has_deriv_within_at` -/ lemma has_fderiv_within_at_iff_has_deriv_within_at {f' : 𝕜 →L[𝕜] F} : has_fderiv_within_at f f' s x ↔ has_deriv_within_at f (f' 1) s x := has_fderiv_at_filter_iff_has_deriv_at_filter /-- Expressing `has_deriv_within_at f f' s x` in terms of `has_fderiv_within_at` -/ lemma has_deriv_within_at_iff_has_fderiv_within_at {f' : F} : has_deriv_within_at f f' s x ↔ has_fderiv_within_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) s x := iff.rfl lemma has_fderiv_within_at.has_deriv_within_at {f' : 𝕜 →L[𝕜] F} : has_fderiv_within_at f f' s x → has_deriv_within_at f (f' 1) s x := has_fderiv_within_at_iff_has_deriv_within_at.mp lemma has_deriv_within_at.has_fderiv_within_at {f' : F} : has_deriv_within_at f f' s x → has_fderiv_within_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) s x := has_deriv_within_at_iff_has_fderiv_within_at.mp /-- Expressing `has_fderiv_at f f' x` in terms of `has_deriv_at` -/ lemma has_fderiv_at_iff_has_deriv_at {f' : 𝕜 →L[𝕜] F} : has_fderiv_at f f' x ↔ has_deriv_at f (f' 1) x := has_fderiv_at_filter_iff_has_deriv_at_filter lemma has_fderiv_at.has_deriv_at {f' : 𝕜 →L[𝕜] F} : has_fderiv_at f f' x → has_deriv_at f (f' 1) x := has_fderiv_at_iff_has_deriv_at.mp lemma has_strict_fderiv_at_iff_has_strict_deriv_at {f' : 𝕜 →L[𝕜] F} : has_strict_fderiv_at f f' x ↔ has_strict_deriv_at f (f' 1) x := by simp [has_strict_deriv_at, has_strict_fderiv_at] protected lemma has_strict_fderiv_at.has_strict_deriv_at {f' : 𝕜 →L[𝕜] F} : has_strict_fderiv_at f f' x → has_strict_deriv_at f (f' 1) x := has_strict_fderiv_at_iff_has_strict_deriv_at.mp /-- Expressing `has_deriv_at f f' x` in terms of `has_fderiv_at` -/ lemma has_deriv_at_iff_has_fderiv_at {f' : F} : has_deriv_at f f' x ↔ has_fderiv_at f (smul_right 1 f' : 𝕜 →L[𝕜] F) x := iff.rfl lemma deriv_within_zero_of_not_differentiable_within_at (h : ¬ differentiable_within_at 𝕜 f s x) : deriv_within f s x = 0 := by { unfold deriv_within, rw fderiv_within_zero_of_not_differentiable_within_at, simp, assumption } lemma deriv_zero_of_not_differentiable_at (h : ¬ differentiable_at 𝕜 f x) : deriv f x = 0 := by { unfold deriv, rw fderiv_zero_of_not_differentiable_at, simp, assumption } theorem unique_diff_within_at.eq_deriv (s : set 𝕜) (H : unique_diff_within_at 𝕜 s x) (h : has_deriv_within_at f f' s x) (h₁ : has_deriv_within_at f f₁' s x) : f' = f₁' := smul_right_one_eq_iff.mp $ unique_diff_within_at.eq H h h₁ theorem has_deriv_at_filter_iff_tendsto : has_deriv_at_filter f f' x L ↔ tendsto (λ x' : 𝕜, ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) L (𝓝 0) := has_fderiv_at_filter_iff_tendsto theorem has_deriv_within_at_iff_tendsto : has_deriv_within_at f f' s x ↔ tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) (nhds_within x s) (𝓝 0) := has_fderiv_at_filter_iff_tendsto theorem has_deriv_at_iff_tendsto : has_deriv_at f f' x ↔ tendsto (λ x', ∥x' - x∥⁻¹ * ∥f x' - f x - (x' - x) • f'∥) (𝓝 x) (𝓝 0) := has_fderiv_at_filter_iff_tendsto theorem has_strict_deriv_at.has_deriv_at (h : has_strict_deriv_at f f' x) : has_deriv_at f f' x := h.has_fderiv_at /-- If the domain has dimension one, then Fréchet derivative is equivalent to the classical definition with a limit. In this version we have to take the limit along the subset `-{x}`, because for `y=x` the slope equals zero due to the convention `0⁻¹=0`. -/ lemma has_deriv_at_filter_iff_tendsto_slope {x : 𝕜} {L : filter 𝕜} : has_deriv_at_filter f f' x L ↔ tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (L ⊓ principal (-{x})) (𝓝 f') := begin conv_lhs { simp only [has_deriv_at_filter_iff_tendsto, (normed_field.norm_inv _).symm, (norm_smul _ _).symm, tendsto_zero_iff_norm_tendsto_zero.symm] }, conv_rhs { rw [← nhds_translation f', tendsto_comap_iff] }, refine (tendsto_inf_principal_nhds_iff_of_forall_eq $ by simp).symm.trans (tendsto_congr' _), rw mem_inf_principal, refine univ_mem_sets' (λ z hz, _), have : z ≠ x, by simpa [function.comp] using hz, simp only [mem_set_of_eq], rw [smul_sub, ← mul_smul, inv_mul_cancel (sub_ne_zero.2 this), one_smul] end lemma has_deriv_within_at_iff_tendsto_slope {x : 𝕜} {s : set 𝕜} : has_deriv_within_at f f' s x ↔ tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (nhds_within x (s \ {x})) (𝓝 f') := begin simp only [has_deriv_within_at, nhds_within, diff_eq, inf_assoc.symm, inf_principal.symm], exact has_deriv_at_filter_iff_tendsto_slope end lemma has_deriv_within_at_iff_tendsto_slope' {x : 𝕜} {s : set 𝕜} (hs : x ∉ s) : has_deriv_within_at f f' s x ↔ tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (nhds_within x s) (𝓝 f') := begin convert ← has_deriv_within_at_iff_tendsto_slope, exact diff_singleton_eq_self hs end lemma has_deriv_at_iff_tendsto_slope {x : 𝕜} : has_deriv_at f f' x ↔ tendsto (λ y, (y - x)⁻¹ • (f y - f x)) (nhds_within x (-{x})) (𝓝 f') := has_deriv_at_filter_iff_tendsto_slope theorem has_deriv_at_iff_is_o_nhds_zero : has_deriv_at f f' x ↔ is_o (λh, f (x + h) - f x - h • f') (λh, h) (𝓝 0) := has_fderiv_at_iff_is_o_nhds_zero theorem has_deriv_at_filter.mono (h : has_deriv_at_filter f f' x L₂) (hst : L₁ ≤ L₂) : has_deriv_at_filter f f' x L₁ := has_fderiv_at_filter.mono h hst theorem has_deriv_within_at.mono (h : has_deriv_within_at f f' t x) (hst : s ⊆ t) : has_deriv_within_at f f' s x := has_fderiv_within_at.mono h hst theorem has_deriv_at.has_deriv_at_filter (h : has_deriv_at f f' x) (hL : L ≤ 𝓝 x) : has_deriv_at_filter f f' x L := has_fderiv_at.has_fderiv_at_filter h hL theorem has_deriv_at.has_deriv_within_at (h : has_deriv_at f f' x) : has_deriv_within_at f f' s x := has_fderiv_at.has_fderiv_within_at h lemma has_deriv_within_at.differentiable_within_at (h : has_deriv_within_at f f' s x) : differentiable_within_at 𝕜 f s x := has_fderiv_within_at.differentiable_within_at h lemma has_deriv_at.differentiable_at (h : has_deriv_at f f' x) : differentiable_at 𝕜 f x := has_fderiv_at.differentiable_at h @[simp] lemma has_deriv_within_at_univ : has_deriv_within_at f f' univ x ↔ has_deriv_at f f' x := has_fderiv_within_at_univ theorem has_deriv_at_unique (h₀ : has_deriv_at f f₀' x) (h₁ : has_deriv_at f f₁' x) : f₀' = f₁' := smul_right_one_eq_iff.mp $ has_fderiv_at_unique h₀ h₁ lemma has_deriv_within_at_inter' (h : t ∈ nhds_within x s) : has_deriv_within_at f f' (s ∩ t) x ↔ has_deriv_within_at f f' s x := has_fderiv_within_at_inter' h lemma has_deriv_within_at_inter (h : t ∈ 𝓝 x) : has_deriv_within_at f f' (s ∩ t) x ↔ has_deriv_within_at f f' s x := has_fderiv_within_at_inter h lemma has_deriv_within_at.union (hs : has_deriv_within_at f f' s x) (ht : has_deriv_within_at f f' t x) : has_deriv_within_at f f' (s ∪ t) x := begin simp only [has_deriv_within_at, nhds_within_union], exact hs.join ht, end lemma has_deriv_within_at.nhds_within (h : has_deriv_within_at f f' s x) (ht : s ∈ nhds_within x t) : has_deriv_within_at f f' t x := (has_deriv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _)) lemma has_deriv_within_at.has_deriv_at (h : has_deriv_within_at f f' s x) (hs : s ∈ 𝓝 x) : has_deriv_at f f' x := has_fderiv_within_at.has_fderiv_at h hs lemma differentiable_within_at.has_deriv_within_at (h : differentiable_within_at 𝕜 f s x) : has_deriv_within_at f (deriv_within f s x) s x := show has_fderiv_within_at _ _ _ _, by { convert h.has_fderiv_within_at, simp [deriv_within] } lemma differentiable_at.has_deriv_at (h : differentiable_at 𝕜 f x) : has_deriv_at f (deriv f x) x := show has_fderiv_at _ _ _, by { convert h.has_fderiv_at, simp [deriv] } lemma has_deriv_at.deriv (h : has_deriv_at f f' x) : deriv f x = f' := has_deriv_at_unique h.differentiable_at.has_deriv_at h lemma has_deriv_within_at.deriv_within (h : has_deriv_within_at f f' s x) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within f s x = f' := hxs.eq_deriv _ h.differentiable_within_at.has_deriv_within_at h lemma fderiv_within_deriv_within : (fderiv_within 𝕜 f s x : 𝕜 → F) 1 = deriv_within f s x := rfl lemma deriv_within_fderiv_within : smul_right 1 (deriv_within f s x) = fderiv_within 𝕜 f s x := by simp [deriv_within] lemma fderiv_deriv : (fderiv 𝕜 f x : 𝕜 → F) 1 = deriv f x := rfl lemma deriv_fderiv : smul_right 1 (deriv f x) = fderiv 𝕜 f x := by simp [deriv] lemma differentiable_at.deriv_within (h : differentiable_at 𝕜 f x) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within f s x = deriv f x := by { unfold deriv_within deriv, rw h.fderiv_within hxs } lemma deriv_within_subset (st : s ⊆ t) (ht : unique_diff_within_at 𝕜 s x) (h : differentiable_within_at 𝕜 f t x) : deriv_within f s x = deriv_within f t x := ((differentiable_within_at.has_deriv_within_at h).mono st).deriv_within ht @[simp] lemma deriv_within_univ : deriv_within f univ = deriv f := by { ext, unfold deriv_within deriv, rw fderiv_within_univ } lemma deriv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_diff_within_at 𝕜 s x) : deriv_within f (s ∩ t) x = deriv_within f s x := by { unfold deriv_within, rw fderiv_within_inter ht hs } section congr /-! ### Congruence properties of derivatives -/ theorem has_deriv_at_filter_congr_of_mem_sets (hx : f₀ x = f₁ x) (h₀ : ∀ᶠ x in L, f₀ x = f₁ x) (h₁ : f₀' = f₁') : has_deriv_at_filter f₀ f₀' x L ↔ has_deriv_at_filter f₁ f₁' x L := has_fderiv_at_filter_congr_of_mem_sets hx h₀ (by simp [h₁]) lemma has_deriv_at_filter.congr_of_mem_sets (h : has_deriv_at_filter f f' x L) (hL : ∀ᶠ x in L, f₁ x = f x) (hx : f₁ x = f x) : has_deriv_at_filter f₁ f' x L := by rwa has_deriv_at_filter_congr_of_mem_sets hx hL rfl lemma has_deriv_within_at.congr_mono (h : has_deriv_within_at f f' s x) (ht : ∀x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) : has_deriv_within_at f₁ f' t x := has_fderiv_within_at.congr_mono h ht hx h₁ lemma has_deriv_within_at.congr (h : has_deriv_within_at f f' s x) (hs : ∀x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : has_deriv_within_at f₁ f' s x := h.congr_mono hs hx (subset.refl _) lemma has_deriv_within_at.congr_of_mem_nhds_within (h : has_deriv_within_at f f' s x) (h₁ : ∀ᶠ y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) : has_deriv_within_at f₁ f' s x := has_deriv_at_filter.congr_of_mem_sets h h₁ hx lemma has_deriv_at.congr_of_mem_nhds (h : has_deriv_at f f' x) (h₁ : ∀ᶠ y in 𝓝 x, f₁ y = f y) : has_deriv_at f₁ f' x := has_deriv_at_filter.congr_of_mem_sets h h₁ (mem_of_nhds h₁ : _) lemma deriv_within_congr_of_mem_nhds_within (hs : unique_diff_within_at 𝕜 s x) (hL : ∀ᶠ y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) : deriv_within f₁ s x = deriv_within f s x := by { unfold deriv_within, rw fderiv_within_congr_of_mem_nhds_within hs hL hx } lemma deriv_within_congr (hs : unique_diff_within_at 𝕜 s x) (hL : ∀y∈s, f₁ y = f y) (hx : f₁ x = f x) : deriv_within f₁ s x = deriv_within f s x := by { unfold deriv_within, rw fderiv_within_congr hs hL hx } lemma deriv_congr_of_mem_nhds (hL : ∀ᶠ y in 𝓝 x, f₁ y = f y) : deriv f₁ x = deriv f x := by { unfold deriv, rwa fderiv_congr_of_mem_nhds } end congr section id /-! ### Derivative of the identity -/ variables (s x L) theorem has_deriv_at_filter_id : has_deriv_at_filter id 1 x L := (has_fderiv_at_filter_id x L).has_deriv_at_filter theorem has_deriv_within_at_id : has_deriv_within_at id 1 s x := has_deriv_at_filter_id _ _ theorem has_deriv_at_id : has_deriv_at id 1 x := has_deriv_at_filter_id _ _ theorem has_deriv_at_id' : has_deriv_at (λ (x : 𝕜), x) 1 x := has_deriv_at_filter_id _ _ theorem has_strict_deriv_at_id : has_strict_deriv_at id 1 x := (has_strict_fderiv_at_id x).has_strict_deriv_at lemma deriv_id : deriv id x = 1 := has_deriv_at.deriv (has_deriv_at_id x) @[simp] lemma deriv_id' : deriv (@id 𝕜) = λ _, 1 := funext deriv_id @[simp] lemma deriv_id'' : deriv (λ x : 𝕜, x) x = 1 := deriv_id x lemma deriv_within_id (hxs : unique_diff_within_at 𝕜 s x) : deriv_within id s x = 1 := (has_deriv_within_at_id x s).deriv_within hxs end id section const /-! ### Derivative of constant functions -/ variables (c : F) (s x L) theorem has_deriv_at_filter_const : has_deriv_at_filter (λ x, c) 0 x L := (has_fderiv_at_filter_const c x L).has_deriv_at_filter theorem has_strict_deriv_at_const : has_strict_deriv_at (λ x, c) 0 x := (has_strict_fderiv_at_const c x).has_strict_deriv_at theorem has_deriv_within_at_const : has_deriv_within_at (λ x, c) 0 s x := has_deriv_at_filter_const _ _ _ theorem has_deriv_at_const : has_deriv_at (λ x, c) 0 x := has_deriv_at_filter_const _ _ _ lemma deriv_const : deriv (λ x, c) x = 0 := has_deriv_at.deriv (has_deriv_at_const x c) @[simp] lemma deriv_const' : deriv (λ x:𝕜, c) = λ x, 0 := funext (λ x, deriv_const x c) lemma deriv_within_const (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λ x, c) s x = 0 := (has_deriv_within_at_const _ _ _).deriv_within hxs end const section continuous_linear_map /-! ### Derivative of continuous linear maps -/ variables (e : 𝕜 →L[𝕜] F) lemma continuous_linear_map.has_deriv_at_filter : has_deriv_at_filter e (e 1) x L := e.has_fderiv_at_filter.has_deriv_at_filter lemma continuous_linear_map.has_strict_deriv_at : has_strict_deriv_at e (e 1) x := e.has_strict_fderiv_at.has_strict_deriv_at lemma continuous_linear_map.has_deriv_at : has_deriv_at e (e 1) x := e.has_deriv_at_filter lemma continuous_linear_map.has_deriv_within_at : has_deriv_within_at e (e 1) s x := e.has_deriv_at_filter @[simp] lemma continuous_linear_map.deriv : deriv e x = e 1 := e.has_deriv_at.deriv lemma continuous_linear_map.deriv_within (hxs : unique_diff_within_at 𝕜 s x) : deriv_within e s x = e 1 := e.has_deriv_within_at.deriv_within hxs end continuous_linear_map section linear_map /-! ### Derivative of bundled linear maps -/ variables (e : 𝕜 →ₗ[𝕜] F) lemma linear_map.has_deriv_at_filter : has_deriv_at_filter e (e 1) x L := e.to_continuous_linear_map₁.has_deriv_at_filter lemma linear_map.has_strict_deriv_at : has_strict_deriv_at e (e 1) x := e.to_continuous_linear_map₁.has_strict_deriv_at lemma linear_map.has_deriv_at : has_deriv_at e (e 1) x := e.has_deriv_at_filter lemma linear_map.has_deriv_within_at : has_deriv_within_at e (e 1) s x := e.has_deriv_at_filter @[simp] lemma linear_map.deriv : deriv e x = e 1 := e.has_deriv_at.deriv lemma linear_map.deriv_within (hxs : unique_diff_within_at 𝕜 s x) : deriv_within e s x = e 1 := e.has_deriv_within_at.deriv_within hxs end linear_map section add /-! ### Derivative of the sum of two functions -/ theorem has_deriv_at_filter.add (hf : has_deriv_at_filter f f' x L) (hg : has_deriv_at_filter g g' x L) : has_deriv_at_filter (λ y, f y + g y) (f' + g') x L := by simpa using (hf.add hg).has_deriv_at_filter theorem has_strict_deriv_at.add (hf : has_strict_deriv_at f f' x) (hg : has_strict_deriv_at g g' x) : has_strict_deriv_at (λ y, f y + g y) (f' + g') x := by simpa using (hf.add hg).has_strict_deriv_at theorem has_deriv_within_at.add (hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) : has_deriv_within_at (λ y, f y + g y) (f' + g') s x := hf.add hg theorem has_deriv_at.add (hf : has_deriv_at f f' x) (hg : has_deriv_at g g' x) : has_deriv_at (λ x, f x + g x) (f' + g') x := hf.add hg lemma deriv_within_add (hxs : unique_diff_within_at 𝕜 s x) (hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) : deriv_within (λy, f y + g y) s x = deriv_within f s x + deriv_within g s x := (hf.has_deriv_within_at.add hg.has_deriv_within_at).deriv_within hxs @[simp] lemma deriv_add (hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) : deriv (λy, f y + g y) x = deriv f x + deriv g x := (hf.has_deriv_at.add hg.has_deriv_at).deriv theorem has_deriv_at_filter.add_const (hf : has_deriv_at_filter f f' x L) (c : F) : has_deriv_at_filter (λ y, f y + c) f' x L := add_zero f' ▸ hf.add (has_deriv_at_filter_const x L c) theorem has_deriv_within_at.add_const (hf : has_deriv_within_at f f' s x) (c : F) : has_deriv_within_at (λ y, f y + c) f' s x := hf.add_const c theorem has_deriv_at.add_const (hf : has_deriv_at f f' x) (c : F) : has_deriv_at (λ x, f x + c) f' x := hf.add_const c lemma deriv_within_add_const (hxs : unique_diff_within_at 𝕜 s x) (hf : differentiable_within_at 𝕜 f s x) (c : F) : deriv_within (λy, f y + c) s x = deriv_within f s x := (hf.has_deriv_within_at.add_const c).deriv_within hxs lemma deriv_add_const (hf : differentiable_at 𝕜 f x) (c : F) : deriv (λy, f y + c) x = deriv f x := (hf.has_deriv_at.add_const c).deriv theorem has_deriv_at_filter.const_add (c : F) (hf : has_deriv_at_filter f f' x L) : has_deriv_at_filter (λ y, c + f y) f' x L := zero_add f' ▸ (has_deriv_at_filter_const x L c).add hf theorem has_deriv_within_at.const_add (c : F) (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ y, c + f y) f' s x := hf.const_add c theorem has_deriv_at.const_add (c : F) (hf : has_deriv_at f f' x) : has_deriv_at (λ x, c + f x) f' x := hf.const_add c lemma deriv_within_const_add (hxs : unique_diff_within_at 𝕜 s x) (c : F) (hf : differentiable_within_at 𝕜 f s x) : deriv_within (λy, c + f y) s x = deriv_within f s x := (hf.has_deriv_within_at.const_add c).deriv_within hxs lemma deriv_const_add (c : F) (hf : differentiable_at 𝕜 f x) : deriv (λy, c + f y) x = deriv f x := (hf.has_deriv_at.const_add c).deriv end add section sum /-! ### Derivative of a finite sum of functions -/ open_locale big_operators variables {ι : Type*} {u : finset ι} {A : ι → (𝕜 → F)} {A' : ι → F} theorem has_deriv_at_filter.sum (h : ∀ i ∈ u, has_deriv_at_filter (A i) (A' i) x L) : has_deriv_at_filter (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x L := by simpa [continuous_linear_map.sum_apply] using (has_fderiv_at_filter.sum h).has_deriv_at_filter theorem has_strict_deriv_at.sum (h : ∀ i ∈ u, has_strict_deriv_at (A i) (A' i) x) : has_strict_deriv_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x := by simpa [continuous_linear_map.sum_apply] using (has_strict_fderiv_at.sum h).has_strict_deriv_at theorem has_deriv_within_at.sum (h : ∀ i ∈ u, has_deriv_within_at (A i) (A' i) s x) : has_deriv_within_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) s x := has_deriv_at_filter.sum h theorem has_deriv_at.sum (h : ∀ i ∈ u, has_deriv_at (A i) (A' i) x) : has_deriv_at (λ y, ∑ i in u, A i y) (∑ i in u, A' i) x := has_deriv_at_filter.sum h lemma deriv_within_sum (hxs : unique_diff_within_at 𝕜 s x) (h : ∀ i ∈ u, differentiable_within_at 𝕜 (A i) s x) : deriv_within (λ y, ∑ i in u, A i y) s x = ∑ i in u, deriv_within (A i) s x := (has_deriv_within_at.sum (λ i hi, (h i hi).has_deriv_within_at)).deriv_within hxs @[simp] lemma deriv_sum (h : ∀ i ∈ u, differentiable_at 𝕜 (A i) x) : deriv (λ y, ∑ i in u, A i y) x = ∑ i in u, deriv (A i) x := (has_deriv_at.sum (λ i hi, (h i hi).has_deriv_at)).deriv end sum section mul_vector /-! ### Derivative of the multiplication of a scalar function and a vector function -/ variables {c : 𝕜 → 𝕜} {c' : 𝕜} theorem has_deriv_within_at.smul (hc : has_deriv_within_at c c' s x) (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ y, c y • f y) (c x • f' + c' • f x) s x := by simpa using (has_fderiv_within_at.smul hc hf).has_deriv_within_at theorem has_deriv_at.smul (hc : has_deriv_at c c' x) (hf : has_deriv_at f f' x) : has_deriv_at (λ y, c y • f y) (c x • f' + c' • f x) x := begin rw [← has_deriv_within_at_univ] at *, exact hc.smul hf end theorem has_strict_deriv_at.smul (hc : has_strict_deriv_at c c' x) (hf : has_strict_deriv_at f f' x) : has_strict_deriv_at (λ y, c y • f y) (c x • f' + c' • f x) x := by simpa using (hc.smul hf).has_strict_deriv_at lemma deriv_within_smul (hxs : unique_diff_within_at 𝕜 s x) (hc : differentiable_within_at 𝕜 c s x) (hf : differentiable_within_at 𝕜 f s x) : deriv_within (λ y, c y • f y) s x = c x • deriv_within f s x + (deriv_within c s x) • f x := (hc.has_deriv_within_at.smul hf.has_deriv_within_at).deriv_within hxs lemma deriv_smul (hc : differentiable_at 𝕜 c x) (hf : differentiable_at 𝕜 f x) : deriv (λ y, c y • f y) x = c x • deriv f x + (deriv c x) • f x := (hc.has_deriv_at.smul hf.has_deriv_at).deriv theorem has_deriv_within_at.smul_const (hc : has_deriv_within_at c c' s x) (f : F) : has_deriv_within_at (λ y, c y • f) (c' • f) s x := begin have := hc.smul (has_deriv_within_at_const x s f), rwa [smul_zero, zero_add] at this end theorem has_deriv_at.smul_const (hc : has_deriv_at c c' x) (f : F) : has_deriv_at (λ y, c y • f) (c' • f) x := begin rw [← has_deriv_within_at_univ] at *, exact hc.smul_const f end lemma deriv_within_smul_const (hxs : unique_diff_within_at 𝕜 s x) (hc : differentiable_within_at 𝕜 c s x) (f : F) : deriv_within (λ y, c y • f) s x = (deriv_within c s x) • f := (hc.has_deriv_within_at.smul_const f).deriv_within hxs lemma deriv_smul_const (hc : differentiable_at 𝕜 c x) (f : F) : deriv (λ y, c y • f) x = (deriv c x) • f := (hc.has_deriv_at.smul_const f).deriv theorem has_deriv_within_at.const_smul (c : 𝕜) (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ y, c • f y) (c • f') s x := begin convert (has_deriv_within_at_const x s c).smul hf, rw [zero_smul, add_zero] end theorem has_deriv_at.const_smul (c : 𝕜) (hf : has_deriv_at f f' x) : has_deriv_at (λ y, c • f y) (c • f') x := begin rw [← has_deriv_within_at_univ] at *, exact hf.const_smul c end lemma deriv_within_const_smul (hxs : unique_diff_within_at 𝕜 s x) (c : 𝕜) (hf : differentiable_within_at 𝕜 f s x) : deriv_within (λ y, c • f y) s x = c • deriv_within f s x := (hf.has_deriv_within_at.const_smul c).deriv_within hxs lemma deriv_const_smul (c : 𝕜) (hf : differentiable_at 𝕜 f x) : deriv (λ y, c • f y) x = c • deriv f x := (hf.has_deriv_at.const_smul c).deriv end mul_vector section neg /-! ### Derivative of the negative of a function -/ theorem has_deriv_at_filter.neg (h : has_deriv_at_filter f f' x L) : has_deriv_at_filter (λ x, -f x) (-f') x L := by simpa using h.neg.has_deriv_at_filter theorem has_deriv_within_at.neg (h : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, -f x) (-f') s x := h.neg theorem has_deriv_at.neg (h : has_deriv_at f f' x) : has_deriv_at (λ x, -f x) (-f') x := h.neg theorem has_strict_deriv_at.neg (h : has_strict_deriv_at f f' x) : has_strict_deriv_at (λ x, -f x) (-f') x := by simpa using h.neg.has_strict_deriv_at lemma deriv_within_neg (hxs : unique_diff_within_at 𝕜 s x) (h : differentiable_within_at 𝕜 f s x) : deriv_within (λy, -f y) s x = - deriv_within f s x := h.has_deriv_within_at.neg.deriv_within hxs lemma deriv_neg : deriv (λy, -f y) x = - deriv f x := if h : differentiable_at 𝕜 f x then h.has_deriv_at.neg.deriv else have ¬differentiable_at 𝕜 (λ y, -f y) x, from λ h', by simpa only [neg_neg] using h'.neg, by simp only [deriv_zero_of_not_differentiable_at h, deriv_zero_of_not_differentiable_at this, neg_zero] @[simp] lemma deriv_neg' : deriv (λy, -f y) = (λ x, - deriv f x) := funext $ λ x, deriv_neg end neg section sub /-! ### Derivative of the difference of two functions -/ theorem has_deriv_at_filter.sub (hf : has_deriv_at_filter f f' x L) (hg : has_deriv_at_filter g g' x L) : has_deriv_at_filter (λ x, f x - g x) (f' - g') x L := hf.add hg.neg theorem has_deriv_within_at.sub (hf : has_deriv_within_at f f' s x) (hg : has_deriv_within_at g g' s x) : has_deriv_within_at (λ x, f x - g x) (f' - g') s x := hf.sub hg theorem has_deriv_at.sub (hf : has_deriv_at f f' x) (hg : has_deriv_at g g' x) : has_deriv_at (λ x, f x - g x) (f' - g') x := hf.sub hg theorem has_strict_deriv_at.sub (hf : has_strict_deriv_at f f' x) (hg : has_strict_deriv_at g g' x) : has_strict_deriv_at (λ x, f x - g x) (f' - g') x := hf.add hg.neg lemma deriv_within_sub (hxs : unique_diff_within_at 𝕜 s x) (hf : differentiable_within_at 𝕜 f s x) (hg : differentiable_within_at 𝕜 g s x) : deriv_within (λy, f y - g y) s x = deriv_within f s x - deriv_within g s x := (hf.has_deriv_within_at.sub hg.has_deriv_within_at).deriv_within hxs @[simp] lemma deriv_sub (hf : differentiable_at 𝕜 f x) (hg : differentiable_at 𝕜 g x) : deriv (λ y, f y - g y) x = deriv f x - deriv g x := (hf.has_deriv_at.sub hg.has_deriv_at).deriv theorem has_deriv_at_filter.is_O_sub (h : has_deriv_at_filter f f' x L) : is_O (λ x', f x' - f x) (λ x', x' - x) L := has_fderiv_at_filter.is_O_sub h theorem has_deriv_at_filter.sub_const (hf : has_deriv_at_filter f f' x L) (c : F) : has_deriv_at_filter (λ x, f x - c) f' x L := hf.add_const (-c) theorem has_deriv_within_at.sub_const (hf : has_deriv_within_at f f' s x) (c : F) : has_deriv_within_at (λ x, f x - c) f' s x := hf.sub_const c theorem has_deriv_at.sub_const (hf : has_deriv_at f f' x) (c : F) : has_deriv_at (λ x, f x - c) f' x := hf.sub_const c lemma deriv_within_sub_const (hxs : unique_diff_within_at 𝕜 s x) (hf : differentiable_within_at 𝕜 f s x) (c : F) : deriv_within (λy, f y - c) s x = deriv_within f s x := (hf.has_deriv_within_at.sub_const c).deriv_within hxs lemma deriv_sub_const (c : F) (hf : differentiable_at 𝕜 f x) : deriv (λ y, f y - c) x = deriv f x := (hf.has_deriv_at.sub_const c).deriv theorem has_deriv_at_filter.const_sub (c : F) (hf : has_deriv_at_filter f f' x L) : has_deriv_at_filter (λ x, c - f x) (-f') x L := hf.neg.const_add c theorem has_deriv_within_at.const_sub (c : F) (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (λ x, c - f x) (-f') s x := hf.const_sub c theorem has_deriv_at.const_sub (c : F) (hf : has_deriv_at f f' x) : has_deriv_at (λ x, c - f x) (-f') x := hf.const_sub c lemma deriv_within_const_sub (hxs : unique_diff_within_at 𝕜 s x) (c : F) (hf : differentiable_within_at 𝕜 f s x) : deriv_within (λy, c - f y) s x = -deriv_within f s x := (hf.has_deriv_within_at.const_sub c).deriv_within hxs lemma deriv_const_sub (c : F) (hf : differentiable_at 𝕜 f x) : deriv (λ y, c - f y) x = -deriv f x := (hf.has_deriv_at.const_sub c).deriv end sub section continuous /-! ### Continuity of a function admitting a derivative -/ theorem has_deriv_at_filter.tendsto_nhds (hL : L ≤ 𝓝 x) (h : has_deriv_at_filter f f' x L) : tendsto f L (𝓝 (f x)) := h.tendsto_nhds hL theorem has_deriv_within_at.continuous_within_at (h : has_deriv_within_at f f' s x) : continuous_within_at f s x := has_deriv_at_filter.tendsto_nhds inf_le_left h theorem has_deriv_at.continuous_at (h : has_deriv_at f f' x) : continuous_at f x := has_deriv_at_filter.tendsto_nhds (le_refl _) h end continuous section cartesian_product /-! ### Derivative of the cartesian product of two functions -/ variables {G : Type w} [normed_group G] [normed_space 𝕜 G] variables {f₂ : 𝕜 → G} {f₂' : G} lemma has_deriv_at_filter.prod (hf₁ : has_deriv_at_filter f₁ f₁' x L) (hf₂ : has_deriv_at_filter f₂ f₂' x L) : has_deriv_at_filter (λ x, (f₁ x, f₂ x)) (f₁', f₂') x L := show has_fderiv_at_filter _ _ _ _, by convert has_fderiv_at_filter.prod hf₁ hf₂ lemma has_deriv_within_at.prod (hf₁ : has_deriv_within_at f₁ f₁' s x) (hf₂ : has_deriv_within_at f₂ f₂' s x) : has_deriv_within_at (λ x, (f₁ x, f₂ x)) (f₁', f₂') s x := hf₁.prod hf₂ lemma has_deriv_at.prod (hf₁ : has_deriv_at f₁ f₁' x) (hf₂ : has_deriv_at f₂ f₂' x) : has_deriv_at (λ x, (f₁ x, f₂ x)) (f₁', f₂') x := hf₁.prod hf₂ end cartesian_product section composition /-! ### Derivative of the composition of a vector function and a scalar function We use `scomp` in lemmas on composition of vector valued and scalar valued functions, and `comp` in lemmas on composition of scalar valued functions, in analogy for `smul` and `mul` (and also because the `comp` version with the shorter name will show up much more often in applications). The formula for the derivative involves `smul` in `scomp` lemmas, which can be reduced to usual multiplication in `comp` lemmas. -/ variables {h h₁ h₂ : 𝕜 → 𝕜} {h' h₁' h₂' : 𝕜} /- For composition lemmas, we put x explicit to help the elaborator, as otherwise Lean tends to get confused since there are too many possibilities for composition -/ variable (x) theorem has_deriv_at_filter.scomp (hg : has_deriv_at_filter g g' (h x) (L.map h)) (hh : has_deriv_at_filter h h' x L) : has_deriv_at_filter (g ∘ h) (h' • g') x L := by simpa using (hg.comp x hh).has_deriv_at_filter theorem has_deriv_within_at.scomp {t : set 𝕜} (hg : has_deriv_within_at g g' t (h x)) (hh : has_deriv_within_at h h' s x) (hst : s ⊆ h ⁻¹' t) : has_deriv_within_at (g ∘ h) (h' • g') s x := begin apply has_deriv_at_filter.scomp _ (has_deriv_at_filter.mono hg _) hh, calc map h (nhds_within x s) ≤ nhds_within (h x) (h '' s) : hh.continuous_within_at.tendsto_nhds_within_image ... ≤ nhds_within (h x) t : nhds_within_mono _ (image_subset_iff.mpr hst) end /-- The chain rule. -/ theorem has_deriv_at.scomp (hg : has_deriv_at g g' (h x)) (hh : has_deriv_at h h' x) : has_deriv_at (g ∘ h) (h' • g') x := (hg.mono hh.continuous_at).scomp x hh theorem has_strict_deriv_at.scomp (hg : has_strict_deriv_at g g' (h x)) (hh : has_strict_deriv_at h h' x) : has_strict_deriv_at (g ∘ h) (h' • g') x := by simpa using (hg.comp x hh).has_strict_deriv_at theorem has_deriv_at.scomp_has_deriv_within_at (hg : has_deriv_at g g' (h x)) (hh : has_deriv_within_at h h' s x) : has_deriv_within_at (g ∘ h) (h' • g') s x := begin rw ← has_deriv_within_at_univ at hg, exact has_deriv_within_at.scomp x hg hh subset_preimage_univ end lemma deriv_within.scomp (hg : differentiable_within_at 𝕜 g t (h x)) (hh : differentiable_within_at 𝕜 h s x) (hs : s ⊆ h ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (g ∘ h) s x = deriv_within h s x • deriv_within g t (h x) := begin apply has_deriv_within_at.deriv_within _ hxs, exact has_deriv_within_at.scomp x (hg.has_deriv_within_at) (hh.has_deriv_within_at) hs end lemma deriv.scomp (hg : differentiable_at 𝕜 g (h x)) (hh : differentiable_at 𝕜 h x) : deriv (g ∘ h) x = deriv h x • deriv g (h x) := begin apply has_deriv_at.deriv, exact has_deriv_at.scomp x hg.has_deriv_at hh.has_deriv_at end /-! ### Derivative of the composition of two scalar functions -/ theorem has_deriv_at_filter.comp (hh₁ : has_deriv_at_filter h₁ h₁' (h₂ x) (L.map h₂)) (hh₂ : has_deriv_at_filter h₂ h₂' x L) : has_deriv_at_filter (h₁ ∘ h₂) (h₁' * h₂') x L := by { rw mul_comm, exact hh₁.scomp x hh₂ } theorem has_deriv_within_at.comp {t : set 𝕜} (hh₁ : has_deriv_within_at h₁ h₁' t (h₂ x)) (hh₂ : has_deriv_within_at h₂ h₂' s x) (hst : s ⊆ h₂ ⁻¹' t) : has_deriv_within_at (h₁ ∘ h₂) (h₁' * h₂') s x := by { rw mul_comm, exact hh₁.scomp x hh₂ hst, } /-- The chain rule. -/ theorem has_deriv_at.comp (hh₁ : has_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_deriv_at h₂ h₂' x) : has_deriv_at (h₁ ∘ h₂) (h₁' * h₂') x := (hh₁.mono hh₂.continuous_at).comp x hh₂ theorem has_strict_deriv_at.comp (hh₁ : has_strict_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_strict_deriv_at h₂ h₂' x) : has_strict_deriv_at (h₁ ∘ h₂) (h₁' * h₂') x := by { rw mul_comm, exact hh₁.scomp x hh₂ } theorem has_deriv_at.comp_has_deriv_within_at (hh₁ : has_deriv_at h₁ h₁' (h₂ x)) (hh₂ : has_deriv_within_at h₂ h₂' s x) : has_deriv_within_at (h₁ ∘ h₂) (h₁' * h₂') s x := begin rw ← has_deriv_within_at_univ at hh₁, exact has_deriv_within_at.comp x hh₁ hh₂ subset_preimage_univ end lemma deriv_within.comp (hh₁ : differentiable_within_at 𝕜 h₁ t (h₂ x)) (hh₂ : differentiable_within_at 𝕜 h₂ s x) (hs : s ⊆ h₂ ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (h₁ ∘ h₂) s x = deriv_within h₁ t (h₂ x) * deriv_within h₂ s x := begin apply has_deriv_within_at.deriv_within _ hxs, exact has_deriv_within_at.comp x (hh₁.has_deriv_within_at) (hh₂.has_deriv_within_at) hs end lemma deriv.comp (hh₁ : differentiable_at 𝕜 h₁ (h₂ x)) (hh₂ : differentiable_at 𝕜 h₂ x) : deriv (h₁ ∘ h₂) x = deriv h₁ (h₂ x) * deriv h₂ x := begin apply has_deriv_at.deriv, exact has_deriv_at.comp x hh₁.has_deriv_at hh₂.has_deriv_at end protected lemma has_deriv_at_filter.iterate {f : 𝕜 → 𝕜} {f' : 𝕜} (hf : has_deriv_at_filter f f' x L) (hL : tendsto f L L) (hx : f x = x) (n : ℕ) : has_deriv_at_filter (f^[n]) (f'^n) x L := begin have := hf.iterate hL hx n, rwa [continuous_linear_map.smul_right_one_pow] at this end protected lemma has_deriv_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜} (hf : has_deriv_at f f' x) (hx : f x = x) (n : ℕ) : has_deriv_at (f^[n]) (f'^n) x := begin have := has_fderiv_at.iterate hf hx n, rwa [continuous_linear_map.smul_right_one_pow] at this end protected lemma has_deriv_within_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜} (hf : has_deriv_within_at f f' s x) (hx : f x = x) (hs : maps_to f s s) (n : ℕ) : has_deriv_within_at (f^[n]) (f'^n) s x := begin have := has_fderiv_within_at.iterate hf hx hs n, rwa [continuous_linear_map.smul_right_one_pow] at this end protected lemma has_strict_deriv_at.iterate {f : 𝕜 → 𝕜} {f' : 𝕜} (hf : has_strict_deriv_at f f' x) (hx : f x = x) (n : ℕ) : has_strict_deriv_at (f^[n]) (f'^n) x := begin have := hf.iterate hx n, rwa [continuous_linear_map.smul_right_one_pow] at this end end composition section composition_vector /-! ### Derivative of the composition of a function between vector spaces and of a function defined on `𝕜` -/ variables {l : F → E} {l' : F →L[𝕜] E} variable (x) /-- The composition `l ∘ f` where `l : F → E` and `f : 𝕜 → F`, has a derivative within a set equal to the Fréchet derivative of `l` applied to the derivative of `f`. -/ theorem has_fderiv_within_at.comp_has_deriv_within_at {t : set F} (hl : has_fderiv_within_at l l' t (f x)) (hf : has_deriv_within_at f f' s x) (hst : s ⊆ f ⁻¹' t) : has_deriv_within_at (l ∘ f) (l' (f')) s x := begin rw has_deriv_within_at_iff_has_fderiv_within_at, convert has_fderiv_within_at.comp x hl hf hst, ext, simp end /-- The composition `l ∘ f` where `l : F → E` and `f : 𝕜 → F`, has a derivative equal to the Fréchet derivative of `l` applied to the derivative of `f`. -/ theorem has_fderiv_at.comp_has_deriv_at (hl : has_fderiv_at l l' (f x)) (hf : has_deriv_at f f' x) : has_deriv_at (l ∘ f) (l' (f')) x := begin rw has_deriv_at_iff_has_fderiv_at, convert has_fderiv_at.comp x hl hf, ext, simp end theorem has_fderiv_at.comp_has_deriv_within_at (hl : has_fderiv_at l l' (f x)) (hf : has_deriv_within_at f f' s x) : has_deriv_within_at (l ∘ f) (l' (f')) s x := begin rw ← has_fderiv_within_at_univ at hl, exact has_fderiv_within_at.comp_has_deriv_within_at x hl hf subset_preimage_univ end lemma fderiv_within.comp_deriv_within {t : set F} (hl : differentiable_within_at 𝕜 l t (f x)) (hf : differentiable_within_at 𝕜 f s x) (hs : s ⊆ f ⁻¹' t) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (l ∘ f) s x = (fderiv_within 𝕜 l t (f x) : F → E) (deriv_within f s x) := begin apply has_deriv_within_at.deriv_within _ hxs, exact (hl.has_fderiv_within_at).comp_has_deriv_within_at x (hf.has_deriv_within_at) hs end lemma fderiv.comp_deriv (hl : differentiable_at 𝕜 l (f x)) (hf : differentiable_at 𝕜 f x) : deriv (l ∘ f) x = (fderiv 𝕜 l (f x) : F → E) (deriv f x) := begin apply has_deriv_at.deriv _, exact (hl.has_fderiv_at).comp_has_deriv_at x (hf.has_deriv_at) end end composition_vector section mul /-! ### Derivative of the multiplication of two scalar functions -/ variables {c d : 𝕜 → 𝕜} {c' d' : 𝕜} theorem has_deriv_within_at.mul (hc : has_deriv_within_at c c' s x) (hd : has_deriv_within_at d d' s x) : has_deriv_within_at (λ y, c y * d y) (c' * d x + c x * d') s x := begin convert hc.smul hd using 1, rw [smul_eq_mul, smul_eq_mul, add_comm] end theorem has_deriv_at.mul (hc : has_deriv_at c c' x) (hd : has_deriv_at d d' x) : has_deriv_at (λ y, c y * d y) (c' * d x + c x * d') x := begin rw [← has_deriv_within_at_univ] at *, exact hc.mul hd end theorem has_strict_deriv_at.mul (hc : has_strict_deriv_at c c' x) (hd : has_strict_deriv_at d d' x) : has_strict_deriv_at (λ y, c y * d y) (c' * d x + c x * d') x := begin convert hc.smul hd using 1, rw [smul_eq_mul, smul_eq_mul, add_comm] end lemma deriv_within_mul (hxs : unique_diff_within_at 𝕜 s x) (hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) : deriv_within (λ y, c y * d y) s x = deriv_within c s x * d x + c x * deriv_within d s x := (hc.has_deriv_within_at.mul hd.has_deriv_within_at).deriv_within hxs @[simp] lemma deriv_mul (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) : deriv (λ y, c y * d y) x = deriv c x * d x + c x * deriv d x := (hc.has_deriv_at.mul hd.has_deriv_at).deriv theorem has_deriv_within_at.mul_const (hc : has_deriv_within_at c c' s x) (d : 𝕜) : has_deriv_within_at (λ y, c y * d) (c' * d) s x := begin convert hc.mul (has_deriv_within_at_const x s d), rw [mul_zero, add_zero] end theorem has_deriv_at.mul_const (hc : has_deriv_at c c' x) (d : 𝕜) : has_deriv_at (λ y, c y * d) (c' * d) x := begin rw [← has_deriv_within_at_univ] at *, exact hc.mul_const d end lemma deriv_within_mul_const (hxs : unique_diff_within_at 𝕜 s x) (hc : differentiable_within_at 𝕜 c s x) (d : 𝕜) : deriv_within (λ y, c y * d) s x = deriv_within c s x * d := (hc.has_deriv_within_at.mul_const d).deriv_within hxs lemma deriv_mul_const (hc : differentiable_at 𝕜 c x) (d : 𝕜) : deriv (λ y, c y * d) x = deriv c x * d := (hc.has_deriv_at.mul_const d).deriv theorem has_deriv_within_at.const_mul (c : 𝕜) (hd : has_deriv_within_at d d' s x) : has_deriv_within_at (λ y, c * d y) (c * d') s x := begin convert (has_deriv_within_at_const x s c).mul hd, rw [zero_mul, zero_add] end theorem has_deriv_at.const_mul (c : 𝕜) (hd : has_deriv_at d d' x) : has_deriv_at (λ y, c * d y) (c * d') x := begin rw [← has_deriv_within_at_univ] at *, exact hd.const_mul c end lemma deriv_within_const_mul (hxs : unique_diff_within_at 𝕜 s x) (c : 𝕜) (hd : differentiable_within_at 𝕜 d s x) : deriv_within (λ y, c * d y) s x = c * deriv_within d s x := (hd.has_deriv_within_at.const_mul c).deriv_within hxs lemma deriv_const_mul (c : 𝕜) (hd : differentiable_at 𝕜 d x) : deriv (λ y, c * d y) x = c * deriv d x := (hd.has_deriv_at.const_mul c).deriv end mul section inverse /-! ### Derivative of `x ↦ x⁻¹` -/ theorem has_strict_deriv_at_inv (hx : x ≠ 0) : has_strict_deriv_at has_inv.inv (-(x^2)⁻¹) x := begin suffices : is_o (λ p : 𝕜 × 𝕜, (p.1 - p.2) * ((x * x)⁻¹ - (p.1 * p.2)⁻¹)) (λ (p : 𝕜 × 𝕜), (p.1 - p.2) * 1) (𝓝 (x, x)), { refine this.congr' _ (eventually_of_forall _ $ λ _, mul_one _), refine eventually.mono (mem_nhds_sets (is_open_prod is_open_ne is_open_ne) ⟨hx, hx⟩) _, rintro ⟨y, z⟩ ⟨hy, hz⟩, simp only [mem_set_of_eq] at hy hz, -- hy : y ≠ 0, hz : z ≠ 0 field_simp [hx, hy, hz], ring, }, refine (is_O_refl (λ p : 𝕜 × 𝕜, p.1 - p.2) _).mul_is_o ((is_o_one_iff _).2 _), rw [← sub_self (x * x)⁻¹], exact tendsto_const_nhds.sub ((continuous_mul.tendsto (x, x)).inv' $ mul_ne_zero hx hx) end theorem has_deriv_at_inv (x_ne_zero : x ≠ 0) : has_deriv_at (λy, y⁻¹) (-(x^2)⁻¹) x := (has_strict_deriv_at_inv x_ne_zero).has_deriv_at theorem has_deriv_within_at_inv (x_ne_zero : x ≠ 0) (s : set 𝕜) : has_deriv_within_at (λx, x⁻¹) (-(x^2)⁻¹) s x := (has_deriv_at_inv x_ne_zero).has_deriv_within_at lemma differentiable_at_inv (x_ne_zero : x ≠ 0) : differentiable_at 𝕜 (λx, x⁻¹) x := (has_deriv_at_inv x_ne_zero).differentiable_at lemma differentiable_within_at_inv (x_ne_zero : x ≠ 0) : differentiable_within_at 𝕜 (λx, x⁻¹) s x := (differentiable_at_inv x_ne_zero).differentiable_within_at lemma differentiable_on_inv : differentiable_on 𝕜 (λx:𝕜, x⁻¹) {x | x ≠ 0} := λx hx, differentiable_within_at_inv hx lemma deriv_inv (x_ne_zero : x ≠ 0) : deriv (λx, x⁻¹) x = -(x^2)⁻¹ := (has_deriv_at_inv x_ne_zero).deriv lemma deriv_within_inv (x_ne_zero : x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, x⁻¹) s x = -(x^2)⁻¹ := begin rw differentiable_at.deriv_within (differentiable_at_inv x_ne_zero) hxs, exact deriv_inv x_ne_zero end lemma has_fderiv_at_inv (x_ne_zero : x ≠ 0) : has_fderiv_at (λx, x⁻¹) (smul_right 1 (-(x^2)⁻¹) : 𝕜 →L[𝕜] 𝕜) x := has_deriv_at_inv x_ne_zero lemma has_fderiv_within_at_inv (x_ne_zero : x ≠ 0) : has_fderiv_within_at (λx, x⁻¹) (smul_right 1 (-(x^2)⁻¹) : 𝕜 →L[𝕜] 𝕜) s x := (has_fderiv_at_inv x_ne_zero).has_fderiv_within_at lemma fderiv_inv (x_ne_zero : x ≠ 0) : fderiv 𝕜 (λx, x⁻¹) x = smul_right 1 (-(x^2)⁻¹) := (has_fderiv_at_inv x_ne_zero).fderiv lemma fderiv_within_inv (x_ne_zero : x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 (λx, x⁻¹) s x = smul_right 1 (-(x^2)⁻¹) := begin rw differentiable_at.fderiv_within (differentiable_at_inv x_ne_zero) hxs, exact fderiv_inv x_ne_zero end variables {c : 𝕜 → 𝕜} {c' : 𝕜} lemma has_deriv_within_at.inv (hc : has_deriv_within_at c c' s x) (hx : c x ≠ 0) : has_deriv_within_at (λ y, (c y)⁻¹) (- c' / (c x)^2) s x := begin convert (has_deriv_at_inv hx).comp_has_deriv_within_at x hc, field_simp end lemma has_deriv_at.inv (hc : has_deriv_at c c' x) (hx : c x ≠ 0) : has_deriv_at (λ y, (c y)⁻¹) (- c' / (c x)^2) x := begin rw ← has_deriv_within_at_univ at *, exact hc.inv hx end lemma differentiable_within_at.inv (hc : differentiable_within_at 𝕜 c s x) (hx : c x ≠ 0) : differentiable_within_at 𝕜 (λx, (c x)⁻¹) s x := (hc.has_deriv_within_at.inv hx).differentiable_within_at @[simp] lemma differentiable_at.inv (hc : differentiable_at 𝕜 c x) (hx : c x ≠ 0) : differentiable_at 𝕜 (λx, (c x)⁻¹) x := (hc.has_deriv_at.inv hx).differentiable_at lemma differentiable_on.inv (hc : differentiable_on 𝕜 c s) (hx : ∀ x ∈ s, c x ≠ 0) : differentiable_on 𝕜 (λx, (c x)⁻¹) s := λx h, (hc x h).inv (hx x h) @[simp] lemma differentiable.inv (hc : differentiable 𝕜 c) (hx : ∀ x, c x ≠ 0) : differentiable 𝕜 (λx, (c x)⁻¹) := λx, (hc x).inv (hx x) lemma deriv_within_inv' (hc : differentiable_within_at 𝕜 c s x) (hx : c x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, (c x)⁻¹) s x = - (deriv_within c s x) / (c x)^2 := (hc.has_deriv_within_at.inv hx).deriv_within hxs @[simp] lemma deriv_inv' (hc : differentiable_at 𝕜 c x) (hx : c x ≠ 0) : deriv (λx, (c x)⁻¹) x = - (deriv c x) / (c x)^2 := (hc.has_deriv_at.inv hx).deriv end inverse section division /-! ### Derivative of `x ↦ c x / d x` -/ variables {c d : 𝕜 → 𝕜} {c' d' : 𝕜} lemma has_deriv_within_at.div (hc : has_deriv_within_at c c' s x) (hd : has_deriv_within_at d d' s x) (hx : d x ≠ 0) : has_deriv_within_at (λ y, c y / d y) ((c' * d x - c x * d') / (d x)^2) s x := begin have A : (d x)⁻¹ * (d x)⁻¹ * (c' * d x) = (d x)⁻¹ * c', by rw [← mul_assoc, mul_comm, ← mul_assoc, ← mul_assoc, mul_inv_cancel hx, one_mul], convert hc.mul ((has_deriv_at_inv hx).comp_has_deriv_within_at x hd), simp [div_eq_inv_mul, pow_two, mul_inv', mul_add, A, sub_eq_add_neg], ring end lemma has_deriv_at.div (hc : has_deriv_at c c' x) (hd : has_deriv_at d d' x) (hx : d x ≠ 0) : has_deriv_at (λ y, c y / d y) ((c' * d x - c x * d') / (d x)^2) x := begin rw ← has_deriv_within_at_univ at *, exact hc.div hd hx end lemma differentiable_within_at.div (hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) (hx : d x ≠ 0) : differentiable_within_at 𝕜 (λx, c x / d x) s x := ((hc.has_deriv_within_at).div (hd.has_deriv_within_at) hx).differentiable_within_at @[simp] lemma differentiable_at.div (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) (hx : d x ≠ 0) : differentiable_at 𝕜 (λx, c x / d x) x := ((hc.has_deriv_at).div (hd.has_deriv_at) hx).differentiable_at lemma differentiable_on.div (hc : differentiable_on 𝕜 c s) (hd : differentiable_on 𝕜 d s) (hx : ∀ x ∈ s, d x ≠ 0) : differentiable_on 𝕜 (λx, c x / d x) s := λx h, (hc x h).div (hd x h) (hx x h) @[simp] lemma differentiable.div (hc : differentiable 𝕜 c) (hd : differentiable 𝕜 d) (hx : ∀ x, d x ≠ 0) : differentiable 𝕜 (λx, c x / d x) := λx, (hc x).div (hd x) (hx x) lemma deriv_within_div (hc : differentiable_within_at 𝕜 c s x) (hd : differentiable_within_at 𝕜 d s x) (hx : d x ≠ 0) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, c x / d x) s x = ((deriv_within c s x) * d x - c x * (deriv_within d s x)) / (d x)^2 := ((hc.has_deriv_within_at).div (hd.has_deriv_within_at) hx).deriv_within hxs @[simp] lemma deriv_div (hc : differentiable_at 𝕜 c x) (hd : differentiable_at 𝕜 d x) (hx : d x ≠ 0) : deriv (λx, c x / d x) x = ((deriv c x) * d x - c x * (deriv d x)) / (d x)^2 := ((hc.has_deriv_at).div (hd.has_deriv_at) hx).deriv lemma differentiable_within_at.div_const (hc : differentiable_within_at 𝕜 c s x) {d : 𝕜} : differentiable_within_at 𝕜 (λx, c x / d) s x := by simp [div_eq_inv_mul, differentiable_within_at.const_mul, hc] @[simp] lemma differentiable_at.div_const (hc : differentiable_at 𝕜 c x) {d : 𝕜} : differentiable_at 𝕜 (λ x, c x / d) x := by simp [div_eq_inv_mul, hc] lemma differentiable_on.div_const (hc : differentiable_on 𝕜 c s) {d : 𝕜} : differentiable_on 𝕜 (λx, c x / d) s := by simp [div_eq_inv_mul, differentiable_on.const_mul, hc] @[simp] lemma differentiable.div_const (hc : differentiable 𝕜 c) {d : 𝕜} : differentiable 𝕜 (λx, c x / d) := by simp [div_eq_inv_mul, differentiable.const_mul, hc] lemma deriv_within_div_const (hc : differentiable_within_at 𝕜 c s x) {d : 𝕜} (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, c x / d) s x = (deriv_within c s x) / d := by simp [div_eq_inv_mul, deriv_within_const_mul, hc, hxs] @[simp] lemma deriv_div_const (hc : differentiable_at 𝕜 c x) {d : 𝕜} : deriv (λx, c x / d) x = (deriv c x) / d := by simp [div_eq_inv_mul, deriv_const_mul, hc] end division theorem has_strict_deriv_at.has_strict_fderiv_at_equiv {f : 𝕜 → 𝕜} {f' x : 𝕜} (hf : has_strict_deriv_at f f' x) (hf' : f' ≠ 0) : has_strict_fderiv_at f (continuous_linear_equiv.units_equiv_aut 𝕜 (units.mk0 f' hf') : 𝕜 →L[𝕜] 𝕜) x := hf theorem has_deriv_at.has_fderiv_at_equiv {f : 𝕜 → 𝕜} {f' x : 𝕜} (hf : has_deriv_at f f' x) (hf' : f' ≠ 0) : has_fderiv_at f (continuous_linear_equiv.units_equiv_aut 𝕜 (units.mk0 f' hf') : 𝕜 →L[𝕜] 𝕜) x := hf /-- If `f (g y) = y` for `y` in some neighborhood of `a`, `g` is continuous at `a`, and `f` has an invertible derivative `f'` at `g a` in the strict sense, then `g` has the derivative `f'⁻¹` at `a` in the strict sense. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem has_strict_deriv_at.of_local_left_inverse {f g : 𝕜 → 𝕜} {f' a : 𝕜} (hg : continuous_at g a) (hf : has_strict_deriv_at f f' (g a)) (hf' : f' ≠ 0) (hfg : ∀ᶠ y in 𝓝 a, f (g y) = y) : has_strict_deriv_at g f'⁻¹ a := (hf.has_strict_fderiv_at_equiv hf').of_local_left_inverse hg hfg /-- If `f (g y) = y` for `y` in some neighborhood of `a`, `g` is continuous at `a`, and `f` has an invertible derivative `f'` at `g a`, then `g` has the derivative `f'⁻¹` at `a`. This is one of the easy parts of the inverse function theorem: it assumes that we already have an inverse function. -/ theorem has_deriv_at.of_local_left_inverse {f g : 𝕜 → 𝕜} {f' a : 𝕜} (hg : continuous_at g a) (hf : has_deriv_at f f' (g a)) (hf' : f' ≠ 0) (hfg : ∀ᶠ y in 𝓝 a, f (g y) = y) : has_deriv_at g f'⁻¹ a := (hf.has_fderiv_at_equiv hf').of_local_left_inverse hg hfg end namespace polynomial /-! ### Derivative of a polynomial -/ variables {x : 𝕜} {s : set 𝕜} variable (p : polynomial 𝕜) /-- The derivative (in the analysis sense) of a polynomial `p` is given by `p.derivative`. -/ protected lemma has_strict_deriv_at (x : 𝕜) : has_strict_deriv_at (λx, p.eval x) (p.derivative.eval x) x := begin apply p.induction_on, { simp [has_strict_deriv_at_const] }, { assume p q hp hq, convert hp.add hq; simp }, { assume n a h, convert h.mul (has_strict_deriv_at_id x), { ext y, simp [pow_add, mul_assoc] }, { simp [pow_add], ring } } end /-- The derivative (in the analysis sense) of a polynomial `p` is given by `p.derivative`. -/ protected lemma has_deriv_at (x : 𝕜) : has_deriv_at (λx, p.eval x) (p.derivative.eval x) x := (p.has_strict_deriv_at x).has_deriv_at protected theorem has_deriv_within_at (x : 𝕜) (s : set 𝕜) : has_deriv_within_at (λx, p.eval x) (p.derivative.eval x) s x := (p.has_deriv_at x).has_deriv_within_at protected lemma differentiable_at : differentiable_at 𝕜 (λx, p.eval x) x := (p.has_deriv_at x).differentiable_at protected lemma differentiable_within_at : differentiable_within_at 𝕜 (λx, p.eval x) s x := p.differentiable_at.differentiable_within_at protected lemma differentiable : differentiable 𝕜 (λx, p.eval x) := λx, p.differentiable_at protected lemma differentiable_on : differentiable_on 𝕜 (λx, p.eval x) s := p.differentiable.differentiable_on @[simp] protected lemma deriv : deriv (λx, p.eval x) x = p.derivative.eval x := (p.has_deriv_at x).deriv protected lemma deriv_within (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, p.eval x) s x = p.derivative.eval x := begin rw differentiable_at.deriv_within p.differentiable_at hxs, exact p.deriv end protected lemma continuous : continuous (λx, p.eval x) := p.differentiable.continuous protected lemma continuous_on : continuous_on (λx, p.eval x) s := p.continuous.continuous_on protected lemma continuous_at : continuous_at (λx, p.eval x) x := p.continuous.continuous_at protected lemma continuous_within_at : continuous_within_at (λx, p.eval x) s x := p.continuous_at.continuous_within_at protected lemma has_fderiv_at (x : 𝕜) : has_fderiv_at (λx, p.eval x) (smul_right 1 (p.derivative.eval x) : 𝕜 →L[𝕜] 𝕜) x := by simpa [has_deriv_at_iff_has_fderiv_at] using p.has_deriv_at x protected lemma has_fderiv_within_at (x : 𝕜) : has_fderiv_within_at (λx, p.eval x) (smul_right 1 (p.derivative.eval x) : 𝕜 →L[𝕜] 𝕜) s x := (p.has_fderiv_at x).has_fderiv_within_at @[simp] protected lemma fderiv : fderiv 𝕜 (λx, p.eval x) x = smul_right 1 (p.derivative.eval x) := (p.has_fderiv_at x).fderiv protected lemma fderiv_within (hxs : unique_diff_within_at 𝕜 s x) : fderiv_within 𝕜 (λx, p.eval x) s x = smul_right 1 (p.derivative.eval x) := begin rw differentiable_at.fderiv_within p.differentiable_at hxs, exact p.fderiv end end polynomial section pow /-! ### Derivative of `x ↦ x^n` for `n : ℕ` -/ variables {x : 𝕜} {s : set 𝕜} {c : 𝕜 → 𝕜} {c' : 𝕜} variable {n : ℕ } lemma has_strict_deriv_at_pow (n : ℕ) (x : 𝕜) : has_strict_deriv_at (λx, x^n) ((n : 𝕜) * x^(n-1)) x := begin convert (polynomial.C (1 : 𝕜) * (polynomial.X)^n).has_strict_deriv_at x, { simp }, { rw [polynomial.derivative_monomial], simp } end lemma has_deriv_at_pow (n : ℕ) (x : 𝕜) : has_deriv_at (λx, x^n) ((n : 𝕜) * x^(n-1)) x := (has_strict_deriv_at_pow n x).has_deriv_at theorem has_deriv_within_at_pow (n : ℕ) (x : 𝕜) (s : set 𝕜) : has_deriv_within_at (λx, x^n) ((n : 𝕜) * x^(n-1)) s x := (has_deriv_at_pow n x).has_deriv_within_at lemma differentiable_at_pow : differentiable_at 𝕜 (λx, x^n) x := (has_deriv_at_pow n x).differentiable_at lemma differentiable_within_at_pow : differentiable_within_at 𝕜 (λx, x^n) s x := differentiable_at_pow.differentiable_within_at lemma differentiable_pow : differentiable 𝕜 (λx:𝕜, x^n) := λx, differentiable_at_pow lemma differentiable_on_pow : differentiable_on 𝕜 (λx, x^n) s := differentiable_pow.differentiable_on lemma deriv_pow : deriv (λx, x^n) x = (n : 𝕜) * x^(n-1) := (has_deriv_at_pow n x).deriv @[simp] lemma deriv_pow' : deriv (λx, x^n) = λ x, (n : 𝕜) * x^(n-1) := funext $ λ x, deriv_pow lemma deriv_within_pow (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, x^n) s x = (n : 𝕜) * x^(n-1) := (has_deriv_within_at_pow n x s).deriv_within hxs lemma iter_deriv_pow' {k : ℕ} : deriv^[k] (λx:𝕜, x^n) = λ x, (∏ i in finset.range k, (n - i) : ℕ) * x^(n-k) := begin induction k with k ihk, { simp only [one_mul, finset.prod_range_zero, function.iterate_zero_apply, nat.sub_zero, nat.cast_one] }, { simp only [function.iterate_succ_apply', ihk, finset.prod_range_succ], ext x, rw [((has_deriv_at_pow (n - k) x).const_mul _).deriv, nat.cast_mul, mul_left_comm, mul_assoc, nat.succ_eq_add_one, nat.sub_sub] } end lemma iter_deriv_pow {k : ℕ} : deriv^[k] (λx:𝕜, x^n) x = (∏ i in finset.range k, (n - i) : ℕ) * x^(n-k) := congr_fun iter_deriv_pow' x lemma has_deriv_within_at.pow (hc : has_deriv_within_at c c' s x) : has_deriv_within_at (λ y, (c y)^n) ((n : 𝕜) * (c x)^(n-1) * c') s x := (has_deriv_at_pow n (c x)).comp_has_deriv_within_at x hc lemma has_deriv_at.pow (hc : has_deriv_at c c' x) : has_deriv_at (λ y, (c y)^n) ((n : 𝕜) * (c x)^(n-1) * c') x := by { rw ← has_deriv_within_at_univ at *, exact hc.pow } lemma differentiable_within_at.pow (hc : differentiable_within_at 𝕜 c s x) : differentiable_within_at 𝕜 (λx, (c x)^n) s x := hc.has_deriv_within_at.pow.differentiable_within_at @[simp] lemma differentiable_at.pow (hc : differentiable_at 𝕜 c x) : differentiable_at 𝕜 (λx, (c x)^n) x := hc.has_deriv_at.pow.differentiable_at lemma differentiable_on.pow (hc : differentiable_on 𝕜 c s) : differentiable_on 𝕜 (λx, (c x)^n) s := λx h, (hc x h).pow @[simp] lemma differentiable.pow (hc : differentiable 𝕜 c) : differentiable 𝕜 (λx, (c x)^n) := λx, (hc x).pow lemma deriv_within_pow' (hc : differentiable_within_at 𝕜 c s x) (hxs : unique_diff_within_at 𝕜 s x) : deriv_within (λx, (c x)^n) s x = (n : 𝕜) * (c x)^(n-1) * (deriv_within c s x) := hc.has_deriv_within_at.pow.deriv_within hxs @[simp] lemma deriv_pow'' (hc : differentiable_at 𝕜 c x) : deriv (λx, (c x)^n) x = (n : 𝕜) * (c x)^(n-1) * (deriv c x) := hc.has_deriv_at.pow.deriv end pow section fpow /-! ### Derivative of `x ↦ x^m` for `m : ℤ` -/ variables {x : 𝕜} {s : set 𝕜} variable {m : ℤ} lemma has_strict_deriv_at_fpow (m : ℤ) (hx : x ≠ 0) : has_strict_deriv_at (λx, x^m) ((m : 𝕜) * x^(m-1)) x := begin have : ∀ m : ℤ, 0 < m → has_strict_deriv_at (λx, x^m) ((m:𝕜) * x^(m-1)) x, { assume m hm, lift m to ℕ using (le_of_lt hm), simp only [fpow_of_nat, int.cast_coe_nat], convert has_strict_deriv_at_pow _ _ using 2, rw [← int.coe_nat_one, ← int.coe_nat_sub, fpow_coe_nat], norm_cast at hm, exact nat.succ_le_of_lt hm }, rcases lt_trichotomy m 0 with hm|hm|hm, { have := (has_strict_deriv_at_inv _).scomp _ (this (-m) (neg_pos.2 hm)); [skip, exact fpow_ne_zero_of_ne_zero hx _], simp only [(∘), fpow_neg, one_div_eq_inv, inv_inv', smul_eq_mul] at this, convert this using 1, rw [pow_two, mul_inv', inv_inv', int.cast_neg, ← neg_mul_eq_neg_mul, neg_mul_neg, ← fpow_add hx, mul_assoc, ← fpow_add hx], congr, abel }, { simp only [hm, fpow_zero, int.cast_zero, zero_mul, has_strict_deriv_at_const] }, { exact this m hm } end lemma has_deriv_at_fpow (m : ℤ) (hx : x ≠ 0) : has_deriv_at (λx, x^m) ((m : 𝕜) * x^(m-1)) x := (has_strict_deriv_at_fpow m hx).has_deriv_at theorem has_deriv_within_at_fpow (m : ℤ) (hx : x ≠ 0) (s : set 𝕜) : has_deriv_within_at (λx, x^m) ((m : 𝕜) * x^(m-1)) s x := (has_deriv_at_fpow m hx).has_deriv_within_at lemma differentiable_at_fpow (hx : x ≠ 0) : differentiable_at 𝕜 (λx, x^m) x := (has_deriv_at_fpow m hx).differentiable_at lemma differentiable_within_at_fpow (hx : x ≠ 0) : differentiable_within_at 𝕜 (λx, x^m) s x := (differentiable_at_fpow hx).differentiable_within_at lemma differentiable_on_fpow (hs : (0:𝕜) ∉ s) : differentiable_on 𝕜 (λx, x^m) s := λ x hxs, differentiable_within_at_fpow (λ hx, hs $ hx ▸ hxs) -- TODO : this is true at `x=0` as well lemma deriv_fpow (hx : x ≠ 0) : deriv (λx, x^m) x = (m : 𝕜) * x^(m-1) := (has_deriv_at_fpow m hx).deriv lemma deriv_within_fpow (hxs : unique_diff_within_at 𝕜 s x) (hx : x ≠ 0) : deriv_within (λx, x^m) s x = (m : 𝕜) * x^(m-1) := (has_deriv_within_at_fpow m hx s).deriv_within hxs lemma iter_deriv_fpow {k : ℕ} (hx : x ≠ 0) : deriv^[k] (λx:𝕜, x^m) x = (∏ i in finset.range k, (m - i) : ℤ) * x^(m-k) := begin induction k with k ihk generalizing x hx, { simp only [one_mul, finset.prod_range_zero, function.iterate_zero_apply, int.coe_nat_zero, sub_zero, int.cast_one] }, { rw [function.iterate_succ', finset.prod_range_succ, int.cast_mul, mul_assoc, mul_left_comm, int.coe_nat_succ, ← sub_sub, ← ((has_deriv_at_fpow _ hx).const_mul _).deriv], exact deriv_congr_of_mem_nhds (eventually.mono (mem_nhds_sets is_open_ne hx) @ihk) } end end fpow /-! ### Upper estimates on liminf and limsup -/ section real variables {f : ℝ → ℝ} {f' : ℝ} {s : set ℝ} {x : ℝ} {r : ℝ} lemma has_deriv_within_at.limsup_slope_le (hf : has_deriv_within_at f f' s x) (hr : f' < r) : ∀ᶠ z in nhds_within x (s \ {x}), (z - x)⁻¹ * (f z - f x) < r := has_deriv_within_at_iff_tendsto_slope.1 hf (mem_nhds_sets is_open_Iio hr) lemma has_deriv_within_at.limsup_slope_le' (hf : has_deriv_within_at f f' s x) (hs : x ∉ s) (hr : f' < r) : ∀ᶠ z in nhds_within x s, (z - x)⁻¹ * (f z - f x) < r := (has_deriv_within_at_iff_tendsto_slope' hs).1 hf (mem_nhds_sets is_open_Iio hr) lemma has_deriv_within_at.liminf_right_slope_le (hf : has_deriv_within_at f f' (Ioi x) x) (hr : f' < r) : ∃ᶠ z in nhds_within x (Ioi x), (z - x)⁻¹ * (f z - f x) < r := (hf.limsup_slope_le' (lt_irrefl x) hr).frequently (nhds_within_Ioi_self_ne_bot x) end real section real_space open metric variables {E : Type u} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {f' : E} {s : set ℝ} {x r : ℝ} /-- If `f` has derivative `f'` within `s` at `x`, then for any `r > ∥f'∥` the ratio `∥f z - f x∥ / ∥z - x∥` is less than `r` in some neighborhood of `x` within `s`. In other words, the limit superior of this ratio as `z` tends to `x` along `s` is less than or equal to `∥f'∥`. -/ lemma has_deriv_within_at.limsup_norm_slope_le (hf : has_deriv_within_at f f' s x) (hr : ∥f'∥ < r) : ∀ᶠ z in nhds_within x s, ∥z - x∥⁻¹ * ∥f z - f x∥ < r := begin have hr₀ : 0 < r, from lt_of_le_of_lt (norm_nonneg f') hr, have A : ∀ᶠ z in nhds_within x (s \ {x}), ∥(z - x)⁻¹ • (f z - f x)∥ ∈ Iio r, from (has_deriv_within_at_iff_tendsto_slope.1 hf).norm (mem_nhds_sets is_open_Iio hr), have B : ∀ᶠ z in nhds_within x {x}, ∥(z - x)⁻¹ • (f z - f x)∥ ∈ Iio r, from mem_sets_of_superset self_mem_nhds_within (singleton_subset_iff.2 $ by simp [hr₀]), have C := mem_sup_sets.2 ⟨A, B⟩, rw [← nhds_within_union, diff_union_self, nhds_within_union, mem_sup_sets] at C, filter_upwards [C.1], simp only [mem_set_of_eq, norm_smul, mem_Iio, normed_field.norm_inv], exact λ _, id end /-- If `f` has derivative `f'` within `s` at `x`, then for any `r > ∥f'∥` the ratio `(∥f z∥ - ∥f x∥) / ∥z - x∥` is less than `r` in some neighborhood of `x` within `s`. In other words, the limit superior of this ratio as `z` tends to `x` along `s` is less than or equal to `∥f'∥`. This lemma is a weaker version of `has_deriv_within_at.limsup_norm_slope_le` where `∥f z∥ - ∥f x∥` is replaced by `∥f z - f x∥`. -/ lemma has_deriv_within_at.limsup_slope_norm_le (hf : has_deriv_within_at f f' s x) (hr : ∥f'∥ < r) : ∀ᶠ z in nhds_within x s, ∥z - x∥⁻¹ * (∥f z∥ - ∥f x∥) < r := begin apply (hf.limsup_norm_slope_le hr).mono, assume z hz, refine lt_of_le_of_lt (mul_le_mul_of_nonneg_left (norm_sub_norm_le _ _) _) hz, exact inv_nonneg.2 (norm_nonneg _) end /-- If `f` has derivative `f'` within `(x, +∞)` at `x`, then for any `r > ∥f'∥` the ratio `∥f z - f x∥ / ∥z - x∥` is frequently less than `r` as `z → x+0`. In other words, the limit inferior of this ratio as `z` tends to `x+0` is less than or equal to `∥f'∥`. See also `has_deriv_within_at.limsup_norm_slope_le` for a stronger version using limit superior and any set `s`. -/ lemma has_deriv_within_at.liminf_right_norm_slope_le (hf : has_deriv_within_at f f' (Ioi x) x) (hr : ∥f'∥ < r) : ∃ᶠ z in nhds_within x (Ioi x), ∥z - x∥⁻¹ * ∥f z - f x∥ < r := (hf.limsup_norm_slope_le hr).frequently (nhds_within_Ioi_self_ne_bot x) /-- If `f` has derivative `f'` within `(x, +∞)` at `x`, then for any `r > ∥f'∥` the ratio `(∥f z∥ - ∥f x∥) / (z - x)` is frequently less than `r` as `z → x+0`. In other words, the limit inferior of this ratio as `z` tends to `x+0` is less than or equal to `∥f'∥`. See also * `has_deriv_within_at.limsup_norm_slope_le` for a stronger version using limit superior and any set `s`; * `has_deriv_within_at.liminf_right_norm_slope_le` for a stronger version using `∥f z - f x∥` instead of `∥f z∥ - ∥f x∥`. -/ lemma has_deriv_within_at.liminf_right_slope_norm_le (hf : has_deriv_within_at f f' (Ioi x) x) (hr : ∥f'∥ < r) : ∃ᶠ z in nhds_within x (Ioi x), (z - x)⁻¹ * (∥f z∥ - ∥f x∥) < r := begin have := (hf.limsup_slope_norm_le hr).frequently (nhds_within_Ioi_self_ne_bot x), refine this.mp (eventually.mono self_mem_nhds_within _), assume z hxz hz, rwa [real.norm_eq_abs, abs_of_pos (sub_pos_of_lt hxz)] at hz end end real_space
b20ea0accbaa7437092f168c0f4ba59282ac9d1f
271e26e338b0c14544a889c31c30b39c989f2e0f
/stage0/src/Init/Lean/Elab/Alias.lean
930f682b3832fa82fd0a8e3349ada4aacba5b6be
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,236
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 -/ prelude import Init.Lean.Environment namespace Lean /- We use aliases to implement the `export <id> (<id>+)` command. -/ abbrev AliasState := SMap Name (List Name) abbrev AliasEntry := Name × Name def addAliasEntry (s : AliasState) (e : AliasEntry) : AliasState := match s.find? e.1 with | none => s.insert e.1 [e.2] | some es => if es.elem e.2 then s else s.insert e.1 (e.2 :: es) def mkAliasExtension : IO (SimplePersistentEnvExtension AliasEntry AliasState) := registerSimplePersistentEnvExtension { name := `aliasesExt, addEntryFn := addAliasEntry, addImportedFn := fun es => (mkStateFromImportedEntries addAliasEntry {} es).switch } @[init mkAliasExtension] constant aliasExtension : SimplePersistentEnvExtension AliasEntry AliasState := arbitrary _ /- Add alias `a` for `e` -/ def addAlias (env : Environment) (a : Name) (e : Name) : Environment := aliasExtension.addEntry env (a, e) def getAliases (env : Environment) (a : Name) : List Name := match (aliasExtension.getState env).find? a with | none => [] | some es => es end Lean
894c8f9fcb6e646a025b7f20ab1d7fa0bd165706
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/monoid_algebra/grading.lean
d0acc07e315b17433bf53094b1649b41c11e1e1d
[ "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
8,771
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 linear_algebra.finsupp import algebra.monoid_algebra.support import algebra.direct_sum.internal import ring_theory.graded_algebra.basic /-! # Internal grading of an `add_monoid_algebra` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we show that an `add_monoid_algebra` has an internal direct sum structure. ## Main results * `add_monoid_algebra.grade_by R f i`: the `i`th grade of an `add_monoid_algebra R M` given by the degree function `f`. * `add_monoid_algebra.grade R i`: the `i`th grade of an `add_monoid_algebra R M` when the degree function is the identity. * `add_monoid_algebra.grade_by.graded_algebra`: `add_monoid_algebra` is an algebra graded by `add_monoid_algebra.grade_by`. * `add_monoid_algebra.grade.graded_algebra`: `add_monoid_algebra` is an algebra graded by `add_monoid_algebra.grade`. * `add_monoid_algebra.grade_by.is_internal`: propositionally, the statement that `add_monoid_algebra.grade_by` defines an internal graded structure. * `add_monoid_algebra.grade.is_internal`: propositionally, the statement that `add_monoid_algebra.grade` defines an internal graded structure when the degree function is the identity. -/ noncomputable theory namespace add_monoid_algebra variables {M : Type*} {ι : Type*} {R : Type*} [decidable_eq M] section variables (R) [comm_semiring R] /-- The submodule corresponding to each grade given by the degree function `f`. -/ abbreviation grade_by (f : M → ι) (i : ι) : submodule R (add_monoid_algebra R M) := { carrier := {a | ∀ m, m ∈ a.support → f m = i }, zero_mem' := set.empty_subset _, add_mem' := λ a b ha hb m h, or.rec_on (finset.mem_union.mp (finsupp.support_add h)) (ha m) (hb m), smul_mem' := λ a m h, set.subset.trans finsupp.support_smul h } /-- The submodule corresponding to each grade. -/ abbreviation grade (m : M) : submodule R (add_monoid_algebra R M) := grade_by R id m lemma grade_by_id : grade_by R (id : M → M) = grade R := by refl lemma mem_grade_by_iff (f : M → ι) (i : ι) (a : add_monoid_algebra R M) : a ∈ grade_by R f i ↔ (a.support : set M) ⊆ f ⁻¹' {i} := by refl lemma mem_grade_iff (m : M) (a : add_monoid_algebra R M) : a ∈ grade R m ↔ a.support ⊆ {m} := begin rw [← finset.coe_subset, finset.coe_singleton], refl end lemma mem_grade_iff' (m : M) (a : add_monoid_algebra R M) : a ∈ grade R m ↔ a ∈ ((finsupp.lsingle m : R →ₗ[R] (M →₀ R)).range : submodule R (add_monoid_algebra R M)) := begin rw [mem_grade_iff, finsupp.support_subset_singleton'], apply exists_congr, intros r, split; exact eq.symm end lemma grade_eq_lsingle_range (m : M) : grade R m = (finsupp.lsingle m : R →ₗ[R] (M →₀ R)).range := submodule.ext (mem_grade_iff' R m) lemma single_mem_grade_by {R} [comm_semiring R] (f : M → ι) (m : M) (r : R) : finsupp.single m r ∈ grade_by R f (f m) := begin intros x hx, rw finset.mem_singleton.mp (finsupp.support_single_subset hx), end lemma single_mem_grade {R} [comm_semiring R] (i : M) (r : R) : finsupp.single i r ∈ grade R i := single_mem_grade_by _ _ _ end open_locale direct_sum instance grade_by.graded_monoid [add_monoid M] [add_monoid ι] [comm_semiring R] (f : M →+ ι) : set_like.graded_monoid (grade_by R f : ι → submodule R (add_monoid_algebra R M)) := { one_mem := λ m h, begin rw one_def at h, by_cases H : (1 : R) = (0 : R), { rw [H , finsupp.single_zero] at h, exfalso, exact h }, { rw [finsupp.support_single_ne_zero _ H, finset.mem_singleton] at h, rw [h, add_monoid_hom.map_zero] } end, mul_mem := λ i j a b ha hb c hc, begin set h := support_mul a b hc, simp only [finset.mem_bUnion] at h, rcases h with ⟨ma, ⟨hma, ⟨mb, ⟨hmb, hmc⟩⟩⟩⟩, rw [← ha ma hma, ← hb mb hmb, finset.mem_singleton.mp hmc], apply add_monoid_hom.map_add end } instance grade.graded_monoid [add_monoid M] [comm_semiring R] : set_like.graded_monoid (grade R : M → submodule R (add_monoid_algebra R M)) := by apply grade_by.graded_monoid (add_monoid_hom.id _) variables {R} [add_monoid M] [decidable_eq ι] [add_monoid ι] [comm_semiring R] (f : M →+ ι) /-- Auxiliary definition; the canonical grade decomposition, used to provide `direct_sum.decompose`. -/ def decompose_aux : add_monoid_algebra R M →ₐ[R] ⨁ i : ι, grade_by R f i := add_monoid_algebra.lift R M _ { to_fun := λ m, direct_sum.of (λ i : ι, grade_by R f i) (f m.to_add) ⟨finsupp.single m.to_add 1, single_mem_grade_by _ _ _⟩, map_one' := direct_sum.of_eq_of_graded_monoid_eq (by congr' 2; try {ext}; simp only [submodule.mem_to_add_submonoid, to_add_one, add_monoid_hom.map_zero]), map_mul' := λ i j, begin symmetry, convert direct_sum.of_mul_of _ _, apply direct_sum.of_eq_of_graded_monoid_eq, congr' 2, { rw [to_add_mul, add_monoid_hom.map_add] }, { ext, simp only [submodule.mem_to_add_submonoid, add_monoid_hom.map_add, to_add_mul] }, { exact eq.trans (by rw [one_mul, to_add_mul]) single_mul_single.symm } end } lemma decompose_aux_single (m : M) (r : R) : decompose_aux f (finsupp.single m r) = direct_sum.of (λ i : ι, grade_by R f i) (f m) ⟨finsupp.single m r, single_mem_grade_by _ _ _⟩ := begin refine (lift_single _ _ _).trans _, refine (direct_sum.of_smul _ _ _ _).symm.trans _, apply direct_sum.of_eq_of_graded_monoid_eq, refine sigma.subtype_ext rfl _, refine (finsupp.smul_single' _ _ _).trans _, rw mul_one, refl, end lemma decompose_aux_coe {i : ι} (x : grade_by R f i) : decompose_aux f ↑x = direct_sum.of (λ i, grade_by R f i) i x := begin obtain ⟨x, hx⟩ := x, revert hx, refine finsupp.induction x _ _, { intros hx, symmetry, exact add_monoid_hom.map_zero _ }, { intros m b y hmy hb ih hmby, have : disjoint (finsupp.single m b).support y.support, { simpa only [finsupp.support_single_ne_zero _ hb, finset.disjoint_singleton_left] }, rw [mem_grade_by_iff, finsupp.support_add_eq this, finset.coe_union, set.union_subset_iff] at hmby, cases hmby with h1 h2, have : f m = i, { rwa [finsupp.support_single_ne_zero _ hb, finset.coe_singleton, set.singleton_subset_iff] at h1 }, subst this, simp only [alg_hom.map_add, submodule.coe_mk, decompose_aux_single f m], let ih' := ih h2, dsimp at ih', rw [ih', ← add_monoid_hom.map_add], apply direct_sum.of_eq_of_graded_monoid_eq, congr' 2 } end instance grade_by.graded_algebra : graded_algebra (grade_by R f) := graded_algebra.of_alg_hom _ (decompose_aux f) (begin ext : 2, simp only [alg_hom.coe_to_monoid_hom, function.comp_app, alg_hom.coe_comp, function.comp.left_id, alg_hom.coe_id, add_monoid_algebra.of_apply, monoid_hom.coe_comp], rw [decompose_aux_single, direct_sum.coe_alg_hom_of, subtype.coe_mk], end) (λ i x, by rw [decompose_aux_coe f x]) -- Lean can't find this later without us repeating it instance grade_by.decomposition : direct_sum.decomposition (grade_by R f) := by apply_instance @[simp] lemma decompose_aux_eq_decompose : ⇑(decompose_aux f : add_monoid_algebra R M →ₐ[R] ⨁ i : ι, grade_by R f i) = (direct_sum.decompose (grade_by R f)) := rfl @[simp] lemma grades_by.decompose_single (m : M) (r : R) : direct_sum.decompose (grade_by R f) (finsupp.single m r : add_monoid_algebra R M) = direct_sum.of (λ i : ι, grade_by R f i) (f m) ⟨finsupp.single m r, single_mem_grade_by _ _ _⟩ := decompose_aux_single _ _ _ instance grade.graded_algebra : graded_algebra (grade R : ι → submodule _ _) := add_monoid_algebra.grade_by.graded_algebra (add_monoid_hom.id _) -- Lean can't find this later without us repeating it instance grade.decomposition : direct_sum.decomposition (grade R : ι → submodule _ _) := by apply_instance @[simp] lemma grade.decompose_single (i : ι) (r : R) : direct_sum.decompose (grade R : ι → submodule _ _) (finsupp.single i r : add_monoid_algebra _ _) = direct_sum.of (λ i : ι, grade R i) i ⟨finsupp.single i r, single_mem_grade _ _⟩ := decompose_aux_single _ _ _ /-- `add_monoid_algebra.gradesby` describe an internally graded algebra -/ lemma grade_by.is_internal : direct_sum.is_internal (grade_by R f) := direct_sum.decomposition.is_internal _ /-- `add_monoid_algebra.grades` describe an internally graded algebra -/ lemma grade.is_internal : direct_sum.is_internal (grade R : ι → submodule R _) := direct_sum.decomposition.is_internal _ end add_monoid_algebra
e9be0ff7546bb2292097d15929b5f254120a78ac
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/adjunction/fully_faithful.lean
c972a5edba2970ec577b4e0b4ddbdd22909a63d8
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
4,340
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.adjunction.basic import category_theory.conj import category_theory.yoneda open category_theory namespace category_theory universes v₁ v₂ u₁ u₂ open category open opposite variables {C : Type u₁} [category.{v₁} C] variables {D : Type u₂} [category.{v₂} D] variables {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) /-- If the left adjoint is fully faithful, then the unit is an isomorphism. See * Lemma 4.5.13 from [Riehl][riehl2017] * https://math.stackexchange.com/a/2727177 * https://stacks.math.columbia.edu/tag/07RB (we only prove the forward direction!) -/ instance unit_is_iso_of_L_fully_faithful [full L] [faithful L] : is_iso (adjunction.unit h) := @nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ (adjunction.unit h) $ λ X, @yoneda.is_iso _ _ _ _ ((adjunction.unit h).app X) { inv := { app := λ Y f, L.preimage ((h.hom_equiv (unop Y) (L.obj X)).symm f) }, inv_hom_id' := begin ext, dsimp, simp only [adjunction.hom_equiv_counit, preimage_comp, preimage_map, category.assoc], rw ←h.unit_naturality, simp, end, hom_inv_id' := begin ext, dsimp, apply L.map_injective, simp, end }. /-- If the right adjoint is fully faithful, then the counit is an isomorphism. See https://stacks.math.columbia.edu/tag/07RB (we only prove the forward direction!) -/ instance counit_is_iso_of_R_fully_faithful [full R] [faithful R] : is_iso (adjunction.counit h) := @nat_iso.is_iso_of_is_iso_app _ _ _ _ _ _ (adjunction.counit h) $ λ X, @is_iso_of_op _ _ _ _ _ $ @coyoneda.is_iso _ _ _ _ ((adjunction.counit h).app X).op { inv := { app := λ Y f, R.preimage ((h.hom_equiv (R.obj X) Y) f) }, inv_hom_id' := begin ext, dsimp, simp only [adjunction.hom_equiv_unit, preimage_comp, preimage_map], rw ←h.counit_naturality, simp, end, hom_inv_id' := begin ext, dsimp, apply R.map_injective, simp, end } -- TODO also prove the converses? -- def L_full_of_unit_is_iso [is_iso (adjunction.unit h)] : full L := sorry -- def L_faithful_of_unit_is_iso [is_iso (adjunction.unit h)] : faithful L := sorry -- def R_full_of_counit_is_iso [is_iso (adjunction.counit h)] : full R := sorry -- def R_faithful_of_counit_is_iso [is_iso (adjunction.counit h)] : faithful R := sorry -- TODO also do the statements from Riehl 4.5.13 for full and faithful separately? universes v₃ v₄ u₃ u₄ variables {C' : Type u₃} [category.{v₃} C'] variables {D' : Type u₄} [category.{v₄} D'] -- TODO: This needs some lemmas describing the produced adjunction, probably in terms of `adj`, -- `iC` and `iD`. /-- If `C` is a full subcategory of `C'` and `D` is a full subcategory of `D'`, then we can restrict an adjunction `L' ⊣ R'` where `L' : C' ⥤ D'` and `R' : D' ⥤ C'` to `C` and `D`. The construction here is slightly more general, in that `C` is required only to have a full and faithful "inclusion" functor `iC : C ⥤ C'` (and similarly `iD : D ⥤ D'`) which commute (up to natural isomorphism) with the proposed restrictions. -/ def adjunction.restrict_fully_faithful (iC : C ⥤ C') (iD : D ⥤ D') {L' : C' ⥤ D'} {R' : D' ⥤ C'} (adj : L' ⊣ R') {L : C ⥤ D} {R : D ⥤ C} (comm1 : iC ⋙ L' ≅ L ⋙ iD) (comm2 : iD ⋙ R' ≅ R ⋙ iC) [full iC] [faithful iC] [full iD] [faithful iD] : L ⊣ R := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, calc (L.obj X ⟶ Y) ≃ (iD.obj (L.obj X) ⟶ iD.obj Y) : equiv_of_fully_faithful iD ... ≃ (L'.obj (iC.obj X) ⟶ iD.obj Y) : iso.hom_congr (comm1.symm.app X) (iso.refl _) ... ≃ (iC.obj X ⟶ R'.obj (iD.obj Y)) : adj.hom_equiv _ _ ... ≃ (iC.obj X ⟶ iC.obj (R.obj Y)) : iso.hom_congr (iso.refl _) (comm2.app Y) ... ≃ (X ⟶ R.obj Y) : (equiv_of_fully_faithful iC).symm, hom_equiv_naturality_left_symm' := λ X' X Y f g, begin apply iD.map_injective, simpa using (comm1.inv.naturality_assoc f _).symm, end, hom_equiv_naturality_right' := λ X Y' Y f g, begin apply iC.map_injective, suffices : R'.map (iD.map g) ≫ comm2.hom.app Y = comm2.hom.app Y' ≫ iC.map (R.map g), simp [this], apply comm2.hom.naturality g, end } end category_theory
f60f29dea3223f6bd36c423483a2fdbb07e9ef80
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Compiler/Decl.lean
87d186d61bfd74732184cb092958493ac1c9a210
[ "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
4,374
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Transform import Lean.Meta.Check import Lean.Compiler.LCNF import Lean.Compiler.Check import Lean.Compiler.CompilerM import Lean.Compiler.JoinPoints namespace Lean.Compiler /-- Declaration being processed by the Lean to Lean compiler passes. -/ structure Decl where /-- The name of the declaration from the `Environment` it came from -/ name : Name /-- The type of the declaration. Note that this is an erased LCNF type instead of the fully dependent one that might have been the original type of the declaration in the `Environment`. -/ type : Expr /-- The value of the declaration, usually changes as it progresses through compiler passes. -/ value : Expr /-- Inline constants tagged with the `[macroInline]` attribute occurring in `e`. -/ def macroInline (e : Expr) : CoreM Expr := Core.transform e fun e => do let .const declName us := e.getAppFn | return .visit e unless hasMacroInlineAttribute (← getEnv) declName do return .visit e let val ← Core.instantiateValueLevelParams (← getConstInfo declName) us return .visit <| val.beta e.getAppArgs /-- Replace nested occurrences of `unsafeRec` names with the safe ones. -/ private def replaceUnsafeRecNames (value : Expr) : CoreM Expr := Core.transform value fun e => match e with | .const declName us => if let some safeDeclName := isUnsafeRecName? declName then return .done (.const safeDeclName us) else return .done e | _ => return .visit e /-- Convert the given declaration from the Lean environment into `Decl`. The steps for this are roughly: - partially erasing type information of the declaration - eta-expanding the declaration value. - if the declaration has an unsafe-rec version, use it. - expand declarations tagged with the `[macroInline]` attribute - turn the resulting term into LCNF -/ def toDecl (declName : Name) : CoreM Decl := do let env ← getEnv let some info := env.find? (mkUnsafeRecName declName) <|> env.find? declName | throwError "declaration `{declName}` not found" let some value := info.value? | throwError "declaration `{declName}` does not have a value" Meta.MetaM.run' do let type ← toLCNFType info.type let value ← Meta.lambdaTelescope value fun xs body => do Meta.mkLambdaFVars xs (← Meta.etaExpand body) let value ← replaceUnsafeRecNames value let value ← macroInline value let value ← toLCNF value return { name := declName, type, value } /-- Join points are let bound variables with an _jp name prefix. They always need to satisfy two conditions: 1. Be fully applied 2. Always be called in tail position in order to allow the optimizer to turn them into efficient machine code. This function ensures that inside the given declaration both of these conditions are satisfied and throws an exception otherwise. -/ def Decl.checkJoinPoints (decl : Decl) : CompilerM Unit := JoinPointChecker.checkJoinPoints decl.value /-- Check whether a declaration still fulfills all the invariants that we put on them, if it does not throw an exception. This is meant as a sanity check for the code generator itself, not to find errors in the user code. These invariants are: - The ones enforced by `Compiler.check` - The stored and the inferred LCNF type of the declaration are compatible according to `compatibleTypes` -/ def Decl.check (decl : Decl) (cfg : Check.Config := {}): CoreM Unit := do Compiler.check decl.value cfg { lctx := {} } checkJoinPoints decl |>.run' {} let valueType ← InferType.inferType decl.value { lctx := {} } unless compatibleTypes decl.type valueType do throwError "declaration type mismatch at `{decl.name}`, value has type{indentExpr valueType}\nbut is expected to have type{indentExpr decl.type}" /-- Apply `f` to declaration `value`. -/ def Decl.mapValue (decl : Decl) (f : Expr → CompilerM Expr) : CoreM Decl := do return { decl with value := (← f decl.value |>.run' { nextIdx := (← getMaxLetVarIdx decl.value) + 1 }) } def Decl.toString (decl : Decl) : CoreM String := do Meta.MetaM.run' do return s!"{decl.name} : {← Meta.ppExpr decl.type} :=\n{← Meta.ppExpr decl.value}" end Lean.Compiler
ef6e9e0425af18c55a3009404d6a897c53d80979
e39f04f6ff425fe3b3f5e26a8998b817d1dba80f
/data/list/sigma.lean
d8afc7da2c2a675231c7f675594903c056ab22ca
[ "Apache-2.0" ]
permissive
kristychoi/mathlib
c504b5e8f84e272ea1d8966693c42de7523bf0ec
257fd84fe98927ff4a5ffe044f68c4e9d235cc75
refs/heads/master
1,586,520,722,896
1,544,030,145,000
1,544,031,933,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,112
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Sean Leather Functions on lists of sigma types. -/ import data.list.perm universes u v namespace list variables {α : Type u} {β : α → Type v} /- nodupkeys -/ def nodupkeys (l : list (sigma β)) : Prop := (l.map sigma.fst).nodup theorem nodupkeys_iff_pairwise {l} : nodupkeys l ↔ pairwise (λ s s' : sigma β, s.1 ≠ s'.1) l := pairwise_map _ @[simp] theorem nodupkeys_nil : @nodupkeys α β [] := pairwise.nil _ @[simp] theorem nodupkeys_cons {a : α} {b : β a} {l : list (sigma β)} : nodupkeys (⟨a, b⟩::l) ↔ (∀ b' : β a, sigma.mk a b' ∉ l) ∧ nodupkeys l := by simp [nodupkeys] theorem nodupkeys.eq_of_fst_eq {l : list (sigma β)} (nd : nodupkeys l) {s s' : sigma β} (h : s ∈ l) (h' : s' ∈ l) : s.1 = s'.1 → s = s' := @forall_of_forall_of_pairwise _ (λ s s' : sigma β, s.1 = s'.1 → s = s') (λ s s' H h, (H h.symm).symm) _ (λ x h _, rfl) ((nodupkeys_iff_pairwise.1 nd).imp (λ s s' h h', (h h').elim)) _ h _ h' theorem nodupkeys.eq_of_mk_mem {a : α} {b b' : β a} {l : list (sigma β)} (nd : nodupkeys l) (h : sigma.mk a b ∈ l) (h' : sigma.mk a b' ∈ l) : b = b' := by cases nd.eq_of_fst_eq h h' rfl; refl theorem nodupkeys_singleton (s : sigma β) : nodupkeys [s] := nodup_singleton _ theorem nodupkeys_of_sublist {l₁ l₂ : list (sigma β)} (h : l₁ <+ l₂) : nodupkeys l₂ → nodupkeys l₁ := nodup_of_sublist (map_sublist_map _ h) theorem nodup_of_nodupkeys {l : list (sigma β)} : nodupkeys l → nodup l := nodup_of_nodup_map _ theorem perm_nodupkeys {l₁ l₂ : list (sigma β)} (h : l₁ ~ l₂) : nodupkeys l₁ ↔ nodupkeys l₂ := perm_nodup $ perm_map _ h theorem nodupkeys_join {L : list (list (sigma β))} : nodupkeys (join L) ↔ (∀ l ∈ L, nodupkeys l) ∧ pairwise disjoint (L.map (map sigma.fst)) := begin rw [nodupkeys_iff_pairwise, pairwise_join, pairwise_map], refine and_congr (ball_congr $ λ l h, by simp [nodupkeys_iff_pairwise]) _, apply iff_of_eq, congr', ext l₁ l₂, rw [disjoint_iff_ne], simp end theorem nodup_enum_map_fst (l : list α) : (l.enum.map prod.fst).nodup := by simp [list.nodup_range] variables [decidable_eq α] /- lookup -/ /-- `lookup a l` is the first value in `l` corresponding to the key `a`, or `none` if no such element exists. -/ def lookup (a : α) : list (sigma β) → option (β a) | [] := none | (⟨a', b⟩ :: l) := if h : a' = a then some (eq.rec_on h b) else lookup l @[simp] theorem lookup_nil (a : α) : lookup a [] = @none (β a) := rfl @[simp] theorem lookup_cons_eq (l) (a : α) (b : β a) : lookup a (⟨a, b⟩::l) = some b := dif_pos rfl @[simp] theorem lookup_cons_ne (l) {a} : ∀ s : sigma β, a ≠ s.1 → lookup a (s::l) = lookup a l | ⟨a', b⟩ h := dif_neg h.symm theorem lookup_is_some {a : α} : ∀ {l : list (sigma β)}, (lookup a l).is_some ↔ ∃ b : β a, sigma.mk a b ∈ l | [] := by simp | (⟨a', b⟩ :: l) := begin by_cases h : a = a', { subst a', simp, exact ⟨b, or.inl rfl⟩ }, { simp [h, lookup_is_some] }, end theorem lookup_eq_none {a : α} {l : list (sigma β)} : lookup a l = none ↔ ∀ b : β a, sigma.mk a b ∉ l := begin have := not_congr (@lookup_is_some _ _ _ a l), simp at this, refine iff.trans _ this, cases lookup a l; exact dec_trivial end theorem of_mem_lookup {a : α} {b : β a} : ∀ {l : list (sigma β)}, b ∈ lookup a l → sigma.mk a b ∈ l | (⟨a', b'⟩ :: l) H := begin by_cases h : a = a', { subst a', simp at H, simp [H] }, { simp [h] at H, exact or.inr (of_mem_lookup H) } end theorem map_lookup_eq_find (a : α) : ∀ l : list (sigma β), (lookup a l).map (sigma.mk a) = find (λ s, a = s.1) l | [] := rfl | (⟨a', b'⟩ :: l) := begin by_cases h : a = a', { subst a', simp }, { simp [h, map_lookup_eq_find] } end theorem mem_lookup_iff {a : α} {b : β a} {l : list (sigma β)} (nd : l.nodupkeys) : b ∈ lookup a l ↔ sigma.mk a b ∈ l := ⟨of_mem_lookup, λ h, begin cases option.is_some_iff_exists.1 (lookup_is_some.2 ⟨_, h⟩) with b' h', cases nd.eq_of_mk_mem h (of_mem_lookup h'), exact h' end⟩ theorem perm_lookup (a : α) {l₁ l₂ : list (sigma β)} (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : lookup a l₁ = lookup a l₂ := by ext b; simp [mem_lookup_iff, nd₁, nd₂]; exact mem_of_perm p /- lookup_all -/ /-- `lookup_all a l` is the list of all values in `l` corresponding to the key `a`. -/ def lookup_all (a : α) : list (sigma β) → list (β a) | [] := [] | (⟨a', b⟩ :: l) := if h : a' = a then eq.rec_on h b :: lookup_all l else lookup_all l @[simp] theorem lookup_all_nil (a : α) : lookup_all a [] = @nil (β a) := rfl @[simp] theorem lookup_all_cons_eq (l) (a : α) (b : β a) : lookup_all a (⟨a, b⟩::l) = b :: lookup_all a l := dif_pos rfl @[simp] theorem lookup_all_cons_ne (l) {a} : ∀ s : sigma β, a ≠ s.1 → lookup_all a (s::l) = lookup_all a l | ⟨a', b⟩ h := dif_neg h.symm theorem lookup_all_eq_nil {a : α} : ∀ {l : list (sigma β)}, lookup_all a l = [] ↔ ∀ b : β a, sigma.mk a b ∉ l | [] := by simp | (⟨a', b⟩ :: l) := begin by_cases h : a = a', { subst a', simp, exact λ H, H b (or.inl rfl) }, { simp [h, lookup_all_eq_nil] }, end theorem head_lookup_all (a : α) : ∀ l : list (sigma β), head' (lookup_all a l) = lookup a l | [] := by simp | (⟨a', b⟩ :: l) := by by_cases h : a = a'; [{subst h, simp}, simp *] theorem mem_lookup_all {a : α} {b : β a} : ∀ {l : list (sigma β)}, b ∈ lookup_all a l ↔ sigma.mk a b ∈ l | [] := by simp | (⟨a', b'⟩ :: l) := by by_cases h : a = a'; [{subst h, simp *}, simp *] theorem lookup_all_sublist (a : α) : ∀ l : list (sigma β), (lookup_all a l).map (sigma.mk a) <+ l | [] := by simp | (⟨a', b'⟩ :: l) := begin by_cases h : a = a', { subst h, simp, exact (lookup_all_sublist l).cons2 _ _ _ }, { simp [h], exact (lookup_all_sublist l).cons _ _ _ } end theorem lookup_all_length_le_one (a : α) {l : list (sigma β)} (h : l.nodupkeys) : length (lookup_all a l) ≤ 1 := by have := nodup_of_sublist (map_sublist_map _ $ lookup_all_sublist a l) h; rw map_map at this; rwa [← nodup_repeat, ← map_const _ a] theorem lookup_all_eq_lookup (a : α) {l : list (sigma β)} (h : l.nodupkeys) : lookup_all a l = (lookup a l).to_list := begin rw ← head_lookup_all, have := lookup_all_length_le_one a h, revert this, rcases lookup_all a l with _|⟨b, _|⟨c, l⟩⟩; intro; try {refl}, exact absurd this dec_trivial end theorem lookup_all_nodup (a : α) {l : list (sigma β)} (h : l.nodupkeys) : (lookup_all a l).nodup := by rw lookup_all_eq_lookup a h; apply option.to_list_nodup theorem perm_lookup_all (a : α) {l₁ l₂ : list (sigma β)} (nd₁ : l₁.nodupkeys) (nd₂ : l₂.nodupkeys) (p : l₁ ~ l₂) : lookup_all a l₁ = lookup_all a l₂ := by simp [lookup_all_eq_lookup, nd₁, nd₂, perm_lookup a nd₁ nd₂ p] /- kreplace -/ def kreplace (a : α) (b : β a) : list (sigma β) → list (sigma β) := lookmap $ λ s, if h : a = s.1 then some ⟨a, b⟩ else none theorem kreplace_of_forall_not (a : α) (b : β a) {l : list (sigma β)} (H : ∀ b : β a, sigma.mk a b ∉ l) : kreplace a b l = l := lookmap_of_forall_not _ $ begin rintro ⟨a', b'⟩ h, dsimp, split_ifs, { subst a', exact H _ h }, {refl} end theorem kreplace_self {a : α} {b : β a} {l : list (sigma β)} (nd : nodupkeys l) (h : sigma.mk a b ∈ l) : kreplace a b l = l := begin refine (lookmap_congr _).trans (lookmap_id' (option.guard (λ s, a = s.1)) _ _), { rintro ⟨a', b'⟩ h', dsimp [option.guard], split_ifs, { subst a', exact ⟨rfl, heq_of_eq $ nd.eq_of_mk_mem h h'⟩ }, { refl } }, { rintro ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, dsimp [option.guard], split_ifs, { subst a₁, rintro ⟨⟩, simp }, { rintro ⟨⟩ } }, end theorem kreplace_map_fst (a : α) (b : β a) : ∀ l : list (sigma β), (kreplace a b l).map sigma.fst = l.map sigma.fst := lookmap_map_eq _ _ $ by rintro ⟨a₁, b₂⟩ ⟨a₂, b₂⟩; dsimp; split_ifs; simp [h] {contextual := tt} theorem kreplace_nodupkeys (a : α) (b : β a) {l : list (sigma β)} : (kreplace a b l).nodupkeys ↔ l.nodupkeys := by simp [nodupkeys, kreplace_map_fst] theorem perm_kreplace {a : α} {b : β a} {l₁ l₂ : list (sigma β)} (nd : l₁.nodupkeys) : l₁ ~ l₂ → kreplace a b l₁ ~ kreplace a b l₂ := perm_lookmap _ $ begin refine (nodupkeys_iff_pairwise.1 nd).imp _, intros x y h z h₁ w h₂, split_ifs at h₁ h₂; cases h₁; cases h₂, exact (h (h_2.symm.trans h_1)).elim end /- kerase -/ def kerase (a : α) : list (sigma β) → list (sigma β) := erasep $ λ s, a = s.1 theorem kerase_sublist (a : α) (l : list (sigma β)) : kerase a l <+ l := erasep_sublist _ theorem kerase_map_fst (a : α) (l : list (sigma β)) : (kerase a l).map sigma.fst = (l.map sigma.fst).erase a := by rw [kerase, ←erasep_map sigma.fst l, erase_eq_erasep] theorem kerase_nodupkeys (a : α) {l : list (sigma β)} : nodupkeys l → (kerase a l).nodupkeys := nodupkeys_of_sublist $ kerase_sublist _ _ theorem perm_kerase {a : α} {l₁ l₂ : list (sigma β)} (nd : l₁.nodupkeys) : l₁ ~ l₂ → kerase a l₁ ~ kerase a l₂ := perm_erasep _ $ (nodupkeys_iff_pairwise.1 nd).imp $ by rintro x y h rfl; exact h def kextract (a : α) : list (sigma β) → option (β a) × list (sigma β) | [] := (none, []) | (s::l) := if h : s.1 = a then (some (eq.rec_on h s.2), l) else let (b', l') := kextract l in (b', s :: l') @[simp] theorem kextract_eq_lookup_kerase (a : α) : ∀ l : list (sigma β), kextract a l = (lookup a l, kerase a l) | [] := rfl | (⟨a', b⟩::l) := begin simp [kextract], dsimp, split_ifs, { subst a', simp [kerase] }, { simp [kextract, ne.symm h, kextract_eq_lookup_kerase l, kerase] } end end list
b357c3f19d1789cb26615c80e266ff1122ba6746
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/algebra/category/Mon/basic.lean
7cd096e980f0984958b6e812ddf6712a0a07b34a
[ "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,664
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.concrete_category import algebra.group /-! # Category instances for monoid, add_monoid, comm_monoid, and add_comm_monoid. We introduce the bundled categories: * `Mon` * `AddMon` * `CommMon` * `AddCommMon` along with the relevant forgetful functors between them. -/ universes u v open category_theory /-- The category of monoids and monoid morphisms. -/ @[reducible, to_additive AddMon] def Mon : Type (u+1) := bundled monoid namespace Mon @[to_additive add_monoid] instance (x : Mon) : monoid x := x.str /-- Construct a bundled Mon from the underlying type and typeclass. -/ @[to_additive] def of (M : Type u) [monoid M] : Mon := bundled.of M @[to_additive] instance bundled_hom : bundled_hom @monoid_hom := ⟨@monoid_hom.to_fun, @monoid_hom.id, @monoid_hom.comp, @monoid_hom.coe_inj⟩ end Mon /-- The category of commutative monoids and monoid morphisms. -/ @[reducible, to_additive AddCommMon] def CommMon : Type (u+1) := bundled comm_monoid namespace CommMon @[to_additive add_comm_monoid] instance (x : CommMon) : comm_monoid x := x.str /-- Construct a bundled CommMon from the underlying type and typeclass. -/ @[to_additive] def of (X : Type u) [comm_monoid X] : CommMon := bundled.of X @[to_additive] instance bundled_hom : bundled_hom _ := Mon.bundled_hom.full_subcategory @comm_monoid.to_monoid @[to_additive has_forget_to_AddMon] instance has_forget_to_Mon : has_forget₂ CommMon.{u} Mon.{u} := Mon.bundled_hom.full_subcategory_has_forget₂ _ end CommMon
703c4ce64c6d45fe1fc3e4124e02da1210902523
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/record_rec_protected.lean
9f97ae68d6b5497acf99a37d67c99c8fc7773041
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
121
lean
-- structure point (A : Type) (B : Type) := mk :: (x : A) (y : B) open point check rec -- error, rec is protected
743cd4e9ed461ee50e5da0f1e4e5463d39bd0402
ef4d3feecef33d1c1b4bd3a023b85e6a58f9e708
/theorem-proving-in-lean/ch1/Example.lean
4a912740085bbdd0480e5aaa55689e1cf610a32d
[]
no_license
MikeMKH/kata
1b7da1b8d2cc115c912f2b06b583a8e675a449e1
305b054a37517dbe4d09545d41f024937f536c20
refs/heads/master
1,585,594,368,426
1,542,835,119,000
1,542,835,119,000
16,891,298
0
0
null
1,542,835,120,000
1,392,575,890,000
Racket
UTF-8
Lean
false
false
266
lean
theorem and_communitive (p q : Prop) : p ∧ q → q ∧ p := assume hpq : p ∧ q, have hp : p, from and.left hpq, have hq : q, from and.right hpq, show q ∧ p, from and.intro hq hp #check and_communitive -- and_communitive : ∀ (p q : Prop), p ∧ q → q ∧ p
8c335cbda74ea51332b8a2c3583cffda0ca9e091
9dc8cecdf3c4634764a18254e94d43da07142918
/src/group_theory/specific_groups/cyclic.lean
234e9c3513e62c4b0c6baf193d4faa041be6697a
[ "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
22,305
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import algebra.big_operators.order import data.nat.totient import group_theory.order_of_element import tactic.group import group_theory.exponent /-! # Cyclic groups A group `G` is called cyclic if there exists an element `g : G` such that every element of `G` is of the form `g ^ n` for some `n : ℕ`. This file only deals with the predicate on a group to be cyclic. For the concrete cyclic group of order `n`, see `data.zmod.basic`. ## Main definitions * `is_cyclic` is a predicate on a group stating that the group is cyclic. ## Main statements * `is_cyclic_of_prime_card` proves that a finite group of prime order is cyclic. * `is_simple_group_of_prime_card`, `is_simple_group.is_cyclic`, and `is_simple_group.prime_card` classify finite simple abelian groups. * `is_cyclic.exponent_eq_card`: For a finite cyclic group `G`, the exponent is equal to the group's cardinality. * `is_cyclic.exponent_eq_zero_of_infinite`: Infinite cyclic groups have exponent zero. * `is_cyclic.iff_exponent_eq_card`: A finite commutative group is cyclic iff its exponent is equal to its cardinality. ## Tags cyclic group -/ universe u variables {α : Type u} {a : α} section cyclic open_locale big_operators local attribute [instance] set_fintype open subgroup /-- A group is called *cyclic* if it is generated by a single element. -/ class is_add_cyclic (α : Type u) [add_group α] : Prop := (exists_generator [] : ∃ g : α, ∀ x, x ∈ add_subgroup.zmultiples g) /-- A group is called *cyclic* if it is generated by a single element. -/ @[to_additive is_add_cyclic] class is_cyclic (α : Type u) [group α] : Prop := (exists_generator [] : ∃ g : α, ∀ x, x ∈ zpowers g) @[priority 100, to_additive is_add_cyclic_of_subsingleton] instance is_cyclic_of_subsingleton [group α] [subsingleton α] : is_cyclic α := ⟨⟨1, λ x, by { rw subsingleton.elim x 1, exact mem_zpowers 1 }⟩⟩ /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `comm_group`. -/ @[to_additive "A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `add_comm_group`."] def is_cyclic.comm_group [hg : group α] [is_cyclic α] : comm_group α := { mul_comm := λ x y, let ⟨g, hg⟩ := is_cyclic.exists_generator α, ⟨n, hn⟩ := hg x, ⟨m, hm⟩ := hg y in hm ▸ hn ▸ zpow_mul_comm _ _ _, ..hg } variables [group α] @[to_additive monoid_add_hom.map_add_cyclic] lemma monoid_hom.map_cyclic {G : Type*} [group G] [h : is_cyclic G] (σ : G →* G) : ∃ m : ℤ, ∀ g : G, σ g = g ^ m := begin obtain ⟨h, hG⟩ := is_cyclic.exists_generator G, obtain ⟨m, hm⟩ := hG (σ h), refine ⟨m, λ g, _⟩, obtain ⟨n, rfl⟩ := hG g, rw [monoid_hom.map_zpow, ←hm, ←zpow_mul, ←zpow_mul'], end @[to_additive is_add_cyclic_of_order_of_eq_card] lemma is_cyclic_of_order_of_eq_card [fintype α] (x : α) (hx : order_of x = fintype.card α) : is_cyclic α := begin classical, use x, simp_rw [← set_like.mem_coe, ← set.eq_univ_iff_forall], rw [←fintype.card_congr (equiv.set.univ α), order_eq_card_zpowers] at hx, exact set.eq_of_subset_of_card_le (set.subset_univ _) (ge_of_eq hx), end /-- A finite group of prime order is cyclic. -/ @[to_additive is_add_cyclic_of_prime_card "A finite group of prime order is cyclic."] lemma is_cyclic_of_prime_card {α : Type u} [group α] [fintype α] {p : ℕ} [hp : fact p.prime] (h : fintype.card α = p) : is_cyclic α := ⟨begin obtain ⟨g, hg⟩ : ∃ g : α, g ≠ 1 := fintype.exists_ne_of_one_lt_card (h.symm ▸ hp.1.one_lt) 1, classical, -- for fintype (subgroup.zpowers g) have : fintype.card (subgroup.zpowers g) ∣ p, { rw ←h, apply card_subgroup_dvd_card }, rw nat.dvd_prime hp.1 at this, cases this, { rw fintype.card_eq_one_iff at this, cases this with t ht, suffices : g = 1, { contradiction }, have hgt := ht ⟨g, by { change g ∈ subgroup.zpowers g, exact subgroup.mem_zpowers g }⟩, rw [←ht 1] at hgt, change (⟨_, _⟩ : subgroup.zpowers g) = ⟨_, _⟩ at hgt, simpa using hgt }, { use g, intro x, rw [←h] at this, rw subgroup.eq_top_of_card_eq _ this, exact subgroup.mem_top _ } end⟩ @[to_additive add_order_of_eq_card_of_forall_mem_zmultiples] lemma order_of_eq_card_of_forall_mem_zpowers [fintype α] {g : α} (hx : ∀ x, x ∈ zpowers g) : order_of g = fintype.card α := begin classical, rw order_eq_card_zpowers, apply fintype.card_of_finset', simpa using hx end @[to_additive infinite.add_order_of_eq_zero_of_forall_mem_zmultiples] lemma infinite.order_of_eq_zero_of_forall_mem_zpowers [infinite α] {g : α} (h : ∀ x, x ∈ zpowers g) : order_of g = 0 := begin classical, rw order_of_eq_zero_iff', refine λ n hn hgn, _, have ho := order_of_pos' ((is_of_fin_order_iff_pow_eq_one g).mpr ⟨n, hn, hgn⟩), obtain ⟨x, hx⟩ := infinite.exists_not_mem_finset (finset.image (pow g) $ finset.range $ order_of g), apply hx, rw [←mem_powers_iff_mem_range_order_of' g x ho, submonoid.mem_powers_iff], obtain ⟨k, hk⟩ := h x, obtain ⟨k, rfl | rfl⟩ := k.eq_coe_or_neg, { exact ⟨k, by exact_mod_cast hk⟩ }, let t : ℤ := -k % (order_of g), rw zpow_eq_mod_order_of at hk, have : 0 ≤ t := int.mod_nonneg (-k) (by exact_mod_cast ho.ne'), refine ⟨t.to_nat, _⟩, rwa [←zpow_coe_nat, int.to_nat_of_nonneg this] end @[to_additive bot.is_add_cyclic] instance bot.is_cyclic {α : Type u} [group α] : is_cyclic (⊥ : subgroup α) := ⟨⟨1, λ x, ⟨0, subtype.eq $ (zpow_zero (1 : α)).trans $ eq.symm (subgroup.mem_bot.1 x.2)⟩⟩⟩ @[to_additive add_subgroup.is_add_cyclic] instance subgroup.is_cyclic {α : Type u} [group α] [is_cyclic α] (H : subgroup α) : is_cyclic H := by haveI := classical.prop_decidable; exact let ⟨g, hg⟩ := is_cyclic.exists_generator α in if hx : ∃ (x : α), x ∈ H ∧ x ≠ (1 : α) then let ⟨x, hx₁, hx₂⟩ := hx in let ⟨k, hk⟩ := hg x in have hex : ∃ n : ℕ, 0 < n ∧ g ^ n ∈ H, from ⟨k.nat_abs, nat.pos_of_ne_zero (λ h, hx₂ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, zpow_zero]), match k, hk with | (k : ℕ), hk := by rw [int.nat_abs_of_nat, ← zpow_coe_nat, hk]; exact hx₁ | -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat, ← subgroup.inv_mem_iff H]; simp * at * end⟩, ⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩, λ ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in have hk₁ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ zpowers (g ^ nat.find hex), from ⟨k / nat.find hex, by rw [← zpow_coe_nat, zpow_mul]⟩, have hk₂ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ H, by { rw zpow_mul, apply H.zpow_mem, exact_mod_cast (nat.find_spec hex).2 }, have hk₃ : g ^ (k % nat.find hex) ∈ H, from (subgroup.mul_mem_cancel_right H hk₂).1 $ by rw [← zpow_add, int.mod_add_div, hk]; exact hx, have hk₄ : k % nat.find hex = (k % nat.find hex).nat_abs, by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)), have hk₅ : g ^ (k % nat.find hex ).nat_abs ∈ H, by rwa [← zpow_coe_nat, ← hk₄], have hk₆ : (k % (nat.find hex : ℤ)).nat_abs = 0, from by_contradiction (λ h, nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hk₄]; exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1)) ⟨nat.pos_of_ne_zero h, hk₅⟩), ⟨k / (nat.find hex : ℤ), subtype.ext_iff_val.2 begin suffices : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) = x, { simpa [zpow_mul] }, rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk] end⟩⟩⟩ else have H = (⊥ : subgroup α), from subgroup.ext $ λ x, ⟨λ h, by simp at *; tauto, λ h, by rw [subgroup.mem_bot.1 h]; exact H.one_mem⟩, by clear _let_match; substI this; apply_instance open finset nat section classical open_locale classical @[to_additive is_add_cyclic.card_pow_eq_one_le] lemma is_cyclic.card_pow_eq_one_le [decidable_eq α] [fintype α] [is_cyclic α] {n : ℕ} (hn0 : 0 < n) : (univ.filter (λ a : α, a ^ n = 1)).card ≤ n := let ⟨g, hg⟩ := is_cyclic.exists_generator α in calc (univ.filter (λ a : α, a ^ n = 1)).card ≤ ((zpowers (g ^ (fintype.card α / (nat.gcd n (fintype.card α))))) : set α).to_finset.card : card_le_of_subset (λ x hx, let ⟨m, hm⟩ := show x ∈ submonoid.powers g, from mem_powers_iff_mem_zpowers.2 $ hg x in set.mem_to_finset.2 ⟨(m / (fintype.card α / (nat.gcd n (fintype.card α))) : ℕ), have hgmn : g ^ (m * nat.gcd n (fintype.card α)) = 1, by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2, begin rw [zpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm], refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card α) hn0) _, conv_lhs { rw [nat.div_mul_cancel (nat.gcd_dvd_right _ _), ←order_of_eq_card_of_forall_mem_zpowers hg] }, exact order_of_dvd_of_pow_eq_one hgmn end⟩) ... ≤ n : let ⟨m, hm⟩ := nat.gcd_dvd_right n (fintype.card α) in have hm0 : 0 < m, from nat.pos_of_ne_zero $ λ hm0, by { rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm, exact hm.elim' 1 }, begin simp only [set.to_finset_card, set_like.coe_sort_coe], rw [←order_eq_card_zpowers, order_of_pow g, order_of_eq_card_of_forall_mem_zpowers hg], rw [hm] {occs := occurrences.pos [2,3]}, rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, nat.mul_div_cancel _ hm0], exact le_of_dvd hn0 (nat.gcd_dvd_left _ _) end end classical @[to_additive] lemma is_cyclic.exists_monoid_generator [finite α] [is_cyclic α] : ∃ x : α, ∀ y : α, y ∈ submonoid.powers x := by { simp_rw [mem_powers_iff_mem_zpowers], exact is_cyclic.exists_generator α } section variables [decidable_eq α] [fintype α] @[to_additive] lemma is_cyclic.image_range_order_of (ha : ∀ x : α, x ∈ zpowers a) : finset.image (λ i, a ^ i) (range (order_of a)) = univ := begin simp_rw [←set_like.mem_coe] at ha, simp only [image_range_order_of, set.eq_univ_iff_forall.mpr ha, set.to_finset_univ], end @[to_additive] lemma is_cyclic.image_range_card (ha : ∀ x : α, x ∈ zpowers a) : finset.image (λ i, a ^ i) (range (fintype.card α)) = univ := by rw [← order_of_eq_card_of_forall_mem_zpowers ha, is_cyclic.image_range_order_of ha] end section totient variables [decidable_eq α] [fintype α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n) include hn private lemma card_pow_eq_one_eq_order_of_aux (a : α) : (finset.univ.filter (λ b : α, b ^ order_of a = 1)).card = order_of a := le_antisymm (hn _ (order_of_pos a)) (calc order_of a = @fintype.card (zpowers a) (id _) : order_eq_card_zpowers ... ≤ @fintype.card (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (fintype.of_finset _ (λ _, iff.rfl)) : @fintype.card_le_of_injective (zpowers a) (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (id _) (id _) (λ b, ⟨b.1, mem_filter.2 ⟨mem_univ _, let ⟨i, hi⟩ := b.2 in by rw [← hi, ← zpow_coe_nat, ← zpow_mul, mul_comm, zpow_mul, zpow_coe_nat, pow_order_of_eq_one, one_zpow]⟩⟩) (λ _ _ h, subtype.eq (subtype.mk.inj h)) ... = (univ.filter (λ b : α, b ^ order_of a = 1)).card : fintype.card_of_finset _ _) open_locale nat -- use φ for nat.totient private lemma card_order_of_eq_totient_aux₁ : ∀ {d : ℕ}, d ∣ fintype.card α → 0 < (univ.filter (λ a : α, order_of a = d)).card → (univ.filter (λ a : α, order_of a = d)).card = φ d := begin intros d hd hd0, induction d using nat.strong_rec' with d IH, rcases d.eq_zero_or_pos with rfl | hd_pos, { cases fintype.card_ne_zero (eq_zero_of_zero_dvd hd) }, rcases card_pos.1 hd0 with ⟨a, ha'⟩, have ha : order_of a = d := (mem_filter.1 ha').2, have h1 : ∑ m in d.proper_divisors, (univ.filter (λ a : α, order_of a = m)).card = ∑ m in d.proper_divisors, φ m, { refine finset.sum_congr rfl (λ m hm, _), simp only [mem_filter, mem_range, mem_proper_divisors] at hm, refine IH m hm.2 (hm.1.trans hd) (finset.card_pos.2 ⟨a ^ (d / m), _⟩), simp only [mem_filter, mem_univ, order_of_pow a, ha, true_and, nat.gcd_eq_right (div_dvd_of_dvd hm.1), nat.div_div_self hm.1 hd_pos] }, have h2 : ∑ m in d.divisors, (univ.filter (λ a : α, order_of a = m)).card = ∑ m in d.divisors, φ m, { rw [←filter_dvd_eq_divisors hd_pos.ne', sum_card_order_of_eq_card_pow_eq_one hd_pos, filter_dvd_eq_divisors hd_pos.ne', sum_totient, ←ha, card_pow_eq_one_eq_order_of_aux hn a] }, simpa [divisors_eq_proper_divisors_insert_self_of_pos hd_pos, ←h1] using h2, end lemma card_order_of_eq_totient_aux₂ {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = φ d := begin let c := fintype.card α, have hc0 : 0 < c := fintype.card_pos_iff.2 ⟨1⟩, apply card_order_of_eq_totient_aux₁ hn hd, by_contradiction h0, simp only [not_lt, _root_.le_zero_iff, card_eq_zero] at h0, apply lt_irrefl c, calc c = ∑ m in c.divisors, (univ.filter (λ a : α, order_of a = m)).card : by { simp only [←filter_dvd_eq_divisors hc0.ne', sum_card_order_of_eq_card_pow_eq_one hc0], apply congr_arg card, simp } ... = ∑ m in c.divisors.erase d, (univ.filter (λ a : α, order_of a = m)).card : by { rw eq_comm, refine (sum_subset (erase_subset _ _) (λ m hm₁ hm₂, _)), have : m = d, by { contrapose! hm₂, exact mem_erase_of_ne_of_mem hm₂ hm₁ }, simp [this, h0] } ... ≤ ∑ m in c.divisors.erase d, φ m : by { refine sum_le_sum (λ m hm, _), have hmc : m ∣ c, { simp only [mem_erase, mem_divisors] at hm, tauto }, rcases (filter (λ (a : α), order_of a = m) univ).card.eq_zero_or_pos with h1 | h1, { simp [h1] }, { simp [card_order_of_eq_totient_aux₁ hn hmc h1] } } ... < ∑ m in c.divisors, φ m : sum_erase_lt_of_pos (mem_divisors.2 ⟨hd, hc0.ne'⟩) (totient_pos (pos_of_dvd_of_pos hd hc0)) ... = c : sum_totient _ end lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic α := have (univ.filter (λ a : α, order_of a = fintype.card α)).nonempty, from (card_pos.1 $ by rw [card_order_of_eq_totient_aux₂ hn dvd_rfl]; exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)), let ⟨x, hx⟩ := this in is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2 lemma is_add_cyclic_of_card_pow_eq_one_le {α} [add_group α] [decidable_eq α] [fintype α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, n • a = 0)).card ≤ n) : is_add_cyclic α := begin obtain ⟨g, hg⟩ := @is_cyclic_of_card_pow_eq_one_le (multiplicative α) _ _ _ hn, exact ⟨⟨g, hg⟩⟩ end attribute [to_additive is_cyclic_of_card_pow_eq_one_le] is_add_cyclic_of_card_pow_eq_one_le end totient lemma is_cyclic.card_order_of_eq_totient [is_cyclic α] [fintype α] {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d := begin classical, apply card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd end lemma is_add_cyclic.card_order_of_eq_totient {α} [add_group α] [is_add_cyclic α] [fintype α] {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, add_order_of a = d)).card = totient d := begin obtain ⟨g, hg⟩ := id ‹is_add_cyclic α›, exact @is_cyclic.card_order_of_eq_totient (multiplicative α) _ ⟨⟨g, hg⟩⟩ _ _ hd end attribute [to_additive is_cyclic.card_order_of_eq_totient] is_add_cyclic.card_order_of_eq_totient /-- A finite group of prime order is simple. -/ @[to_additive "A finite group of prime order is simple."] lemma is_simple_group_of_prime_card {α : Type u} [group α] [fintype α] {p : ℕ} [hp : fact p.prime] (h : fintype.card α = p) : is_simple_group α := ⟨begin have h' := nat.prime.one_lt (fact.out p.prime), rw ← h at h', haveI := fintype.one_lt_card_iff_nontrivial.1 h', apply exists_pair_ne α, end, λ H Hn, begin classical, have hcard := card_subgroup_dvd_card H, rw [h, dvd_prime (fact.out p.prime)] at hcard, refine hcard.imp (λ h1, _) (λ hp, _), { haveI := fintype.card_le_one_iff_subsingleton.1 (le_of_eq h1), apply eq_bot_of_subsingleton }, { exact eq_top_of_card_eq _ (hp.trans h.symm) } end⟩ end cyclic section quotient_center open subgroup variables {G : Type*} {H : Type*} [group G] [group H] /-- A group is commutative if the quotient by the center is cyclic. Also see `comm_group_of_cycle_center_quotient` for the `comm_group` instance. -/ @[to_additive commutative_of_add_cyclic_center_quotient "A group is commutative if the quotient by the center is cyclic. Also see `add_comm_group_of_cycle_center_quotient` for the `add_comm_group` instance."] lemma commutative_of_cyclic_center_quotient [is_cyclic H] (f : G →* H) (hf : f.ker ≤ center G) (a b : G) : a * b = b * a := let ⟨⟨x, y, (hxy : f y = x)⟩, (hx : ∀ a : f.range, a ∈ zpowers _)⟩ := is_cyclic.exists_generator f.range in let ⟨m, hm⟩ := hx ⟨f a, a, rfl⟩ in let ⟨n, hn⟩ := hx ⟨f b, b, rfl⟩ in have hm : x ^ m = f a, by simpa [subtype.ext_iff] using hm, have hn : x ^ n = f b, by simpa [subtype.ext_iff] using hn, have ha : y ^ (-m) * a ∈ center G, from hf (by rw [f.mem_ker, f.map_mul, f.map_zpow, hxy, zpow_neg, hm, inv_mul_self]), have hb : y ^ (-n) * b ∈ center G, from hf (by rw [f.mem_ker, f.map_mul, f.map_zpow, hxy, zpow_neg, hn, inv_mul_self]), calc a * b = y ^ m * ((y ^ (-m) * a) * y ^ n) * (y ^ (-n) * b) : by simp [mul_assoc] ... = y ^ m * (y ^ n * (y ^ (-m) * a)) * (y ^ (-n) * b) : by rw [mem_center_iff.1 ha] ... = y ^ m * y ^ n * y ^ (-m) * (a * (y ^ (-n) * b)) : by simp [mul_assoc] ... = y ^ m * y ^ n * y ^ (-m) * ((y ^ (-n) * b) * a) : by rw [mem_center_iff.1 hb] ... = b * a : by group /-- A group is commutative if the quotient by the center is cyclic. -/ @[to_additive commutative_of_add_cycle_center_quotient "A group is commutative if the quotient by the center is cyclic."] def comm_group_of_cycle_center_quotient [is_cyclic H] (f : G →* H) (hf : f.ker ≤ center G) : comm_group G := { mul_comm := commutative_of_cyclic_center_quotient f hf, ..show group G, by apply_instance } end quotient_center namespace is_simple_group section comm_group variables [comm_group α] [is_simple_group α] @[priority 100, to_additive is_simple_add_group.is_add_cyclic] instance : is_cyclic α := begin cases subsingleton_or_nontrivial α with hi hi; haveI := hi, { apply is_cyclic_of_subsingleton }, { obtain ⟨g, hg⟩ := exists_ne (1 : α), refine ⟨⟨g, λ x, _⟩⟩, cases is_simple_order.eq_bot_or_eq_top (subgroup.zpowers g) with hb ht, { exfalso, apply hg, rw [← subgroup.mem_bot, ← hb], apply subgroup.mem_zpowers }, { rw ht, apply subgroup.mem_top } } end @[to_additive] theorem prime_card [fintype α] : (fintype.card α).prime := begin have h0 : 0 < fintype.card α := fintype.card_pos_iff.2 (by apply_instance), obtain ⟨g, hg⟩ := is_cyclic.exists_generator α, rw nat.prime_def_lt'', refine ⟨fintype.one_lt_card_iff_nontrivial.2 infer_instance, λ n hn, _⟩, refine (is_simple_order.eq_bot_or_eq_top (subgroup.zpowers (g ^ n))).symm.imp _ _, { intro h, have hgo := order_of_pow g, rw [order_of_eq_card_of_forall_mem_zpowers hg, nat.gcd_eq_right_iff_dvd.1 hn, order_of_eq_card_of_forall_mem_zpowers, eq_comm, nat.div_eq_iff_eq_mul_left (nat.pos_of_dvd_of_pos hn h0) hn] at hgo, { exact (mul_left_cancel₀ (ne_of_gt h0) ((mul_one (fintype.card α)).trans hgo)).symm }, { intro x, rw h, exact subgroup.mem_top _ } }, { intro h, apply le_antisymm (nat.le_of_dvd h0 hn), rw ← order_of_eq_card_of_forall_mem_zpowers hg, apply order_of_le_of_pow_eq_one (nat.pos_of_dvd_of_pos hn h0), rw [← subgroup.mem_bot, ← h], exact subgroup.mem_zpowers _ } end end comm_group end is_simple_group @[to_additive add_comm_group.is_simple_iff_is_add_cyclic_and_prime_card] theorem comm_group.is_simple_iff_is_cyclic_and_prime_card [fintype α] [comm_group α] : is_simple_group α ↔ is_cyclic α ∧ (fintype.card α).prime := begin split, { introI h, exact ⟨is_simple_group.is_cyclic, is_simple_group.prime_card⟩ }, { rintro ⟨hc, hp⟩, haveI : fact (fintype.card α).prime := ⟨hp⟩, exact is_simple_group_of_prime_card rfl } end section exponent open monoid @[to_additive] lemma is_cyclic.exponent_eq_card [group α] [is_cyclic α] [fintype α] : exponent α = fintype.card α := begin obtain ⟨g, hg⟩ := is_cyclic.exists_generator α, apply nat.dvd_antisymm, { rw [←lcm_order_eq_exponent, finset.lcm_dvd_iff], exact λ b _, order_of_dvd_card_univ }, rw ←order_of_eq_card_of_forall_mem_zpowers hg, exact order_dvd_exponent _ end @[to_additive] lemma is_cyclic.of_exponent_eq_card [comm_group α] [fintype α] (h : exponent α = fintype.card α) : is_cyclic α := let ⟨g, _, hg⟩ := finset.mem_image.mp (finset.max'_mem _ _) in is_cyclic_of_order_of_eq_card g $ hg.trans $ exponent_eq_max'_order_of.symm.trans h @[to_additive] lemma is_cyclic.iff_exponent_eq_card [comm_group α] [fintype α] : is_cyclic α ↔ exponent α = fintype.card α := ⟨λ h, by exactI is_cyclic.exponent_eq_card, is_cyclic.of_exponent_eq_card⟩ @[to_additive] lemma is_cyclic.exponent_eq_zero_of_infinite [group α] [is_cyclic α] [infinite α] : exponent α = 0 := let ⟨g, hg⟩ := is_cyclic.exists_generator α in exponent_eq_zero_of_order_zero $ infinite.order_of_eq_zero_of_forall_mem_zpowers hg end exponent
9b96a515361f2546a3a11956232b27f84df821aa
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/equation_with_values.lean
99cd5d4f94afffe319f2bbe6fdda74c5ef556e2b
[ "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
924
lean
def f : char → nat | #"a" := 0 | #"b" := 1 | #"c" := 2 | #"d" := 3 | #"e" := 4 | _ := 5 check f.equations._eqn_1 check f.equations._eqn_2 check f.equations._eqn_3 check f.equations._eqn_4 check f.equations._eqn_5 check f.equations._eqn_6 def g : nat → nat | 100000 := 0 | 200000 := 1 | 300000 := 2 | 400000 := 3 | _ := 5 check g.equations._eqn_1 check g.equations._eqn_2 check g.equations._eqn_3 check g.equations._eqn_4 check g.equations._eqn_5 def h : string → nat | "hello" := 0 | "world" := 1 | "bla" := 2 | "boo" := 3 | _ := 5 check h.equations._eqn_1 check h.equations._eqn_2 check h.equations._eqn_3 check h.equations._eqn_4 check h.equations._eqn_5 def r : string × string → nat | ("hello", "world") := 0 | ("world", "hello") := 1 | _ := 2 check r.equations._eqn_1 check r.equations._eqn_2 check r.equations._eqn_3 check r.equations._eqn_4 check r.equations._eqn_5
24de78d26011d852ee7fcb83c38f6015e9f26cbe
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/topology/algebra/uniform_field.lean
1f7b16963a5a509a4a1494e79724f16cfb7220b2
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,595
lean
/- Copyright (c) 2019 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import topology.algebra.uniform_ring import topology.algebra.field /-! # Completion of topological fields The goal of this file is to prove the main part of Proposition 7 of Bourbaki GT III 6.8 : The completion `hat K` of a Hausdorff topological field is a field if the image under the mapping `x ↦ x⁻¹` of every Cauchy filter (with respect to the additive uniform structure) which does not have a cluster point at `0` is a Cauchy filter (with respect to the additive uniform structure). Bourbaki does not give any detail here, he refers to the general discussion of extending functions defined on a dense subset with values in a complete Hausdorff space. In particular the subtlety about clustering at zero is totally left to readers. Note that the separated completion of a non-separated topological field is the zero ring, hence the separation assumption is needed. Indeed the kernel of the completion map is the closure of zero which is an ideal. Hence it's either zero (and the field is separated) or the full field, which implies one is sent to zero and the completion ring is trivial. The main definition is `completable_top_field` which packages the assumptions as a Prop-valued type class and the main results are the instances `field_completion` and `topological_division_ring_completion`. -/ noncomputable theory open_locale classical uniformity topological_space open set uniform_space uniform_space.completion filter variables (K : Type*) [field K] [uniform_space K] local notation `hat` := completion @[priority 100] instance [separated_space K] : nontrivial (hat K) := ⟨⟨0, 1, λ h, zero_ne_one $ (uniform_embedding_coe K).inj h⟩⟩ /-- A topological field is completable if it is separated and the image under the mapping x ↦ x⁻¹ of every Cauchy filter (with respect to the additive uniform structure) which does not have a cluster point at 0 is a Cauchy filter (with respect to the additive uniform structure). This ensures the completion is a field. -/ class completable_top_field extends separated_space K : Prop := (nice : ∀ F : filter K, cauchy F → 𝓝 0 ⊓ F = ⊥ → cauchy (map (λ x, x⁻¹) F)) variables {K} /-- extension of inversion to the completion of a field. -/ def hat_inv : hat K → hat K := dense_inducing_coe.extend (λ x : K, (coe x⁻¹ : hat K)) lemma continuous_hat_inv [completable_top_field K] {x : hat K} (h : x ≠ 0) : continuous_at hat_inv x := begin haveI : regular_space (hat K) := completion.regular_space K, refine dense_inducing_coe.continuous_at_extend _, apply mem_of_superset (compl_singleton_mem_nhds h), intros y y_ne, rw mem_compl_singleton_iff at y_ne, apply complete_space.complete, rw ← filter.map_map, apply cauchy.map _ (completion.uniform_continuous_coe K), apply completable_top_field.nice, { haveI := dense_inducing_coe.comap_nhds_ne_bot y, apply cauchy_nhds.comap, { rw completion.comap_coe_eq_uniformity, exact le_rfl } }, { have eq_bot : 𝓝 (0 : hat K) ⊓ 𝓝 y = ⊥, { by_contradiction h, exact y_ne (eq_of_nhds_ne_bot $ ne_bot_iff.mpr h).symm }, erw [dense_inducing_coe.nhds_eq_comap (0 : K), ← comap_inf, eq_bot], exact comap_bot }, end /- The value of `hat_inv` at zero is not really specified, although it's probably zero. Here we explicitly enforce the `inv_zero` axiom. -/ instance completion.has_inv : has_inv (hat K) := ⟨λ x, if x = 0 then 0 else hat_inv x⟩ variables [topological_division_ring K] lemma hat_inv_extends {x : K} (h : x ≠ 0) : hat_inv (x : hat K) = coe (x⁻¹ : K) := dense_inducing_coe.extend_eq_at _ ((continuous_coe K).continuous_at.comp (topological_division_ring.continuous_inv x h)) variables [completable_top_field K] @[norm_cast] lemma coe_inv (x : K) : (x : hat K)⁻¹ = ((x⁻¹ : K) : hat K) := begin by_cases h : x = 0, { rw [h, inv_zero], dsimp [has_inv.inv], norm_cast, simp [if_pos] }, { conv_lhs { dsimp [has_inv.inv] }, norm_cast, rw if_neg, { exact hat_inv_extends h }, { exact λ H, h (dense_embedding_coe.inj H) } } end variables [uniform_add_group K] [topological_ring K] lemma mul_hat_inv_cancel {x : hat K} (x_ne : x ≠ 0) : x*hat_inv x = 1 := begin haveI : t1_space (hat K) := t2_space.t1_space, let f := λ x : hat K, x*hat_inv x, let c := (coe : K → hat K), change f x = 1, have cont : continuous_at f x, { letI : topological_space (hat K × hat K) := prod.topological_space, have : continuous_at (λ y : hat K, ((y, hat_inv y) : hat K × hat K)) x, from continuous_id.continuous_at.prod (continuous_hat_inv x_ne), exact (_root_.continuous_mul.continuous_at.comp this : _) }, have clo : x ∈ closure (c '' {0}ᶜ), { have := dense_inducing_coe.dense x, rw [← image_univ, show (univ : set K) = {0} ∪ {0}ᶜ, from (union_compl_self _).symm, image_union] at this, apply mem_closure_of_mem_closure_union this, rw image_singleton, exact compl_singleton_mem_nhds x_ne }, have fxclo : f x ∈ closure (f '' (c '' {0}ᶜ)) := mem_closure_image cont clo, have : f '' (c '' {0}ᶜ) ⊆ {1}, { rw image_image, rintros _ ⟨z, z_ne, rfl⟩, rw mem_singleton_iff, rw mem_compl_singleton_iff at z_ne, dsimp [c, f], rw hat_inv_extends z_ne, norm_cast, rw mul_inv_cancel z_ne, norm_cast }, replace fxclo := closure_mono this fxclo, rwa [closure_singleton, mem_singleton_iff] at fxclo end instance field_completion : field (hat K) := { exists_pair_ne := ⟨0, 1, λ h, zero_ne_one ((uniform_embedding_coe K).inj h)⟩, mul_inv_cancel := λ x x_ne, by { dsimp [has_inv.inv], simp [if_neg x_ne, mul_hat_inv_cancel x_ne], }, inv_zero := show ((0 : K) : hat K)⁻¹ = ((0 : K) : hat K), by rw [coe_inv, inv_zero], ..completion.has_inv, ..(by apply_instance : comm_ring (hat K)) } instance topological_division_ring_completion : topological_division_ring (hat K) := { continuous_inv := begin intros x x_ne, have : {y | hat_inv y = y⁻¹ } ∈ 𝓝 x, { have : {(0 : hat K)}ᶜ ⊆ {y : hat K | hat_inv y = y⁻¹ }, { intros y y_ne, rw mem_compl_singleton_iff at y_ne, dsimp [has_inv.inv], rw if_neg y_ne }, exact mem_of_superset (compl_singleton_mem_nhds x_ne) this }, exact continuous_at.congr (continuous_hat_inv x_ne) this end, ..completion.top_ring_compl }
347725dcc53f3a8c12dc3c2e73eaa3869f41384e
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/special_functions/trigonometric/complex.lean
5128ed705542deccded085d95e1a682e1295b6bc
[ "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,432
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import algebra.quadratic_discriminant import analysis.complex.polynomial import field_theory.is_alg_closed.basic import analysis.special_functions.trigonometric.basic import analysis.convex.specific_functions /-! # Complex trigonometric functions Basic facts and derivatives for the complex trigonometric functions. Several facts about the real trigonometric functions have the proofs deferred here, rather than `analysis.special_functions.trigonometric.basic`, as they are most easily proved by appealing to the corresponding fact for complex trigonometric functions, or require additional imports which are not available in that file. -/ noncomputable theory namespace complex open set filter open_locale real theorem cos_eq_zero_iff {θ : ℂ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 := begin have h : (exp (θ * I) + exp (-θ * I)) / 2 = 0 ↔ exp (2 * θ * I) = -1, { rw [@div_eq_iff _ _ (exp (θ * I) + exp (-θ * I)) 2 0 two_ne_zero', zero_mul, add_eq_zero_iff_eq_neg, neg_eq_neg_one_mul, ← div_eq_iff (exp_ne_zero _), ← exp_sub], field_simp only, congr' 3, ring }, rw [cos, h, ← exp_pi_mul_I, exp_eq_exp_iff_exists_int, mul_right_comm], refine exists_congr (λ x, _), refine (iff_of_eq $ congr_arg _ _).trans (mul_right_inj' $ mul_ne_zero two_ne_zero' I_ne_zero), ring, end theorem cos_ne_zero_iff {θ : ℂ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 := by rw [← not_exists, not_iff_not, cos_eq_zero_iff] theorem sin_eq_zero_iff {θ : ℂ} : sin θ = 0 ↔ ∃ k : ℤ, θ = k * π := begin rw [← complex.cos_sub_pi_div_two, cos_eq_zero_iff], split, { rintros ⟨k, hk⟩, use k + 1, field_simp [eq_add_of_sub_eq hk], ring }, { rintros ⟨k, rfl⟩, use k - 1, field_simp, ring } end theorem sin_ne_zero_iff {θ : ℂ} : sin θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π := by rw [← not_exists, not_iff_not, sin_eq_zero_iff] lemma tan_eq_zero_iff {θ : ℂ} : tan θ = 0 ↔ ∃ k : ℤ, θ = k * π / 2 := begin have h := (sin_two_mul θ).symm, rw mul_assoc at h, rw [tan, div_eq_zero_iff, ← mul_eq_zero, ← zero_mul ((1/2):ℂ), mul_one_div, cancel_factors.cancel_factors_eq_div h two_ne_zero', mul_comm], simpa only [zero_div, zero_mul, ne.def, not_false_iff] with field_simps using sin_eq_zero_iff, end lemma tan_ne_zero_iff {θ : ℂ} : tan θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ k * π / 2 := by rw [← not_exists, not_iff_not, tan_eq_zero_iff] lemma tan_int_mul_pi_div_two (n : ℤ) : tan (n * π/2) = 0 := tan_eq_zero_iff.mpr (by use n) lemma cos_eq_cos_iff {x y : ℂ} : cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x := calc cos x = cos y ↔ cos x - cos y = 0 : sub_eq_zero.symm ... ↔ -2 * sin((x + y)/2) * sin((x - y)/2) = 0 : by rw cos_sub_cos ... ↔ sin((x + y)/2) = 0 ∨ sin((x - y)/2) = 0 : by simp [(by norm_num : (2:ℂ) ≠ 0)] ... ↔ sin((x - y)/2) = 0 ∨ sin((x + y)/2) = 0 : or.comm ... ↔ (∃ k : ℤ, y = 2 * k * π + x) ∨ (∃ k :ℤ, y = 2 * k * π - x) : begin apply or_congr; field_simp [sin_eq_zero_iff, (by norm_num : -(2:ℂ) ≠ 0), eq_sub_iff_add_eq', sub_eq_iff_eq_add, mul_comm (2:ℂ), mul_right_comm _ (2:ℂ)], split; { rintros ⟨k, rfl⟩, use -k, simp, }, end ... ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x : exists_or_distrib.symm lemma sin_eq_sin_iff {x y : ℂ} : sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x := begin simp only [← complex.cos_sub_pi_div_two, cos_eq_cos_iff, sub_eq_iff_eq_add], refine exists_congr (λ k, or_congr _ _); refine eq.congr rfl _; field_simp; ring end lemma tan_add {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y = (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := begin rcases h with ⟨h1, h2⟩ | ⟨⟨k, rfl⟩, ⟨l, rfl⟩⟩, { rw [tan, sin_add, cos_add, ← div_div_div_cancel_right (sin x * cos y + cos x * sin y) (mul_ne_zero (cos_ne_zero_iff.mpr h1) (cos_ne_zero_iff.mpr h2)), add_div, sub_div], simp only [←div_mul_div_comm, ←tan, mul_one, one_mul, div_self (cos_ne_zero_iff.mpr h1), div_self (cos_ne_zero_iff.mpr h2)] }, { obtain ⟨t, hx, hy, hxy⟩ := ⟨tan_int_mul_pi_div_two, t (2*k+1), t (2*l+1), t (2*k+1+(2*l+1))⟩, simp only [int.cast_add, int.cast_bit0, int.cast_mul, int.cast_one, hx, hy] at hx hy hxy, rw [hx, hy, add_zero, zero_div, mul_div_assoc, mul_div_assoc, ← add_mul (2*(k:ℂ)+1) (2*l+1) (π/2), ← mul_div_assoc, hxy] }, end lemma tan_add' {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y ≠ (2 * l + 1) * π / 2)) : tan (x + y) = (tan x + tan y) / (1 - tan x * tan y) := tan_add (or.inl h) lemma tan_two_mul {z : ℂ} : tan (2 * z) = 2 * tan z / (1 - tan z ^ 2) := begin by_cases h : ∀ k : ℤ, z ≠ (2 * k + 1) * π / 2, { rw [two_mul, two_mul, sq, tan_add (or.inl ⟨h, h⟩)] }, { rw not_forall_not at h, rw [two_mul, two_mul, sq, tan_add (or.inr ⟨h, h⟩)] }, end lemma tan_add_mul_I {x y : ℂ} (h : ((∀ k : ℤ, x ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, y * I ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, x = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, y * I = (2 * l + 1) * π / 2)) : tan (x + y*I) = (tan x + tanh y * I) / (1 - tan x * tanh y * I) := by rw [tan_add h, tan_mul_I, mul_assoc] lemma tan_eq {z : ℂ} (h : ((∀ k : ℤ, (z.re:ℂ) ≠ (2 * k + 1) * π / 2) ∧ ∀ l : ℤ, (z.im:ℂ) * I ≠ (2 * l + 1) * π / 2) ∨ ((∃ k : ℤ, (z.re:ℂ) = (2 * k + 1) * π / 2) ∧ ∃ l : ℤ, (z.im:ℂ) * I = (2 * l + 1) * π / 2)) : tan z = (tan z.re + tanh z.im * I) / (1 - tan z.re * tanh z.im * I) := by convert tan_add_mul_I h; exact (re_add_im z).symm open_locale topological_space lemma continuous_on_tan : continuous_on tan {x | cos x ≠ 0} := continuous_on_sin.div continuous_on_cos $ λ x, id @[continuity] lemma continuous_tan : continuous (λ x : {x | cos x ≠ 0}, tan x) := continuous_on_iff_continuous_restrict.1 continuous_on_tan lemma cos_eq_iff_quadratic {z w : ℂ} : cos z = w ↔ (exp (z * I)) ^ 2 - 2 * w * exp (z * I) + 1 = 0 := begin rw ← sub_eq_zero, field_simp [cos, exp_neg, exp_ne_zero], refine eq.congr _ rfl, ring end lemma cos_surjective : function.surjective cos := begin intro x, obtain ⟨w, w₀, hw⟩ : ∃ w ≠ 0, 1 * w * w + (-2 * x) * w + 1 = 0, { rcases exists_quadratic_eq_zero (@one_ne_zero ℂ _ _) (is_alg_closed.exists_eq_mul_self _) with ⟨w, hw⟩, refine ⟨w, _, hw⟩, rintro rfl, simpa only [zero_add, one_ne_zero, mul_zero] using hw }, refine ⟨log w / I, cos_eq_iff_quadratic.2 _⟩, rw [div_mul_cancel _ I_ne_zero, exp_log w₀], convert hw, ring end @[simp] lemma range_cos : range cos = set.univ := cos_surjective.range_eq lemma sin_surjective : function.surjective sin := begin intro x, rcases cos_surjective x with ⟨z, rfl⟩, exact ⟨z + π / 2, sin_add_pi_div_two z⟩ end @[simp] lemma range_sin : range sin = set.univ := sin_surjective.range_eq end complex namespace real open_locale real theorem cos_eq_zero_iff {θ : ℝ} : cos θ = 0 ↔ ∃ k : ℤ, θ = (2 * k + 1) * π / 2 := by exact_mod_cast @complex.cos_eq_zero_iff θ theorem cos_ne_zero_iff {θ : ℝ} : cos θ ≠ 0 ↔ ∀ k : ℤ, θ ≠ (2 * k + 1) * π / 2 := by rw [← not_exists, not_iff_not, cos_eq_zero_iff] lemma cos_eq_cos_iff {x y : ℝ} : cos x = cos y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = 2 * k * π - x := by exact_mod_cast @complex.cos_eq_cos_iff x y lemma sin_eq_sin_iff {x y : ℝ} : sin x = sin y ↔ ∃ k : ℤ, y = 2 * k * π + x ∨ y = (2 * k + 1) * π - x := by exact_mod_cast @complex.sin_eq_sin_iff x y lemma lt_sin_mul {x : ℝ} (hx : 0 < x) (hx' : x < 1) : x < sin ((π / 2) * x) := by simpa [mul_comm x] using strict_concave_on_sin_Icc.2 ⟨le_rfl, pi_pos.le⟩ ⟨pi_div_two_pos.le, half_le_self pi_pos.le⟩ pi_div_two_pos.ne (sub_pos.2 hx') hx lemma le_sin_mul {x : ℝ} (hx : 0 ≤ x) (hx' : x ≤ 1) : x ≤ sin ((π / 2) * x) := by simpa [mul_comm x] using strict_concave_on_sin_Icc.concave_on.2 ⟨le_rfl, pi_pos.le⟩ ⟨pi_div_two_pos.le, half_le_self pi_pos.le⟩ (sub_nonneg.2 hx') hx lemma mul_lt_sin {x : ℝ} (hx : 0 < x) (hx' : x < π / 2) : (2 / π) * x < sin x := begin rw [←inv_div], simpa [-inv_div, pi_div_two_pos.ne'] using @lt_sin_mul ((π / 2)⁻¹ * x) _ _, { exact mul_pos (inv_pos.2 pi_div_two_pos) hx }, { rwa [←div_eq_inv_mul, div_lt_one pi_div_two_pos] }, end /-- In the range `[0, π / 2]`, we have a linear lower bound on `sin`. This inequality forms one half of Jordan's inequality, the other half is `real.sin_lt` -/ lemma mul_le_sin {x : ℝ} (hx : 0 ≤ x) (hx' : x ≤ π / 2) : (2 / π) * x ≤ sin x := begin rw [←inv_div], simpa [-inv_div, pi_div_two_pos.ne'] using @le_sin_mul ((π / 2)⁻¹ * x) _ _, { exact mul_nonneg (inv_nonneg.2 pi_div_two_pos.le) hx }, { rwa [←div_eq_inv_mul, div_le_one pi_div_two_pos] }, end end real
ee6b601bb3267d187579547f7e0dff538e1d0005
968e2f50b755d3048175f176376eff7139e9df70
/examples/prop_logic_lean_summary/unnamed_189.lean
9d05a397db302ad3aa9524306b5ae07135da3f81
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
141
lean
variables p q : Prop -- BEGIN example (h₁ : p) (h₂ : q) : p ∧ q := ⟨h₁, h₂⟩ -- Enter these 'French quotes' with `\<` and `\>`
12c8f11fba0cde540ba42a101db1f6f310008c2a
618003631150032a5676f229d13a079ac875ff77
/src/logic/function/iterate.lean
3ace045acdb2404aed23eaa23fa29d5cba93709a
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
5,302
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury Kudryashov -/ import logic.function.conjugate /-! # Iterations of a function In this file we prove simple properties of `nat.iterate f n` a.k.a. `f^[n]`: * `iterate_zero`, `iterate_succ`, `iterate_succ'`, `iterate_add`, `iterate_mul`: formulas for `f^[0]`, `f^[n+1]` (two versions), `f^[n+m]`, and `f^[n*m]`; * `iterate_id` : `id^[n]=id`; * `injective.iterate`, `surjective.iterate`, `bijective.iterate` : iterates of an injective/surjective/bijective function belong to the same class; * `left_inverse.iterate`, `right_inverse.iterate`, `commute.iterate_left`, `comute.iterate_right`, `commute.iterate_iterate`: some properties of pairs of functions survive under iterations * `iterate_fixed`, `semiconj.iterate_*`, `semiconj₂.iterate`: if `f` fixes a point (resp., semiconjugates unary/binary operarations), then so does `f^[n]`. -/ universes u v variables {α : Type u} {β : Type v} namespace function variable (f : α → α) @[simp] theorem iterate_zero : f^[0] = id := rfl theorem iterate_zero_apply (x : α) : f^[0] x = x := rfl @[simp] theorem iterate_succ (n : ℕ) : f^[n.succ] = (f^[n]) ∘ f := rfl theorem iterate_succ_apply (n : ℕ) (x : α) : f^[n.succ] x = (f^[n]) (f x) := rfl theorem iterate_id (n : ℕ) : nat.iterate (id : α → α) n = id := nat.rec_on n rfl $ λ n ihn, by rw [iterate_succ, ihn, comp.left_id] theorem iterate_add : ∀ (m n : ℕ), f^[m + n] = (f^[m]) ∘ (f^[n]) | m 0 := rfl | m (nat.succ n) := by rw [iterate_succ, iterate_succ, iterate_add] theorem iterate_add_apply (m n : ℕ) (x : α) : f^[m + n] x = (f^[m] (f^[n] x)) := by rw iterate_add @[simp] theorem iterate_one : f^[1] = f := funext $ λ a, rfl lemma iterate_mul (m : ℕ) : ∀ n, f^[m * n] = (f^[m]^[n]) | 0 := by simp only [nat.mul_zero, iterate_zero] | (n + 1) := by simp only [nat.mul_succ, nat.mul_one, iterate_one, iterate_add, iterate_mul n] variable {f} theorem iterate_fixed {x} (h : f x = x) (n : ℕ) : f^[n] x = x := nat.rec_on n rfl $ λ n ihn, by rw [iterate_succ_apply, h, ihn] theorem injective.iterate (Hinj : injective f) (n : ℕ) : injective (f^[n]) := nat.rec_on n injective_id $ λ n ihn, ihn.comp Hinj theorem surjective.iterate (Hsurj : surjective f) (n : ℕ) : surjective (f^[n]) := nat.rec_on n surjective_id $ λ n ihn, ihn.comp Hsurj theorem bijective.iterate (Hbij : bijective f) (n : ℕ) : bijective (f^[n]) := ⟨Hbij.1.iterate n, Hbij.2.iterate n⟩ namespace semiconj lemma iterate_right {f : α → β} {ga : α → α} {gb : β → β} (h : semiconj f ga gb) (n : ℕ) : semiconj f (ga^[n]) (gb^[n]) := nat.rec_on n id_right $ λ n ihn, ihn.comp_right h lemma iterate_left {g : ℕ → α → α} (H : ∀ n, semiconj f (g n) (g $ n + 1)) (n k : ℕ) : semiconj (f^[n]) (g k) (g $ n + k) := begin induction n with n ihn generalizing k, { rw [nat.zero_add], exact id_left }, { rw [nat.succ_eq_add_one, nat.add_right_comm, nat.add_assoc], exact (H k).comp_left (ihn (k + 1)) } end end semiconj namespace commute variable {g : α → α} lemma iterate_right (h : commute f g) (n : ℕ) : commute f (g^[n]) := h.iterate_right n lemma iterate_left (h : commute f g) (n : ℕ) : commute (f^[n]) g := (h.symm.iterate_right n).symm lemma iterate_iterate (h : commute f g) (m n : ℕ) : commute (f^[m]) (g^[n]) := (h.iterate_left m).iterate_right n lemma iterate_eq_of_map_eq (h : commute f g) (n : ℕ) {x} (hx : f x = g x) : f^[n] x = (g^[n]) x := nat.rec_on n rfl $ λ n ihn, by simp only [iterate_succ_apply, hx, (h.iterate_left n).eq, ihn, ((refl g).iterate_right n).eq] lemma comp_iterate (h : commute f g) (n : ℕ) : (f ∘ g)^[n] = (f^[n]) ∘ (g^[n]) := begin induction n with n ihn, { refl }, funext x, simp only [ihn, (h.iterate_right n).eq, iterate_succ, comp_app] end variable (f) lemma iterate_self (n : ℕ) : commute (f^[n]) f := (refl f).iterate_left n lemma self_iterate (n : ℕ) : commute f (f^[n]) := (refl f).iterate_right n lemma iterate_iterate_self (m n : ℕ) : commute (f^[m]) (f^[n]) := (refl f).iterate_iterate m n end commute lemma semiconj₂.iterate {f : α → α} {op : α → α → α} (hf : semiconj₂ f op op) (n : ℕ) : semiconj₂ (f^[n]) op op := nat.rec_on n (semiconj₂.id_left op) (λ n ihn, ihn.comp hf) variable (f) theorem iterate_succ' (n : ℕ) : f^[n.succ] = f ∘ (f^[n]) := by rw [iterate_succ, (commute.self_iterate f n).comp_eq] theorem iterate_succ_apply' (n : ℕ) (x : α) : f^[n.succ] x = f (f^[n] x) := by rw [iterate_succ'] theorem iterate_pred_comp_of_pos {n : ℕ} (hn : 0 < n) : f^[n.pred] ∘ f = (f^[n]) := by rw [← iterate_succ, nat.succ_pred_eq_of_pos hn] theorem comp_iterate_pred_of_pos {n : ℕ} (hn : 0 < n) : f ∘ (f^[n.pred]) = (f^[n]) := by rw [← iterate_succ', nat.succ_pred_eq_of_pos hn] variable {f} theorem left_inverse.iterate {g : α → α} (hg : left_inverse g f) (n : ℕ) : left_inverse (g^[n]) (f^[n]) := nat.rec_on n (λ _, rfl) $ λ n ihn, by { rw [iterate_succ', iterate_succ], exact ihn.comp hg } theorem right_inverse.iterate {g : α → α} (hg : right_inverse g f) (n : ℕ) : right_inverse (g^[n]) (f^[n]) := hg.iterate n end function
392adebb48ddcee33caa52518e205da597d70734
737dc4b96c97368cb66b925eeea3ab633ec3d702
/stage0/src/Lean/PrettyPrinter/Parenthesizer.lean
8f500a3c658d79c6eca359b37401ad46250c1323
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,605
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.CoreM import Lean.KeyedDeclsAttribute import Lean.Parser.Extension import Lean.ParserCompiler.Attribute import Lean.PrettyPrinter.Basic /-! The parenthesizer inserts parentheses into a `Syntax` object where syntactically necessary, usually as an intermediary step between the delaborator and the formatter. While the delaborator outputs structurally well-formed syntax trees that can be re-elaborated without post-processing, this tree structure is lost in the formatter and thus needs to be preserved by proper insertion of parentheses. # The abstract problem & solution The Lean 4 grammar is unstructured and extensible with arbitrary new parsers, so in general it is undecidable whether parentheses are necessary or even allowed at any point in the syntax tree. Parentheses for different categories, e.g. terms and levels, might not even have the same structure. In this module, we focus on the correct parenthesization of parsers defined via `Lean.Parser.prattParser`, which includes both aforementioned built-in categories. Custom parenthesizers can be added for new node kinds, but the data collected in the implementation below might not be appropriate for other parenthesization strategies. Usages of a parser defined via `prattParser` in general have the form `p prec`, where `prec` is the minimum precedence or binding power. Recall that a Pratt parser greedily runs a leading parser with precedence at least `prec` (otherwise it fails) followed by zero or more trailing parsers with precedence at least `prec`; the precedence of a parser is encoded in the call to `leadingNode/trailingNode`, respectively. Thus we should parenthesize a syntax node `stx` supposedly produced by `p prec` if 1. the leading/any trailing parser involved in `stx` has precedence < `prec` (because without parentheses, `p prec` would not produce all of `stx`), or 2. the trailing parser parsing the input to *the right of* `stx`, if any, has precedence >= `prec` (because without parentheses, `p prec` would have parsed it as well and made it a part of `stx`). We also check that the two parsers are from the same syntax category. Note that in case 2, it is also sufficient to parenthesize a *parent* node as long as the offending parser is still to the right of that node. For example, imagine the tree structure of `(f $ fun x => x) y` without parentheses. We need to insert *some* parentheses between `x` and `y` since the lambda body is parsed with precedence 0, while the identifier parser for `y` has precedence `maxPrec`. But we need to parenthesize the `$` node anyway since the precedence of its RHS (0) again is smaller than that of `y`. So it's better to only parenthesize the outer node than ending up with `(f $ (fun x => x)) y`. # Implementation We transform the syntax tree and collect the necessary precedence information for that in a single traversal. The traversal is right-to-left to cover case 2. More specifically, for every Pratt parser call, we store as monadic state the precedence of the left-most trailing parser and the minimum precedence of any parser (`contPrec`/`minPrec`) in this call, if any, and the precedence of the nested trailing Pratt parser call (`trailPrec`), if any. If `stP` is the state resulting from the traversal of a Pratt parser call `p prec`, and `st` is the state of the surrounding call, we parenthesize if `prec > stP.minPrec` (case 1) or if `stP.trailPrec <= st.contPrec` (case 2). The traversal can be customized for each `[*Parser]` parser declaration `c` (more specifically, each `SyntaxNodeKind` `c`) using the `[parenthesizer c]` attribute. Otherwise, a default parenthesizer will be synthesized from the used parser combinators by recursively replacing them with declarations tagged as `[combinatorParenthesizer]` for the respective combinator. If a called function does not have a registered combinator parenthesizer and is not reducible, the synthesizer fails. This happens mostly at the `Parser.mk` decl, which is irreducible, when some parser primitive has not been handled yet. The traversal over the `Syntax` object is complicated by the fact that a parser does not produce exactly one syntax node, but an arbitrary (but constant, for each parser) amount that it pushes on top of the parser stack. This amount can even be zero for parsers such as `checkWsBefore`. Thus we cannot simply pass and return a `Syntax` object to and from `visit`. Instead, we use a `Syntax.Traverser` that allows arbitrary movement and modification inside the syntax tree. Our traversal invariant is that a parser interpreter should stop at the syntax object to the *left* of all syntax objects its parser produced, except when it is already at the left-most child. This special case is not an issue in practice since if there is another parser to the left that produced zero nodes in this case, it should always do so, so there is no danger of the left-most child being processed multiple times. Ultimately, most parenthesizers are implemented via three primitives that do all the actual syntax traversal: `maybeParenthesize mkParen prec x` runs `x` and afterwards transforms it with `mkParen` if the above condition for `p prec` is fulfilled. `visitToken` advances to the preceding sibling and is used on atoms. `visitArgs x` executes `x` on the last child of the current node and then advances to the preceding sibling (of the original current node). -/ namespace Lean namespace PrettyPrinter namespace Parenthesizer structure Context where -- We need to store this `categoryParser` argument to deal with the implicit Pratt parser call in `trailingNode.parenthesizer`. cat : Name := Name.anonymous structure State where stxTrav : Syntax.Traverser --- precedence and category of the current left-most trailing parser, if any; see module doc for details contPrec : Option Nat := none contCat : Name := Name.anonymous -- current minimum precedence in this Pratt parser call, if any; see module doc for details minPrec : Option Nat := none -- precedence and category of the trailing Pratt parser call if any; see module doc for details trailPrec : Option Nat := none trailCat : Name := Name.anonymous -- true iff we have already visited a token on this parser level; used for detecting trailing parsers visitedToken : Bool := false end Parenthesizer abbrev ParenthesizerM := ReaderT Parenthesizer.Context $ StateRefT Parenthesizer.State CoreM abbrev Parenthesizer := ParenthesizerM Unit @[inline] def ParenthesizerM.orElse (p₁ : ParenthesizerM α) (p₂ : Unit → ParenthesizerM α) : ParenthesizerM α := do let s ← get catchInternalId backtrackExceptionId p₁ (fun _ => do set s; p₂ ()) instance : OrElse (ParenthesizerM α) := ⟨ParenthesizerM.orElse⟩ unsafe def mkParenthesizerAttribute : IO (KeyedDeclsAttribute Parenthesizer) := KeyedDeclsAttribute.init { builtinName := `builtinParenthesizer, name := `parenthesizer, descr := "Register a parenthesizer for a parser. [parenthesizer k] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `SyntaxNodeKind` `k`.", valueTypeName := `Lean.PrettyPrinter.Parenthesizer, evalKey := fun builtin stx => do let env ← getEnv let id ← Attribute.Builtin.getId stx -- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to -- synthesize a parenthesizer for it immediately, so we just check for a declaration in this case if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id else throwError "invalid [parenthesizer] argument, unknown syntax kind '{id}'" } `Lean.PrettyPrinter.parenthesizerAttribute @[builtinInit mkParenthesizerAttribute] constant parenthesizerAttribute : KeyedDeclsAttribute Parenthesizer abbrev CategoryParenthesizer := forall (prec : Nat), Parenthesizer unsafe def mkCategoryParenthesizerAttribute : IO (KeyedDeclsAttribute CategoryParenthesizer) := KeyedDeclsAttribute.init { builtinName := `builtinCategoryParenthesizer, name := `categoryParenthesizer, descr := "Register a parenthesizer for a syntax category. [categoryParenthesizer cat] registers a declaration of type `Lean.PrettyPrinter.CategoryParenthesizer` for the category `cat`, which is used when parenthesizing calls of `categoryParser cat prec`. Implementations should call `maybeParenthesize` with the precedence and `cat`. If no category parenthesizer is registered, the category will never be parenthesized, but still be traversed for parenthesizing nested categories.", valueTypeName := `Lean.PrettyPrinter.CategoryParenthesizer, evalKey := fun _ stx => do let env ← getEnv let id ← Attribute.Builtin.getId stx if Parser.isParserCategory env id then pure id else throwError "invalid [categoryParenthesizer] argument, unknown parser category '{toString id}'" } `Lean.PrettyPrinter.categoryParenthesizerAttribute @[builtinInit mkCategoryParenthesizerAttribute] constant categoryParenthesizerAttribute : KeyedDeclsAttribute CategoryParenthesizer unsafe def mkCombinatorParenthesizerAttribute : IO ParserCompiler.CombinatorAttribute := ParserCompiler.registerCombinatorAttribute `combinatorParenthesizer "Register a parenthesizer for a parser combinator. [combinatorParenthesizer c] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `Parser` declaration `c`. Note that, unlike with [parenthesizer], this is not a node kind since combinators usually do not introduce their own node kinds. The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced with `Parenthesizer` in the parameter types." @[builtinInit mkCombinatorParenthesizerAttribute] constant combinatorParenthesizerAttribute : ParserCompiler.CombinatorAttribute namespace Parenthesizer open Lean.Core open Std.Format def throwBacktrack {α} : ParenthesizerM α := throw $ Exception.internal backtrackExceptionId instance : Syntax.MonadTraverser ParenthesizerM := ⟨{ get := State.stxTrav <$> get, set := fun t => modify (fun st => { st with stxTrav := t }), modifyGet := fun f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t })) }⟩ open Syntax.MonadTraverser def addPrecCheck (prec : Nat) : ParenthesizerM Unit := modify fun st => { st with contPrec := Nat.min (st.contPrec.getD prec) prec, minPrec := Nat.min (st.minPrec.getD prec) prec } /-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/ def visitArgs (x : ParenthesizerM Unit) : ParenthesizerM Unit := do let stx ← getCur if stx.getArgs.size > 0 then goDown (stx.getArgs.size - 1) *> x <* goUp goLeft -- Macro scopes in the parenthesizer output are ultimately ignored by the pretty printer, -- so give a trivial implementation. instance : MonadQuotation ParenthesizerM := { getCurrMacroScope := pure arbitrary, getMainModule := pure arbitrary, withFreshMacroScope := fun x => x, } /-- Run `x` and parenthesize the result using `mkParen` if necessary. If `canJuxtapose` is false, we assume the category does not have a token-less juxtaposition syntax a la function application and deactivate rule 2. -/ def maybeParenthesize (cat : Name) (canJuxtapose : Bool) (mkParen : Syntax → Syntax) (prec : Nat) (x : ParenthesizerM Unit) : ParenthesizerM Unit := do let stx ← getCur let idx ← getIdx let st ← get -- reset precs for the recursive call set { stxTrav := st.stxTrav : State } trace[PrettyPrinter.parenthesize] "parenthesizing (cont := {(st.contPrec, st.contCat)}){indentD (format stx)}" x let { minPrec := some minPrec, trailPrec := trailPrec, trailCat := trailCat, .. } ← get | trace[PrettyPrinter.parenthesize] "visited a syntax tree without precedences?!{line ++ format stx}" trace[PrettyPrinter.parenthesize] (m!"...precedences are {prec} >? {minPrec}" ++ if canJuxtapose then m!", {(trailPrec, trailCat)} <=? {(st.contPrec, st.contCat)}" else "") -- Should we parenthesize? if (prec > minPrec || canJuxtapose && match trailPrec, st.contPrec with | some trailPrec, some contPrec => trailCat == st.contCat && trailPrec <= contPrec | _, _ => false) then -- The recursive `visit` call, by the invariant, has moved to the preceding node. In order to parenthesize -- the original node, we must first move to the right, except if we already were at the left-most child in the first -- place. if idx > 0 then goRight let mut stx ← getCur -- Move leading/trailing whitespace of `stx` outside of parentheses if let SourceInfo.original _ pos trail endPos := stx.getHeadInfo then stx := stx.setHeadInfo (SourceInfo.original "".toSubstring pos trail endPos) if let SourceInfo.original lead pos _ endPos := stx.getTailInfo then stx := stx.setTailInfo (SourceInfo.original lead pos "".toSubstring endPos) let mut stx' := mkParen stx if let SourceInfo.original lead pos _ endPos := stx.getHeadInfo then stx' := stx'.setHeadInfo (SourceInfo.original lead pos "".toSubstring endPos) if let SourceInfo.original _ pos trail endPos := stx.getTailInfo then stx' := stx'.setTailInfo (SourceInfo.original "".toSubstring pos trail endPos) trace[PrettyPrinter.parenthesize] "parenthesized: {stx'.formatStx none}" setCur stx' goLeft -- after parenthesization, there is no more trailing parser modify (fun st => { st with contPrec := Parser.maxPrec, contCat := cat, trailPrec := none }) let { trailPrec := trailPrec, .. } ← get -- If we already had a token at this level, keep the trailing parser. Otherwise, use the minimum of -- `prec` and `trailPrec`. if st.visitedToken then modify fun stP => { stP with trailPrec := st.trailPrec, trailCat := st.trailCat } else let trailPrec := match trailPrec with | some trailPrec => Nat.min trailPrec prec | _ => prec modify fun stP => { stP with trailPrec := trailPrec, trailCat := cat } modify fun stP => { stP with minPrec := st.minPrec } /-- Adjust state and advance. -/ def visitToken : Parenthesizer := do modify fun st => { st with contPrec := none, contCat := Name.anonymous, visitedToken := true } goLeft @[combinatorParenthesizer Lean.Parser.orelse] def orelse.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := do let st ← get -- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try -- them in turn. Uses the syntax traverser non-linearly! p1 <|> p2 -- `mkAntiquot` is quite complex, so we'd rather have its parenthesizer synthesized below the actual parser definition. -- Note that there is a mutual recursion -- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere -- anyway. @[extern 8 "lean_mk_antiquot_parenthesizer"] constant mkAntiquot.parenthesizer' (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Parenthesizer @[inline] def liftCoreM {α} (x : CoreM α) : ParenthesizerM α := liftM x -- break up big mutual recursion @[extern "lean_pretty_printer_parenthesizer_interpret_parser_descr"] constant interpretParserDescr' : ParserDescr → CoreM Parenthesizer unsafe def parenthesizerForKindUnsafe (k : SyntaxNodeKind) : Parenthesizer := do if k == `missing then pure () else let p ← runForNodeKind parenthesizerAttribute k interpretParserDescr' p @[implementedBy parenthesizerForKindUnsafe] constant parenthesizerForKind (k : SyntaxNodeKind) : Parenthesizer @[combinatorParenthesizer Lean.Parser.withAntiquot] def withAntiquot.parenthesizer (antiP p : Parenthesizer) : Parenthesizer := -- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather -- fix the backtracking hack outright. orelse.parenthesizer antiP p @[combinatorParenthesizer Lean.Parser.withAntiquotSuffixSplice] def withAntiquotSuffixSplice.parenthesizer (k : SyntaxNodeKind) (p suffix : Parenthesizer) : Parenthesizer := do if (← getCur).isAntiquotSuffixSplice then visitArgs <| suffix *> p else p @[combinatorParenthesizer Lean.Parser.tokenWithAntiquot] def tokenWithAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do if (← getCur).isTokenAntiquot then visitArgs p else p def parenthesizeCategoryCore (cat : Name) (prec : Nat) : Parenthesizer := withReader (fun ctx => { ctx with cat := cat }) do let stx ← getCur if stx.getKind == `choice then visitArgs $ stx.getArgs.size.forM fun _ => do let stx ← getCur parenthesizerForKind stx.getKind else withAntiquot.parenthesizer (mkAntiquot.parenthesizer' cat.toString none) (parenthesizerForKind stx.getKind) modify fun st => { st with contCat := cat } @[combinatorParenthesizer Lean.Parser.categoryParser] def categoryParser.parenthesizer (cat : Name) (prec : Nat) : Parenthesizer := do let env ← getEnv match categoryParenthesizerAttribute.getValues env cat with | p::_ => p prec -- Fall back to the generic parenthesizer. -- In this case this node will never be parenthesized since we don't know which parentheses to use. | _ => parenthesizeCategoryCore cat prec @[combinatorParenthesizer Lean.Parser.categoryParserOfStack] def categoryParserOfStack.parenthesizer (offset : Nat) (prec : Nat) : Parenthesizer := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) categoryParser.parenthesizer stx.getId prec @[combinatorParenthesizer Lean.Parser.parserOfStack] def parserOfStack.parenthesizer (offset : Nat) (prec : Nat := 0) : Parenthesizer := do let st ← get let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset) parenthesizerForKind stx.getKind @[builtinCategoryParenthesizer term] def term.parenthesizer : CategoryParenthesizer | prec => do let stx ← getCur -- this can happen at `termParser <|> many1 commandParser` in `Term.stxQuot` if stx.getKind == nullKind then throwBacktrack else do maybeParenthesize `term true (fun stx => Unhygienic.run `(($stx))) prec $ parenthesizeCategoryCore `term prec @[builtinCategoryParenthesizer tactic] def tactic.parenthesizer : CategoryParenthesizer | prec => do maybeParenthesize `tactic false (fun stx => Unhygienic.run `(tactic|($stx))) prec $ parenthesizeCategoryCore `tactic prec @[builtinCategoryParenthesizer level] def level.parenthesizer : CategoryParenthesizer | prec => do maybeParenthesize `level false (fun stx => Unhygienic.run `(level|($stx))) prec $ parenthesizeCategoryCore `level prec @[combinatorParenthesizer Lean.Parser.error] def error.parenthesizer (msg : String) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.errorAtSavedPos] def errorAtSavedPos.parenthesizer (msg : String) (delta : Bool) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.atomic] def atomic.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.lookahead] def lookahead.parenthesizer (p : Parenthesizer) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.notFollowedBy] def notFollowedBy.parenthesizer (p : Parenthesizer) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.andthen] def andthen.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := p2 *> p1 @[combinatorParenthesizer Lean.Parser.node] def node.parenthesizer (k : SyntaxNodeKind) (p : Parenthesizer) : Parenthesizer := do let stx ← getCur if k != stx.getKind then trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'" -- HACK; see `orelse.parenthesizer` throwBacktrack visitArgs p @[combinatorParenthesizer Lean.Parser.checkPrec] def checkPrec.parenthesizer (prec : Nat) : Parenthesizer := addPrecCheck prec @[combinatorParenthesizer Lean.Parser.leadingNode] def leadingNode.parenthesizer (k : SyntaxNodeKind) (prec : Nat) (p : Parenthesizer) : Parenthesizer := do node.parenthesizer k p addPrecCheck prec -- Limit `cont` precedence to `maxPrec-1`. -- This is because `maxPrec-1` is the precedence of function application, which is the only way to turn a leading parser -- into a trailing one. modify fun st => { st with contPrec := Nat.min (Parser.maxPrec-1) prec } @[combinatorParenthesizer Lean.Parser.trailingNode] def trailingNode.parenthesizer (k : SyntaxNodeKind) (prec lhsPrec : Nat) (p : Parenthesizer) : Parenthesizer := do let stx ← getCur if k != stx.getKind then trace[PrettyPrinter.parenthesize.backtrack] "unexpected node kind '{stx.getKind}', expected '{k}'" -- HACK; see `orelse.parenthesizer` throwBacktrack visitArgs do p addPrecCheck prec let ctx ← read modify fun st => { st with contCat := ctx.cat } -- After visiting the nodes actually produced by the parser passed to `trailingNode`, we are positioned on the -- left-most child, which is the term injected by `trailingNode` in place of the recursion. Left recursion is not an -- issue for the parenthesizer, so we can think of this child being produced by `termParser lhsPrec`, or whichever Pratt -- parser is calling us. categoryParser.parenthesizer ctx.cat lhsPrec @[combinatorParenthesizer Lean.Parser.rawCh] def rawCh.parenthesizer (ch : Char) := visitToken @[combinatorParenthesizer Lean.Parser.symbolNoAntiquot] def symbolNoAntiquot.parenthesizer (sym : String) := visitToken @[combinatorParenthesizer Lean.Parser.unicodeSymbolNoAntiquot] def unicodeSymbolNoAntiquot.parenthesizer (sym asciiSym : String) := visitToken @[combinatorParenthesizer Lean.Parser.identNoAntiquot] def identNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.rawIdentNoAntiquot] def rawIdentNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.identEq] def identEq.parenthesizer (id : Name) := visitToken @[combinatorParenthesizer Lean.Parser.nonReservedSymbolNoAntiquot] def nonReservedSymbolNoAntiquot.parenthesizer (sym : String) (includeIdent : Bool) := visitToken @[combinatorParenthesizer Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.scientificLitNoAntiquot] def scientificLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.fieldIdx] def fieldIdx.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.manyNoAntiquot] def manyNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do let stx ← getCur visitArgs $ stx.getArgs.size.forM fun _ => p @[combinatorParenthesizer Lean.Parser.many1NoAntiquot] def many1NoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do manyNoAntiquot.parenthesizer p @[combinatorParenthesizer Lean.Parser.many1Unbox] def many1Unbox.parenthesizer (p : Parenthesizer) : Parenthesizer := do let stx ← getCur if stx.getKind == nullKind then manyNoAntiquot.parenthesizer p else p @[combinatorParenthesizer Lean.Parser.optionalNoAntiquot] def optionalNoAntiquot.parenthesizer (p : Parenthesizer) : Parenthesizer := do visitArgs p @[combinatorParenthesizer Lean.Parser.sepByNoAntiquot] def sepByNoAntiquot.parenthesizer (p pSep : Parenthesizer) : Parenthesizer := do let stx ← getCur visitArgs $ (List.range stx.getArgs.size).reverse.forM $ fun i => if i % 2 == 0 then p else pSep @[combinatorParenthesizer Lean.Parser.sepBy1NoAntiquot] def sepBy1NoAntiquot.parenthesizer := sepByNoAntiquot.parenthesizer @[combinatorParenthesizer Lean.Parser.withPosition] def withPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := do -- We assume the formatter will indent syntax sufficiently such that parenthesizing a `withPosition` node is never necessary modify fun st => { st with contPrec := none } p @[combinatorParenthesizer Lean.Parser.withoutPosition] def withoutPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.withForbidden] def withForbidden.parenthesizer (tk : Parser.Token) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.withoutForbidden] def withoutForbidden.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.withoutInfo] def withoutInfo.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.setExpected] def setExpected.parenthesizer (expected : List String) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.incQuotDepth] def incQuotDepth.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.decQuotDepth] def decQuotDepth.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.suppressInsideQuot] def suppressInsideQuot.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.evalInsideQuot] def evalInsideQuot.parenthesizer (declName : Name) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.checkStackTop] def checkStackTop.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkWsBefore] def checkWsBefore.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkNoWsBefore] def checkNoWsBefore.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkLinebreakBefore] def checkLinebreakBefore.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkTailWs] def checkTailWs.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkColGe] def checkColGe.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkColGt] def checkColGt.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkLineEq] def checkLineEq.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.eoi] def eoi.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.notFollowedByCategoryToken] def notFollowedByCategoryToken.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkInsideQuot] def checkInsideQuot.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkOutsideQuot] def checkOutsideQuot.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.skip] def skip.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.pushNone] def pushNone.parenthesizer : Parenthesizer := goLeft @[combinatorParenthesizer Lean.Parser.withOpenDecl] def withOpenDecl.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.withOpen] def withOpen.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.interpolatedStr] def interpolatedStr.parenthesizer (p : Parenthesizer) : Parenthesizer := do visitArgs $ (← getCur).getArgs.reverse.forM fun chunk => if chunk.isOfKind interpolatedStrLitKind then goLeft else p @[combinatorParenthesizer Lean.Parser.dbgTraceState] def dbgTraceState.parenthesizer (label : String) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer ite, macroInline] def ite {α : Type} (c : Prop) [h : Decidable c] (t e : Parenthesizer) : Parenthesizer := if c then t else e open Parser abbrev ParenthesizerAliasValue := AliasValue Parenthesizer builtin_initialize parenthesizerAliasesRef : IO.Ref (NameMap ParenthesizerAliasValue) ← IO.mkRef {} def registerAlias (aliasName : Name) (v : ParenthesizerAliasValue) : IO Unit := do Parser.registerAliasCore parenthesizerAliasesRef aliasName v instance : Coe Parenthesizer ParenthesizerAliasValue := { coe := AliasValue.const } instance : Coe (Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.unary } instance : Coe (Parenthesizer → Parenthesizer → Parenthesizer) ParenthesizerAliasValue := { coe := AliasValue.binary } end Parenthesizer open Parenthesizer /-- Add necessary parentheses in `stx` parsed by `parser`. -/ def parenthesize (parenthesizer : Parenthesizer) (stx : Syntax) : CoreM Syntax := do trace[PrettyPrinter.parenthesize.input] "{format stx}" catchInternalId backtrackExceptionId (do let (_, st) ← (parenthesizer {}).run { stxTrav := Syntax.Traverser.fromSyntax stx } pure st.stxTrav.cur) (fun _ => throwError "parenthesize: uncaught backtrack exception") def parenthesizeTerm := parenthesize $ categoryParser.parenthesizer `term 0 def parenthesizeCommand := parenthesize $ categoryParser.parenthesizer `command 0 builtin_initialize registerTraceClass `PrettyPrinter.parenthesize end PrettyPrinter end Lean
76ca9400c0134d7aa0131ea878b73c102ec72b57
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/topology/sheaves/local_predicate.lean
3d18685db34fe2d6680258c20d3ef7414339bd15
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,760
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison, Adam Topaz -/ import topology.sheaves.sheaf_of_functions import topology.sheaves.stalks import topology.sheaves.sheaf_condition.unique_gluing /-! # Functions satisfying a local predicate form a sheaf. At this stage, in `topology/sheaves/sheaf_of_functions.lean` we've proved that not-necessarily-continuous functions from a topological space into some type (or type family) form a sheaf. Why do the continuous functions form a sheaf? The point is just that continuity is a local condition, so one can use the lifting condition for functions to provide a candidate lift, then verify that the lift is actually continuous by using the factorisation condition for the lift (which guarantees that on each open set it agrees with the functions being lifted, which were assumed to be continuous). This file abstracts this argument to work for any collection of dependent functions on a topological space satisfying a "local predicate". As an application, we check that continuity is a local predicate in this sense, and provide * `Top.sheaf_condition.to_Top`: continuous functions into a topological space form a sheaf A sheaf constructed in this way has a natural map `stalk_to_fiber` from the stalks to the types in the ambient type family. We give conditions sufficient to show that this map is injective and/or surjective. -/ universe v noncomputable theory variables {X : Top.{v}} variables (T : X → Type v) open topological_space open opposite open category_theory open category_theory.limits open category_theory.limits.types namespace Top /-- Given a topological space `X : Top` and a type family `T : X → Type`, a `P : prelocal_predicate T` consists of: * a family of predicates `P.pred`, one for each `U : opens X`, of the form `(Π x : U, T x) → Prop` * a proof that if `f : Π x : V, T x` satisfies the predicate on `V : opens X`, then the restriction of `f` to any open subset `U` also satisfies the predicate. -/ structure prelocal_predicate := (pred : Π {U : opens X}, (Π x : U, T x) → Prop) (res : ∀ {U V : opens X} (i : U ⟶ V) (f : Π x : V, T x) (h : pred f), pred (λ x : U, f (i x))) variables (X) /-- Continuity is a "prelocal" predicate on functions to a fixed topological space `T`. -/ @[simps] def continuous_prelocal (T : Top.{v}) : prelocal_predicate (λ x : X, T) := { pred := λ U f, continuous f, res := λ U V i f h, continuous.comp h (opens.open_embedding_of_le i.le).continuous, } /-- Satisfying the inhabited linter. -/ instance inhabited_prelocal_predicate (T : Top.{v}) : inhabited (prelocal_predicate (λ x : X, T)) := ⟨continuous_prelocal X T⟩ variables {X} /-- Given a topological space `X : Top` and a type family `T : X → Type`, a `P : local_predicate T` consists of: * a family of predicates `P.pred`, one for each `U : opens X`, of the form `(Π x : U, T x) → Prop` * a proof that if `f : Π x : V, T x` satisfies the predicate on `V : opens X`, then the restriction of `f` to any open subset `U` also satisfies the predicate, and * a proof that given some `f : Π x : U, T x`, if for every `x : U` we can find an open set `x ∈ V ≤ U` so that the restriction of `f` to `V` satisfies the predicate, then `f` itself satisfies the predicate. -/ structure local_predicate extends prelocal_predicate T := (locality : ∀ {U : opens X} (f : Π x : U, T x) (w : ∀ x : U, ∃ (V : opens X) (m : x.1 ∈ V) (i : V ⟶ U), pred (λ x : V, f (i x : U))), pred f) variables (X) /-- Continuity is a "local" predicate on functions to a fixed topological space `T`. -/ def continuous_local (T : Top.{v}) : local_predicate (λ x : X, T) := { locality := λ U f w, begin apply continuous_iff_continuous_at.2, intro x, specialize w x, rcases w with ⟨V, m, i, w⟩, dsimp at w, rw continuous_iff_continuous_at at w, specialize w ⟨x, m⟩, simpa using (opens.open_embedding_of_le i.le).continuous_at_iff.1 w, end, ..continuous_prelocal X T } /-- Satisfying the inhabited linter. -/ instance inhabited_local_predicate (T : Top.{v}) : inhabited (local_predicate _) := ⟨continuous_local X T⟩ variables {X T} /-- Given a `P : prelocal_predicate`, we can always construct a `local_predicate` by asking that the condition from `P` holds locally near every point. -/ def prelocal_predicate.sheafify {T : X → Type v} (P : prelocal_predicate T) : local_predicate T := { pred := λ U f, ∀ x : U, ∃ (V : opens X) (m : x.1 ∈ V) (i : V ⟶ U), P.pred (λ x : V, f (i x : U)), res := λ V U i f w x, begin specialize w (i x), rcases w with ⟨V', m', i', p⟩, refine ⟨V ⊓ V', ⟨x.2,m'⟩, opens.inf_le_left _ _, _⟩, convert P.res (opens.inf_le_right V V') _ p, end, locality := λ U f w x, begin specialize w x, rcases w with ⟨V, m, i, p⟩, specialize p ⟨x.1, m⟩, rcases p with ⟨V', m', i', p'⟩, exact ⟨V', m', i' ≫ i, p'⟩, end } lemma prelocal_predicate.sheafify_of {T : X → Type v} {P : prelocal_predicate T} {U : opens X} {f : Π x : U, T x} (h : P.pred f) : P.sheafify.pred f := λ x, ⟨U, x.2, 𝟙 _, by { convert h, ext ⟨y, w⟩, refl, }⟩ /-- The subpresheaf of dependent functions on `X` satisfying the "pre-local" predicate `P`. -/ @[simps] def subpresheaf_to_Types (P : prelocal_predicate T) : presheaf (Type v) X := { obj := λ U, { f : Π x : unop U, T x // P.pred f }, map := λ U V i f, ⟨λ x, f.1 (i.unop x), P.res i.unop f.1 f.2⟩ }. namespace subpresheaf_to_Types variables (P : prelocal_predicate T) /-- The natural transformation including the subpresheaf of functions satisfying a local predicate into the presheaf of all functions. -/ def subtype : subpresheaf_to_Types P ⟶ presheaf_to_Types X T := { app := λ U f, f.1 } open Top.presheaf /-- The functions satisfying a local predicate satisfy the sheaf condition. -/ def sheaf_condition (P : local_predicate T) : sheaf_condition (subpresheaf_to_Types P.to_prelocal_predicate) := sheaf_condition_of_exists_unique_gluing _ $ λ ι U sf sf_comp, begin -- We show the sheaf condition in terms of unique gluing. -- First we obtain a family of sections for the underlying sheaf of functions, -- by forgetting that the prediacte holds let sf' : Π i : ι, (presheaf_to_Types X T).obj (op (U i)) := λ i, (sf i).val, -- Since our original family is compatible, this one is as well have sf'_comp : (presheaf_to_Types X T).is_compatible U sf' := λ i j, congr_arg subtype.val (sf_comp i j), -- So, we can obtain a unique gluing obtain ⟨gl,gl_spec,gl_uniq⟩ := (sheaf_to_Types X T).exists_unique_gluing U sf' sf'_comp, refine ⟨⟨gl,_⟩,_,_⟩, { -- Our first goal is to show that this chosen gluing satisfies the -- predicate. Of course, we use locality of the predicate. apply P.locality, rintros ⟨x, mem⟩, -- Once we're at a particular point `x`, we can select some open set `x ∈ U i`. choose i hi using opens.mem_supr.mp mem, -- We claim that the predicate holds in `U i` use [U i, hi, opens.le_supr U i], -- This follows, since our original family `sf` satisfies the predicate convert (sf i).property, exact gl_spec i }, -- It remains to show that the chosen lift is really a gluing for the subsheaf and -- that it is unique. Both of which follow immediately from the corresponding facts -- in the sheaf of functions without the local predicate. { intro i, ext1, exact (gl_spec i) }, { intros gl' hgl', ext1, exact gl_uniq gl'.1 (λ i, congr_arg subtype.val (hgl' i)) }, end end subpresheaf_to_Types /-- The subsheaf of the sheaf of all dependently typed functions satisfying the local predicate `P`. -/ @[simps] def subsheaf_to_Types (P : local_predicate T) : sheaf (Type v) X := { presheaf := subpresheaf_to_Types P.to_prelocal_predicate, sheaf_condition := subpresheaf_to_Types.sheaf_condition P }. /-- There is a canonical map from the stalk to the original fiber, given by evaluating sections. -/ def stalk_to_fiber (P : local_predicate T) (x : X) : (subsheaf_to_Types P).presheaf.stalk x ⟶ T x := begin refine colimit.desc _ { X := T x, ι := { app := λ U f, _, naturality' := _ } }, { exact f.1 ⟨x, (unop U).2⟩, }, { tidy, } end @[simp] lemma stalk_to_fiber_germ (P : local_predicate T) (U : opens X) (x : U) (f) : stalk_to_fiber P x ((subsheaf_to_Types P).presheaf.germ x f) = f.1 x := begin dsimp [presheaf.germ, stalk_to_fiber], cases x, simp, refl, end /-- The `stalk_to_fiber` map is surjective at `x` if every point in the fiber `T x` has an allowed section passing through it. -/ lemma stalk_to_fiber_surjective (P : local_predicate T) (x : X) (w : ∀ (t : T x), ∃ (U : open_nhds x) (f : Π y : U.1, T y) (h : P.pred f), f ⟨x, U.2⟩ = t) : function.surjective (stalk_to_fiber P x) := λ t, begin rcases w t with ⟨U, f, h, rfl⟩, fsplit, { exact (subsheaf_to_Types P).presheaf.germ ⟨x, U.2⟩ ⟨f, h⟩, }, { exact stalk_to_fiber_germ _ U.1 ⟨x, U.2⟩ ⟨f, h⟩, } end /-- The `stalk_to_fiber` map is injective at `x` if any two allowed sections which agree at `x` agree on some neighborhood of `x`. -/ lemma stalk_to_fiber_injective (P : local_predicate T) (x : X) (w : ∀ (U V : open_nhds x) (fU : Π y : U.1, T y) (hU : P.pred fU) (fV : Π y : V.1, T y) (hV : P.pred fV) (e : fU ⟨x, U.2⟩ = fV ⟨x, V.2⟩), ∃ (W : open_nhds x) (iU : W ⟶ U) (iV : W ⟶ V), ∀ (w : W.1), fU (iU w : U.1) = fV (iV w : V.1)) : function.injective (stalk_to_fiber P x) := λ tU tV h, begin -- We promise to provide all the ingredients of the proof later: let Q : ∃ (W : (open_nhds x)ᵒᵖ) (s : Π w : (unop W).1, T w) (hW : P.pred s), tU = (subsheaf_to_Types P).presheaf.germ ⟨x, (unop W).2⟩ ⟨s, hW⟩ ∧ tV = (subsheaf_to_Types P).presheaf.germ ⟨x, (unop W).2⟩ ⟨s, hW⟩ := _, { choose W s hW e using Q, exact e.1.trans e.2.symm, }, -- Then use induction to pick particular representatives of `tU tV : stalk x` obtain ⟨U, ⟨fU, hU⟩, rfl⟩ := jointly_surjective' tU, obtain ⟨V, ⟨fV, hV⟩, rfl⟩ := jointly_surjective' tV, { -- Decompose everything into its constituent parts: dsimp, simp only [stalk_to_fiber, types.colimit.ι_desc_apply] at h, specialize w (unop U) (unop V) fU hU fV hV h, rcases w with ⟨W, iU, iV, w⟩, -- and put it back together again in the correct order. refine ⟨(op W), (λ w, fU (iU w : (unop U).1)), P.res _ _ hU, _⟩, rcases W with ⟨W, m⟩, exact ⟨colimit_sound iU.op (subtype.eq rfl), colimit_sound iV.op (subtype.eq (funext w).symm)⟩, }, end /-- Some repackaging: the presheaf of functions satisfying `continuous_prelocal` is just the same thing as the presheaf of continuous functions. -/ def subpresheaf_continuous_prelocal_iso_presheaf_to_Top (T : Top.{v}) : subpresheaf_to_Types (continuous_prelocal X T) ≅ presheaf_to_Top X T := nat_iso.of_components (λ X, { hom := by { rintro ⟨f, c⟩, exact ⟨f, c⟩, }, inv := by { rintro ⟨f, c⟩, exact ⟨f, c⟩, }, hom_inv_id' := by { ext ⟨f, p⟩ x, refl, }, inv_hom_id' := by { ext ⟨f, p⟩ x, refl, }, }) (by tidy) /-- The sheaf of continuous functions on `X` with values in a space `T`. -/ def sheaf_to_Top (T : Top.{v}) : sheaf (Type v) X := { presheaf := presheaf_to_Top X T, sheaf_condition := presheaf.sheaf_condition_equiv_of_iso (subpresheaf_continuous_prelocal_iso_presheaf_to_Top T) (subpresheaf_to_Types.sheaf_condition (continuous_local X T)), } end Top
1d63ea380c07adba68206f8dbe76a0a9348412d0
e031d1fbf8353b338e3189e0d9aec3adb5bb0512
/src/subuniverse.lean
3a198c316802a66e21a85f7ff372980c2fad78ca
[ "Apache-2.0" ]
permissive
UniversalAlgebra/lean-ualib
e64431a70007a835b1dd933d66be04ffca118601
ab9cbddbb5bdf1eeac4b0d5994bd6cad2a3665d4
refs/heads/master
1,584,931,281,084
1,558,364,533,000
1,558,364,533,000
140,986,567
6
0
Apache-2.0
1,532,718,578,000
1,531,613,794,000
Lean
UTF-8
Lean
false
false
4,750
lean
import basic import data.set namespace subuniverse section parameters {α : Type*} {S : signature} (A : algebra_on S α) {I ζ : Type} {R : I → set α} open set def Sub (β : set α) : Prop := ∀ f (a : S.ρ f → α), (∀ x, a x ∈ β) → A f a ∈ β -- N.B. A f a ∈ β is notation for β (A f a) def Sg (X : set α) : set α := ⋂₀ {U | Sub U ∧ X ⊆ U} def index_of_sub_above_X (X : set α) (C : I → set α) : I → Prop := λ i, Sub (C i) ∧ X ⊆ (C i) theorem mem_Inter_of_mem {s : I → set α} : ∀ x, (∀ i, x ∈ s i) → (x ∈ ⋂ i, s i) := assume x h t ⟨a, (eq : t = s a)⟩, eq.symm ▸ h a theorem Inter.intro {x : α} (C : I → set α) (h : ∀ i, x ∈ C i) : x ∈ ⋂ i, C i := by simp; assumption theorem Inter.elim {x : α} (C : I → set α) (h : x ∈ ⋂ i, C i) (i : I) : x ∈ C i := by simp at h; apply h lemma sub_of_sub_inter_sub (C : I → set α) : (∀ i, Sub (C i)) → Sub ⋂i, C i := assume h : ∀ i, Sub (C i), show Sub (⋂i, C i), from assume f a (h1 : (∀ x, a x ∈ ⋂i, C i)), show A f a ∈ (⋂i, C i), from have h4 : ∀ j, A f a ∈ C j, from assume j : I, show A f a ∈ C j, from have h3 : ∀ x, a x ∈ (C j), from assume x, Inter.elim C (h1 x) j, (h j) f a h3, (mem_Inter_of_mem (A f a)) h4 --Inter.intro C h4 lemma subset_X_of_SgX (X : set α) : X ⊆ Sg X := assume x (h1 : x ∈ X), show x ∈ ⋂₀ {U | Sub U ∧ X ⊆ U}, from assume V (h2 : V ∈ {U | Sub U ∧ X ⊆ U}), show x ∈ V, from have h3: Sub V ∧ X ⊆ V, from h2, h3.right h1 lemma sInter_mem {X : set α} (x : α) : x ∈ Sg X → ∀ {R : set α }, Sub R → X ⊆ R → x ∈ R := (assume (h1 : x ∈ Sg X) (R : set α) (h2 : Sub R) (h3 : X ⊆ R), show x ∈ R, from h1 R (and.intro h2 h3)) -- Sg X is a Sub lemma sub_SgX (X : set α) : Sub (Sg X) := assume f a (h : ∀ i, a i ∈ Sg X), show A f a ∈ Sg X, from assume U (h1 : Sub U ∧ X ⊆ U), show A f a ∈ U, from have h3 : Sg X ⊆ U, from assume r (h4 : r ∈ Sg X), show r ∈ U, from sInter_mem r h4 h1.left h1.right, have h5 : ∀ i, a i ∈ U, from assume i, h3 (h i), (h1.left f a h5) lemma sInter_mem_of_mem {X : set α} (x : α) : x ∈ Sg X ↔ ∀ {R : set α }, Sub R → X ⊆ R → x ∈ R := iff.intro (assume (h1 : x ∈ Sg X) (R : set α) (h2 : Sub R) (h3 : X ⊆ R), show x ∈ R, from h1 R (and.intro h2 h3)) (assume (h1 : ∀ {R : set α}, Sub R → X ⊆ R → x ∈ R), show x∈ Sg X, from h1 (sub_SgX X) (subset_X_of_SgX X)) parameter (X : set α) -- One way to make this SEGFAULT lol -- Siva, do you get a segv here? -- I don't... inductive Y : set α | var (x : α) : x ∈ X → Y x | app (f : S.F) (a : S.ρ f → α) : (∀ i, Y (a i)) → Y (A f a) -- Y is a Sub lemma sub_Y : (Sub Y) := assume f a (h: ∀ i, Y (a i)), show Y (A f a), from Y.app f a h -- Y is the smallest Sub containing X lemma sub_min_Y (U : set α) : Sub U → X ⊆ U → Y ⊆ U := assume (h₁ : Sub U) (h₂ : X ⊆ U), assume (y : α) (p : Y y), show U y, from have q : Y y → Y y → U y, from Y.rec --base: y = x ∈ X ( assume y (h : X y) (h' : Y y), h₂ h ) --inductive: y = A f a for some a with ∀ i, a i ∈ Y ( assume f a (h : ∀ i, Y (a i)) (h' : ∀ i, Y (a i) → U (a i)) (h'' : Y (A f a)), have h₄ : ∀ i, a i ∈ U, from assume i, h' i (h i), show U (A f a), from h₁ f a h₄ ), q p p -- Y is the Sub generated by X theorem sg_inductive : Sg X = Y := have h' : X ⊆ Y, from assume x (h3 : x ∈ X), show x ∈ Y, from Y.var x h3, have h : Sub Y, from assume f a (h1 : ∀ x, Y (a x)), show Y (A f a), from Y.app f a h1, have l : Sg X ⊆ Y, from assume u (h3 : u ∈ Sg X), show u ∈ Y, from (sInter_mem u) h3 h h', have r : Y ⊆ Sg X, from assume a (p: a ∈ Y), show a ∈ Sg X, from have q : a ∈ Y → a ∈ Sg X, from Y.rec --base: a = x ∈ X ( assume x (h1 : x ∈ X), show x ∈ Sg X, from subset_X_of_SgX X h1 ) --inductive: a = A f b for some b with ∀ i, b i ∈ Sg X ( assume f b (h2 : ∀ i, b i ∈ Y) (h3 : ∀ i, b i ∈ Sg X), show A f b ∈ Sg X, from sub_SgX X f b h3 ), q p, subset.antisymm l r end end subuniverse -- Miscellaneous Notes -- ⋂₀ is notation for sInter (S : set (set α)) : set α := Inf S, -- and Inf S is defined as follows: -- Inf := λs, {a | ∀ t ∈ s, a ∈ t }, -- So, if S : set (set α) (i.e., a collection of sets of type α), -- then Inf S is the intersection of the sets in S.
e4656456affba6674d63f5dcae125321e98c46dc
4727251e0cd73359b15b664c3170e5d754078599
/src/data/int/parity.lean
476c0333879610a9d63e6785aea118869e25f7c0
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
7,496
lean
/- Copyright (c) 2019 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Benjamin Davidson -/ import data.nat.parity /-! # Parity of integers This file contains theorems about the `even` and `odd` predicates on the integers. ## Tags even, odd -/ namespace int variables {m n : ℤ} @[simp] theorem mod_two_ne_one : ¬ n % 2 = 1 ↔ n % 2 = 0 := by cases mod_two_eq_zero_or_one n with h h; simp [h] local attribute [simp] -- euclidean_domain.mod_eq_zero uses (2 ∣ n) as normal form theorem mod_two_ne_zero : ¬ n % 2 = 0 ↔ n % 2 = 1 := by cases mod_two_eq_zero_or_one n with h h; simp [h] theorem even_iff : even n ↔ n % 2 = 0 := ⟨λ ⟨m, hm⟩, by simp [← two_mul, hm], λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by simp [← two_mul, h])⟩⟩ theorem odd_iff : odd n ↔ n % 2 = 1 := ⟨λ ⟨m, hm⟩, by { rw [hm, add_mod], norm_num }, λ h, ⟨n / 2, (mod_add_div n 2).symm.trans (by { rw h, abel })⟩⟩ lemma not_even_iff : ¬ even n ↔ n % 2 = 1 := by rw [even_iff, mod_two_ne_zero] lemma not_odd_iff : ¬ odd n ↔ n % 2 = 0 := by rw [odd_iff, mod_two_ne_one] lemma even_iff_not_odd : even n ↔ ¬ odd n := by rw [not_odd_iff, even_iff] @[simp] lemma odd_iff_not_even : odd n ↔ ¬ even n := by rw [not_even_iff, odd_iff] lemma is_compl_even_odd : is_compl {n : ℤ | even n} {n | odd n} := by simp [← set.compl_set_of, is_compl_compl] lemma even_or_odd (n : ℤ) : even n ∨ odd n := or.imp_right odd_iff_not_even.2 $ em $ even n lemma even_or_odd' (n : ℤ) : ∃ k, n = 2 * k ∨ n = 2 * k + 1 := by simpa only [← two_mul, exists_or_distrib, ← odd, ← even] using even_or_odd n lemma even_xor_odd (n : ℤ) : xor (even n) (odd n) := begin cases even_or_odd n with h, { exact or.inl ⟨h, even_iff_not_odd.mp h⟩ }, { exact or.inr ⟨h, odd_iff_not_even.mp h⟩ }, end lemma even_xor_odd' (n : ℤ) : ∃ k, xor (n = 2 * k) (n = 2 * k + 1) := begin rcases even_or_odd n with ⟨k, rfl⟩ | ⟨k, rfl⟩; use k, { simpa only [← two_mul, xor, true_and, eq_self_iff_true, not_true, or_false, and_false] using (succ_ne_self (2*k)).symm }, { simp only [xor, add_right_eq_self, false_or, eq_self_iff_true, not_true, not_false_iff, one_ne_zero, and_self] }, end @[simp] theorem two_dvd_ne_zero : ¬ 2 ∣ n ↔ n % 2 = 1 := even_iff_two_dvd.symm.not.trans not_even_iff instance : decidable_pred (even : ℤ → Prop) := λ n, decidable_of_iff _ even_iff.symm instance : decidable_pred (odd : ℤ → Prop) := λ n, decidable_of_iff _ odd_iff_not_even.symm @[simp] theorem not_even_one : ¬ even (1 : ℤ) := by rw even_iff; norm_num @[parity_simps] theorem even_add : even (m + n) ↔ (even m ↔ even n) := by cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂, int.add_mod]; norm_num theorem even_add' : even (m + n) ↔ (odd m ↔ odd n) := by rw [even_add, even_iff_not_odd, even_iff_not_odd, not_iff_not] @[simp] theorem not_even_bit1 (n : ℤ) : ¬ even (bit1 n) := by simp [bit1] with parity_simps lemma two_not_dvd_two_mul_add_one (n : ℤ) : ¬(2 ∣ 2 * n + 1) := by { simp [add_mod], refl } @[parity_simps] theorem even_sub : even (m - n) ↔ (even m ↔ even n) := by simp [sub_eq_add_neg] with parity_simps theorem even_sub' : even (m - n) ↔ (odd m ↔ odd n) := by rw [even_sub, even_iff_not_odd, even_iff_not_odd, not_iff_not] @[parity_simps] theorem even_add_one : even (n + 1) ↔ ¬ even n := by simp [even_add] @[parity_simps] theorem even_mul : even (m * n) ↔ even m ∨ even n := by cases mod_two_eq_zero_or_one m with h₁ h₁; cases mod_two_eq_zero_or_one n with h₂ h₂; simp [even_iff, h₁, h₂, int.mul_mod]; norm_num theorem odd_mul : odd (m * n) ↔ odd m ∧ odd n := by simp [not_or_distrib] with parity_simps theorem odd.of_mul_left (h : odd (m * n)) : odd m := (odd_mul.mp h).1 theorem odd.of_mul_right (h : odd (m * n)) : odd n := (odd_mul.mp h).2 @[parity_simps] theorem even_pow {n : ℕ} : even (m ^ n) ↔ even m ∧ n ≠ 0 := by { induction n with n ih; simp [*, even_mul, pow_succ], tauto } theorem even_pow' {n : ℕ} (h : n ≠ 0) : even (m ^ n) ↔ even m := even_pow.trans $ and_iff_left h @[parity_simps] theorem odd_add : odd (m + n) ↔ (odd m ↔ even n) := by rw [odd_iff_not_even, even_add, not_iff, odd_iff_not_even] theorem odd_add' : odd (m + n) ↔ (odd n ↔ even m) := by rw [add_comm, odd_add] lemma ne_of_odd_add (h : odd (m + n)) : m ≠ n := λ hnot, by simpa [hnot] with parity_simps using h @[parity_simps] theorem odd_sub : odd (m - n) ↔ (odd m ↔ even n) := by rw [odd_iff_not_even, even_sub, not_iff, odd_iff_not_even] theorem odd_sub' : odd (m - n) ↔ (odd n ↔ even m) := by rw [odd_iff_not_even, even_sub, not_iff, not_iff_comm, odd_iff_not_even] lemma even_mul_succ_self (n : ℤ) : even (n * (n + 1)) := begin rw even_mul, convert n.even_or_odd, simp with parity_simps end @[simp, norm_cast] theorem even_coe_nat (n : ℕ) : even (n : ℤ) ↔ even n := by rw_mod_cast [even_iff, nat.even_iff] @[simp, norm_cast] theorem odd_coe_nat (n : ℕ) : odd (n : ℤ) ↔ odd n := by rw [odd_iff_not_even, nat.odd_iff_not_even, even_coe_nat] @[simp] theorem nat_abs_even : even n.nat_abs ↔ even n := by simp [even_iff_two_dvd, dvd_nat_abs, coe_nat_dvd_left.symm] @[simp] theorem nat_abs_odd : odd n.nat_abs ↔ odd n := by rw [odd_iff_not_even, nat.odd_iff_not_even, nat_abs_even] alias nat_abs_even ↔ _ even.nat_abs alias nat_abs_odd ↔ _ odd.nat_abs attribute [protected] even.nat_abs odd.nat_abs lemma four_dvd_add_or_sub_of_odd {a b : ℤ} (ha : odd a) (hb : odd b) : 4 ∣ a + b ∨ 4 ∣ a - b := begin obtain ⟨m, rfl⟩ := ha, obtain ⟨n, rfl⟩ := hb, obtain h|h := int.even_or_odd (m + n), { right, rw [int.even_add, ←int.even_sub] at h, obtain ⟨k, hk⟩ := h, convert dvd_mul_right 4 k, rw [eq_add_of_sub_eq hk, mul_add, add_assoc, add_sub_cancel, ← two_mul, ←mul_assoc], refl }, { left, obtain ⟨k, hk⟩ := h, convert dvd_mul_right 4 (k + 1), rw [eq_sub_of_add_eq hk, add_right_comm, ←add_sub, mul_add, mul_sub, add_assoc, add_assoc, sub_add, add_assoc, ←sub_sub (2 * n), sub_self, zero_sub, sub_neg_eq_add, ←mul_assoc, mul_add], refl }, end lemma two_mul_div_two_of_even : even n → 2 * (n / 2) = n := λ h, int.mul_div_cancel' (even_iff_two_dvd.mp h) lemma div_two_mul_two_of_even : even n → n / 2 * 2 = n := --int.div_mul_cancel λ h, int.div_mul_cancel (even_iff_two_dvd.mp h) lemma two_mul_div_two_add_one_of_odd : odd n → 2 * (n / 2) + 1 = n := by { rintro ⟨c, rfl⟩, rw mul_comm, convert int.div_add_mod' _ _, simpa [int.add_mod] } lemma div_two_mul_two_add_one_of_odd : odd n → n / 2 * 2 + 1 = n := by { rintro ⟨c, rfl⟩, convert int.div_add_mod' _ _, simpa [int.add_mod] } lemma add_one_div_two_mul_two_of_odd : odd n → 1 + n / 2 * 2 = n := by { rintro ⟨c, rfl⟩, rw add_comm, convert int.div_add_mod' _ _, simpa [int.add_mod] } lemma two_mul_div_two_of_odd (h : odd n) : 2 * (n / 2) = n - 1 := eq_sub_of_add_eq (two_mul_div_two_add_one_of_odd h) -- Here are examples of how `parity_simps` can be used with `int`. example (m n : ℤ) (h : even m) : ¬ even (n + 3) ↔ even (m^2 + m + n) := by simp [*, (dec_trivial : ¬ 2 = 0)] with parity_simps example : ¬ even (25394535 : ℤ) := by simp end int
7db8723b9277e3038541cebd5ca4e3168b6f3324
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/category_theory/grothendieck.lean
d1597ceb37473190b7356a8298dca542a48fdbba
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
4,704
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 category_theory.category.Cat import category_theory.elements /-! # The Grothendieck construction Given a functor `F : C ⥤ Cat`, the objects of `grothendieck F` consist of dependent pairs `(b, f)`, where `b : C` and `f : F.obj c`, and a morphism `(b, f) ⟶ (b', f')` is a pair `β : b ⟶ b'` in `C`, and `φ : (F.map β).obj f ⟶ f'` Categories such as `PresheafedSpace` are in fact examples of this construction, and it may be interesting to try to generalize some of the development there. ## Implementation notes Really we should treat `Cat` as a 2-category, and allow `F` to be a 2-functor. There is also a closely related construction starting with `G : Cᵒᵖ ⥤ Cat`, where morphisms consists again of `β : b ⟶ b'` and `φ : f ⟶ (F.map (op β)).obj f'`. ## References See also `category_theory.functor.elements` for the category of elements of functor `F : C ⥤ Type`. * https://stacks.math.columbia.edu/tag/02XV * https://ncatlab.org/nlab/show/Grothendieck+construction -/ universe u namespace category_theory variables {C D : Type*} [category C] [category D] variables (F : C ⥤ Cat) /-- The Grothendieck construction (often written as `∫ F` in mathematics) for a functor `F : C ⥤ Cat` gives a category whose * objects `X` consist of `X.base : C` and `X.fiber : F.obj base` * morphisms `f : X ⟶ Y` consist of `base : X.base ⟶ Y.base` and `f.fiber : (F.map base).obj X.fiber ⟶ Y.fiber` -/ @[nolint has_inhabited_instance] structure grothendieck := (base : C) (fiber : F.obj base) namespace grothendieck variables {F} /-- A morphism in the Grothendieck category `F : C ⥤ Cat` consists of `base : X.base ⟶ Y.base` and `f.fiber : (F.map base).obj X.fiber ⟶ Y.fiber`. -/ structure hom (X Y : grothendieck F) := (base : X.base ⟶ Y.base) (fiber : (F.map base).obj X.fiber ⟶ Y.fiber) @[ext] lemma ext {X Y : grothendieck F} (f g : hom X Y) (w_base : f.base = g.base) (w_fiber : eq_to_hom (by rw w_base) ≫ f.fiber = g.fiber) : f = g := begin cases f; cases g, congr, dsimp at w_base, induction w_base, refl, dsimp at w_base, induction w_base, simpa using w_fiber, end /-- The identity morphism in the Grothendieck category. -/ @[simps] def id (X : grothendieck F) : hom X X := { base := 𝟙 X.base, fiber := eq_to_hom (by erw [category_theory.functor.map_id, functor.id_obj X.fiber]), } instance (X : grothendieck F) : inhabited (hom X X) := ⟨id X⟩ /-- Composition of morphisms in the Grothendieck category. -/ @[simps] def comp {X Y Z : grothendieck F} (f : hom X Y) (g : hom Y Z) : hom X Z := { base := f.base ≫ g.base, fiber := eq_to_hom (by erw [functor.map_comp, functor.comp_obj]) ≫ (F.map g.base).map f.fiber ≫ g.fiber, } instance : category (grothendieck F) := { hom := λ X Y, grothendieck.hom X Y, id := λ X, grothendieck.id X, comp := λ X Y Z f g, grothendieck.comp f g, comp_id' := λ X Y f, begin ext, { dsimp, -- We need to turn `F.map_id` (which is an equation between functors) -- into a natural isomorphism. rw ← nat_iso.naturality_2 (eq_to_iso (F.map_id Y.base)) f.fiber, simp, refl, }, { simp, }, end, id_comp' := λ X Y f, by ext; simp, assoc' := λ W X Y Z f g h, begin ext, swap, { simp, }, { dsimp, rw ← nat_iso.naturality_2 (eq_to_iso (F.map_comp _ _)) f.fiber, simp, refl, }, end, } section variables (F) /-- The forgetful functor from `grothendieck F` to the source category. -/ @[simps] def forget : grothendieck F ⥤ C := { obj := λ X, X.1, map := λ X Y f, f.1, } end universe w variables (G : C ⥤ Type w) /-- The Grothendieck construction applied to a functor to `Type` (thought of as a functor to `Cat` by realising a type as a discrete category) is the same as the 'category of elements' construction. -/ def grothendieck_Type_to_Cat : grothendieck (G ⋙ Type_to_Cat) ≌ G.elements := { functor := { obj := λ X, ⟨X.1, X.2⟩, map := λ X Y f, ⟨f.1, f.2.1.1⟩ }, inverse := { obj := λ X, ⟨X.1, X.2⟩, map := λ X Y f, ⟨f.1, ⟨⟨f.2⟩⟩⟩ }, unit_iso := nat_iso.of_components (λ X, by { cases X, exact iso.refl _, }) (by { rintro ⟨⟩ ⟨⟩ ⟨base, ⟨⟨f⟩⟩⟩, dsimp at *, subst f, simp, }), counit_iso := nat_iso.of_components (λ X, by { cases X, exact iso.refl _, }) (by { rintro ⟨⟩ ⟨⟩ ⟨f, e⟩, dsimp at *, subst e, simp }), functor_unit_iso_comp' := by { rintro ⟨⟩, dsimp, simp, refl, } } end grothendieck end category_theory
d28ba16df60da17105ad1b48e47e772c026d2503
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/cc_lemmas_auto.lean
816fe866ebd457d2a838a6f7d8d407f7406c0bc6
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
5,243
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.propext import Mathlib.Lean3Lib.init.classical universes u namespace Mathlib /- Lemmas use by the congruence closure module -/ theorem iff_eq_of_eq_true_left {a : Prop} {b : Prop} (h : a = True) : (a ↔ b) = b := Eq.symm h ▸ propext (true_iff b) theorem iff_eq_of_eq_true_right {a : Prop} {b : Prop} (h : b = True) : (a ↔ b) = a := Eq.symm h ▸ propext (iff_true a) theorem iff_eq_true_of_eq {a : Prop} {b : Prop} (h : a = b) : (a ↔ b) = True := h ▸ propext (iff_self a) theorem and_eq_of_eq_true_left {a : Prop} {b : Prop} (h : a = True) : (a ∧ b) = b := Eq.symm h ▸ propext (true_and b) theorem and_eq_of_eq_true_right {a : Prop} {b : Prop} (h : b = True) : (a ∧ b) = a := Eq.symm h ▸ propext (and_true a) theorem and_eq_of_eq_false_left {a : Prop} {b : Prop} (h : a = False) : (a ∧ b) = False := Eq.symm h ▸ propext (false_and b) theorem and_eq_of_eq_false_right {a : Prop} {b : Prop} (h : b = False) : (a ∧ b) = False := Eq.symm h ▸ propext (and_false a) theorem and_eq_of_eq {a : Prop} {b : Prop} (h : a = b) : (a ∧ b) = a := h ▸ propext (and_self a) theorem or_eq_of_eq_true_left {a : Prop} {b : Prop} (h : a = True) : (a ∨ b) = True := Eq.symm h ▸ propext (true_or b) theorem or_eq_of_eq_true_right {a : Prop} {b : Prop} (h : b = True) : (a ∨ b) = True := Eq.symm h ▸ propext (or_true a) theorem or_eq_of_eq_false_left {a : Prop} {b : Prop} (h : a = False) : (a ∨ b) = b := Eq.symm h ▸ propext (false_or b) theorem or_eq_of_eq_false_right {a : Prop} {b : Prop} (h : b = False) : (a ∨ b) = a := Eq.symm h ▸ propext (or_false a) theorem or_eq_of_eq {a : Prop} {b : Prop} (h : a = b) : (a ∨ b) = a := h ▸ propext (or_self a) theorem imp_eq_of_eq_true_left {a : Prop} {b : Prop} (h : a = True) : (a → b) = b := Eq.symm h ▸ propext { mp := fun (h : True → b) => h trivial, mpr := fun (h₁ : b) (h₂ : True) => h₁ } theorem imp_eq_of_eq_true_right {a : Prop} {b : Prop} (h : b = True) : (a → b) = True := Eq.symm h ▸ propext { mp := fun (h : a → True) => trivial, mpr := fun (h₁ : True) (h₂ : a) => h₁ } theorem imp_eq_of_eq_false_left {a : Prop} {b : Prop} (h : a = False) : (a → b) = True := Eq.symm h ▸ propext { mp := fun (h : False → b) => trivial, mpr := fun (h₁ : True) (h₂ : False) => false.elim h₂ } theorem imp_eq_of_eq_false_right {a : Prop} {b : Prop} (h : b = False) : (a → b) = (¬a) := Eq.symm h ▸ propext { mp := fun (h : a → False) => h, mpr := fun (hna : ¬a) (ha : a) => hna ha } /- Remark: the congruence closure module will only use the following lemma is cc_config.em is tt. -/ theorem not_imp_eq_of_eq_false_right {a : Prop} {b : Prop} (h : b = False) : (¬a → b) = a := sorry theorem imp_eq_true_of_eq {a : Prop} {b : Prop} (h : a = b) : (a → b) = True := h ▸ propext { mp := fun (h : a → a) => trivial, mpr := fun (h : True) (ha : a) => ha } theorem not_eq_of_eq_true {a : Prop} (h : a = True) : (¬a) = False := Eq.symm h ▸ propext not_true_iff theorem not_eq_of_eq_false {a : Prop} (h : a = False) : (¬a) = True := Eq.symm h ▸ propext not_false_iff theorem false_of_a_eq_not_a {a : Prop} (h : a = (¬a)) : False := (fun (this : ¬a) => absurd (eq.mpr h this) this) fun (ha : a) => absurd ha (eq.mp h ha) theorem if_eq_of_eq_true {c : Prop} [d : Decidable c] {α : Sort u} (t : α) (e : α) (h : c = True) : ite c t e = t := if_pos (of_eq_true h) theorem if_eq_of_eq_false {c : Prop} [d : Decidable c] {α : Sort u} (t : α) (e : α) (h : c = False) : ite c t e = e := if_neg (not_of_eq_false h) theorem if_eq_of_eq (c : Prop) [d : Decidable c] {α : Sort u} {t : α} {e : α} (h : t = e) : ite c t e = t := sorry theorem eq_true_of_and_eq_true_left {a : Prop} {b : Prop} (h : (a ∧ b) = True) : a = True := eq_true_intro (and.left (of_eq_true h)) theorem eq_true_of_and_eq_true_right {a : Prop} {b : Prop} (h : (a ∧ b) = True) : b = True := eq_true_intro (and.right (of_eq_true h)) theorem eq_false_of_or_eq_false_left {a : Prop} {b : Prop} (h : (a ∨ b) = False) : a = False := eq_false_intro fun (ha : a) => false.elim (eq.mp h (Or.inl ha)) theorem eq_false_of_or_eq_false_right {a : Prop} {b : Prop} (h : (a ∨ b) = False) : b = False := eq_false_intro fun (hb : b) => false.elim (eq.mp h (Or.inr hb)) theorem eq_false_of_not_eq_true {a : Prop} (h : (¬a) = True) : a = False := eq_false_intro fun (ha : a) => absurd ha (eq.mpr h trivial) /- Remark: the congruence closure module will only use the following lemma is cc_config.em is tt. -/ theorem eq_true_of_not_eq_false {a : Prop} (h : (¬a) = False) : a = True := eq_true_intro (classical.by_contradiction fun (hna : ¬a) => eq.mp h hna) theorem ne_of_eq_of_ne {α : Sort u} {a : α} {b : α} {c : α} (h₁ : a = b) (h₂ : b ≠ c) : a ≠ c := Eq.symm h₁ ▸ h₂ theorem ne_of_ne_of_eq {α : Sort u} {a : α} {b : α} {c : α} (h₁ : a ≠ b) (h₂ : b = c) : a ≠ c := h₂ ▸ h₁ end Mathlib
51b4356b731907f9380713829adcf514f8e8fdee
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch3/ex0702.lean
75d64bb69f4f30d1023ea1597e9ab412815b5b40
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
1,209
lean
open classical variables p q r s : Prop example : (p → r ∨ s) → ((p → r) ∨ (p → s)) := assume h, by_cases (assume hp, (h hp).elim (assume hr, or.inl (λ hp, hr)) (assume hr, or.inr (λ hp, hr))) (assume hnp, or.inl (λ hp, absurd hp hnp)) example : ¬(p ∧ q) → ¬p ∨ ¬q := assume h, by_cases (assume hp, suffices hnq : ¬q, from or.inr hnq, assume hq, h ⟨hp, hq⟩) (assume hnp, or.inl hnp) example : ¬(p → q) → p ∧ ¬q := assume h, by_cases (assume hp, suffices hnq : ¬q, from ⟨hp, hnq⟩, assume hq, h (λ hp, hq)) (assume hnp, suffices hp2q : p → q, from false.elim (h hp2q), assume hp, absurd hp hnp) example : (p → q) → (¬p ∨ q) := assume hp2q, by_cases (assume hp, or.inr (hp2q hp)) (assume hnp, or.inl hnp) example : (¬q → ¬p) → (p → q) := assume hnq2np hp, by_cases id (assume hnq, absurd hp (hnq2np hnq)) example : p ∨ ¬p := em p example : (((p → q) → p) → p) := assume h, by_contradiction (assume hnp, have hp : p, from h (λ hp, absurd hp hnp), hnp hp)
73074a10a0ad2c3b71b20f67fbe4a04c302b356d
4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1
/src/o_minimal/sheaf/basic.lean
8dd71e19b38b8d95521689bc14dc9b5546146823
[]
no_license
rwbarton/lean-omin
da209ed061d64db65a8f7f71f198064986f30eb9
fd733c6d95ef6f4743aae97de5e15df79877c00e
refs/heads/master
1,674,408,673,325
1,607,343,535,000
1,607,343,535,000
285,150,399
9
0
null
null
null
null
UTF-8
Lean
false
false
3,930
lean
import o_minimal.Def /- Definable sheaves. Here we provide just enough definitions, instances and lemmas to set up the tactic environment. The eventual frontend for all this is intended to consist of `definable` and the `definable_sheaf` class, but there is still plenty of construction to do. -/ namespace o_minimal universe u variables {R : Type u} (S : struc R) class definable_sheaf (X : Type*) := (definable : Π {K : Def S}, (K → X) → Prop) (definable_precomp : ∀ {L K : Def S} (φ : L ⟶ K) (f : K → X), definable f → definable (f ∘ φ)) (definable_cover : ∀ {K : Def S} (f : K → X) (𝓛 : Def.cover K), (∀ i, definable (f ∘ 𝓛.map i)) → definable f) namespace definable_sheaf variables {S} def rep {W : Type*} [has_coordinates R W] [is_definable S W] : definable_sheaf S W := { definable := λ K f, def_fun S f, definable_precomp := λ L K φ f hf, hf.comp φ.is_definable, definable_cover := λ K f 𝓛 h, Def.subcanonical 𝓛 f h } instance Def.definable_sheaf {X : Def S} : definable_sheaf S X := definable_sheaf.rep variables {X Y : Type*} [definable_sheaf S X] [definable_sheaf S Y] instance prod.definable_sheaf : definable_sheaf S (X × Y) := { definable := λ K f, definable_sheaf.definable (prod.fst ∘ f) ∧ definable_sheaf.definable (prod.snd ∘ f), definable_precomp := λ L K φ _ h, ⟨definable_sheaf.definable_precomp φ _ h.1, definable_sheaf.definable_precomp φ _ h.2⟩, definable_cover := λ K f 𝓛 h, ⟨definable_sheaf.definable_cover _ 𝓛 (λ i, (h i).1), definable_sheaf.definable_cover _ 𝓛 (λ i, (h i).2)⟩ } instance fun.definable_sheaf : definable_sheaf S (X → Y) := { definable := λ K f, ∀ (L : Def S) (g : L → K × X) (h : definable_sheaf.definable g), definable_sheaf.definable (function.uncurry f ∘ g), definable_precomp := λ L K φ f hf M g hg, hf M (λ m, (φ (g m).1, (g m).2)) ⟨definable_sheaf.definable_precomp ⟨λ m, (g m).1, hg.1⟩ φ φ.is_definable, hg.2⟩, definable_cover := λ K f 𝓛 h K' g hg, begin let g₁ : K' ⟶ K := ⟨λ k', (g k').1, hg.1⟩, let 𝓛' := 𝓛.pullback g₁, apply definable_sheaf.definable_cover _ 𝓛', intro i, specialize h i (𝓛'.obj i) (λ l', (Def.pullback.π₂ g₁ _ l', (g (𝓛'.map i l')).2)) ⟨(Def.pullback.π₂ g₁ _).is_definable, definable_sheaf.definable_precomp _ _ hg.2⟩, dsimp only [function.uncurry, function.comp] at ⊢ h, convert h, ext x, congr, exact x.property.snd.snd, end } /-- Intended to be an implementation detail of the tactic mode. In "user code", use `definable` instead. -/ structure sect (Γ : Def S) (X : Type*) [definable_sheaf S X] := (to_fun : Γ → X) (definable : definable_sheaf.definable to_fun) def sect.precomp {Γ' Γ : Def S} (σ : sect Γ X) (φ : Γ' ⟶ Γ) : sect Γ' X := { to_fun := σ.to_fun ∘ φ.to_fun, definable := definable_sheaf.definable_precomp φ σ.to_fun σ.definable } lemma definable_fun_iff {Γ : Def S} {f : Γ → X → Y} : definable_sheaf.definable f ↔ ∀ (Γ' : Def S) (π : Hom Γ' Γ) (σ : sect Γ' X), definable_sheaf.definable (λ i', f (π.to_fun i') (σ.to_fun i')) := ⟨λ H Γ' π σ, H Γ' (λ γ', (π.to_fun γ', σ.to_fun γ')) ⟨π.is_definable, σ.definable⟩, λ H M g hg, H M ⟨_, hg.1⟩ ⟨_, hg.2⟩⟩ /- The weird binder types are because this lemma is intended to be applied by a special `defin` tactic. -/ lemma definable_app {Γ : Def S} (f : Γ → X → Y) {hf : definable_sheaf.definable f} (x : Γ → X) {hx : definable_sheaf.definable x} : definable_sheaf.definable (λ i, f i (x i)) := hf Γ (λ γ, (γ, x γ)) ⟨def_fun.id, hx⟩ end definable_sheaf structure definable {X : Type*} [definable_sheaf S X] (x : X) : Prop := (definable : ∀ (K : Def S), definable_sheaf.definable (λ (i : K), x)) end o_minimal
8046c20e92d008a61a627e9f8577c4b9dd596118
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/run/instances.lean
5b0af30b99024da34083d721ab80b75c777928ab
[ "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
357
lean
import Lean open Lean open Lean.Meta instance : ToFormat InstanceEntry where format e := format e.val unsafe def tst1 : IO Unit := withImportModules [{module := `Lean}] {} 0 fun env => do let aux : MetaM Unit := do let insts ← getGlobalInstancesIndex IO.println (format insts) discard <| aux.run |>.toIO {} { env := env } #eval tst1
4548d984db93c04dd8544082413d21ed8590f82b
367134ba5a65885e863bdc4507601606690974c1
/src/topology/topological_fiber_bundle.lean
ff209fa9d448c509186fc675d63e8506599fbe4b
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
32,393
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 /-! # Fiber bundles A topological fiber bundle with fiber `F` over a base `B` is a space projecting on `B` for which the fibers are all homeomorphic to `F`, such that the local situation around each point is a direct product. We define a predicate `is_topological_fiber_bundle F p` saying that `p : Z → B` is a topological fiber bundle with fiber `F`. It is in general nontrivial to construct a fiber bundle. A way is to start from the knowledge of how changes of local trivializations act on the fiber. From this, one can construct the total space of the bundle and its topology by a suitable gluing construction. The main content of this file is an implementation of this construction: starting from an object of type `topological_fiber_bundle_core` registering the trivialization changes, one gets the corresponding fiber bundle and projection. ## Main definitions ### Basic definitions * `bundle_trivialization F p` : structure extending local homeomorphisms, defining a local trivialization of a topological space `Z` with projection `p` and fiber `F`. * `is_topological_fiber_bundle F p` : Prop saying that the map `p` between topological spaces is a fiber bundle with fiber `F`. * `is_trivial_topological_fiber_bundle F p` : Prop saying that the map `p : Z → B` between topological spaces is a trivial topological fiber bundle, i.e., there exists a homeomorphism `h : Z ≃ₜ B × F` such that `proj x = (h x).1`. ### Operations on bundles We provide the following operations on `bundle_trivialization`s. * `bundle_trivialization.comap`: given a local trivialization `e` of a fiber bundle `p : Z → B`, a continuous map `f : B' → B` and a point `b' : B'` such that `f b' ∈ e.base_set`, `e.comap f hf b' hb'` is a trivialization of the pullback bundle. The pullback bundle (a.k.a., the induced bundle) has total space `{(x, y) : B' × Z | f x = p y}`, and is given by `λ ⟨(x, y), h⟩, x`. * `is_topological_fiber_bundle.comap`: if `p : Z → B` is a topological fiber bundle, then its pullback along a continuous map `f : B' → B` is a topological fiber bundle as well. * `bundle_trivialization.comp_homeomorph`: given a local trivialization `e` of a fiber bundle `p : Z → B` and a homeomorphism `h : Z' ≃ₜ Z`, returns a local trivialization of the fiber bundle `p ∘ h`. * `is_topological_fiber_bundle.comp_homeomorph`: if `p : Z → B` is a topological fiber bundle and `h : Z' ≃ₜ Z` is a homeomorphism, then `p ∘ h : Z' → B` is a topological fiber bundle with the same fiber. ### Construction of a bundle from trivializations * `topological_fiber_bundle_core ι B F` : structure registering how changes of coordinates act on the fiber `F` above open subsets of `B`, where local trivializations are indexed by `ι`. Let `Z : topological_fiber_bundle_core ι B F`. Then we define * `Z.total_space` : the total space of `Z`, defined as a `Type` as `Σ (b : B), F`, but with a twisted topology coming from the fiber bundle structure * `Z.proj` : projection from `Z.total_space` to `B`. It is continuous. * `Z.fiber x` : the fiber above `x`, homeomorphic to `F` (and defeq to `F` as a type). * `Z.local_triv i`: for `i : ι`, a local homeomorphism from `Z.total_space` to `B × F`, that realizes a trivialization above the set `Z.base_set i`, which is an open set in `B`. ## Implementation notes A topological fiber bundle with fiber `F` over a base `B` is a family of spaces isomorphic to `F`, indexed by `B`, which is locally trivial in the following sense: there is a covering of `B` by open sets such that, on each such open set `s`, the bundle is isomorphic to `s × F`. To construct a fiber bundle formally, the main data is what happens when one changes trivializations from `s × F` to `s' × F` on `s ∩ s'`: one should get a family of homeomorphisms of `F`, depending continuously on the base point, satisfying basic compatibility conditions (cocycle property). Useful classes of bundles can then be specified by requiring that these homeomorphisms of `F` belong to some subgroup, preserving some structure (the "structure group of the bundle"): then these structures are inherited by the fibers of the bundle. Given such trivialization change data (encoded below in a structure called `topological_fiber_bundle_core`), one can construct the fiber bundle. The intrinsic canonical mathematical construction is the following. The fiber above `x` is the disjoint union of `F` over all trivializations, modulo the gluing identifications: one gets a fiber which is isomorphic to `F`, but non-canonically (each choice of one of the trivializations around `x` gives such an isomorphism). Given a trivialization over a set `s`, one gets an isomorphism between `s × F` and `proj^{-1} s`, by using the identification corresponding to this trivialization. One chooses the topology on the bundle that makes all of these into homeomorphisms. For the practical implementation, it turns out to be more convenient to avoid completely the gluing and quotienting construction above, and to declare above each `x` that the fiber is `F`, but thinking that it corresponds to the `F` coming from the choice of one trivialization around `x`. This has several practical advantages: * without any work, one gets a topological space structure on the fiber. And if `F` has more structure it is inherited for free by the fiber. * In the case of the tangent bundle of manifolds, this implies that on vector spaces the derivative (from `F` to `F`) and the manifold derivative (from `tangent_space I x` to `tangent_space I' (f x)`) are equal. A drawback is that some silly constructions will typecheck: in the case of the tangent bundle, one can add two vectors in different tangent spaces (as they both are elements of `F` from the point of view of Lean). To solve this, one could mark the tangent space as irreducible, but then one would lose the identification of the tangent space to `F` with `F`. There is however a big advantage of this situation: even if Lean can not check that two basepoints are defeq, it will accept the fact that the tangent spaces are the same. For instance, if two maps `f` and `g` are locally inverse to each other, one can express that the composition of their derivatives is the identity of `tangent_space I x`. One could fear issues as this composition goes from `tangent_space I x` to `tangent_space I (g (f x))` (which should be the same, but should not be obvious to Lean as it does not know that `g (f x) = x`). As these types are the same to Lean (equal to `F`), there are in fact no dependent type difficulties here! For this construction of a fiber bundle from a `topological_fiber_bundle_core`, we should thus choose for each `x` one specific trivialization around it. We include this choice in the definition of the `topological_fiber_bundle_core`, as it makes some constructions more functorial and it is a nice way to say that the trivializations cover the whole space `B`. With this definition, the type of the fiber bundle space constructed from the core data is just `Σ (b : B), F `, but the topology is not the product one, in general. We also take the indexing type (indexing all the trivializations) as a parameter to the fiber bundle core: it could always be taken as a subtype of all the maps from open subsets of `B` to continuous maps of `F`, but in practice it will sometimes be something else. For instance, on a manifold, one will use the set of charts as a good parameterization for the trivializations of the tangent bundle. Or for the pullback of a `topological_fiber_bundle_core`, the indexing type will be the same as for the initial bundle. ## Tags Fiber bundle, topological bundle, vector bundle, local trivialization, structure group -/ variables {ι : Type*} {B : Type*} {F : Type*} open topological_space filter set open_locale topological_space section topological_fiber_bundle variables (F) {Z : Type*} [topological_space B] [topological_space Z] [topological_space F] {proj : Z → B} /-- A structure extending local homeomorphisms, defining a local trivialization of a projection `proj : Z → B` with fiber `F`, as a local homeomorphism between `Z` and `B × F` defined between two sets of the form `proj ⁻¹' base_set` and `base_set × F`, acting trivially on the first coordinate. -/ structure bundle_trivialization (proj : Z → B) extends local_homeomorph Z (B × F) := (base_set : set B) (open_base_set : is_open base_set) (source_eq : source = proj ⁻¹' base_set) (target_eq : target = set.prod base_set univ) (proj_to_fun : ∀ p ∈ source, (to_local_homeomorph p).1 = proj p) instance : has_coe_to_fun (bundle_trivialization F proj) := ⟨_, λ e, e.to_fun⟩ variable {F} @[simp, mfld_simps] lemma bundle_trivialization.coe_coe (e : bundle_trivialization F proj) : ⇑e.to_local_homeomorph = e := rfl @[simp, mfld_simps] lemma bundle_trivialization.coe_mk (e : local_homeomorph Z (B × F)) (i j k l m) (x : Z) : (bundle_trivialization.mk e i j k l m : bundle_trivialization F proj) x = e x := rfl variable (F) /-- A topological fiber bundle with fiber `F` over a base `B` is a space projecting on `B` for which the fibers are all homeomorphic to `F`, such that the local situation around each point is a direct product. -/ def is_topological_fiber_bundle (proj : Z → B) : Prop := ∀ x : B, ∃e : bundle_trivialization F proj, x ∈ e.base_set /-- A trivial topological fiber bundle with fiber `F` over a base `B` is a space `Z` projecting on `B` for which there exists a homeomorphism to `B × F` that sends `proj` to `prod.fst`. -/ def is_trivial_topological_fiber_bundle (proj : Z → B) : Prop := ∃ e : Z ≃ₜ (B × F), ∀ x, (e x).1 = proj x variables {F} lemma bundle_trivialization.mem_source (e : bundle_trivialization F proj) {x : Z} : x ∈ e.source ↔ proj x ∈ e.base_set := by rw [e.source_eq, mem_preimage] lemma bundle_trivialization.mem_target (e : bundle_trivialization F proj) {x : B × F} : x ∈ e.target ↔ x.1 ∈ e.base_set := by rw [e.target_eq, prod_univ, mem_preimage] @[simp, mfld_simps] lemma bundle_trivialization.coe_fst (e : bundle_trivialization F proj) {x : Z} (ex : x ∈ e.source) : (e x).1 = proj x := e.proj_to_fun x ex lemma bundle_trivialization.coe_fst' (e : bundle_trivialization F proj) {x : Z} (ex : proj x ∈ e.base_set) : (e x).1 = proj x := e.coe_fst (e.mem_source.2 ex) lemma bundle_trivialization.proj_symm_apply (e : bundle_trivialization F proj) {x : B × F} (hx : x ∈ e.target) : proj (e.to_local_homeomorph.symm x) = x.1 := begin have := (e.coe_fst (e.to_local_homeomorph.map_target hx)).symm, rwa [← e.coe_coe, e.to_local_homeomorph.right_inv hx] at this end lemma bundle_trivialization.proj_symm_apply' (e : bundle_trivialization F proj) {b : B} {x : F} (hx : b ∈ e.base_set) : proj (e.to_local_homeomorph.symm (b, x)) = b := e.proj_symm_apply (e.mem_target.2 hx) lemma bundle_trivialization.apply_symm_apply (e : bundle_trivialization F proj) {x : B × F} (hx : x ∈ e.target) : e (e.to_local_homeomorph.symm x) = x := e.to_local_homeomorph.right_inv hx lemma bundle_trivialization.apply_symm_apply' (e : bundle_trivialization F proj) {b : B} {x : F} (hx : b ∈ e.base_set) : e (e.to_local_homeomorph.symm (b, x)) = (b, x) := e.apply_symm_apply (e.mem_target.2 hx) @[simp, mfld_simps] lemma bundle_trivialization.symm_apply_mk_proj (e : bundle_trivialization F proj) {x : Z} (ex : x ∈ e.source) : e.to_local_homeomorph.symm (proj x, (e x).2) = x := by rw [← e.coe_fst ex, prod.mk.eta, ← e.coe_coe, e.to_local_homeomorph.left_inv ex] lemma bundle_trivialization.coe_fst_eventually_eq_proj (e : bundle_trivialization F proj) {x : Z} (ex : x ∈ e.source) : prod.fst ∘ e =ᶠ[𝓝 x] proj := mem_nhds_sets_iff.2 ⟨e.source, λ y hy, e.coe_fst hy, e.open_source, ex⟩ lemma bundle_trivialization.coe_fst_eventually_eq_proj' (e : bundle_trivialization F proj) {x : Z} (ex : proj x ∈ e.base_set) : prod.fst ∘ e =ᶠ[𝓝 x] proj := e.coe_fst_eventually_eq_proj (e.mem_source.2 ex) lemma is_trivial_topological_fiber_bundle.is_topological_fiber_bundle (h : is_trivial_topological_fiber_bundle F proj) : is_topological_fiber_bundle F proj := let ⟨e, he⟩ := h in λ x, ⟨⟨e.to_local_homeomorph, univ, is_open_univ, rfl, univ_prod_univ.symm, λ x _, he x⟩, mem_univ x⟩ lemma bundle_trivialization.map_proj_nhds (e : bundle_trivialization F proj) {x : Z} (ex : x ∈ e.source) : map proj (𝓝 x) = 𝓝 (proj x) := by rw [← e.coe_fst ex, ← map_congr (e.coe_fst_eventually_eq_proj ex), ← map_map, ← e.coe_coe, e.to_local_homeomorph.map_nhds_eq ex, map_fst_nhds] /-- In the domain of a bundle trivialization, the projection is continuous-/ lemma bundle_trivialization.continuous_at_proj (e : bundle_trivialization F proj) {x : Z} (ex : x ∈ e.source) : continuous_at proj x := (e.map_proj_nhds ex).le /-- The projection from a topological fiber bundle to its base is continuous. -/ lemma is_topological_fiber_bundle.continuous_proj (h : is_topological_fiber_bundle F proj) : continuous proj := begin rw continuous_iff_continuous_at, assume x, rcases h (proj x) with ⟨e, ex⟩, apply e.continuous_at_proj, rwa e.source_eq end /-- The projection from a topological fiber bundle to its base is an open map. -/ lemma is_topological_fiber_bundle.is_open_map_proj (h : is_topological_fiber_bundle F proj) : is_open_map proj := begin refine is_open_map_iff_nhds_le.2 (λ x, _), rcases h (proj x) with ⟨e, ex⟩, refine (e.map_proj_nhds _).ge, rwa e.source_eq end /-- The first projection in a product is a trivial topological fiber bundle. -/ lemma is_trivial_topological_fiber_bundle_fst : is_trivial_topological_fiber_bundle F (prod.fst : B × F → B) := ⟨homeomorph.refl _, λ x, rfl⟩ /-- The first projection in a product is a topological fiber bundle. -/ lemma is_topological_fiber_bundle_fst : is_topological_fiber_bundle F (prod.fst : B × F → B) := is_trivial_topological_fiber_bundle_fst.is_topological_fiber_bundle /-- The second projection in a product is a trivial topological fiber bundle. -/ lemma is_trivial_topological_fiber_bundle_snd : is_trivial_topological_fiber_bundle F (prod.snd : F × B → B) := ⟨homeomorph.prod_comm _ _, λ x, rfl⟩ /-- The second projection in a product is a topological fiber bundle. -/ lemma is_topological_fiber_bundle_snd : is_topological_fiber_bundle F (prod.snd : F × B → B) := is_trivial_topological_fiber_bundle_snd.is_topological_fiber_bundle /-- Composition of a `bundle_trivialization` and a `homeomorph`. -/ def bundle_trivialization.comp_homeomorph {Z' : Type*} [topological_space Z'] (e : bundle_trivialization F proj) (h : Z' ≃ₜ Z) : bundle_trivialization F (proj ∘ h) := { to_local_homeomorph := h.to_local_homeomorph.trans e.to_local_homeomorph, base_set := e.base_set, open_base_set := e.open_base_set, source_eq := by simp [e.source_eq, preimage_preimage], target_eq := by simp [e.target_eq], proj_to_fun := λ p hp, have hp : h p ∈ e.source, by simpa using hp, by simp [hp] } lemma is_topological_fiber_bundle.comp_homeomorph {Z' : Type*} [topological_space Z'] (e : is_topological_fiber_bundle F proj) (h : Z' ≃ₜ Z) : is_topological_fiber_bundle F (proj ∘ h) := λ x, let ⟨e, he⟩ := e x in ⟨e.comp_homeomorph h, by simpa [bundle_trivialization.comp_homeomorph] using he⟩ section induced open_locale classical variables {B' : Type*} [topological_space B'] /-- Given a bundle trivialization of `proj : Z → B` and a continuous map `f : B' → B`, construct a bundle trivialization of `φ : {p : B' × Z | f p.1 = proj p.2} → B'` given by `φ x = (x : B' × Z).1`. -/ noncomputable def bundle_trivialization.comap (e : bundle_trivialization F proj) (f : B' → B) (hf : continuous f) (b' : B') (hb' : f b' ∈ e.base_set) : bundle_trivialization F (λ x : {p : B' × Z | f p.1 = proj p.2}, (x : B' × Z).1) := { to_fun := λ p, ((p : B' × Z).1, (e (p : B' × Z).2).2), inv_fun := λ p, if h : f p.1 ∈ e.base_set then ⟨⟨p.1, e.to_local_homeomorph.symm (f p.1, p.2)⟩, by simp [e.proj_symm_apply' h]⟩ else ⟨⟨b', e.to_local_homeomorph.symm (f b', p.2)⟩, by simp [e.proj_symm_apply' hb']⟩, source := {p | f (p : B' × Z).1 ∈ e.base_set}, target := {p | f p.1 ∈ e.base_set}, map_source' := λ p hp, hp, map_target' := λ p (hp : f p.1 ∈ e.base_set), by simp [hp], left_inv' := begin rintro ⟨⟨b, x⟩, hbx⟩ hb, dsimp at *, have hx : x ∈ e.source, from e.mem_source.2 (hbx ▸ hb), ext; simp * end, right_inv' := λ p (hp : f p.1 ∈ e.base_set), by simp [*, e.apply_symm_apply'], open_source := e.open_base_set.preimage (hf.comp $ continuous_fst.comp continuous_subtype_coe), open_target := e.open_base_set.preimage (hf.comp continuous_fst), continuous_to_fun := ((continuous_fst.comp continuous_subtype_coe).continuous_on).prod $ continuous_snd.comp_continuous_on $ e.continuous_to_fun.comp (continuous_snd.comp continuous_subtype_coe).continuous_on $ by { rintro ⟨⟨b, x⟩, (hbx : f b = proj x)⟩ (hb : f b ∈ e.base_set), rw hbx at hb, exact e.mem_source.2 hb }, continuous_inv_fun := begin rw [embedding_subtype_coe.continuous_on_iff], suffices : continuous_on (λ p : B' × F, (p.1, e.to_local_homeomorph.symm (f p.1, p.2))) {p : B' × F | f p.1 ∈ e.base_set}, { refine this.congr (λ p (hp : f p.1 ∈ e.base_set), _), simp [hp] }, { refine continuous_on_fst.prod (e.to_local_homeomorph.symm.continuous_on.comp _ _), { exact ((hf.comp continuous_fst).prod_mk continuous_snd).continuous_on }, { exact λ p hp, e.mem_target.2 hp } } end, base_set := f ⁻¹' e.base_set, source_eq := rfl, target_eq := by { ext, simp }, open_base_set := e.open_base_set.preimage hf, proj_to_fun := λ _ _, rfl } /-- If `proj : Z → B` is a topological fiber bundle with fiber `F` and `f : B' → B` is a continuous map, then the pullback bundle (a.k.a. induced bundle) is the topological bundle with the total space `{(x, y) : B' × Z | f x = proj y}` given by `λ ⟨(x, y), h⟩, x`. -/ lemma is_topological_fiber_bundle.comap (h : is_topological_fiber_bundle F proj) {f : B' → B} (hf : continuous f) : is_topological_fiber_bundle F (λ x : {p : B' × Z | f p.1 = proj p.2}, (x : B' × Z).1) := λ x, let ⟨e, he⟩ := h (f x) in ⟨e.comap f hf x he, he⟩ end induced end topological_fiber_bundle /-- Core data defining a locally trivial topological bundle with fiber `F` over a topological space `B`. Note that "bundle" is used in its mathematical sense. This is the (computer science) bundled version, i.e., all the relevant data is contained in the following structure. A family of local trivializations is indexed by a type ι, on open subsets `base_set i` for each `i : ι`. Trivialization changes from `i` to `j` are given by continuous maps `coord_change i j` from `base_set i ∩ base_set j` to the set of homeomorphisms of `F`, but we express them as maps `B → F → F` and require continuity on `(base_set i ∩ base_set j) × F` to avoid the topology on the space of continuous maps on `F`. -/ structure topological_fiber_bundle_core (ι : Type*) (B : Type*) [topological_space B] (F : Type*) [topological_space F] := (base_set : ι → set B) (is_open_base_set : ∀i, is_open (base_set i)) (index_at : B → ι) (mem_base_set_at : ∀x, x ∈ base_set (index_at x)) (coord_change : ι → ι → B → F → F) (coord_change_self : ∀i, ∀ x ∈ base_set i, ∀v, coord_change i i x v = v) (coord_change_continuous : ∀i j, continuous_on (λp : B × F, coord_change i j p.1 p.2) (set.prod ((base_set i) ∩ (base_set j)) univ)) (coord_change_comp : ∀i j k, ∀x ∈ (base_set i) ∩ (base_set j) ∩ (base_set k), ∀v, (coord_change j k x) (coord_change i j x v) = coord_change i k x v) attribute [simp, mfld_simps] topological_fiber_bundle_core.mem_base_set_at namespace topological_fiber_bundle_core variables [topological_space B] [topological_space F] (Z : topological_fiber_bundle_core ι B F) include Z /-- The index set of a topological fiber bundle core, as a convenience function for dot notation -/ @[nolint unused_arguments] def index := ι /-- The base space of a topological fiber bundle core, as a convenience function for dot notation -/ @[nolint unused_arguments] def base := B /-- The fiber of a topological fiber bundle core, as a convenience function for dot notation and typeclass inference -/ @[nolint unused_arguments] def fiber (x : B) := F instance topological_space_fiber (x : B) : topological_space (Z.fiber x) := by { dsimp [fiber], apply_instance } /-- Total space of a topological bundle created from core. It is equal to `Σ (x : B), F` as a type, but the fiber above `x` is registered as `Z.fiber x` to make sure that it is possible to register additional type classes on these fibers. -/ @[nolint unused_arguments] def total_space := Σ (x : B), Z.fiber x /-- The projection from the total space of a topological fiber bundle core, on its base. -/ @[simp, mfld_simps] def proj : Z.total_space → B := λp, p.1 /-- Local homeomorphism version of the trivialization change. -/ def triv_change (i j : ι) : local_homeomorph (B × F) (B × F) := { source := set.prod (Z.base_set i ∩ Z.base_set j) univ, target := set.prod (Z.base_set i ∩ Z.base_set j) univ, to_fun := λp, ⟨p.1, Z.coord_change i j p.1 p.2⟩, inv_fun := λp, ⟨p.1, Z.coord_change j i p.1 p.2⟩, map_source' := λp hp, by simpa using hp, map_target' := λp hp, by simpa using hp, left_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx.1 }, { simp [hx] } end, right_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, mem_inter_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx.2 }, { simp [hx] }, end, open_source := (is_open_inter (Z.is_open_base_set i) (Z.is_open_base_set j)).prod is_open_univ, open_target := (is_open_inter (Z.is_open_base_set i) (Z.is_open_base_set j)).prod is_open_univ, continuous_to_fun := continuous_on.prod continuous_fst.continuous_on (Z.coord_change_continuous i j), continuous_inv_fun := by simpa [inter_comm] using continuous_on.prod continuous_fst.continuous_on (Z.coord_change_continuous j i) } @[simp, mfld_simps] lemma mem_triv_change_source (i j : ι) (p : B × F) : p ∈ (Z.triv_change i j).source ↔ p.1 ∈ Z.base_set i ∩ Z.base_set j := by { erw [mem_prod], simp } /-- Associate to a trivialization index `i : ι` the corresponding trivialization, i.e., a bijection between `proj ⁻¹ (base_set i)` and `base_set i × F`. As the fiber above `x` is `F` but read in the chart with index `index_at x`, the trivialization in the fiber above x is by definition the coordinate change from i to `index_at x`, so it depends on `x`. The local trivialization will ultimately be a local homeomorphism. For now, we only introduce the local equiv version, denoted with a prime. In further developments, avoid this auxiliary version, and use `Z.local_triv` instead. -/ def local_triv' (i : ι) : local_equiv Z.total_space (B × F) := { source := Z.proj ⁻¹' (Z.base_set i), target := set.prod (Z.base_set i) univ, inv_fun := λp, ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩, to_fun := λp, ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩, map_source' := λp hp, by simpa only [set.mem_preimage, and_true, set.mem_univ, set.prod_mk_mem_set_prod_eq] using hp, map_target' := λp hp, by simpa only [set.mem_preimage, and_true, set.mem_univ, set.mem_prod] using hp, left_inv' := begin rintros ⟨x, v⟩ hx, change x ∈ Z.base_set i at hx, dsimp, rw [Z.coord_change_comp, Z.coord_change_self], { exact Z.mem_base_set_at _ }, { simp [hx] } end, right_inv' := begin rintros ⟨x, v⟩ hx, simp only [prod_mk_mem_set_prod_eq, and_true, mem_univ] at hx, rw [Z.coord_change_comp, Z.coord_change_self], { exact hx }, { simp [hx] } end } @[simp, mfld_simps] lemma mem_local_triv'_source (i : ι) (p : Z.total_space) : p ∈ (Z.local_triv' i).source ↔ p.1 ∈ Z.base_set i := iff.rfl @[simp, mfld_simps] lemma mem_local_triv'_target (i : ι) (p : B × F) : p ∈ (Z.local_triv' i).target ↔ p.1 ∈ Z.base_set i := by { erw [mem_prod], simp } @[simp, mfld_simps] lemma local_triv'_apply (i : ι) (p : Z.total_space) : (Z.local_triv' i) p = ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩ := rfl @[simp, mfld_simps] lemma local_triv'_symm_apply (i : ι) (p : B × F) : (Z.local_triv' i).symm p = ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩ := rfl /-- The composition of two local trivializations is the trivialization change Z.triv_change i j. -/ lemma local_triv'_trans (i j : ι) : (Z.local_triv' i).symm.trans (Z.local_triv' j) ≈ (Z.triv_change i j).to_local_equiv := begin split, { ext x, erw [mem_prod], simp [local_equiv.trans_source] }, { rintros ⟨x, v⟩ hx, simp only [triv_change, local_triv', local_equiv.symm, true_and, prod_mk_mem_set_prod_eq, local_equiv.trans_source, mem_inter_eq, and_true, mem_univ, prod.mk.inj_iff, mem_preimage, proj, local_equiv.coe_mk, eq_self_iff_true, local_equiv.coe_trans] at hx ⊢, simp [Z.coord_change_comp, hx] } end /-- Topological structure on the total space of a topological bundle created from core, designed so that all the local trivialization are continuous. -/ instance to_topological_space : topological_space Z.total_space := topological_space.generate_from $ ⋃ (i : ι) (s : set (B × F)) (s_open : is_open s), {(Z.local_triv' i).source ∩ (Z.local_triv' i) ⁻¹' s} lemma open_source' (i : ι) : is_open (Z.local_triv' i).source := begin apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], refine ⟨i, set.prod (Z.base_set i) univ, (Z.is_open_base_set i).prod is_open_univ, _⟩, ext p, simp only with mfld_simps end lemma open_target' (i : ι) : is_open (Z.local_triv' i).target := (Z.is_open_base_set i).prod is_open_univ /-- Local trivialization of a topological bundle created from core, as a local homeomorphism. -/ def local_triv (i : ι) : local_homeomorph Z.total_space (B × F) := { open_source := Z.open_source' i, open_target := Z.open_target' i, continuous_to_fun := begin rw continuous_on_open_iff (Z.open_source' i), assume s s_open, apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], exact ⟨i, s, s_open, rfl⟩ end, continuous_inv_fun := begin apply continuous_on_open_of_generate_from (Z.open_target' i), assume t ht, simp only [exists_prop, mem_Union, mem_singleton_iff] at ht, obtain ⟨j, s, s_open, ts⟩ : ∃ j s, is_open s ∧ t = (local_triv' Z j).source ∩ (local_triv' Z j) ⁻¹' s := ht, rw ts, simp only [local_equiv.right_inv, preimage_inter, local_equiv.left_inv], let e := Z.local_triv' i, let e' := Z.local_triv' j, let f := e.symm.trans e', have : is_open (f.source ∩ f ⁻¹' s), { rw [(Z.local_triv'_trans i j).source_inter_preimage_eq], exact (continuous_on_open_iff (Z.triv_change i j).open_source).1 ((Z.triv_change i j).continuous_on) _ s_open }, convert this using 1, dsimp [local_equiv.trans_source], rw [← preimage_comp, inter_assoc] end, to_local_equiv := Z.local_triv' i } /- We will now state again the basic properties of the local trivializations, but without primes, i.e., for the local homeomorphism instead of the local equiv. -/ @[simp, mfld_simps] lemma mem_local_triv_source (i : ι) (p : Z.total_space) : p ∈ (Z.local_triv i).source ↔ p.1 ∈ Z.base_set i := iff.rfl @[simp, mfld_simps] lemma mem_local_triv_target (i : ι) (p : B × F) : p ∈ (Z.local_triv i).target ↔ p.1 ∈ Z.base_set i := by { erw [mem_prod], simp } @[simp, mfld_simps] lemma local_triv_apply (i : ι) (p : Z.total_space) : (Z.local_triv i) p = ⟨p.1, Z.coord_change (Z.index_at p.1) i p.1 p.2⟩ := rfl @[simp, mfld_simps] lemma local_triv_symm_fst (i : ι) (p : B × F) : (Z.local_triv i).symm p = ⟨p.1, Z.coord_change i (Z.index_at p.1) p.1 p.2⟩ := rfl /-- The composition of two local trivializations is the trivialization change Z.triv_change i j. -/ lemma local_triv_trans (i j : ι) : (Z.local_triv i).symm.trans (Z.local_triv j) ≈ Z.triv_change i j := Z.local_triv'_trans i j /-- Extended version of the local trivialization of a fiber bundle constructed from core, registering additionally in its type that it is a local bundle trivialization. -/ def local_triv_ext (i : ι) : bundle_trivialization F Z.proj := { base_set := Z.base_set i, open_base_set := Z.is_open_base_set i, source_eq := rfl, target_eq := rfl, proj_to_fun := λp hp, by simp, to_local_homeomorph := Z.local_triv i } /-- A topological fiber bundle constructed from core is indeed a topological fiber bundle. -/ protected theorem is_topological_fiber_bundle : is_topological_fiber_bundle F Z.proj := λx, ⟨Z.local_triv_ext (Z.index_at x), Z.mem_base_set_at x⟩ /-- The projection on the base of a topological bundle created from core is continuous -/ lemma continuous_proj : continuous Z.proj := Z.is_topological_fiber_bundle.continuous_proj /-- The projection on the base of a topological bundle created from core is an open map -/ lemma is_open_map_proj : is_open_map Z.proj := Z.is_topological_fiber_bundle.is_open_map_proj /-- Preferred local trivialization of a fiber bundle constructed from core, at a given point, as a local homeomorphism -/ def local_triv_at (p : Z.total_space) : local_homeomorph Z.total_space (B × F) := Z.local_triv (Z.index_at (Z.proj p)) @[simp, mfld_simps] lemma mem_local_triv_at_source (p : Z.total_space) : p ∈ (Z.local_triv_at p).source := by simp [local_triv_at] @[simp, mfld_simps] lemma local_triv_at_fst (p q : Z.total_space) : ((Z.local_triv_at p) q).1 = q.1 := rfl @[simp, mfld_simps] lemma local_triv_at_symm_fst (p : Z.total_space) (q : B × F) : ((Z.local_triv_at p).symm q).1 = q.1 := rfl /-- Preferred local trivialization of a fiber bundle constructed from core, at a given point, as a bundle trivialization -/ def local_triv_at_ext (p : Z.total_space) : bundle_trivialization F Z.proj := Z.local_triv_ext (Z.index_at (Z.proj p)) @[simp, mfld_simps] lemma local_triv_at_ext_to_local_homeomorph (p : Z.total_space) : (Z.local_triv_at_ext p).to_local_homeomorph = Z.local_triv_at p := rfl /-- If an element of `F` is invariant under all coordinate changes, then one can define a corresponding section of the fiber bundle, which is continuous. This applies in particular to the zero section of a vector bundle. Another example (not yet defined) would be the identity section of the endomorphism bundle of a vector bundle. -/ lemma continuous_const_section (v : F) (h : ∀ i j, ∀ x ∈ (Z.base_set i) ∩ (Z.base_set j), Z.coord_change i j x v = v) : continuous (show B → Z.total_space, from λ x, ⟨x, v⟩) := begin apply continuous_iff_continuous_at.2 (λ x, _), have A : Z.base_set (Z.index_at x) ∈ 𝓝 x := mem_nhds_sets (Z.is_open_base_set (Z.index_at x)) (Z.mem_base_set_at x), apply ((Z.local_triv (Z.index_at x)).continuous_at_iff_continuous_at_comp_left _).2, { simp only [(∘)] with mfld_simps, apply continuous_at_id.prod, have : continuous_on (λ (y : B), v) (Z.base_set (Z.index_at x)) := continuous_on_const, apply (this.congr _).continuous_at A, assume y hy, simp only [h, hy] with mfld_simps }, { exact A } end end topological_fiber_bundle_core
25b612fdc5b8c997ed885e88091baf1c7d6433d3
0d107d7abd6ae235d586830f8e09b1b30e7eef0b
/src/limit_unique/Preloaded.lean
d947981599070f1aa85683f98699c75b549b3335
[]
no_license
ukikagi/codewars-lean
6b9a83ebbb159e7eebf8551b745a1c4d450e747f
1912f2a4e25e917abfce70d65c0469cfac19dc93
refs/heads/master
1,672,948,190,244
1,603,361,004,000
1,603,795,841,000
303,746,208
0
0
null
null
null
null
UTF-8
Lean
false
false
412
lean
import topology.metric_space.basic def converges_to {X : Type*} [metric_space X] (s : ℕ → X) (x : X) := ∀ (ε : ℝ) (hε : 0 < ε), ∃ N : ℕ, ∀ (n : ℕ) (hn : N ≤ n), dist x (s n) < ε notation s ` ⟶ ` x := converges_to s x def SUBMISSION := ∀ {X : Type*} [h : metric_space X] {s : ℕ → X} (x₀ x₁ : X), by letI := h; exact ∀ (h₀ : s ⟶ x₀) (h₁ : s ⟶ x₁), x₀ = x₁
e73880c01dbbd657c2cec03c400eceeaf835675f
618003631150032a5676f229d13a079ac875ff77
/src/algebra/char_p.lean
28fcd6a7b66fc9f327adf4c9ae7a28dd8584139e
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
10,719
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kenny Lau, Joey van Langen, Casper Putz -/ import data.fintype.basic import data.nat.choose import data.int.modeq import algebra.module import algebra.iterate_hom /-! # Characteristic of semirings -/ universes u v /-- The generator of the kernel of the unique homomorphism ℕ → α for a semiring α -/ class char_p (α : Type u) [semiring α] (p : ℕ) : Prop := (cast_eq_zero_iff [] : ∀ x:ℕ, (x:α) = 0 ↔ p ∣ x) theorem char_p.cast_eq_zero (α : Type u) [semiring α] (p : ℕ) [char_p α p] : (p:α) = 0 := (char_p.cast_eq_zero_iff α p p).2 (dvd_refl p) lemma char_p.int_cast_eq_zero_iff (R : Type u) [ring R] (p : ℕ) [char_p R p] (a : ℤ) : (a : R) = 0 ↔ (p:ℤ) ∣ a := begin rcases lt_trichotomy a 0 with h|rfl|h, { rw [← neg_eq_zero, ← int.cast_neg, ← dvd_neg], lift -a to ℕ using neg_nonneg.mpr (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] }, { simp only [int.cast_zero, eq_self_iff_true, dvd_zero] }, { lift a to ℕ using (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] } end lemma char_p.int_coe_eq_int_coe_iff (R : Type*) [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = (b : R) ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd] theorem char_p.eq (α : Type u) [semiring α] {p q : ℕ} (c1 : char_p α p) (c2 : char_p α q) : p = q := nat.dvd_antisymm ((char_p.cast_eq_zero_iff α p q).1 (char_p.cast_eq_zero _ _)) ((char_p.cast_eq_zero_iff α q p).1 (char_p.cast_eq_zero _ _)) instance char_p.of_char_zero (α : Type u) [semiring α] [char_zero α] : char_p α 0 := ⟨λ x, by rw [zero_dvd_iff, ← nat.cast_zero, nat.cast_inj]⟩ theorem char_p.exists (α : Type u) [semiring α] : ∃ p, char_p α p := by letI := classical.dec_eq α; exact classical.by_cases (assume H : ∀ p:ℕ, (p:α) = 0 → p = 0, ⟨0, ⟨λ x, by rw [zero_dvd_iff]; exact ⟨H x, by rintro rfl; refl⟩⟩⟩) (λ H, ⟨nat.find (classical.not_forall.1 H), ⟨λ x, ⟨λ H1, nat.dvd_of_mod_eq_zero (by_contradiction $ λ H2, nat.find_min (classical.not_forall.1 H) (nat.mod_lt x $ nat.pos_of_ne_zero $ not_of_not_imp $ nat.find_spec (classical.not_forall.1 H)) (not_imp_of_and_not ⟨by rwa [← nat.mod_add_div x (nat.find (classical.not_forall.1 H)), nat.cast_add, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (classical.not_forall.1 H)), zero_mul, add_zero] at H1, H2⟩)), λ H1, by rw [← nat.mul_div_cancel' H1, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (classical.not_forall.1 H)), zero_mul]⟩⟩⟩) theorem char_p.exists_unique (α : Type u) [semiring α] : ∃! p, char_p α p := let ⟨c, H⟩ := char_p.exists α in ⟨c, H, λ y H2, char_p.eq α H2 H⟩ /-- Noncomuptable function that outputs the unique characteristic of a semiring. -/ noncomputable def ring_char (α : Type u) [semiring α] : ℕ := classical.some (char_p.exists_unique α) theorem ring_char.spec (α : Type u) [semiring α] : ∀ x:ℕ, (x:α) = 0 ↔ ring_char α ∣ x := by letI := (classical.some_spec (char_p.exists_unique α)).1; unfold ring_char; exact char_p.cast_eq_zero_iff α (ring_char α) theorem ring_char.eq (α : Type u) [semiring α] {p : ℕ} (C : char_p α p) : p = ring_char α := (classical.some_spec (char_p.exists_unique α)).2 p C theorem add_pow_char (α : Type u) [comm_ring α] {p : ℕ} (hp : nat.prime p) [char_p α p] (x y : α) : (x + y)^p = x^p + y^p := begin rw [add_pow, finset.sum_range_succ, nat.sub_self, pow_zero, nat.choose_self], rw [nat.cast_one, mul_one, mul_one, add_right_inj], transitivity, { refine finset.sum_eq_single 0 _ _, { intros b h1 h2, have := nat.prime.dvd_choose_self (nat.pos_of_ne_zero h2) (finset.mem_range.1 h1) hp, rw [← nat.div_mul_cancel this, nat.cast_mul, char_p.cast_eq_zero α p], simp only [mul_zero] }, { intro H, exfalso, apply H, exact finset.mem_range.2 hp.pos } }, rw [pow_zero, nat.sub_zero, one_mul, nat.choose_zero_right, nat.cast_one, mul_one] end lemma eq_iff_modeq_int (R : Type*) [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = b ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd] lemma char_p.neg_one_ne_one (R : Type*) [ring R] (p : ℕ) [char_p R p] [fact (2 < p)] : (-1 : R) ≠ (1 : R) := begin suffices : (2 : R) ≠ 0, { symmetry, rw [ne.def, ← sub_eq_zero, sub_neg_eq_add], exact this }, assume h, rw [show (2 : R) = (2 : ℕ), by norm_cast] at h, have := (char_p.cast_eq_zero_iff R p 2).mp h, have := nat.le_of_dvd dec_trivial this, rw fact at *, linarith, end section frobenius variables (R : Type u) [comm_ring R] {S : Type v} [comm_ring S] (f : R →* S) (g : R →+* S) (p : ℕ) [fact p.prime] [char_p R p] [char_p S p] (x y : R) /-- The frobenius map that sends x to x^p -/ def frobenius : R →+* R := { to_fun := λ x, x^p, map_one' := one_pow p, map_mul' := λ x y, mul_pow x y p, map_zero' := zero_pow (lt_trans zero_lt_one ‹nat.prime p›.one_lt), map_add' := add_pow_char R ‹nat.prime p› } variable {R} theorem frobenius_def : frobenius R p x = x ^ p := rfl theorem frobenius_mul : frobenius R p (x * y) = frobenius R p x * frobenius R p y := (frobenius R p).map_mul x y theorem frobenius_one : frobenius R p 1 = 1 := one_pow _ variable {R} theorem monoid_hom.map_frobenius : f (frobenius R p x) = frobenius S p (f x) := f.map_pow x p theorem ring_hom.map_frobenius : g (frobenius R p x) = frobenius S p (g x) := g.map_pow x p theorem monoid_hom.map_iterate_frobenius (n : ℕ) : f (frobenius R p^[n] x) = (frobenius S p^[n] (f x)) := function.semiconj.iterate_right (f.map_frobenius p) n x theorem ring_hom.map_iterate_frobenius (n : ℕ) : g (frobenius R p^[n] x) = (frobenius S p^[n] (g x)) := g.to_monoid_hom.map_iterate_frobenius p x n theorem monoid_hom.iterate_map_frobenius (f : R →* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ theorem ring_hom.iterate_map_frobenius (f : R →+* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ variable (R) theorem frobenius_zero : frobenius R p 0 = 0 := (frobenius R p).map_zero theorem frobenius_add : frobenius R p (x + y) = frobenius R p x + frobenius R p y := (frobenius R p).map_add x y theorem frobenius_neg : frobenius R p (-x) = -frobenius R p x := (frobenius R p).map_neg x theorem frobenius_sub : frobenius R p (x - y) = frobenius R p x - frobenius R p y := (frobenius R p).map_sub x y theorem frobenius_nat_cast (n : ℕ) : frobenius R p n = n := (frobenius R p).map_nat_cast n end frobenius theorem frobenius_inj (α : Type u) [integral_domain α] (p : ℕ) [fact p.prime] [char_p α p] : function.injective (frobenius α p) := λ x h H, by { rw ← sub_eq_zero at H ⊢, rw ← frobenius_sub at H, exact pow_eq_zero H } namespace char_p section variables (α : Type u) [ring α] lemma char_p_to_char_zero [char_p α 0] : char_zero α := add_group.char_zero_of_inj_zero $ λ n h0, eq_zero_of_zero_dvd ((cast_eq_zero_iff α 0 n).mp h0) lemma cast_eq_mod (p : ℕ) [char_p α p] (k : ℕ) : (k : α) = (k % p : ℕ) := calc (k : α) = ↑(k % p + p * (k / p)) : by rw [nat.mod_add_div] ... = ↑(k % p) : by simp[cast_eq_zero] theorem char_ne_zero_of_fintype (p : ℕ) [hc : char_p α p] [fintype α] : p ≠ 0 := assume h : p = 0, have char_zero α := @char_p_to_char_zero α _ (h ▸ hc), absurd (@nat.cast_injective α _ _ this) (not_injective_infinite_fintype coe) end section integral_domain open nat variables (α : Type u) [integral_domain α] theorem char_ne_one (p : ℕ) [hc : char_p α p] : p ≠ 1 := assume hp : p = 1, have ( 1 : α) = 0, by simpa using (cast_eq_zero_iff α p 1).mpr (hp ▸ dvd_refl p), absurd this one_ne_zero theorem char_is_prime_of_ge_two (p : ℕ) [hc : char_p α p] (hp : p ≥ 2) : nat.prime p := suffices ∀d ∣ p, d = 1 ∨ d = p, from ⟨hp, this⟩, assume (d : ℕ) (hdvd : ∃ e, p = d * e), let ⟨e, hmul⟩ := hdvd in have (p : α) = 0, from (cast_eq_zero_iff α p p).mpr (dvd_refl p), have (d : α) * e = 0, from (@cast_mul α _ d e) ▸ (hmul ▸ this), or.elim (no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero (d : α) e this) (assume hd : (d : α) = 0, have p ∣ d, from (cast_eq_zero_iff α p d).mp hd, show d = 1 ∨ d = p, from or.inr (dvd_antisymm ⟨e, hmul⟩ this)) (assume he : (e : α) = 0, have p ∣ e, from (cast_eq_zero_iff α p e).mp he, have e ∣ p, from dvd_of_mul_left_eq d (eq.symm hmul), have e = p, from dvd_antisymm ‹e ∣ p› ‹p ∣ e›, have h₀ : p > 0, from gt_of_ge_of_gt hp (nat.zero_lt_succ 1), have d * p = 1 * p, by rw ‹e = p› at hmul; rw [one_mul]; exact eq.symm hmul, show d = 1 ∨ d = p, from or.inl (eq_of_mul_eq_mul_right h₀ this)) theorem char_is_prime_or_zero (p : ℕ) [hc : char_p α p] : nat.prime p ∨ p = 0 := match p, hc with | 0, _ := or.inr rfl | 1, hc := absurd (eq.refl (1 : ℕ)) (@char_ne_one α _ (1 : ℕ) hc) | (m+2), hc := or.inl (@char_is_prime_of_ge_two α _ (m+2) hc (nat.le_add_left 2 m)) end lemma char_is_prime_of_pos (p : ℕ) [h : fact (0 < p)] [char_p α p] : fact p.prime := (char_p.char_is_prime_or_zero α _).resolve_right (nat.pos_iff_ne_zero.1 h) theorem char_is_prime [fintype α] (p : ℕ) [char_p α p] : p.prime := or.resolve_right (char_is_prime_or_zero α p) (char_ne_zero_of_fintype α p) end integral_domain section char_one variables {R : Type*} section prio set_option default_priority 100 -- see Note [default priority] instance [semiring R] [char_p R 1] : subsingleton R := subsingleton.intro $ suffices ∀ (r : R), r = 0, from assume a b, show a = b, by rw [this a, this b], assume r, calc r = 1 * r : by rw one_mul ... = (1 : ℕ) * r : by rw nat.cast_one ... = 0 * r : by rw char_p.cast_eq_zero ... = 0 : by rw zero_mul end prio lemma false_of_nonzero_of_char_one [semiring R] [nonzero R] [char_p R 1] : false := zero_ne_one $ show (0:R) = 1, from subsingleton.elim 0 1 lemma ring_char_ne_one [semiring R] [nonzero R] : ring_char R ≠ 1 := by { intros h, apply @zero_ne_one R, symmetry, rw [←nat.cast_one, ring_char.spec, h], } end char_one end char_p
1bf87e0279918400e7e9dae86a5a3b7576ba421a
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/locally_constant/algebra.lean
22126bc6d6ae4a0c5fc3d276c28b9211dce38b50
[ "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
4,934
lean
/- Copyright (c) 2021 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import topology.locally_constant.basic /-! # Algebraic structure on locally constant functions This file puts algebraic structure (`add_group`, etc) on the type of locally constant functions. -/ namespace locally_constant variables {X Y : Type*} [topological_space X] @[to_additive] instance [has_one Y] : has_one (locally_constant X Y) := { one := const X 1 } @[simp, to_additive] lemma one_apply [has_one Y] (x : X) : (1 : locally_constant X Y) x = 1 := rfl @[to_additive] instance [has_inv Y] : has_inv (locally_constant X Y) := { inv := λ f, ⟨f⁻¹ , f.is_locally_constant.inv⟩ } @[simp, to_additive] lemma inv_apply [has_inv Y] (f : locally_constant X Y) (x : X) : f⁻¹ x = (f x)⁻¹ := rfl @[to_additive] instance [has_mul Y] : has_mul (locally_constant X Y) := { mul := λ f g, ⟨f * g, f.is_locally_constant.mul g.is_locally_constant⟩ } @[simp, to_additive] lemma mul_apply [has_mul Y] (f g : locally_constant X Y) (x : X) : (f * g) x = f x * g x := rfl @[to_additive] instance [mul_one_class Y] : mul_one_class (locally_constant X Y) := { one_mul := by { intros, ext, simp only [mul_apply, one_apply, one_mul] }, mul_one := by { intros, ext, simp only [mul_apply, one_apply, mul_one] }, .. locally_constant.has_one, .. locally_constant.has_mul } instance [mul_zero_class Y] : mul_zero_class (locally_constant X Y) := { zero_mul := by { intros, ext, simp only [mul_apply, zero_apply, zero_mul] }, mul_zero := by { intros, ext, simp only [mul_apply, zero_apply, mul_zero] }, .. locally_constant.has_zero, .. locally_constant.has_mul } instance [mul_zero_one_class Y] : mul_zero_one_class (locally_constant X Y) := { .. locally_constant.mul_zero_class, .. locally_constant.mul_one_class } @[to_additive] instance [has_div Y] : has_div (locally_constant X Y) := { div := λ f g, ⟨f / g, f.is_locally_constant.div g.is_locally_constant⟩ } @[to_additive] lemma div_apply [has_div Y] (f g : locally_constant X Y) (x : X) : (f / g) x = f x / g x := rfl @[to_additive] instance [semigroup Y] : semigroup (locally_constant X Y) := { mul_assoc := by { intros, ext, simp only [mul_apply, mul_assoc] }, .. locally_constant.has_mul } instance [semigroup_with_zero Y] : semigroup_with_zero (locally_constant X Y) := { .. locally_constant.mul_zero_class, .. locally_constant.semigroup } @[to_additive] instance [comm_semigroup Y] : comm_semigroup (locally_constant X Y) := { mul_comm := by { intros, ext, simp only [mul_apply, mul_comm] }, .. locally_constant.semigroup } @[to_additive] instance [monoid Y] : monoid (locally_constant X Y) := { mul := (*), .. locally_constant.semigroup, .. locally_constant.mul_one_class } @[to_additive] instance [comm_monoid Y] : comm_monoid (locally_constant X Y) := { .. locally_constant.comm_semigroup, .. locally_constant.monoid } @[to_additive] instance [group Y] : group (locally_constant X Y) := { mul_left_inv := by { intros, ext, simp only [mul_apply, inv_apply, one_apply, mul_left_inv] }, div_eq_mul_inv := by { intros, ext, simp only [mul_apply, inv_apply, div_apply, div_eq_mul_inv] }, .. locally_constant.monoid, .. locally_constant.has_inv, .. locally_constant.has_div } @[to_additive] instance [comm_group Y] : comm_group (locally_constant X Y) := { .. locally_constant.comm_monoid, .. locally_constant.group } instance [distrib Y] : distrib (locally_constant X Y) := { left_distrib := by { intros, ext, simp only [mul_apply, add_apply, mul_add] }, right_distrib := by { intros, ext, simp only [mul_apply, add_apply, add_mul] }, .. locally_constant.has_add, .. locally_constant.has_mul } instance [non_unital_non_assoc_semiring Y] : non_unital_non_assoc_semiring (locally_constant X Y) := { .. locally_constant.add_comm_monoid, .. locally_constant.has_mul, .. locally_constant.distrib, .. locally_constant.mul_zero_class } instance [non_unital_semiring Y] : non_unital_semiring (locally_constant X Y) := { .. locally_constant.semigroup, .. locally_constant.non_unital_non_assoc_semiring } instance [non_assoc_semiring Y] : non_assoc_semiring (locally_constant X Y) := { .. locally_constant.mul_one_class, .. locally_constant.non_unital_non_assoc_semiring } instance [semiring Y] : semiring (locally_constant X Y) := { .. locally_constant.add_comm_monoid, .. locally_constant.monoid, .. locally_constant.distrib, .. locally_constant.mul_zero_class } instance [comm_semiring Y] : comm_semiring (locally_constant X Y) := { .. locally_constant.semiring, .. locally_constant.comm_monoid } instance [ring Y] : ring (locally_constant X Y) := { .. locally_constant.semiring, .. locally_constant.add_comm_group } instance [comm_ring Y] : comm_ring (locally_constant X Y) := { .. locally_constant.comm_semiring, .. locally_constant.ring } end locally_constant
f1266e599a5f28a64c7225d6c0cb51b736c7b01a
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/topology/instances/nnreal.lean
ecc2a360c181ed79a1b19b65302fcabc062514d6
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,325
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import topology.algebra.infinite_sum import topology.algebra.group_with_zero /-! # Topology on `ℝ≥0` The natural topology on `ℝ≥0` (the one induced from `ℝ`), and a basic API. ## Main definitions Instances for the following typeclasses are defined: * `topological_space ℝ≥0` * `topological_ring ℝ≥0` * `second_countable_topology ℝ≥0` * `order_topology ℝ≥0` * `has_continuous_sub ℝ≥0` * `has_continuous_inv' ℝ≥0` (continuity of `x⁻¹` away from `0`) * `has_continuous_smul ℝ≥0 ℝ` Everything is inherited from the corresponding structures on the reals. ## Main statements Various mathematically trivial lemmas are proved about the compatibility of limits and sums in `ℝ≥0` and `ℝ`. For example * `tendsto_coe {f : filter α} {m : α → ℝ≥0} {x : ℝ≥0} : tendsto (λa, (m a : ℝ)) f (𝓝 (x : ℝ)) ↔ tendsto m f (𝓝 x)` says that the limit of a filter along a map to `ℝ≥0` is the same in `ℝ` and `ℝ≥0`, and * `coe_tsum {f : α → ℝ≥0} : ((∑'a, f a) : ℝ) = (∑'a, (f a : ℝ))` says that says that a sum of elements in `ℝ≥0` is the same in `ℝ` and `ℝ≥0`. Similarly, some mathematically trivial lemmas about infinite sums are proved, a few of which rely on the fact that subtraction is continuous. -/ noncomputable theory open set topological_space metric filter open_locale topological_space namespace nnreal open_locale nnreal big_operators filter instance : topological_space ℝ≥0 := infer_instance -- short-circuit type class inference instance : topological_ring ℝ≥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 real.to_nnreal := 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, real.to_nnreal (m a)) f (𝓝 (real.to_nnreal x)) := (continuous_of_real.tendsto _).comp h lemma nhds_zero : 𝓝 (0 : ℝ≥0) = ⨅a ≠ 0, 𝓟 (Iio a) := nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot, Iio] lemma nhds_zero_basis : (𝓝 (0 : ℝ≥0)).has_basis (λ a : ℝ≥0, 0 < a) (λ a, Iio a) := nhds_bot_basis 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⟩ instance : has_continuous_smul ℝ≥0 ℝ := { continuous_smul := continuous.comp real.continuous_mul $ continuous.prod_mk (continuous.comp continuous_subtype_val continuous_fst) continuous_snd } @[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, real.to_nnreal (f n)) (real.to_nnreal (∑' n, f n)) := begin have h_sum : (λ s, ∑ b in s, real.to_nnreal (f b)) = λ s, real.to_nnreal (∑ b in s, f b), from funext (λ _, (real.to_nnreal_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 lemma summable_coe_of_nonneg {f : α → ℝ} (hf₁ : ∀ n, 0 ≤ f n) : @summable (ℝ≥0) _ _ _ (λ n, ⟨f n, hf₁ n⟩) ↔ summable f := begin lift f to α → ℝ≥0 using hf₁ with f rfl hf₁, simp only [summable_coe, subtype.coe_eta] 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 coe_tsum_of_nonneg {f : α → ℝ} (hf₁ : ∀ n, 0 ≤ f n) : (⟨∑' n, f n, tsum_nonneg hf₁⟩ : ℝ≥0) = (∑' n, ⟨f n, hf₁ n⟩ : ℝ≥0) := begin lift f to α → ℝ≥0 using hf₁ with f rfl hf₁, simp_rw [← nnreal.coe_tsum, subtype.coe_eta] end 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 has_sum_nat_add_iff {f : ℕ → ℝ≥0} (k : ℕ) {a : ℝ≥0} : has_sum (λ n, f (n + k)) a ↔ has_sum f (a + ∑ i in range k, f i) := by simp [← has_sum_coe, coe_sum, nnreal.coe_add, ← has_sum_nat_add_iff k] 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 lemma tendsto_cofinite_zero_of_summable {α} {f : α → ℝ≥0} (hf : summable f) : tendsto f cofinite (𝓝 0) := begin have h_f_coe : f = λ n, real.to_nnreal (f n : ℝ), from funext (λ n, real.to_nnreal_coe.symm), rw [h_f_coe, ← @real.to_nnreal_coe 0], exact tendsto_of_real ((summable_coe.mpr hf).tendsto_cofinite_zero), end lemma tendsto_at_top_zero_of_summable {f : ℕ → ℝ≥0} (hf : summable f) : tendsto f at_top (𝓝 0) := by { rw ←nat.cofinite_eq_at_top, exact tendsto_cofinite_zero_of_summable hf } end nnreal
ada238ebf6428d1be8954d9ae76dee9e4d6cd8e0
46125763b4dbf50619e8846a1371029346f4c3db
/src/algebra/group_power.lean
64803ec223a3198f4e7f14f49b8ba89beac1cadb
[ "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
26,442
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 The power operation on monoids and groups. We separate this from group, because it depends on nat, which in turn depends on other parts of algebra. We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation a^n is used for the first, but users can locally redefine it to gpow when needed. Note: power adopts the convention that 0^0=1. -/ import data.int.basic variables {M : Type*} {N : Type*} {G : Type*} {H : Type*} {A : Type*} {B : Type*} {R : Type*} {S : Type*} /-- The power operation in a monoid. `a^n = a*a*...*a` n times. -/ def monoid.pow [monoid M] (a : M) : ℕ → M | 0 := 1 | (n+1) := a * monoid.pow n def add_monoid.smul [add_monoid A] (n : ℕ) (a : A) : A := @monoid.pow (multiplicative A) _ a n precedence `•`:70 localized "infix ` • ` := add_monoid.smul" in add_monoid @[priority 5] instance monoid.has_pow [monoid M] : has_pow M ℕ := ⟨monoid.pow⟩ /-! ### (Additive) monoid -/ section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] @[simp] theorem pow_zero (a : M) : a^0 = 1 := rfl @[simp] theorem add_monoid.zero_smul (a : A) : 0 • a = 0 := rfl theorem pow_succ (a : M) (n : ℕ) : a^(n+1) = a * a^n := rfl theorem succ_smul (a : A) (n : ℕ) : (n+1)•a = a + n•a := rfl @[simp] theorem pow_one (a : M) : a^1 = a := mul_one _ @[simp] theorem add_monoid.one_smul (a : A) : 1•a = a := add_zero _ theorem pow_mul_comm' (a : M) (n : ℕ) : a^n * a = a * a^n := by induction n with n ih; [rw [pow_zero, one_mul, mul_one], rw [pow_succ, mul_assoc, ih]] theorem smul_add_comm' : ∀ (a : A) (n : ℕ), n•a + a = a + n•a := @pow_mul_comm' (multiplicative A) _ theorem pow_succ' (a : M) (n : ℕ) : a^(n+1) = a^n * a := by rw [pow_succ, pow_mul_comm'] theorem succ_smul' (a : A) (n : ℕ) : (n+1)•a = n•a + a := by rw [succ_smul, smul_add_comm'] theorem pow_two (a : M) : a^2 = a * a := show a*(a*1)=a*a, by rw mul_one theorem two_smul (a : A) : 2•a = a + a := show a+(a+0)=a+a, by rw add_zero theorem pow_add (a : M) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [add_zero, pow_zero, mul_one], rw [pow_succ, ← pow_mul_comm', ← mul_assoc, ← ih, ← pow_succ']]; refl theorem add_monoid.add_smul : ∀ (a : A) (m n : ℕ), (m + n)•a = m•a + n•a := @pow_add (multiplicative A) _ @[simp] theorem one_pow (n : ℕ) : (1 : M)^n = 1 := by induction n with n ih; [refl, rw [pow_succ, ih, one_mul]] @[simp] theorem add_monoid.smul_zero (n : ℕ) : n•(0 : A) = 0 := by induction n with n ih; [refl, rw [succ_smul, ih, zero_add]] theorem pow_mul (a : M) (m n : ℕ) : a^(m * n) = (a^m)^n := by induction n with n ih; [rw mul_zero, rw [nat.mul_succ, pow_add, pow_succ', ih]]; refl theorem add_monoid.mul_smul' : ∀ (a : A) (m n : ℕ), m * n • a = n•(m•a) := @pow_mul (multiplicative A) _ theorem pow_mul' (a : M) (m n : ℕ) : a^(m * n) = (a^n)^m := by rw [mul_comm, pow_mul] theorem add_monoid.mul_smul (a : A) (m n : ℕ) : m * n • a = m•(n•a) := by rw [mul_comm, add_monoid.mul_smul'] @[simp] theorem add_monoid.smul_one [has_one A] : ∀ n : ℕ, n • (1 : A) = n := nat.eq_cast _ (add_monoid.zero_smul _) (add_monoid.one_smul _) (add_monoid.add_smul _) theorem pow_bit0 (a : M) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ theorem bit0_smul (a : A) (n : ℕ) : bit0 n • a = n•a + n•a := add_monoid.add_smul _ _ _ theorem pow_bit1 (a : M) (n : ℕ) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] theorem bit1_smul : ∀ (a : A) (n : ℕ), bit1 n • a = n•a + n•a + a := @pow_bit1 (multiplicative A) _ theorem pow_mul_comm (a : M) (m n : ℕ) : a^m * a^n = a^n * a^m := by rw [←pow_add, ←pow_add, add_comm] theorem smul_add_comm : ∀ (a : A) (m n : ℕ), m•a + n•a = n•a + m•a := @pow_mul_comm (multiplicative A) _ @[simp, priority 500] theorem list.prod_repeat (a : M) (n : ℕ) : (list.repeat a n).prod = a ^ n := by induction n with n ih; [refl, rw [list.repeat_succ, list.prod_cons, ih]]; refl @[simp, priority 500] theorem list.sum_repeat : ∀ (a : A) (n : ℕ), (list.repeat a n).sum = n • a := @list.prod_repeat (multiplicative A) _ theorem monoid_hom.map_pow (f : M →* N) (a : M) : ∀(n : ℕ), f (a ^ n) = (f a) ^ n | 0 := f.map_one | (n+1) := by rw [pow_succ, pow_succ, f.map_mul, monoid_hom.map_pow] theorem add_monoid_hom.map_smul (f : A →+ B) (a : A) (n : ℕ) : f (n • a) = n • f a := f.to_multiplicative.map_pow a n theorem is_monoid_hom.map_pow (f : M → N) [is_monoid_hom f] (a : M) : ∀(n : ℕ), f (a ^ n) = (f a) ^ n := (monoid_hom.of f).map_pow a theorem is_add_monoid_hom.map_smul (f : A → B) [is_add_monoid_hom f] (a : A) (n : ℕ) : f (n • a) = n • f a := (add_monoid_hom.of f).map_smul a n @[simp] lemma units.coe_pow (u : units M) (n : ℕ) : ((u ^ n : units M) : M) = u ^ n := (units.coe_hom M).map_pow u n end monoid @[simp] theorem nat.pow_eq_pow (p q : ℕ) : @has_pow.pow _ _ monoid.has_pow p q = p ^ q := by induction q with q ih; [refl, rw [nat.pow_succ, pow_succ, mul_comm, ih]] @[simp] theorem nat.smul_eq_mul (m n : ℕ) : m • n = m * n := by induction m with m ih; [rw [add_monoid.zero_smul, zero_mul], rw [succ_smul', ih, nat.succ_mul]] /-! ### Commutative (additive) monoid -/ section comm_monoid variables [comm_monoid M] [add_comm_monoid A] theorem mul_pow (a b : M) (n : ℕ) : (a * b)^n = a^n * b^n := by induction n with n ih; [exact (mul_one _).symm, simp only [pow_succ, ih, mul_assoc, mul_left_comm]] theorem add_monoid.smul_add : ∀ (a b : A) (n : ℕ), n•(a + b) = n•a + n•b := @mul_pow (multiplicative A) _ instance pow.is_monoid_hom (n : ℕ) : is_monoid_hom ((^ n) : M → M) := { map_mul := λ _ _, mul_pow _ _ _, map_one := one_pow _ } instance add_monoid.smul.is_add_monoid_hom (n : ℕ) : is_add_monoid_hom (add_monoid.smul n : A → A) := { map_add := λ _ _, add_monoid.smul_add _ _ _, map_zero := add_monoid.smul_zero _ } end comm_monoid section group variables [group G] [group H] [add_group A] [add_group B] section nat @[simp] theorem inv_pow (a : G) (n : ℕ) : (a⁻¹)^n = (a^n)⁻¹ := by induction n with n ih; [exact one_inv.symm, rw [pow_succ', pow_succ, ih, mul_inv_rev]] @[simp] theorem add_monoid.neg_smul : ∀ (a : A) (n : ℕ), n•(-a) = -(n•a) := @inv_pow (multiplicative A) _ theorem pow_sub (a : G) {m n : ℕ} (h : n ≤ m) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 theorem add_monoid.smul_sub : ∀ (a : A) {m n : ℕ}, n ≤ m → (m - n)•a = m•a - n•a := @pow_sub (multiplicative A) _ theorem pow_inv_comm (a : G) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := by rw inv_pow; exact inv_comm_of_comm (pow_mul_comm _ _ _) theorem add_monoid.smul_neg_comm : ∀ (a : A) (m n : ℕ), m•(-a) + n•a = n•a + m•(-a) := @pow_inv_comm (multiplicative A) _ end nat open int /-- The power operation in a group. This extends `monoid.pow` to negative integers with the definition `a^(-n) = (a^n)⁻¹`. -/ def gpow (a : G) : ℤ → G | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ def gsmul (n : ℤ) (a : A) : A := @gpow (multiplicative A) _ a n @[priority 10] instance group.has_pow : has_pow G ℤ := ⟨gpow⟩ localized "infix ` • `:70 := gsmul" in add_group localized "infix ` •ℕ `:70 := add_monoid.smul" in smul localized "infix ` •ℤ `:70 := gsmul" in smul @[simp] theorem gpow_coe_nat (a : G) (n : ℕ) : a ^ (n:ℤ) = a ^ n := rfl @[simp] theorem gsmul_coe_nat (a : A) (n : ℕ) : (n:ℤ) • a = n •ℕ a := rfl theorem gpow_of_nat (a : G) (n : ℕ) : a ^ of_nat n = a ^ n := rfl theorem gsmul_of_nat (a : A) (n : ℕ) : of_nat n • a = n •ℕ a := rfl @[simp] theorem gpow_neg_succ (a : G) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := rfl @[simp] theorem gsmul_neg_succ (a : A) (n : ℕ) : -[1+n] • a = - (n.succ •ℕ a) := rfl local attribute [ematch] le_of_lt open nat @[simp] theorem gpow_zero (a : G) : a ^ (0:ℤ) = 1 := rfl @[simp] theorem zero_gsmul (a : A) : (0:ℤ) • a = 0 := rfl @[simp] theorem gpow_one (a : G) : a ^ (1:ℤ) = a := mul_one _ @[simp] theorem one_gsmul (a : A) : (1:ℤ) • a = a := add_zero _ @[simp] theorem one_gpow : ∀ (n : ℤ), (1 : G) ^ n = 1 | (n : ℕ) := one_pow _ | -[1+ n] := show _⁻¹=(1:G), by rw [_root_.one_pow, one_inv] @[simp] theorem gsmul_zero : ∀ (n : ℤ), n • (0 : A) = 0 := @one_gpow (multiplicative A) _ @[simp] theorem gpow_neg (a : G) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := rfl | 0 := one_inv.symm | -[1+ n] := (inv_inv _).symm @[simp] theorem neg_gsmul : ∀ (a : A) (n : ℤ), -n • a = -(n • a) := @gpow_neg (multiplicative A) _ theorem gpow_neg_one (x : G) : x ^ (-1:ℤ) = x⁻¹ := congr_arg has_inv.inv $ pow_one x theorem neg_one_gsmul (x : A) : (-1:ℤ) • x = -x := congr_arg has_neg.neg $ add_monoid.one_smul x theorem gsmul_one [has_one A] (n : ℤ) : n • (1 : A) = n := by cases n; simp theorem inv_gpow (a : G) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) := inv_pow a n | -[1+ n] := congr_arg has_inv.inv $ inv_pow a (n+1) theorem gsmul_neg (a : A) (n : ℤ) : gsmul n (- a) = - gsmul n a := @inv_gpow (multiplicative A) _ a n private lemma gpow_add_aux (a : G) (m n : nat) : a ^ ((of_nat m) + -[1+n]) = a ^ of_nat m * a ^ -[1+n] := or.elim (nat.lt_or_ge m (nat.succ n)) (assume h1 : m < succ n, have h2 : m ≤ n, from le_of_lt_succ h1, suffices a ^ -[1+ n-m] = a ^ of_nat m * a ^ -[1+n], by rwa [of_nat_add_neg_succ_of_nat_of_lt h1], show (a ^ nat.succ (n - m))⁻¹ = a ^ of_nat m * a ^ -[1+n], by rw [← succ_sub h2, pow_sub _ (le_of_lt h1), mul_inv_rev, inv_inv]; refl) (assume : m ≥ succ n, suffices a ^ (of_nat (m - succ n)) = (a ^ (of_nat m)) * (a ^ -[1+ n]), by rw [of_nat_add_neg_succ_of_nat_of_ge]; assumption, suffices a ^ (m - succ n) = a ^ m * (a ^ n.succ)⁻¹, from this, by rw pow_sub; assumption) theorem gpow_add (a : G) : ∀ (i j : ℤ), a ^ (i + j) = a ^ i * a ^ j | (of_nat m) (of_nat n) := pow_add _ _ _ | (of_nat m) -[1+n] := gpow_add_aux _ _ _ | -[1+m] (of_nat n) := by rw [add_comm, gpow_add_aux, gpow_neg_succ, gpow_of_nat, ← inv_pow, ← pow_inv_comm] | -[1+m] -[1+n] := suffices (a ^ (m + succ (succ n)))⁻¹ = (a ^ m.succ)⁻¹ * (a ^ n.succ)⁻¹, from this, by rw [← succ_add_eq_succ_add, add_comm, _root_.pow_add, mul_inv_rev] theorem add_gsmul : ∀ (a : A) (i j : ℤ), (i + j) • a = i • a + j • a := @gpow_add (multiplicative A) _ theorem gpow_add_one (a : G) (i : ℤ) : a ^ (i + 1) = a ^ i * a := by rw [gpow_add, gpow_one] theorem add_one_gsmul : ∀ (a : A) (i : ℤ), (i + 1) • a = i • a + a := @gpow_add_one (multiplicative A) _ theorem gpow_one_add (a : G) (i : ℤ) : a ^ (1 + i) = a * a ^ i := by rw [gpow_add, gpow_one] theorem one_add_gsmul : ∀ (a : A) (i : ℤ), (1 + i) • a = a + i • a := @gpow_one_add (multiplicative A) _ theorem gpow_mul_comm (a : G) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← gpow_add, ← gpow_add, add_comm] theorem gsmul_add_comm : ∀ (a : A) (i j), i • a + j • a = j • a + i • a := @gpow_mul_comm (multiplicative A) _ theorem gpow_mul (a : G) : ∀ m n : ℤ, a ^ (m * n) = (a ^ m) ^ n | (m : ℕ) (n : ℕ) := pow_mul _ _ _ | (m : ℕ) -[1+ n] := (gpow_neg _ (m * succ n)).trans $ show (a ^ (m * succ n))⁻¹ = _, by rw pow_mul; refl | -[1+ m] (n : ℕ) := (gpow_neg _ (succ m * n)).trans $ show (a ^ (m.succ * n))⁻¹ = _, by rw [pow_mul, ← inv_pow]; refl | -[1+ m] -[1+ n] := (pow_mul a (succ m) (succ n)).trans $ show _ = (_⁻¹^_)⁻¹, by rw [inv_pow, inv_inv] theorem gsmul_mul' : ∀ (a : A) (m n : ℤ), m * n • a = n • (m • a) := @gpow_mul (multiplicative A) _ theorem gpow_mul' (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, gpow_mul] theorem gsmul_mul (a : A) (m n : ℤ) : m * n • a = m • (n • a) := by rw [mul_comm, gsmul_mul'] theorem gpow_bit0 (a : G) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _ theorem bit0_gsmul (a : A) (n : ℤ) : bit0 n • a = n • a + n • a := gpow_add _ _ _ theorem gpow_bit1 (a : G) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, gpow_add]; simp [gpow_bit0] theorem bit1_gsmul : ∀ (a : A) (n : ℤ), bit1 n • a = n • a + n • a + a := @gpow_bit1 (multiplicative A) _ theorem monoid_hom.map_gpow (f : G →* H) (a : G) (n : ℤ) : f (a ^ n) = f a ^ n := by cases n; [exact f.map_pow _ _, exact (f.map_inv _).trans (congr_arg _ $ f.map_pow _ _)] theorem add_monoid_hom.map_gsmul (f : A →+ B) (a : A) (n : ℤ) : f (n • a) = n • f a := f.to_multiplicative.map_gpow a n end group open_locale smul section comm_group variables [comm_group G] [add_comm_group A] theorem mul_gpow (a b : G) : ∀ n:ℤ, (a * b)^n = a^n * b^n | (n : ℕ) := mul_pow a b n | -[1+ n] := show _⁻¹=_⁻¹*_⁻¹, by rw [mul_pow, mul_inv_rev, mul_comm] theorem gsmul_add : ∀ (a b : A) (n : ℤ), n •ℤ (a + b) = n •ℤ a + n •ℤ b := @mul_gpow (multiplicative A) _ theorem gsmul_sub (a b : A) (n : ℤ) : gsmul n (a - b) = gsmul n a - gsmul n b := by simp only [gsmul_add, gsmul_neg, sub_eq_add_neg] instance gpow.is_group_hom (n : ℤ) : is_group_hom ((^ n) : G → G) := { map_mul := λ _ _, mul_gpow _ _ n } instance gsmul.is_add_group_hom (n : ℤ) : is_add_group_hom (gsmul n : A → A) := { map_add := λ _ _, gsmul_add _ _ n } end comm_group @[simp] lemma with_bot.coe_smul [add_monoid A] (a : A) (n : ℕ) : ((add_monoid.smul n a : A) : with_bot A) = add_monoid.smul n a := add_monoid_hom.map_smul ⟨_, with_bot.coe_zero, with_bot.coe_add⟩ a n theorem add_monoid.smul_eq_mul' [semiring R] (a : R) (n : ℕ) : n • a = a * n := by induction n with n ih; [rw [add_monoid.zero_smul, nat.cast_zero, mul_zero], rw [succ_smul', ih, nat.cast_succ, mul_add, mul_one]] theorem add_monoid.smul_eq_mul [semiring R] (n : ℕ) (a : R) : n • a = n * a := by rw [add_monoid.smul_eq_mul', nat.mul_cast_comm] theorem add_monoid.mul_smul_left [semiring R] (a b : R) (n : ℕ) : n • (a * b) = a * (n • b) := by rw [add_monoid.smul_eq_mul', add_monoid.smul_eq_mul', mul_assoc] theorem add_monoid.mul_smul_assoc [semiring R] (a b : R) (n : ℕ) : n • (a * b) = n • a * b := by rw [add_monoid.smul_eq_mul, add_monoid.smul_eq_mul, mul_assoc] lemma zero_pow [semiring R] : ∀ {n : ℕ}, 0 < n → (0 : R) ^ n = 0 | (n+1) _ := zero_mul _ @[simp, move_cast] theorem nat.cast_pow [semiring R] (n m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m := by induction m with m ih; [exact nat.cast_one, rw [nat.pow_succ, pow_succ', nat.cast_mul, ih]] @[simp, move_cast] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [nat.pow_succ, pow_succ', int.coe_nat_mul, ih]] theorem int.nat_abs_pow (n : ℤ) (k : ℕ) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k := by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, nat.pow_succ, ih]] @[simp] lemma ring_hom.map_pow [semiring R] [semiring S] (f : R →+* S) (a) : ∀ n : ℕ, f (a ^ n) = (f a) ^ n := f.to_monoid_hom.map_pow a lemma is_semiring_hom.map_pow [semiring R] [semiring S] (f : R → S) [is_semiring_hom f] (a) : ∀ n : ℕ, f (a ^ n) = (f a) ^ n := is_monoid_hom.map_pow f a theorem neg_one_pow_eq_or [ring R] : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl rfl | (n+1) := (neg_one_pow_eq_or n).swap.imp (λ h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (λ h, by rw [pow_succ, h, mul_one]) lemma pow_dvd_pow [comm_semiring R] (a : R) {m n : ℕ} (h : m ≤ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_sub_cancel' h]⟩ theorem gsmul_eq_mul [ring R] (a : R) : ∀ n, n •ℤ a = n * a | (n : ℕ) := add_monoid.smul_eq_mul _ _ | -[1+ n] := show -(_•_)=-_*_, by rw [neg_mul_eq_neg_mul_symm, add_monoid.smul_eq_mul, nat.cast_succ] theorem gsmul_eq_mul' [ring R] (a : R) (n : ℤ) : n •ℤ a = a * n := by rw [gsmul_eq_mul, int.mul_cast_comm] theorem mul_gsmul_left [ring R] (a b : R) (n : ℤ) : n •ℤ (a * b) = a * (n •ℤ b) := by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc] theorem mul_gsmul_assoc [ring R] (a b : R) (n : ℤ) : n •ℤ (a * b) = n •ℤ a * b := by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc] @[simp, move_cast] theorem int.cast_pow [ring R] (n : ℤ) (m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m := by induction m with m ih; [exact int.cast_one, rw [pow_succ, pow_succ, int.cast_mul, ih]] lemma neg_one_pow_eq_pow_mod_two [ring R] {n : ℕ} : (-1 : R) ^ n = -1 ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two] theorem sq_sub_sq [comm_ring R] (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [pow_two, pow_two, mul_self_sub_mul_self] theorem pow_eq_zero [domain R] {x : R} {n : ℕ} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, exact or.cases_on (mul_eq_zero.1 H) id ih end @[field_simps] theorem pow_ne_zero [domain R] {a : R} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h theorem one_div_pow [division_ring R] {a : R} (n : ℕ) : (1 / a) ^ n = 1 / a ^ n := by induction n with n ih; [exact (div_one _).symm, rw [pow_succ', ih, division_ring.one_div_mul_one_div]]; refl @[simp] theorem division_ring.inv_pow [division_ring R] {a : R} (n : ℕ) : a⁻¹ ^ n = (a ^ n)⁻¹ := by simpa only [inv_eq_one_div] using one_div_pow n @[simp] theorem div_pow [field R] (a : R) {b : R} (n : ℕ) : (a / b) ^ n = a ^ n / b ^ n := by rw [div_eq_mul_one_div, mul_pow, one_div_pow, ← div_eq_mul_one_div] theorem add_monoid.smul_nonneg [ordered_comm_monoid R] {a : R} (H : 0 ≤ a) : ∀ n : ℕ, 0 ≤ n • a | 0 := le_refl _ | (n+1) := add_nonneg' H (add_monoid.smul_nonneg n) lemma pow_abs [decidable_linear_ordered_comm_ring R] (a : R) (n : ℕ) : (abs a)^n = abs (a^n) := by induction n with n ih; [exact (abs_one).symm, rw [pow_succ, pow_succ, ih, abs_mul]] lemma abs_neg_one_pow [decidable_linear_ordered_comm_ring R] (n : ℕ) : abs ((-1 : R)^n) = 1 := by rw [←pow_abs, abs_neg, abs_one, one_pow] @[field_simps] lemma inv_pow' [field R] (a : R) (n : ℕ) : a⁻¹ ^ n = (a ^ n)⁻¹ := by induction n; simp [*, pow_succ, mul_inv', mul_comm] @[field_simps] lemma pow_div [field R] (a b : R) (n : ℕ) : (a / b)^n = a^n / b^n := by simp [div_eq_mul_inv, mul_pow, inv_pow'] lemma pow_inv [division_ring R] (a : R) : ∀ n : ℕ, a ≠ 0 → (a^n)⁻¹ = (a⁻¹)^n | 0 ha := inv_one | (n+1) ha := by rw [pow_succ, pow_succ', mul_inv', pow_inv _ ha] namespace add_monoid variable [ordered_comm_monoid A] theorem smul_le_smul {a : A} {n m : ℕ} (ha : 0 ≤ a) (h : n ≤ m) : n • a ≤ m • a := let ⟨k, hk⟩ := nat.le.dest h in calc n • a = n • a + 0 : (add_zero _).symm ... ≤ n • a + k • a : add_le_add_left' (smul_nonneg ha _) ... = m • a : by rw [← hk, add_smul] lemma smul_le_smul_of_le_right {a b : A} (hab : a ≤ b) : ∀ i : ℕ, i • a ≤ i • b | 0 := by simp | (k+1) := add_le_add' hab (smul_le_smul_of_le_right _) end add_monoid namespace canonically_ordered_semiring variable [canonically_ordered_comm_semiring R] theorem pow_pos {a : R} (H : 0 < a) : ∀ n : ℕ, 0 < a ^ n | 0 := canonically_ordered_semiring.zero_lt_one | (n+1) := canonically_ordered_semiring.mul_pos.2 ⟨H, pow_pos n⟩ lemma pow_le_pow_of_le_left {a b : R} (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i | 0 := by simp | (k+1) := canonically_ordered_semiring.mul_le_mul hab (pow_le_pow_of_le_left k) theorem one_le_pow_of_one_le {a : R} (H : 1 ≤ a) (n : ℕ) : 1 ≤ a ^ n := by simpa only [one_pow] using pow_le_pow_of_le_left H n theorem pow_le_one {a : R} (H : a ≤ 1) (n : ℕ) : a ^ n ≤ 1:= by simpa only [one_pow] using pow_le_pow_of_le_left H n end canonically_ordered_semiring section linear_ordered_semiring variable [linear_ordered_semiring R] theorem pow_pos {a : R} (H : 0 < a) : ∀ (n : ℕ), 0 < a ^ n | 0 := zero_lt_one | (n+1) := mul_pos H (pow_pos _) theorem pow_nonneg {a : R} (H : 0 ≤ a) : ∀ (n : ℕ), 0 ≤ a ^ n | 0 := zero_le_one | (n+1) := mul_nonneg H (pow_nonneg _) theorem pow_lt_pow_of_lt_left {x y : R} {n : ℕ} (Hxy : x < y) (Hxpos : 0 ≤ x) (Hnpos : 0 < n) : x ^ n < y ^ n := begin cases lt_or_eq_of_le Hxpos, { rw ←nat.sub_add_cancel Hnpos, induction (n - 1), { simpa only [pow_one] }, rw [pow_add, pow_add, nat.succ_eq_add_one, pow_one, pow_one], apply mul_lt_mul ih (le_of_lt Hxy) h (le_of_lt (pow_pos (lt_trans h Hxy) _)) }, { rw [←h, zero_pow Hnpos], apply pow_pos (by rwa ←h at Hxy : 0 < y),} end theorem pow_right_inj {x y : R} {n : ℕ} (Hxpos : 0 ≤ x) (Hypos : 0 ≤ y) (Hnpos : 0 < n) (Hxyn : x ^ n = y ^ n) : x = y := begin rcases lt_trichotomy x y with hxy | rfl | hyx, { exact absurd Hxyn (ne_of_lt (pow_lt_pow_of_lt_left hxy Hxpos Hnpos)) }, { refl }, { exact absurd Hxyn (ne_of_gt (pow_lt_pow_of_lt_left hyx Hypos Hnpos)) }, end theorem one_le_pow_of_one_le {a : R} (H : 1 ≤ a) : ∀ (n : ℕ), 1 ≤ a ^ n | 0 := le_refl _ | (n+1) := by simpa only [mul_one] using mul_le_mul H (one_le_pow_of_one_le n) zero_le_one (le_trans zero_le_one H) /-- Bernoulli's inequality. This version works for semirings but requires an additional hypothesis `0 ≤ a * a`. -/ theorem one_add_mul_le_pow' {a : R} (Hsqr : 0 ≤ a * a) (H : 0 ≤ 1 + a) : ∀ (n : ℕ), 1 + n • a ≤ (1 + a) ^ n | 0 := le_of_eq $ add_zero _ | (n+1) := calc 1 + (n + 1) • a ≤ (1 + a) * (1 + n • a) : by simpa [succ_smul, mul_add, add_mul, add_monoid.mul_smul_left, add_comm, add_left_comm] using add_monoid.smul_nonneg Hsqr n ... ≤ (1 + a)^(n+1) : mul_le_mul_of_nonneg_left (one_add_mul_le_pow' n) H theorem pow_le_pow {a : R} {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := let ⟨k, hk⟩ := nat.le.dest h in calc a ^ n = a ^ n * 1 : (mul_one _).symm ... ≤ a ^ n * a ^ k : mul_le_mul_of_nonneg_left (one_le_pow_of_one_le ha _) (pow_nonneg (le_trans zero_le_one ha) _) ... = a ^ m : by rw [←hk, pow_add] lemma pow_lt_pow {a : R} {n m : ℕ} (h : 1 < a) (h2 : n < m) : a ^ n < a ^ m := begin have h' : 1 ≤ a := le_of_lt h, have h'' : 0 < a := lt_trans zero_lt_one h, cases m, cases h2, rw [pow_succ, ←one_mul (a ^ n)], exact mul_lt_mul h (pow_le_pow h' (nat.le_of_lt_succ h2)) (pow_pos h'' _) (le_of_lt h'') end lemma pow_le_pow_of_le_left {a b : R} (ha : 0 ≤ a) (hab : a ≤ b) : ∀ i : ℕ, a^i ≤ b^i | 0 := by simp | (k+1) := mul_le_mul hab (pow_le_pow_of_le_left _) (pow_nonneg ha _) (le_trans ha hab) lemma lt_of_pow_lt_pow {a b : R} (n : ℕ) (hb : 0 ≤ b) (h : a ^ n < b ^ n) : a < b := lt_of_not_ge $ λ hn, not_lt_of_ge (pow_le_pow_of_le_left hb hn _) h private lemma pow_lt_pow_of_lt_one_aux {a : R} (h : 0 < a) (ha : a < 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k + 1) < a ^ i | 0 := begin simp only [add_zero], rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end | (k+1) := begin rw ←one_mul (a^i), apply mul_lt_mul ha _ _ zero_le_one, { apply le_of_lt, apply pow_lt_pow_of_lt_one_aux }, { show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h } end private lemma pow_le_pow_of_le_one_aux {a : R} (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k) ≤ a ^ i | 0 := by simp | (k+1) := by rw [←add_assoc, ←one_mul (a^i)]; exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one lemma pow_lt_pow_of_lt_one {a : R} (h : 0 < a) (ha : a < 1) {i j : ℕ} (hij : i < j) : a ^ j < a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _ lemma pow_le_pow_of_le_one {a : R} (h : 0 ≤ a) (ha : a ≤ 1) {i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_one {x : R} : ∀ (n : ℕ) (h0 : 0 ≤ x) (h1 : x ≤ 1), x ^ n ≤ 1 | 0 h0 h1 := le_refl (1 : R) | (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1) end linear_ordered_semiring theorem pow_two_nonneg [linear_ordered_ring R] (a : R) : 0 ≤ a ^ 2 := by { rw pow_two, exact mul_self_nonneg _ } /-- Bernoulli's inequality for `n : ℕ`, `-2 ≤ a`. -/ theorem one_add_mul_le_pow [linear_ordered_ring R] {a : R} (H : -2 ≤ a) : ∀ (n : ℕ), 1 + n • a ≤ (1 + a) ^ n | 0 := le_of_eq $ add_zero _ | 1 := by simp | (n+2) := have H' : 0 ≤ 2 + a, from neg_le_iff_add_nonneg.1 H, have 0 ≤ n • (a * a * (2 + a)) + a * a, from add_nonneg (add_monoid.smul_nonneg (mul_nonneg (mul_self_nonneg a) H') n) (mul_self_nonneg a), calc 1 + (n + 2) • a ≤ 1 + (n + 2) • a + (n • (a * a * (2 + a)) + a * a) : (le_add_iff_nonneg_right _).2 this ... = (1 + a) * (1 + a) * (1 + n • a) : by { simp only [add_mul, mul_add, mul_two, mul_one, one_mul, succ_smul, add_monoid.smul_add, add_monoid.mul_smul_assoc, (add_monoid.mul_smul_left _ _ _).symm], ac_refl } ... ≤ (1 + a) * (1 + a) * (1 + a)^n : mul_le_mul_of_nonneg_left (one_add_mul_le_pow n) (mul_self_nonneg (1 + a)) ... = (1 + a)^(n + 2) : by simp only [pow_succ, mul_assoc] /-- Bernoulli's inequality reformulated to estimate `a^n`. -/ theorem one_add_sub_mul_le_pow [linear_ordered_ring R] {a : R} (H : -1 ≤ a) (n : ℕ) : 1 + n • (a - 1) ≤ a ^ n := have -2 ≤ a - 1, by { rw [bit0, neg_add], exact sub_le_sub_right H 1 }, by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow this n namespace int lemma units_pow_two (u : units ℤ) : u ^ 2 = 1 := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) lemma units_pow_eq_pow_mod_two (u : units ℤ) (n : ℕ) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one] end int @[simp] lemma neg_square {α} [ring α] (z : α) : (-z)^2 = z^2 := by simp [pow, monoid.pow] lemma div_sq_cancel {α} [field α] {a : α} (ha : a ≠ 0) (b : α) : a^2 * b / a = a * b := by rw [pow_two, mul_assoc, mul_div_cancel_left _ ha]
56b2113c82709ad608a231911c06b09566f4750d
7da5ceac20aaab989eeb795a4be9639982e7b35a
/src/group_theory/conway_groups.lean
6d8be50ec7c8e18670845ce43fdc199cb84ee329
[ "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
4,306
lean
import ..data.dvector .presentation .monster .euclidean_lattice local notation `⟪`:50 a `⟫`:50 := free_group.of a local notation h :: t := dvector.cons h t local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l /- We define the Conway groups. Except for Co1, which is constructed as a quotient of the automorphism group of the Leech lattice, the Conway groups Co2 and Co3 admit general Coxeter-type presentations modulo additional relations (p. 134 and in the atlas)-/ open coxeter_vertices namespace conway_groups noncomputable def Co1 : Group := category_theory.mk_ob $ quotient_group.quotient $ is_subgroup.center $ lattice_Aut Λ_24 section Co2 def Co2_prediagram : annotated_graph := annotated_graph_of_graph $ coxeter_edges [1,1,3,1] instance Co2_prediagram_decidable_eq : decidable_eq Co2_prediagram.vertex := by apply_instance noncomputable instance Co2_prediagram_decidable_rel : decidable_rel Co2_prediagram.edge := λ _ _, classical.prop_decidable _ private def a : Co2_prediagram.vertex := arm (by to_dfin 2) (by to_dfin 1) private def b : Co2_prediagram.vertex := arm (by to_dfin 2) (by to_dfin 2) private def c : Co2_prediagram.vertex := arm (by to_dfin 1) (by to_dfin 0) private def d : Co2_prediagram.vertex := arm (by to_dfin 0) (by to_dfin 0) private def e : Co2_prediagram.vertex := arm (by to_dfin 2) (by to_dfin 0) private def f : Co2_prediagram.vertex := torso private def g : Co2_prediagram.vertex := arm (by to_dfin 3) (by to_dfin 0) noncomputable def Co2_diagram : annotated_graph := insert_edge (insert_edge (insert_edge (insert_edge (annotate (annotate (annotate Co2_prediagram (e,f) 6) (a,e) 4) (g,f) 4) (c,d) 4) (c,e) 3) (c,b) 5) (g,b) 4 noncomputable instance Co2_diagram_decidable_rel : decidable_rel Co2_diagram.edge := λ _ _, classical.prop_decidable _ noncomputable def Co2_diagram_group : Group := coxeter_group $ matrix_of_annotated_graph (Co2_diagram) private def a : Co2_diagram_group := generated_of a private def b : Co2_diagram_group := generated_of b private def c : Co2_diagram_group := generated_of c private def d : Co2_diagram_group := generated_of d private def e : Co2_diagram_group := generated_of e private def f : Co2_diagram_group := generated_of f private def g : Co2_diagram_group := generated_of g noncomputable def Co2 : Group := Co2_diagram_group/⟪{(c*f)^2 * a⁻¹, (e*f)^3 * b⁻¹, (b*g)^2 * e⁻¹, (a*e*c*d)^4, (c*e*f)^7, (b*a*e*f*g)^3}⟫ end Co2 section Co3 def Co3_prediagram : annotated_graph := annotated_graph_of_graph $ coxeter_edges [1,1,4] instance Co3_prediagram_decidable_eq : decidable_eq Co3_prediagram.vertex := by apply_instance noncomputable instance Co3_prediagram_decidable_rel : decidable_rel Co3_prediagram.edge := λ _ _, classical.prop_decidable _ private def a : Co3_prediagram.vertex := torso private def b : Co3_prediagram.vertex := arm (by to_dfin 1) (by to_dfin 0) private def c : Co3_prediagram.vertex := arm (by to_dfin 2) (by to_dfin 1) private def d : Co3_prediagram.vertex := arm (by to_dfin 2) (by to_dfin 2) private def e : Co3_prediagram.vertex := arm (by to_dfin 2) (by to_dfin 0) private def f : Co3_prediagram.vertex := arm (by to_dfin 2) (by to_dfin 3) private def g : Co3_prediagram.vertex := arm (by to_dfin 0) (by to_dfin 0) noncomputable def Co3_diagram : annotated_graph := insert_edge (insert_edge (insert_edge (insert_edge (insert_edge (annotate (Co3_prediagram) (a,e) 4) (g,b) 4) (g,f) 6) (b,c) 5) (f,e) 6) (f,c) 4 noncomputable instance Co3_diagram_decidable_rel : decidable_rel Co3_diagram.edge := λ _ _, classical.prop_decidable _ noncomputable def Co3_diagram_group : Group := coxeter_group $ matrix_of_annotated_graph (Co3_diagram) private def a : Co3_diagram_group := generated_of a private def b : Co3_diagram_group := generated_of b private def c : Co3_diagram_group := generated_of c private def d : Co3_diagram_group := generated_of d private def e : Co3_diagram_group := generated_of e private def f : Co3_diagram_group := generated_of f private def g : Co3_diagram_group := generated_of g noncomputable def Co3 : Group := Co3_diagram_group/⟪{(c * f)^2 * a ⁻¹, (e * f)^3 * b⁻¹, (b * g)^2 * d⁻¹, (g*a*e)^3*d⁻¹, (e*a*b)^3, (b*c*e)^5, (a*d*f*g)^3, (c*e*f)^7}⟫ end Co3 end conway_groups
6c662816f29e1bdf3420fd6b47580b7421014558
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/group_ring_action.lean
87b894dc1dd669eab6d6a10aaf2ec4016302ff92
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
9,328
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import group_theory.group_action import data.equiv.ring import data.polynomial.monic /-! # Group action on rings This file defines the typeclass of monoid acting on semirings `mul_semiring_action M R`, and the corresponding typeclass of invariant subrings. Note that `algebra` does not satisfy the axioms of `mul_semiring_action`. ## Implementation notes There is no separate typeclass for group acting on rings, group acting on fields, etc. They are all grouped under `mul_semiring_action`. ## Tags group action, invariant subring -/ universes u v open_locale big_operators /-- Typeclass for multiplicative actions by monoids on semirings. -/ class mul_semiring_action (M : Type u) [monoid M] (R : Type v) [semiring R] extends distrib_mul_action M R := (smul_one : ∀ (g : M), (g • 1 : R) = 1) (smul_mul : ∀ (g : M) (x y : R), g • (x * y) = (g • x) * (g • y)) export mul_semiring_action (smul_one) /-- Typeclass for faithful multiplicative actions by monoids on semirings. -/ class faithful_mul_semiring_action (M : Type u) [monoid M] (R : Type v) [semiring R] extends mul_semiring_action M R := (eq_of_smul_eq_smul' : ∀ {m₁ m₂ : M}, (∀ r : R, m₁ • r = m₂ • r) → m₁ = m₂) section semiring variables (M G : Type u) [monoid M] [group G] variables (A R S F : Type v) [add_monoid A] [semiring R] [comm_semiring S] [field F] variables {M R} lemma smul_mul' [mul_semiring_action M R] (g : M) (x y : R) : g • (x * y) = (g • x) * (g • y) := mul_semiring_action.smul_mul g x y variables {M} (R) theorem eq_of_smul_eq_smul [faithful_mul_semiring_action M R] {m₁ m₂ : M} : (∀ r : R, m₁ • r = m₂ • r) → m₁ = m₂ := faithful_mul_semiring_action.eq_of_smul_eq_smul' variables (M R) /-- Each element of the monoid defines a additive monoid homomorphism. -/ def distrib_mul_action.to_add_monoid_hom [distrib_mul_action M A] (x : M) : A →+ A := { to_fun := (•) x, map_zero' := smul_zero x, map_add' := smul_add x } /-- Each element of the group defines an additive monoid isomorphism. -/ def distrib_mul_action.to_add_equiv [distrib_mul_action G A] (x : G) : A ≃+ A := { .. distrib_mul_action.to_add_monoid_hom G A x, .. mul_action.to_perm G A x } /-- Each element of the group defines an additive monoid homomorphism. -/ def distrib_mul_action.hom_add_monoid_hom [distrib_mul_action M A] : M →* add_monoid.End A := { to_fun := distrib_mul_action.to_add_monoid_hom M A, map_one' := add_monoid_hom.ext $ λ x, one_smul M x, map_mul' := λ x y, add_monoid_hom.ext $ λ z, mul_smul x y z } /-- Each element of the monoid defines a semiring homomorphism. -/ def mul_semiring_action.to_semiring_hom [mul_semiring_action M R] (x : M) : R →+* R := { map_one' := smul_one x, map_mul' := smul_mul' x, .. distrib_mul_action.to_add_monoid_hom M R x } theorem injective_to_semiring_hom [faithful_mul_semiring_action M R] : function.injective (mul_semiring_action.to_semiring_hom M R) := λ m₁ m₂ h, eq_of_smul_eq_smul R $ λ r, ring_hom.ext_iff.1 h r /-- Each element of the group defines a semiring isomorphism. -/ def mul_semiring_action.to_semiring_equiv [mul_semiring_action G R] (x : G) : R ≃+* R := { .. distrib_mul_action.to_add_equiv G R x, .. mul_semiring_action.to_semiring_hom G R x } section prod variables [mul_semiring_action M R] [mul_semiring_action M S] lemma list.smul_prod (g : M) (L : list R) : g • L.prod = (L.map $ (•) g).prod := monoid_hom.map_list_prod (mul_semiring_action.to_semiring_hom M R g : R →* R) L lemma multiset.smul_prod (g : M) (m : multiset S) : g • m.prod = (m.map $ (•) g).prod := monoid_hom.map_multiset_prod (mul_semiring_action.to_semiring_hom M S g : S →* S) m lemma smul_prod (g : M) {ι : Type*} (f : ι → S) (s : finset ι) : g • ∏ i in s, f i = ∏ i in s, g • f i := monoid_hom.map_prod (mul_semiring_action.to_semiring_hom M S g : S →* S) f s end prod section simp_lemmas variables {M G A R} attribute [simp] smul_one smul_mul' smul_zero smul_add @[simp] lemma smul_inv [mul_semiring_action M F] (x : M) (m : F) : x • m⁻¹ = (x • m)⁻¹ := (mul_semiring_action.to_semiring_hom M F x).map_inv _ @[simp] lemma smul_pow [mul_semiring_action M R] (x : M) (m : R) (n : ℕ) : x • m ^ n = (x • m) ^ n := nat.rec_on n (smul_one x) $ λ n ih, (smul_mul' x m (m ^ n)).trans $ congr_arg _ ih end simp_lemmas namespace polynomial noncomputable instance [mul_semiring_action M S] : mul_semiring_action M (polynomial S) := { smul := λ m, map $ mul_semiring_action.to_semiring_hom M S m, one_smul := λ p, by { ext n, erw coeff_map, exact one_smul M (p.coeff n) }, mul_smul := λ m n p, by { ext i, iterate 3 { rw coeff_map (mul_semiring_action.to_semiring_hom M S _) }, exact mul_smul m n (p.coeff i) }, smul_add := λ m p q, map_add (mul_semiring_action.to_semiring_hom M S m), smul_zero := λ m, map_zero (mul_semiring_action.to_semiring_hom M S m), smul_one := λ m, map_one (mul_semiring_action.to_semiring_hom M S m), smul_mul := λ m p q, map_mul (mul_semiring_action.to_semiring_hom M S m), } noncomputable instance [faithful_mul_semiring_action M S] : faithful_mul_semiring_action M (polynomial S) := { eq_of_smul_eq_smul' := λ m₁ m₂ h, eq_of_smul_eq_smul S $ λ s, C_inj.1 $ calc C (m₁ • s) = m₁ • C s : (map_C $ mul_semiring_action.to_semiring_hom M S m₁).symm ... = m₂ • C s : h (C s) ... = C (m₂ • s) : map_C _, .. polynomial.mul_semiring_action M S } variables [mul_semiring_action M S] @[simp] lemma coeff_smul' (m : M) (p : polynomial S) (n : ℕ) : (m • p).coeff n = m • p.coeff n := coeff_map _ _ @[simp] lemma smul_C (m : M) (r : S) : m • C r = C (m • r) := map_C _ @[simp] lemma smul_X (m : M) : (m • X : polynomial S) = X := map_X _ theorem smul_eval_smul (m : M) (f : polynomial S) (x : S) : (m • f).eval (m • x) = m • f.eval x := polynomial.induction_on f (λ r, by rw [smul_C, eval_C, eval_C]) (λ f g ihf ihg, by rw [smul_add, eval_add, ihf, ihg, eval_add, smul_add]) (λ n r ih, by rw [smul_mul', smul_pow, smul_C, smul_X, eval_mul, eval_C, eval_pow, eval_X, eval_mul, eval_C, eval_pow, eval_X, smul_mul', smul_pow]) theorem eval_smul' [mul_semiring_action G S] (g : G) (f : polynomial S) (x : S) : f.eval (g • x) = g • (g⁻¹ • f).eval x := by rw [← smul_eval_smul, mul_action.smul_inv_smul] theorem smul_eval [mul_semiring_action G S] (g : G) (f : polynomial S) (x : S) : (g • f).eval x = g • f.eval (g⁻¹ • x) := by rw [← smul_eval_smul, mul_action.smul_inv_smul] end polynomial end semiring section ring variables (M : Type u) [monoid M] {R : Type v} [ring R] [mul_semiring_action M R] variables (S : set R) [is_subring S] open mul_action set_option old_structure_cmd false /-- A subring invariant under the action. -/ class is_invariant_subring : Prop := (smul_mem : ∀ (m : M) {x : R}, x ∈ S → m • x ∈ S) variables [is_invariant_subring M S] local attribute [instance] subset.ring instance is_invariant_subring.to_mul_semiring_action : mul_semiring_action M S := { smul := λ m x, ⟨m • x, is_invariant_subring.smul_mem m x.2⟩, one_smul := λ s, subtype.eq $ one_smul M s, mul_smul := λ m₁ m₂ s, subtype.eq $ mul_smul m₁ m₂ s, smul_add := λ m s₁ s₂, subtype.eq $ smul_add m s₁ s₂, smul_zero := λ m, subtype.eq $ smul_zero m, smul_one := λ m, subtype.eq $ smul_one m, smul_mul := λ m s₁ s₂, subtype.eq $ smul_mul' m s₁ s₂ } end ring section comm_ring variables (G : Type u) [group G] [fintype G] variables (R : Type v) [comm_ring R] [mul_semiring_action G R] open mul_action open_locale classical noncomputable instance (s : subgroup G) : fintype (quotient_group.quotient s) := quotient.fintype _ /-- the product of `(X - g • x)` over distinct `g • x`. -/ noncomputable def prod_X_sub_smul (x : R) : polynomial R := (finset.univ : finset (quotient_group.quotient $ mul_action.stabilizer G x)).prod $ λ g, polynomial.X - polynomial.C (of_quotient_stabilizer G x g) theorem prod_X_sub_smul.monic (x : R) : (prod_X_sub_smul G R x).monic := polynomial.monic_prod_of_monic _ _ $ λ g _, polynomial.monic_X_sub_C _ theorem prod_X_sub_smul.eval (x : R) : (prod_X_sub_smul G R x).eval x = 0 := (finset.prod_hom _ (polynomial.eval x)).symm.trans $ finset.prod_eq_zero (finset.mem_univ $ quotient_group.mk 1) $ by rw [of_quotient_stabilizer_mk, one_smul, polynomial.eval_sub, polynomial.eval_X, polynomial.eval_C, sub_self] theorem prod_X_sub_smul.smul (x : R) (g : G) : g • prod_X_sub_smul G R x = prod_X_sub_smul G R x := (smul_prod _ _ _ _ _).trans $ finset.prod_bij (λ g' _, g • g') (λ _ _, finset.mem_univ _) (λ g' _, by rw [of_quotient_stabilizer_smul, smul_sub, polynomial.smul_X, polynomial.smul_C]) (λ _ _ _ _ H, (mul_action.bijective g).1 H) (λ g' _, ⟨g⁻¹ • g', finset.mem_univ _, by rw [smul_smul, mul_inv_self, one_smul]⟩) theorem prod_X_sub_smul.coeff (x : R) (g : G) (n : ℕ) : g • (prod_X_sub_smul G R x).coeff n = (prod_X_sub_smul G R x).coeff n := by rw [← polynomial.coeff_smul', prod_X_sub_smul.smul] end comm_ring
d447e5c062e23fe726ab7f2617360ff7777e6f78
1d265c7dd8cb3d0e1d645a19fd6157a2084c3921
/src/lessons/lesson4.lean
42cc7a32ba80b777d4d7092f815a4c9ba2a5a554
[ "MIT" ]
permissive
hanzhi713/lean-proofs
de432372f220d302be09b5ca4227f8986567e4fd
4d8356a878645b9ba7cb036f87737f3f1e68ede5
refs/heads/master
1,585,580,245,658
1,553,646,623,000
1,553,646,623,000
151,342,188
0
1
null
null
null
null
UTF-8
Lean
false
false
311
lean
def is_even (x: ℕ) : bool := x % 2 = 0 def positive (n: ℕ) : bool := n > 0 def uint32 (n: ℕ) : bool := n >= 0 ∧ n < 2^32 def positive' : ℕ → bool := λ n, n > 0 #check positive' #check λ n : ℕ, n > 0 #reduce is_even 1 #reduce positive 0 theorem ttt {a b : ℕ}: a=b→ b=a := λ f, eq.symm f
1a478b22efa462200212419c941f1403c16289a6
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Init/Coe.lean
1186e93c1b0d81518943f81ca442b12db5cb7268
[ "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
4,916
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Core universe u v w w' class Coe (α : Sort u) (β : Sort v) where coe : α → β /-- Auxiliary class that contains the transitive closure of `Coe`. -/ class CoeTC (α : Sort u) (β : Sort v) where coe : α → β /- Expensive coercion that can only appear at the beginning of a sequence of coercions. -/ class CoeHead (α : Sort u) (β : Sort v) where coe : α → β /- Expensive coercion that can only appear at the end of a sequence of coercions. -/ class CoeTail (α : Sort u) (β : Sort v) where coe : α → β /-- Auxiliary class that contains `CoeHead` + `CoeTC` + `CoeTail`. -/ class CoeHTCT (α : Sort u) (β : Sort v) where coe : α → β class CoeDep (α : Sort u) (a : α) (β : Sort v) where coe : β /- Combines CoeHead, CoeTC, CoeTail, CoeDep -/ class CoeT (α : Sort u) (a : α) (β : Sort v) where coe : β class CoeFun (α : Sort u) (γ : outParam (α → Sort v)) where coe : (a : α) → γ a class CoeSort (α : Sort u) (β : outParam (Sort v)) where coe : α → β abbrev coeB {α : Sort u} {β : Sort v} [Coe α β] (a : α) : β := Coe.coe a abbrev coeHead {α : Sort u} {β : Sort v} [CoeHead α β] (a : α) : β := CoeHead.coe a abbrev coeTail {α : Sort u} {β : Sort v} [CoeTail α β] (a : α) : β := CoeTail.coe a abbrev coeD {α : Sort u} {β : Sort v} (a : α) [CoeDep α a β] : β := CoeDep.coe a abbrev coeTC {α : Sort u} {β : Sort v} [CoeTC α β] (a : α) : β := CoeTC.coe a /-- Apply coercion manually. -/ abbrev coe {α : Sort u} {β : Sort v} (a : α) [CoeT α a β] : β := CoeT.coe a prefix:max "↑" => coe abbrev coeFun {α : Sort u} {γ : α → Sort v} (a : α) [CoeFun α γ] : γ a := CoeFun.coe a abbrev coeSort {α : Sort u} {β : Sort v} (a : α) [CoeSort α β] : β := CoeSort.coe a instance coeTrans {α : Sort u} {β : Sort v} {δ : Sort w} [Coe β δ] [CoeTC α β] : CoeTC α δ where coe a := coeB (coeTC a : β) instance coeBase {α : Sort u} {β : Sort v} [Coe α β] : CoeTC α β where coe a := coeB a instance coeOfHeafOfTCOfTail {α : Sort u} {β : Sort v} {δ : Sort w} {γ : Sort w'} [CoeHead α β] [CoeTail δ γ] [CoeTC β δ] : CoeHTCT α γ where coe a := coeTail (coeTC (coeHead a : β) : δ) instance coeOfHeadOfTC {α : Sort u} {β : Sort v} {δ : Sort w} [CoeHead α β] [CoeTC β δ] : CoeHTCT α δ where coe a := coeTC (coeHead a : β) instance coeOfTCOfTail {α : Sort u} {β : Sort v} {δ : Sort w} [CoeTail β δ] [CoeTC α β] : CoeHTCT α δ where coe a := coeTail (coeTC a : β) instance coeOfHead {α : Sort u} {β : Sort v} [CoeHead α β] : CoeHTCT α β where coe a := coeHead a instance coeOfTail {α : Sort u} {β : Sort v} [CoeTail α β] : CoeHTCT α β where coe a := coeTail a instance coeOfTC {α : Sort u} {β : Sort v} [CoeTC α β] : CoeHTCT α β where coe a := coeTC a instance coeOfHTCT {α : Sort u} {β : Sort v} [CoeHTCT α β] (a : α) : CoeT α a β where coe := CoeHTCT.coe a instance coeOfDep {α : Sort u} {β : Sort v} (a : α) [CoeDep α a β] : CoeT α a β where coe := coeD a instance coeId {α : Sort u} (a : α) : CoeT α a α where coe := a instance coeSortToCoeTail [inst : CoeSort α β] : CoeTail α β where coe := inst.coe /- Basic instances -/ @[inline] instance boolToProp : Coe Bool Prop where coe b := b = true instance boolToSort : CoeSort Bool Prop where coe b := b @[inline] instance coeDecidableEq (x : Bool) : Decidable (coe x) := inferInstanceAs (Decidable (x = true)) instance decPropToBool (p : Prop) [Decidable p] : CoeDep Prop p Bool where coe := decide p instance optionCoe {α : Type u} : CoeTail α (Option α) where coe := some instance subtypeCoe {α : Sort u} {p : α → Prop} : CoeHead { x // p x } α where coe v := v.val /- Coe & OfNat bridge -/ /- Remark: one may question why we use `OfNat α` instead of `Coe Nat α`. Reason: `OfNat` is for implementing polymorphic numeric literals, and we may want to have numeric literals for a type α and **no** coercion from `Nat` to `α`. -/ instance hasOfNatOfCoe [Coe α β] [OfNat α n] : OfNat β n where ofNat := coe (OfNat.ofNat n : α) @[inline] def liftCoeM {m : Type u → Type v} {n : Type u → Type w} {α β : Type u} [MonadLiftT m n] [∀ a, CoeT α a β] [Monad n] (x : m α) : n β := do let a ← liftM x pure (coe a) @[inline] def coeM {m : Type u → Type v} {α β : Type u} [∀ a, CoeT α a β] [Monad m] (x : m α) : m β := do let a ← x pure <| coe a instance [CoeFun α β] (a : α) : CoeDep α a (β a) where coe := coeFun a instance [CoeFun α (fun _ => β)] : CoeTail α β where coe a := coeFun a instance [CoeSort α β] : CoeTail α β where coe a := coeSort a
8a40744ad702d985bdf1105578b45e8667d4ed01
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/src/Init/NotationExtra.lean
4f034fa189ca4077c1e7a019e9cb35afe6368577
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
7,577
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 Extra notation that depends on Init/Meta -/ prelude import Init.Meta import Init.Data.Array.Subarray namespace Lean -- Auxiliary parsers and functions for declaring notation with binders syntax binderIdent := ident <|> "_" syntax unbracktedExplicitBinders := binderIdent+ (" : " term)? syntax bracketedExplicitBinders := "(" binderIdent+ " : " term ")" syntax explicitBinders := bracketedExplicitBinders+ <|> unbracktedExplicitBinders def expandExplicitBindersAux (combinator : Syntax) (idents : Array Syntax) (type? : Option Syntax) (body : Syntax) : MacroM Syntax := let rec loop (i : Nat) (acc : Syntax) := do match i with | 0 => pure acc | i+1 => let ident := idents[i][0] let acc ← match ident.isIdent, type? with | true, none => `($combinator fun $ident => $acc) | true, some type => `($combinator fun $ident:ident : $type => $acc) | false, none => `($combinator fun _ => $acc) | false, some type => `($combinator fun _ : $type => $acc) loop i acc loop idents.size body def expandBrackedBindersAux (combinator : Syntax) (binders : Array Syntax) (body : Syntax) : MacroM Syntax := let rec loop (i : Nat) (acc : Syntax) := do match i with | 0 => pure acc | i+1 => let idents := binders[i][1].getArgs let type := binders[i][3] loop i (← expandExplicitBindersAux combinator idents (some type) acc) loop binders.size body def expandExplicitBinders (combinatorDeclName : Name) (explicitBinders : Syntax) (body : Syntax) : MacroM Syntax := do let combinator := mkIdentFrom (← getRef) combinatorDeclName let explicitBinders := explicitBinders[0] if explicitBinders.getKind == `Lean.unbracktedExplicitBinders then let idents := explicitBinders[0].getArgs let type? := if explicitBinders[1].isNone then none else some explicitBinders[1][1] expandExplicitBindersAux combinator idents type? body else if explicitBinders.getArgs.all (·.getKind == `Lean.bracketedExplicitBinders) then expandBrackedBindersAux combinator explicitBinders.getArgs body else Macro.throwError "unexpected explicit binder" def expandBrackedBinders (combinatorDeclName : Name) (bracketedExplicitBinders : Syntax) (body : Syntax) : MacroM Syntax := do let combinator := mkIdentFrom (← getRef) combinatorDeclName expandBrackedBindersAux combinator #[bracketedExplicitBinders] body syntax unifConstraint := term (" =?= " <|> " ≟ ") term syntax unifConstraintElem := colGe unifConstraint ", "? syntax attrKind "unif_hint " (ident)? bracketedBinder* " where " withPosition(unifConstraintElem*) ("|-" <|> "⊢ ") unifConstraint : command private def mkHintBody (cs : Array Syntax) (p : Syntax) : MacroM Syntax := do let mut body ← `($(p[0]) = $(p[2])) for c in cs.reverse do body ← `($(c[0][0]) = $(c[0][2]) → $body) return body macro_rules | `($kind:attrKind unif_hint $bs:explicitBinder* where $cs* |- $p) => do let body ← mkHintBody cs p `(@[$kind:attrKind unificationHint] def hint $bs:explicitBinder* : Sort _ := $body) | `($kind:attrKind unif_hint $n:ident $bs* where $cs* |- $p) => do let body ← mkHintBody cs p `(@[$kind:attrKind unificationHint] def $n:ident $bs:explicitBinder* : Sort _ := $body) end Lean open Lean macro "∃ " xs:explicitBinders ", " b:term : term => expandExplicitBinders `Exists xs b macro "exists" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Exists xs b macro "Σ" xs:explicitBinders ", " b:term : term => expandExplicitBinders `Sigma xs b macro "Σ'" xs:explicitBinders ", " b:term : term => expandExplicitBinders `PSigma xs b macro:35 xs:bracketedExplicitBinders " × " b:term:35 : term => expandBrackedBinders `Sigma xs b macro:35 xs:bracketedExplicitBinders " ×' " b:term:35 : term => expandBrackedBinders `PSigma xs b @[appUnexpander Exists] def unexpandExists : Lean.PrettyPrinter.Unexpander | `(Exists fun $x:ident => ∃ $xs:binderIdent*, $b) => `(∃ $x:ident $xs:binderIdent*, $b) | `(Exists fun $x:ident => $b) => `(∃ $x:ident, $b) | `(Exists fun ($x:ident : $t) => $b) => `(∃ ($x:ident : $t), $b) | _ => throw () @[appUnexpander Sigma] def unexpandSigma : Lean.PrettyPrinter.Unexpander | `(Sigma fun ($x:ident : $t) => $b) => `(($x:ident : $t) × $b) | _ => throw () @[appUnexpander PSigma] def unexpandPSigma : Lean.PrettyPrinter.Unexpander | `(PSigma fun ($x:ident : $t) => $b) => `(($x:ident : $t) ×' $b) | _ => throw () syntax "funext " (colGt term:max)+ : tactic macro_rules | `(tactic|funext $xs*) => if xs.size == 1 then `(tactic| apply funext; intro $(xs[0]):term) else `(tactic| apply funext; intro $(xs[0]):term; funext $(xs[1:])*) macro_rules | `(%[ $[$x],* | $k ]) => if x.size < 8 then x.foldrM (init := k) fun x k => `(List.cons $x $k) else let m := x.size / 2 let y := x[m:] let z := x[:m] `(let y := %[ $[$y],* | $k ] %[ $[$z],* | y ]) /- Expands ``` class abbrev C <params> := D_1, ..., D_n ``` into ``` class C <params> extends D_1, ..., D_n instance <params> [D_1] ... [D_n] : C <params> := C.mk _ ... _ inferInstance ... inferInstance ``` -/ syntax "class " "abbrev " ident bracketedBinder* ":=" withPosition(group(colGe term ","?)*) : command macro_rules | `(class abbrev $name:ident $params* := $[ $parents:term $[,]? ]*) => do let mut auxBinders := #[] let mut typeArgs := #[] let mut ctorArgs := #[] let hole ← `(_) for param in params do match param with | `(bracketedBinder| ( $ids:ident* $[: $type:term]? ) ) => auxBinders := auxBinders.push (← `(bracketedBinder| { $ids:ident* }) ) typeArgs := typeArgs ++ ids ctorArgs := ctorArgs ++ ids.map fun _ => hole | `(bracketedBinder| [ $id:ident : $type:term ] ) => auxBinders := auxBinders.push param typeArgs := typeArgs.push hole ctorArgs := ctorArgs.push hole | `(bracketedBinder| [ $type:term ] ) => auxBinders := auxBinders.push param typeArgs := typeArgs.push hole ctorArgs := ctorArgs.push hole | `(bracketedBinder| { $ids:ident* $[: $type ]? } ) => auxBinders := auxBinders.push param typeArgs := typeArgs ++ ids.map fun _ => hole ctorArgs := typeArgs ++ ids.map fun _ => hole | _ => Lean.Macro.throwErrorAt param "unexpected binder at `class abbrev` macro" let inferInst ← `(inferInstance) for parent in parents do auxBinders := auxBinders.push (← `(bracketedBinder| [ $parent:term ])) ctorArgs := ctorArgs.push inferInst let view := Lean.extractMacroScopes name.getId let ctor := mkIdentFrom name { view with name := view.name ++ `mk }.review `(class $name:ident $params* extends $[$parents:term],* instance $auxBinders:explicitBinder* : @ $name:ident $typeArgs* := @ $ctor:ident $ctorArgs*) /- Similar to `first`, but succeeds only if one the given tactics solves the curretn goal. -/ syntax "solve " "|"? sepBy1(tacticSeq, "|") : tactic macro_rules | `(tactic| solve $[|]? $ts:tacticSeq|*) => `(tactic| focus first $[($ts); done]|*)
2a6d65976a539e559bd535264b5d6fd1d4300ee0
63abd62053d479eae5abf4951554e1064a4c45b4
/src/analysis/calculus/local_extr.lean
ec8cd03cc09f04e48e2254a1ba7335569a309244
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
16,304
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.local_extr import analysis.calculus.deriv /-! # Local extrema of smooth functions ## Main definitions In a real normed space `E` we define `pos_tangent_cone_at (s : set E) (x : E)`. This would be the same as `tangent_cone_at ℝ≥0 s x` if we had a theory of normed semifields. This set is used in the proof of Fermat's Theorem (see below), and can be used to formalize [Lagrange multipliers](https://en.wikipedia.org/wiki/Lagrange_multiplier) and/or [Karush–Kuhn–Tucker conditions](https://en.wikipedia.org/wiki/Karush–Kuhn–Tucker_conditions). ## Main statements For each theorem name listed below, we also prove similar theorems for `min`, `extr` (if applicable)`, and `(f)deriv` instead of `has_fderiv`. * `is_local_max_on.has_fderiv_within_at_nonpos` : `f' y ≤ 0` whenever `a` is a local maximum of `f` on `s`, `f` has derivative `f'` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`. * `is_local_max_on.has_fderiv_within_at_eq_zero` : In the settings of the previous theorem, if both `y` and `-y` belong to the positive tangent cone, then `f' y = 0`. * `is_local_max.has_fderiv_at_eq_zero` : [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)), the derivative of a differentiable function at a local extremum point equals zero. * `exists_has_deriv_at_eq_zero` : [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem): given a function `f` continuous on `[a, b]` and differentiable on `(a, b)`, there exists `c ∈ (a, b)` such that `f' c = 0`. ## Implementation notes For each mathematical fact we prove several versions of its formalization: * for maxima and minima; * using `has_fderiv*`/`has_deriv*` or `fderiv*`/`deriv*`. For the `fderiv*`/`deriv*` versions we omit the differentiability condition whenever it is possible due to the fact that `fderiv` and `deriv` are defined to be zero for non-differentiable functions. ## References * [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)); * [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem); * [Tangent cone](https://en.wikipedia.org/wiki/Tangent_cone); ## Tags local extremum, Fermat's Theorem, Rolle's Theorem -/ universes u v open filter set open_locale topological_space classical section vector_space variables {E : Type u} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {a : E} {f' : E →L[ℝ] ℝ} /-- "Positive" tangent cone to `s` at `x`; the only difference from `tangent_cone_at` is that we require `c n → ∞` instead of `∥c n∥ → ∞`. One can think about `pos_tangent_cone_at` as `tangent_cone_at nnreal` but we have no theory of normed semifields yet. -/ def pos_tangent_cone_at (s : set E) (x : E) : set E := {y : E | ∃(c : ℕ → ℝ) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧ (tendsto c at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))} lemma pos_tangent_cone_at_mono : monotone (λ s, pos_tangent_cone_at s a) := begin rintros s t hst y ⟨c, d, hd, hc, hcd⟩, exact ⟨c, d, mem_sets_of_superset hd $ λ h hn, hst hn, hc, hcd⟩ end lemma mem_pos_tangent_cone_at_of_segment_subset {s : set E} {x y : E} (h : segment x y ⊆ s) : y - x ∈ pos_tangent_cone_at s x := begin let c := λn:ℕ, (2:ℝ)^n, let d := λn:ℕ, (c n)⁻¹ • (y-x), refine ⟨c, d, filter.univ_mem_sets' (λn, h _), _, _⟩, show x + d n ∈ segment x y, { rw segment_eq_image, refine ⟨(c n)⁻¹, ⟨_, _⟩, _⟩, { rw inv_nonneg, apply pow_nonneg, norm_num }, { apply inv_le_one, apply one_le_pow_of_one_le, norm_num }, { simp only [d, sub_smul, smul_sub, one_smul], abel } }, show tendsto c at_top at_top, { exact tendsto_pow_at_top_at_top_of_one_lt one_lt_two }, show filter.tendsto (λ (n : ℕ), c n • d n) filter.at_top (𝓝 (y - x)), { have : (λ (n : ℕ), c n • d n) = (λn, y - x), { ext n, simp only [d, smul_smul], rw [mul_inv_cancel, one_smul], exact pow_ne_zero _ (by norm_num) }, rw this, apply tendsto_const_nhds } end lemma pos_tangent_cone_at_univ : pos_tangent_cone_at univ a = univ := eq_univ_iff_forall.2 begin assume x, rw [← add_sub_cancel x a], exact mem_pos_tangent_cone_at_of_segment_subset (subset_univ _) end /-- If `f` has a local max on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.has_fderiv_within_at_nonpos {s : set E} (h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : f' y ≤ 0 := begin rcases hy with ⟨c, d, hd, hc, hcd⟩, have hc' : tendsto (λ n, ∥c n∥) at_top at_top, from tendsto_at_top_mono (λ n, le_abs_self _) hc, refine le_of_tendsto (hf.lim at_top hd hc' hcd) _, replace hd : tendsto (λ n, a + d n) at_top (𝓝[s] (a + 0)), from tendsto_inf.2 ⟨tendsto_const_nhds.add (tangent_cone_at.lim_zero _ hc' hcd), by rwa tendsto_principal⟩, rw [add_zero] at hd, replace h : ∀ᶠ n in at_top, f (a + d n) ≤ f a, from mem_map.1 (hd h), replace hc : ∀ᶠ n in at_top, 0 ≤ c n, from mem_map.1 (hc (mem_at_top (0:ℝ))), filter_upwards [h, hc], simp only [smul_eq_mul, mem_preimage, subset_def], assume n hnf hn, exact mul_nonpos_of_nonneg_of_nonpos hn (sub_nonpos.2 hnf) end /-- If `f` has a local max on `s` at `a` and `y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.fderiv_within_nonpos {s : set E} (h : is_local_max_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y ≤ 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_nonpos hf.has_fderiv_within_at hy else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.has_fderiv_within_at_eq_zero {s : set E} (h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : f' y = 0 := le_antisymm (h.has_fderiv_within_at_nonpos hf hy) $ by simpa using h.has_fderiv_within_at_nonpos hf hy' /-- If `f` has a local max on `s` at `a` and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y = 0`. -/ lemma is_local_max_on.fderiv_within_eq_zero {s : set E} (h : is_local_max_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y = 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_eq_zero hf.has_fderiv_within_at hy hy' else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- If `f` has a local min on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/ lemma is_local_min_on.has_fderiv_within_at_nonneg {s : set E} (h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : 0 ≤ f' y := by simpa using h.neg.has_fderiv_within_at_nonpos hf.neg hy /-- If `f` has a local min on `s` at `a` and `y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/ lemma is_local_min_on.fderiv_within_nonneg {s : set E} (h : is_local_min_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : (0:ℝ) ≤ (fderiv_within ℝ f s a : E → ℝ) y := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_nonneg hf.has_fderiv_within_at hy else by { rw [fderiv_within_zero_of_not_differentiable_within_at hf], refl } /-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_min_on.has_fderiv_within_at_eq_zero {s : set E} (h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : f' y = 0 := by simpa using h.neg.has_fderiv_within_at_eq_zero hf.neg hy hy' /-- If `f` has a local min on `s` at `a` and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y = 0`. -/ lemma is_local_min_on.fderiv_within_eq_zero {s : set E} (h : is_local_min_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y = 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_eq_zero hf.has_fderiv_within_at hy hy' else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.has_fderiv_at_eq_zero (h : is_local_min f a) (hf : has_fderiv_at f f' a) : f' = 0 := begin ext y, apply (h.on univ).has_fderiv_within_at_eq_zero hf.has_fderiv_within_at; rw pos_tangent_cone_at_univ; apply mem_univ end /-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.fderiv_eq_zero (h : is_local_min f a) : fderiv ℝ f a = 0 := if hf : differentiable_at ℝ f a then h.has_fderiv_at_eq_zero hf.has_fderiv_at else fderiv_zero_of_not_differentiable_at hf /-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.has_fderiv_at_eq_zero (h : is_local_max f a) (hf : has_fderiv_at f f' a) : f' = 0 := neg_eq_zero.1 $ h.neg.has_fderiv_at_eq_zero hf.neg /-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.fderiv_eq_zero (h : is_local_max f a) : fderiv ℝ f a = 0 := if hf : differentiable_at ℝ f a then h.has_fderiv_at_eq_zero hf.has_fderiv_at else fderiv_zero_of_not_differentiable_at hf /-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.has_fderiv_at_eq_zero (h : is_local_extr f a) : has_fderiv_at f f' a → f' = 0 := h.elim is_local_min.has_fderiv_at_eq_zero is_local_max.has_fderiv_at_eq_zero /-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.fderiv_eq_zero (h : is_local_extr f a) : fderiv ℝ f a = 0 := h.elim is_local_min.fderiv_eq_zero is_local_max.fderiv_eq_zero end vector_space section real variables {f : ℝ → ℝ} {f' : ℝ} {a b : ℝ} /-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.has_deriv_at_eq_zero (h : is_local_min f a) (hf : has_deriv_at f f' a) : f' = 0 := by simpa using continuous_linear_map.ext_iff.1 (h.has_fderiv_at_eq_zero (has_deriv_at_iff_has_fderiv_at.1 hf)) 1 /-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.deriv_eq_zero (h : is_local_min f a) : deriv f a = 0 := if hf : differentiable_at ℝ f a then h.has_deriv_at_eq_zero hf.has_deriv_at else deriv_zero_of_not_differentiable_at hf /-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.has_deriv_at_eq_zero (h : is_local_max f a) (hf : has_deriv_at f f' a) : f' = 0 := neg_eq_zero.1 $ h.neg.has_deriv_at_eq_zero hf.neg /-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.deriv_eq_zero (h : is_local_max f a) : deriv f a = 0 := if hf : differentiable_at ℝ f a then h.has_deriv_at_eq_zero hf.has_deriv_at else deriv_zero_of_not_differentiable_at hf /-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.has_deriv_at_eq_zero (h : is_local_extr f a) : has_deriv_at f f' a → f' = 0 := h.elim is_local_min.has_deriv_at_eq_zero is_local_max.has_deriv_at_eq_zero /-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.deriv_eq_zero (h : is_local_extr f a) : deriv f a = 0 := h.elim is_local_min.deriv_eq_zero is_local_max.deriv_eq_zero end real section Rolle variables (f f' : ℝ → ℝ) {a b : ℝ} (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) include hab hfc hfI /-- A continuous function on a closed interval with `f a = f b` takes either its maximum or its minimum value at a point in the interior of the interval. -/ lemma exists_Ioo_extr_on_Icc : ∃ c ∈ Ioo a b, is_extr_on f (Icc a b) c := begin have ne : (Icc a b).nonempty, from nonempty_Icc.2 (le_of_lt hab), -- Consider absolute min and max points obtain ⟨c, cmem, cle⟩ : ∃ c ∈ Icc a b, ∀ x ∈ Icc a b, f c ≤ f x, from compact_Icc.exists_forall_le ne hfc, obtain ⟨C, Cmem, Cge⟩ : ∃ C ∈ Icc a b, ∀ x ∈ Icc a b, f x ≤ f C, from compact_Icc.exists_forall_ge ne hfc, by_cases hc : f c = f a, { by_cases hC : f C = f a, { have : ∀ x ∈ Icc a b, f x = f a, from λ x hx, le_antisymm (hC ▸ Cge x hx) (hc ▸ cle x hx), -- `f` is a constant, so we can take any point in `Ioo a b` rcases exists_between hab with ⟨c', hc'⟩, refine ⟨c', hc', or.inl _⟩, assume x hx, rw [mem_set_of_eq, this x hx, ← hC], exact Cge c' ⟨le_of_lt hc'.1, le_of_lt hc'.2⟩ }, { refine ⟨C, ⟨lt_of_le_of_ne Cmem.1 $ mt _ hC, lt_of_le_of_ne Cmem.2 $ mt _ hC⟩, or.inr Cge⟩, exacts [λ h, by rw h, λ h, by rw [h, hfI]] } }, { refine ⟨c, ⟨lt_of_le_of_ne cmem.1 $ mt _ hc, lt_of_le_of_ne cmem.2 $ mt _ hc⟩, or.inl cle⟩, exacts [λ h, by rw h, λ h, by rw [h, hfI]] } end /-- A continuous function on a closed interval with `f a = f b` has a local extremum at some point of the corresponding open interval. -/ lemma exists_local_extr_Ioo : ∃ c ∈ Ioo a b, is_local_extr f c := let ⟨c, cmem, hc⟩ := exists_Ioo_extr_on_Icc f hab hfc hfI in ⟨c, cmem, hc.is_local_extr $ mem_nhds_sets_iff.2 ⟨Ioo a b, Ioo_subset_Icc_self, is_open_Ioo, cmem⟩⟩ /-- Rolle's Theorem `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_zero (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) : ∃ c ∈ Ioo a b, f' c = 0 := let ⟨c, cmem, hc⟩ := exists_local_extr_Ioo f hab hfc hfI in ⟨c, cmem, hc.has_deriv_at_eq_zero $ hff' c cmem⟩ /-- Rolle's Theorem `deriv` version -/ lemma exists_deriv_eq_zero : ∃ c ∈ Ioo a b, deriv f c = 0 := let ⟨c, cmem, hc⟩ := exists_local_extr_Ioo f hab hfc hfI in ⟨c, cmem, hc.deriv_eq_zero⟩ omit hfc hfI variables {f f'} lemma exists_has_deriv_at_eq_zero' {l : ℝ} (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 l)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 l)) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) : ∃ c ∈ Ioo a b, f' c = 0 := begin have : continuous_on f (Ioo a b) := λ x hx, (hff' x hx).continuous_at.continuous_within_at, have hcont := continuous_on_Icc_extend_from_Ioo hab this hfa hfb, obtain ⟨c, hc, hcextr⟩ : ∃ c ∈ Ioo a b, is_local_extr (extend_from (Ioo a b) f) c, { apply exists_local_extr_Ioo _ hab hcont, rw eq_lim_at_right_extend_from_Ioo hab hfb, exact eq_lim_at_left_extend_from_Ioo hab hfa }, use [c, hc], apply (hcextr.congr _).has_deriv_at_eq_zero (hff' c hc), rw eventually_eq_iff_exists_mem, exact ⟨Ioo a b, Ioo_mem_nhds hc.1 hc.2, extend_from_extends this⟩ end lemma exists_deriv_eq_zero' {l : ℝ} (hfa : tendsto f (𝓝[Ioi a] a) (𝓝 l)) (hfb : tendsto f (𝓝[Iio b] b) (𝓝 l)) : ∃ c ∈ Ioo a b, deriv f c = 0 := classical.by_cases (assume h : ∀ x ∈ Ioo a b, differentiable_at ℝ f x, show ∃ c ∈ Ioo a b, deriv f c = 0, from exists_has_deriv_at_eq_zero' hab hfa hfb (λ x hx, (h x hx).has_deriv_at)) (assume h : ¬∀ x ∈ Ioo a b, differentiable_at ℝ f x, have h : ∃ x, x ∈ Ioo a b ∧ ¬differentiable_at ℝ f x, by { push_neg at h, exact h }, let ⟨c, hc, hcdiff⟩ := h in ⟨c, hc, deriv_zero_of_not_differentiable_at hcdiff⟩) end Rolle
8db26dd140c5fdac6ddb266b5d98ff50e4cc7c69
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/data/equiv/list.lean
4fe402c66555db13562d2ba4c8bb4a135f4088e0
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
8,155
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Additional equiv and encodable instances for lists and finsets. -/ import data.equiv.denumerable data.nat.pairing order.order_iso data.vector2 data.array.lemmas data.fintype open nat list namespace encodable variables {α : Type*} section list variable [encodable α] def encode_list : list α → ℕ | [] := 0 | (a::l) := succ (mkpair (encode a) (encode_list l)) def decode_list : ℕ → option (list α) | 0 := some [] | (succ v) := match unpair v, unpair_le_right v with | (v₁, v₂), h := have v₂ < succ v, from lt_succ_of_le h, (::) <$> decode α v₁ <*> decode_list v₂ end instance list : encodable (list α) := ⟨encode_list, decode_list, λ l, by induction l with a l IH; simp [encode_list, decode_list, unpair_mkpair, encodek, *]⟩ @[simp] theorem encode_list_nil : encode (@nil α) = 0 := rfl @[simp] theorem encode_list_cons (a : α) (l : list α) : encode (a :: l) = succ (mkpair (encode a) (encode l)) := rfl @[simp] theorem decode_list_zero : decode (list α) 0 = some [] := rfl @[simp] theorem decode_list_succ (v : ℕ) : decode (list α) (succ v) = (::) <$> decode α v.unpair.1 <*> decode (list α) v.unpair.2 := show decode_list (succ v) = _, begin cases e : unpair v with v₁ v₂, simp [decode_list, e], refl end theorem length_le_encode : ∀ (l : list α), length l ≤ encode l | [] := zero_le _ | (a :: l) := succ_le_succ $ le_trans (length_le_encode l) (le_mkpair_right _ _) end list section finset variables [encodable α] private def enle : α → α → Prop := encode ⁻¹'o (≤) private lemma enle.is_linear_order : is_linear_order α enle := (order_embedding.preimage ⟨encode, encode_injective⟩ (≤)).is_linear_order private def decidable_enle (a b : α) : decidable (enle a b) := by unfold enle order.preimage; apply_instance local attribute [instance] enle.is_linear_order decidable_enle def encode_multiset (s : multiset α) : ℕ := encode (s.sort enle) def decode_multiset (n : ℕ) : option (multiset α) := coe <$> decode (list α) n instance multiset : encodable (multiset α) := ⟨encode_multiset, decode_multiset, λ s, by simp [encode_multiset, decode_multiset, encodek]⟩ end finset def encodable_of_list [decidable_eq α] (l : list α) (H : ∀ x, x ∈ l) : encodable α := ⟨λ a, index_of a l, l.nth, λ a, index_of_nth (H _)⟩ def trunc_encodable_of_fintype (α : Type*) [decidable_eq α] [fintype α] : trunc (encodable α) := @@quot.rec_on_subsingleton _ (λ s : multiset α, (∀ x:α, x ∈ s) → trunc (encodable α)) _ finset.univ.1 (λ l H, trunc.mk $ encodable_of_list l H) finset.mem_univ instance vector [encodable α] {n} : encodable (vector α n) := encodable.subtype instance fin_arrow [encodable α] {n} : encodable (fin n → α) := of_equiv _ (equiv.vector_equiv_fin _ _).symm instance array [encodable α] {n} : encodable (array n α) := of_equiv _ (equiv.array_equiv_fin _ _) instance finset [encodable α] : encodable (finset α) := by haveI := decidable_eq_of_encodable α; exact of_equiv {s : multiset α // s.nodup} ⟨λ ⟨a, b⟩, ⟨a, b⟩, λ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ end encodable namespace denumerable variables {α : Type*} {β : Type*} [denumerable α] [denumerable β] open encodable section list theorem denumerable_list_aux : ∀ n : ℕ, ∃ a ∈ @decode_list α _ n, encode_list a = n | 0 := ⟨_, rfl, rfl⟩ | (succ v) := begin cases e : unpair v with v₁ v₂, have h := unpair_le_right v, rw e at h, rcases have v₂ < succ v, from lt_succ_of_le h, denumerable_list_aux v₂ with ⟨a, h₁, h₂⟩, simp at h₁, simp [decode_list, e, h₂, h₁, encode_list, mkpair_unpair' e] end instance denumerable_list : denumerable (list α) := ⟨denumerable_list_aux⟩ @[simp] theorem list_of_nat_zero : of_nat (list α) 0 = [] := rfl @[simp] theorem list_of_nat_succ (v : ℕ) : of_nat (list α) (succ v) = of_nat α v.unpair.1 :: of_nat (list α) v.unpair.2 := of_nat_of_decode $ show decode_list (succ v) = _, begin cases e : unpair v with v₁ v₂, simp [decode_list, e], rw [show decode_list v₂ = decode (list α) v₂, from rfl, decode_eq_of_nat]; refl end end list section multiset def lower : list ℕ → ℕ → list ℕ | [] n := [] | (m :: l) n := (m - n) :: lower l m def raise : list ℕ → ℕ → list ℕ | [] n := [] | (m :: l) n := (m + n) :: raise l (m + n) lemma lower_raise : ∀ l n, lower (raise l n) n = l | [] n := rfl | (m :: l) n := by simp [raise, lower, nat.add_sub_cancel, lower_raise] lemma raise_lower : ∀ {l n}, list.sorted (≤) (n :: l) → raise (lower l n) n = l | [] n h := rfl | (m :: l) n h := have n ≤ m, from list.rel_of_sorted_cons h _ (l.mem_cons_self _), by simp [raise, lower, nat.add_sub_cancel' this, raise_lower (list.sorted_of_sorted_cons h)] lemma raise_chain : ∀ l n, list.chain (≤) n (raise l n) | [] n := list.chain.nil _ _ | (m :: l) n := list.chain.cons (nat.le_add_left _ _) (raise_chain _ _) lemma raise_sorted : ∀ l n, list.sorted (≤) (raise l n) | [] n := list.sorted_nil | (m :: l) n := (list.chain_iff_pairwise (@le_trans _ _)).1 (raise_chain _ _) /- Warning: this is not the same encoding as used in `encodable` -/ instance multiset : denumerable (multiset α) := mk' ⟨ λ s : multiset α, encode $ lower ((s.map encode).sort (≤)) 0, λ n, multiset.map (of_nat α) (raise (of_nat (list ℕ) n) 0), λ s, by have := raise_lower (list.sorted_cons.2 ⟨λ n _, zero_le n, (s.map encode).sort_sorted _⟩); simp [-multiset.coe_map, this], λ n, by simp [-multiset.coe_map, list.merge_sort_eq_self _ (raise_sorted _ _), lower_raise]⟩ end multiset section finset def lower' : list ℕ → ℕ → list ℕ | [] n := [] | (m :: l) n := (m - n) :: lower' l (m + 1) def raise' : list ℕ → ℕ → list ℕ | [] n := [] | (m :: l) n := (m + n) :: raise' l (m + n + 1) lemma lower_raise' : ∀ l n, lower' (raise' l n) n = l | [] n := rfl | (m :: l) n := by simp [raise', lower', nat.add_sub_cancel, lower_raise'] lemma raise_lower' : ∀ {l n}, (∀ m ∈ l, n ≤ m) → list.sorted (<) l → raise' (lower' l n) n = l | [] n h₁ h₂ := rfl | (m :: l) n h₁ h₂ := have n ≤ m, from h₁ _ (l.mem_cons_self _), by simp [raise', lower', nat.add_sub_cancel' this, raise_lower' (list.rel_of_sorted_cons h₂ : ∀ a ∈ l, m < a) (list.sorted_of_sorted_cons h₂)] lemma raise'_chain : ∀ l {m n}, m < n → list.chain (<) m (raise' l n) | [] m n h := list.chain.nil _ _ | (a :: l) m n h := list.chain.cons (lt_of_lt_of_le h (nat.le_add_left _ _)) (raise'_chain _ (lt_succ_self _)) lemma raise'_sorted : ∀ l n, list.sorted (<) (raise' l n) | [] n := list.sorted_nil | (m :: l) n := (list.chain_iff_pairwise (@lt_trans _ _)).1 (raise'_chain _ (lt_succ_self _)) def raise'_finset (l : list ℕ) (n : ℕ) : finset ℕ := ⟨raise' l n, (raise'_sorted _ _).imp (@ne_of_lt _ _)⟩ /- Warning: this is not the same encoding as used in `encodable` -/ instance finset : denumerable (finset α) := mk' ⟨ λ s : finset α, encode $ lower' ((s.map (eqv α).to_embedding).sort (≤)) 0, λ n, finset.map (eqv α).symm.to_embedding (raise'_finset (of_nat (list ℕ) n) 0), λ s, finset.eq_of_veq $ by simp [-multiset.coe_map, raise'_finset, raise_lower' (λ n _, zero_le n) (finset.sort_sorted_lt _)], λ n, by simp [-multiset.coe_map, finset.map, raise'_finset, finset.sort, list.merge_sort_eq_self (≤) ((raise'_sorted _ _).imp (@le_of_lt _ _)), lower_raise']⟩ end finset end denumerable namespace equiv def list_nat_equiv_nat : list ℕ ≃ ℕ := denumerable.eqv _ def list_equiv_self_of_equiv_nat {α : Type} (e : α ≃ ℕ) : list α ≃ α := calc list α ≃ list ℕ : list_equiv_of_equiv e ... ≃ ℕ : list_nat_equiv_nat ... ≃ α : e.symm end equiv
216ea4de73f4ae878fcd65bc91b2eb9df86458d1
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/group_theory/submonoid/membership.lean
4c89569dbfb133e6e51525ed9dcef445532f6798
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,984
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.group_theory.submonoid.operations import Mathlib.algebra.big_operators.basic import Mathlib.algebra.free_monoid import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # Submonoids: membership criteria In this file we prove various facts about membership in a submonoid: * `list_prod_mem`, `multiset_prod_mem`, `prod_mem`: if each element of a collection belongs to a multiplicative submonoid, then so does their product; * `list_sum_mem`, `multiset_sum_mem`, `sum_mem`: if each element of a collection belongs to an additive submonoid, then so does their sum; * `pow_mem`, `nsmul_mem`: if `x ∈ S` where `S` is a multiplicative (resp., additive) submonoid and `n` is a natural number, then `x^n` (resp., `n •ℕ x`) belongs to `S`; * `mem_supr_of_directed`, `coe_supr_of_directed`, `mem_Sup_of_directed_on`, `coe_Sup_of_directed_on`: the supremum of a directed collection of submonoid is their union. * `sup_eq_range`, `mem_sup`: supremum of two submonoids `S`, `T` of a commutative monoid is the set of products; * `closure_singleton_eq`, `mem_closure_singleton`: the multiplicative (resp., additive) closure of `{x}` consists of powers (resp., natural multiples) of `x`. ## Tags submonoid, submonoids -/ namespace submonoid /-- Product of a list of elements in a submonoid is in the submonoid. -/ theorem Mathlib.add_submonoid.list_sum_mem {M : Type u_1} [add_monoid M] (S : add_submonoid M) {l : List M} : (∀ (x : M), x ∈ l → x ∈ S) → list.sum l ∈ S := sorry /-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/ theorem Mathlib.add_submonoid.multiset_sum_mem {M : Type u_1} [add_comm_monoid M] (S : add_submonoid M) (m : multiset M) : (∀ (a : M), a ∈ m → a ∈ S) → multiset.sum m ∈ S := sorry /-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the submonoid. -/ theorem Mathlib.add_submonoid.sum_mem {M : Type u_1} [add_comm_monoid M] (S : add_submonoid M) {ι : Type u_2} {t : finset ι} {f : ι → M} (h : ∀ (c : ι), c ∈ t → f c ∈ S) : (finset.sum t fun (c : ι) => f c) ∈ S := sorry theorem pow_mem {M : Type u_1} [monoid M] (S : submonoid M) {x : M} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := sorry theorem mem_supr_of_directed {M : Type u_1} [monoid M] {ι : Sort u_2} [hι : Nonempty ι] {S : ι → submonoid M} (hS : directed LessEq S) {x : M} : (x ∈ supr fun (i : ι) => S i) ↔ ∃ (i : ι), x ∈ S i := sorry theorem Mathlib.add_submonoid.coe_supr_of_directed {M : Type u_1} [add_monoid M] {ι : Sort u_2} [Nonempty ι] {S : ι → add_submonoid M} (hS : directed LessEq S) : ↑(supr fun (i : ι) => S i) = set.Union fun (i : ι) => ↑(S i) := sorry theorem Mathlib.add_submonoid.mem_Sup_of_directed_on {M : Type u_1} [add_monoid M] {S : set (add_submonoid M)} (Sne : set.nonempty S) (hS : directed_on LessEq S) {x : M} : x ∈ Sup S ↔ ∃ (s : add_submonoid M), ∃ (H : s ∈ S), x ∈ s := sorry theorem Mathlib.add_submonoid.coe_Sup_of_directed_on {M : Type u_1} [add_monoid M] {S : set (add_submonoid M)} (Sne : set.nonempty S) (hS : directed_on LessEq S) : ↑(Sup S) = set.Union fun (s : add_submonoid M) => set.Union fun (H : s ∈ S) => ↑s := sorry theorem mem_sup_left {M : Type u_1} [monoid M] {S : submonoid M} {T : submonoid M} {x : M} : x ∈ S → x ∈ S ⊔ T := (fun (this : S ≤ S ⊔ T) => this) le_sup_left theorem Mathlib.add_submonoid.mem_sup_right {M : Type u_1} [add_monoid M] {S : add_submonoid M} {T : add_submonoid M} {x : M} : x ∈ T → x ∈ S ⊔ T := (fun (this : T ≤ S ⊔ T) => this) le_sup_right theorem Mathlib.add_submonoid.mem_supr_of_mem {M : Type u_1} [add_monoid M] {ι : Type u_2} {S : ι → add_submonoid M} (i : ι) {x : M} : x ∈ S i → x ∈ supr S := (fun (this : S i ≤ supr S) => this) (le_supr S i) theorem mem_Sup_of_mem {M : Type u_1} [monoid M] {S : set (submonoid M)} {s : submonoid M} (hs : s ∈ S) {x : M} : x ∈ s → x ∈ Sup S := (fun (this : s ≤ Sup S) => this) (le_Sup hs) end submonoid namespace free_monoid theorem closure_range_of {α : Type u_3} : submonoid.closure (set.range of) = ⊤ := sorry end free_monoid namespace submonoid theorem closure_singleton_eq {M : Type u_1} [monoid M] (x : M) : closure (singleton x) = monoid_hom.mrange (coe_fn (powers_hom M) x) := sorry /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ theorem mem_closure_singleton {M : Type u_1} [monoid M] {x : M} {y : M} : y ∈ closure (singleton x) ↔ ∃ (n : ℕ), x ^ n = y := sorry theorem mem_closure_singleton_self {M : Type u_1} [monoid M] {y : M} : y ∈ closure (singleton y) := iff.mpr mem_closure_singleton (Exists.intro 1 (pow_one y)) theorem closure_singleton_one {M : Type u_1} [monoid M] : closure (singleton 1) = ⊥ := sorry theorem Mathlib.add_submonoid.closure_eq_mrange {M : Type u_1} [add_monoid M] (s : set M) : add_submonoid.closure s = add_monoid_hom.mrange (coe_fn free_add_monoid.lift coe) := sorry theorem Mathlib.add_submonoid.exists_list_of_mem_closure {M : Type u_1} [add_monoid M] {s : set M} {x : M} (hx : x ∈ add_submonoid.closure s) : ∃ (l : List M), ∃ (hl : ∀ (y : M), y ∈ l → y ∈ s), list.sum l = x := sorry /-- The submonoid generated by an element. -/ def powers {N : Type u_3} [monoid N] (n : N) : submonoid N := copy (monoid_hom.mrange (coe_fn (powers_hom N) n)) (set.range (pow n)) sorry @[simp] theorem mem_powers {N : Type u_3} [monoid N] (n : N) : n ∈ powers n := Exists.intro 1 (pow_one n) theorem powers_eq_closure {N : Type u_3} [monoid N] (n : N) : powers n = closure (singleton n) := ext fun (x : N) => iff.symm mem_closure_singleton theorem powers_subset {N : Type u_3} [monoid N] {n : N} {P : submonoid N} (h : n ∈ P) : powers n ≤ P := sorry end submonoid namespace submonoid theorem sup_eq_range {N : Type u_3} [comm_monoid N] (s : submonoid N) (t : submonoid N) : s ⊔ t = monoid_hom.mrange (monoid_hom.coprod (subtype s) (subtype t)) := sorry theorem Mathlib.add_submonoid.mem_sup {N : Type u_3} [add_comm_monoid N] {s : add_submonoid N} {t : add_submonoid N} {x : N} : x ∈ s ⊔ t ↔ ∃ (y : N), ∃ (H : y ∈ s), ∃ (z : N), ∃ (H : z ∈ t), y + z = x := sorry end submonoid namespace add_submonoid theorem nsmul_mem {A : Type u_2} [add_monoid A] (S : add_submonoid A) {x : A} (hx : x ∈ S) (n : ℕ) : n •ℕ x ∈ S := sorry theorem closure_singleton_eq {A : Type u_2} [add_monoid A] (x : A) : closure (singleton x) = add_monoid_hom.mrange (coe_fn (multiples_hom A) x) := sorry /-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element. -/ theorem mem_closure_singleton {A : Type u_2} [add_monoid A] {x : A} {y : A} : y ∈ closure (singleton x) ↔ ∃ (n : ℕ), n •ℕ x = y := sorry theorem closure_singleton_zero {A : Type u_2} [add_monoid A] : closure (singleton 0) = ⊥ := sorry /-- The additive submonoid generated by an element. -/ def multiples {A : Type u_2} [add_monoid A] (x : A) : add_submonoid A := copy (add_monoid_hom.mrange (coe_fn (multiples_hom A) x)) (set.range fun (i : ℕ) => i •ℕ x) sorry @[simp] theorem mem_multiples {A : Type u_2} [add_monoid A] (x : A) : x ∈ multiples x := Exists.intro 1 (one_nsmul x) theorem multiples_eq_closure {A : Type u_2} [add_monoid A] (x : A) : multiples x = closure (singleton x) := ext fun (x_1 : A) => iff.symm mem_closure_singleton theorem multiples_subset {A : Type u_2} [add_monoid A] {x : A} {P : add_submonoid A} (h : x ∈ P) : multiples x ≤ P := sorry
80927f24400e7cb694ccc523b7acec44714fd76f
77c5b91fae1b966ddd1db969ba37b6f0e4901e88
/src/data/set/basic.lean
818ef0fbdf405064f66f8a3169241caac5248fc4
[ "Apache-2.0" ]
permissive
dexmagic/mathlib
ff48eefc56e2412429b31d4fddd41a976eb287ce
7a5d15a955a92a90e1d398b2281916b9c41270b2
refs/heads/master
1,693,481,322,046
1,633,360,193,000
1,633,360,193,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
116,309
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import logic.unique import logic.relation import order.boolean_algebra /-! # Basic properties of sets Sets in Lean are homogeneous; all their elements have the same type. Sets whose elements have type `X` are thus defined as `set X := X → Prop`. Note that this function need not be decidable. The definition is in the core library. This file provides some basic definitions related to sets and functions not present in the core library, as well as extra lemmas for functions in the core library (empty set, univ, union, intersection, insert, singleton, set-theoretic difference, complement, and powerset). Note that a set is a term, not a type. There is a coercion from `set α` to `Type*` sending `s` to the corresponding subtype `↥s`. See also the file `set_theory/zfc.lean`, which contains an encoding of ZFC set theory in Lean. ## Main definitions Notation used here: - `f : α → β` is a function, - `s : set α` and `s₁ s₂ : set α` are subsets of `α` - `t : set β` is a subset of `β`. Definitions in the file: * `strict_subset s₁ s₂ : Prop` : the predicate `s₁ ⊆ s₂` but `s₁ ≠ s₂`. * `nonempty s : Prop` : the predicate `s ≠ ∅`. Note that this is the preferred way to express the fact that `s` has an element (see the Implementation Notes). * `preimage f t : set α` : the preimage f⁻¹(t) (written `f ⁻¹' t` in Lean) of a subset of β. * `subsingleton s : Prop` : the predicate saying that `s` has at most one element. * `range f : set β` : the image of `univ` under `f`. Also works for `{p : Prop} (f : p → α)` (unlike `image`) * `s.prod t : set (α × β)` : the subset `s × t`. * `inclusion s₁ s₂ : ↥s₁ → ↥s₂` : the map `↥s₁ → ↥s₂` induced by an inclusion `s₁ ⊆ s₂`. ## Notation * `f ⁻¹' t` for `preimage f t` * `f '' s` for `image f s` * `sᶜ` for the complement of `s` ## Implementation notes * `s.nonempty` is to be preferred to `s ≠ ∅` or `∃ x, x ∈ s`. It has the advantage that the `s.nonempty` dot notation can be used. * For `s : set α`, do not use `subtype s`. Instead use `↥s` or `(s : Type*)` or `s`. ## Tags set, sets, subset, subsets, image, preimage, pre-image, range, union, intersection, insert, singleton, complement, powerset -/ /-! ### Set coercion to a type -/ open function universe variables u v w x run_cmd do e ← tactic.get_env, tactic.set_env $ e.mk_protected `set.compl lemma has_subset.subset.trans {α : Type*} [has_subset α] [is_trans α (⊆)] {a b c : α} (h : a ⊆ b) (h' : b ⊆ c) : a ⊆ c := trans h h' lemma has_subset.subset.antisymm {α : Type*} [has_subset α] [is_antisymm α (⊆)] {a b : α} (h : a ⊆ b) (h' : b ⊆ a) : a = b := antisymm h h' lemma has_ssubset.ssubset.trans {α : Type*} [has_ssubset α] [is_trans α (⊂)] {a b c : α} (h : a ⊂ b) (h' : b ⊂ c) : a ⊂ c := trans h h' lemma has_ssubset.ssubset.asymm {α : Type*} [has_ssubset α] [is_asymm α (⊂)] {a b : α} (h : a ⊂ b) : ¬(b ⊂ a) := asymm h namespace set variable {α : Type*} instance : has_le (set α) := ⟨(⊆)⟩ instance : has_lt (set α) := ⟨λ s t, s ≤ t ∧ ¬t ≤ s⟩ -- `⊂` is not defined until further down instance {α : Type*} : boolean_algebra (set α) := { sup := (∪), le := (≤), lt := (<), inf := (∩), bot := ∅, compl := set.compl, top := univ, sdiff := (\), .. (infer_instance : boolean_algebra (α → Prop)) } @[simp] lemma top_eq_univ : (⊤ : set α) = univ := rfl @[simp] lemma bot_eq_empty : (⊥ : set α) = ∅ := rfl @[simp] lemma sup_eq_union : ((⊔) : set α → set α → set α) = (∪) := rfl @[simp] lemma inf_eq_inter : ((⊓) : set α → set α → set α) = (∩) := rfl @[simp] lemma le_eq_subset : ((≤) : set α → set α → Prop) = (⊆) := rfl /-! `set.lt_eq_ssubset` is defined further down -/ @[simp] lemma compl_eq_compl : set.compl = (has_compl.compl : set α → set α) := rfl /-- Coercion from a set to the corresponding subtype. -/ instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩ instance pi_set_coe.can_lift (ι : Type u) (α : Π i : ι, Type v) [ne : Π i, nonempty (α i)] (s : set ι) : can_lift (Π i : s, α i) (Π i, α i) := { coe := λ f i, f i, .. pi_subtype.can_lift ι α s } instance pi_set_coe.can_lift' (ι : Type u) (α : Type v) [ne : nonempty α] (s : set ι) : can_lift (s → α) (ι → α) := pi_set_coe.can_lift ι (λ _, α) s instance set_coe.can_lift (s : set α) : can_lift α s := { coe := coe, cond := λ a, a ∈ s, prf := λ a ha, ⟨⟨a, ha⟩, rfl⟩ } end set section set_coe variables {α : Type u} theorem set.set_coe_eq_subtype (s : set α) : coe_sort.{(u+1) (u+2)} s = {x // x ∈ s} := rfl @[simp] theorem set_coe.forall {s : set α} {p : s → Prop} : (∀ x : s, p x) ↔ (∀ x (h : x ∈ s), p ⟨x, h⟩) := subtype.forall @[simp] theorem set_coe.exists {s : set α} {p : s → Prop} : (∃ x : s, p x) ↔ (∃ x (h : x ∈ s), p ⟨x, h⟩) := subtype.exists theorem set_coe.exists' {s : set α} {p : Π x, x ∈ s → Prop} : (∃ x (h : x ∈ s), p x h) ↔ (∃ x : s, p x x.2) := (@set_coe.exists _ _ $ λ x, p x.1 x.2).symm theorem set_coe.forall' {s : set α} {p : Π x, x ∈ s → Prop} : (∀ x (h : x ∈ s), p x h) ↔ (∀ x : s, p x x.2) := (@set_coe.forall _ _ $ λ x, p x.1 x.2).symm @[simp] theorem set_coe_cast : ∀ {s t : set α} (H' : s = t) (H : @eq (Type u) s t) (x : s), cast H x = ⟨x.1, H' ▸ x.2⟩ | s _ rfl _ ⟨x, h⟩ := rfl theorem set_coe.ext {s : set α} {a b : s} : (↑a : α) = ↑b → a = b := subtype.eq theorem set_coe.ext_iff {s : set α} {a b : s} : (↑a : α) = ↑b ↔ a = b := iff.intro set_coe.ext (assume h, h ▸ rfl) end set_coe /-- See also `subtype.prop` -/ lemma subtype.mem {α : Type*} {s : set α} (p : s) : (p : α) ∈ s := p.prop lemma eq.subset {α} {s t : set α} : s = t → s ⊆ t := by { rintro rfl x hx, exact hx } namespace set variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a : α} {s t : set α} instance : inhabited (set α) := ⟨∅⟩ @[ext] theorem ext {a b : set α} (h : ∀ x, x ∈ a ↔ x ∈ b) : a = b := funext (assume x, propext (h x)) theorem ext_iff {s t : set α} : s = t ↔ ∀ x, x ∈ s ↔ x ∈ t := ⟨λ h x, by rw h, ext⟩ @[trans] theorem mem_of_mem_of_subset {x : α} {s t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx /-! ### Lemmas about `mem` and `set_of` -/ @[simp] theorem mem_set_of_eq {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl theorem nmem_set_of_eq {a : α} {P : α → Prop} : a ∉ {a : α | P a} = ¬ P a := rfl @[simp] theorem set_of_mem_eq {s : set α} : {x | x ∈ s} = s := rfl theorem set_of_set {s : set α} : set_of s = s := rfl lemma set_of_app_iff {p : α → Prop} {x : α} : { x | p x } x ↔ p x := iff.rfl theorem mem_def {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl instance decidable_set_of (p : α → Prop) [H : decidable_pred p] : decidable_pred (∈ {a | p a}) := H @[simp] theorem set_of_subset_set_of {p q : α → Prop} : {a | p a} ⊆ {a | q a} ↔ (∀a, p a → q a) := iff.rfl @[simp] lemma sep_set_of {p q : α → Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl lemma set_of_and {p q : α → Prop} : {a | p a ∧ q a} = {a | p a} ∩ {a | q a} := rfl lemma set_of_or {p q : α → Prop} : {a | p a ∨ q a} = {a | p a} ∪ {a | q a} := rfl /-! ### Lemmas about subsets -/ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? theorem subset_def {s t : set α} : (s ⊆ t) = ∀ x, x ∈ s → x ∈ t := rfl @[refl] theorem subset.refl (a : set α) : a ⊆ a := assume x, id theorem subset.rfl {s : set α} : s ⊆ s := subset.refl s @[trans] theorem subset.trans {a b c : set α} (ab : a ⊆ b) (bc : b ⊆ c) : a ⊆ c := assume x h, bc (ab h) @[trans] theorem mem_of_eq_of_mem {x y : α} {s : set α} (hx : x = y) (h : y ∈ s) : x ∈ s := hx.symm ▸ h theorem subset.antisymm {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := set.ext $ λ x, ⟨@h₁ _, @h₂ _⟩ theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := ⟨λ e, ⟨e.subset, e.symm.subset⟩, λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩ -- an alternative name theorem eq_of_subset_of_subset {a b : set α} : a ⊆ b → b ⊆ a → a = b := subset.antisymm theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} (h : s₁ ⊆ s₂) : a ∈ s₁ → a ∈ s₂ := @h _ theorem not_mem_subset (h : s ⊆ t) : a ∉ t → a ∉ s := mt $ mem_of_subset_of_mem h theorem not_subset : (¬ s ⊆ t) ↔ ∃a ∈ s, a ∉ t := by simp only [subset_def, not_forall] /-! ### Definition of strict subsets `s ⊂ t` and basic properties. -/ instance : has_ssubset (set α) := ⟨(<)⟩ @[simp] lemma lt_eq_ssubset : ((<) : set α → set α → Prop) = (⊂) := rfl theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ ¬ (t ⊆ s)) := rfl theorem eq_or_ssubset_of_subset (h : s ⊆ t) : s = t ∨ s ⊂ t := eq_or_lt_of_le h lemma exists_of_ssubset {s t : set α} (h : s ⊂ t) : (∃x∈t, x ∉ s) := not_subset.1 h.2 lemma ssubset_iff_subset_ne {s t : set α} : s ⊂ t ↔ s ⊆ t ∧ s ≠ t := @lt_iff_le_and_ne (set α) _ s t lemma ssubset_iff_of_subset {s t : set α} (h : s ⊆ t) : s ⊂ t ↔ ∃ x ∈ t, x ∉ s := ⟨exists_of_ssubset, λ ⟨x, hxt, hxs⟩, ⟨h, λ h, hxs $ h hxt⟩⟩ lemma ssubset_of_ssubset_of_subset {s₁ s₂ s₃ : set α} (hs₁s₂ : s₁ ⊂ s₂) (hs₂s₃ : s₂ ⊆ s₃) : s₁ ⊂ s₃ := ⟨subset.trans hs₁s₂.1 hs₂s₃, λ hs₃s₁, hs₁s₂.2 (subset.trans hs₂s₃ hs₃s₁)⟩ lemma ssubset_of_subset_of_ssubset {s₁ s₂ s₃ : set α} (hs₁s₂ : s₁ ⊆ s₂) (hs₂s₃ : s₂ ⊂ s₃) : s₁ ⊂ s₃ := ⟨subset.trans hs₁s₂ hs₂s₃.1, λ hs₃s₁, hs₂s₃.2 (subset.trans hs₃s₁ hs₁s₂)⟩ theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) := id @[simp] theorem not_not_mem : ¬ (a ∉ s) ↔ a ∈ s := not_not /-! ### Non-empty sets -/ /-- The property `s.nonempty` expresses the fact that the set `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : set α) : Prop := ∃ x, x ∈ s @[simp] lemma nonempty_coe_sort (s : set α) : nonempty ↥s ↔ s.nonempty := nonempty_subtype lemma nonempty_def : s.nonempty ↔ ∃ x, x ∈ s := iff.rfl lemma nonempty_of_mem {x} (h : x ∈ s) : s.nonempty := ⟨x, h⟩ theorem nonempty.not_subset_empty : s.nonempty → ¬(s ⊆ ∅) | ⟨x, hx⟩ hs := hs hx theorem nonempty.ne_empty : ∀ {s : set α}, s.nonempty → s ≠ ∅ | _ ⟨x, hx⟩ rfl := hx @[simp] theorem not_nonempty_empty : ¬(∅ : set α).nonempty := λ h, h.ne_empty rfl /-- Extract a witness from `s.nonempty`. This function might be used instead of case analysis on the argument. Note that it makes a proof depend on the `classical.choice` axiom. -/ protected noncomputable def nonempty.some (h : s.nonempty) : α := classical.some h protected lemma nonempty.some_mem (h : s.nonempty) : h.some ∈ s := classical.some_spec h lemma nonempty.mono (ht : s ⊆ t) (hs : s.nonempty) : t.nonempty := hs.imp ht lemma nonempty_of_not_subset (h : ¬s ⊆ t) : (s \ t).nonempty := let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩ lemma nonempty_of_ssubset (ht : s ⊂ t) : (t \ s).nonempty := nonempty_of_not_subset ht.2 lemma nonempty.of_diff (h : (s \ t).nonempty) : s.nonempty := h.imp $ λ _, and.left lemma nonempty_of_ssubset' (ht : s ⊂ t) : t.nonempty := (nonempty_of_ssubset ht).of_diff lemma nonempty.inl (hs : s.nonempty) : (s ∪ t).nonempty := hs.imp $ λ _, or.inl lemma nonempty.inr (ht : t.nonempty) : (s ∪ t).nonempty := ht.imp $ λ _, or.inr @[simp] lemma union_nonempty : (s ∪ t).nonempty ↔ s.nonempty ∨ t.nonempty := exists_or_distrib lemma nonempty.left (h : (s ∩ t).nonempty) : s.nonempty := h.imp $ λ _, and.left lemma nonempty.right (h : (s ∩ t).nonempty) : t.nonempty := h.imp $ λ _, and.right lemma nonempty_inter_iff_exists_right : (s ∩ t).nonempty ↔ ∃ x : t, ↑x ∈ s := ⟨λ ⟨x, xs, xt⟩, ⟨⟨x, xt⟩, xs⟩, λ ⟨⟨x, xt⟩, xs⟩, ⟨x, xs, xt⟩⟩ lemma nonempty_inter_iff_exists_left : (s ∩ t).nonempty ↔ ∃ x : s, ↑x ∈ t := ⟨λ ⟨x, xs, xt⟩, ⟨⟨x, xs⟩, xt⟩, λ ⟨⟨x, xt⟩, xs⟩, ⟨x, xt, xs⟩⟩ lemma nonempty_iff_univ_nonempty : nonempty α ↔ (univ : set α).nonempty := ⟨λ ⟨x⟩, ⟨x, trivial⟩, λ ⟨x, _⟩, ⟨x⟩⟩ @[simp] lemma univ_nonempty : ∀ [h : nonempty α], (univ : set α).nonempty | ⟨x⟩ := ⟨x, trivial⟩ lemma nonempty.to_subtype (h : s.nonempty) : nonempty s := nonempty_subtype.2 h instance [nonempty α] : nonempty (set.univ : set α) := set.univ_nonempty.to_subtype @[simp] lemma nonempty_insert (a : α) (s : set α) : (insert a s).nonempty := ⟨a, or.inl rfl⟩ lemma nonempty_of_nonempty_subtype [nonempty s] : s.nonempty := nonempty_subtype.mp ‹_› /-! ### Lemmas about the empty set -/ theorem empty_def : (∅ : set α) = {x | false} := rfl @[simp] theorem mem_empty_eq (x : α) : x ∈ (∅ : set α) = false := rfl @[simp] theorem set_of_false : {a : α | false} = ∅ := rfl @[simp] theorem empty_subset (s : set α) : ∅ ⊆ s. theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ := (subset.antisymm_iff.trans $ and_iff_left (empty_subset _)).symm theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s := subset_empty_iff.symm theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ := subset_empty_iff.1 theorem eq_empty_of_is_empty [is_empty α] (s : set α) : s = ∅ := eq_empty_of_subset_empty $ λ x hx, is_empty_elim x /-- There is exactly one set of a type that is empty. -/ -- TODO[gh-6025]: make this an instance once safe to do so def unique_empty [is_empty α] : unique (set α) := { default := ∅, uniq := eq_empty_of_is_empty } lemma not_nonempty_iff_eq_empty {s : set α} : ¬s.nonempty ↔ s = ∅ := by simp only [set.nonempty, eq_empty_iff_forall_not_mem, not_exists] lemma empty_not_nonempty : ¬(∅ : set α).nonempty := λ h, h.ne_empty rfl theorem ne_empty_iff_nonempty : s ≠ ∅ ↔ s.nonempty := not_iff_comm.1 not_nonempty_iff_eq_empty lemma eq_empty_or_nonempty (s : set α) : s = ∅ ∨ s.nonempty := or_iff_not_imp_left.2 ne_empty_iff_nonempty.1 theorem subset_eq_empty {s t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ := subset_empty_iff.1 $ e ▸ h theorem ball_empty_iff {p : α → Prop} : (∀ x ∈ (∅ : set α), p x) ↔ true := iff_true_intro $ λ x, false.elim instance (α : Type u) : is_empty.{u+1} (∅ : set α) := ⟨λ x, x.2⟩ /-! ### Universal set. In Lean `@univ α` (or `univ : set α`) is the set that contains all elements of type `α`. Mathematically it is the same as `α` but it has a different type. -/ @[simp] theorem set_of_true : {x : α | true} = univ := rfl @[simp] theorem mem_univ (x : α) : x ∈ @univ α := trivial @[simp] lemma univ_eq_empty_iff : (univ : set α) = ∅ ↔ is_empty α := eq_empty_iff_forall_not_mem.trans ⟨λ H, ⟨λ x, H x trivial⟩, λ H x _, @is_empty.false α H x⟩ theorem empty_ne_univ [nonempty α] : (∅ : set α) ≠ univ := λ e, not_is_empty_of_nonempty α $ univ_eq_empty_iff.1 e.symm @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ := (subset.antisymm_iff.trans $ and_iff_right (subset_univ _)).symm theorem eq_univ_of_univ_subset {s : set α} : univ ⊆ s → s = univ := univ_subset_iff.1 theorem eq_univ_iff_forall {s : set α} : s = univ ↔ ∀ x, x ∈ s := univ_subset_iff.symm.trans $ forall_congr $ λ x, imp_iff_right ⟨⟩ theorem eq_univ_of_forall {s : set α} : (∀ x, x ∈ s) → s = univ := eq_univ_iff_forall.2 lemma eq_univ_of_subset {s t : set α} (h : s ⊆ t) (hs : s = univ) : t = univ := eq_univ_of_univ_subset $ hs ▸ h lemma exists_mem_of_nonempty (α) : ∀ [nonempty α], ∃x:α, x ∈ (univ : set α) | ⟨x⟩ := ⟨x, trivial⟩ instance univ_decidable : decidable_pred (∈ @set.univ α) := λ x, is_true trivial lemma ne_univ_iff_exists_not_mem {α : Type*} (s : set α) : s ≠ univ ↔ ∃ a, a ∉ s := by rw [←not_forall, ←eq_univ_iff_forall] lemma not_subset_iff_exists_mem_not_mem {α : Type*} {s t : set α} : ¬ s ⊆ t ↔ ∃ x, x ∈ s ∧ x ∉ t := by simp [subset_def] /-- `diagonal α` is the subset of `α × α` consisting of all pairs of the form `(a, a)`. -/ def diagonal (α : Type*) : set (α × α) := {p | p.1 = p.2} @[simp] lemma mem_diagonal {α : Type*} (x : α) : (x, x) ∈ diagonal α := by simp [diagonal] /-! ### Lemmas about union -/ theorem union_def {s₁ s₂ : set α} : s₁ ∪ s₂ = {a | a ∈ s₁ ∨ a ∈ s₂} := rfl theorem mem_union_left {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := or.inl theorem mem_union_right {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := or.inr theorem mem_or_mem_of_mem_union {x : α} {a b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {x : α} {a b : set α} {P : Prop} (H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P := or.elim H₁ H₂ H₃ theorem mem_union (x : α) (a b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl @[simp] theorem mem_union_eq (x : α) (a b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl @[simp] theorem union_self (a : set α) : a ∪ a = a := ext $ λ x, or_self _ @[simp] theorem union_empty (a : set α) : a ∪ ∅ = a := ext $ λ x, or_false _ @[simp] theorem empty_union (a : set α) : ∅ ∪ a = a := ext $ λ x, false_or _ theorem union_comm (a b : set α) : a ∪ b = b ∪ a := ext $ λ x, or.comm theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) := ext $ λ x, or.assoc instance union_is_assoc : is_associative (set α) (∪) := ⟨union_assoc⟩ instance union_is_comm : is_commutative (set α) (∪) := ⟨union_comm⟩ theorem union_left_comm (s₁ s₂ s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext $ λ x, or.left_comm theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := ext $ λ x, or.right_comm @[simp] theorem union_eq_left_iff_subset {s t : set α} : s ∪ t = s ↔ t ⊆ s := sup_eq_left @[simp] theorem union_eq_right_iff_subset {s t : set α} : s ∪ t = t ↔ s ⊆ t := sup_eq_right theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := union_eq_right_iff_subset.mpr h theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := union_eq_left_iff_subset.mpr h @[simp] theorem subset_union_left (s t : set α) : s ⊆ s ∪ t := λ x, or.inl @[simp] theorem subset_union_right (s t : set α) : t ⊆ s ∪ t := λ x, or.inr theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := λ x, or.rec (@sr _) (@tr _) @[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := (forall_congr (by exact λ x, or_imp_distrib)).trans forall_and_distrib theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := λ x, or.imp (@h₁ _) (@h₂ _) theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h subset.rfl theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union subset.rfl h lemma subset_union_of_subset_left {s t : set α} (h : s ⊆ t) (u : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_left t u) lemma subset_union_of_subset_right {s u : set α} (h : s ⊆ u) (t : set α) : s ⊆ t ∪ u := subset.trans h (subset_union_right t u) @[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ := by simp only [← subset_empty_iff]; exact union_subset_iff @[simp] lemma union_univ {s : set α} : s ∪ univ = univ := sup_top_eq @[simp] lemma univ_union {s : set α} : univ ∪ s = univ := top_sup_eq /-! ### Lemmas about intersection -/ theorem inter_def {s₁ s₂ : set α} : s₁ ∩ s₂ = {a | a ∈ s₁ ∧ a ∈ s₂} := rfl theorem mem_inter_iff (x : α) (a b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq (x : α) (a b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {x : α} {a b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := ⟨ha, hb⟩ theorem mem_of_mem_inter_left {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ a := h.left theorem mem_of_mem_inter_right {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ b := h.right @[simp] theorem inter_self (a : set α) : a ∩ a = a := ext $ λ x, and_self _ @[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ := ext $ λ x, and_false _ @[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ := ext $ λ x, false_and _ theorem inter_comm (a b : set α) : a ∩ b = b ∩ a := ext $ λ x, and.comm theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext $ λ x, and.assoc instance inter_is_assoc : is_associative (set α) (∩) := ⟨inter_assoc⟩ instance inter_is_comm : is_commutative (set α) (∩) := ⟨inter_comm⟩ theorem inter_left_comm (s₁ s₂ s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext $ λ x, and.left_comm theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext $ λ x, and.right_comm @[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x, and.left @[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x, and.right theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := λ x h, ⟨rs h, rt h⟩ @[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := (forall_congr (by exact λ x, imp_and_distrib)).trans forall_and_distrib @[simp] theorem inter_eq_left_iff_subset {s t : set α} : s ∩ t = s ↔ s ⊆ t := inf_eq_left @[simp] theorem inter_eq_right_iff_subset {s t : set α} : s ∩ t = t ↔ t ⊆ s := inf_eq_right theorem inter_eq_self_of_subset_left {s t : set α} : s ⊆ t → s ∩ t = s := inter_eq_left_iff_subset.mpr theorem inter_eq_self_of_subset_right {s t : set α} : t ⊆ s → s ∩ t = t := inter_eq_right_iff_subset.mpr @[simp] theorem inter_univ (a : set α) : a ∩ univ = a := inf_top_eq @[simp] theorem univ_inter (a : set α) : univ ∩ a = a := top_inf_eq theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := λ x, and.imp (@h₁ _) (@h₂ _) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := inter_subset_inter H subset.rfl theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := inter_subset_inter subset.rfl H theorem union_inter_cancel_left {s t : set α} : (s ∪ t) ∩ s = s := inter_eq_self_of_subset_right $ subset_union_left _ _ theorem union_inter_cancel_right {s t : set α} : (s ∪ t) ∩ t = t := inter_eq_self_of_subset_right $ subset_union_right _ _ /-! ### Distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_union_distrib_left {s t u : set α} : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_inter_distrib_right {s t u : set α} : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_inter_distrib_left {s t u : set α} : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right theorem inter_union_distrib_right {s t u : set α} : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right /-! ### Lemmas about `insert` `insert α s` is the set `{α} ∪ s`. -/ theorem insert_def (x : α) (s : set α) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem subset_insert (x : α) (s : set α) : s ⊆ insert x s := λ y, or.inr theorem mem_insert (x : α) (s : set α) : x ∈ insert x s := or.inl rfl theorem mem_insert_of_mem {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := or.inr theorem eq_or_mem_of_mem_insert {x a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := id theorem mem_of_mem_insert_of_ne {x a : α} {s : set α} : x ∈ insert a s → x ≠ a → x ∈ s := or.resolve_left @[simp] theorem mem_insert_iff {x a : α} {s : set α} : x ∈ insert a s ↔ x = a ∨ x ∈ s := iff.rfl @[simp] theorem insert_eq_of_mem {a : α} {s : set α} (h : a ∈ s) : insert a s = s := ext $ λ x, or_iff_right_of_imp $ λ e, e.symm ▸ h lemma ne_insert_of_not_mem {s : set α} (t : set α) {a : α} : a ∉ s → s ≠ insert a t := mt $ λ e, e.symm ▸ mem_insert _ _ theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) := by simp only [subset_def, or_imp_distrib, forall_and_distrib, forall_eq, mem_insert_iff] theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t := λ x, or.imp_right (@h _) theorem ssubset_iff_insert {s t : set α} : s ⊂ t ↔ ∃ a ∉ s, insert a s ⊆ t := begin simp only [insert_subset, exists_and_distrib_right, ssubset_def, not_subset], simp only [exists_prop, and_comm] end theorem ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff_insert.2 ⟨a, h, subset.refl _⟩ theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := ext $ λ x, or.left_comm theorem insert_union : insert a s ∪ t = insert a (s ∪ t) := ext $ λ x, or.assoc @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := ext $ λ x, or.left_comm theorem insert_nonempty (a : α) (s : set α) : (insert a s).nonempty := ⟨a, mem_insert a s⟩ instance (a : α) (s : set α) : nonempty (insert a s : set α) := (insert_nonempty a s).to_subtype lemma insert_inter (x : α) (s t : set α) : insert x (s ∩ t) = insert x s ∩ insert x t := ext $ λ y, or_and_distrib_left -- useful in proofs by induction theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (H : ∀ x, x ∈ insert a s → P x) (x) (h : x ∈ s) : P x := H _ (or.inr h) theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (H : ∀ x, x ∈ s → P x) (ha : P a) (x) (h : x ∈ insert a s) : P x := h.elim (λ e, e.symm ▸ ha) (H _) theorem bex_insert_iff {P : α → Prop} {a : α} {s : set α} : (∃ x ∈ insert a s, P x) ↔ P a ∨ (∃ x ∈ s, P x) := bex_or_left_distrib.trans $ or_congr_left bex_eq_left theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := ball_or_left_distrib.trans $ and_congr_left' forall_eq /-! ### Lemmas about singletons -/ theorem singleton_def (a : α) : ({a} : set α) = insert a ∅ := (insert_emptyc_eq _).symm @[simp] theorem mem_singleton_iff {a b : α} : a ∈ ({b} : set α) ↔ a = b := iff.rfl @[simp] lemma set_of_eq_eq_singleton {a : α} : {n | n = a} = {a} := rfl @[simp] lemma set_of_eq_eq_singleton' {a : α} : {x | a = x} = {a} := ext $ λ x, eq_comm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := @rfl _ _ theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := h @[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y := ext_iff.trans eq_iff_eq_cancel_left theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := H theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := rfl @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := union_self _ theorem pair_comm (a b : α) : ({a, b} : set α) = {b, a} := union_comm _ _ @[simp] theorem singleton_nonempty (a : α) : ({a} : set α).nonempty := ⟨a, rfl⟩ @[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s := forall_eq theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} := rfl @[simp] theorem singleton_union : {a} ∪ s = insert a s := rfl @[simp] theorem union_singleton : s ∪ {a} = insert a s := union_comm _ _ @[simp] theorem singleton_inter_nonempty : ({a} ∩ s).nonempty ↔ a ∈ s := by simp only [set.nonempty, mem_inter_eq, mem_singleton_iff, exists_eq_left] @[simp] theorem inter_singleton_nonempty : (s ∩ {a}).nonempty ↔ a ∈ s := by rw [inter_comm, singleton_inter_nonempty] @[simp] theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s := not_nonempty_iff_eq_empty.symm.trans $ not_congr singleton_inter_nonempty @[simp] theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s := by rw [inter_comm, singleton_inter_eq_empty] lemma nmem_singleton_empty {s : set α} : s ∉ ({∅} : set (set α)) ↔ s.nonempty := ne_empty_iff_nonempty instance unique_singleton (a : α) : unique ↥({a} : set α) := ⟨⟨⟨a, mem_singleton a⟩⟩, λ ⟨x, h⟩, subtype.eq h⟩ lemma eq_singleton_iff_unique_mem {s : set α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := subset.antisymm_iff.trans $ and.comm.trans $ and_congr_left' singleton_subset_iff lemma eq_singleton_iff_nonempty_unique_mem {s : set α} {a : α} : s = {a} ↔ s.nonempty ∧ ∀ x ∈ s, x = a := eq_singleton_iff_unique_mem.trans $ and_congr_left $ λ H, ⟨λ h', ⟨_, h'⟩, λ ⟨x, h⟩, H x h ▸ h⟩ -- while `simp` is capable of proving this, it is not capable of turning the LHS into the RHS. @[simp] lemma default_coe_singleton (x : α) : default ({x} : set α) = ⟨x, rfl⟩ := rfl /-! ### Lemmas about sets defined as `{x ∈ s | p x}`. -/ theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[simp] theorem sep_mem_eq {s t : set α} : {x ∈ s | x ∈ t} = s ∩ t := rfl @[simp] theorem mem_sep_eq {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem eq_sep_of_subset {s t : set α} (h : s ⊆ t) : s = {x ∈ t | x ∈ s} := (inter_eq_self_of_subset_right h).symm @[simp] theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := λ x, and.left theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (H : {x ∈ s | p x} = ∅) (x) : x ∈ s → ¬ p x := not_and.1 (eq_empty_iff_forall_not_mem.1 H x : _) @[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} := univ_inter _ @[simp] lemma sep_true : {a ∈ s | true} = s := by { ext, simp } @[simp] lemma sep_false : {a ∈ s | false} = ∅ := by { ext, simp } @[simp] lemma subset_singleton_iff {α : Type*} {s : set α} {x : α} : s ⊆ {x} ↔ ∀ y ∈ s, y = x := iff.rfl lemma subset_singleton_iff_eq {s : set α} {x : α} : s ⊆ {x} ↔ s = ∅ ∨ s = {x} := begin obtain (rfl | hs) := s.eq_empty_or_nonempty, use ⟨λ _, or.inl rfl, λ _, empty_subset _⟩, simp [eq_singleton_iff_nonempty_unique_mem, hs, ne_empty_iff_nonempty.2 hs], end lemma ssubset_singleton_iff {s : set α} {x : α} : s ⊂ {x} ↔ s = ∅ := begin rw [ssubset_iff_subset_ne, subset_singleton_iff_eq, or_and_distrib_right, and_not_self, or_false, and_iff_left_iff_imp], rintro rfl, refine ne_comm.1 (ne_empty_iff_nonempty.2 (singleton_nonempty _)), end lemma eq_empty_of_ssubset_singleton {s : set α} {x : α} (hs : s ⊂ {x}) : s = ∅ := ssubset_singleton_iff.1 hs /-! ### Lemmas about complement -/ theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ sᶜ := h lemma compl_set_of {α} (p : α → Prop) : {a | p a}ᶜ = { a | ¬ p a } := rfl theorem not_mem_of_mem_compl {s : set α} {x : α} (h : x ∈ sᶜ) : x ∉ s := h @[simp] theorem mem_compl_eq (s : set α) (x : α) : x ∈ sᶜ = (x ∉ s) := rfl theorem mem_compl_iff (s : set α) (x : α) : x ∈ sᶜ ↔ x ∉ s := iff.rfl @[simp] theorem inter_compl_self (s : set α) : s ∩ sᶜ = ∅ := inf_compl_eq_bot @[simp] theorem compl_inter_self (s : set α) : sᶜ ∩ s = ∅ := compl_inf_eq_bot @[simp] theorem compl_empty : (∅ : set α)ᶜ = univ := compl_bot @[simp] theorem compl_union (s t : set α) : (s ∪ t)ᶜ = sᶜ ∩ tᶜ := compl_sup theorem compl_inter (s t : set α) : (s ∩ t)ᶜ = sᶜ ∪ tᶜ := compl_inf @[simp] theorem compl_univ : (univ : set α)ᶜ = ∅ := compl_top @[simp] lemma compl_empty_iff {s : set α} : sᶜ = ∅ ↔ s = univ := compl_eq_bot @[simp] lemma compl_univ_iff {s : set α} : sᶜ = univ ↔ s = ∅ := compl_eq_top lemma nonempty_compl {s : set α} : sᶜ.nonempty ↔ s ≠ univ := ne_empty_iff_nonempty.symm.trans $ not_congr $ compl_empty_iff lemma mem_compl_singleton_iff {a x : α} : x ∈ ({a} : set α)ᶜ ↔ x ≠ a := not_congr mem_singleton_iff lemma compl_singleton_eq (a : α) : ({a} : set α)ᶜ = {x | x ≠ a} := ext $ λ x, mem_compl_singleton_iff @[simp] lemma compl_ne_eq_singleton (a : α) : ({x | x ≠ a} : set α)ᶜ = {a} := by { ext, simp, } theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = (sᶜ ∩ tᶜ)ᶜ := ext $ λ x, or_iff_not_and_not theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = (sᶜ ∪ tᶜ)ᶜ := ext $ λ x, and_iff_not_or_not @[simp] theorem union_compl_self (s : set α) : s ∪ sᶜ = univ := eq_univ_iff_forall.2 $ λ x, em _ @[simp] theorem compl_union_self (s : set α) : sᶜ ∪ s = univ := by rw [union_comm, union_compl_self] theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext compl_compl theorem compl_subset_comm {s t : set α} : sᶜ ⊆ t ↔ tᶜ ⊆ s := @compl_le_iff_compl_le _ s t _ @[simp] lemma compl_subset_compl {s t : set α} : sᶜ ⊆ tᶜ ↔ t ⊆ s := @compl_le_compl_iff_le _ t s _ theorem compl_subset_iff_union {s t : set α} : sᶜ ⊆ t ↔ s ∪ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a, or_iff_not_imp_left theorem subset_compl_comm {s t : set α} : s ⊆ tᶜ ↔ t ⊆ sᶜ := forall_congr $ λ a, imp_not_comm theorem subset_compl_iff_disjoint {s t : set α} : s ⊆ tᶜ ↔ s ∩ t = ∅ := iff.trans (forall_congr $ λ a, and_imp.symm) subset_empty_iff lemma subset_compl_singleton_iff {a : α} {s : set α} : s ⊆ {a}ᶜ ↔ a ∉ s := subset_compl_comm.trans singleton_subset_iff theorem inter_subset (a b c : set α) : a ∩ b ⊆ c ↔ a ⊆ bᶜ ∪ c := forall_congr $ λ x, and_imp.trans $ imp_congr_right $ λ _, imp_iff_not_or lemma inter_compl_nonempty_iff {s t : set α} : (s ∩ tᶜ).nonempty ↔ ¬ s ⊆ t := (not_subset.trans $ exists_congr $ by exact λ x, by simp [mem_compl]).symm /-! ### Lemmas about set difference -/ theorem diff_eq (s t : set α) : s \ t = s ∩ tᶜ := rfl @[simp] theorem mem_diff {s t : set α} (x : α) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.rfl theorem mem_diff_of_mem {s t : set α} {x : α} (h1 : x ∈ s) (h2 : x ∉ t) : x ∈ s \ t := ⟨h1, h2⟩ theorem mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s := h.left theorem not_mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∉ t := h.right theorem diff_eq_compl_inter {s t : set α} : s \ t = tᶜ ∩ s := by rw [diff_eq, inter_comm] theorem nonempty_diff {s t : set α} : (s \ t).nonempty ↔ ¬ (s ⊆ t) := inter_compl_nonempty_iff theorem diff_subset (s t : set α) : s \ t ⊆ s := show s \ t ≤ s, from sdiff_le theorem union_diff_cancel' {s t u : set α} (h₁ : s ⊆ t) (h₂ : t ⊆ u) : t ∪ (u \ s) = u := sup_sdiff_cancel' h₁ h₂ theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := sup_sdiff_of_le h theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := disjoint.sup_sdiff_cancel_left h theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := disjoint.sup_sdiff_cancel_right h @[simp] theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s := sup_sdiff_left_self @[simp] theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t := sup_sdiff_right_self theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := sup_sdiff theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) := inf_sdiff_assoc @[simp] theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ := inf_sdiff_self_right @[simp] theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s := sup_inf_sdiff s t @[simp] lemma diff_union_inter (s t : set α) : (s \ t) ∪ (s ∩ t) = s := by { rw union_comm, exact sup_inf_sdiff _ _ } @[simp] theorem inter_union_compl (s t : set α) : (s ∩ t) ∪ (s ∩ tᶜ) = s := inter_union_diff _ _ theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := show s₁ ≤ s₂ → t₂ ≤ t₁ → s₁ \ t₁ ≤ s₂ \ t₂, from sdiff_le_sdiff theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := sdiff_le_self_sdiff ‹s₁ ≤ s₂› theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := sdiff_le_sdiff_self ‹t ≤ u› theorem compl_eq_univ_diff (s : set α) : sᶜ = univ \ s := top_sdiff.symm @[simp] lemma empty_diff (s : set α) : (∅ \ s : set α) = ∅ := bot_sdiff theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t := sdiff_eq_bot_iff @[simp] theorem diff_empty {s : set α} : s \ ∅ = s := sdiff_bot @[simp] lemma diff_univ (s : set α) : s \ univ = ∅ := diff_eq_empty.2 (subset_univ s) theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) := sdiff_sdiff_left -- the following statement contains parentheses to help the reader lemma diff_diff_comm {s t u : set α} : (s \ t) \ u = (s \ u) \ t := sdiff_sdiff_comm lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := show s \ t ≤ u ↔ s ≤ t ∪ u, from sdiff_le_iff lemma subset_diff_union (s t : set α) : s ⊆ (s \ t) ∪ t := show s ≤ (s \ t) ∪ t, from le_sdiff_sup lemma diff_union_of_subset {s t : set α} (h : t ⊆ s) : (s \ t) ∪ t = s := subset.antisymm (union_subset (diff_subset _ _) h) (subset_diff_union _ _) @[simp] lemma diff_singleton_subset_iff {x : α} {s t : set α} : s \ {x} ⊆ t ↔ s ⊆ insert x t := by { rw [←union_singleton, union_comm], apply diff_subset_iff } lemma subset_diff_singleton {x : α} {s t : set α} (h : s ⊆ t) (hx : x ∉ s) : s ⊆ t \ {x} := subset_inter h $ subset_compl_comm.1 $ singleton_subset_iff.2 hx lemma subset_insert_diff_singleton (x : α) (s : set α) : s ⊆ insert x (s \ {x}) := by rw [←diff_singleton_subset_iff] lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t := show s \ t ≤ u ↔ s \ u ≤ t, from sdiff_le_comm lemma diff_inter {s t u : set α} : s \ (t ∩ u) = (s \ t) ∪ (s \ u) := sdiff_inf lemma diff_inter_diff {s t u : set α} : s \ t ∩ (s \ u) = s \ (t ∪ u) := sdiff_sup.symm lemma diff_compl : s \ tᶜ = s ∩ t := sdiff_compl lemma diff_diff_right {s t u : set α} : s \ (t \ u) = (s \ t) ∪ (s ∩ u) := sdiff_sdiff_right' @[simp] theorem insert_diff_of_mem (s) (h : a ∈ t) : insert a s \ t = s \ t := by { ext, split; simp [or_imp_distrib, h] {contextual := tt} } theorem insert_diff_of_not_mem (s) (h : a ∉ t) : insert a s \ t = insert a (s \ t) := begin classical, ext x, by_cases h' : x ∈ t, { have : x ≠ a, { assume H, rw H at h', exact h h' }, simp [h, h', this] }, { simp [h, h'] } end lemma insert_diff_self_of_not_mem {a : α} {s : set α} (h : a ∉ s) : insert a s \ {a} = s := by { ext, simp [and_iff_left_of_imp (λ hx : x ∈ s, show x ≠ a, from λ hxa, h $ hxa ▸ hx)] } lemma insert_inter_of_mem {s₁ s₂ : set α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := by simp [set.insert_inter, h] lemma insert_inter_of_not_mem {s₁ s₂ : set α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := begin ext x, simp only [mem_inter_iff, mem_insert_iff, mem_inter_eq, and.congr_left_iff, or_iff_right_iff_imp], cc, end @[simp] theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t := sup_sdiff_self_right @[simp] theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t := sup_sdiff_self_left @[simp] theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ := inf_sdiff_self_left @[simp] theorem diff_inter_self_eq_diff {s t : set α} : s \ (t ∩ s) = s \ t := sdiff_inf_self_right @[simp] theorem diff_self_inter {s t : set α} : s \ (s ∩ t) = s \ t := sdiff_inf_self_left @[simp] theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := show s \ t = s ↔ t ⊓ s ≤ ⊥, from sdiff_eq_self_iff_disjoint @[simp] theorem diff_singleton_eq_self {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s := diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h] @[simp] theorem insert_diff_singleton {a : α} {s : set α} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union] @[simp] lemma diff_self {s : set α} : s \ s = ∅ := sdiff_self lemma diff_diff_cancel_left {s t : set α} (h : s ⊆ t) : t \ (t \ s) = s := sdiff_sdiff_eq_self h lemma mem_diff_singleton {x y : α} {s : set α} : x ∈ s \ {y} ↔ (x ∈ s ∧ x ≠ y) := iff.rfl lemma mem_diff_singleton_empty {s : set α} {t : set (set α)} : s ∈ t \ {∅} ↔ (s ∈ t ∧ s.nonempty) := mem_diff_singleton.trans $ and_congr iff.rfl ne_empty_iff_nonempty lemma union_eq_diff_union_diff_union_inter (s t : set α) : s ∪ t = (s \ t) ∪ (t \ s) ∪ (s ∩ t) := sup_eq_sdiff_sup_sdiff_sup_inf /-! ### Powerset -/ theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h theorem subset_of_mem_powerset {x s : set α} (h : x ∈ powerset s) : x ⊆ s := h @[simp] theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl theorem powerset_inter (s t : set α) : 𝒫 (s ∩ t) = 𝒫 s ∩ 𝒫 t := ext $ λ u, subset_inter_iff @[simp] theorem powerset_mono : 𝒫 s ⊆ 𝒫 t ↔ s ⊆ t := ⟨λ h, h (subset.refl s), λ h u hu, subset.trans hu h⟩ theorem monotone_powerset : monotone (powerset : set α → set (set α)) := λ s t, powerset_mono.2 @[simp] theorem powerset_nonempty : (𝒫 s).nonempty := ⟨∅, empty_subset s⟩ @[simp] theorem powerset_empty : 𝒫 (∅ : set α) = {∅} := ext $ λ s, subset_empty_iff @[simp] theorem powerset_univ : 𝒫 (univ : set α) = univ := eq_univ_of_forall subset_univ /-! ### If-then-else for sets -/ /-- `ite` for sets: `set.ite t s s' ∩ t = s ∩ t`, `set.ite t s s' ∩ tᶜ = s' ∩ tᶜ`. Defined as `s ∩ t ∪ s' \ t`. -/ protected def ite (t s s' : set α) : set α := s ∩ t ∪ s' \ t @[simp] lemma ite_inter_self (t s s' : set α) : t.ite s s' ∩ t = s ∩ t := by rw [set.ite, union_inter_distrib_right, diff_inter_self, inter_assoc, inter_self, union_empty] @[simp] lemma ite_compl (t s s' : set α) : tᶜ.ite s s' = t.ite s' s := by rw [set.ite, set.ite, diff_compl, union_comm, diff_eq] @[simp] lemma ite_inter_compl_self (t s s' : set α) : t.ite s s' ∩ tᶜ = s' ∩ tᶜ := by rw [← ite_compl, ite_inter_self] @[simp] lemma ite_diff_self (t s s' : set α) : t.ite s s' \ t = s' \ t := ite_inter_compl_self t s s' @[simp] lemma ite_same (t s : set α) : t.ite s s = s := inter_union_diff _ _ @[simp] lemma ite_left (s t : set α) : s.ite s t = s ∪ t := by simp [set.ite] @[simp] lemma ite_right (s t : set α) : s.ite t s = t ∩ s := by simp [set.ite] @[simp] lemma ite_empty (s s' : set α) : set.ite ∅ s s' = s' := by simp [set.ite] @[simp] lemma ite_univ (s s' : set α) : set.ite univ s s' = s := by simp [set.ite] @[simp] lemma ite_empty_left (t s : set α) : t.ite ∅ s = s \ t := by simp [set.ite] @[simp] lemma ite_empty_right (t s : set α) : t.ite s ∅ = s ∩ t := by simp [set.ite] lemma ite_mono (t : set α) {s₁ s₁' s₂ s₂' : set α} (h : s₁ ⊆ s₂) (h' : s₁' ⊆ s₂') : t.ite s₁ s₁' ⊆ t.ite s₂ s₂' := union_subset_union (inter_subset_inter_left _ h) (inter_subset_inter_left _ h') lemma ite_subset_union (t s s' : set α) : t.ite s s' ⊆ s ∪ s' := union_subset_union (inter_subset_left _ _) (diff_subset _ _) lemma inter_subset_ite (t s s' : set α) : s ∩ s' ⊆ t.ite s s' := ite_same t (s ∩ s') ▸ ite_mono _ (inter_subset_left _ _) (inter_subset_right _ _) lemma ite_inter_inter (t s₁ s₂ s₁' s₂' : set α) : t.ite (s₁ ∩ s₂) (s₁' ∩ s₂') = t.ite s₁ s₁' ∩ t.ite s₂ s₂' := by { ext x, finish [set.ite, iff_def] } lemma ite_inter (t s₁ s₂ s : set α) : t.ite (s₁ ∩ s) (s₂ ∩ s) = t.ite s₁ s₂ ∩ s := by rw [ite_inter_inter, ite_same] lemma ite_inter_of_inter_eq (t : set α) {s₁ s₂ s : set α} (h : s₁ ∩ s = s₂ ∩ s) : t.ite s₁ s₂ ∩ s = s₁ ∩ s := by rw [← ite_inter, ← h, ite_same] lemma subset_ite {t s s' u : set α} : u ⊆ t.ite s s' ↔ u ∩ t ⊆ s ∧ u \ t ⊆ s' := begin simp only [subset_def, ← forall_and_distrib], refine forall_congr (λ x, _), by_cases hx : x ∈ t; simp [*, set.ite] end /-! ### Inverse image -/ /-- The preimage of `s : set β` by `f : α → β`, written `f ⁻¹' s`, is the set of `x : α` such that `f x ∈ s`. -/ def preimage {α : Type u} {β : Type v} (f : α → β) (s : set β) : set α := {x | f x ∈ s} infix ` ⁻¹' `:80 := preimage section preimage variables {f : α → β} {g : β → γ} @[simp] theorem preimage_empty : f ⁻¹' ∅ = ∅ := rfl @[simp] theorem mem_preimage {s : set β} {a : α} : (a ∈ f ⁻¹' s) ↔ (f a ∈ s) := iff.rfl lemma preimage_congr {f g : α → β} {s : set β} (h : ∀ (x : α), f x = g x) : f ⁻¹' s = g ⁻¹' s := by { congr' with x, apply_assumption } theorem preimage_mono {s t : set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t := assume x hx, h hx @[simp] theorem preimage_univ : f ⁻¹' univ = univ := rfl theorem subset_preimage_univ {s : set α} : s ⊆ f ⁻¹' univ := subset_univ _ @[simp] theorem preimage_inter {s t : set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {s t : set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl @[simp] theorem preimage_compl {s : set β} : f ⁻¹' sᶜ = (f ⁻¹' s)ᶜ := rfl @[simp] theorem preimage_diff (f : α → β) (s t : set β) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl @[simp] theorem preimage_ite (f : α → β) (s t₁ t₂ : set β) : f ⁻¹' (s.ite t₁ t₂) = (f ⁻¹' s).ite (f ⁻¹' t₁) (f ⁻¹' t₂) := rfl @[simp] theorem preimage_set_of_eq {p : α → Prop} {f : β → α} : f ⁻¹' {a | p a} = {a | p (f a)} := rfl @[simp] theorem preimage_id {s : set α} : id ⁻¹' s = s := rfl @[simp] theorem preimage_id' {s : set α} : (λ x, x) ⁻¹' s = s := rfl @[simp] theorem preimage_const_of_mem {b : β} {s : set β} (h : b ∈ s) : (λ (x : α), b) ⁻¹' s = univ := eq_univ_of_forall $ λ x, h @[simp] theorem preimage_const_of_not_mem {b : β} {s : set β} (h : b ∉ s) : (λ (x : α), b) ⁻¹' s = ∅ := eq_empty_of_subset_empty $ λ x hx, h hx theorem preimage_const (b : β) (s : set β) [decidable (b ∈ s)] : (λ (x : α), b) ⁻¹' s = if b ∈ s then univ else ∅ := by { split_ifs with hb hb, exacts [preimage_const_of_mem hb, preimage_const_of_not_mem hb] } theorem preimage_comp {s : set γ} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl lemma preimage_preimage {g : β → γ} {f : α → β} {s : set γ} : f ⁻¹' (g ⁻¹' s) = (λ x, g (f x)) ⁻¹' s := preimage_comp.symm theorem eq_preimage_subtype_val_iff {p : α → Prop} {s : set (subtype p)} {t : set α} : s = subtype.val ⁻¹' t ↔ (∀x (h : p x), (⟨x, h⟩ : subtype p) ∈ s ↔ x ∈ t) := ⟨assume s_eq x h, by { rw [s_eq], simp }, assume h, ext $ λ ⟨x, hx⟩, by simp [h]⟩ lemma preimage_coe_coe_diagonal {α : Type*} (s : set α) : (prod.map coe coe) ⁻¹' (diagonal α) = diagonal s := begin ext ⟨⟨x, x_in⟩, ⟨y, y_in⟩⟩, simp [set.diagonal], end end preimage /-! ### Image of a set under a function -/ section image infix ` '' `:80 := image theorem mem_image_iff_bex {f : α → β} {s : set α} {y : β} : y ∈ f '' s ↔ ∃ x (_ : x ∈ s), f x = y := bex_def.symm theorem mem_image_eq (f : α → β) (s : set α) (y: β) : y ∈ f '' s = ∃ x, x ∈ s ∧ f x = y := rfl @[simp] theorem mem_image (f : α → β) (s : set α) (y : β) : y ∈ f '' s ↔ ∃ x, x ∈ s ∧ f x = y := iff.rfl lemma image_eta (f : α → β) : f '' s = (λ x, f x) '' s := rfl theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a := ⟨_, h, rfl⟩ theorem _root_.function.injective.mem_set_image {f : α → β} (hf : injective f) {s : set α} {a : α} : f a ∈ f '' s ↔ a ∈ s := ⟨λ ⟨b, hb, eq⟩, (hf eq) ▸ hb, mem_image_of_mem f⟩ theorem ball_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∀ y ∈ f '' s, p y) ↔ (∀ x ∈ s, p (f x)) := by simp theorem ball_image_of_ball {f : α → β} {s : set α} {p : β → Prop} (h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f '' s, p y := ball_image_iff.2 h theorem bex_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∃ y ∈ f '' s, p y) ↔ (∃ x ∈ s, p (f x)) := by simp theorem mem_image_elim {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) : ∀{y : β}, y ∈ f '' s → C y | ._ ⟨a, a_in, rfl⟩ := h a a_in theorem mem_image_elim_on {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f '' s) (h : ∀ (x : α), x ∈ s → C (f x)) : C y := mem_image_elim h h_y @[congr] lemma image_congr {f g : α → β} {s : set α} (h : ∀a∈s, f a = g a) : f '' s = g '' s := by safe [ext_iff, iff_def] /-- A common special case of `image_congr` -/ lemma image_congr' {f g : α → β} {s : set α} (h : ∀ (x : α), f x = g x) : f '' s = g '' s := image_congr (λx _, h x) theorem image_comp (f : β → γ) (g : α → β) (a : set α) : (f ∘ g) '' a = f '' (g '' a) := subset.antisymm (ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha) (ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha) /-- A variant of `image_comp`, useful for rewriting -/ lemma image_image (g : β → γ) (f : α → β) (s : set α) : g '' (f '' s) = (λ x, g (f x)) '' s := (image_comp g f s).symm /-- Image is monotone with respect to `⊆`. See `set.monotone_image` for the statement in terms of `≤`. -/ theorem image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b := by finish [subset_def, mem_image_eq] theorem image_union (f : α → β) (s t : set α) : f '' (s ∪ t) = f '' s ∪ f '' t := ext $ λ x, ⟨by rintro ⟨a, h|h, rfl⟩; [left, right]; exact ⟨_, h, rfl⟩, by rintro (⟨a, h, rfl⟩ | ⟨a, h, rfl⟩); refine ⟨_, _, rfl⟩; [left, right]; exact h⟩ @[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := by { ext, simp } lemma image_inter_subset (f : α → β) (s t : set α) : f '' (s ∩ t) ⊆ f '' s ∩ f '' t := subset_inter (image_subset _ $ inter_subset_left _ _) (image_subset _ $ inter_subset_right _ _) theorem image_inter_on {f : α → β} {s t : set α} (h : ∀x∈t, ∀y∈s, f x = f y → x = y) : f '' s ∩ f '' t = f '' (s ∩ t) := subset.antisymm (assume b ⟨⟨a₁, ha₁, h₁⟩, ⟨a₂, ha₂, h₂⟩⟩, have a₂ = a₁, from h _ ha₂ _ ha₁ (by simp *), ⟨a₁, ⟨ha₁, this ▸ ha₂⟩, h₁⟩) (image_inter_subset _ _ _) theorem image_inter {f : α → β} {s t : set α} (H : injective f) : f '' s ∩ f '' t = f '' (s ∩ t) := image_inter_on (assume x _ y _ h, H h) theorem image_univ_of_surjective {ι : Type*} {f : ι → β} (H : surjective f) : f '' univ = univ := eq_univ_of_forall $ by { simpa [image] } @[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} := by { ext, simp [image, eq_comm] } @[simp] theorem nonempty.image_const {s : set α} (hs : s.nonempty) (a : β) : (λ _, a) '' s = {a} := ext $ λ x, ⟨λ ⟨y, _, h⟩, h ▸ mem_singleton _, λ h, (eq_of_mem_singleton h).symm ▸ hs.imp (λ y hy, ⟨hy, rfl⟩)⟩ @[simp] lemma image_eq_empty {α β} {f : α → β} {s : set α} : f '' s = ∅ ↔ s = ∅ := by { simp only [eq_empty_iff_forall_not_mem], exact ⟨λ H a ha, H _ ⟨_, ha, rfl⟩, λ H b ⟨_, ha, _⟩, H _ ha⟩ } -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_compl_image (t : set α) (S : set (set α)) : t ∈ compl '' S ↔ tᶜ ∈ S := begin suffices : ∀ x, xᶜ = t ↔ tᶜ = x, { simp [this] }, intro x, split; { intro e, subst e, simp } end /-- A variant of `image_id` -/ @[simp] lemma image_id' (s : set α) : (λx, x) '' s = s := by { ext, simp } theorem image_id (s : set α) : id '' s = s := by simp theorem compl_compl_image (S : set (set α)) : compl '' (compl '' S) = S := by rw [← image_comp, compl_comp_compl, image_id] theorem image_insert_eq {f : α → β} {a : α} {s : set α} : f '' (insert a s) = insert (f a) (f '' s) := by { ext, simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] } theorem image_pair (f : α → β) (a b : α) : f '' {a, b} = {f a, f b} := by simp only [image_insert_eq, image_singleton] theorem image_subset_preimage_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set α) : f '' s ⊆ g ⁻¹' s := λ b ⟨a, h, e⟩, e ▸ ((I a).symm ▸ h : g (f a) ∈ s) theorem preimage_subset_image_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set β) : f ⁻¹' s ⊆ g '' s := λ b h, ⟨f b, h, I b⟩ theorem image_eq_preimage_of_inverse {f : α → β} {g : β → α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : image f = preimage g := funext $ λ s, subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse h₂ s) theorem mem_image_iff_of_inverse {f : α → β} {g : β → α} {b : β} {s : set α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : b ∈ f '' s ↔ g b ∈ s := by rw image_eq_preimage_of_inverse h₁ h₂; refl theorem image_compl_subset {f : α → β} {s : set α} (H : injective f) : f '' sᶜ ⊆ (f '' s)ᶜ := subset_compl_iff_disjoint.2 $ by simp [image_inter H] theorem subset_image_compl {f : α → β} {s : set α} (H : surjective f) : (f '' s)ᶜ ⊆ f '' sᶜ := compl_subset_iff_union.2 $ by { rw ← image_union, simp [image_univ_of_surjective H] } theorem image_compl_eq {f : α → β} {s : set α} (H : bijective f) : f '' sᶜ = (f '' s)ᶜ := subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2) theorem subset_image_diff (f : α → β) (s t : set α) : f '' s \ f '' t ⊆ f '' (s \ t) := begin rw [diff_subset_iff, ← image_union, union_diff_self], exact image_subset f (subset_union_right t s) end theorem image_diff {f : α → β} (hf : injective f) (s t : set α) : f '' (s \ t) = f '' s \ f '' t := subset.antisymm (subset.trans (image_inter_subset _ _ _) $ inter_subset_inter_right _ $ image_compl_subset hf) (subset_image_diff f s t) lemma nonempty.image (f : α → β) {s : set α} : s.nonempty → (f '' s).nonempty | ⟨x, hx⟩ := ⟨f x, mem_image_of_mem f hx⟩ lemma nonempty.of_image {f : α → β} {s : set α} : (f '' s).nonempty → s.nonempty | ⟨y, x, hx, _⟩ := ⟨x, hx⟩ @[simp] lemma nonempty_image_iff {f : α → β} {s : set α} : (f '' s).nonempty ↔ s.nonempty := ⟨nonempty.of_image, λ h, h.image f⟩ lemma nonempty.preimage {s : set β} (hs : s.nonempty) {f : α → β} (hf : surjective f) : (f ⁻¹' s).nonempty := let ⟨y, hy⟩ := hs, ⟨x, hx⟩ := hf y in ⟨x, mem_preimage.2 $ hx.symm ▸ hy⟩ instance (f : α → β) (s : set α) [nonempty s] : nonempty (f '' s) := (set.nonempty.image f nonempty_of_nonempty_subtype).to_subtype /-- image and preimage are a Galois connection -/ @[simp] theorem image_subset_iff {s : set α} {t : set β} {f : α → β} : f '' s ⊆ t ↔ s ⊆ f ⁻¹' t := ball_image_iff theorem image_preimage_subset (f : α → β) (s : set β) : f '' (f ⁻¹' s) ⊆ s := image_subset_iff.2 (subset.refl _) theorem subset_preimage_image (f : α → β) (s : set α) : s ⊆ f ⁻¹' (f '' s) := λ x, mem_image_of_mem f theorem preimage_image_eq {f : α → β} (s : set α) (h : injective f) : f ⁻¹' (f '' s) = s := subset.antisymm (λ x ⟨y, hy, e⟩, h e ▸ hy) (subset_preimage_image f s) theorem image_preimage_eq {f : α → β} (s : set β) (h : surjective f) : f '' (f ⁻¹' s) = s := subset.antisymm (image_preimage_subset f s) (λ x hx, let ⟨y, e⟩ := h x in ⟨y, (e.symm ▸ hx : f y ∈ s), e⟩) lemma preimage_eq_preimage {f : β → α} (hf : surjective f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := iff.intro (assume eq, by rw [← image_preimage_eq s hf, ← image_preimage_eq t hf, eq]) (assume eq, eq ▸ rfl) lemma image_inter_preimage (f : α → β) (s : set α) (t : set β) : f '' (s ∩ f ⁻¹' t) = f '' s ∩ t := begin apply subset.antisymm, { calc f '' (s ∩ f ⁻¹' t) ⊆ f '' s ∩ (f '' (f⁻¹' t)) : image_inter_subset _ _ _ ... ⊆ f '' s ∩ t : inter_subset_inter_right _ (image_preimage_subset f t) }, { rintros _ ⟨⟨x, h', rfl⟩, h⟩, exact ⟨x, ⟨h', h⟩, rfl⟩ } end lemma image_preimage_inter (f : α → β) (s : set α) (t : set β) : f '' (f ⁻¹' t ∩ s) = t ∩ f '' s := by simp only [inter_comm, image_inter_preimage] @[simp] lemma image_inter_nonempty_iff {f : α → β} {s : set α} {t : set β} : (f '' s ∩ t).nonempty ↔ (s ∩ f ⁻¹' t).nonempty := by rw [←image_inter_preimage, nonempty_image_iff] lemma image_diff_preimage {f : α → β} {s : set α} {t : set β} : f '' (s \ f ⁻¹' t) = f '' s \ t := by simp_rw [diff_eq, ← preimage_compl, image_inter_preimage] theorem compl_image : image (compl : set α → set α) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {p : set α → Prop} : compl '' {s | p s} = {s | p sᶜ} := congr_fun compl_image p theorem inter_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) := λ x h, ⟨mem_image_of_mem _ h.left, h.right⟩ theorem union_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) := λ x h, or.elim h (λ l, or.inl $ mem_image_of_mem _ l) (λ r, or.inr r) theorem subset_image_union (f : α → β) (s : set α) (t : set β) : f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t := image_subset_iff.2 (union_preimage_subset _ _ _) lemma preimage_subset_iff {A : set α} {B : set β} {f : α → β} : f⁻¹' B ⊆ A ↔ (∀ a : α, f a ∈ B → a ∈ A) := iff.rfl lemma image_eq_image {f : α → β} (hf : injective f) : f '' s = f '' t ↔ s = t := iff.symm $ iff.intro (assume eq, eq ▸ rfl) $ assume eq, by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq] lemma image_subset_image_iff {f : α → β} (hf : injective f) : f '' s ⊆ f '' t ↔ s ⊆ t := begin refine (iff.symm $ iff.intro (image_subset f) $ assume h, _), rw [← preimage_image_eq s hf, ← preimage_image_eq t hf], exact preimage_mono h end lemma prod_quotient_preimage_eq_image [s : setoid α] (g : quotient s → β) {h : α → β} (Hh : h = g ∘ quotient.mk) (r : set (β × β)) : {x : quotient s × quotient s | (g x.1, g x.2) ∈ r} = (λ a : α × α, (⟦a.1⟧, ⟦a.2⟧)) '' ((λ a : α × α, (h a.1, h a.2)) ⁻¹' r) := Hh.symm ▸ set.ext (λ ⟨a₁, a₂⟩, ⟨quotient.induction_on₂ a₁ a₂ (λ a₁ a₂ h, ⟨(a₁, a₂), h, rfl⟩), λ ⟨⟨b₁, b₂⟩, h₁, h₂⟩, show (g a₁, g a₂) ∈ r, from have h₃ : ⟦b₁⟧ = a₁ ∧ ⟦b₂⟧ = a₂ := prod.ext_iff.1 h₂, h₃.1 ▸ h₃.2 ▸ h₁⟩) /-- Restriction of `f` to `s` factors through `s.image_factorization f : s → f '' s`. -/ def image_factorization (f : α → β) (s : set α) : s → f '' s := λ p, ⟨f p.1, mem_image_of_mem f p.2⟩ lemma image_factorization_eq {f : α → β} {s : set α} : subtype.val ∘ image_factorization f s = f ∘ subtype.val := funext $ λ p, rfl lemma surjective_onto_image {f : α → β} {s : set α} : surjective (image_factorization f s) := λ ⟨_, ⟨a, ha, rfl⟩⟩, ⟨⟨a, ha⟩, rfl⟩ end image /-! ### Subsingleton -/ /-- A set `s` is a `subsingleton`, if it has at most one element. -/ protected def subsingleton (s : set α) : Prop := ∀ ⦃x⦄ (hx : x ∈ s) ⦃y⦄ (hy : y ∈ s), x = y lemma subsingleton.mono (ht : t.subsingleton) (hst : s ⊆ t) : s.subsingleton := λ x hx y hy, ht (hst hx) (hst hy) lemma subsingleton.image (hs : s.subsingleton) (f : α → β) : (f '' s).subsingleton := λ _ ⟨x, hx, Hx⟩ _ ⟨y, hy, Hy⟩, Hx ▸ Hy ▸ congr_arg f (hs hx hy) lemma subsingleton.eq_singleton_of_mem (hs : s.subsingleton) {x:α} (hx : x ∈ s) : s = {x} := ext $ λ y, ⟨λ hy, (hs hx hy) ▸ mem_singleton _, λ hy, (eq_of_mem_singleton hy).symm ▸ hx⟩ @[simp] lemma subsingleton_empty : (∅ : set α).subsingleton := λ x, false.elim @[simp] lemma subsingleton_singleton {a} : ({a} : set α).subsingleton := λ x hx y hy, (eq_of_mem_singleton hx).symm ▸ (eq_of_mem_singleton hy).symm ▸ rfl lemma subsingleton_iff_singleton {x} (hx : x ∈ s) : s.subsingleton ↔ s = {x} := ⟨λ h, h.eq_singleton_of_mem hx, λ h,h.symm ▸ subsingleton_singleton⟩ lemma subsingleton.eq_empty_or_singleton (hs : s.subsingleton) : s = ∅ ∨ ∃ x, s = {x} := s.eq_empty_or_nonempty.elim or.inl (λ ⟨x, hx⟩, or.inr ⟨x, hs.eq_singleton_of_mem hx⟩) lemma subsingleton.induction_on {p : set α → Prop} (hs : s.subsingleton) (he : p ∅) (h₁ : ∀ x, p {x}) : p s := by { rcases hs.eq_empty_or_singleton with rfl|⟨x, rfl⟩, exacts [he, h₁ _] } lemma subsingleton_univ [subsingleton α] : (univ : set α).subsingleton := λ x hx y hy, subsingleton.elim x y lemma subsingleton_of_subsingleton [subsingleton α] {s : set α} : set.subsingleton s := subsingleton.mono subsingleton_univ (subset_univ s) lemma subsingleton_is_top (α : Type*) [partial_order α] : set.subsingleton {x : α | is_top x} := λ x hx y hy, hx.unique (hy x) lemma subsingleton_is_bot (α : Type*) [partial_order α] : set.subsingleton {x : α | is_bot x} := λ x hx y hy, hx.unique (hy x) /-- `s`, coerced to a type, is a subsingleton type if and only if `s` is a subsingleton set. -/ @[simp, norm_cast] lemma subsingleton_coe (s : set α) : subsingleton s ↔ s.subsingleton := begin split, { refine λ h, (λ a ha b hb, _), exact set_coe.ext_iff.2 (@subsingleton.elim s h ⟨a, ha⟩ ⟨b, hb⟩) }, { exact λ h, subsingleton.intro (λ a b, set_coe.ext (h a.property b.property)) } end /-- The preimage of a subsingleton under an injective map is a subsingleton. -/ theorem subsingleton.preimage {s : set β} (hs : s.subsingleton) {f : α → β} (hf : function.injective f) : (f ⁻¹' s).subsingleton := λ a ha b hb, hf $ hs ha hb /-- `s` is a subsingleton, if its image of an injective function is. -/ theorem subsingleton_of_image {α β : Type*} {f : α → β} (hf : function.injective f) (s : set α) (hs : (f '' s).subsingleton) : s.subsingleton := (hs.preimage hf).mono $ subset_preimage_image _ _ theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) /-! ### Lemmas about range of a function. -/ section range variables {f : ι → α} open function /-- Range of a function. This function is more flexible than `f '' univ`, as the image requires that the domain is in Type and not an arbitrary Sort. -/ def range (f : ι → α) : set α := {x | ∃y, f y = x} @[simp] theorem mem_range {x : α} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl @[simp] theorem mem_range_self (i : ι) : f i ∈ range f := ⟨i, rfl⟩ theorem forall_range_iff {p : α → Prop} : (∀ a ∈ range f, p a) ↔ (∀ i, p (f i)) := by simp theorem forall_subtype_range_iff {p : range f → Prop} : (∀ a : range f, p a) ↔ ∀ i, p ⟨f i, mem_range_self _⟩ := ⟨λ H i, H _, λ H ⟨y, i, hi⟩, by { subst hi, apply H }⟩ theorem exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ (∃ i, p (f i)) := by simp lemma exists_range_iff' {p : α → Prop} : (∃ a, a ∈ range f ∧ p a) ↔ ∃ i, p (f i) := by simpa only [exists_prop] using exists_range_iff lemma exists_subtype_range_iff {p : range f → Prop} : (∃ a : range f, p a) ↔ ∃ i, p ⟨f i, mem_range_self _⟩ := ⟨λ ⟨⟨a, i, hi⟩, ha⟩, by { subst a, exact ⟨i, ha⟩}, λ ⟨i, hi⟩, ⟨_, hi⟩⟩ theorem range_iff_surjective : range f = univ ↔ surjective f := eq_univ_iff_forall alias range_iff_surjective ↔ _ function.surjective.range_eq @[simp] theorem range_id : range (@id α) = univ := range_iff_surjective.2 surjective_id @[simp] theorem _root_.prod.range_fst [nonempty β] : range (prod.fst : α × β → α) = univ := prod.fst_surjective.range_eq @[simp] theorem _root_.prod.range_snd [nonempty α] : range (prod.snd : α × β → β) = univ := prod.snd_surjective.range_eq @[simp] theorem range_eval {ι : Type*} {α : ι → Sort*} [Π i, nonempty (α i)] (i : ι) : range (eval i : (Π i, α i) → α i) = univ := (surjective_eval i).range_eq theorem is_compl_range_inl_range_inr : is_compl (range $ @sum.inl α β) (range sum.inr) := ⟨by { rintro y ⟨⟨x₁, rfl⟩, ⟨x₂, _⟩⟩, cc }, by { rintro (x|y) -; [left, right]; exact mem_range_self _ }⟩ @[simp] theorem range_inl_union_range_inr : range (sum.inl : α → α ⊕ β) ∪ range sum.inr = univ := is_compl_range_inl_range_inr.sup_eq_top @[simp] theorem range_inl_inter_range_inr : range (sum.inl : α → α ⊕ β) ∩ range sum.inr = ∅ := is_compl_range_inl_range_inr.inf_eq_bot @[simp] theorem range_inr_union_range_inl : range (sum.inr : β → α ⊕ β) ∪ range sum.inl = univ := is_compl_range_inl_range_inr.symm.sup_eq_top @[simp] theorem range_inr_inter_range_inl : range (sum.inr : β → α ⊕ β) ∩ range sum.inl = ∅ := is_compl_range_inl_range_inr.symm.inf_eq_bot @[simp] theorem preimage_inl_range_inr : sum.inl ⁻¹' range (sum.inr : β → α ⊕ β) = ∅ := by { ext, simp } @[simp] theorem preimage_inr_range_inl : sum.inr ⁻¹' range (sum.inl : α → α ⊕ β) = ∅ := by { ext, simp } @[simp] theorem range_quot_mk (r : α → α → Prop) : range (quot.mk r) = univ := (surjective_quot_mk r).range_eq @[simp] theorem image_univ {f : α → β} : f '' univ = range f := by { ext, simp [image, range] } theorem image_subset_range (f : α → β) (s) : f '' s ⊆ range f := by rw ← image_univ; exact image_subset _ (subset_univ _) theorem mem_range_of_mem_image (f : α → β) (s) {x : β} (h : x ∈ f '' s) : x ∈ range f := mem_of_mem_of_subset h $ image_subset_range f s theorem range_comp (g : α → β) (f : ι → α) : range (g ∘ f) = g '' range f := subset.antisymm (forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _)) (ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self) theorem range_subset_iff : range f ⊆ s ↔ ∀ y, f y ∈ s := forall_range_iff lemma range_comp_subset_range (f : α → β) (g : β → γ) : range (g ∘ f) ⊆ range g := by rw range_comp; apply image_subset_range lemma range_nonempty_iff_nonempty : (range f).nonempty ↔ nonempty ι := ⟨λ ⟨y, x, hxy⟩, ⟨x⟩, λ ⟨x⟩, ⟨f x, mem_range_self x⟩⟩ lemma range_nonempty [h : nonempty ι] (f : ι → α) : (range f).nonempty := range_nonempty_iff_nonempty.2 h @[simp] lemma range_eq_empty_iff {f : ι → α} : range f = ∅ ↔ is_empty ι := by rw [← not_nonempty_iff, ← range_nonempty_iff_nonempty, not_nonempty_iff_eq_empty] lemma range_eq_empty [is_empty ι] (f : ι → α) : range f = ∅ := range_eq_empty_iff.2 ‹_› instance [nonempty ι] (f : ι → α) : nonempty (range f) := (range_nonempty f).to_subtype @[simp] lemma image_union_image_compl_eq_range (f : α → β) : (f '' s) ∪ (f '' sᶜ) = range f := by rw [← image_union, ← image_univ, ← union_compl_self] theorem image_preimage_eq_inter_range {f : α → β} {t : set β} : f '' (f ⁻¹' t) = t ∩ range f := ext $ assume x, ⟨assume ⟨x, hx, heq⟩, heq ▸ ⟨hx, mem_range_self _⟩, assume ⟨hx, ⟨y, h_eq⟩⟩, h_eq ▸ mem_image_of_mem f $ show y ∈ f ⁻¹' t, by simp [preimage, h_eq, hx]⟩ lemma image_preimage_eq_of_subset {f : α → β} {s : set β} (hs : s ⊆ range f) : f '' (f ⁻¹' s) = s := by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs] instance set.can_lift [can_lift α β] : can_lift (set α) (set β) := { coe := λ s, can_lift.coe '' s, cond := λ s, ∀ x ∈ s, can_lift.cond β x, prf := λ s hs, ⟨can_lift.coe ⁻¹' s, image_preimage_eq_of_subset $ λ x hx, can_lift.prf _ (hs x hx)⟩ } lemma image_preimage_eq_iff {f : α → β} {s : set β} : f '' (f ⁻¹' s) = s ↔ s ⊆ range f := ⟨by { intro h, rw [← h], apply image_subset_range }, image_preimage_eq_of_subset⟩ lemma preimage_subset_preimage_iff {s t : set α} {f : β → α} (hs : s ⊆ range f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := begin split, { intros h x hx, rcases hs hx with ⟨y, rfl⟩, exact h hx }, intros h x, apply h end lemma preimage_eq_preimage' {s t : set α} {f : β → α} (hs : s ⊆ range f) (ht : t ⊆ range f) : f ⁻¹' s = f ⁻¹' t ↔ s = t := begin split, { intro h, apply subset.antisymm, rw [←preimage_subset_preimage_iff hs, h], rw [←preimage_subset_preimage_iff ht, h] }, rintro rfl, refl end @[simp] theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := set.ext $ λ x, and_iff_left ⟨x, rfl⟩ @[simp] theorem preimage_range_inter {f : α → β} {s : set β} : f ⁻¹' (range f ∩ s) = f ⁻¹' s := by rw [inter_comm, preimage_inter_range] theorem preimage_image_preimage {f : α → β} {s : set β} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := by rw [image_preimage_eq_inter_range, preimage_inter_range] @[simp] theorem quot_mk_range_eq [setoid α] : range (λx : α, ⟦x⟧) = univ := range_iff_surjective.2 quot.exists_rep lemma range_const_subset {c : α} : range (λx:ι, c) ⊆ {c} := range_subset_iff.2 $ λ x, rfl @[simp] lemma range_const : ∀ [nonempty ι] {c : α}, range (λx:ι, c) = {c} | ⟨x⟩ c := subset.antisymm range_const_subset $ assume y hy, (mem_singleton_iff.1 hy).symm ▸ mem_range_self x lemma diagonal_eq_range {α : Type*} : diagonal α = range (λ x, (x, x)) := by { ext ⟨x, y⟩, simp [diagonal, eq_comm] } theorem preimage_singleton_nonempty {f : α → β} {y : β} : (f ⁻¹' {y}).nonempty ↔ y ∈ range f := iff.rfl theorem preimage_singleton_eq_empty {f : α → β} {y : β} : f ⁻¹' {y} = ∅ ↔ y ∉ range f := not_nonempty_iff_eq_empty.symm.trans $ not_congr preimage_singleton_nonempty lemma range_subset_singleton {f : ι → α} {x : α} : range f ⊆ {x} ↔ f = const ι x := by simp [range_subset_iff, funext_iff, mem_singleton] lemma image_compl_preimage {f : α → β} {s : set β} : f '' ((f ⁻¹' s)ᶜ) = range f \ s := by rw [compl_eq_univ_diff, image_diff_preimage, image_univ] @[simp] theorem range_sigma_mk {β : α → Type*} (a : α) : range (sigma.mk a : β a → Σ a, β a) = sigma.fst ⁻¹' {a} := begin apply subset.antisymm, { rintros _ ⟨b, rfl⟩, simp }, { rintros ⟨x, y⟩ (rfl|_), exact mem_range_self y } end /-- Any map `f : ι → β` factors through a map `range_factorization f : ι → range f`. -/ def range_factorization (f : ι → β) : ι → range f := λ i, ⟨f i, mem_range_self i⟩ lemma range_factorization_eq {f : ι → β} : subtype.val ∘ range_factorization f = f := funext $ λ i, rfl @[simp] lemma range_factorization_coe (f : ι → β) (a : ι) : (range_factorization f a : β) = f a := rfl lemma surjective_onto_range : surjective (range_factorization f) := λ ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩ lemma image_eq_range (f : α → β) (s : set α) : f '' s = range (λ(x : s), f x) := by { ext, split, rintro ⟨x, h1, h2⟩, exact ⟨⟨x, h1⟩, h2⟩, rintro ⟨⟨x, h1⟩, h2⟩, exact ⟨x, h1, h2⟩ } @[simp] lemma sum.elim_range {α β γ : Type*} (f : α → γ) (g : β → γ) : range (sum.elim f g) = range f ∪ range g := by simp [set.ext_iff, mem_range] lemma range_ite_subset' {p : Prop} [decidable p] {f g : α → β} : range (if p then f else g) ⊆ range f ∪ range g := begin by_cases h : p, {rw if_pos h, exact subset_union_left _ _}, {rw if_neg h, exact subset_union_right _ _} end lemma range_ite_subset {p : α → Prop} [decidable_pred p] {f g : α → β} : range (λ x, if p x then f x else g x) ⊆ range f ∪ range g := begin rw range_subset_iff, intro x, by_cases h : p x, simp [if_pos h, mem_union, mem_range_self], simp [if_neg h, mem_union, mem_range_self] end @[simp] lemma preimage_range (f : α → β) : f ⁻¹' (range f) = univ := eq_univ_of_forall mem_range_self /-- The range of a function from a `unique` type contains just the function applied to its single value. -/ lemma range_unique [h : unique ι] : range f = {f $ default ι} := begin ext x, rw mem_range, split, { rintros ⟨i, hi⟩, rw h.uniq i at hi, exact hi ▸ mem_singleton _ }, { exact λ h, ⟨default ι, h.symm⟩ } end lemma range_diff_image_subset (f : α → β) (s : set α) : range f \ f '' s ⊆ f '' sᶜ := λ y ⟨⟨x, h₁⟩, h₂⟩, ⟨x, λ h, h₂ ⟨x, h, h₁⟩, h₁⟩ lemma range_diff_image {f : α → β} (H : injective f) (s : set α) : range f \ f '' s = f '' sᶜ := subset.antisymm (range_diff_image_subset f s) $ λ y ⟨x, hx, hy⟩, hy ▸ ⟨mem_range_self _, λ ⟨x', hx', eq⟩, hx $ H eq ▸ hx'⟩ /-- We can use the axiom of choice to pick a preimage for every element of `range f`. -/ noncomputable def range_splitting (f : α → β) : range f → α := λ x, x.2.some -- This can not be a `@[simp]` lemma because the head of the left hand side is a variable. lemma apply_range_splitting (f : α → β) (x : range f) : f (range_splitting f x) = x := x.2.some_spec attribute [irreducible] range_splitting @[simp] lemma comp_range_splitting (f : α → β) : f ∘ range_splitting f = coe := by { ext, simp only [function.comp_app], apply apply_range_splitting, } -- When `f` is injective, see also `equiv.of_injective`. lemma left_inverse_range_splitting (f : α → β) : left_inverse (range_factorization f) (range_splitting f) := λ x, by { ext, simp only [range_factorization_coe], apply apply_range_splitting, } lemma range_splitting_injective (f : α → β) : injective (range_splitting f) := (left_inverse_range_splitting f).injective lemma is_compl_range_some_none (α : Type*) : is_compl (range (some : α → option α)) {none} := ⟨λ x ⟨⟨a, ha⟩, (hn : x = none)⟩, option.some_ne_none _ (ha.trans hn), λ x hx, option.cases_on x (or.inr rfl) (λ x, or.inl $ mem_range_self _)⟩ @[simp] lemma compl_range_some (α : Type*) : (range (some : α → option α))ᶜ = {none} := (is_compl_range_some_none α).compl_eq @[simp] lemma range_some_inter_none (α : Type*) : range (some : α → option α) ∩ {none} = ∅ := (is_compl_range_some_none α).inf_eq_bot @[simp] lemma range_some_union_none (α : Type*) : range (some : α → option α) ∪ {none} = univ := (is_compl_range_some_none α).sup_eq_top end range /-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/ def pairwise_on (s : set α) (r : α → α → Prop) := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → r x y lemma pairwise_on_of_forall (s : set α) (p : α → α → Prop) (h : ∀ (a b : α), p a b) : pairwise_on s p := λ a _ b _ _, h a b lemma pairwise_on.imp_on {s : set α} {p q : α → α → Prop} (h : pairwise_on s p) (hpq : pairwise_on s (λ ⦃a b : α⦄, p a b → q a b)) : pairwise_on s q := λ a ha b hb hab, hpq a ha b hb hab (h a ha b hb hab) lemma pairwise_on.imp {s : set α} {p q : α → α → Prop} (h : pairwise_on s p) (hpq : ∀ ⦃a b : α⦄, p a b → q a b) : pairwise_on s q := h.imp_on (pairwise_on_of_forall s _ hpq) theorem pairwise_on.mono {s t : set α} {r} (h : t ⊆ s) (hp : pairwise_on s r) : pairwise_on t r := λ x xt y yt, hp x (h xt) y (h yt) theorem pairwise_on.mono' {s : set α} {r r' : α → α → Prop} (H : r ≤ r') (hp : pairwise_on s r) : pairwise_on s r' := hp.imp H theorem pairwise_on_top (s : set α) : pairwise_on s ⊤ := pairwise_on_of_forall s _ (λ a b, trivial) protected lemma subsingleton.pairwise_on (h : s.subsingleton) (r : α → α → Prop) : pairwise_on s r := λ x hx y hy hne, (hne (h hx hy)).elim @[simp] lemma pairwise_on_empty (r : α → α → Prop) : (∅ : set α).pairwise_on r := subsingleton_empty.pairwise_on r @[simp] lemma pairwise_on_singleton (a : α) (r : α → α → Prop) : pairwise_on {a} r := subsingleton_singleton.pairwise_on r theorem nonempty.pairwise_on_iff_exists_forall {s : set α} (hs : s.nonempty) {f : α → β} {r : β → β → Prop} [is_equiv β r] : (pairwise_on s (r on f)) ↔ ∃ z, ∀ x ∈ s, r (f x) z := begin fsplit, { rcases hs with ⟨y, hy⟩, refine λ H, ⟨f y, λ x hx, _⟩, rcases eq_or_ne x y with rfl|hne, { apply is_refl.refl }, { exact H _ hx _ hy hne } }, { rintro ⟨z, hz⟩ x hx y hy hne, exact @is_trans.trans β r _ (f x) z (f y) (hz _ hx) (is_symm.symm _ _ $ hz _ hy) } end /-- For a nonempty set `s`, a function `f` takes pairwise equal values on `s` if and only if for some `z` in the codomain, `f` takes value `z` on all `x ∈ s`. See also `set.pairwise_on_eq_iff_exists_eq` for a version that assumes `[nonempty β]` instead of `set.nonempty s`. -/ theorem nonempty.pairwise_on_eq_iff_exists_eq {s : set α} (hs : s.nonempty) {f : α → β} : (pairwise_on s (λ x y, f x = f y)) ↔ ∃ z, ∀ x ∈ s, f x = z := hs.pairwise_on_iff_exists_forall lemma pairwise_on_iff_exists_forall [nonempty β] (s : set α) (f : α → β) {r : β → β → Prop} [is_equiv β r] : (pairwise_on s (r on f)) ↔ ∃ z, ∀ x ∈ s, r (f x) z := begin rcases s.eq_empty_or_nonempty with rfl|hne, { simp }, { exact hne.pairwise_on_iff_exists_forall } end /-- A function `f : α → β` with nonempty codomain takes pairwise equal values on a set `s` if and only if for some `z` in the codomain, `f` takes value `z` on all `x ∈ s`. See also `set.nonempty.pairwise_on_eq_iff_exists_eq` for a version that assumes `set.nonempty s` instead of `[nonempty β]`. -/ lemma pairwise_on_eq_iff_exists_eq [nonempty β] (s : set α) (f : α → β) : (pairwise_on s (λ x y, f x = f y)) ↔ ∃ z, ∀ x ∈ s, f x = z := pairwise_on_iff_exists_forall s f lemma pairwise_on_union {α} {s t : set α} {r : α → α → Prop} : (s ∪ t).pairwise_on r ↔ s.pairwise_on r ∧ t.pairwise_on r ∧ ∀ (a ∈ s) (b ∈ t), a ≠ b → r a b ∧ r b a := begin simp only [pairwise_on, mem_union_eq, or_imp_distrib, forall_and_distrib], exact ⟨λ H, ⟨H.1.1, H.2.2, H.2.1, λ x hx y hy hne, H.1.2 y hy x hx hne.symm⟩, λ H, ⟨⟨H.1, λ x hx y hy hne, H.2.2.2 y hy x hx hne.symm⟩, H.2.2.1, H.2.1⟩⟩ end lemma pairwise_on_union_of_symmetric {α} {s t : set α} {r : α → α → Prop} (hr : symmetric r) : (s ∪ t).pairwise_on r ↔ s.pairwise_on r ∧ t.pairwise_on r ∧ ∀ (a ∈ s) (b ∈ t), a ≠ b → r a b := pairwise_on_union.trans $ by simp only [hr.iff, and_self] lemma pairwise_on_insert {α} {s : set α} {a : α} {r : α → α → Prop} : (insert a s).pairwise_on r ↔ s.pairwise_on r ∧ ∀ b ∈ s, a ≠ b → r a b ∧ r b a := by simp only [insert_eq, pairwise_on_union, pairwise_on_singleton, true_and, mem_singleton_iff, forall_eq] lemma pairwise_on_insert_of_symmetric {α} {s : set α} {a : α} {r : α → α → Prop} (hr : symmetric r) : (insert a s).pairwise_on r ↔ s.pairwise_on r ∧ ∀ b ∈ s, a ≠ b → r a b := by simp only [pairwise_on_insert, hr.iff a, and_self] lemma pairwise_on_pair {r : α → α → Prop} {x y : α} : pairwise_on {x, y} r ↔ (x ≠ y → r x y ∧ r y x) := by simp [pairwise_on_insert] lemma pairwise_on_pair_of_symmetric {r : α → α → Prop} {x y : α} (hr : symmetric r) : pairwise_on {x, y} r ↔ (x ≠ y → r x y) := by simp [pairwise_on_insert_of_symmetric hr] end set open set namespace function variables {ι : Sort*} {α : Type*} {β : Type*} {f : α → β} lemma surjective.preimage_injective (hf : surjective f) : injective (preimage f) := assume s t, (preimage_eq_preimage hf).1 lemma injective.preimage_image (hf : injective f) (s : set α) : f ⁻¹' (f '' s) = s := preimage_image_eq s hf lemma injective.preimage_surjective (hf : injective f) : surjective (preimage f) := by { intro s, use f '' s, rw hf.preimage_image } lemma injective.subsingleton_image_iff (hf : injective f) {s : set α} : (f '' s).subsingleton ↔ s.subsingleton := ⟨subsingleton_of_image hf s, λ h, h.image f⟩ lemma surjective.image_preimage (hf : surjective f) (s : set β) : f '' (f ⁻¹' s) = s := image_preimage_eq s hf lemma surjective.image_surjective (hf : surjective f) : surjective (image f) := by { intro s, use f ⁻¹' s, rw hf.image_preimage } lemma surjective.nonempty_preimage (hf : surjective f) {s : set β} : (f ⁻¹' s).nonempty ↔ s.nonempty := by rw [← nonempty_image_iff, hf.image_preimage] lemma injective.image_injective (hf : injective f) : injective (image f) := by { intros s t h, rw [←preimage_image_eq s hf, ←preimage_image_eq t hf, h] } lemma surjective.preimage_subset_preimage_iff {s t : set β} (hf : surjective f) : f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t := by { apply preimage_subset_preimage_iff, rw [hf.range_eq], apply subset_univ } lemma surjective.range_comp {ι' : Sort*} {f : ι → ι'} (hf : surjective f) (g : ι' → α) : range (g ∘ f) = range g := ext $ λ y, (@surjective.exists _ _ _ hf (λ x, g x = y)).symm lemma injective.nonempty_apply_iff {f : set α → set β} (hf : injective f) (h2 : f ∅ = ∅) {s : set α} : (f s).nonempty ↔ s.nonempty := by rw [← ne_empty_iff_nonempty, ← h2, ← ne_empty_iff_nonempty, hf.ne_iff] lemma injective.mem_range_iff_exists_unique (hf : injective f) {b : β} : b ∈ range f ↔ ∃! a, f a = b := ⟨λ ⟨a, h⟩, ⟨a, h, λ a' ha, hf (ha.trans h.symm)⟩, exists_unique.exists⟩ lemma injective.exists_unique_of_mem_range (hf : injective f) {b : β} (hb : b ∈ range f) : ∃! a, f a = b := hf.mem_range_iff_exists_unique.mp hb theorem injective.compl_image_eq (hf : injective f) (s : set α) : (f '' s)ᶜ = f '' sᶜ ∪ (range f)ᶜ := begin ext y, rcases em (y ∈ range f) with ⟨x, rfl⟩|hx, { simp [hf.eq_iff] }, { rw [mem_range, not_exists] at hx, simp [hx] } end lemma left_inverse.image_image {g : β → α} (h : left_inverse g f) (s : set α) : g '' (f '' s) = s := by rw [← image_comp, h.comp_eq_id, image_id] lemma left_inverse.preimage_preimage {g : β → α} (h : left_inverse g f) (s : set α) : f ⁻¹' (g ⁻¹' s) = s := by rw [← preimage_comp, h.comp_eq_id, preimage_id] end function open function /-! ### Image and preimage on subtypes -/ namespace subtype variable {α : Type*} lemma coe_image {p : α → Prop} {s : set (subtype p)} : coe '' s = {x | ∃h : p x, (⟨x, h⟩ : subtype p) ∈ s} := set.ext $ assume a, ⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq ▸ ⟨ha', in_s⟩, assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩ @[simp] lemma coe_image_of_subset {s t : set α} (h : t ⊆ s) : coe '' {x : ↥s | ↑x ∈ t} = t := begin ext x, rw set.mem_image, exact ⟨λ ⟨x', hx', hx⟩, hx ▸ hx', λ hx, ⟨⟨x, h hx⟩, hx, rfl⟩⟩, end lemma range_coe {s : set α} : range (coe : s → α) = s := by { rw ← set.image_univ, simp [-set.image_univ, coe_image] } /-- A variant of `range_coe`. Try to use `range_coe` if possible. This version is useful when defining a new type that is defined as the subtype of something. In that case, the coercion doesn't fire anymore. -/ lemma range_val {s : set α} : range (subtype.val : s → α) = s := range_coe /-- We make this the simp lemma instead of `range_coe`. The reason is that if we write for `s : set α` the function `coe : s → α`, then the inferred implicit arguments of `coe` are `coe α (λ x, x ∈ s)`. -/ @[simp] lemma range_coe_subtype {p : α → Prop} : range (coe : subtype p → α) = {x | p x} := range_coe @[simp] lemma coe_preimage_self (s : set α) : (coe : s → α) ⁻¹' s = univ := by rw [← preimage_range (coe : s → α), range_coe] lemma range_val_subtype {p : α → Prop} : range (subtype.val : subtype p → α) = {x | p x} := range_coe theorem coe_image_subset (s : set α) (t : set s) : coe '' t ⊆ s := λ x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property theorem coe_image_univ (s : set α) : (coe : s → α) '' set.univ = s := image_univ.trans range_coe @[simp] theorem image_preimage_coe (s t : set α) : (coe : s → α) '' (coe ⁻¹' t) = t ∩ s := image_preimage_eq_inter_range.trans $ congr_arg _ range_coe theorem image_preimage_val (s t : set α) : (subtype.val : s → α) '' (subtype.val ⁻¹' t) = t ∩ s := image_preimage_coe s t theorem preimage_coe_eq_preimage_coe_iff {s t u : set α} : ((coe : s → α) ⁻¹' t = coe ⁻¹' u) ↔ t ∩ s = u ∩ s := begin rw [←image_preimage_coe, ←image_preimage_coe], split, { intro h, rw h }, intro h, exact coe_injective.image_injective h end theorem preimage_val_eq_preimage_val_iff (s t u : set α) : ((subtype.val : s → α) ⁻¹' t = subtype.val ⁻¹' u) ↔ (t ∩ s = u ∩ s) := preimage_coe_eq_preimage_coe_iff lemma exists_set_subtype {t : set α} (p : set α → Prop) : (∃(s : set t), p (coe '' s)) ↔ ∃(s : set α), s ⊆ t ∧ p s := begin split, { rintro ⟨s, hs⟩, refine ⟨coe '' s, _, hs⟩, convert image_subset_range _ _, rw [range_coe] }, rintro ⟨s, hs₁, hs₂⟩, refine ⟨coe ⁻¹' s, _⟩, rw [image_preimage_eq_of_subset], exact hs₂, rw [range_coe], exact hs₁ end lemma preimage_coe_nonempty {s t : set α} : ((coe : s → α) ⁻¹' t).nonempty ↔ (s ∩ t).nonempty := by rw [inter_comm, ← image_preimage_coe, nonempty_image_iff] lemma preimage_coe_eq_empty {s t : set α} : (coe : s → α) ⁻¹' t = ∅ ↔ s ∩ t = ∅ := by simp only [← not_nonempty_iff_eq_empty, preimage_coe_nonempty] @[simp] lemma preimage_coe_compl (s : set α) : (coe : s → α) ⁻¹' sᶜ = ∅ := preimage_coe_eq_empty.2 (inter_compl_self s) @[simp] lemma preimage_coe_compl' (s : set α) : (coe : sᶜ → α) ⁻¹' s = ∅ := preimage_coe_eq_empty.2 (compl_inter_self s) end subtype namespace set /-! ### Lemmas about cartesian product of sets -/ section prod variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def prod (s : set α) (t : set β) : set (α × β) := {p | p.1 ∈ s ∧ p.2 ∈ t} lemma prod_eq (s : set α) (t : set β) : s.prod t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl theorem mem_prod_eq {p : α × β} : p ∈ s.prod t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] theorem mem_prod {p : α × β} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[simp] theorem prod_mk_mem_set_prod_eq {a : α} {b : β} : (a, b) ∈ s.prod t = (a ∈ s ∧ b ∈ t) := rfl lemma mk_mem_prod {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ s.prod t := ⟨a_in, b_in⟩ theorem prod_mono {s₁ s₂ : set α} {t₁ t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : s₁.prod t₁ ⊆ s₂.prod t₂ := assume x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩ lemma prod_subset_iff {P : set (α × β)} : (s.prod t ⊆ P) ↔ ∀ (x ∈ s) (y ∈ t), (x, y) ∈ P := ⟨λ h _ xin _ yin, h (mk_mem_prod xin yin), λ h ⟨_, _⟩ pin, h _ pin.1 _ pin.2⟩ lemma forall_prod_set {p : α × β → Prop} : (∀ x ∈ s.prod t, p x) ↔ ∀ (x ∈ s) (y ∈ t), p (x, y) := prod_subset_iff lemma exists_prod_set {p : α × β → Prop} : (∃ x ∈ s.prod t, p x) ↔ ∃ (x ∈ s) (y ∈ t), p (x, y) := by simp [and_assoc] @[simp] theorem prod_empty : s.prod ∅ = (∅ : set (α × β)) := by { ext, simp } @[simp] theorem empty_prod : set.prod ∅ t = (∅ : set (α × β)) := by { ext, simp } @[simp] theorem univ_prod_univ : (@univ α).prod (@univ β) = univ := by { ext ⟨x, y⟩, simp } lemma univ_prod {t : set β} : set.prod (univ : set α) t = prod.snd ⁻¹' t := by simp [prod_eq] lemma prod_univ {s : set α} : set.prod s (univ : set β) = prod.fst ⁻¹' s := by simp [prod_eq] @[simp] theorem singleton_prod {a : α} : set.prod {a} t = prod.mk a '' t := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } @[simp] theorem prod_singleton {b : β} : s.prod {b} = (λ a, (a, b)) '' s := by { ext ⟨x, y⟩, simp [and.left_comm, eq_comm] } theorem singleton_prod_singleton {a : α} {b : β} : set.prod {a} {b} = ({(a, b)} : set (α × β)) := by simp @[simp] theorem union_prod : (s₁ ∪ s₂).prod t = s₁.prod t ∪ s₂.prod t := by { ext ⟨x, y⟩, simp [or_and_distrib_right] } @[simp] theorem prod_union : s.prod (t₁ ∪ t₂) = s.prod t₁ ∪ s.prod t₂ := by { ext ⟨x, y⟩, simp [and_or_distrib_left] } theorem prod_inter_prod : s₁.prod t₁ ∩ s₂.prod t₂ = (s₁ ∩ s₂).prod (t₁ ∩ t₂) := by { ext ⟨x, y⟩, simp [and_assoc, and.left_comm] } theorem insert_prod {a : α} : (insert a s).prod t = (prod.mk a '' t) ∪ s.prod t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } theorem prod_insert {b : β} : s.prod (insert b t) = ((λa, (a, b)) '' s) ∪ s.prod t := by { ext ⟨x, y⟩, simp [image, iff_def, or_imp_distrib, imp.swap] {contextual := tt} } theorem prod_preimage_eq {f : γ → α} {g : δ → β} : (f ⁻¹' s).prod (g ⁻¹' t) = (λ p, (f p.1, g p.2)) ⁻¹' s.prod t := rfl lemma prod_preimage_left {f : γ → α} : (f ⁻¹' s).prod t = (λp, (f p.1, p.2)) ⁻¹' (s.prod t) := rfl lemma prod_preimage_right {g : δ → β} : s.prod (g ⁻¹' t) = (λp, (p.1, g p.2)) ⁻¹' (s.prod t) := rfl lemma preimage_prod_map_prod (f : α → β) (g : γ → δ) (s : set β) (t : set δ) : prod.map f g ⁻¹' (s.prod t) = (f ⁻¹' s).prod (g ⁻¹' t) := rfl lemma mk_preimage_prod (f : γ → α) (g : γ → β) : (λ x, (f x, g x)) ⁻¹' s.prod t = f ⁻¹' s ∩ g ⁻¹' t := rfl @[simp] lemma mk_preimage_prod_left {y : β} (h : y ∈ t) : (λ x, (x, y)) ⁻¹' s.prod t = s := by { ext x, simp [h] } @[simp] lemma mk_preimage_prod_right {x : α} (h : x ∈ s) : prod.mk x ⁻¹' s.prod t = t := by { ext y, simp [h] } @[simp] lemma mk_preimage_prod_left_eq_empty {y : β} (hy : y ∉ t) : (λ x, (x, y)) ⁻¹' s.prod t = ∅ := by { ext z, simp [hy] } @[simp] lemma mk_preimage_prod_right_eq_empty {x : α} (hx : x ∉ s) : prod.mk x ⁻¹' s.prod t = ∅ := by { ext z, simp [hx] } lemma mk_preimage_prod_left_eq_if {y : β} [decidable_pred (∈ t)] : (λ x, (x, y)) ⁻¹' s.prod t = if y ∈ t then s else ∅ := by { split_ifs; simp [h] } lemma mk_preimage_prod_right_eq_if {x : α} [decidable_pred (∈ s)] : prod.mk x ⁻¹' s.prod t = if x ∈ s then t else ∅ := by { split_ifs; simp [h] } lemma mk_preimage_prod_left_fn_eq_if {y : β} [decidable_pred (∈ t)] (f : γ → α) : (λ x, (f x, y)) ⁻¹' s.prod t = if y ∈ t then f ⁻¹' s else ∅ := by rw [← mk_preimage_prod_left_eq_if, prod_preimage_left, preimage_preimage] lemma mk_preimage_prod_right_fn_eq_if {x : α} [decidable_pred (∈ s)] (g : δ → β) : (λ y, (x, g y)) ⁻¹' s.prod t = if x ∈ s then g ⁻¹' t else ∅ := by rw [← mk_preimage_prod_right_eq_if, prod_preimage_right, preimage_preimage] theorem image_swap_eq_preimage_swap : image (@prod.swap α β) = preimage prod.swap := image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse theorem preimage_swap_prod {s : set α} {t : set β} : prod.swap ⁻¹' t.prod s = s.prod t := by { ext ⟨x, y⟩, simp [and_comm] } theorem image_swap_prod : prod.swap '' t.prod s = s.prod t := by rw [image_swap_eq_preimage_swap, preimage_swap_prod] theorem prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} : (m₁ '' s).prod (m₂ '' t) = image (λp:α×β, (m₁ p.1, m₂ p.2)) (s.prod t) := ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm] theorem prod_range_range_eq {α β γ δ} {m₁ : α → γ} {m₂ : β → δ} : (range m₁).prod (range m₂) = range (λp:α×β, (m₁ p.1, m₂ p.2)) := ext $ by simp [range] @[simp] theorem range_prod_map {α β γ δ} {m₁ : α → γ} {m₂ : β → δ} : range (prod.map m₁ m₂) = (range m₁).prod (range m₂) := prod_range_range_eq.symm theorem prod_range_univ_eq {α β γ} {m₁ : α → γ} : (range m₁).prod (univ : set β) = range (λp:α×β, (m₁ p.1, p.2)) := ext $ by simp [range] theorem prod_univ_range_eq {α β δ} {m₂ : β → δ} : (univ : set α).prod (range m₂) = range (λp:α×β, (p.1, m₂ p.2)) := ext $ by simp [range] lemma range_pair_subset {α β γ : Type*} (f : α → β) (g : α → γ) : range (λ x, (f x, g x)) ⊆ (range f).prod (range g) := have (λ x, (f x, g x)) = prod.map f g ∘ (λ x, (x, x)), from funext (λ x, rfl), by { rw [this, ← range_prod_map], apply range_comp_subset_range } theorem nonempty.prod : s.nonempty → t.nonempty → (s.prod t).nonempty | ⟨x, hx⟩ ⟨y, hy⟩ := ⟨(x, y), ⟨hx, hy⟩⟩ theorem nonempty.fst : (s.prod t).nonempty → s.nonempty | ⟨p, hp⟩ := ⟨p.1, hp.1⟩ theorem nonempty.snd : (s.prod t).nonempty → t.nonempty | ⟨p, hp⟩ := ⟨p.2, hp.2⟩ theorem prod_nonempty_iff : (s.prod t).nonempty ↔ s.nonempty ∧ t.nonempty := ⟨λ h, ⟨h.fst, h.snd⟩, λ h, nonempty.prod h.1 h.2⟩ theorem prod_eq_empty_iff : s.prod t = ∅ ↔ (s = ∅ ∨ t = ∅) := by simp only [not_nonempty_iff_eq_empty.symm, prod_nonempty_iff, not_and_distrib] lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} : s.prod t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W := by simp [subset_def] lemma fst_image_prod_subset (s : set α) (t : set β) : prod.fst '' (s.prod t) ⊆ s := λ _ h, let ⟨_, ⟨h₂, _⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂ lemma prod_subset_preimage_fst (s : set α) (t : set β) : s.prod t ⊆ prod.fst ⁻¹' s := image_subset_iff.1 (fst_image_prod_subset s t) lemma fst_image_prod (s : set β) {t : set α} (ht : t.nonempty) : prod.fst '' (s.prod t) = s := set.subset.antisymm (fst_image_prod_subset _ _) $ λ y y_in, let ⟨x, x_in⟩ := ht in ⟨(y, x), ⟨y_in, x_in⟩, rfl⟩ lemma snd_image_prod_subset (s : set α) (t : set β) : prod.snd '' (s.prod t) ⊆ t := λ _ h, let ⟨_, ⟨_, h₂⟩, h₁⟩ := (set.mem_image _ _ _).1 h in h₁ ▸ h₂ lemma prod_subset_preimage_snd (s : set α) (t : set β) : s.prod t ⊆ prod.snd ⁻¹' t := image_subset_iff.1 (snd_image_prod_subset s t) lemma snd_image_prod {s : set α} (hs : s.nonempty) (t : set β) : prod.snd '' (s.prod t) = t := set.subset.antisymm (snd_image_prod_subset _ _) $ λ y y_in, let ⟨x, x_in⟩ := hs in ⟨(x, y), ⟨x_in, y_in⟩, rfl⟩ lemma prod_diff_prod : s.prod t \ s₁.prod t₁ = s.prod (t \ t₁) ∪ (s \ s₁).prod t := by { ext x, by_cases h₁ : x.1 ∈ s₁; by_cases h₂ : x.2 ∈ t₁; simp * } /-- A product set is included in a product set if and only factors are included, or a factor of the first set is empty. -/ lemma prod_subset_prod_iff : (s.prod t ⊆ s₁.prod t₁) ↔ (s ⊆ s₁ ∧ t ⊆ t₁) ∨ (s = ∅) ∨ (t = ∅) := begin classical, cases (s.prod t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, { have st : s.nonempty ∧ t.nonempty, by rwa [prod_nonempty_iff] at h, split, { assume H : s.prod t ⊆ s₁.prod t₁, have h' : s₁.nonempty ∧ t₁.nonempty := prod_nonempty_iff.1 (h.mono H), refine or.inl ⟨_, _⟩, show s ⊆ s₁, { have := image_subset (prod.fst : α × β → α) H, rwa [fst_image_prod _ st.2, fst_image_prod _ h'.2] at this }, show t ⊆ t₁, { have := image_subset (prod.snd : α × β → β) H, rwa [snd_image_prod st.1, snd_image_prod h'.1] at this } }, { assume H, simp only [st.1.ne_empty, st.2.ne_empty, or_false] at H, exact prod_mono H.1 H.2 } } end end prod /-! ### Lemmas about set-indexed products of sets -/ section pi variables {ι : Type*} {α : ι → Type*} {s s₁ : set ι} {t t₁ t₂ : Π i, set (α i)} /-- Given an index set `ι` and a family of sets `t : Π i, set (α i)`, `pi s t` is the set of dependent functions `f : Πa, π a` such that `f a` belongs to `t a` whenever `a ∈ s`. -/ def pi (s : set ι) (t : Π i, set (α i)) : set (Π i, α i) := { f | ∀i ∈ s, f i ∈ t i } @[simp] lemma mem_pi {f : Π i, α i} : f ∈ s.pi t ↔ ∀ i ∈ s, f i ∈ t i := by refl @[simp] lemma mem_univ_pi {f : Π i, α i} : f ∈ pi univ t ↔ ∀ i, f i ∈ t i := by simp @[simp] lemma empty_pi (s : Π i, set (α i)) : pi ∅ s = univ := by { ext, simp [pi] } @[simp] lemma pi_univ (s : set ι) : pi s (λ i, (univ : set (α i))) = univ := eq_univ_of_forall $ λ f i hi, mem_univ _ lemma pi_mono (h : ∀ i ∈ s, t₁ i ⊆ t₂ i) : pi s t₁ ⊆ pi s t₂ := λ x hx i hi, (h i hi $ hx i hi) lemma pi_inter_distrib : s.pi (λ i, t i ∩ t₁ i) = s.pi t ∩ s.pi t₁ := ext $ λ x, by simp only [forall_and_distrib, mem_pi, mem_inter_eq] lemma pi_congr (h : s = s₁) (h' : ∀ i ∈ s, t i = t₁ i) : pi s t = pi s₁ t₁ := h ▸ (ext $ λ x, forall_congr $ λ i, forall_congr $ λ hi, h' i hi ▸ iff.rfl) lemma pi_eq_empty {i : ι} (hs : i ∈ s) (ht : t i = ∅) : s.pi t = ∅ := by { ext f, simp only [mem_empty_eq, not_forall, iff_false, mem_pi, not_imp], exact ⟨i, hs, by simp [ht]⟩ } lemma univ_pi_eq_empty {i : ι} (ht : t i = ∅) : pi univ t = ∅ := pi_eq_empty (mem_univ i) ht lemma pi_nonempty_iff : (s.pi t).nonempty ↔ ∀ i, ∃ x, i ∈ s → x ∈ t i := by simp [classical.skolem, set.nonempty] lemma univ_pi_nonempty_iff : (pi univ t).nonempty ↔ ∀ i, (t i).nonempty := by simp [classical.skolem, set.nonempty] lemma pi_eq_empty_iff : s.pi t = ∅ ↔ ∃ i, (α i → false) ∨ (i ∈ s ∧ t i = ∅) := begin rw [← not_nonempty_iff_eq_empty, pi_nonempty_iff], push_neg, apply exists_congr, intro i, split, { intro h, by_cases hα : nonempty (α i), { cases hα with x, refine or.inr ⟨(h x).1, by simp [eq_empty_iff_forall_not_mem, h]⟩ }, { exact or.inl (λ x, hα ⟨x⟩) }}, { rintro (h|h) x, exfalso, exact h x, simp [h] } end lemma univ_pi_eq_empty_iff : pi univ t = ∅ ↔ ∃ i, t i = ∅ := by simp [← not_nonempty_iff_eq_empty, univ_pi_nonempty_iff] @[simp] lemma univ_pi_empty [h : nonempty ι] : pi univ (λ i, ∅ : Π i, set (α i)) = ∅ := univ_pi_eq_empty_iff.2 $ h.elim $ λ x, ⟨x, rfl⟩ @[simp] lemma range_dcomp {β : ι → Type*} (f : Π i, α i → β i) : range (λ (g : Π i, α i), (λ i, f i (g i))) = pi univ (λ i, range (f i)) := begin apply subset.antisymm, { rintro _ ⟨x, rfl⟩ i -, exact ⟨x i, rfl⟩ }, { intros x hx, choose y hy using hx, exact ⟨λ i, y i trivial, funext $ λ i, hy i trivial⟩ } end @[simp] lemma insert_pi (i : ι) (s : set ι) (t : Π i, set (α i)) : pi (insert i s) t = (eval i ⁻¹' t i) ∩ pi s t := by { ext, simp [pi, or_imp_distrib, forall_and_distrib] } @[simp] lemma singleton_pi (i : ι) (t : Π i, set (α i)) : pi {i} t = (eval i ⁻¹' t i) := by { ext, simp [pi] } lemma singleton_pi' (i : ι) (t : Π i, set (α i)) : pi {i} t = {x | x i ∈ t i} := singleton_pi i t lemma pi_if {p : ι → Prop} [h : decidable_pred p] (s : set ι) (t₁ t₂ : Π i, set (α i)) : pi s (λ i, if p i then t₁ i else t₂ i) = pi {i ∈ s | p i} t₁ ∩ pi {i ∈ s | ¬ p i} t₂ := begin ext f, split, { assume h, split; { rintros i ⟨his, hpi⟩, simpa [*] using h i } }, { rintros ⟨ht₁, ht₂⟩ i his, by_cases p i; simp * at * } end lemma union_pi : (s ∪ s₁).pi t = s.pi t ∩ s₁.pi t := by simp [pi, or_imp_distrib, forall_and_distrib, set_of_and] @[simp] lemma pi_inter_compl (s : set ι) : pi s t ∩ pi sᶜ t = pi univ t := by rw [← union_pi, union_compl_self] lemma pi_update_of_not_mem [decidable_eq ι] {β : Π i, Type*} {i : ι} (hi : i ∉ s) (f : Π j, α j) (a : α i) (t : Π j, α j → set (β j)) : s.pi (λ j, t j (update f i a j)) = s.pi (λ j, t j (f j)) := pi_congr rfl $ λ j hj, by { rw update_noteq, exact λ h, hi (h ▸ hj) } lemma pi_update_of_mem [decidable_eq ι] {β : Π i, Type*} {i : ι} (hi : i ∈ s) (f : Π j, α j) (a : α i) (t : Π j, α j → set (β j)) : s.pi (λ j, t j (update f i a j)) = {x | x i ∈ t i a} ∩ (s \ {i}).pi (λ j, t j (f j)) := calc s.pi (λ j, t j (update f i a j)) = ({i} ∪ s \ {i}).pi (λ j, t j (update f i a j)) : by rw [union_diff_self, union_eq_self_of_subset_left (singleton_subset_iff.2 hi)] ... = {x | x i ∈ t i a} ∩ (s \ {i}).pi (λ j, t j (f j)) : by { rw [union_pi, singleton_pi', update_same, pi_update_of_not_mem], simp } lemma univ_pi_update [decidable_eq ι] {β : Π i, Type*} (i : ι) (f : Π j, α j) (a : α i) (t : Π j, α j → set (β j)) : pi univ (λ j, t j (update f i a j)) = {x | x i ∈ t i a} ∩ pi {i}ᶜ (λ j, t j (f j)) := by rw [compl_eq_univ_diff, ← pi_update_of_mem (mem_univ _)] lemma univ_pi_update_univ [decidable_eq ι] (i : ι) (s : set (α i)) : pi univ (update (λ j : ι, (univ : set (α j))) i s) = eval i ⁻¹' s := by rw [univ_pi_update i (λ j, (univ : set (α j))) s (λ j t, t), pi_univ, inter_univ, preimage] open_locale classical lemma eval_image_pi {i : ι} (hs : i ∈ s) (ht : (s.pi t).nonempty) : eval i '' s.pi t = t i := begin ext x, rcases ht with ⟨f, hf⟩, split, { rintro ⟨g, hg, rfl⟩, exact hg i hs }, { intro hg, refine ⟨update f i x, _, by simp⟩, intros j hj, by_cases hji : j = i, { subst hji, simp [hg] }, { rw [mem_pi] at hf, simp [hji, hf, hj] }}, end @[simp] lemma eval_image_univ_pi {i : ι} (ht : (pi univ t).nonempty) : (λ f : Π i, α i, f i) '' pi univ t = t i := eval_image_pi (mem_univ i) ht lemma eval_preimage {ι} {α : ι → Type*} {i : ι} {s : set (α i)} : eval i ⁻¹' s = pi univ (update (λ i, univ) i s) := by { ext x, simp [@forall_update_iff _ (λ i, set (α i)) _ _ _ _ (λ i' y, x i' ∈ y)] } lemma eval_preimage' {ι} {α : ι → Type*} {i : ι} {s : set (α i)} : eval i ⁻¹' s = pi {i} (update (λ i, univ) i s) := by { ext, simp } lemma update_preimage_pi {i : ι} {f : Π i, α i} (hi : i ∈ s) (hf : ∀ j ∈ s, j ≠ i → f j ∈ t j) : (update f i) ⁻¹' s.pi t = t i := begin ext x, split, { intro h, convert h i hi, simp }, { intros hx j hj, by_cases h : j = i, { cases h, simpa }, { rw [update_noteq h], exact hf j hj h }} end lemma update_preimage_univ_pi {i : ι} {f : Π i, α i} (hf : ∀ j ≠ i, f j ∈ t j) : (update f i) ⁻¹' pi univ t = t i := update_preimage_pi (mem_univ i) (λ j _, hf j) lemma subset_pi_eval_image (s : set ι) (u : set (Π i, α i)) : u ⊆ pi s (λ i, eval i '' u) := λ f hf i hi, ⟨f, hf, rfl⟩ lemma univ_pi_ite (s : set ι) (t : Π i, set (α i)) : pi univ (λ i, if i ∈ s then t i else univ) = s.pi t := by { ext, simp_rw [mem_univ_pi], apply forall_congr, intro i, split_ifs; simp [h] } end pi /-! ### Lemmas about `inclusion`, the injection of subtypes induced by `⊆` -/ section inclusion variable {α : Type*} /-- `inclusion` is the "identity" function between two subsets `s` and `t`, where `s ⊆ t` -/ def inclusion {s t : set α} (h : s ⊆ t) : s → t := λ x : s, (⟨x, h x.2⟩ : t) @[simp] lemma inclusion_self {s : set α} (x : s) : inclusion (set.subset.refl _) x = x := by { cases x, refl } @[simp] lemma inclusion_right {s t : set α} (h : s ⊆ t) (x : t) (m : (x : α) ∈ s) : inclusion h ⟨x, m⟩ = x := by { cases x, refl } @[simp] lemma inclusion_inclusion {s t u : set α} (hst : s ⊆ t) (htu : t ⊆ u) (x : s) : inclusion htu (inclusion hst x) = inclusion (set.subset.trans hst htu) x := by { cases x, refl } @[simp] lemma coe_inclusion {s t : set α} (h : s ⊆ t) (x : s) : (inclusion h x : α) = (x : α) := rfl lemma inclusion_injective {s t : set α} (h : s ⊆ t) : function.injective (inclusion h) | ⟨_, _⟩ ⟨_, _⟩ := subtype.ext_iff_val.2 ∘ subtype.ext_iff_val.1 @[simp] lemma range_inclusion {s t : set α} (h : s ⊆ t) : range (inclusion h) = {x : t | (x:α) ∈ s} := by { ext ⟨x, hx⟩, simp [inclusion] } lemma eq_of_inclusion_surjective {s t : set α} {h : s ⊆ t} (h_surj : function.surjective (inclusion h)) : s = t := begin rw [← range_iff_surjective, range_inclusion, eq_univ_iff_forall] at h_surj, exact set.subset.antisymm h (λ x hx, h_surj ⟨x, hx⟩) end end inclusion /-! ### Injectivity and surjectivity lemmas for image and preimage -/ section image_preimage variables {α : Type u} {β : Type v} {f : α → β} @[simp] lemma preimage_injective : injective (preimage f) ↔ surjective f := begin refine ⟨λ h y, _, surjective.preimage_injective⟩, obtain ⟨x, hx⟩ : (f ⁻¹' {y}).nonempty, { rw [h.nonempty_apply_iff preimage_empty], apply singleton_nonempty }, exact ⟨x, hx⟩ end @[simp] lemma preimage_surjective : surjective (preimage f) ↔ injective f := begin refine ⟨λ h x x' hx, _, injective.preimage_surjective⟩, cases h {x} with s hs, have := mem_singleton x, rwa [← hs, mem_preimage, hx, ← mem_preimage, hs, mem_singleton_iff, eq_comm] at this end @[simp] lemma image_surjective : surjective (image f) ↔ surjective f := begin refine ⟨λ h y, _, surjective.image_surjective⟩, cases h {y} with s hs, have := mem_singleton y, rw [← hs] at this, rcases this with ⟨x, h1x, h2x⟩, exact ⟨x, h2x⟩ end @[simp] lemma image_injective : injective (image f) ↔ injective f := begin refine ⟨λ h x x' hx, _, injective.image_injective⟩, rw [← singleton_eq_singleton_iff], apply h, rw [image_singleton, image_singleton, hx] end lemma preimage_eq_iff_eq_image {f : α → β} (hf : bijective f) {s t} : f ⁻¹' s = t ↔ s = f '' t := by rw [← image_eq_image hf.1, hf.2.image_preimage] lemma eq_preimage_iff_image_eq {f : α → β} (hf : bijective f) {s t} : s = f ⁻¹' t ↔ f '' s = t := by rw [← image_eq_image hf.1, hf.2.image_preimage] end image_preimage /-! ### Lemmas about images of binary and ternary functions -/ section n_ary_image variables {α β γ δ ε : Type*} {f f' : α → β → γ} {g g' : α → β → γ → δ} variables {s s' : set α} {t t' : set β} {u u' : set γ} {a a' : α} {b b' : β} {c c' : γ} {d d' : δ} /-- The image of a binary function `f : α → β → γ` as a function `set α → set β → set γ`. Mathematically this should be thought of as the image of the corresponding function `α × β → γ`. -/ def image2 (f : α → β → γ) (s : set α) (t : set β) : set γ := {c | ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c } lemma mem_image2_eq : c ∈ image2 f s t = ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := rfl @[simp] lemma mem_image2 : c ∈ image2 f s t ↔ ∃ a b, a ∈ s ∧ b ∈ t ∧ f a b = c := iff.rfl lemma mem_image2_of_mem (h1 : a ∈ s) (h2 : b ∈ t) : f a b ∈ image2 f s t := ⟨a, b, h1, h2, rfl⟩ lemma mem_image2_iff (hf : injective2 f) : f a b ∈ image2 f s t ↔ a ∈ s ∧ b ∈ t := ⟨ by { rintro ⟨a', b', ha', hb', h⟩, rcases hf h with ⟨rfl, rfl⟩, exact ⟨ha', hb'⟩ }, λ ⟨ha, hb⟩, mem_image2_of_mem ha hb⟩ /-- image2 is monotone with respect to `⊆`. -/ lemma image2_subset (hs : s ⊆ s') (ht : t ⊆ t') : image2 f s t ⊆ image2 f s' t' := by { rintro _ ⟨a, b, ha, hb, rfl⟩, exact mem_image2_of_mem (hs ha) (ht hb) } lemma forall_image2_iff {p : γ → Prop} : (∀ z ∈ image2 f s t, p z) ↔ ∀ (x ∈ s) (y ∈ t), p (f x y) := ⟨λ h x hx y hy, h _ ⟨x, y, hx, hy, rfl⟩, λ h z ⟨x, y, hx, hy, hz⟩, hz ▸ h x hx y hy⟩ @[simp] lemma image2_subset_iff {u : set γ} : image2 f s t ⊆ u ↔ ∀ (x ∈ s) (y ∈ t), f x y ∈ u := forall_image2_iff lemma image2_union_left : image2 f (s ∪ s') t = image2 f s t ∪ image2 f s' t := begin ext c, split, { rintros ⟨a, b, h1a|h2a, hb, rfl⟩;[left, right]; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ }, { rintro (⟨_, _, _, _, rfl⟩|⟨_, _, _, _, rfl⟩); refine ⟨_, _, _, ‹_›, rfl⟩; simp [mem_union, *] } end lemma image2_union_right : image2 f s (t ∪ t') = image2 f s t ∪ image2 f s t' := begin ext c, split, { rintros ⟨a, b, ha, h1b|h2b, rfl⟩;[left, right]; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ }, { rintro (⟨_, _, _, _, rfl⟩|⟨_, _, _, _, rfl⟩); refine ⟨_, _, ‹_›, _, rfl⟩; simp [mem_union, *] } end @[simp] lemma image2_empty_left : image2 f ∅ t = ∅ := ext $ by simp @[simp] lemma image2_empty_right : image2 f s ∅ = ∅ := ext $ by simp lemma image2_inter_subset_left : image2 f (s ∩ s') t ⊆ image2 f s t ∩ image2 f s' t := by { rintro _ ⟨a, b, ⟨h1a, h2a⟩, hb, rfl⟩, split; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ } lemma image2_inter_subset_right : image2 f s (t ∩ t') ⊆ image2 f s t ∩ image2 f s t' := by { rintro _ ⟨a, b, ha, ⟨h1b, h2b⟩, rfl⟩, split; exact ⟨_, _, ‹_›, ‹_›, rfl⟩ } @[simp] lemma image2_singleton_left : image2 f {a} t = f a '' t := ext $ λ x, by simp @[simp] lemma image2_singleton_right : image2 f s {b} = (λ a, f a b) '' s := ext $ λ x, by simp lemma image2_singleton : image2 f {a} {b} = {f a b} := by simp @[congr] lemma image2_congr (h : ∀ (a ∈ s) (b ∈ t), f a b = f' a b) : image2 f s t = image2 f' s t := by { ext, split; rintro ⟨a, b, ha, hb, rfl⟩; refine ⟨a, b, ha, hb, by rw h a ha b hb⟩ } /-- A common special case of `image2_congr` -/ lemma image2_congr' (h : ∀ a b, f a b = f' a b) : image2 f s t = image2 f' s t := image2_congr (λ a _ b _, h a b) /-- The image of a ternary function `f : α → β → γ → δ` as a function `set α → set β → set γ → set δ`. Mathematically this should be thought of as the image of the corresponding function `α × β × γ → δ`. -/ def image3 (g : α → β → γ → δ) (s : set α) (t : set β) (u : set γ) : set δ := {d | ∃ a b c, a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d } @[simp] lemma mem_image3 : d ∈ image3 g s t u ↔ ∃ a b c, a ∈ s ∧ b ∈ t ∧ c ∈ u ∧ g a b c = d := iff.rfl @[congr] lemma image3_congr (h : ∀ (a ∈ s) (b ∈ t) (c ∈ u), g a b c = g' a b c) : image3 g s t u = image3 g' s t u := by { ext x, split; rintro ⟨a, b, c, ha, hb, hc, rfl⟩; exact ⟨a, b, c, ha, hb, hc, by rw h a ha b hb c hc⟩ } /-- A common special case of `image3_congr` -/ lemma image3_congr' (h : ∀ a b c, g a b c = g' a b c) : image3 g s t u = image3 g' s t u := image3_congr (λ a _ b _ c _, h a b c) lemma image2_image2_left (f : δ → γ → ε) (g : α → β → δ) : image2 f (image2 g s t) u = image3 (λ a b c, f (g a b) c) s t u := begin ext, split, { rintro ⟨_, c, ⟨a, b, ha, hb, rfl⟩, hc, rfl⟩, refine ⟨a, b, c, ha, hb, hc, rfl⟩ }, { rintro ⟨a, b, c, ha, hb, hc, rfl⟩, refine ⟨_, c, ⟨a, b, ha, hb, rfl⟩, hc, rfl⟩ } end lemma image2_image2_right (f : α → δ → ε) (g : β → γ → δ) : image2 f s (image2 g t u) = image3 (λ a b c, f a (g b c)) s t u := begin ext, split, { rintro ⟨a, _, ha, ⟨b, c, hb, hc, rfl⟩, rfl⟩, refine ⟨a, b, c, ha, hb, hc, rfl⟩ }, { rintro ⟨a, b, c, ha, hb, hc, rfl⟩, refine ⟨a, _, ha, ⟨b, c, hb, hc, rfl⟩, rfl⟩ } end lemma image2_assoc {ε'} {f : δ → γ → ε} {g : α → β → δ} {f' : α → ε' → ε} {g' : β → γ → ε'} (h_assoc : ∀ a b c, f (g a b) c = f' a (g' b c)) : image2 f (image2 g s t) u = image2 f' s (image2 g' t u) := by simp only [image2_image2_left, image2_image2_right, h_assoc] lemma image_image2 (f : α → β → γ) (g : γ → δ) : g '' image2 f s t = image2 (λ a b, g (f a b)) s t := begin ext, split, { rintro ⟨_, ⟨a, b, ha, hb, rfl⟩, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨_, ⟨a, b, ha, hb, rfl⟩, rfl⟩ } end lemma image2_image_left (f : γ → β → δ) (g : α → γ) : image2 f (g '' s) t = image2 (λ a b, f (g a) b) s t := begin ext, split, { rintro ⟨_, b, ⟨a, ha, rfl⟩, hb, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨_, b, ⟨a, ha, rfl⟩, hb, rfl⟩ } end lemma image2_image_right (f : α → γ → δ) (g : β → γ) : image2 f s (g '' t) = image2 (λ a b, f a (g b)) s t := begin ext, split, { rintro ⟨a, _, ha, ⟨b, hb, rfl⟩, rfl⟩, refine ⟨a, b, ha, hb, rfl⟩ }, { rintro ⟨a, b, ha, hb, rfl⟩, refine ⟨a, _, ha, ⟨b, hb, rfl⟩, rfl⟩ } end lemma image2_swap (f : α → β → γ) (s : set α) (t : set β) : image2 f s t = image2 (λ a b, f b a) t s := by { ext, split; rintro ⟨a, b, ha, hb, rfl⟩; refine ⟨b, a, hb, ha, rfl⟩ } @[simp] lemma image2_left (h : t.nonempty) : image2 (λ x y, x) s t = s := by simp [nonempty_def.mp h, ext_iff] @[simp] lemma image2_right (h : s.nonempty) : image2 (λ x y, y) s t = t := by simp [nonempty_def.mp h, ext_iff] @[simp] lemma image_prod (f : α → β → γ) : (λ x : α × β, f x.1 x.2) '' s.prod t = image2 f s t := set.ext $ λ a, ⟨ by { rintros ⟨_, _, rfl⟩, exact ⟨_, _, (mem_prod.mp ‹_›).1, (mem_prod.mp ‹_›).2, rfl⟩ }, by { rintros ⟨_, _, _, _, rfl⟩, exact ⟨(_, _), mem_prod.mpr ⟨‹_›, ‹_›⟩, rfl⟩ }⟩ lemma nonempty.image2 (hs : s.nonempty) (ht : t.nonempty) : (image2 f s t).nonempty := by { cases hs with a ha, cases ht with b hb, exact ⟨f a b, ⟨a, b, ha, hb, rfl⟩⟩ } end n_ary_image end set namespace subsingleton variables {α : Type*} [subsingleton α] lemma eq_univ_of_nonempty {s : set α} : s.nonempty → s = univ := λ ⟨x, hx⟩, eq_univ_of_forall $ λ y, subsingleton.elim x y ▸ hx @[elab_as_eliminator] lemma set_cases {p : set α → Prop} (h0 : p ∅) (h1 : p univ) (s) : p s := s.eq_empty_or_nonempty.elim (λ h, h.symm ▸ h0) $ λ h, (eq_univ_of_nonempty h).symm ▸ h1 lemma mem_iff_nonempty {α : Type*} [subsingleton α] {s : set α} {x : α} : x ∈ s ↔ s.nonempty := ⟨λ hx, ⟨x, hx⟩, λ ⟨y, hy⟩, subsingleton.elim y x ▸ hy⟩ end subsingleton
f21700ad45ef12ede18e42af4b592b4004e85237
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/lean-scheme-submission/src/spectrum_of_a_ring/strucutre_sheaf_stalks.lean
a5f54421478526cddf0558013ca29f1e8521b147
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
1,773
lean
/- Stalks of the structure sheaf are local rings. -/ import ring_theory.ideals import to_mathlib.localization.local_rings import to_mathlib.localization.localization_of_iso import to_mathlib.localization.localization_alt import spectrum_of_a_ring.structure_presheaf_stalks import spectrum_of_a_ring.structure_sheaf import spectrum_of_a_ring.structure_presheaf_stalks import sheaves.stalk_of_rings import sheaves.sheaf_of_rings_on_standard_basis universe u open localization_alt noncomputable theory variables {R : Type u} [comm_ring R] variables (P : Spec R) def O := structure_sheaf R def O_P := stalk_of_rings O.F P -- We know it is a ring. instance O_P.is_comm_ring : comm_ring (O_P P) := by simp [O_P]; by apply_instance -- Now we use what we proved about the stalks on basis and the fact -- that the extension doesn't change stalks to prove the property. lemma structure_sheaf.stalk_local : local_ring (O_P P) := begin have Hbij := to_stalk_extension.bijective (D_fs_standard_basis R) (structure_presheaf_on_basis R) P, haveI Hhom := to_stalk_extension.is_ring_hom (D_fs_standard_basis R) (structure_presheaf_on_basis R) P, haveI Hhom2 := strucutre_presheaf_stalks.φP.is_ring_hom P, have Hloc := strucutre_presheaf_stalks.stalk_local.localization P, have Hloc' := is_localization_data.of_iso (-P.1 : set R) (strucutre_presheaf_stalks.φ P) Hloc (to_stalk_extension _ _ P) Hbij Hhom, have : is_ring_hom (to_stalk_extension Bstd strucutre_presheaf_stalks.F P ∘ strucutre_presheaf_stalks.φ P), exact @is_ring_hom.comp _ _ _ _ (strucutre_presheaf_stalks.φ P) _ _ _ (to_stalk_extension _ _ P) Hhom, exact local_ring.of_is_localization_data_at_prime P.2 Hloc', end
28e0ad978c4bdc61ff542c992f5023736718e4f2
63abd62053d479eae5abf4951554e1064a4c45b4
/src/analysis/calculus/darboux.lean
afdccc1a2496c191ae57001580c615484838790e
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
4,784
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.calculus.mean_value /-! # Darboux's theorem In this file we prove that the derivative of a differentiable function on an interval takes all intermediate values. The proof is based on the [Wikipedia](https://en.wikipedia.org/wiki/Darboux%27s_theorem_(analysis)) page about this theorem. -/ open filter set open_locale topological_space classical variables {a b : ℝ} {f f' : ℝ → ℝ} /-- Darboux's theorem: if `a ≤ b` and `f' a < m < f' b`, then `f' c = m` for some `c ∈ [a, b]`. -/ theorem exists_has_deriv_within_at_eq_of_gt_of_lt (hab : a ≤ b) (hf : ∀ x ∈ (Icc a b), has_deriv_within_at f (f' x) (Icc a b) x) {m : ℝ} (hma : f' a < m) (hmb : m < f' b) : m ∈ f' '' (Icc a b) := begin have hab' : a < b, { refine lt_of_le_of_ne hab (λ hab', _), subst b, exact lt_asymm hma hmb }, set g : ℝ → ℝ := λ x, f x - m * x, have hg : ∀ x ∈ Icc a b, has_deriv_within_at g (f' x - m) (Icc a b) x, { intros x hx, simpa using (hf x hx).sub ((has_deriv_within_at_id x _).const_mul m) }, obtain ⟨c, cmem, hc⟩ : ∃ c ∈ Icc a b, is_min_on g (Icc a b) c, from compact_Icc.exists_forall_le (nonempty_Icc.2 $ hab) (λ x hx, (hg x hx).continuous_within_at), have cmem' : c ∈ Ioo a b, { cases eq_or_lt_of_le cmem.1 with hac hac, -- Show that `c` can't be equal to `a` { subst c, refine absurd (sub_nonneg.1 $ nonneg_of_mul_nonneg_left _ (sub_pos.2 hab')) (not_le_of_lt hma), have : b - a ∈ pos_tangent_cone_at (Icc a b) a, from mem_pos_tangent_cone_at_of_segment_subset (segment_eq_Icc hab ▸ subset.refl _), simpa [-sub_nonneg, -continuous_linear_map.map_sub] using hc.localize.has_fderiv_within_at_nonneg (hg a (left_mem_Icc.2 hab)) this }, cases eq_or_lt_of_le cmem.2 with hbc hbc, -- Show that `c` can't be equal to `a` { subst c, refine absurd (sub_nonpos.1 $ nonpos_of_mul_nonneg_right _ (sub_lt_zero.2 hab')) (not_le_of_lt hmb), have : a - b ∈ pos_tangent_cone_at (Icc a b) b, from mem_pos_tangent_cone_at_of_segment_subset (by rw [segment_symm, segment_eq_Icc hab]), simpa [-sub_nonneg, -continuous_linear_map.map_sub] using hc.localize.has_fderiv_within_at_nonneg (hg b (right_mem_Icc.2 hab)) this }, exact ⟨hac, hbc⟩ }, use [c, cmem], rw [← sub_eq_zero], have : Icc a b ∈ 𝓝 c, by rwa [← mem_interior_iff_mem_nhds, interior_Icc], exact (hc.is_local_min this).has_deriv_at_eq_zero ((hg c cmem).has_deriv_at this) end /-- Darboux's theorem: if `a ≤ b` and `f' a > m > f' b`, then `f' c = m` for some `c ∈ [a, b]`. -/ theorem exists_has_deriv_within_at_eq_of_lt_of_gt (hab : a ≤ b) (hf : ∀ x ∈ (Icc a b), has_deriv_within_at f (f' x) (Icc a b) x) {m : ℝ} (hma : m < f' a) (hmb : f' b < m) : m ∈ f' '' (Icc a b) := let ⟨c, cmem, hc⟩ := exists_has_deriv_within_at_eq_of_gt_of_lt hab (λ x hx, (hf x hx).neg) (neg_lt_neg hma) (neg_lt_neg hmb) in ⟨c, cmem, neg_injective hc⟩ /-- Darboux's theorem: the image of a convex set under `f'` is a convex set. -/ theorem convex_image_has_deriv_at {s : set ℝ} (hs : convex s) (hf : ∀ x ∈ s, has_deriv_at f (f' x) x) : convex (f' '' s) := begin refine real.convex_iff_ord_connected.2 _, rintros _ ⟨a, ha, rfl⟩ _ ⟨b, hb, rfl⟩ m ⟨hma, hmb⟩, cases eq_or_lt_of_le hma with hma hma, by exact hma ▸ mem_image_of_mem f' ha, cases eq_or_lt_of_le hmb with hmb hmb, by exact hmb.symm ▸ mem_image_of_mem f' hb, cases le_total a b with hab hab, { have : Icc a b ⊆ s, from hs.ord_connected ha hb, rcases exists_has_deriv_within_at_eq_of_gt_of_lt hab (λ x hx, (hf x $ this hx).has_deriv_within_at) hma hmb with ⟨c, cmem, hc⟩, exact ⟨c, this cmem, hc⟩ }, { have : Icc b a ⊆ s, from hs.ord_connected hb ha, rcases exists_has_deriv_within_at_eq_of_lt_of_gt hab (λ x hx, (hf x $ this hx).has_deriv_within_at) hmb hma with ⟨c, cmem, hc⟩, exact ⟨c, this cmem, hc⟩ } end /-- If the derivative of a function is never equal to `m`, then either it is always greater than `m`, or it is always less than `m`. -/ theorem deriv_forall_lt_or_forall_gt_of_forall_ne {s : set ℝ} (hs : convex s) (hf : ∀ x ∈ s, has_deriv_at f (f' x) x) {m : ℝ} (hf' : ∀ x ∈ s, f' x ≠ m) : (∀ x ∈ s, f' x < m) ∨ (∀ x ∈ s, m < f' x) := begin contrapose! hf', rcases hf' with ⟨⟨b, hb, hmb⟩, ⟨a, ha, hma⟩⟩, exact (convex_image_has_deriv_at hs hf).ord_connected (mem_image_of_mem f' ha) (mem_image_of_mem f' hb) ⟨hma, hmb⟩ end
fa54d28ff06b74a51b0eac8d216bb09df8f69ef2
f3849be5d845a1cb97680f0bbbe03b85518312f0
/library/data/bitvec.lean
89f23c41a1d638a222f63ead171d4785441ba85b
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,740
lean
/- Copyright (c) 2015 Joe Hendrix. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joe Hendrix, Sebastian Ullrich Basic operations on bitvectors. This is a work-in-progress, and contains additions to other theories. -/ import data.vector @[reducible] def bitvec (n : ℕ) := vector bool n namespace bitvec open nat open vector local infix `++ₜ`:65 := vector.append -- Create a zero bitvector @[reducible] protected def zero (n : ℕ) : bitvec n := repeat ff n -- Create a bitvector with the constant one. @[reducible] protected def one : Π (n : ℕ), bitvec n | 0 := [] | (succ n) := repeat ff n ++ₜ [tt] protected def cong {a b : ℕ} (h : a = b) : bitvec a → bitvec b | ⟨x, p⟩ := ⟨x, h ▸ p⟩ -- bitvec specific version of vector.append def append {m n} : bitvec m → bitvec n → bitvec (m + n) := vector.append section shift variable {n : ℕ} def shl (x : bitvec n) (i : ℕ) : bitvec n := bitvec.cong (by simp) $ dropn i x ++ₜ repeat ff (min n i) def fill_shr (x : bitvec n) (i : ℕ) (fill : bool) : bitvec n := bitvec.cong begin by_cases (i ≤ n), { intro h, note h₁ := sub_le n i, rw [min_eq_right h], rw [min_eq_left h₁, -nat.add_sub_assoc h, add_comm, nat.add_sub_cancel] }, { intro h, note h₁ := le_of_not_ge h, rw [min_eq_left h₁, sub_eq_zero_of_le h₁, min_zero_left, add_zero] } end $ repeat fill (min n i) ++ₜ taken (n-i) x -- unsigned shift right def ushr (x : bitvec n) (i : ℕ) : bitvec n := fill_shr x i ff -- signed shift right def sshr : Π {m : ℕ}, bitvec m → ℕ → bitvec m | 0 _ _ := [] | (succ m) x i := head x :: fill_shr (tail x) i (head x) end shift section bitwise variable {n : ℕ} def not : bitvec n → bitvec n := map bnot def and : bitvec n → bitvec n → bitvec n := map₂ band def or : bitvec n → bitvec n → bitvec n := map₂ bor def xor : bitvec n → bitvec n → bitvec n := map₂ bxor end bitwise section arith variable {n : ℕ} protected def xor3 (x y c : bool) := bxor (bxor x y) c protected def carry (x y c : bool) := x && y || x && c || y && c protected def neg (x : bitvec n) : bitvec n := let f := λ y c, (y || c, bxor y c) in prod.snd (map_accumr f x ff) -- Add with carry (no overflow) def adc (x y : bitvec n) (c : bool) : bitvec (n+1) := let f := λ x y c, (bitvec.carry x y c, bitvec.xor3 x y c) in let ⟨c, z⟩ := vector.map_accumr₂ f x y c in c :: z protected def add (x y : bitvec n) : bitvec n := tail (adc x y ff) protected def borrow (x y b : bool) := bnot x && y || bnot x && b || y && b -- Subtract with borrow def sbb (x y : bitvec n) (b : bool) : bool × bitvec n := let f := λ x y c, (bitvec.borrow x y c, bitvec.xor3 x y c) in vector.map_accumr₂ f x y b protected def sub (x y : bitvec n) : bitvec n := prod.snd (sbb x y ff) instance : has_zero (bitvec n) := ⟨bitvec.zero n⟩ instance : has_one (bitvec n) := ⟨bitvec.one n⟩ instance : has_add (bitvec n) := ⟨bitvec.add⟩ instance : has_sub (bitvec n) := ⟨bitvec.sub⟩ instance : has_neg (bitvec n) := ⟨bitvec.neg⟩ protected def mul (x y : bitvec n) : bitvec n := let f := λ r b, cond b (r + r + y) (r + r) in (to_list x).foldl f 0 instance : has_mul (bitvec n) := ⟨bitvec.mul⟩ end arith section comparison variable {n : ℕ} def uborrow (x y : bitvec n) : bool := prod.fst (sbb x y ff) def ult (x y : bitvec n) : Prop := uborrow x y def ugt (x y : bitvec n) : Prop := ult y x def ule (x y : bitvec n) : Prop := ¬ (ult y x) def uge (x y : bitvec n) : Prop := ule y x def sborrow : Π {n : ℕ}, bitvec n → bitvec n → bool | 0 _ _ := ff | (succ n) x y := match (head x, head y) with | (tt, ff) := tt | (ff, tt) := ff | _ := uborrow (tail x) (tail y) end def slt (x y : bitvec n) : Prop := sborrow x y def sgt (x y : bitvec n) : Prop := slt y x def sle (x y : bitvec n) : Prop := ¬ (slt y x) def sge (x y : bitvec n) : Prop := sle y x end comparison section conversion variable {α : Type} protected def of_nat : Π (n : ℕ), nat → bitvec n | 0 x := nil | (succ n) x := of_nat n (x / 2) ++ₜ [to_bool (x % 2 = 1)] protected def of_int : Π (n : ℕ), int → bitvec (succ n) | n (int.of_nat m) := ff :: bitvec.of_nat n m | n (int.neg_succ_of_nat m) := tt :: not (bitvec.of_nat n m) def add_lsb (r : ℕ) (b : bool) := r + r + cond b 1 0 def bits_to_nat (v : list bool) : nat := v.foldl add_lsb 0 protected def to_nat {n : nat} (v : bitvec n) : nat := bits_to_nat (to_list v) lemma bits_to_nat_to_list {n : ℕ} (x : bitvec n) : bitvec.to_nat x = bits_to_nat (vector.to_list x) := rfl theorem to_nat_append {m : ℕ} (xs : bitvec m) (b : bool) : bitvec.to_nat (xs ++ₜ[b]) = bitvec.to_nat xs * 2 + bitvec.to_nat [b] := begin cases xs with xs P, simp [bits_to_nat_to_list], clear P, unfold bits_to_nat list.foldl, -- the next 4 lines generalize the accumulator of foldl pose x := 0, change _ = add_lsb x b + _, generalize 0 y, revert x, simp, induction xs with x xs ; intro y, { simp, unfold list.foldl add_lsb, simp [nat.mul_succ] }, { simp, unfold list.foldl, apply ih_1 } end theorem bits_to_nat_to_bool (n : ℕ) : bitvec.to_nat [to_bool (n % 2 = 1)] = n % 2 := begin simp [bits_to_nat_to_list], unfold bits_to_nat add_lsb list.foldl cond, simp [cond_to_bool_mod_two], end lemma of_nat_succ {k n : ℕ} : bitvec.of_nat (succ k) n = bitvec.of_nat k (n / 2) ++ₜ[to_bool (n % 2 = 1)] := rfl theorem to_nat_of_nat {k n : ℕ} : bitvec.to_nat (bitvec.of_nat k n) = n % 2^k := begin revert n, induction k with k ; intro n, { unfold pow, simp [nat.mod_one], refl }, { assert h : 0 < 2, { apply le_succ }, rw [ of_nat_succ , to_nat_append , ih_1 , bits_to_nat_to_bool , mod_pow_succ h], ac_refl, } end protected def to_int : Π {n : nat}, bitvec n → int | 0 _ := 0 | (succ n) v := cond (head v) (int.neg_succ_of_nat $ bitvec.to_nat $ not $ tail v) (int.of_nat $ bitvec.to_nat $ tail v) end conversion private def to_string {n : nat} : bitvec n → string | ⟨bs, p⟩ := "0b" ++ (bs.reverse.map (λ b, if b then '1' else '0')) instance (n : nat) : has_to_string (bitvec n) := ⟨to_string⟩ end bitvec instance {n} {x y : bitvec n} : decidable (bitvec.ult x y) := bool.decidable_eq _ _ instance {n} {x y : bitvec n} : decidable (bitvec.ugt x y) := bool.decidable_eq _ _
f8a28e015439bbce87f815743170a1302be22e40
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/shapes/zero_auto.lean
0b6f1953e3b70362aafab9d147b8df60a5c0bddf
[]
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
16,706
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.shapes.terminal import Mathlib.category_theory.limits.shapes.binary_products import Mathlib.category_theory.limits.shapes.products import Mathlib.category_theory.limits.shapes.images import Mathlib.PostPort universes v u l u_1 v' u' namespace Mathlib /-! # Zero morphisms and zero objects A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space, and compositions of zero morphisms with anything give the zero morphism. (Notice this is extra structure, not merely a property.) A category "has a zero object" if it has an object which is both initial and terminal. Having a zero object provides zero morphisms, as the unique morphisms factoring through the zero object. ## References * https://en.wikipedia.org/wiki/Zero_morphism * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ namespace category_theory.limits /-- A category "has zero morphisms" if there is a designated "zero morphism" in each morphism space, and compositions of zero morphisms with anything give the zero morphism. -/ class has_zero_morphisms (C : Type u) [category C] where has_zero : (X Y : C) → HasZero (X ⟶ Y) comp_zero' : autoParam (∀ {X Y : C} (f : X ⟶ Y), C → f ≫ 0 = 0) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) zero_comp' : autoParam (C → ∀ {Y Z : C} (f : Y ⟶ Z), 0 ≫ f = 0) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.obviously") (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "obviously") []) theorem has_zero_morphisms.comp_zero {C : Type u} [category C] [c : has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (Z : C) : f ≫ 0 = 0 := sorry theorem has_zero_morphisms.zero_comp {C : Type u} [category C] [c : has_zero_morphisms C] (X : C) {Y : C} {Z : C} (f : Y ⟶ Z) : 0 ≫ f = 0 := sorry @[simp] theorem comp_zero {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {f : X ⟶ Y} {Z : C} : f ≫ 0 = 0 := has_zero_morphisms.comp_zero f Z @[simp] theorem zero_comp {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {Z : C} {f : Y ⟶ Z} : 0 ≫ f = 0 := has_zero_morphisms.zero_comp X f protected instance has_zero_morphisms_pempty : has_zero_morphisms (discrete pempty) := has_zero_morphisms.mk protected instance has_zero_morphisms_punit : has_zero_morphisms (discrete PUnit) := has_zero_morphisms.mk namespace has_zero_morphisms /-- This lemma will be immediately superseded by `ext`, below. -/ /-- If you're tempted to use this lemma "in the wild", you should probably carefully consider whether you've made a mistake in allowing two instances of `has_zero_morphisms` to exist at all. See, particularly, the note on `zero_morphisms_of_zero_object` below. -/ theorem ext {C : Type u} [category C] (I : has_zero_morphisms C) (J : has_zero_morphisms C) : I = J := sorry protected instance subsingleton {C : Type u} [category C] : subsingleton (has_zero_morphisms C) := subsingleton.intro ext end has_zero_morphisms theorem zero_of_comp_mono {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} (g : Y ⟶ Z) [mono g] (h : f ≫ g = 0) : f = 0 := eq.mp (Eq._oldrec (Eq.refl (f ≫ g = 0 ≫ g)) (propext (cancel_mono g))) (eq.mp (Eq._oldrec (Eq.refl (f ≫ g = 0)) (Eq.symm zero_comp)) h) theorem zero_of_epi_comp {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {Z : C} (f : X ⟶ Y) {g : Y ⟶ Z} [epi f] (h : f ≫ g = 0) : g = 0 := eq.mp (Eq._oldrec (Eq.refl (f ≫ g = f ≫ 0)) (propext (cancel_epi f))) (eq.mp (Eq._oldrec (Eq.refl (f ≫ g = 0)) (Eq.symm comp_zero)) h) theorem eq_zero_of_image_eq_zero {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (w : image.ι f = 0) : f = 0 := sorry theorem nonzero_image_of_nonzero {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {f : X ⟶ Y} [has_image f] (w : f ≠ 0) : image.ι f ≠ 0 := fun (h : image.ι f = 0) => w (eq_zero_of_image_eq_zero h) theorem equivalence_preserves_zero_morphisms {C : Type u} [category C] (D : Type u') [category D] [has_zero_morphisms C] [has_zero_morphisms D] (F : C ≌ D) (X : C) (Y : C) : functor.map (equivalence.functor F) 0 = 0 := sorry @[simp] theorem is_equivalence_preserves_zero_morphisms {C : Type u} [category C] (D : Type u') [category D] [has_zero_morphisms C] [has_zero_morphisms D] (F : C ⥤ D) [is_equivalence F] (X : C) (Y : C) : functor.map F 0 = 0 := sorry /-- A category "has a zero object" if it has an object which is both initial and terminal. -/ class has_zero_object (C : Type u) [category C] where zero : C unique_to : (X : C) → unique (zero ⟶ X) unique_from : (X : C) → unique (X ⟶ zero) protected instance has_zero_object_punit : has_zero_object (discrete PUnit) := has_zero_object.mk PUnit.unit (fun (X : discrete PUnit) => punit.cases_on X (unique.mk sorry sorry)) fun (X : discrete PUnit) => punit.cases_on X (unique.mk sorry sorry) namespace has_zero_object /-- Construct a `has_zero C` for a category with a zero object. This can not be a global instance as it will trigger for every `has_zero C` typeclass search. -/ protected def has_zero {C : Type u} [category C] [has_zero_object C] : HasZero C := { zero := zero } theorem to_zero_ext {C : Type u} [category C] [has_zero_object C] {X : C} (f : X ⟶ 0) (g : X ⟶ 0) : f = g := eq.mpr (id (Eq._oldrec (Eq.refl (f = g)) (unique.uniq (unique_from X) f))) (eq.mpr (id (Eq._oldrec (Eq.refl (Inhabited.default = g)) (unique.uniq (unique_from X) g))) (Eq.refl Inhabited.default)) theorem from_zero_ext {C : Type u} [category C] [has_zero_object C] {X : C} (f : 0 ⟶ X) (g : 0 ⟶ X) : f = g := eq.mpr (id (Eq._oldrec (Eq.refl (f = g)) (unique.uniq (unique_to X) f))) (eq.mpr (id (Eq._oldrec (Eq.refl (Inhabited.default = g)) (unique.uniq (unique_to X) g))) (Eq.refl Inhabited.default)) protected instance category_theory.iso.subsingleton {C : Type u} [category C] [has_zero_object C] (X : C) : subsingleton (X ≅ 0) := subsingleton.intro fun (a b : X ≅ 0) => iso.ext (of_as_true trivial) protected instance category_theory.mono {C : Type u} [category C] [has_zero_object C] {X : C} (f : 0 ⟶ X) : mono f := mono.mk fun (Z : C) (g h : Z ⟶ 0) (w : g ≫ f = h ≫ f) => to_zero_ext g h protected instance category_theory.epi {C : Type u} [category C] [has_zero_object C] {X : C} (f : X ⟶ 0) : epi f := epi.mk fun (Z : C) (g h : 0 ⟶ Z) (w : f ≫ g = f ≫ h) => from_zero_ext g h /-- A category with a zero object has zero morphisms. It is rarely a good idea to use this. Many categories that have a zero object have zero morphisms for some other reason, for example from additivity. Library code that uses `zero_morphisms_of_zero_object` will then be incompatible with these categories because the `has_zero_morphisms` instances will not be definitionally equal. For this reason library code should generally ask for an instance of `has_zero_morphisms` separately, even if it already asks for an instance of `has_zero_objects`. -/ def zero_morphisms_of_zero_object {C : Type u} [category C] [has_zero_object C] : has_zero_morphisms C := has_zero_morphisms.mk /-- A zero object is in particular initial. -/ theorem has_initial {C : Type u} [category C] [has_zero_object C] : has_initial C := has_initial_of_unique 0 /-- A zero object is in particular terminal. -/ theorem has_terminal {C : Type u} [category C] [has_zero_object C] : has_terminal C := has_terminal_of_unique 0 end has_zero_object @[simp] theorem id_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] : 𝟙 = 0 := has_zero_object.from_zero_ext 𝟙 0 /-- An arrow ending in the zero object is zero -/ -- This can't be a `simp` lemma because the left hand side would be a metavariable. theorem zero_of_to_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} (f : X ⟶ 0) : f = 0 := has_zero_object.to_zero_ext f 0 theorem zero_of_target_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : f = 0 := sorry /-- An arrow starting at the zero object is zero -/ theorem zero_of_from_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} (f : 0 ⟶ X) : f = 0 := has_zero_object.from_zero_ext f 0 theorem zero_of_source_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (i : X ≅ 0) : f = 0 := sorry theorem mono_of_source_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (i : X ≅ 0) : mono f := sorry theorem epi_of_target_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] {X : C} {Y : C} (f : X ⟶ Y) (i : Y ≅ 0) : epi f := sorry /-- An object `X` has `𝟙 X = 0` if and only if it is isomorphic to the zero object. Because `X ≅ 0` contains data (even if a subsingleton), we express this `↔` as an `≃`. -/ def id_zero_equiv_iso_zero {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] (X : C) : 𝟙 = 0 ≃ (X ≅ 0) := equiv.mk (fun (h : 𝟙 = 0) => iso.mk 0 0) sorry sorry sorry @[simp] theorem id_zero_equiv_iso_zero_apply_hom {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] (X : C) (h : 𝟙 = 0) : iso.hom (coe_fn (id_zero_equiv_iso_zero X) h) = 0 := rfl @[simp] theorem id_zero_equiv_iso_zero_apply_inv {C : Type u} [category C] [has_zero_object C] [has_zero_morphisms C] (X : C) (h : 𝟙 = 0) : iso.inv (coe_fn (id_zero_equiv_iso_zero X) h) = 0 := rfl /-- A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if the identities on both `X` and `Y` are zero. -/ def is_iso_zero_equiv {C : Type u} [category C] [has_zero_morphisms C] (X : C) (Y : C) : is_iso 0 ≃ 𝟙 = 0 ∧ 𝟙 = 0 := equiv.mk sorry (fun (h : 𝟙 = 0 ∧ 𝟙 = 0) => is_iso.mk 0) sorry sorry /-- A zero morphism `0 : X ⟶ X` is an isomorphism if and only if the identity on `X` is zero. -/ def is_iso_zero_self_equiv {C : Type u} [category C] [has_zero_morphisms C] (X : C) : is_iso 0 ≃ 𝟙 = 0 := eq.mpr sorry (eq.mp sorry (is_iso_zero_equiv X X)) /-- A zero morphism `0 : X ⟶ Y` is an isomorphism if and only if `X` and `Y` are isomorphic to the zero object. -/ def is_iso_zero_equiv_iso_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] (X : C) (Y : C) : is_iso 0 ≃ (X ≅ 0) × (Y ≅ 0) := equiv.trans (is_iso_zero_equiv X Y) (equiv.symm (equiv.mk sorry (fun (ᾰ : 𝟙 = 0 ∧ 𝟙 = 0) => and.dcases_on ᾰ fun (hX : 𝟙 = 0) (hY : 𝟙 = 0) => (coe_fn (id_zero_equiv_iso_zero X) hX, coe_fn (id_zero_equiv_iso_zero Y) hY)) sorry sorry)) /-- A zero morphism `0 : X ⟶ X` is an isomorphism if and only if `X` is isomorphic to the zero object. -/ def is_iso_zero_self_equiv_iso_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] (X : C) : is_iso 0 ≃ (X ≅ 0) := equiv.trans (is_iso_zero_equiv_iso_zero X X) subsingleton_prod_self_equiv /-- If there are zero morphisms, any initial object is a zero object. -/ protected instance has_zero_object_of_has_initial_object {C : Type u} [category C] [has_zero_morphisms C] [has_initial C] : has_zero_object C := has_zero_object.mk (⊥_C) (fun (X : C) => unique.mk { default := 0 } sorry) fun (X : C) => unique.mk { default := 0 } sorry /-- If there are zero morphisms, any terminal object is a zero object. -/ protected instance has_zero_object_of_has_terminal_object {C : Type u} [category C] [has_zero_morphisms C] [has_terminal C] : has_zero_object C := has_zero_object.mk (⊤_C) (fun (X : C) => unique.mk { default := 0 } sorry) fun (X : C) => unique.mk { default := 0 } sorry theorem image_ι_comp_eq_zero {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} {Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} [has_image f] [epi (factor_thru_image f)] (h : f ≫ g = 0) : image.ι f ≫ g = 0 := sorry /-- The zero morphism has a `mono_factorisation` through the zero object. -/ @[simp] theorem mono_factorisation_zero_e {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] (X : C) (Y : C) : mono_factorisation.e (mono_factorisation_zero X Y) = 0 := Eq.refl (mono_factorisation.e (mono_factorisation_zero X Y)) /-- The factorisation through the zero object is an image factorisation. -/ def image_factorisation_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] (X : C) (Y : C) : image_factorisation 0 := image_factorisation.mk (mono_factorisation_zero X Y) (is_image.mk fun (F' : mono_factorisation 0) => 0) protected instance has_image_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] {X : C} {Y : C} : has_image 0 := has_image.mk (image_factorisation_zero X Y) /-- The image of a zero morphism is the zero object. -/ def image_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] {X : C} {Y : C} : image 0 ≅ 0 := is_image.iso_ext (image.is_image 0) (image_factorisation.is_image (image_factorisation_zero X Y)) /-- The image of a morphism which is equal to zero is the zero object. -/ def image_zero' {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] {X : C} {Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] : image f ≅ 0 := image.eq_to_iso h ≪≫ image_zero @[simp] theorem image.ι_zero {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] {X : C} {Y : C} [has_image 0] : image.ι 0 = 0 := sorry /-- If we know `f = 0`, it requires a little work to conclude `image.ι f = 0`, because `f = g` only implies `image f ≅ image g`. -/ @[simp] theorem image.ι_zero' {C : Type u} [category C] [has_zero_morphisms C] [has_zero_object C] [has_equalizers C] {X : C} {Y : C} {f : X ⟶ Y} (h : f = 0) [has_image f] : image.ι f = 0 := sorry /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ protected instance split_mono_sigma_ι {C : Type u} [category C] {β : Type v} [DecidableEq β] [has_zero_morphisms C] (f : β → C) [has_colimit (discrete.functor f)] (b : β) : split_mono (sigma.ι f b) := split_mono.mk (sigma.desc fun (b' : β) => dite (b' = b) (fun (h : b' = b) => eq_to_hom (congr_arg f h)) fun (h : ¬b' = b) => 0) /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ protected instance split_epi_pi_π {C : Type u} [category C] {β : Type v} [DecidableEq β] [has_zero_morphisms C] (f : β → C) [has_limit (discrete.functor f)] (b : β) : split_epi (pi.π f b) := split_epi.mk (pi.lift fun (b' : β) => dite (b = b') (fun (h : b = b') => eq_to_hom (congr_arg f h)) fun (h : ¬b = b') => 0) /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ protected instance split_mono_coprod_inl {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} [has_colimit (pair X Y)] : split_mono coprod.inl := split_mono.mk (coprod.desc 𝟙 0) /-- In the presence of zero morphisms, coprojections into a coproduct are (split) monomorphisms. -/ protected instance split_mono_coprod_inr {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} [has_colimit (pair X Y)] : split_mono coprod.inr := split_mono.mk (coprod.desc 0 𝟙) /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ protected instance split_epi_prod_fst {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} [has_limit (pair X Y)] : split_epi prod.fst := split_epi.mk (prod.lift 𝟙 0) /-- In the presence of zero morphisms, projections into a product are (split) epimorphisms. -/ protected instance split_epi_prod_snd {C : Type u} [category C] [has_zero_morphisms C] {X : C} {Y : C} [has_limit (pair X Y)] : split_epi prod.snd := split_epi.mk (prod.lift 0 𝟙) end Mathlib
2eacb5006c6bd151868898d117438b29b82b1ec4
f083c4ed5d443659f3ed9b43b1ca5bb037ddeb58
/data/zmod.lean
25b0847163042276b47c4033db76296d2ea61da6
[ "Apache-2.0" ]
permissive
semorrison/mathlib
1be6f11086e0d24180fec4b9696d3ec58b439d10
20b4143976dad48e664c4847b75a85237dca0a89
refs/heads/master
1,583,799,212,170
1,535,634,130,000
1,535,730,505,000
129,076,205
0
0
Apache-2.0
1,551,697,998,000
1,523,442,265,000
Lean
UTF-8
Lean
false
false
9,570
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Chris Hughes -/ import data.int.modeq data.int.gcd data.fintype data.pnat open nat nat.modeq int def zmod (n : ℕ+) := fin n namespace zmod instance (n : ℕ+) : has_neg (zmod n) := ⟨λ a, ⟨nat_mod (-(a.1 : ℤ)) n, have h : (n : ℤ) ≠ 0 := int.coe_nat_ne_zero_iff_pos.2 n.pos, have h₁ : ((n : ℕ) : ℤ) = abs n := (abs_of_nonneg (int.coe_nat_nonneg n)).symm, by rw [← int.coe_nat_lt, nat_mod, to_nat_of_nonneg (int.mod_nonneg _ h), h₁]; exact int.mod_lt _ h⟩⟩ instance (n : ℕ+) : add_comm_semigroup (zmod n) := { add_assoc := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (show ((a + b) % n + c) ≡ (a + (b + c) % n) [MOD n], from calc ((a + b) % n + c) ≡ a + b + c [MOD n] : modeq_add (nat.mod_mod _ _) rfl ... ≡ a + (b + c) [MOD n] : by rw add_assoc ... ≡ (a + (b + c) % n) [MOD n] : modeq_add rfl (nat.mod_mod _ _).symm), add_comm := λ ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a + b) % n = (b + a) % n, by rw add_comm), ..fin.has_add } instance (n : ℕ+) : comm_semigroup (zmod n) := { mul_assoc := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (calc ((a * b) % n * c) ≡ a * b * c [MOD n] : modeq_mul (nat.mod_mod _ _) rfl ... ≡ a * (b * c) [MOD n] : by rw mul_assoc ... ≡ a * (b * c % n) [MOD n] : modeq_mul rfl (nat.mod_mod _ _).symm), mul_comm := λ ⟨a, _⟩ ⟨b, _⟩, fin.eq_of_veq (show (a * b) % n = (b * a) % n, by rw mul_comm), ..fin.has_mul } instance (n : ℕ+) : has_one (zmod n) := ⟨⟨(1 % n), nat.mod_lt _ n.pos⟩⟩ instance (n : ℕ+) : has_zero (zmod n) := ⟨⟨0, n.pos⟩⟩ instance zmod_one.subsingleton : subsingleton (zmod 1) := ⟨λ a b, fin.eq_of_veq (by rw [eq_zero_of_le_zero (le_of_lt_succ a.2), eq_zero_of_le_zero (le_of_lt_succ b.2)])⟩ lemma add_val {n : ℕ+} : ∀ a b : zmod n, (a + b).val = (a.val + b.val) % n | ⟨_, _⟩ ⟨_, _⟩ := rfl lemma mul_val {n : ℕ+} : ∀ a b : zmod n, (a * b).val = (a.val * b.val) % n | ⟨_, _⟩ ⟨_, _⟩ := rfl lemma one_val {n : ℕ+} : (1 : zmod n).val = 1 % n := rfl @[simp] lemma zero_val (n : ℕ+) : (0 : zmod n).val = 0 := rfl private lemma one_mul_aux (n : ℕ+) (a : zmod n) : (1 : zmod n) * a = a := begin cases n with n hn, cases n with n, { exact (lt_irrefl _ hn).elim }, { cases n with n, { exact @subsingleton.elim (zmod 1) _ _ _ }, { have h₁ : a.1 % n.succ.succ = a.1 := nat.mod_eq_of_lt a.2, have h₂ : 1 % n.succ.succ = 1 := nat.mod_eq_of_lt dec_trivial, refine fin.eq_of_veq _, simp [mul_val, one_val, h₁, h₂] } } end private lemma left_distrib_aux (n : ℕ+) : ∀ a b c : zmod n, a * (b + c) = a * b + a * c := λ ⟨a, ha⟩ ⟨b, hb⟩ ⟨c, hc⟩, fin.eq_of_veq (calc a * ((b + c) % n) ≡ a * (b + c) [MOD n] : modeq_mul rfl (nat.mod_mod _ _) ... ≡ a * b + a * c [MOD n] : by rw mul_add ... ≡ (a * b) % n + (a * c) % n [MOD n] : modeq_add (nat.mod_mod _ _).symm (nat.mod_mod _ _).symm) instance (n : ℕ+) : comm_ring (zmod n) := { zero_add := λ ⟨a, ha⟩, fin.eq_of_veq (show (0 + a) % n = a, by rw zero_add; exact nat.mod_eq_of_lt ha), add_zero := λ ⟨a, ha⟩, fin.eq_of_veq (nat.mod_eq_of_lt ha), add_left_neg := λ ⟨a, ha⟩, fin.eq_of_veq (show (((-a : ℤ) % n).to_nat + a) % n = 0, from int.coe_nat_inj begin have hn : (n : ℤ) ≠ 0 := (ne_of_lt (int.coe_nat_lt.2 n.pos)).symm, rw [int.coe_nat_mod, int.coe_nat_add, to_nat_of_nonneg (int.mod_nonneg _ hn), add_comm], simp, end), one_mul := one_mul_aux n, mul_one := λ a, by rw mul_comm; exact one_mul_aux n a, left_distrib := left_distrib_aux n, right_distrib := λ a b c, by rw [mul_comm, left_distrib_aux, mul_comm _ b, mul_comm]; refl, ..zmod.has_zero n, ..zmod.has_one n, ..zmod.has_neg n, ..zmod.add_comm_semigroup n, ..zmod.comm_semigroup n } lemma val_cast_nat {n : ℕ+} (a : ℕ) : (a : zmod n).val = a % n := begin induction a with a ih, { rw [nat.zero_mod]; refl }, { rw [succ_eq_add_one, nat.cast_add, add_val, ih], show (a % n + ((0 + (1 % n)) % n)) % n = (a + 1) % n, rw [zero_add, nat.mod_mod], exact nat.modeq.modeq_add (nat.mod_mod a n) (nat.mod_mod 1 n) } end lemma mk_eq_cast {n : ℕ+} {a : ℕ} (h : a < n) : (⟨a, h⟩ : zmod n) = (a : zmod n) := fin.eq_of_veq (by rw [val_cast_nat, nat.mod_eq_of_lt h]) @[simp] lemma cast_self_eq_zero {n : ℕ+} : ((n : ℕ) : zmod n) = 0 := fin.eq_of_veq (show (n : zmod n).val = 0, by simp [val_cast_nat]) lemma val_cast_of_lt {n : ℕ+} {a : ℕ} (h : a < n) : (a : zmod n).val = a := by rw [val_cast_nat, nat.mod_eq_of_lt h] @[simp] lemma cast_mod_nat (n : ℕ+) (a : ℕ) : ((a % n : ℕ) : zmod n) = a := by conv {to_rhs, rw ← nat.mod_add_div a n}; simp @[simp] lemma cast_val {n : ℕ+} (a : zmod n) : (a.val : zmod n) = a := by cases a; simp [mk_eq_cast] @[simp] lemma cast_mod_int (n : ℕ+) (a : ℤ) : ((a % (n : ℕ) : ℤ) : zmod n) = a := by conv {to_rhs, rw ← int.mod_add_div a n}; simp lemma val_cast_int {n : ℕ+} (a : ℤ) : (a : zmod n).val = (a % (n : ℕ)).nat_abs := have h : nat_abs (a % (n : ℕ)) < n := int.coe_nat_lt.1 begin rw [nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos))], conv {to_rhs, rw ← abs_of_nonneg (int.coe_nat_nonneg n)}, exact int.mod_lt _ (int.coe_nat_ne_zero_iff_pos.2 n.pos) end, int.coe_nat_inj $ by conv {to_lhs, rw [← cast_mod_int n a, ← nat_abs_of_nonneg (mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos)), int.cast_coe_nat, val_cast_of_lt h] } lemma eq_iff_modeq_nat {n : ℕ+} {a b : ℕ} : (a : zmod n) = b ↔ a ≡ b [MOD n] := ⟨λ h, by have := fin.veq_of_eq h; rwa [val_cast_nat, val_cast_nat] at this, λ h, fin.eq_of_veq $ by rwa [val_cast_nat, val_cast_nat]⟩ lemma eq_iff_modeq_int {n : ℕ+} {a b : ℤ} : (a : zmod n) = b ↔ a ≡ b [ZMOD (n : ℕ)] := ⟨λ h, by have := fin.veq_of_eq h; rwa [val_cast_int, val_cast_int, ← int.coe_nat_eq_coe_nat_iff, nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos)), nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 n.pos))] at this, λ h : a % (n : ℕ) = b % (n : ℕ), by rw [← cast_mod_int n a, ← cast_mod_int n b, h]⟩ instance (n : ℕ+) : fintype (zmod n) := fin.fintype _ lemma card_zmod (n : ℕ+) : fintype.card (zmod n) = n := fintype.card_fin n end zmod def zmodp (p : ℕ) (hp : prime p) : Type := zmod ⟨p, hp.pos⟩ namespace zmodp variables {p : ℕ} (hp : prime p) instance : comm_ring (zmodp p hp) := zmod.comm_ring ⟨p, hp.pos⟩ instance {p : ℕ} (hp : prime p) : has_inv (zmodp p hp) := ⟨λ a, gcd_a a.1 p⟩ lemma add_val : ∀ a b : zmodp p hp, (a + b).val = (a.val + b.val) % p | ⟨_, _⟩ ⟨_, _⟩ := rfl lemma mul_val : ∀ a b : zmodp p hp, (a * b).val = (a.val * b.val) % p | ⟨_, _⟩ ⟨_, _⟩ := rfl @[simp] lemma one_val : (1 : zmodp p hp).val = 1 := nat.mod_eq_of_lt hp.gt_one @[simp] lemma zero_val : (0 : zmodp p hp).val = 0 := rfl lemma val_cast_nat (a : ℕ) : (a : zmodp p hp).val = a % p := @zmod.val_cast_nat ⟨p, hp.pos⟩ _ lemma mk_eq_cast {a : ℕ} (h : a < p) : (⟨a, h⟩ : zmodp p hp) = (a : zmodp p hp) := @zmod.mk_eq_cast ⟨p, hp.pos⟩ _ _ @[simp] lemma cast_self_eq_zero: (p : zmodp p hp) = 0 := fin.eq_of_veq (by simpa [val_cast_nat]) lemma val_cast_of_lt {a : ℕ} (h : a < p) : (a : zmodp p hp).val = a := @zmod.val_cast_of_lt ⟨p, hp.pos⟩ _ h @[simp] lemma cast_mod_nat (a : ℕ) : ((a % p : ℕ) : zmodp p hp) = a := @zmod.cast_mod_nat ⟨p, hp.pos⟩ _ @[simp] lemma cast_val (a : zmodp p hp) : (a.val : zmodp p hp) = a := @zmod.cast_val ⟨p, hp.pos⟩ _ @[simp] lemma cast_mod_int (a : ℤ) : ((a % p : ℤ) : zmodp p hp) = a := @zmod.cast_mod_int ⟨p, hp.pos⟩ _ lemma val_cast_int (a : ℤ) : (a : zmodp p hp).val = (a % p).nat_abs := @zmod.val_cast_int ⟨p, hp.pos⟩ _ lemma eq_iff_modeq_nat {a b : ℕ} : (a : zmodp p hp) = b ↔ a ≡ b [MOD p] := @zmod.eq_iff_modeq_nat ⟨p, hp.pos⟩ _ _ lemma eq_iff_modeq_int {a b : ℤ} : (a : zmodp p hp) = b ↔ a ≡ b [ZMOD p] := @zmod.eq_iff_modeq_int ⟨p, hp.pos⟩ _ _ lemma gcd_a_modeq (a b : ℕ) : (a : ℤ) * gcd_a a b ≡ nat.gcd a b [ZMOD b] := by rw [← add_zero ((a : ℤ) * _), gcd_eq_gcd_ab]; exact int.modeq.modeq_add rfl (int.modeq.modeq_zero_iff.2 (dvd_mul_right _ _)).symm lemma mul_inv_eq_gcd (a : ℕ) : (a : zmodp p hp) * a⁻¹ = nat.gcd a p := by rw [← int.cast_coe_nat (nat.gcd _ _), nat.gcd_comm, nat.gcd_rec, ← (eq_iff_modeq_int _).2 (gcd_a_modeq _ _)]; simp [has_inv.inv, val_cast_nat] private lemma mul_inv_cancel_aux : ∀ a : zmodp p hp, a ≠ 0 → a * a⁻¹ = 1 := λ ⟨a, hap⟩ ha0, begin rw [mk_eq_cast, ne.def, ← @nat.cast_zero (zmodp p hp), eq_iff_modeq_nat, modeq_zero_iff] at ha0, have : nat.gcd p a = 1 := (prime.coprime_iff_not_dvd hp).2 ha0, rw [mk_eq_cast _ hap, mul_inv_eq_gcd, nat.gcd_comm], simpa [nat.gcd_comm, this] end instance : discrete_field (zmodp p hp) := { zero_ne_one := fin.ne_of_vne $ show 0 ≠ 1 % p, by rw nat.mod_eq_of_lt hp.gt_one; exact zero_ne_one, mul_inv_cancel := mul_inv_cancel_aux hp, inv_mul_cancel := λ a, by rw mul_comm; exact mul_inv_cancel_aux hp _, has_decidable_eq := by apply_instance, inv_zero := show (gcd_a 0 p : zmodp p hp) = 0, by unfold gcd_a xgcd xgcd_aux; refl, ..zmodp.comm_ring hp, ..zmodp.has_inv hp } end zmodp
deea525cacb436766b7b71d41710b0bc3fa2b864
3d2a7f1582fe5bae4d0bdc2fe86e997521239a65
/misc/bs.lean
0cd62ea1d3f1a7c41d311014661538f82df451c8
[]
no_license
own-pt/common-sense-lean
e4fa643ae010459de3d1bf673be7cbc7062563c9
f672210aecb4172f5bae265e43e6867397e13b1c
refs/heads/master
1,622,065,660,261
1,589,487,533,000
1,589,487,533,000
254,167,782
3
2
null
1,589,487,535,000
1,586,370,214,000
Lean
UTF-8
Lean
false
false
15,736
lean
/- The problem is originally presented in: A. Pease, G. Sutcliffe, N. Siegel, and S. Trac, “Large Theory Reasoning with SUMO at CASC,” pp. 1–8, Jul. 2009. Here we present the natural deduction proof in Lean. -/ constant U : Type constants SetOrClass Set Class Object Entity NullList_m List CorpuscularObject Invertebrate Vertebrate Animal SpinalColumn Organism Agent Physical Abstract subclass_m TransitiveRelation PartialOrderingRelation Relation : U constant BananaSlug10 : U constants exhaustiveDecomposition3 disjointDecomposition3 partition3 : U → U → U → Prop constant ins : U → U → Prop constant subclass : U → U → Prop constant disjoint : U → U → Prop constant component : U → U → Prop constant part : U → U → Prop constant inList : U → U → Prop constant ConsFn : U → U → U constant ListFn1 : U → U constant ListFn2 : U → U → U constant ListFn3 : U → U → U → U /- SUMO axioms -/ variable a13 : ins subclass_m PartialOrderingRelation variable a15 : ∀ x y z : U, ins x SetOrClass ∧ ins y SetOrClass → (subclass x y ∧ ins z x → ins z y) /- EDITED (see https://github.com/own-pt/cl-krr/issues/23) -/ variable a72773 : ∀ a : U, ((ins a Animal) ∧ (¬ ∃ p : U, ins p SpinalColumn ∧ part p a)) → ¬ ins a Vertebrate /- EDITED -/ variable a72774 : ¬ ∃ s : U, ins s SpinalColumn ∧ part s BananaSlug10 variable a72761 : ∀ x row0 row1 : U, ins row0 Entity ∧ ins row1 Entity ∧ ins x Entity → (ListFn3 x row0 row1 = ConsFn x (ListFn2 row0 row1)) variable a72767 : ∀ x y : U, (ins x Entity ∧ ins y Entity) → ((ListFn2 x y) = (ConsFn x (ConsFn y NullList_m))) variable a72768 : ∀ x : U, ins x Entity → (ListFn1 x = ConsFn x NullList_m) variable a72769 : ∀ x : U, ins x Entity → ¬ inList x NullList_m variable a72770 : ∀ L x y : U, (ins x Entity ∧ ins y Entity ∧ ins L List) → ((inList x (ConsFn y L)) ↔ ((x = y) ∨ inList x L)) variable a67959 : ins NullList_m List variable a67958 : ins List SetOrClass variable a72772 : ins BananaSlug10 Animal variable a72771 : ins Animal SetOrClass variable a72778 : ins Invertebrate SetOrClass variable a71402 : ins Vertebrate SetOrClass variable a71371 : ins Organism SetOrClass variable a71872 : ins Agent SetOrClass variable a71669 : ins Object SetOrClass variable a69763 : ins Physical SetOrClass variable a67331 : ins Entity SetOrClass variable a67448 : ins SetOrClass SetOrClass variable a68771 : ins Abstract SetOrClass variable a68763 : ins Relation SetOrClass variable a71844 : ins TransitiveRelation SetOrClass variable a72180 : ins PartialOrderingRelation SetOrClass variable a71370 : partition3 Animal Vertebrate Invertebrate variable a67131 : ∀ (c row0 row1 : U), (ins c Class ∧ ins row0 Class ∧ ins row1 Class) → (partition3 c row0 row1 ↔ (exhaustiveDecomposition3 c row0 row1 ∧ disjointDecomposition3 c row0 row1)) -- EDITED (see https://github.com/own-pt/cl-krr/issues/22) variable a67115 : ∀ (row0 row1 c obj : U), ∃ (item : U), ins item SetOrClass ∧ (ins obj Entity → ins c SetOrClass ∧ ins c Class ∧ ins row0 Class ∧ ins row0 Entity ∧ ins row1 Class ∧ ins row1 Entity → exhaustiveDecomposition3 c row0 row1 → ins obj c → inList item (ListFn2 row0 row1) ∧ ins obj item) variable a67447 : partition3 SetOrClass Set Class variable a67172 : ∃ x : U, ins x Entity variable a67173 : ∀ c : U, ins c Class ↔ subclass c Entity variable a67818 : subclass PartialOrderingRelation TransitiveRelation variable a67809 : ∀ x y z : U, ins x SetOrClass ∧ ins y SetOrClass ∧ ins z SetOrClass → ins subclass_m TransitiveRelation → (subclass x y ∧ subclass y z → subclass x z) variable a71382 : subclass Vertebrate Animal variable a71383 : subclass Invertebrate Animal variable a71369 : subclass Animal Organism variable a71340 : subclass Organism Agent variable a67315 : subclass Agent Object variable a67177 : subclass Object Physical variable a67174 : subclass Physical Entity variable a67446 : subclass SetOrClass Abstract variable a67332 : subclass Abstract Entity variable a67954 : subclass List Relation variable a67450 : subclass Relation Abstract -- commented in list.kif variable novo1 : ∀ (x L : U), ins L Entity → ins L List → ins (ConsFn x L) List -- some initial tests include a15 a71382 a71383 a71402 a72771 lemma VertebrateAnimal : ∀ (x : U), ins x Vertebrate → ins x Animal := begin intros a h, have h1, from a15 Vertebrate Animal a, apply h1, exact (and.intro a71402 a72771), exact (and.intro a71382 h) end omit a15 a71382 a71383 a71402 a72771 include a15 a72180 a71844 a67818 a13 lemma subclass_TransitiveRelation : ins subclass_m TransitiveRelation := begin specialize a15 PartialOrderingRelation TransitiveRelation subclass_m, apply a15, exact ⟨ a72180, a71844 ⟩, exact ⟨ a67818, a13 ⟩, end omit a15 a72180 a71844 a67818 a13 include a15 a72180 a71383 a71844 a13 a67818 a71402 a72771 a71371 a71382 a71369 a67809 lemma VertebrateOrganism : ∀ (x : U), ins x Vertebrate → ins x Organism := begin intros a h, have h1, from a15 Animal Organism a, apply h1, exact and.intro a72771 a71371, have h0 : ∀ x, ins x Vertebrate → ins x Animal, apply VertebrateAnimal; assumption, have h2, from h0 a h, exact and.intro a71369 h2, end lemma VertebrateOrganism' : ∀ (x : U), ins x Vertebrate → ins x Organism := begin intros a h, have h₁ : ins subclass_m TransitiveRelation, specialize a15 PartialOrderingRelation TransitiveRelation subclass_m, apply a15, exact ⟨a72180, a71844⟩, exact ⟨a67818, a13⟩, have h₂ : subclass Vertebrate Organism, apply a67809 _ Animal _, exact ⟨a71402, ⟨a72771, a71371⟩⟩, exact h₁, exact ⟨a71382, a71369⟩, apply a15 Vertebrate _ _, exact ⟨a71402, a71371⟩, exact ⟨h₂, h⟩ end omit a15 a72180 a71383 a71844 a13 a67818 a71402 a72771 a71371 a71382 a71369 a67809 include a15 a72180 a71844 a13 a67818 a71402 a72771 a71371 a71369 a67809 a71382 a67174 a69763 a67331 a71669 a71340 a67315 a67177 a71872 lemma VertebrateEntity : ∀ (x : U), ins x Vertebrate → ins x Entity := begin intros a h, have h1, apply subclass_TransitiveRelation; assumption, have h2 : subclass Vertebrate Organism, apply a67809 _ Animal _, exact ⟨a71402, ⟨ a72771, a71371 ⟩⟩, exact h1, exact ⟨a71382, a71369⟩, have h3 : subclass Vertebrate Agent, apply a67809 _ Organism _, exact ⟨a71402, ⟨ a71371, a71872 ⟩⟩, exact h1, exact ⟨h2, a71340⟩, have h4 : subclass Vertebrate Object, apply a67809 _ Agent _, exact ⟨a71402, ⟨ a71872, a71669 ⟩⟩, exact h1, exact ⟨h3, a67315⟩, have h5 : subclass Vertebrate Physical, apply a67809 _ Object _, exact ⟨a71402, ⟨ a71669, a69763 ⟩⟩, exact h1, exact ⟨h4, a67177⟩, have h6 : subclass Vertebrate Entity, apply a67809 _ Physical _, exact ⟨a71402, ⟨ a69763, a67331 ⟩⟩, exact h1, exact ⟨ h5, a67174 ⟩, apply a15 Vertebrate _ _, exact ⟨a71402, a67331⟩, exact ⟨h6, h⟩ end omit a15 a72180 a71844 a13 a67818 a71402 a72771 a71371 a71369 a67809 a71382 a67174 a69763 a67331 a71669 a71340 a67315 a67177 a71872 -- start proofs include a72767 a72768 a72769 a72770 a67959 a15 novo1 a67954 a67332 a67331 a68771 a68763 a67958 a67450 lemma listLemma (hne : nonempty U) : ∀ x y z : U, ins x Entity ∧ ins y Entity ∧ ins z Entity → inList x (ListFn2 y z) → x = y ∨ x = z := begin intros x y z h h1, rw (a72767 y z ⟨h.right.left, h.right.right⟩) at h1, have h2 : x = y ∨ inList x (ConsFn z NullList_m), rw ←(a72770 (ConsFn z NullList_m) x y), exact h1, simp *, apply novo1 z NullList_m, apply a15 Abstract Entity NullList_m; simp *, apply a15 Relation Abstract NullList_m; simp *, apply a15 List Relation NullList_m; simp *, assumption, cases h2, exact or.inl h2, have h3 : x = z ∨ inList x NullList_m, rw ←(a72770 NullList_m x z), exact h2, exact ⟨h.1, ⟨h.2.2, a67959⟩⟩, cases h3, exact or.inr h3, apply false.elim, exact ((a72769 x) h.left) h3 end omit a72767 a72768 a72769 a72770 a67959 a15 novo1 a67954 a67332 a67331 a68771 a68763 a67958 a67450 include a67131 a67115 a67809 a67448 a68771 a67331 a15 a72180 a71844 a67818 a13 a67446 a67332 a72767 a72768 a72769 a72770 a67959 novo1 a67954 a68763 a67958 a67450 lemma lX (hne : nonempty U) : ∀ x c c1 c2, (ins c SetOrClass ∧ ins c1 SetOrClass ∧ ins c2 SetOrClass) → (ins c Class ∧ ins c1 Class ∧ ins c2 Class ∧ ins x Entity) → (partition3 c c1 c2 ∧ ins x c ∧ ¬ ins x c1) → ins x c2 := begin intros a c c1 c2 h1 h2 h3, specialize a67131 c c1 c2, specialize a67115 c1 c2 c a, have h₃, from a67131 ⟨ h2.1, ⟨h2.2.1, h2.2.2.1 ⟩⟩, have h4, from iff.elim_left h₃ h3.1, cases h4 with h4a h4b, cases a67115 with b h5, have h7, from h5.right, have h8, from h2.right.right.right, specialize h7 h8, have h9 : subclass SetOrClass Entity, apply (a67809 _ Abstract _), exact ⟨a67448, ⟨a68771, a67331⟩⟩, apply subclass_TransitiveRelation; assumption, exact ⟨a67446, a67332⟩, have h10 : ins c1 Entity, apply (a15 SetOrClass _ _), exact ⟨a67448, a67331⟩, exact ⟨h9, h1.2.1⟩, have h11 : ins c2 Entity, apply (a15 SetOrClass _ _), exact ⟨a67448, a67331⟩, exact ⟨h9, h1.2.2⟩, specialize h7 ⟨h1.1, ⟨h2.1, ⟨h2.2.1, ⟨h10, ⟨h2.2.2.1, h11⟩⟩⟩⟩⟩, specialize h7 h4a, specialize h7 h3.right.left, have h12 : b = c1 ∨ b = c2, apply listLemma, repeat { assumption }, split, apply a15 SetOrClass _ _, exact ⟨a67448, a67331⟩, exact ⟨h9, h5.left⟩, exact ⟨h10, h11⟩, exact h7.left, cases h12, rw h12 at h7, apply false.elim, exact h3.right.right h7.right, rw ←h12, exact h7.right end include a72771 a71371 a71872 a71369 a71340 a71669 a67315 a69763 a67177 a67174 lemma subclass_animal_entity : subclass Animal Entity := begin have h1, apply subclass_TransitiveRelation; assumption, have h2 : subclass Animal Agent, apply a67809 _ Organism _, exact ⟨a72771, ⟨ a71371, a71872 ⟩⟩, exact h1, exact ⟨a71369, a71340⟩, have h3 : subclass Animal Object, apply a67809 _ Agent _, exact ⟨a72771, ⟨ a71872, a71669 ⟩⟩, exact h1, exact ⟨h2, a67315⟩, have h4 : subclass Animal Physical, apply a67809 _ Object _, exact ⟨a72771, ⟨ a71669, a69763 ⟩⟩, exact h1, exact ⟨h3, a67177⟩, apply a67809 _ Physical _, exact ⟨a72771, ⟨ a69763, a67331 ⟩⟩, exact h1, exact ⟨ h4, a67174 ⟩, end include a71402 a71382 lemma subclass_vertebrate_entity : subclass Vertebrate Entity := begin have h1, apply subclass_TransitiveRelation; assumption, have h2 : subclass Vertebrate Organism, apply a67809 _ Animal _, exact ⟨a71402, ⟨ a72771, a71371 ⟩⟩, exact h1, exact ⟨a71382, a71369⟩, have h3 : subclass Vertebrate Agent, apply a67809 _ Organism _, exact ⟨a71402, ⟨ a71371, a71872 ⟩⟩, exact h1, exact ⟨h2, a71340⟩, have h4 : subclass Vertebrate Object, apply a67809 _ Agent _, exact ⟨a71402, ⟨ a71872, a71669 ⟩⟩, exact h1, exact ⟨h3, a67315⟩, have h5 : subclass Vertebrate Physical, apply a67809 _ Object _, exact ⟨a71402, ⟨ a71669, a69763 ⟩⟩, exact h1, exact ⟨h4, a67177⟩, apply a67809 _ Physical _, exact ⟨a71402, ⟨ a69763, a67331 ⟩⟩, exact h1, exact ⟨ h5, a67174 ⟩, end include a72778 a71383 lemma subclass_invertebrate_entity : subclass Invertebrate Entity := begin have h1, apply subclass_TransitiveRelation; assumption, have h2 : subclass Invertebrate Organism, apply a67809 _ Animal _, exact ⟨a72778, ⟨ a72771, a71371 ⟩⟩, exact h1, exact ⟨a71383, a71369⟩, have h3 : subclass Invertebrate Agent, apply a67809 _ Organism _, exact ⟨a72778, ⟨ a71371, a71872 ⟩⟩, exact h1, exact ⟨h2, a71340⟩, have h4 : subclass Invertebrate Object, apply a67809 _ Agent _, exact ⟨a72778, ⟨ a71872, a71669 ⟩⟩, exact h1, exact ⟨h3, a67315⟩, have h5 : subclass Invertebrate Physical, apply a67809 _ Object _, exact ⟨a72778, ⟨ a71669, a69763 ⟩⟩, exact h1, exact ⟨h4, a67177⟩, apply a67809 _ Physical _, exact ⟨a72778, ⟨ a69763, a67331 ⟩⟩, exact h1, exact ⟨ h5, a67174 ⟩, end include a72772 a72774 lemma ins_banana_entity : ins BananaSlug10 Entity := begin have h1, apply subclass_TransitiveRelation; assumption, have h2 : ins BananaSlug10 Organism, specialize a15 Animal Organism BananaSlug10, apply a15, exact and.intro a72771 a71371, exact and.intro a71369 a72772, have h3 : ins BananaSlug10 Agent, specialize a15 Organism Agent BananaSlug10, apply a15, exact and.intro a71371 a71872, exact and.intro a71340 h2, have h4 : ins BananaSlug10 Object, specialize a15 Agent Object BananaSlug10, apply a15, exact and.intro a71872 a71669, exact and.intro a67315 h3, have h5 : ins BananaSlug10 Physical, specialize a15 Object Physical BananaSlug10, apply a15, exact and.intro a71669 a69763, exact and.intro a67177 h4, specialize a15 Physical Entity BananaSlug10, apply a15, exact and.intro a69763 a67331, exact and.intro a67174 h5, end include a67173 lemma ins_animal_class : ins Animal Class := begin have h0 : subclass Animal Entity, apply subclass_animal_entity; assumption, have h1, from (a67173 Animal), exact h1.2 h0, end include a71370 a72773 lemma l0' (hne : nonempty U) : ¬(ins BananaSlug10 Vertebrate) := by simp * lemma l0 (hne : nonempty U) : ¬(ins BananaSlug10 Vertebrate) := begin specialize a72773 BananaSlug10, exact a72773 (and.intro a72772 a72774) end theorem Banana_Invertebrate (hne: nonempty U) : ins BananaSlug10 Invertebrate := begin have h1 : ¬ ins BananaSlug10 Vertebrate, apply l0; assumption, have h2 : ∀ x c c1 c2, (ins c SetOrClass ∧ ins c1 SetOrClass ∧ ins c2 SetOrClass) → (ins c Class ∧ ins c1 Class ∧ ins c2 Class ∧ ins x Entity) → (partition3 c c1 c2 ∧ ins x c ∧ ¬ ins x c1) → ins x c2, apply lX; assumption, have h3, from h2 BananaSlug10 Animal Vertebrate Invertebrate, apply h3, exact ⟨ a72771, ⟨ a71402, a72778 ⟩⟩, have h₁ : subclass Animal Entity, apply subclass_animal_entity; assumption, have h₂ : ins Animal Class, rw a67173, exact h₁, have h₃ : subclass Vertebrate Entity, apply subclass_vertebrate_entity; assumption, have h₄ : ins Vertebrate Class, rw a67173, exact h₃, have h₅ : subclass Invertebrate Entity, apply subclass_invertebrate_entity; assumption, have h₆ : ins Invertebrate Class, rw a67173, exact h₅, have h₇ : ins BananaSlug10 Entity, apply ins_banana_entity; assumption, exact and.intro h₂ (and.intro h₄ (and.intro h₆ h₇)), exact and.intro a71370 (and.intro a72772 h1), end example (X Y C : U) (h : partition3 C X Y) : subclass X C ∧ subclass Y C := sorry example (h : ins SetOrClass SetOrClass) : false := sorry
e1a8d16f942ba587338835df3be4dad97e39c400
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/calculus/series.lean
cc4856796a27a636e99ecceac466df45afaa2719
[ "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,371
lean
/- Copyright (c) 2022 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.calculus.uniform_limits_deriv import analysis.calculus.cont_diff import data.nat.cast.with_top /-! # Smoothness of series > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We show that series of functions are continuous, or differentiable, or smooth, when each individual function in the series is and additionally suitable uniform summable bounds are satisfied. More specifically, * `continuous_tsum` ensures that a series of continuous functions is continuous. * `differentiable_tsum` ensures that a series of differentiable functions is differentiable. * `cont_diff_tsum` ensures that a series of smooth functions is smooth. We also give versions of these statements which are localized to a set. -/ open set metric topological_space function asymptotics filter open_locale topology nnreal big_operators variables {α β 𝕜 E F : Type*} [is_R_or_C 𝕜] [normed_add_comm_group E] [normed_space 𝕜 E] [normed_add_comm_group F] [complete_space F] {u : α → ℝ} /-! ### Continuity -/ /-- An infinite sum of functions with summable sup norm is the uniform limit of its partial sums. Version relative to a set, with general index set. -/ lemma tendsto_uniformly_on_tsum {f : α → β → F} (hu : summable u) {s : set β} (hfu : ∀ n x, x ∈ s → ‖f n x‖ ≤ u n) : tendsto_uniformly_on (λ (t : finset α), (λ x, ∑ n in t, f n x)) (λ x, ∑' n, f n x) at_top s := begin refine tendsto_uniformly_on_iff.2 (λ ε εpos, _), filter_upwards [(tendsto_order.1 (tendsto_tsum_compl_at_top_zero u)).2 _ εpos] with t ht x hx, have A : summable (λ n, ‖f n x‖), from summable_of_nonneg_of_le (λ n, norm_nonneg _) (λ n, hfu n x hx) hu, rw [dist_eq_norm, ← sum_add_tsum_subtype_compl (summable_of_summable_norm A) t, add_sub_cancel'], apply lt_of_le_of_lt _ ht, apply (norm_tsum_le_tsum_norm (A.subtype _)).trans, exact tsum_le_tsum (λ n, hfu _ _ hx) (A.subtype _) (hu.subtype _) end /-- An infinite sum of functions with summable sup norm is the uniform limit of its partial sums. Version relative to a set, with index set `ℕ`. -/ lemma tendsto_uniformly_on_tsum_nat {f : ℕ → β → F} {u : ℕ → ℝ} (hu : summable u) {s : set β} (hfu : ∀ n x, x ∈ s → ‖f n x‖ ≤ u n) : tendsto_uniformly_on (λ N, (λ x, ∑ n in finset.range N, f n x)) (λ x, ∑' n, f n x) at_top s := λ v hv, tendsto_finset_range.eventually (tendsto_uniformly_on_tsum hu hfu v hv) /-- An infinite sum of functions with summable sup norm is the uniform limit of its partial sums. Version with general index set. -/ lemma tendsto_uniformly_tsum {f : α → β → F} (hu : summable u) (hfu : ∀ n x, ‖f n x‖ ≤ u n) : tendsto_uniformly (λ (t : finset α), (λ x, ∑ n in t, f n x)) (λ x, ∑' n, f n x) at_top := by { rw ← tendsto_uniformly_on_univ, exact tendsto_uniformly_on_tsum hu (λ n x hx, hfu n x) } /-- An infinite sum of functions with summable sup norm is the uniform limit of its partial sums. Version with index set `ℕ`. -/ lemma tendsto_uniformly_tsum_nat {f : ℕ → β → F} {u : ℕ → ℝ} (hu : summable u) (hfu : ∀ n x, ‖f n x‖ ≤ u n) : tendsto_uniformly (λ N, (λ x, ∑ n in finset.range N, f n x)) (λ x, ∑' n, f n x) at_top := λ v hv, tendsto_finset_range.eventually (tendsto_uniformly_tsum hu hfu v hv) /-- An infinite sum of functions with summable sup norm is continuous on a set if each individual function is. -/ lemma continuous_on_tsum [topological_space β] {f : α → β → F} {s : set β} (hf : ∀ i, continuous_on (f i) s) (hu : summable u) (hfu : ∀ n x, x ∈ s → ‖f n x‖ ≤ u n) : continuous_on (λ x, ∑' n, f n x) s := begin classical, refine (tendsto_uniformly_on_tsum hu hfu).continuous_on (eventually_of_forall _), assume t, exact continuous_on_finset_sum _ (λ i hi, hf i), end /-- An infinite sum of functions with summable sup norm is continuous if each individual function is. -/ lemma continuous_tsum [topological_space β] {f : α → β → F} (hf : ∀ i, continuous (f i)) (hu : summable u) (hfu : ∀ n x, ‖f n x‖ ≤ u n) : continuous (λ x, ∑' n, f n x) := begin simp_rw [continuous_iff_continuous_on_univ] at hf ⊢, exact continuous_on_tsum hf hu (λ n x hx, hfu n x), end /-! ### Differentiability -/ variables [normed_space 𝕜 F] variables {f : α → E → F} {f' : α → E → (E →L[𝕜] F)} {v : ℕ → α → ℝ} {s : set E} {x₀ x : E} {N : ℕ∞} /-- Consider a series of functions `∑' n, f n x` on a preconnected open set. If the series converges at a point, and all functions in the series are differentiable with a summable bound on the derivatives, then the series converges everywhere on the set. -/ lemma summable_of_summable_has_fderiv_at_of_is_preconnected (hu : summable u) (hs : is_open s) (h's : is_preconnected s) (hf : ∀ n x, x ∈ s → has_fderiv_at (f n) (f' n x) x) (hf' : ∀ n x, x ∈ s → ‖f' n x‖ ≤ u n) (hx₀ : x₀ ∈ s) (hf0 : summable (λ n, f n x₀)) {x : E} (hx : x ∈ s) : summable (λ n, f n x) := begin rw summable_iff_cauchy_seq_finset at hf0 ⊢, have A : uniform_cauchy_seq_on (λ (t : finset α), (λ x, ∑ i in t, f' i x)) at_top s, from (tendsto_uniformly_on_tsum hu hf').uniform_cauchy_seq_on, apply cauchy_map_of_uniform_cauchy_seq_on_fderiv hs h's A (λ t y hy, _) hx₀ hx hf0, exact has_fderiv_at.sum (λ i hi, hf i y hy), end /-- Consider a series of functions `∑' n, f n x` on a preconnected open set. If the series converges at a point, and all functions in the series are differentiable with a summable bound on the derivatives, then the series is differentiable on the set and its derivative is the sum of the derivatives. -/ lemma has_fderiv_at_tsum_of_is_preconnected (hu : summable u) (hs : is_open s) (h's : is_preconnected s) (hf : ∀ n x, x ∈ s → has_fderiv_at (f n) (f' n x) x) (hf' : ∀ n x, x ∈ s → ‖f' n x‖ ≤ u n) (hx₀ : x₀ ∈ s) (hf0 : summable (λ n, f n x₀)) (hx : x ∈ s) : has_fderiv_at (λ y, ∑' n, f n y) (∑' n, f' n x) x := begin classical, have A : ∀ (x : E), x ∈ s → tendsto (λ (t : finset α), ∑ n in t, f n x) at_top (𝓝 (∑' n, f n x)), { assume y hy, apply summable.has_sum, exact summable_of_summable_has_fderiv_at_of_is_preconnected hu hs h's hf hf' hx₀ hf0 hy }, apply has_fderiv_at_of_tendsto_uniformly_on hs (tendsto_uniformly_on_tsum hu hf') (λ t y hy, _) A _ hx, exact has_fderiv_at.sum (λ n hn, hf n y hy), end /-- Consider a series of functions `∑' n, f n x`. If the series converges at a point, and all functions in the series are differentiable with a summable bound on the derivatives, then the series converges everywhere. -/ lemma summable_of_summable_has_fderiv_at (hu : summable u) (hf : ∀ n x, has_fderiv_at (f n) (f' n x) x) (hf' : ∀ n x, ‖f' n x‖ ≤ u n) (hf0 : summable (λ n, f n x₀)) (x : E) : summable (λ n, f n x) := begin letI : normed_space ℝ E, from normed_space.restrict_scalars ℝ 𝕜 _, apply summable_of_summable_has_fderiv_at_of_is_preconnected hu is_open_univ is_connected_univ.is_preconnected (λ n x hx, hf n x) (λ n x hx, hf' n x) (mem_univ _) hf0 (mem_univ _), end /-- Consider a series of functions `∑' n, f n x`. If the series converges at a point, and all functions in the series are differentiable with a summable bound on the derivatives, then the series is differentiable and its derivative is the sum of the derivatives. -/ lemma has_fderiv_at_tsum (hu : summable u) (hf : ∀ n x, has_fderiv_at (f n) (f' n x) x) (hf' : ∀ n x, ‖f' n x‖ ≤ u n) (hf0 : summable (λ n, f n x₀)) (x : E) : has_fderiv_at (λ y, ∑' n, f n y) (∑' n, f' n x) x := begin letI : normed_space ℝ E, from normed_space.restrict_scalars ℝ 𝕜 _, exact has_fderiv_at_tsum_of_is_preconnected hu is_open_univ is_connected_univ.is_preconnected (λ n x hx, hf n x) (λ n x hx, hf' n x) (mem_univ _) hf0 (mem_univ _), end /-- Consider a series of functions `∑' n, f n x`. If all functions in the series are differentiable with a summable bound on the derivatives, then the series is differentiable. Note that our assumptions do not ensure the pointwise convergence, but if there is no pointwise convergence then the series is zero everywhere so the result still holds. -/ lemma differentiable_tsum (hu : summable u) (hf : ∀ n x, has_fderiv_at (f n) (f' n x) x) (hf' : ∀ n x, ‖f' n x‖ ≤ u n) : differentiable 𝕜 (λ y, ∑' n, f n y) := begin by_cases h : ∃ x₀, summable (λ n, f n x₀), { rcases h with ⟨x₀, hf0⟩, assume x, exact (has_fderiv_at_tsum hu hf hf' hf0 x).differentiable_at }, { push_neg at h, have : (λ x, ∑' n, f n x) = 0, { ext1 x, exact tsum_eq_zero_of_not_summable (h x) }, rw this, exact differentiable_const 0 } end lemma fderiv_tsum_apply (hu : summable u) (hf : ∀ n, differentiable 𝕜 (f n)) (hf' : ∀ n x, ‖fderiv 𝕜 (f n) x‖ ≤ u n) (hf0 : summable (λ n, f n x₀)) (x : E) : fderiv 𝕜 (λ y, ∑' n, f n y) x = ∑' n, fderiv 𝕜 (f n) x := (has_fderiv_at_tsum hu (λ n x, (hf n x).has_fderiv_at) hf' hf0 _).fderiv lemma fderiv_tsum (hu : summable u) (hf : ∀ n, differentiable 𝕜 (f n)) (hf' : ∀ n x, ‖fderiv 𝕜 (f n) x‖ ≤ u n) {x₀ : E} (hf0 : summable (λ n, f n x₀)) : fderiv 𝕜 (λ y, ∑' n, f n y) = (λ x, ∑' n, fderiv 𝕜 (f n) x) := by { ext1 x, exact fderiv_tsum_apply hu hf hf' hf0 x} /-! ### Higher smoothness -/ /-- Consider a series of smooth functions, with summable uniform bounds on the successive derivatives. Then the iterated derivative of the sum is the sum of the iterated derivative. -/ lemma iterated_fderiv_tsum (hf : ∀ i, cont_diff 𝕜 N (f i)) (hv : ∀ (k : ℕ), (k : ℕ∞) ≤ N → summable (v k)) (h'f : ∀ (k : ℕ) (i : α) (x : E), (k : ℕ∞) ≤ N → ‖iterated_fderiv 𝕜 k (f i) x‖ ≤ v k i) {k : ℕ} (hk : (k : ℕ∞) ≤ N) : iterated_fderiv 𝕜 k (λ y, ∑' n, f n y) = (λ x, ∑' n, iterated_fderiv 𝕜 k (f n) x) := begin induction k with k IH, { ext1 x, simp_rw [iterated_fderiv_zero_eq_comp], exact (continuous_multilinear_curry_fin0 𝕜 E F).symm.to_continuous_linear_equiv.map_tsum }, { have h'k : (k : ℕ∞) < N, from lt_of_lt_of_le (with_top.coe_lt_coe.2 (nat.lt_succ_self _)) hk, have A : summable (λ n, iterated_fderiv 𝕜 k (f n) 0), from summable_of_norm_bounded (v k) (hv k h'k.le) (λ n, h'f k n 0 h'k.le), simp_rw [iterated_fderiv_succ_eq_comp_left, IH h'k.le], rw fderiv_tsum (hv _ hk) (λ n, (hf n).differentiable_iterated_fderiv h'k) _ A, { ext1 x, exact (continuous_multilinear_curry_left_equiv 𝕜 (λ (i : fin (k + 1)), E) F) .to_continuous_linear_equiv.map_tsum }, { assume n x, simpa only [iterated_fderiv_succ_eq_comp_left, linear_isometry_equiv.norm_map] using h'f k.succ n x hk } } end /-- Consider a series of smooth functions, with summable uniform bounds on the successive derivatives. Then the iterated derivative of the sum is the sum of the iterated derivative. -/ lemma iterated_fderiv_tsum_apply (hf : ∀ i, cont_diff 𝕜 N (f i)) (hv : ∀ (k : ℕ), (k : ℕ∞) ≤ N → summable (v k)) (h'f : ∀ (k : ℕ) (i : α) (x : E), (k : ℕ∞) ≤ N → ‖iterated_fderiv 𝕜 k (f i) x‖ ≤ v k i) {k : ℕ} (hk : (k : ℕ∞) ≤ N) (x : E) : iterated_fderiv 𝕜 k (λ y, ∑' n, f n y) x = ∑' n, iterated_fderiv 𝕜 k (f n) x := by rw iterated_fderiv_tsum hf hv h'f hk /-- Consider a series of functions `∑' i, f i x`. Assume that each individual function `f i` is of class `C^N`, and moreover there is a uniform summable upper bound on the `k`-th derivative for each `k ≤ N`. Then the series is also `C^N`. -/ lemma cont_diff_tsum (hf : ∀ i, cont_diff 𝕜 N (f i)) (hv : ∀ (k : ℕ), (k : ℕ∞) ≤ N → summable (v k)) (h'f : ∀ (k : ℕ) (i : α) (x : E), (k : ℕ∞) ≤ N → ‖iterated_fderiv 𝕜 k (f i) x‖ ≤ v k i) : cont_diff 𝕜 N (λ x, ∑' i, f i x) := begin rw cont_diff_iff_continuous_differentiable, split, { assume m hm, rw iterated_fderiv_tsum hf hv h'f hm, refine continuous_tsum _ (hv m hm) _, { assume i, exact cont_diff.continuous_iterated_fderiv hm (hf i) }, { assume n x, exact h'f _ _ _ hm } }, { assume m hm, have h'm : ((m+1 : ℕ) : ℕ∞) ≤ N, by simpa only [enat.coe_add, nat.cast_with_bot, enat.coe_one] using enat.add_one_le_of_lt hm, rw iterated_fderiv_tsum hf hv h'f hm.le, have A : ∀ n x, has_fderiv_at (iterated_fderiv 𝕜 m (f n)) (fderiv 𝕜 (iterated_fderiv 𝕜 m (f n)) x) x, from λ n x, (cont_diff.differentiable_iterated_fderiv hm (hf n)).differentiable_at.has_fderiv_at, apply differentiable_tsum (hv _ h'm) A (λ n x, _), rw [fderiv_iterated_fderiv, linear_isometry_equiv.norm_map], exact h'f _ _ _ h'm } end /-- Consider a series of functions `∑' i, f i x`. Assume that each individual function `f i` is of class `C^N`, and moreover there is a uniform summable upper bound on the `k`-th derivative for each `k ≤ N` (except maybe for finitely many `i`s). Then the series is also `C^N`. -/ lemma cont_diff_tsum_of_eventually (hf : ∀ i, cont_diff 𝕜 N (f i)) (hv : ∀ (k : ℕ), (k : ℕ∞) ≤ N → summable (v k)) (h'f : ∀ (k : ℕ), (k : ℕ∞) ≤ N → ∀ᶠ i in (filter.cofinite : filter α), ∀ (x : E), ‖iterated_fderiv 𝕜 k (f i) x‖ ≤ v k i) : cont_diff 𝕜 N (λ x, ∑' i, f i x) := begin classical, apply cont_diff_iff_forall_nat_le.2 (λ m hm, _), let t : set α := {i : α | ¬∀ (k : ℕ), k ∈ finset.range (m + 1) → ∀ x, ‖iterated_fderiv 𝕜 k (f i) x‖ ≤ v k i}, have ht : set.finite t, { have A : ∀ᶠ i in (filter.cofinite : filter α), ∀ (k : ℕ), k ∈ finset.range (m+1) → ∀ (x : E), ‖iterated_fderiv 𝕜 k (f i) x‖ ≤ v k i, { rw eventually_all_finset, assume i hi, apply h'f, simp only [finset.mem_range_succ_iff] at hi, exact (with_top.coe_le_coe.2 hi).trans hm }, exact eventually_cofinite.2 A }, let T : finset α := ht.to_finset, have : (λ x, ∑' i, f i x) = (λ x, ∑ i in T, f i x) + (λ x, ∑' i : {i // i ∉ T}, f i x), { ext1 x, refine (sum_add_tsum_subtype_compl _ T).symm, refine summable_of_norm_bounded_eventually _ (hv 0 (zero_le _)) _, filter_upwards [h'f 0 (zero_le _)] with i hi, simpa only [norm_iterated_fderiv_zero] using hi x }, rw this, apply (cont_diff.sum (λ i hi, (hf i).of_le hm)).add, have h'u : ∀ (k : ℕ), (k : ℕ∞) ≤ m → summable ((v k) ∘ (coe : {i // i ∉ T} → α)), from λ k hk, (hv k (hk.trans hm)).subtype _, refine cont_diff_tsum (λ i, (hf i).of_le hm) h'u _, rintros k ⟨i, hi⟩ x hk, dsimp, simp only [finite.mem_to_finset, mem_set_of_eq, finset.mem_range, not_forall, not_le, exists_prop, not_exists, not_and, not_lt] at hi, exact hi k (nat.lt_succ_iff.2 (with_top.coe_le_coe.1 hk)) x, end
e6b4ea2560fbdc09d9997cfc84a9903a2b2aa51d
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_648.lean
f2dc2462e670148e7e236ed492461048ef3c154b
[]
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
import algebra.ring namespace my_ring variables {R : Type*} [ring R] -- BEGIN theorem add_neg_cancel_right (a b : R) : (a + b) + -b = a := sorry -- END end my_ring
7473adba372e80997ff565d7cf627fec2d123d12
2c096fdfecf64e46ea7bc6ce5521f142b5926864
/src/Lean/Elab/Quotation.lean
3673a1159a74cb9c940e9cd5a07148e5a90b2917
[ "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
Kha/lean4
1005785d2c8797ae266a303968848e5f6ce2fe87
b99e11346948023cd6c29d248cd8f3e3fb3474cf
refs/heads/master
1,693,355,498,027
1,669,080,461,000
1,669,113,138,000
184,748,176
0
0
Apache-2.0
1,665,995,520,000
1,556,884,930,000
Lean
UTF-8
Lean
false
false
33,518
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich Elaboration of syntax quotations as terms and patterns (in `match_syntax`). See also `./Hygiene.lean` for the basic hygiene workings and data types. -/ import Lean.Syntax import Lean.ResolveName import Lean.Elab.Term import Lean.Elab.Quotation.Util import Lean.Elab.Quotation.Precheck import Lean.Elab.Syntax import Lean.Parser.Syntax namespace Lean.Elab.Term.Quotation open Lean.Parser.Term open Lean.Syntax open Meta open TSyntax.Compat -- TODO(gabriel): We heavily use `have` here to keep the local context clean. -- We should replace this by non-dependent lets in the future. /-- `C[$(e)]` ~> `let a := e; C[$a]`. Used in the implementation of antiquot splices. -/ private partial def floatOutAntiquotTerms (stx : Syntax) : StateT (Syntax → TermElabM Syntax) TermElabM Syntax := if isAntiquots stx && !isEscapedAntiquot (getCanonicalAntiquot stx) then let e := getAntiquotTerm (getCanonicalAntiquot stx) if !e.isIdent || !e.getId.isAtomic then withFreshMacroScope do let a ← `(__stx_lift) modify (fun _ (stx : Syntax) => (`(let $a:ident := $e; $stx) : TermElabM Syntax)) let stx := if stx.isOfKind choiceKind then mkNullNode <| stx.getArgs.map (·.setArg 2 a) else stx.setArg 2 a return stx else return stx else if let Syntax.node i k args := stx then return Syntax.node i k (← args.mapM floatOutAntiquotTerms) else return stx private def getSepFromSplice (splice : Syntax) : String := if let Syntax.atom _ sep := getAntiquotSpliceSuffix splice then sep.dropRight 1 -- drop trailing * else unreachable! private def getSepStxFromSplice (splice : Syntax) : Syntax := Unhygienic.run do match getSepFromSplice splice with | "" => `(mkNullNode) -- sepByIdent uses the null node for separator-less enumerations | sep => `(mkAtom $(Syntax.mkStrLit sep)) partial def mkTuple : Array Syntax → TermElabM Syntax | #[] => `(Unit.unit) | #[e] => return e | es => do let stx ← mkTuple (es.eraseIdx 0) `(Prod.mk $(es[0]!) $stx) def resolveSectionVariable (sectionVars : NameMap Name) (id : Name) : List (Name × List String) := -- decode macro scopes from name before recursion let extractionResult := extractMacroScopes id let rec loop : Name → List String → List (Name × List String) | id@(.str p s), projs => -- NOTE: we assume that macro scopes always belong to the projected constant, not the projections let id := { extractionResult with name := id }.review match sectionVars.find? id with | some newId => [(newId, projs)] | none => loop p (s::projs) | _, _ => [] loop extractionResult.name [] /-- Transform sequence of pushes and appends into acceptable code -/ def ArrayStxBuilder := Sum (Array Term) Term namespace ArrayStxBuilder def empty : ArrayStxBuilder := .inl #[] def build : ArrayStxBuilder → Term | .inl elems => quote elems | .inr arr => arr def push (b : ArrayStxBuilder) (elem : Syntax) : ArrayStxBuilder := match b with | .inl elems => .inl <| elems.push elem | .inr arr => .inr <| mkCApp ``Array.push #[arr, elem] def append (b : ArrayStxBuilder) (arr : Syntax) (appendName := ``Array.append) : ArrayStxBuilder := .inr <| mkCApp appendName #[b.build, arr] def mkNode (b : ArrayStxBuilder) (k : SyntaxNodeKind) : TermElabM Term := do let k := quote k match b with | .inl #[a₁] => `(Syntax.node1 info $(k) $(a₁)) | .inl #[a₁, a₂] => `(Syntax.node2 info $(k) $(a₁) $(a₂)) | .inl #[a₁, a₂, a₃] => `(Syntax.node3 info $(k) $(a₁) $(a₂) $(a₃)) | .inl #[a₁, a₂, a₃, a₄] => `(Syntax.node4 info $(k) $(a₁) $(a₂) $(a₃) $(a₄)) | .inl #[a₁, a₂, a₃, a₄, a₅] => `(Syntax.node5 info $(k) $(a₁) $(a₂) $(a₃) $(a₄) $(a₅)) | .inl #[a₁, a₂, a₃, a₄, a₅, a₆] => `(Syntax.node6 info $(k) $(a₁) $(a₂) $(a₃) $(a₄) $(a₅) $(a₆)) | .inl #[a₁, a₂, a₃, a₄, a₅, a₆, a₇] => `(Syntax.node7 info $(k) $(a₁) $(a₂) $(a₃) $(a₄) $(a₅) $(a₆) $(a₇)) | .inl #[a₁, a₂, a₃, a₄, a₅, a₆, a₇, a₈] => `(Syntax.node8 info $(k) $(a₁) $(a₂) $(a₃) $(a₄) $(a₅) $(a₆) $(a₇) $(a₈)) | _ => `(Syntax.node info $(k) $(b.build)) end ArrayStxBuilder def tryAddSyntaxNodeKindInfo (stx : Syntax) (k : SyntaxNodeKind) : TermElabM Unit := do if (← getEnv).contains k then addTermInfo' stx (← mkConstWithFreshMVarLevels k) else -- HACK to support built in categories, which use a different naming convention let k := ``Lean.Parser.Category ++ k if (← getEnv).contains k then addTermInfo' stx (← mkConstWithFreshMVarLevels k) instance : Quote Syntax.Preresolved where quote | .namespace ns => Unhygienic.run ``(Syntax.Preresolved.namespace $(quote ns)) | .decl n fs => Unhygienic.run ``(Syntax.Preresolved.decl $(quote n) $(quote fs)) /-- Elaborate the content of a syntax quotation term -/ private partial def quoteSyntax : Syntax → TermElabM Term | Syntax.ident _ rawVal val preresolved => do if !hygiene.get (← getOptions) then return ← `(Syntax.ident info $(quote rawVal) $(quote val) $(quote preresolved)) -- Add global scopes at compilation time (now), add macro scope at runtime (in the quotation). -- See the paper for details. let consts ← resolveGlobalName val -- extension of the paper algorithm: also store unique section variable names as top-level scopes -- so they can be captured and used inside the section, but not outside let sectionVars := resolveSectionVariable (← read).sectionVars val -- extension of the paper algorithm: resolve namespaces as well let namespaces ← resolveNamespaceCore (allowEmpty := true) val let preresolved := (consts ++ sectionVars).map (fun (n, projs) => Preresolved.decl n projs) ++ namespaces.map .namespace ++ preresolved let val := quote val -- `scp` is bound in stxQuot.expand `(Syntax.ident info $(quote rawVal) (addMacroScope mainModule $val scp) $(quote preresolved)) -- if antiquotation, insert contents as-is, else recurse | stx@(Syntax.node _ k _) => do if let some (k, _) := stx.antiquotKind? then if let some name := getAntiquotKindSpec? stx then tryAddSyntaxNodeKindInfo name k if isAntiquots stx && !isEscapedAntiquot (getCanonicalAntiquot stx) then let ks := antiquotKinds stx `(@TSyntax.raw $(quote <| ks.map (·.1)) $(getAntiquotTerm (getCanonicalAntiquot stx))) else if isTokenAntiquot stx && !isEscapedAntiquot stx then match stx[0] with | Syntax.atom _ val => `(Syntax.atom (SourceInfo.fromRef $(getAntiquotTerm stx) (canonical := true)) $(quote val)) | _ => throwErrorAt stx "expected token" else if isAntiquotSuffixSplice stx && !isEscapedAntiquot (getCanonicalAntiquot (getAntiquotSuffixSpliceInner stx)) then -- splices must occur in a `many` node throwErrorAt stx "unexpected antiquotation splice" else if isAntiquotSplice stx && !isEscapedAntiquot stx then throwErrorAt stx "unexpected antiquotation splice" else -- if escaped antiquotation, decrement by one escape level let stx := unescapeAntiquot stx let mut args := ArrayStxBuilder.empty let appendName := if (← getEnv).contains ``Array.append then ``Array.append else ``Array.appendCore for arg in stx.getArgs do if k == nullKind && isAntiquotSuffixSplice arg && !isEscapedAntiquot (getCanonicalAntiquot (getAntiquotSuffixSpliceInner arg)) then let antiquot := getAntiquotSuffixSpliceInner arg let ks := antiquotKinds antiquot |>.map (·.1) let val := getAntiquotTerm (getCanonicalAntiquot antiquot) args := args.append (appendName := appendName) <| ← match antiquotSuffixSplice? arg with | `optional => `(match Option.map (@TSyntax.raw $(quote ks)) $val:term with | some x => Array.empty.push x | none => Array.empty) | `many => `(@TSyntaxArray.raw $(quote ks) $val) | `sepBy => let sep := quote <| getSepFromSplice arg `(@TSepArray.elemsAndSeps $(quote ks) $sep $val) | k => throwErrorAt arg "invalid antiquotation suffix splice kind '{k}'" else if k == nullKind && isAntiquotSplice arg && !isEscapedAntiquot arg then let k := antiquotSpliceKind? arg let (arg, bindLets) ← floatOutAntiquotTerms arg |>.run pure let inner ← (getAntiquotSpliceContents arg).mapM quoteSyntax let ids ← getAntiquotationIds arg if ids.isEmpty then throwErrorAt stx "antiquotation splice must contain at least one antiquotation" let arr ← match k with | `optional => `(match $[$ids:ident],* with | $[some $ids:ident],* => $(quote inner) | $[_%$ids],* => Array.empty) | _ => let arr ← ids[:ids.size-1].foldrM (fun id arr => `(Array.zip $id:ident $arr)) ids.back `(Array.map (fun $(← mkTuple ids) => $(inner[0]!)) $arr) let arr ← if k == `sepBy then `(mkSepArray $arr $(getSepStxFromSplice arg)) else pure arr let arr ← bindLets arr args := args.append (appendName := appendName) arr else do let arg ← quoteSyntax arg args := args.push arg args.mkNode k | Syntax.atom _ val => `(Syntax.atom info $(quote val)) | Syntax.missing => throwUnsupportedSyntax def addNamedQuotInfo (stx : Syntax) (k : SyntaxNodeKind) : TermElabM SyntaxNodeKind := do if stx.getNumArgs == 3 && stx[0].isAtom then let s := stx[0].getAtomVal if s.length > 3 then if let (some l, some r) := (stx[0].getPos? true, stx[0].getTailPos? true) then -- HACK: The atom is the string "`(foo|", so chop off the edges. let name := stx[0].setInfo <| .synthetic ⟨l.1 + 2⟩ ⟨r.1 - 1⟩ (canonical := true) tryAddSyntaxNodeKindInfo name k pure k def getQuotKind (stx : Syntax) : TermElabM SyntaxNodeKind := do match stx.getKind with | ``Parser.Command.quot => addNamedQuotInfo stx `command | ``Parser.Term.quot => addNamedQuotInfo stx `term | ``Parser.Tactic.quot => addNamedQuotInfo stx `tactic | ``Parser.Tactic.quotSeq => addNamedQuotInfo stx `tactic.seq | .str kind "quot" => addNamedQuotInfo stx kind | ``dynamicQuot => match ← elabParserName stx[1] with | .parser n _ => return n | .category c => return c | k => throwError "unexpected quotation kind {k}" def mkSyntaxQuotation (stx : Syntax) (kind : Name) : TermElabM Syntax := do /- Syntax quotations are monadic values depending on the current macro scope. For efficiency, we bind the macro scope once for each quotation, then build the syntax tree in a completely pure computation depending on this binding. Note that regular function calls do not introduce a new macro scope (i.e. we preserve referential transparency), so we can refer to this same `scp` inside `quoteSyntax` by including it literally in a syntax quotation. -/ `(Bind.bind MonadRef.mkInfoFromRefPos (fun info => Bind.bind getCurrMacroScope (fun scp => Bind.bind getMainModule (fun mainModule => Pure.pure (@TSyntax.mk $(quote kind) $stx))))) /- NOTE: It may seem like the newly introduced binding `scp` may accidentally capture identifiers in an antiquotation introduced by `quoteSyntax`. However, note that the syntax quotation above enjoys the same hygiene guarantees as anywhere else in Lean; that is, we implement hygienic quotations by making use of the hygienic quotation support of the bootstrapped Lean compiler! Aside: While this might sound "dangerous", it is in fact less reliant on a "chain of trust" than other bootstrapping parts of Lean: because this implementation itself never uses `scp` (or any other identifier) both inside and outside quotations, it can actually correctly be compiled by an unhygienic (but otherwise correct) implementation of syntax quotations. As long as it is then compiled again with the resulting executable (i.e. up to stage 2), the result is a correct hygienic implementation. In this sense the implementation is "self-stabilizing". It was in fact originally compiled by an unhygienic prototype implementation. -/ def stxQuot.expand (stx : Syntax) : TermElabM Syntax := do let stx := if stx.getNumArgs == 1 then stx[0] else stx let kind ← getQuotKind stx let stx ← quoteSyntax stx.getQuotContent mkSyntaxQuotation stx kind macro "elab_stx_quot" kind:ident : command => `(@[builtin_term_elab $kind:ident] def elabQuot : TermElab := adaptExpander stxQuot.expand) elab_stx_quot Parser.Term.quot elab_stx_quot Parser.Tactic.quot elab_stx_quot Parser.Tactic.quotSeq elab_stx_quot Parser.Term.dynamicQuot elab_stx_quot Parser.Command.quot /-! # match -/ /-- an "alternative" of patterns plus right-hand side -/ private abbrev Alt := List Term × Term /-- In a single match step, we match the first discriminant against the "head" of the first pattern of the first alternative. This datatype describes what kind of check this involves, which helps other patterns decide if they are covered by the same check and don't have to be checked again (see also `MatchResult`). -/ inductive HeadCheck where /-- match step that always succeeds: _, x, `($x), ... -/ | unconditional /-- match step based on kind and, optionally, arity of discriminant If `arity` is given, that number of new discriminants is introduced. `covered` patterns should then introduce the same number of new patterns. We actually check the arity at run time only in the case of `null` nodes since it should otherwise by implied by the node kind. without arity: `($x:k) with arity: any quotation without an antiquotation head pattern -/ | shape (k : List SyntaxNodeKind) (arity : Option Nat) /-- Match step that succeeds on `null` nodes of arity at least `numPrefix + numSuffix`, introducing discriminants for the first `numPrefix` children, one `null` node for those in between, and for the `numSuffix` last children. example: `([$x, $xs,*, $y]) is `slice 2 2` -/ | slice (numPrefix numSuffix : Nat) /-- other, complicated match step that will probably only cover identical patterns example: antiquotation splices `($[...]*) -/ | other (pat : Syntax) open HeadCheck /-- Describe whether a pattern is covered by a head check (induced by the pattern itself or a different pattern). -/ inductive MatchResult where /-- Pattern agrees with head check, remove and transform remaining alternative. If `exhaustive` is `false`, *also* include unchanged alternative in the "no" branch. -/ | covered (f : Alt → TermElabM Alt) (exhaustive : Bool) /-- Pattern disagrees with head check, include in "no" branch only -/ | uncovered /-- Pattern is not quite sure yet; include unchanged in both branches -/ | undecided open MatchResult /-- All necessary information on a pattern head. -/ structure HeadInfo where /-- check induced by the pattern -/ check : HeadCheck /-- compute compatibility of pattern with given head check -/ onMatch (taken : HeadCheck) : MatchResult /-- actually run the specified head check, with the discriminant bound to `__discr` -/ doMatch (yes : (newDiscrs : List Term) → TermElabM Term) (no : TermElabM Term) : TermElabM Term /-- Adapt alternatives that do not introduce new discriminants in `doMatch`, but are covered by those that do so. -/ private def noOpMatchAdaptPats : HeadCheck → Alt → Alt | shape _ (some sz), (pats, rhs) => (List.replicate sz (Unhygienic.run `(_)) ++ pats, rhs) | slice p s, (pats, rhs) => (List.replicate (p + 1 + s) (Unhygienic.run `(_)) ++ pats, rhs) | _, alt => alt private def adaptRhs (fn : Term → TermElabM Term) : Alt → TermElabM Alt | (pats, rhs) => return (pats, ← fn rhs) private partial def getHeadInfo (alt : Alt) : TermElabM HeadInfo := let pat := alt.fst.head! let unconditionally rhsFn := pure { check := unconditional, doMatch := fun yes _ => yes [], onMatch := fun taken => covered (adaptRhs rhsFn ∘ noOpMatchAdaptPats taken) (taken matches unconditional) } -- quotation pattern if isQuot pat then do let quoted := getQuotContent pat if let some (k, _) := quoted.antiquotKind? then if let some name := getAntiquotKindSpec? quoted then tryAddSyntaxNodeKindInfo name k if quoted.isAtom || quoted.isOfKind `patternIgnore then -- We assume that atoms are uniquely determined by the node kind and never have to be checked unconditionally pure else if quoted.isTokenAntiquot then unconditionally (`(have $(quoted.getAntiquotTerm) := __discr; $(·))) else if isAntiquots quoted && !isEscapedAntiquot (getCanonicalAntiquot quoted) then -- quotation contains a single antiquotation let (ks, pseudoKinds) := antiquotKinds quoted |>.unzip let rhsFn := match getAntiquotTerm (getCanonicalAntiquot quoted) with | `(_) => pure | `($id:ident) => fun stx => `(have $id := @TSyntax.mk $(quote ks) __discr; $(stx)) | anti => fun _ => throwErrorAt anti "unsupported antiquotation kind in pattern" -- Antiquotation kinds like `$id:ident` influence the parser, but also need to be considered by -- `match` (but not by quotation terms). For example, `($id:ident) and `($e) are not -- distinguishable without checking the kind of the node to be captured. Note that some -- antiquotations like the latter one for terms do not correspond to any actual node kind, -- so we would only check for `ident` here. -- -- if stx.isOfKind `ident then -- let id := stx; let e := stx; ... -- else -- let e := stx; ... if (getCanonicalAntiquot quoted)[3].isNone || pseudoKinds.all id then unconditionally rhsFn else pure { check := shape ks none, onMatch := fun | other _ => undecided | taken@(shape ks' sz) => if ks' == ks then covered (adaptRhs rhsFn ∘ noOpMatchAdaptPats taken) (exhaustive := sz.isNone) else uncovered | _ => uncovered, doMatch := fun yes no => do let cond ← ks.foldlM (fun cond k => `(or $cond (Syntax.isOfKind __discr $(quote k)))) (← `(false)) `(cond $cond $(← yes []) $(← no)), } else if isAntiquotSuffixSplice quoted then throwErrorAt quoted "unexpected antiquotation splice" else if isAntiquotSplice quoted then throwErrorAt quoted "unexpected antiquotation splice" else if quoted.getArgs.size == 1 && isAntiquotSuffixSplice quoted[0] then let inner := getAntiquotSuffixSpliceInner quoted[0] let anti := getAntiquotTerm (getCanonicalAntiquot inner) let ks := antiquotKinds inner |>.map (·.1) unconditionally fun rhs => match antiquotSuffixSplice? quoted[0] with | `optional => `(have $anti := Option.map (@TSyntax.mk $(quote ks)) (Syntax.getOptional? __discr); $rhs) | `many => `(have $anti := @TSyntaxArray.mk $(quote ks) (Syntax.getArgs __discr); $rhs) | `sepBy => `(have $anti := @TSepArray.mk $(quote ks) $(quote <| getSepFromSplice quoted[0]) (Syntax.getArgs __discr); $rhs) | k => throwErrorAt quoted "invalid antiquotation suffix splice kind '{k}'" else if quoted.getArgs.size == 1 && isAntiquotSplice quoted[0] then pure { check := other pat, onMatch := fun | other pat' => if pat' == pat then covered pure (exhaustive := true) else undecided | _ => undecided, doMatch := fun yes no => do let splice := quoted[0] let k := antiquotSpliceKind? splice let contents := getAntiquotSpliceContents splice let ids ← getAntiquotationIds splice let yes ← yes [] let no ← no match k with | `optional => let nones := mkArray ids.size (← `(none)) `(let_delayed yes _ $ids* := $yes; if __discr.isNone then yes () $[ $nones]* else match __discr with | `($(mkNullNode contents)) => yes () $[ (some $ids)]* | _ => $no) | _ => let mut discrs ← `(Syntax.getArgs __discr) if k == `sepBy then discrs ← `(Array.getSepElems $discrs) let tuple ← mkTuple ids let mut yes := yes let resId ← match ids with | #[id] => pure id | _ => for id in ids do yes ← `(have $id := tuples.map (fun $tuple => $id); $yes) `(tuples) let contents := if contents.size == 1 then contents[0]! else mkNullNode contents -- We use `no_error_if_unused%` in auxiliary `match`-syntax to avoid spurious error messages, -- the outer `match` is checking for unused alternatives `(match ($(discrs).sequenceMap fun | `($contents) => no_error_if_unused% some $tuple | _ => no_error_if_unused% none) with | some $resId => $yes | none => $no) } else if let some idx := quoted.getArgs.findIdx? (fun arg => isAntiquotSuffixSplice arg || isAntiquotSplice arg) then do /- patterns of the form `match __discr, ... with | `(pat_0 ... pat_(idx-1) $[...]* pat_(idx+1) ...), ...` transform to ``` if __discr.getNumArgs >= $quoted.getNumArgs - 1 then match __discr[0], ..., __discr[idx-1], mkNullNode (__discr.getArgs.extract idx (__discr.getNumArgs - $numSuffix))), ..., __discr[quoted.getNumArgs - 1] with | `(pat_0), ... `(pat_(idx-1)), `($[...])*, `(pat_(idx+1)), ... ``` -/ let numSuffix := quoted.getNumArgs - 1 - idx pure { check := slice idx numSuffix onMatch := fun | other _ => undecided | slice p s => if p == idx && s == numSuffix then let argPats := quoted.getArgs.mapIdx fun i arg => let arg := if (i : Nat) == idx then mkNullNode #[arg] else arg Unhygienic.run `(`($(arg))) covered (fun (pats, rhs) => pure (argPats.toList ++ pats, rhs)) (exhaustive := true) else uncovered | _ => uncovered doMatch := fun yes no => do let prefixDiscrs ← (List.range idx).mapM (`(Syntax.getArg __discr $(quote ·))) let sliceDiscr ← `(mkNullNode (__discr.getArgs.extract $(quote idx) (__discr.getNumArgs - $(quote numSuffix)))) let suffixDiscrs ← (List.range numSuffix).mapM fun i => `(Syntax.getArg __discr (__discr.getNumArgs - $(quote (numSuffix - i)))) `(ite (GE.ge __discr.getNumArgs $(quote (quoted.getNumArgs - 1))) $(← yes (prefixDiscrs ++ sliceDiscr :: suffixDiscrs)) $(← no)) } else -- not an antiquotation, or an escaped antiquotation: match head shape let quoted := unescapeAntiquot quoted let kind := quoted.getKind let lit := isLitKind kind let argPats := quoted.getArgs.map fun arg => Unhygienic.run `(`($(arg))) pure { check := -- Atoms are matched only within literals because it would be a waste of time for keywords -- such as `if` and `then` and would blow up the generated code. -- See also `elabMatchSyntax` docstring below. if quoted.isIdent || lit then -- NOTE: We could make this case split more precise by refining `HeadCheck`, -- but matching on literals is quite rare. other quoted else shape [kind] argPats.size, onMatch := fun | other stx' => if (quoted.isIdent || lit) && quoted == stx' then covered pure (exhaustive := true) else uncovered | shape ks sz => if ks == [kind] && sz == argPats.size then covered (fun (pats, rhs) => pure (argPats.toList ++ pats, rhs)) (exhaustive := true) else uncovered | _ => uncovered, doMatch := fun yes no => do let (cond, newDiscrs) ← if lit then let cond ← `(Syntax.matchesLit __discr $(quote kind) $(quote (isLit? kind quoted).get!)) pure (cond, []) else let cond ← match kind with | `null => `(Syntax.matchesNull __discr $(quote argPats.size)) | `ident => `(Syntax.matchesIdent __discr $(quote quoted.getId)) | _ => `(Syntax.isOfKind __discr $(quote kind)) let newDiscrs ← (List.range argPats.size).mapM fun i => `(Syntax.getArg __discr $(quote i)) pure (cond, newDiscrs) `(ite (Eq $cond true) $(← yes newDiscrs) $(← no)) } else match pat with | `(_) => unconditionally pure | `($id:ident) => unconditionally (`(have $id := __discr; $(·))) | `($id:ident@$pat) => do let info ← getHeadInfo (pat::alt.1.tail!, alt.2) return { info with onMatch := fun taken => match info.onMatch taken with | covered f exh => covered (fun alt => f alt >>= adaptRhs (`(have $id := __discr; $(·)))) exh | r => r } | _ => throwErrorAt pat "match (syntax) : unexpected pattern kind {pat}" /-- Bind right-hand side to new `let_delayed` decl in order to prevent code duplication -/ private def deduplicate (floatedLetDecls : Array Syntax) : Alt → TermElabM (Array Syntax × Alt) -- NOTE: new macro scope so that introduced bindings do not collide | (pats, rhs) => do if let `($_:ident $[ $_:ident]*) := rhs then -- looks simple enough/created by this function, skip return (floatedLetDecls, (pats, rhs)) withFreshMacroScope do match (← getPatternsVars pats.toArray) with | #[] => -- no antiquotations => introduce Unit parameter to preserve evaluation order let rhs' ← `(rhs Unit.unit) pure (floatedLetDecls.push (← `(letDecl|rhs _ := $rhs)), (pats, rhs')) | vars => let rhs' ← `(rhs $vars*) pure (floatedLetDecls.push (← `(letDecl|rhs $vars:ident* := $rhs)), (pats, rhs')) private partial def compileStxMatch (discrs : List Term) (alts : List Alt) : TermElabM Syntax := do trace[Elab.match_syntax] "match {discrs} with {alts}" match discrs, alts with | [], ([], rhs)::_ => pure rhs -- nothing left to match | _, [] => logError "non-exhaustive 'match' (syntax)" pure Syntax.missing | discr::discrs, alt::alts => do let info ← getHeadInfo alt let alts ← (alt::alts).mapM fun alt => return ((← getHeadInfo alt).onMatch info.check, alt) let mut yesAlts := #[] let mut undecidedAlts := #[] let mut nonExhaustiveAlts := #[] let mut floatedLetDecls := #[] for (x, alt') in alts do let mut alt' := alt' match x with | covered f exh => -- we can only factor out a common check if there are no undecided patterns in between; -- otherwise we would change the order of alternatives if undecidedAlts.isEmpty then yesAlts ← yesAlts.push <$> f (alt'.1.tail!, alt'.2) if !exh then nonExhaustiveAlts := nonExhaustiveAlts.push alt' else (floatedLetDecls, alt') ← deduplicate floatedLetDecls alt' undecidedAlts := undecidedAlts.push alt' nonExhaustiveAlts := nonExhaustiveAlts.push alt' | undecided => (floatedLetDecls, alt') ← deduplicate floatedLetDecls alt' undecidedAlts := undecidedAlts.push alt' nonExhaustiveAlts := nonExhaustiveAlts.push alt' | uncovered => nonExhaustiveAlts := nonExhaustiveAlts.push alt' let mut stx ← info.doMatch (yes := fun newDiscrs => do let mut yesAlts := yesAlts if !undecidedAlts.isEmpty then -- group undecided alternatives in a new default case `| discr2, ... => match discr, discr2, ... with ...` let vars ← discrs.mapM fun _ => withFreshMacroScope `(__discr) let pats := List.replicate newDiscrs.length (Unhygienic.run `(_)) ++ vars let alts ← undecidedAlts.mapM fun alt => `(matchAltExpr| | $(alt.1.toArray),* => no_error_if_unused% $(alt.2)) let rhs ← `(match __discr, $[$(vars.toArray):term],* with $alts:matchAlt*) yesAlts := yesAlts.push (pats, rhs) withFreshMacroScope $ compileStxMatch (newDiscrs ++ discrs) yesAlts.toList) (no := withFreshMacroScope $ compileStxMatch (discr::discrs) nonExhaustiveAlts.toList) for d in floatedLetDecls do stx ← `(let_delayed $d:letDecl; $stx) `(have __discr := $discr; $stx) | _, _ => unreachable! abbrev IdxSet := HashSet Nat private partial def hasNoErrorIfUnused : Syntax → Bool | `(no_error_if_unused% $_) => true | `(clear% $_; $body) => hasNoErrorIfUnused body | _ => false /-- Given `rhss` the right-hand-sides of a `match`-syntax notation, We tag them with with fresh identifiers `alt_idx`. We use them to detect whether an alternative has been used or not. The result is a triple `(altIdxMap, ignoreIfUnused, rhssNew)` where - `altIdxMap` is a mapping from the `alt_idx` identifiers to right-hand-side indices. That is, the map contains the entry `alt_idx ↦ i` if `alt_idx` was used to mark `rhss[i]`. - `i ∈ ignoreIfUnused` if `rhss[i]` is marked with `no_error_if_unused%` - `rhssNew` is the updated array of right-hand-sides. -/ private def markRhss (rhss : Array Term) : TermElabM (NameMap Nat × IdxSet × Array Term) := do let mut altIdxMap : NameMap Nat := {} let mut ignoreIfUnused : IdxSet := {} let mut rhssNew := #[] for rhs in rhss do if hasNoErrorIfUnused rhs then ignoreIfUnused := ignoreIfUnused.insert rhssNew.size let (idx, rhs) ← withFreshMacroScope do let idx ← `(alt_idx) let rhs ← `(alt_idx $rhs) return (idx, rhs) altIdxMap := altIdxMap.insert idx.getId rhssNew.size rhssNew := rhssNew.push rhs return (altIdxMap, ignoreIfUnused, rhssNew) /-- Given the mapping `idxMap` built using `markRhss`, and `stx` the resulting syntax after expanding `match`-syntax, return the pair `(stxNew, usedSet)`, where `stxNew` is `stx` after removing the `alt_idx` markers in `idxMap`, and `i ∈ usedSet` if `stx` contains an `alt_idx` s.t. `alt_idx ↦ i` is in `idxMap`. That is, `usedSet` contains the index of the used match-syntax right-hand-sides. -/ private partial def findUsedAlts (stx : Syntax) (altIdxMap : NameMap Nat) : TermElabM (Syntax × IdxSet) := do go stx |>.run {} where go (stx : Syntax) : StateRefT IdxSet TermElabM Syntax := do match stx with | `($id:ident $rhs:term) => if let some idx := altIdxMap.find? id.getId then modify fun s => s.insert idx return rhs | _ => pure () match stx with | .node info kind cs => return .node info kind (← cs.mapM go) | _ => return stx /-- Check whether `stx` has unused alternatives, and remove the auxiliary `alt_idx` markers from it (see `markRhss`). The parameter `alts` provides position information for alternatives. `altIdxMap` and `ignoreIfUnused` are the map and set built using `markRhss`. -/ private def checkUnusedAlts (stx : Syntax) (alts : Array Syntax) (altIdxMap : NameMap Nat) (ignoreIfUnused : IdxSet) : TermElabM Syntax := do let (stx, used) ← findUsedAlts stx altIdxMap for i in [:alts.size] do unless used.contains i || ignoreIfUnused.contains i do logErrorAt alts[i]! s!"redundant alternative #{i+1}" return stx def match_syntax.expand (stx : Syntax) : TermElabM Syntax := do match stx with | `(match $[$discrs:term],* with $[|%$alt $[$patss],* => $rhss]*) => do if !patss.any (·.any (fun | `($_@$pat) => pat.raw.isQuot | pat => pat.raw.isQuot)) then -- no quotations => fall back to regular `match` throwUnsupportedSyntax let (altIdxMap, ignoreIfUnused, rhss) ← markRhss rhss -- call `getQuotKind` on all the patterns, which adds -- term info for all `(foo| ...)` as a side effect patss.forM (·.forM fun ⟨pat⟩ => do if pat.isQuot then _ ← getQuotKind pat) let stx ← compileStxMatch discrs.toList (patss.map (·.toList) |>.zip rhss).toList let stx ← checkUnusedAlts stx alt altIdxMap ignoreIfUnused trace[Elab.match_syntax.result] "{stx}" return stx | _ => throwUnsupportedSyntax @[builtin_term_elab «match»] def elabMatchSyntax : TermElab := adaptExpander match_syntax.expand @[builtin_term_elab noErrorIfUnused] def elabNoErrorIfUnused : TermElab := fun stx expectedType? => match stx with | `(no_error_if_unused% $term) => elabTerm term expectedType? | _ => throwUnsupportedSyntax builtin_initialize registerTraceClass `Elab.match_syntax registerTraceClass `Elab.match_syntax.result end Lean.Elab.Term.Quotation
502199f89686c9f561277c08048d63b88d4a0b2c
90edd5cdcf93124fe15627f7304069fdce3442dd
/stage0/src/Lean/Aesop/RuleTac.lean
a4e2610638d25907fdfdf1b252275786ae9b24c7
[ "Apache-2.0" ]
permissive
JLimperg/lean4-aesop
8a9d9cd3ee484a8e67fda2dd9822d76708098712
5c4b9a3e05c32f69a4357c3047c274f4b94f9c71
refs/heads/master
1,689,415,944,104
1,627,383,284,000
1,627,383,284,000
377,536,770
0
0
null
null
null
null
UTF-8
Lean
false
false
2,280
lean
/- Copyright (c) 2021 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jannis Limperg -/ import Lean.Elab open Lean.Elab.Tactic open Lean.Meta namespace Lean.Aesop /- A `RuleTacDescr` is a 'recipe' for constructing the tactic used by a rule. When we serialise the rule set to an olean file, we serialise `RuleTacDescr`s because we can't (currently?) serialise the actual tactics. -/ inductive RuleTacDescr | applyConst (decl : Name) | tactic (decl : Name) deriving Inhabited, BEq /- A `RuleTac` bundles a `RuleTacDescr` and the tactic that was computed from the description. -/ structure RuleTac where tac : TacticM Unit descr : RuleTacDescr deriving Inhabited namespace RuleTac end RuleTac abbrev RuleTacBuilder := MetaM RuleTac namespace RuleTacBuilder /- Convenience for evalTacticUnsafe. -/ private abbrev TacticType := TacticM Unit def checkTacticM (decl : Name) : MetaM Unit := do let info ← getConstInfo decl unless (← isDefEq info.type (mkConst ``TacticType)) do throwError "aesop: {decl} was expected to have type\n TacticM Unit\nbut has type\n {info.type}" unsafe def evalTacticConstUnsafe (decl : Name) : TacticM Unit := do checkTacticM decl -- TODO Maybe we can elide the above check. We already check the type -- when we register the tactic. let tac ← evalConst TacticType decl tac @[implementedBy evalTacticConstUnsafe] constant evalTacticConst : Name → TacticM Unit -- I think the above use of `evalConst` is safe because we call it at a concrete -- type, making sure that the constant actually has that type. def tactic (decl : Name) : RuleTacBuilder := do checkTacticM decl return { tac := evalTacticConst decl, descr := RuleTacDescr.tactic decl } def apply (decl : Name) : RuleTacBuilder := return { tac := liftMetaTactic λ goal => do Lean.Meta.apply goal (← mkConstWithFreshMVarLevels decl) -- TODO Go via apply tactic syntax to ensure intuitive behaviour? descr := RuleTacDescr.applyConst decl } end RuleTacBuilder namespace RuleTacDescr def toRuleTac : RuleTacDescr → MetaM RuleTac | applyConst decl => RuleTacBuilder.apply decl | tactic decl => RuleTacBuilder.tactic decl end RuleTacDescr end Lean.Aesop
ff4a0d9db7f2b439127d8a9ddf810c5f99a501cb
9dc8cecdf3c4634764a18254e94d43da07142918
/src/measure_theory/measure/stieltjes.lean
77600cdcf4d72b89f6683eb3da59e65da79a894e
[ "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
16,630
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov, Sébastien Gouëzel -/ import measure_theory.constructions.borel_space /-! # Stieltjes measures on the real line Consider a function `f : ℝ → ℝ` which is monotone and right-continuous. Then one can define a corrresponding measure, giving mass `f b - f a` to the interval `(a, b]`. ## Main definitions * `stieltjes_function` is a structure containing a function from `ℝ → ℝ`, together with the assertions that it is monotone and right-continuous. To `f : stieltjes_function`, one associates a Borel measure `f.measure`. * `f.left_lim x` is the limit of `f` to the left of `x`. * `f.measure_Ioc` asserts that `f.measure (Ioc a b) = of_real (f b - f a)` * `f.measure_Ioo` asserts that `f.measure (Ioo a b) = of_real (f.left_lim b - f a)`. * `f.measure_Icc` and `f.measure_Ico` are analogous. -/ noncomputable theory open classical set filter open ennreal (of_real) open_locale big_operators ennreal nnreal topological_space measure_theory /-! ### Basic properties of Stieltjes functions -/ /-- Bundled monotone right-continuous real functions, used to construct Stieltjes measures. -/ structure stieltjes_function := (to_fun : ℝ → ℝ) (mono' : monotone to_fun) (right_continuous' : ∀ x, continuous_within_at to_fun (Ici x) x) namespace stieltjes_function instance : has_coe_to_fun stieltjes_function (λ _, ℝ → ℝ) := ⟨to_fun⟩ initialize_simps_projections stieltjes_function (to_fun → apply) variable (f : stieltjes_function) lemma mono : monotone f := f.mono' lemma right_continuous (x : ℝ) : continuous_within_at f (Ici x) x := f.right_continuous' x /-- The limit of a Stieltjes function to the left of `x` (it exists by monotonicity). The fact that it is indeed a left limit is asserted in `tendsto_left_lim` -/ @[irreducible] def left_lim (x : ℝ) := Sup (f '' (Iio x)) lemma tendsto_left_lim (x : ℝ) : tendsto f (𝓝[<] x) (𝓝 (f.left_lim x)) := by { rw left_lim, exact f.mono.tendsto_nhds_within_Iio x } lemma left_lim_le {x y : ℝ} (h : x ≤ y) : f.left_lim x ≤ f y := begin apply le_of_tendsto (f.tendsto_left_lim x), filter_upwards [self_mem_nhds_within] with _ hz using (f.mono (le_of_lt hz)).trans (f.mono h), end lemma le_left_lim {x y : ℝ} (h : x < y) : f x ≤ f.left_lim y := begin apply ge_of_tendsto (f.tendsto_left_lim y), apply mem_nhds_within_Iio_iff_exists_Ioo_subset.2 ⟨x, h, _⟩, assume z hz, exact f.mono hz.1.le, end lemma left_lim_le_left_lim {x y : ℝ} (h : x ≤ y) : f.left_lim x ≤ f.left_lim y := begin rcases eq_or_lt_of_le h with rfl|hxy, { exact le_rfl }, { exact (f.left_lim_le le_rfl).trans (f.le_left_lim hxy) } end /-- The identity of `ℝ` as a Stieltjes function, used to construct Lebesgue measure. -/ @[simps] protected def id : stieltjes_function := { to_fun := id, mono' := λ x y, id, right_continuous' := λ x, continuous_within_at_id } @[simp] lemma id_left_lim (x : ℝ) : stieltjes_function.id.left_lim x = x := tendsto_nhds_unique (stieltjes_function.id.tendsto_left_lim x) $ (continuous_at_id).tendsto.mono_left nhds_within_le_nhds instance : inhabited stieltjes_function := ⟨stieltjes_function.id⟩ /-! ### The outer measure associated to a Stieltjes function -/ /-- Length of an interval. This is the largest monotone function which correctly measures all intervals. -/ def length (s : set ℝ) : ℝ≥0∞ := ⨅a b (h : s ⊆ Ioc a b), of_real (f b - f a) @[simp] lemma length_empty : f.length ∅ = 0 := nonpos_iff_eq_zero.1 $ infi_le_of_le 0 $ infi_le_of_le 0 $ by simp @[simp] lemma length_Ioc (a b : ℝ) : f.length (Ioc a b) = of_real (f b - f a) := begin refine le_antisymm (infi_le_of_le a $ infi₂_le b subset.rfl) (le_infi $ λ a', le_infi $ λ b', le_infi $ λ h, ennreal.coe_le_coe.2 _), cases le_or_lt b a with ab ab, { rw real.to_nnreal_of_nonpos (sub_nonpos.2 (f.mono ab)), apply zero_le, }, cases (Ioc_subset_Ioc_iff ab).1 h with h₁ h₂, exact real.to_nnreal_le_to_nnreal (sub_le_sub (f.mono h₁) (f.mono h₂)) end lemma length_mono {s₁ s₂ : set ℝ} (h : s₁ ⊆ s₂) : f.length s₁ ≤ f.length s₂ := infi_mono $ λ a, binfi_mono $ λ b, h.trans open measure_theory /-- The Stieltjes outer measure associated to a Stieltjes function. -/ protected def outer : outer_measure ℝ := outer_measure.of_function f.length f.length_empty lemma outer_le_length (s : set ℝ) : f.outer s ≤ f.length s := outer_measure.of_function_le _ /-- If a compact interval `[a, b]` is covered by a union of open interval `(c i, d i)`, then `f b - f a ≤ ∑ f (d i) - f (c i)`. This is an auxiliary technical statement to prove the same statement for half-open intervals, the point of the current statement being that one can use compactness to reduce it to a finite sum, and argue by induction on the size of the covering set. -/ lemma length_subadditive_Icc_Ioo {a b : ℝ} {c d : ℕ → ℝ} (ss : Icc a b ⊆ ⋃ i, Ioo (c i) (d i)) : of_real (f b - f a) ≤ ∑' i, of_real (f (d i) - f (c i)) := begin suffices : ∀ (s:finset ℕ) b (cv : Icc a b ⊆ ⋃ i ∈ (↑s:set ℕ), Ioo (c i) (d i)), (of_real (f b - f a) : ℝ≥0∞) ≤ ∑ i in s, of_real (f (d i) - f (c i)), { rcases is_compact_Icc.elim_finite_subcover_image (λ (i : ℕ) (_ : i ∈ univ), @is_open_Ioo _ _ _ _ (c i) (d i)) (by simpa using ss) with ⟨s, su, hf, hs⟩, have e : (⋃ i ∈ (↑hf.to_finset:set ℕ), Ioo (c i) (d i)) = (⋃ i ∈ s, Ioo (c i) (d i)), by simp only [ext_iff, exists_prop, finset.set_bUnion_coe, mem_Union, forall_const, iff_self, finite.mem_to_finset], rw ennreal.tsum_eq_supr_sum, refine le_trans _ (le_supr _ hf.to_finset), exact this hf.to_finset _ (by simpa only [e]) }, clear ss b, refine λ s, finset.strong_induction_on s (λ s IH b cv, _), cases le_total b a with ab ab, { rw ennreal.of_real_eq_zero.2 (sub_nonpos.2 (f.mono ab)), exact zero_le _, }, have := cv ⟨ab, le_rfl⟩, simp at this, rcases this with ⟨i, is, cb, bd⟩, rw [← finset.insert_erase is] at cv ⊢, rw [finset.coe_insert, bUnion_insert] at cv, rw [finset.sum_insert (finset.not_mem_erase _ _)], refine le_trans _ (add_le_add_left (IH _ (finset.erase_ssubset is) (c i) _) _), { refine le_trans (ennreal.of_real_le_of_real _) ennreal.of_real_add_le, rw sub_add_sub_cancel, exact sub_le_sub_right (f.mono bd.le) _ }, { rintro x ⟨h₁, h₂⟩, refine (cv ⟨h₁, le_trans h₂ (le_of_lt cb)⟩).resolve_left (mt and.left (not_lt_of_le h₂)) } end @[simp] lemma outer_Ioc (a b : ℝ) : f.outer (Ioc a b) = of_real (f b - f a) := begin /- It suffices to show that, if `(a, b]` is covered by sets `s i`, then `f b - f a` is bounded by `∑ f.length (s i) + ε`. The difficulty is that `f.length` is expressed in terms of half-open intervals, while we would like to have a compact interval covered by open intervals to use compactness and finite sums, as provided by `length_subadditive_Icc_Ioo`. The trick is to use the right-continuity of `f`. If `a'` is close enough to `a` on its right, then `[a', b]` is still covered by the sets `s i` and moreover `f b - f a'` is very close to `f b - f a` (up to `ε/2`). Also, by definition one can cover `s i` by a half-closed interval `(p i, q i]` with `f`-length very close to that of `s i` (within a suitably small `ε' i`, say). If one moves `q i` very slightly to the right, then the `f`-length will change very little by right continuity, and we will get an open interval `(p i, q' i)` covering `s i` with `f (q' i) - f (p i)` within `ε' i` of the `f`-length of `s i`. -/ refine le_antisymm (by { rw ← f.length_Ioc, apply outer_le_length }) (le_infi₂ $ λ s hs, ennreal.le_of_forall_pos_le_add $ λ ε εpos h, _), let δ := ε / 2, have δpos : 0 < (δ : ℝ≥0∞), by simpa using εpos.ne', rcases ennreal.exists_pos_sum_of_countable δpos.ne' ℕ with ⟨ε', ε'0, hε⟩, obtain ⟨a', ha', aa'⟩ : ∃ a', f a' - f a < δ ∧ a < a', { have A : continuous_within_at (λ r, f r - f a) (Ioi a) a, { refine continuous_within_at.sub _ continuous_within_at_const, exact (f.right_continuous a).mono Ioi_subset_Ici_self }, have B : f a - f a < δ, by rwa [sub_self, nnreal.coe_pos, ← ennreal.coe_pos], exact (((tendsto_order.1 A).2 _ B).and self_mem_nhds_within).exists }, have : ∀ i, ∃ p:ℝ×ℝ, s i ⊆ Ioo p.1 p.2 ∧ (of_real (f p.2 - f p.1) : ℝ≥0∞) < f.length (s i) + ε' i, { intro i, have := (ennreal.lt_add_right ((ennreal.le_tsum i).trans_lt h).ne (ennreal.coe_ne_zero.2 (ε'0 i).ne')), conv at this { to_lhs, rw length }, simp only [infi_lt_iff, exists_prop] at this, rcases this with ⟨p, q', spq, hq'⟩, have : continuous_within_at (λ r, of_real (f r - f p)) (Ioi q') q', { apply ennreal.continuous_of_real.continuous_at.comp_continuous_within_at, refine continuous_within_at.sub _ continuous_within_at_const, exact (f.right_continuous q').mono Ioi_subset_Ici_self }, rcases (((tendsto_order.1 this).2 _ hq').and self_mem_nhds_within).exists with ⟨q, hq, q'q⟩, exact ⟨⟨p, q⟩, spq.trans (Ioc_subset_Ioo_right q'q), hq⟩ }, choose g hg using this, have I_subset : Icc a' b ⊆ ⋃ i, Ioo (g i).1 (g i).2 := calc Icc a' b ⊆ Ioc a b : λ x hx, ⟨aa'.trans_le hx.1, hx.2⟩ ... ⊆ ⋃ i, s i : hs ... ⊆ ⋃ i, Ioo (g i).1 (g i).2 : Union_mono (λ i, (hg i).1), calc of_real (f b - f a) = of_real ((f b - f a') + (f a' - f a)) : by rw sub_add_sub_cancel ... ≤ of_real (f b - f a') + of_real (f a' - f a) : ennreal.of_real_add_le ... ≤ (∑' i, of_real (f (g i).2 - f (g i).1)) + of_real δ : add_le_add (f.length_subadditive_Icc_Ioo I_subset) (ennreal.of_real_le_of_real ha'.le) ... ≤ (∑' i, (f.length (s i) + ε' i)) + δ : add_le_add (ennreal.tsum_le_tsum (λ i, (hg i).2.le)) (by simp only [ennreal.of_real_coe_nnreal, le_rfl]) ... = (∑' i, f.length (s i)) + (∑' i, ε' i) + δ : by rw [ennreal.tsum_add] ... ≤ (∑' i, f.length (s i)) + δ + δ : add_le_add (add_le_add le_rfl hε.le) le_rfl ... = ∑' (i : ℕ), f.length (s i) + ε : by simp [add_assoc, ennreal.add_halves] end lemma measurable_set_Ioi {c : ℝ} : measurable_set[f.outer.caratheodory] (Ioi c) := begin apply outer_measure.of_function_caratheodory (λ t, _), refine le_infi (λ a, le_infi (λ b, le_infi (λ h, _))), refine le_trans (add_le_add (f.length_mono $ inter_subset_inter_left _ h) (f.length_mono $ diff_subset_diff_left h)) _, cases le_total a c with hac hac; cases le_total b c with hbc hbc, { simp only [Ioc_inter_Ioi, f.length_Ioc, hac, sup_eq_max, hbc, le_refl, Ioc_eq_empty, max_eq_right, min_eq_left, Ioc_diff_Ioi, f.length_empty, zero_add, not_lt] }, { simp only [hac, hbc, Ioc_inter_Ioi, Ioc_diff_Ioi, f.length_Ioc, min_eq_right, sup_eq_max, ←ennreal.of_real_add, f.mono hac, f.mono hbc, sub_nonneg, sub_add_sub_cancel, le_refl, max_eq_right] }, { simp only [hbc, le_refl, Ioc_eq_empty, Ioc_inter_Ioi, min_eq_left, Ioc_diff_Ioi, f.length_empty, zero_add, or_true, le_sup_iff, f.length_Ioc, not_lt] }, { simp only [hac, hbc, Ioc_inter_Ioi, Ioc_diff_Ioi, f.length_Ioc, min_eq_right, sup_eq_max, le_refl, Ioc_eq_empty, add_zero, max_eq_left, f.length_empty, not_lt] } end theorem outer_trim : f.outer.trim = f.outer := begin refine le_antisymm (λ s, _) (outer_measure.le_trim _), rw outer_measure.trim_eq_infi, refine le_infi (λ t, le_infi $ λ ht, ennreal.le_of_forall_pos_le_add $ λ ε ε0 h, _), rcases ennreal.exists_pos_sum_of_countable (ennreal.coe_pos.2 ε0).ne' ℕ with ⟨ε', ε'0, hε⟩, refine le_trans _ (add_le_add_left (le_of_lt hε) _), rw ← ennreal.tsum_add, choose g hg using show ∀ i, ∃ s, t i ⊆ s ∧ measurable_set s ∧ f.outer s ≤ f.length (t i) + of_real (ε' i), { intro i, have := (ennreal.lt_add_right ((ennreal.le_tsum i).trans_lt h).ne (ennreal.coe_pos.2 (ε'0 i)).ne'), conv at this {to_lhs, rw length}, simp only [infi_lt_iff] at this, rcases this with ⟨a, b, h₁, h₂⟩, rw ← f.outer_Ioc at h₂, exact ⟨_, h₁, measurable_set_Ioc, le_of_lt $ by simpa using h₂⟩ }, simp at hg, apply infi_le_of_le (Union g) _, apply infi_le_of_le (ht.trans $ Union_mono (λ i, (hg i).1)) _, apply infi_le_of_le (measurable_set.Union (λ i, (hg i).2.1)) _, exact le_trans (f.outer.Union _) (ennreal.tsum_le_tsum $ λ i, (hg i).2.2) end lemma borel_le_measurable : borel ℝ ≤ f.outer.caratheodory := begin rw borel_eq_generate_from_Ioi, refine measurable_space.generate_from_le _, simp [f.measurable_set_Ioi] { contextual := tt } end /-! ### The measure associated to a Stieltjes function -/ /-- The measure associated to a Stieltjes function, giving mass `f b - f a` to the interval `(a, b]`. -/ @[irreducible] protected def measure : measure ℝ := { to_outer_measure := f.outer, m_Union := λ s hs, f.outer.Union_eq_of_caratheodory $ λ i, f.borel_le_measurable _ (hs i), trimmed := f.outer_trim } @[simp] lemma measure_Ioc (a b : ℝ) : f.measure (Ioc a b) = of_real (f b - f a) := by { rw stieltjes_function.measure, exact f.outer_Ioc a b } @[simp] lemma measure_singleton (a : ℝ) : f.measure {a} = of_real (f a - f.left_lim a) := begin obtain ⟨u, u_mono, u_lt_a, u_lim⟩ : ∃ (u : ℕ → ℝ), strict_mono u ∧ (∀ (n : ℕ), u n < a) ∧ tendsto u at_top (𝓝 a) := exists_seq_strict_mono_tendsto a, have A : {a} = ⋂ n, Ioc (u n) a, { refine subset.antisymm (λ x hx, by simp [mem_singleton_iff.1 hx, u_lt_a]) (λ x hx, _), simp at hx, have : a ≤ x := le_of_tendsto' u_lim (λ n, (hx n).1.le), simp [le_antisymm this (hx 0).2] }, have L1 : tendsto (λ n, f.measure (Ioc (u n) a)) at_top (𝓝 (f.measure {a})), { rw A, refine tendsto_measure_Inter (λ n, measurable_set_Ioc) (λ m n hmn, _) _, { exact Ioc_subset_Ioc (u_mono.monotone hmn) le_rfl }, { exact ⟨0, by simpa only [measure_Ioc] using ennreal.of_real_ne_top⟩ } }, have L2 : tendsto (λ n, f.measure (Ioc (u n) a)) at_top (𝓝 (of_real (f a - f.left_lim a))), { simp only [measure_Ioc], have : tendsto (λ n, f (u n)) at_top (𝓝 (f.left_lim a)), { apply (f.tendsto_left_lim a).comp, exact tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ u_lim (eventually_of_forall (λ n, u_lt_a n)) }, exact ennreal.continuous_of_real.continuous_at.tendsto.comp (tendsto_const_nhds.sub this) }, exact tendsto_nhds_unique L1 L2 end @[simp] lemma measure_Icc (a b : ℝ) : f.measure (Icc a b) = of_real (f b - f.left_lim a) := begin rcases le_or_lt a b with hab|hab, { have A : disjoint {a} (Ioc a b), by simp, simp [← Icc_union_Ioc_eq_Icc le_rfl hab, -singleton_union, ← ennreal.of_real_add, f.left_lim_le, measure_union A measurable_set_Ioc, f.mono hab] }, { simp only [hab, measure_empty, Icc_eq_empty, not_le], symmetry, simp [ennreal.of_real_eq_zero, f.le_left_lim hab] } end @[simp] lemma measure_Ioo {a b : ℝ} : f.measure (Ioo a b) = of_real (f.left_lim b - f a) := begin rcases le_or_lt b a with hab|hab, { simp only [hab, measure_empty, Ioo_eq_empty, not_lt], symmetry, simp [ennreal.of_real_eq_zero, f.left_lim_le hab] }, { have A : disjoint (Ioo a b) {b}, by simp, have D : f b - f a = (f b - f.left_lim b) + (f.left_lim b - f a), by abel, have := f.measure_Ioc a b, simp only [←Ioo_union_Icc_eq_Ioc hab le_rfl, measure_singleton, measure_union A (measurable_set_singleton b), Icc_self] at this, rw [D, ennreal.of_real_add, add_comm] at this, { simpa only [ennreal.add_right_inj ennreal.of_real_ne_top] }, { simp only [f.left_lim_le, sub_nonneg] }, { simp only [f.le_left_lim hab, sub_nonneg] } }, end @[simp] lemma measure_Ico (a b : ℝ) : f.measure (Ico a b) = of_real (f.left_lim b - f.left_lim a) := begin rcases le_or_lt b a with hab|hab, { simp only [hab, measure_empty, Ico_eq_empty, not_lt], symmetry, simp [ennreal.of_real_eq_zero, f.left_lim_le_left_lim hab] }, { have A : disjoint {a} (Ioo a b) := by simp, simp [← Icc_union_Ioo_eq_Ico le_rfl hab, -singleton_union, hab.ne, f.left_lim_le, measure_union A measurable_set_Ioo, f.le_left_lim hab, ← ennreal.of_real_add] } end end stieltjes_function
ee7f5b28c76b05bcc8a655141faaa907ce826e04
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/adjunction/mates.lean
25b793c6ac2696f6a9bea0d1a190cbaaa2a2d218
[ "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
10,494
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.adjunction.basic import category_theory.conj import category_theory.yoneda /-! # Mate of natural transformations This file establishes the bijection between the 2-cells L₁ R₁ C --→ D C ←-- D G ↓ ↗ ↓ H G ↓ ↘ ↓ H E --→ F E ←-- F L₂ R₂ where `L₁ ⊣ R₁` and `L₂ ⊣ R₂`, and shows that in the special case where `G,H` are identity then the bijection preserves and reflects isomorphisms (i.e. we have bijections `(L₂ ⟶ L₁) ≃ (R₁ ⟶ R₂)`, and if either side is an iso then the other side is as well). On its own, this bijection is not particularly useful but it includes a number of interesting cases as specializations. For instance, this generalises the fact that adjunctions are unique (since if `L₁ ≅ L₂` then we deduce `R₁ ≅ R₂`). Another example arises from considering the square representing that a functor `H` preserves products, in particular the morphism `HA ⨯ H- ⟶ H(A ⨯ -)`. Then provided `(A ⨯ -)` and `HA ⨯ -` have left adjoints (for instance if the relevant categories are cartesian closed), the transferred natural transformation is the exponential comparison morphism: `H(A ^ -) ⟶ HA ^ H-`. Furthermore if `H` has a left adjoint `L`, this morphism is an isomorphism iff its mate `L(HA ⨯ -) ⟶ A ⨯ L-` is an isomorphism, see https://ncatlab.org/nlab/show/Frobenius+reciprocity#InCategoryTheory. This also relates to Grothendieck's yoga of six operations, though this is not spelled out in mathlib: https://ncatlab.org/nlab/show/six+operations. -/ universes v₁ v₂ v₃ v₄ u₁ u₂ u₃ u₄ namespace category_theory open category variables {C : Type u₁} {D : Type u₂} [category.{v₁} C] [category.{v₂} D] section square variables {E : Type u₃} {F : Type u₄} [category.{v₃} E] [category.{v₄} F] variables {G : C ⥤ E} {H : D ⥤ F} {L₁ : C ⥤ D} {R₁ : D ⥤ C} {L₂ : E ⥤ F} {R₂ : F ⥤ E} variables (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) include adj₁ adj₂ /-- Suppose we have a square of functors (where the top and bottom are adjunctions `L₁ ⊣ R₁` and `L₂ ⊣ R₂` respectively). C ↔ D G ↓ ↓ H E ↔ F Then we have a bijection between natural transformations `G ⋙ L₂ ⟶ L₁ ⋙ H` and `R₁ ⋙ G ⟶ H ⋙ R₂`. This can be seen as a bijection of the 2-cells: L₁ R₁ C --→ D C ←-- D G ↓ ↗ ↓ H G ↓ ↘ ↓ H E --→ F E ←-- F L₂ R₂ Note that if one of the transformations is an iso, it does not imply the other is an iso. -/ def transfer_nat_trans : (G ⋙ L₂ ⟶ L₁ ⋙ H) ≃ (R₁ ⋙ G ⟶ H ⋙ R₂) := { to_fun := λ h, { app := λ X, adj₂.unit.app _ ≫ R₂.map (h.app _ ≫ H.map (adj₁.counit.app _)), naturality' := λ X Y f, begin dsimp, rw [assoc, ← R₂.map_comp, assoc, ← H.map_comp, ← adj₁.counit_naturality, H.map_comp, ←functor.comp_map L₁, ←h.naturality_assoc], simp, end }, inv_fun := λ h, { app := λ X, L₂.map (G.map (adj₁.unit.app _) ≫ h.app _) ≫ adj₂.counit.app _, naturality' := λ X Y f, begin dsimp, rw [← L₂.map_comp_assoc, ← G.map_comp_assoc, ← adj₁.unit_naturality, G.map_comp_assoc, ← functor.comp_map, h.naturality], simp, end }, left_inv := λ h, begin ext X, dsimp, simp only [L₂.map_comp, assoc, adj₂.counit_naturality, adj₂.left_triangle_components_assoc, ←functor.comp_map G L₂, h.naturality_assoc, functor.comp_map L₁, ←H.map_comp, adj₁.left_triangle_components], dsimp, simp, -- See library note [dsimp, simp]. end, right_inv := λ h, begin ext X, dsimp, simp [-functor.comp_map, ←functor.comp_map H, functor.comp_map R₁, -nat_trans.naturality, ←h.naturality, -functor.map_comp, ←functor.map_comp_assoc G, R₂.map_comp], end } lemma transfer_nat_trans_counit (f : G ⋙ L₂ ⟶ L₁ ⋙ H) (Y : D) : L₂.map ((transfer_nat_trans adj₁ adj₂ f).app _) ≫ adj₂.counit.app _ = f.app _ ≫ H.map (adj₁.counit.app Y) := by { erw functor.map_comp, simp } lemma unit_transfer_nat_trans (f : G ⋙ L₂ ⟶ L₁ ⋙ H) (X : C) : G.map (adj₁.unit.app X) ≫ (transfer_nat_trans adj₁ adj₂ f).app _ = adj₂.unit.app _ ≫ R₂.map (f.app _) := begin dsimp [transfer_nat_trans], rw [←adj₂.unit_naturality_assoc, ←R₂.map_comp, ← functor.comp_map G L₂, f.naturality_assoc, functor.comp_map, ← H.map_comp], dsimp, simp, -- See library note [dsimp, simp] end end square section self variables {L₁ L₂ L₃ : C ⥤ D} {R₁ R₂ R₃ : D ⥤ C} variables (adj₁ : L₁ ⊣ R₁) (adj₂ : L₂ ⊣ R₂) (adj₃ : L₃ ⊣ R₃) /-- Given two adjunctions `L₁ ⊣ R₁` and `L₂ ⊣ R₂` both between categories `C`, `D`, there is a bijection between natural transformations `L₂ ⟶ L₁` and natural transformations `R₁ ⟶ R₂`. This is defined as a special case of `transfer_nat_trans`, where the two "vertical" functors are identity. TODO: Generalise to when the two vertical functors are equivalences rather than being exactly `𝟭`. Furthermore, this bijection preserves (and reflects) isomorphisms, i.e. a transformation is an iso iff its image under the bijection is an iso, see eg `category_theory.transfer_nat_trans_self_iso`. This is in contrast to the general case `transfer_nat_trans` which does not in general have this property. -/ def transfer_nat_trans_self : (L₂ ⟶ L₁) ≃ (R₁ ⟶ R₂) := calc (L₂ ⟶ L₁) ≃ _ : (iso.hom_congr L₂.left_unitor L₁.right_unitor).symm ... ≃ _ : transfer_nat_trans adj₁ adj₂ ... ≃ (R₁ ⟶ R₂) : R₁.right_unitor.hom_congr R₂.left_unitor lemma transfer_nat_trans_self_counit (f : L₂ ⟶ L₁) (X) : L₂.map ((transfer_nat_trans_self adj₁ adj₂ f).app _) ≫ adj₂.counit.app X = f.app _ ≫ adj₁.counit.app X := begin dsimp [transfer_nat_trans_self], rw [id_comp, comp_id], have := transfer_nat_trans_counit adj₁ adj₂ (L₂.left_unitor.hom ≫ f ≫ L₁.right_unitor.inv) X, dsimp at this, rw this, simp, end lemma unit_transfer_nat_trans_self (f : L₂ ⟶ L₁) (X) : adj₁.unit.app _ ≫ (transfer_nat_trans_self adj₁ adj₂ f).app _ = adj₂.unit.app X ≫ functor.map _ (f.app _) := begin dsimp [transfer_nat_trans_self], rw [id_comp, comp_id], have := unit_transfer_nat_trans adj₁ adj₂ (L₂.left_unitor.hom ≫ f ≫ L₁.right_unitor.inv) X, dsimp at this, rw this, simp end @[simp] lemma transfer_nat_trans_self_id : transfer_nat_trans_self adj₁ adj₁ (𝟙 _) = 𝟙 _ := by { ext, dsimp [transfer_nat_trans_self, transfer_nat_trans], simp } -- See library note [dsimp, simp] @[simp] lemma transfer_nat_trans_self_symm_id : (transfer_nat_trans_self adj₁ adj₁).symm (𝟙 _) = 𝟙 _ := by { rw equiv.symm_apply_eq, simp } lemma transfer_nat_trans_self_comp (f g) : transfer_nat_trans_self adj₁ adj₂ f ≫ transfer_nat_trans_self adj₂ adj₃ g = transfer_nat_trans_self adj₁ adj₃ (g ≫ f) := begin ext, dsimp [transfer_nat_trans_self, transfer_nat_trans], simp only [id_comp, comp_id], rw [←adj₃.unit_naturality_assoc, ←R₃.map_comp, g.naturality_assoc, L₂.map_comp, assoc, adj₂.counit_naturality, adj₂.left_triangle_components_assoc, assoc], end lemma transfer_nat_trans_self_symm_comp (f g) : (transfer_nat_trans_self adj₂ adj₁).symm f ≫ (transfer_nat_trans_self adj₃ adj₂).symm g = (transfer_nat_trans_self adj₃ adj₁).symm (g ≫ f) := by { rw [equiv.eq_symm_apply, ← transfer_nat_trans_self_comp _ adj₂], simp } lemma transfer_nat_trans_self_comm {f g} (gf : g ≫ f = 𝟙 _) : transfer_nat_trans_self adj₁ adj₂ f ≫ transfer_nat_trans_self adj₂ adj₁ g = 𝟙 _ := by rw [transfer_nat_trans_self_comp, gf, transfer_nat_trans_self_id] lemma transfer_nat_trans_self_symm_comm {f g} (gf : g ≫ f = 𝟙 _) : (transfer_nat_trans_self adj₁ adj₂).symm f ≫ (transfer_nat_trans_self adj₂ adj₁).symm g = 𝟙 _ := by rw [transfer_nat_trans_self_symm_comp, gf, transfer_nat_trans_self_symm_id] /-- If `f` is an isomorphism, then the transferred natural transformation is an isomorphism. The converse is given in `transfer_nat_trans_self_of_iso`. -/ instance transfer_nat_trans_self_iso (f : L₂ ⟶ L₁) [is_iso f] : is_iso (transfer_nat_trans_self adj₁ adj₂ f) := ⟨⟨transfer_nat_trans_self adj₂ adj₁ (inv f), ⟨transfer_nat_trans_self_comm _ _ (by simp), transfer_nat_trans_self_comm _ _ (by simp)⟩⟩⟩ /-- If `f` is an isomorphism, then the un-transferred natural transformation is an isomorphism. The converse is given in `transfer_nat_trans_self_symm_of_iso`. -/ instance transfer_nat_trans_self_symm_iso (f : R₁ ⟶ R₂) [is_iso f] : is_iso ((transfer_nat_trans_self adj₁ adj₂).symm f) := ⟨⟨(transfer_nat_trans_self adj₂ adj₁).symm (inv f), ⟨transfer_nat_trans_self_symm_comm _ _ (by simp), transfer_nat_trans_self_symm_comm _ _ (by simp)⟩⟩⟩ /-- If `f` is a natural transformation whose transferred natural transformation is an isomorphism, then `f` is an isomorphism. The converse is given in `transfer_nat_trans_self_iso`. -/ lemma transfer_nat_trans_self_of_iso (f : L₂ ⟶ L₁) [is_iso (transfer_nat_trans_self adj₁ adj₂ f)] : is_iso f := begin suffices : is_iso ((transfer_nat_trans_self adj₁ adj₂).symm (transfer_nat_trans_self adj₁ adj₂ f)), { simpa using this }, apply_instance, end /-- If `f` is a natural transformation whose un-transferred natural transformation is an isomorphism, then `f` is an isomorphism. The converse is given in `transfer_nat_trans_self_symm_iso`. -/ lemma transfer_nat_trans_self_symm_of_iso (f : R₁ ⟶ R₂) [is_iso ((transfer_nat_trans_self adj₁ adj₂).symm f)] : is_iso f := begin suffices : is_iso ((transfer_nat_trans_self adj₁ adj₂) ((transfer_nat_trans_self adj₁ adj₂).symm f)), { simpa using this }, apply_instance, end end self end category_theory
f4c4aea7ab759803d1bf648f1e52d0396a315d42
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/CompilerProbe.lean
7ffebba8d8fb32b7d33e3109676a56135a16957f
[ "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,107
lean
import Lean import Lean.Compiler.LCNF.Probing open Lean.Compiler.LCNF -- Find functions that have jps which take a lambda #eval Probe.runGlobally (phase := .mono) <| Probe.filterByJp (·.params.anyM (fun param => return param.type.isForall)) >=> Probe.declNames >=> Probe.toString -- Count lambda lifted functions def lambdaCounter : Probe Decl Nat := Probe.filter (fun decl => if let .str _ val := decl.name then return val.startsWith "_lam" else return false) >=> Probe.declNames >=> Probe.count -- Run everywhere #eval Probe.runGlobally (phase := .mono) <| lambdaCounter -- Run limited #eval Probe.runOnModule `Lean.Compiler.LCNF.JoinPoints (phase := .mono) <| lambdaCounter -- Find most commonly used function with threshold #eval Probe.runOnModule `Lean.Compiler.LCNF.JoinPoints (phase := .mono) <| Probe.getLetValues >=> Probe.filter (fun e => return e matches .const ..) >=> Probe.map (fun | .const declName .. => return s!"{declName}" | _ => unreachable!) >=> Probe.countUniqueSorted >=> Probe.filter (fun (_, count) => return count > 100)
9e8092ba3c2fb81b5ea29a9e3a0819f1ab96fb36
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/algebra/open_subgroup.lean
145f6a278c9658346d5f1ec3953951a0d6947699
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
10,158
lean
/- Copyright (c) 2019 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import topology.algebra.ring import topology.algebra.filter_basis import topology.sets.opens /-! # Open subgroups of a topological groups This files builds the lattice `open_subgroup G` of open subgroups in a topological group `G`, and its additive version `open_add_subgroup`. This lattice has a top element, the subgroup of all elements, but no bottom element in general. The trivial subgroup which is the natural candidate bottom has no reason to be open (this happens only in discrete groups). Note that this notion is especially relevant in a non-archimedean context, for instance for `p`-adic groups. ## Main declarations * `open_subgroup.is_closed`: An open subgroup is automatically closed. * `subgroup.is_open_mono`: A subgroup containing an open subgroup is open. There are also versions for additive groups, submodules and ideals. * `open_subgroup.comap`: Open subgroups can be pulled back by a continuous group morphism. ## TODO * Prove that the identity component of a locally path connected group is an open subgroup. Up to now this file is really geared towards non-archimedean algebra, not Lie groups. -/ open topological_space open_locale topological_space /-- The type of open subgroups of a topological additive group. -/ @[ancestor add_subgroup] structure open_add_subgroup (G : Type*) [add_group G] [topological_space G] extends add_subgroup G := (is_open' : is_open carrier) /-- The type of open subgroups of a topological group. -/ @[ancestor subgroup, to_additive] structure open_subgroup (G : Type*) [group G] [topological_space G] extends subgroup G := (is_open' : is_open carrier) /-- Reinterpret an `open_subgroup` as a `subgroup`. -/ add_decl_doc open_subgroup.to_subgroup /-- Reinterpret an `open_add_subgroup` as an `add_subgroup`. -/ add_decl_doc open_add_subgroup.to_add_subgroup namespace open_subgroup open function topological_space variables {G : Type*} [group G] [topological_space G] variables {U V : open_subgroup G} {g : G} @[to_additive] instance has_coe_set : has_coe_t (open_subgroup G) (set G) := ⟨λ U, U.1⟩ @[to_additive] instance : has_mem G (open_subgroup G) := ⟨λ g U, g ∈ (U : set G)⟩ @[to_additive] instance has_coe_subgroup : has_coe_t (open_subgroup G) (subgroup G) := ⟨to_subgroup⟩ @[to_additive] instance has_coe_opens : has_coe_t (open_subgroup G) (opens G) := ⟨λ U, ⟨U, U.is_open'⟩⟩ @[simp, norm_cast, to_additive] lemma mem_coe : g ∈ (U : set G) ↔ g ∈ U := iff.rfl @[simp, norm_cast, to_additive] lemma mem_coe_opens : g ∈ (U : opens G) ↔ g ∈ U := iff.rfl @[simp, norm_cast, to_additive] lemma mem_coe_subgroup : g ∈ (U : subgroup G) ↔ g ∈ U := iff.rfl @[to_additive] lemma coe_injective : injective (coe : open_subgroup G → set G) := by { rintros ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨h⟩, congr, } @[ext, to_additive] lemma ext (h : ∀ x, x ∈ U ↔ x ∈ V) : (U = V) := coe_injective $ set.ext h @[to_additive] lemma ext_iff : (U = V) ↔ (∀ x, x ∈ U ↔ x ∈ V) := ⟨λ h x, h ▸ iff.rfl, ext⟩ variable (U) @[to_additive] protected lemma is_open : is_open (U : set G) := U.is_open' @[to_additive] protected lemma one_mem : (1 : G) ∈ U := U.one_mem' @[to_additive] protected lemma inv_mem {g : G} (h : g ∈ U) : g⁻¹ ∈ U := U.inv_mem' h @[to_additive] protected lemma mul_mem {g₁ g₂ : G} (h₁ : g₁ ∈ U) (h₂ : g₂ ∈ U) : g₁ * g₂ ∈ U := U.mul_mem' h₁ h₂ @[to_additive] lemma mem_nhds_one : (U : set G) ∈ 𝓝 (1 : G) := is_open.mem_nhds U.is_open U.one_mem variable {U} @[to_additive] instance : has_top (open_subgroup G) := ⟨{ is_open' := is_open_univ, .. (⊤ : subgroup G) }⟩ @[to_additive] instance : inhabited (open_subgroup G) := ⟨⊤⟩ @[to_additive] lemma is_closed [has_continuous_mul G] (U : open_subgroup G) : is_closed (U : set G) := begin apply is_open_compl_iff.1, refine is_open_iff_forall_mem_open.2 (λ x hx, ⟨(λ y, y * x⁻¹) ⁻¹' U, _, _, _⟩), { intros u hux, simp only [set.mem_preimage, set.mem_compl_iff, mem_coe] at hux hx ⊢, refine mt (λ hu, _) hx, convert U.mul_mem (U.inv_mem hux) hu, simp }, { exact U.is_open.preimage (continuous_mul_right _) }, { simp [U.one_mem] } end section variables {H : Type*} [group H] [topological_space H] /-- The product of two open subgroups as an open subgroup of the product group. -/ @[to_additive "The product of two open subgroups as an open subgroup of the product group."] def prod (U : open_subgroup G) (V : open_subgroup H) : open_subgroup (G × H) := { carrier := (U : set G) ×ˢ (V : set H), is_open' := U.is_open.prod V.is_open, .. (U : subgroup G).prod (V : subgroup H) } end @[to_additive] instance : partial_order (open_subgroup G) := { le := λ U V, ∀ ⦃x⦄, x ∈ U → x ∈ V, .. partial_order.lift (coe : open_subgroup G → set G) coe_injective } @[to_additive] instance : semilattice_inf (open_subgroup G) := { inf := λ U V, { is_open' := is_open.inter U.is_open V.is_open, .. (U : subgroup G) ⊓ V }, inf_le_left := λ U V, set.inter_subset_left _ _, inf_le_right := λ U V, set.inter_subset_right _ _, le_inf := λ U V W hV hW, set.subset_inter hV hW, ..open_subgroup.partial_order } @[to_additive] instance : order_top (open_subgroup G) := { top := ⊤, le_top := λ U, set.subset_univ _ } @[simp, norm_cast, to_additive] lemma coe_inf : (↑(U ⊓ V) : set G) = (U : set G) ∩ V := rfl @[simp, norm_cast, to_additive] lemma coe_subset : (U : set G) ⊆ V ↔ U ≤ V := iff.rfl @[simp, norm_cast, to_additive] lemma coe_subgroup_le : (U : subgroup G) ≤ (V : subgroup G) ↔ U ≤ V := iff.rfl variables {N : Type*} [group N] [topological_space N] /-- The preimage of an `open_subgroup` along a continuous `monoid` homomorphism is an `open_subgroup`. -/ @[to_additive "The preimage of an `open_add_subgroup` along a continuous `add_monoid` homomorphism is an `open_add_subgroup`."] def comap (f : G →* N) (hf : continuous f) (H : open_subgroup N) : open_subgroup G := { is_open' := H.is_open.preimage hf, .. (H : subgroup N).comap f } @[simp, to_additive] lemma coe_comap (H : open_subgroup N) (f : G →* N) (hf : continuous f) : (H.comap f hf : set G) = f ⁻¹' H := rfl @[simp, to_additive] lemma mem_comap {H : open_subgroup N} {f : G →* N} {hf : continuous f} {x : G} : x ∈ H.comap f hf ↔ f x ∈ H := iff.rfl @[to_additive] lemma comap_comap {P : Type*} [group P] [topological_space P] (K : open_subgroup P) (f₂ : N →* P) (hf₂ : continuous f₂) (f₁ : G →* N) (hf₁ : continuous f₁) : (K.comap f₂ hf₂).comap f₁ hf₁ = K.comap (f₂.comp f₁) (hf₂.comp hf₁) := rfl end open_subgroup namespace subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] (H : subgroup G) @[to_additive] lemma is_open_of_mem_nhds {g : G} (hg : (H : set G) ∈ 𝓝 g) : is_open (H : set G) := begin simp only [is_open_iff_mem_nhds, set_like.mem_coe] at hg ⊢, intros x hx, have : filter.tendsto (λ y, y * (x⁻¹ * g)) (𝓝 x) (𝓝 $ x * (x⁻¹ * g)) := (continuous_id.mul continuous_const).tendsto _, rw [mul_inv_cancel_left] at this, have := filter.mem_map'.1 (this hg), replace hg : g ∈ H := set_like.mem_coe.1 (mem_of_mem_nhds hg), simp only [set_like.mem_coe, H.mul_mem_cancel_right (H.mul_mem (H.inv_mem hx) hg)] at this, exact this end @[to_additive] lemma is_open_of_open_subgroup {U : open_subgroup G} (h : U.1 ≤ H) : is_open (H : set G) := H.is_open_of_mem_nhds (filter.mem_of_superset U.mem_nhds_one h) /-- If a subgroup of a topological group has `1` in its interior, then it is open. -/ @[to_additive "If a subgroup of an additive topological group has `0` in its interior, then it is open."] lemma is_open_of_one_mem_interior {G : Type*} [group G] [topological_space G] [topological_group G] {H : subgroup G} (h_1_int : (1 : G) ∈ interior (H : set G)) : is_open (H : set G) := begin have h : 𝓝 1 ≤ filter.principal (H : set G) := nhds_le_of_le h_1_int (is_open_interior) (filter.principal_mono.2 interior_subset), rw is_open_iff_nhds, intros g hg, rw (show 𝓝 g = filter.map ⇑(homeomorph.mul_left g) (𝓝 1), by simp), convert filter.map_mono h, simp only [homeomorph.coe_mul_left, filter.map_principal, set.image_mul_left, filter.principal_eq_iff_eq], ext, simp [H.mul_mem_cancel_left (H.inv_mem hg)], end @[to_additive] lemma is_open_mono {H₁ H₂ : subgroup G} (h : H₁ ≤ H₂) (h₁ : is_open (H₁ : set G)) : is_open (H₂ : set G) := @is_open_of_open_subgroup _ _ _ _ H₂ { is_open' := h₁, .. H₁ } h end subgroup namespace open_subgroup variables {G : Type*} [group G] [topological_space G] [has_continuous_mul G] @[to_additive] instance : semilattice_sup (open_subgroup G) := { sup := λ U V, { is_open' := show is_open (((U : subgroup G) ⊔ V : subgroup G) : set G), from subgroup.is_open_mono le_sup_left U.is_open, .. ((U : subgroup G) ⊔ V) }, le_sup_left := λ U V, coe_subgroup_le.1 le_sup_left, le_sup_right := λ U V, coe_subgroup_le.1 le_sup_right, sup_le := λ U V W hU hV, coe_subgroup_le.1 (sup_le hU hV), ..open_subgroup.semilattice_inf } @[to_additive] instance : lattice (open_subgroup G) := { ..open_subgroup.semilattice_sup, ..open_subgroup.semilattice_inf } end open_subgroup namespace submodule open open_add_subgroup variables {R : Type*} {M : Type*} [comm_ring R] variables [add_comm_group M] [topological_space M] [topological_add_group M] [module R M] lemma is_open_mono {U P : submodule R M} (h : U ≤ P) (hU : is_open (U : set M)) : is_open (P : set M) := @add_subgroup.is_open_mono M _ _ _ U.to_add_subgroup P.to_add_subgroup h hU end submodule namespace ideal variables {R : Type*} [comm_ring R] variables [topological_space R] [topological_ring R] lemma is_open_of_open_subideal {U I : ideal R} (h : U ≤ I) (hU : is_open (U : set R)) : is_open (I : set R) := submodule.is_open_mono h hU end ideal
9381483f219b557da3e99e12b2168362325a8e83
bb31430994044506fa42fd667e2d556327e18dfe
/src/order/filter/bases.lean
9e2ead76bddd30b4a89f6ba47af3bdfda364d39e
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
49,395
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import data.prod.pprod import data.set.countable import order.filter.prod /-! # Filter bases A filter basis `B : filter_basis α` on a type `α` is a nonempty collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. Compared to filters, filter bases do not require that any set containing an element of `B` belongs to `B`. A filter basis `B` can be used to construct `B.filter : filter α` such that a set belongs to `B.filter` if and only if it contains an element of `B`. Given an indexing type `ι`, a predicate `p : ι → Prop`, and a map `s : ι → set α`, the proposition `h : filter.is_basis p s` makes sure the range of `s` bounded by `p` (ie. `s '' set_of p`) defines a filter basis `h.filter_basis`. If one already has a filter `l` on `α`, `filter.has_basis l p s` (where `p : ι → Prop` and `s : ι → set α` as above) means that a set belongs to `l` if and only if it contains some `s i` with `p i`. It implies `h : filter.is_basis p s`, and `l = h.filter_basis.filter`. The point of this definition is that checking statements involving elements of `l` often reduces to checking them on the basis elements. We define a function `has_basis.index (h : filter.has_basis l p s) (t) (ht : t ∈ l)` that returns some index `i` such that `p i` and `s i ⊆ t`. This function can be useful to avoid manual destruction of `h.mem_iff.mpr ht` using `cases` or `let`. This file also introduces more restricted classes of bases, involving monotonicity or countability. In particular, for `l : filter α`, `l.is_countably_generated` means there is a countable set of sets which generates `s`. This is reformulated in term of bases, and consequences are derived. ## Main statements * `has_basis.mem_iff`, `has_basis.mem_of_superset`, `has_basis.mem_of_mem` : restate `t ∈ f` in terms of a basis; * `basis_sets` : all sets of a filter form a basis; * `has_basis.inf`, `has_basis.inf_principal`, `has_basis.prod`, `has_basis.prod_self`, `has_basis.map`, `has_basis.comap` : combinators to construct filters of `l ⊓ l'`, `l ⊓ 𝓟 t`, `l ×ᶠ l'`, `l ×ᶠ l`, `l.map f`, `l.comap f` respectively; * `has_basis.le_iff`, `has_basis.ge_iff`, has_basis.le_basis_iff` : restate `l ≤ l'` in terms of bases. * `has_basis.tendsto_right_iff`, `has_basis.tendsto_left_iff`, `has_basis.tendsto_iff` : restate `tendsto f l l'` in terms of bases. * `is_countably_generated_iff_exists_antitone_basis` : proves a filter is countably generated if and only if it admits a basis parametrized by a decreasing sequence of sets indexed by `ℕ`. * `tendsto_iff_seq_tendsto ` : an abstract version of "sequentially continuous implies continuous". ## Implementation notes As with `Union`/`bUnion`/`sUnion`, there are three different approaches to filter bases: * `has_basis l s`, `s : set (set α)`; * `has_basis l s`, `s : ι → set α`; * `has_basis l p s`, `p : ι → Prop`, `s : ι → set α`. We use the latter one because, e.g., `𝓝 x` in an `emetric_space` or in a `metric_space` has a basis of this form. The other two can be emulated using `s = id` or `p = λ _, true`. With this approach sometimes one needs to `simp` the statement provided by the `has_basis` machinery, e.g., `simp only [exists_prop, true_and]` or `simp only [forall_const]` can help with the case `p = λ _, true`. -/ open set filter open_locale filter classical section sort variables {α β γ : Type*} {ι ι' : Sort*} /-- A filter basis `B` on a type `α` is a nonempty collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. -/ structure filter_basis (α : Type*) := (sets : set (set α)) (nonempty : sets.nonempty) (inter_sets {x y} : x ∈ sets → y ∈ sets → ∃ z ∈ sets, z ⊆ x ∩ y) instance filter_basis.nonempty_sets (B : filter_basis α) : nonempty B.sets := B.nonempty.to_subtype /-- If `B` is a filter basis on `α`, and `U` a subset of `α` then we can write `U ∈ B` as on paper. -/ @[reducible] instance {α : Type*}: has_mem (set α) (filter_basis α) := ⟨λ U B, U ∈ B.sets⟩ -- For illustration purposes, the filter basis defining (at_top : filter ℕ) instance : inhabited (filter_basis ℕ) := ⟨{ sets := range Ici, nonempty := ⟨Ici 0, mem_range_self 0⟩, inter_sets := begin rintros _ _ ⟨n, rfl⟩ ⟨m, rfl⟩, refine ⟨Ici (max n m), mem_range_self _, _⟩, rintros p p_in, split ; rw mem_Ici at *, exact le_of_max_le_left p_in, exact le_of_max_le_right p_in, end }⟩ /-- View a filter as a filter basis. -/ def filter.as_basis (f : filter α) : filter_basis α := ⟨f.sets, ⟨univ, univ_mem⟩, λ x y hx hy, ⟨x ∩ y, inter_mem hx hy, subset_rfl⟩⟩ /-- `is_basis p s` means the image of `s` bounded by `p` is a filter basis. -/ protected structure filter.is_basis (p : ι → Prop) (s : ι → set α) : Prop := (nonempty : ∃ i, p i) (inter : ∀ {i j}, p i → p j → ∃ k, p k ∧ s k ⊆ s i ∩ s j) namespace filter namespace is_basis /-- Constructs a filter basis from an indexed family of sets satisfying `is_basis`. -/ protected def filter_basis {p : ι → Prop} {s : ι → set α} (h : is_basis p s) : filter_basis α := { sets := {t | ∃ i, p i ∧ s i = t}, nonempty := let ⟨i, hi⟩ := h.nonempty in ⟨s i, ⟨i, hi, rfl⟩⟩, inter_sets := by { rintros _ _ ⟨i, hi, rfl⟩ ⟨j, hj, rfl⟩, rcases h.inter hi hj with ⟨k, hk, hk'⟩, exact ⟨_, ⟨k, hk, rfl⟩, hk'⟩ } } variables {p : ι → Prop} {s : ι → set α} (h : is_basis p s) lemma mem_filter_basis_iff {U : set α} : U ∈ h.filter_basis ↔ ∃ i, p i ∧ s i = U := iff.rfl end is_basis end filter namespace filter_basis /-- The filter associated to a filter basis. -/ protected def filter (B : filter_basis α) : filter α := { sets := {s | ∃ t ∈ B, t ⊆ s}, univ_sets := let ⟨s, s_in⟩ := B.nonempty in ⟨s, s_in, s.subset_univ⟩, sets_of_superset := λ x y ⟨s, s_in, h⟩ hxy, ⟨s, s_in, set.subset.trans h hxy⟩, inter_sets := λ x y ⟨s, s_in, hs⟩ ⟨t, t_in, ht⟩, let ⟨u, u_in, u_sub⟩ := B.inter_sets s_in t_in in ⟨u, u_in, set.subset.trans u_sub $ set.inter_subset_inter hs ht⟩ } lemma mem_filter_iff (B : filter_basis α) {U : set α} : U ∈ B.filter ↔ ∃ s ∈ B, s ⊆ U := iff.rfl lemma mem_filter_of_mem (B : filter_basis α) {U : set α} : U ∈ B → U ∈ B.filter:= λ U_in, ⟨U, U_in, subset.refl _⟩ lemma eq_infi_principal (B : filter_basis α) : B.filter = ⨅ s : B.sets, 𝓟 s := begin have : directed (≥) (λ (s : B.sets), 𝓟 (s : set α)), { rintros ⟨U, U_in⟩ ⟨V, V_in⟩, rcases B.inter_sets U_in V_in with ⟨W, W_in, W_sub⟩, use [W, W_in], simp only [ge_iff_le, le_principal_iff, mem_principal, subtype.coe_mk], exact subset_inter_iff.mp W_sub }, ext U, simp [mem_filter_iff, mem_infi_of_directed this] end protected lemma generate (B : filter_basis α) : generate B.sets = B.filter := begin apply le_antisymm, { intros U U_in, rcases B.mem_filter_iff.mp U_in with ⟨V, V_in, h⟩, exact generate_sets.superset (generate_sets.basic V_in) h }, { rw sets_iff_generate, apply mem_filter_of_mem } end end filter_basis namespace filter namespace is_basis variables {p : ι → Prop} {s : ι → set α} /-- Constructs a filter from an indexed family of sets satisfying `is_basis`. -/ protected def filter (h : is_basis p s) : filter α := h.filter_basis.filter protected lemma mem_filter_iff (h : is_basis p s) {U : set α} : U ∈ h.filter ↔ ∃ i, p i ∧ s i ⊆ U := begin erw [h.filter_basis.mem_filter_iff], simp only [mem_filter_basis_iff h, exists_prop], split, { rintros ⟨_, ⟨i, pi, rfl⟩, h⟩, tauto }, { tauto } end lemma filter_eq_generate (h : is_basis p s) : h.filter = generate {U | ∃ i, p i ∧ s i = U} := by erw h.filter_basis.generate ; refl end is_basis /-- We say that a filter `l` has a basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`. -/ protected structure has_basis (l : filter α) (p : ι → Prop) (s : ι → set α) : Prop := (mem_iff' : ∀ (t : set α), t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t) section same_type variables {l l' : filter α} {p : ι → Prop} {s : ι → set α} {t : set α} {i : ι} {p' : ι' → Prop} {s' : ι' → set α} {i' : ι'} lemma has_basis_generate (s : set (set α)) : (generate s).has_basis (λ t, set.finite t ∧ t ⊆ s) (λ t, ⋂₀ t) := ⟨λ U, by simp only [mem_generate_iff, exists_prop, and.assoc, and.left_comm]⟩ /-- The smallest filter basis containing a given collection of sets. -/ def filter_basis.of_sets (s : set (set α)) : filter_basis α := { sets := sInter '' { t | set.finite t ∧ t ⊆ s}, nonempty := ⟨univ, ∅, ⟨⟨finite_empty, empty_subset s⟩, sInter_empty⟩⟩, inter_sets := begin rintros _ _ ⟨a, ⟨fina, suba⟩, rfl⟩ ⟨b, ⟨finb, subb⟩, rfl⟩, exact ⟨⋂₀ (a ∪ b), mem_image_of_mem _ ⟨fina.union finb, union_subset suba subb⟩, by rw sInter_union⟩, end } /-- Definition of `has_basis` unfolded with implicit set argument. -/ lemma has_basis.mem_iff (hl : l.has_basis p s) : t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t := hl.mem_iff' t lemma has_basis.eq_of_same_basis (hl : l.has_basis p s) (hl' : l'.has_basis p s) : l = l' := begin ext t, rw [hl.mem_iff, hl'.mem_iff] end lemma has_basis_iff : l.has_basis p s ↔ ∀ t, t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t := ⟨λ ⟨h⟩, h, λ h, ⟨h⟩⟩ lemma has_basis.ex_mem (h : l.has_basis p s) : ∃ i, p i := let ⟨i, pi, h⟩ := h.mem_iff.mp univ_mem in ⟨i, pi⟩ protected lemma has_basis.nonempty (h : l.has_basis p s) : nonempty ι := nonempty_of_exists h.ex_mem protected lemma is_basis.has_basis (h : is_basis p s) : has_basis h.filter p s := ⟨λ t, by simp only [h.mem_filter_iff, exists_prop]⟩ lemma has_basis.mem_of_superset (hl : l.has_basis p s) (hi : p i) (ht : s i ⊆ t) : t ∈ l := (hl.mem_iff).2 ⟨i, hi, ht⟩ lemma has_basis.mem_of_mem (hl : l.has_basis p s) (hi : p i) : s i ∈ l := hl.mem_of_superset hi $ subset.refl _ /-- Index of a basis set such that `s i ⊆ t` as an element of `subtype p`. -/ noncomputable def has_basis.index (h : l.has_basis p s) (t : set α) (ht : t ∈ l) : {i : ι // p i} := ⟨(h.mem_iff.1 ht).some, (h.mem_iff.1 ht).some_spec.fst⟩ lemma has_basis.property_index (h : l.has_basis p s) (ht : t ∈ l) : p (h.index t ht) := (h.index t ht).2 lemma has_basis.set_index_mem (h : l.has_basis p s) (ht : t ∈ l) : s (h.index t ht) ∈ l := h.mem_of_mem $ h.property_index _ lemma has_basis.set_index_subset (h : l.has_basis p s) (ht : t ∈ l) : s (h.index t ht) ⊆ t := (h.mem_iff.1 ht).some_spec.snd lemma has_basis.is_basis (h : l.has_basis p s) : is_basis p s := { nonempty := let ⟨i, hi, H⟩ := h.mem_iff.mp univ_mem in ⟨i, hi⟩, inter := λ i j hi hj, by simpa [h.mem_iff] using l.inter_sets (h.mem_of_mem hi) (h.mem_of_mem hj) } lemma has_basis.filter_eq (h : l.has_basis p s) : h.is_basis.filter = l := by { ext U, simp [h.mem_iff, is_basis.mem_filter_iff] } lemma has_basis.eq_generate (h : l.has_basis p s) : l = generate { U | ∃ i, p i ∧ s i = U } := by rw [← h.is_basis.filter_eq_generate, h.filter_eq] lemma generate_eq_generate_inter (s : set (set α)) : generate s = generate (sInter '' { t | set.finite t ∧ t ⊆ s}) := by erw [(filter_basis.of_sets s).generate, ← (has_basis_generate s).filter_eq] ; refl lemma of_sets_filter_eq_generate (s : set (set α)) : (filter_basis.of_sets s).filter = generate s := by rw [← (filter_basis.of_sets s).generate, generate_eq_generate_inter s] ; refl protected lemma _root_.filter_basis.has_basis {α : Type*} (B : filter_basis α) : has_basis (B.filter) (λ s : set α, s ∈ B) id := ⟨λ t, B.mem_filter_iff⟩ lemma has_basis.to_has_basis' (hl : l.has_basis p s) (h : ∀ i, p i → ∃ i', p' i' ∧ s' i' ⊆ s i) (h' : ∀ i', p' i' → s' i' ∈ l) : l.has_basis p' s' := begin refine ⟨λ t, ⟨λ ht, _, λ ⟨i', hi', ht⟩, mem_of_superset (h' i' hi') ht⟩⟩, rcases hl.mem_iff.1 ht with ⟨i, hi, ht⟩, rcases h i hi with ⟨i', hi', hs's⟩, exact ⟨i', hi', subset.trans hs's ht⟩ end lemma has_basis.to_has_basis (hl : l.has_basis p s) (h : ∀ i, p i → ∃ i', p' i' ∧ s' i' ⊆ s i) (h' : ∀ i', p' i' → ∃ i, p i ∧ s i ⊆ s' i') : l.has_basis p' s' := hl.to_has_basis' h $ λ i' hi', let ⟨i, hi, hss'⟩ := h' i' hi' in hl.mem_iff.2 ⟨i, hi, hss'⟩ lemma has_basis.to_subset (hl : l.has_basis p s) {t : ι → set α} (h : ∀ i, p i → t i ⊆ s i) (ht : ∀ i, p i → t i ∈ l) : l.has_basis p t := hl.to_has_basis' (λ i hi, ⟨i, hi, h i hi⟩) ht lemma has_basis.eventually_iff (hl : l.has_basis p s) {q : α → Prop} : (∀ᶠ x in l, q x) ↔ ∃ i, p i ∧ ∀ ⦃x⦄, x ∈ s i → q x := by simpa using hl.mem_iff lemma has_basis.frequently_iff (hl : l.has_basis p s) {q : α → Prop} : (∃ᶠ x in l, q x) ↔ ∀ i, p i → ∃ x ∈ s i, q x := by simp [filter.frequently, hl.eventually_iff] lemma has_basis.exists_iff (hl : l.has_basis p s) {P : set α → Prop} (mono : ∀ ⦃s t⦄, s ⊆ t → P t → P s) : (∃ s ∈ l, P s) ↔ ∃ (i) (hi : p i), P (s i) := ⟨λ ⟨s, hs, hP⟩, let ⟨i, hi, his⟩ := hl.mem_iff.1 hs in ⟨i, hi, mono his hP⟩, λ ⟨i, hi, hP⟩, ⟨s i, hl.mem_of_mem hi, hP⟩⟩ lemma has_basis.forall_iff (hl : l.has_basis p s) {P : set α → Prop} (mono : ∀ ⦃s t⦄, s ⊆ t → P s → P t) : (∀ s ∈ l, P s) ↔ ∀ i, p i → P (s i) := ⟨λ H i hi, H (s i) $ hl.mem_of_mem hi, λ H s hs, let ⟨i, hi, his⟩ := hl.mem_iff.1 hs in mono his (H i hi)⟩ lemma has_basis.ne_bot_iff (hl : l.has_basis p s) : ne_bot l ↔ (∀ {i}, p i → (s i).nonempty) := forall_mem_nonempty_iff_ne_bot.symm.trans $ hl.forall_iff $ λ _ _, nonempty.mono lemma has_basis.eq_bot_iff (hl : l.has_basis p s) : l = ⊥ ↔ ∃ i, p i ∧ s i = ∅ := not_iff_not.1 $ ne_bot_iff.symm.trans $ hl.ne_bot_iff.trans $ by simp only [not_exists, not_and, nonempty_iff_ne_empty] lemma generate_ne_bot_iff {s : set (set α)} : ne_bot (generate s) ↔ ∀ t ⊆ s, t.finite → (⋂₀ t).nonempty := (has_basis_generate s).ne_bot_iff.trans $ by simp only [← and_imp, and_comm] lemma basis_sets (l : filter α) : l.has_basis (λ s : set α, s ∈ l) id := ⟨λ t, exists_mem_subset_iff.symm⟩ lemma as_basis_filter (f : filter α) : f.as_basis.filter = f := by ext t; exact exists_mem_subset_iff lemma has_basis_self {l : filter α} {P : set α → Prop} : has_basis l (λ s, s ∈ l ∧ P s) id ↔ ∀ t ∈ l, ∃ r ∈ l, P r ∧ r ⊆ t := begin simp only [has_basis_iff, exists_prop, id, and_assoc], exact forall_congr (λ s, ⟨λ h, h.1, λ h, ⟨h, λ ⟨t, hl, hP, hts⟩, mem_of_superset hl hts⟩⟩) end lemma has_basis.comp_of_surjective (h : l.has_basis p s) {g : ι' → ι} (hg : function.surjective g) : l.has_basis (p ∘ g) (s ∘ g) := ⟨λ t, h.mem_iff.trans hg.exists⟩ lemma has_basis.comp_equiv (h : l.has_basis p s) (e : ι' ≃ ι) : l.has_basis (p ∘ e) (s ∘ e) := h.comp_of_surjective e.surjective /-- If `{s i | p i}` is a basis of a filter `l` and each `s i` includes `s j` such that `p j ∧ q j`, then `{s j | p j ∧ q j}` is a basis of `l`. -/ lemma has_basis.restrict (h : l.has_basis p s) {q : ι → Prop} (hq : ∀ i, p i → ∃ j, p j ∧ q j ∧ s j ⊆ s i) : l.has_basis (λ i, p i ∧ q i) s := begin refine ⟨λ t, ⟨λ ht, _, λ ⟨i, hpi, hti⟩, h.mem_iff.2 ⟨i, hpi.1, hti⟩⟩⟩, rcases h.mem_iff.1 ht with ⟨i, hpi, hti⟩, rcases hq i hpi with ⟨j, hpj, hqj, hji⟩, exact ⟨j, ⟨hpj, hqj⟩, subset.trans hji hti⟩ end /-- If `{s i | p i}` is a basis of a filter `l` and `V ∈ l`, then `{s i | p i ∧ s i ⊆ V}` is a basis of `l`. -/ lemma has_basis.restrict_subset (h : l.has_basis p s) {V : set α} (hV : V ∈ l) : l.has_basis (λ i, p i ∧ s i ⊆ V) s := h.restrict $ λ i hi, (h.mem_iff.1 (inter_mem hV (h.mem_of_mem hi))).imp $ λ j hj, ⟨hj.fst, subset_inter_iff.1 hj.snd⟩ lemma has_basis.has_basis_self_subset {p : set α → Prop} (h : l.has_basis (λ s, s ∈ l ∧ p s) id) {V : set α} (hV : V ∈ l) : l.has_basis (λ s, s ∈ l ∧ p s ∧ s ⊆ V) id := by simpa only [and_assoc] using h.restrict_subset hV theorem has_basis.ge_iff (hl' : l'.has_basis p' s') : l ≤ l' ↔ ∀ i', p' i' → s' i' ∈ l := ⟨λ h i' hi', h $ hl'.mem_of_mem hi', λ h s hs, let ⟨i', hi', hs⟩ := hl'.mem_iff.1 hs in mem_of_superset (h _ hi') hs⟩ theorem has_basis.le_iff (hl : l.has_basis p s) : l ≤ l' ↔ ∀ t ∈ l', ∃ i (hi : p i), s i ⊆ t := by simp only [le_def, hl.mem_iff] theorem has_basis.le_basis_iff (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : l ≤ l' ↔ ∀ i', p' i' → ∃ i (hi : p i), s i ⊆ s' i' := by simp only [hl'.ge_iff, hl.mem_iff] lemma has_basis.ext (hl : l.has_basis p s) (hl' : l'.has_basis p' s') (h : ∀ i, p i → ∃ i', p' i' ∧ s' i' ⊆ s i) (h' : ∀ i', p' i' → ∃ i, p i ∧ s i ⊆ s' i') : l = l' := begin apply le_antisymm, { rw hl.le_basis_iff hl', simpa using h' }, { rw hl'.le_basis_iff hl, simpa using h }, end lemma has_basis.inf' (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊓ l').has_basis (λ i : pprod ι ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∩ s' i.2) := ⟨begin intro t, split, { simp only [mem_inf_iff, exists_prop, hl.mem_iff, hl'.mem_iff], rintros ⟨t, ⟨i, hi, ht⟩, t', ⟨i', hi', ht'⟩, rfl⟩, use [⟨i, i'⟩, ⟨hi, hi'⟩, inter_subset_inter ht ht'] }, { rintros ⟨⟨i, i'⟩, ⟨hi, hi'⟩, H⟩, exact mem_inf_of_inter (hl.mem_of_mem hi) (hl'.mem_of_mem hi') H } end⟩ lemma has_basis.inf {ι ι' : Type*} {p : ι → Prop} {s : ι → set α} {p' : ι' → Prop} {s' : ι' → set α} (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊓ l').has_basis (λ i : ι × ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∩ s' i.2) := (hl.inf' hl').to_has_basis (λ i hi, ⟨⟨i.1, i.2⟩, hi, subset.rfl⟩) (λ i hi, ⟨⟨i.1, i.2⟩, hi, subset.rfl⟩) lemma has_basis_infi' {ι : Type*} {ι' : ι → Type*} {l : ι → filter α} {p : Π i, ι' i → Prop} {s : Π i, ι' i → set α} (hl : ∀ i, (l i).has_basis (p i) (s i)) : (⨅ i, l i).has_basis (λ If : set ι × Π i, ι' i, If.1.finite ∧ ∀ i ∈ If.1, p i (If.2 i)) (λ If : set ι × Π i, ι' i, ⋂ i ∈ If.1, s i (If.2 i)) := ⟨begin intro t, split, { simp only [mem_infi', (hl _).mem_iff], rintros ⟨I, hI, V, hV, -, rfl, -⟩, choose u hu using hV, exact ⟨⟨I, u⟩, ⟨hI, λ i _, (hu i).1⟩, Inter_mono (λ i, Inter_mono $ λ hi, (hu i).2)⟩ }, { rintros ⟨⟨I, f⟩, ⟨hI₁, hI₂⟩, hsub⟩, refine mem_of_superset _ hsub, exact (bInter_mem hI₁).mpr (λ i hi, mem_infi_of_mem i $ (hl i).mem_of_mem $ hI₂ _ hi) } end⟩ lemma has_basis_infi {ι : Type*} {ι' : ι → Type*} {l : ι → filter α} {p : Π i, ι' i → Prop} {s : Π i, ι' i → set α} (hl : ∀ i, (l i).has_basis (p i) (s i)) : (⨅ i, l i).has_basis (λ If : Σ I : set ι, Π i : I, ι' i, If.1.finite ∧ ∀ i : If.1, p i (If.2 i)) (λ If, ⋂ i : If.1, s i (If.2 i)) := begin refine ⟨λ t, ⟨λ ht, _, _⟩⟩, { rcases (has_basis_infi' hl).mem_iff.mp ht with ⟨⟨I, f⟩, ⟨hI, hf⟩, hsub⟩, exact ⟨⟨I, λ i, f i⟩, ⟨hI, subtype.forall.mpr hf⟩, trans_rel_right _ (Inter_subtype _ _) hsub⟩ }, { rintro ⟨⟨I, f⟩, ⟨hI, hf⟩, hsub⟩, refine mem_of_superset _ hsub, casesI hI.nonempty_fintype, exact Inter_mem.2 (λ i, mem_infi_of_mem i $ (hl i).mem_of_mem $ hf _) } end lemma has_basis_infi_of_directed' {ι : Type*} {ι' : ι → Sort*} [nonempty ι] {l : ι → filter α} (s : Π i, (ι' i) → set α) (p : Π i, (ι' i) → Prop) (hl : ∀ i, (l i).has_basis (p i) (s i)) (h : directed (≥) l) : (⨅ i, l i).has_basis (λ (ii' : Σ i, ι' i), p ii'.1 ii'.2) (λ ii', s ii'.1 ii'.2) := begin refine ⟨λ t, _⟩, rw [mem_infi_of_directed h, sigma.exists], exact exists_congr (λ i, (hl i).mem_iff) end lemma has_basis_infi_of_directed {ι : Type*} {ι' : Sort*} [nonempty ι] {l : ι → filter α} (s : ι → ι' → set α) (p : ι → ι' → Prop) (hl : ∀ i, (l i).has_basis (p i) (s i)) (h : directed (≥) l) : (⨅ i, l i).has_basis (λ (ii' : ι × ι'), p ii'.1 ii'.2) (λ ii', s ii'.1 ii'.2) := begin refine ⟨λ t, _⟩, rw [mem_infi_of_directed h, prod.exists], exact exists_congr (λ i, (hl i).mem_iff) end lemma has_basis_binfi_of_directed' {ι : Type*} {ι' : ι → Sort*} {dom : set ι} (hdom : dom.nonempty) {l : ι → filter α} (s : Π i, (ι' i) → set α) (p : Π i, (ι' i) → Prop) (hl : ∀ i ∈ dom, (l i).has_basis (p i) (s i)) (h : directed_on (l ⁻¹'o ge) dom) : (⨅ i ∈ dom, l i).has_basis (λ (ii' : Σ i, ι' i), ii'.1 ∈ dom ∧ p ii'.1 ii'.2) (λ ii', s ii'.1 ii'.2) := begin refine ⟨λ t, _⟩, rw [mem_binfi_of_directed h hdom, sigma.exists], refine exists_congr (λ i, ⟨_, _⟩), { rintros ⟨hi, hti⟩, rcases (hl i hi).mem_iff.mp hti with ⟨b, hb, hbt⟩, exact ⟨b, ⟨hi, hb⟩, hbt⟩ }, { rintros ⟨b, ⟨hi, hb⟩, hibt⟩, exact ⟨hi, (hl i hi).mem_iff.mpr ⟨b, hb, hibt⟩⟩ } end lemma has_basis_binfi_of_directed {ι : Type*} {ι' : Sort*} {dom : set ι} (hdom : dom.nonempty) {l : ι → filter α} (s : ι → ι' → set α) (p : ι → ι' → Prop) (hl : ∀ i ∈ dom, (l i).has_basis (p i) (s i)) (h : directed_on (l ⁻¹'o ge) dom) : (⨅ i ∈ dom, l i).has_basis (λ (ii' : ι × ι'), ii'.1 ∈ dom ∧ p ii'.1 ii'.2) (λ ii', s ii'.1 ii'.2) := begin refine ⟨λ t, _⟩, rw [mem_binfi_of_directed h hdom, prod.exists], refine exists_congr (λ i, ⟨_, _⟩), { rintros ⟨hi, hti⟩, rcases (hl i hi).mem_iff.mp hti with ⟨b, hb, hbt⟩, exact ⟨b, ⟨hi, hb⟩, hbt⟩ }, { rintros ⟨b, ⟨hi, hb⟩, hibt⟩, exact ⟨hi, (hl i hi).mem_iff.mpr ⟨b, hb, hibt⟩⟩ } end lemma has_basis_principal (t : set α) : (𝓟 t).has_basis (λ i : unit, true) (λ i, t) := ⟨λ U, by simp⟩ lemma has_basis_pure (x : α) : (pure x : filter α).has_basis (λ i : unit, true) (λ i, {x}) := by simp only [← principal_singleton, has_basis_principal] lemma has_basis.sup' (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊔ l').has_basis (λ i : pprod ι ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∪ s' i.2) := ⟨begin intros t, simp only [mem_sup, hl.mem_iff, hl'.mem_iff, pprod.exists, union_subset_iff, exists_prop, and_assoc, exists_and_distrib_left], simp only [← and_assoc, exists_and_distrib_right, and_comm] end⟩ lemma has_basis.sup {ι ι' : Type*} {p : ι → Prop} {s : ι → set α} {p' : ι' → Prop} {s' : ι' → set α} (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : (l ⊔ l').has_basis (λ i : ι × ι', p i.1 ∧ p' i.2) (λ i, s i.1 ∪ s' i.2) := (hl.sup' hl').to_has_basis (λ i hi, ⟨⟨i.1, i.2⟩, hi, subset.rfl⟩) (λ i hi, ⟨⟨i.1, i.2⟩, hi, subset.rfl⟩) lemma has_basis_supr {ι : Sort*} {ι' : ι → Type*} {l : ι → filter α} {p : Π i, ι' i → Prop} {s : Π i, ι' i → set α} (hl : ∀ i, (l i).has_basis (p i) (s i)) : (⨆ i, l i).has_basis (λ f : Π i, ι' i, ∀ i, p i (f i)) (λ f : Π i, ι' i, ⋃ i, s i (f i)) := has_basis_iff.mpr $ λ t, by simp only [has_basis_iff, (hl _).mem_iff, classical.skolem, forall_and_distrib, Union_subset_iff, mem_supr] lemma has_basis.sup_principal (hl : l.has_basis p s) (t : set α) : (l ⊔ 𝓟 t).has_basis p (λ i, s i ∪ t) := ⟨λ u, by simp only [(hl.sup' (has_basis_principal t)).mem_iff, pprod.exists, exists_prop, and_true, unique.exists_iff]⟩ lemma has_basis.sup_pure (hl : l.has_basis p s) (x : α) : (l ⊔ pure x).has_basis p (λ i, s i ∪ {x}) := by simp only [← principal_singleton, hl.sup_principal] lemma has_basis.inf_principal (hl : l.has_basis p s) (s' : set α) : (l ⊓ 𝓟 s').has_basis p (λ i, s i ∩ s') := ⟨λ t, by simp only [mem_inf_principal, hl.mem_iff, subset_def, mem_set_of_eq, mem_inter_iff, and_imp]⟩ lemma has_basis.principal_inf (hl : l.has_basis p s) (s' : set α) : (𝓟 s' ⊓ l).has_basis p (λ i, s' ∩ s i) := by simpa only [inf_comm, inter_comm] using hl.inf_principal s' lemma has_basis.inf_basis_ne_bot_iff (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : ne_bot (l ⊓ l') ↔ ∀ ⦃i⦄ (hi : p i) ⦃i'⦄ (hi' : p' i'), (s i ∩ s' i').nonempty := (hl.inf' hl').ne_bot_iff.trans $ by simp [@forall_swap _ ι'] lemma has_basis.inf_ne_bot_iff (hl : l.has_basis p s) : ne_bot (l ⊓ l') ↔ ∀ ⦃i⦄ (hi : p i) ⦃s'⦄ (hs' : s' ∈ l'), (s i ∩ s').nonempty := hl.inf_basis_ne_bot_iff l'.basis_sets lemma has_basis.inf_principal_ne_bot_iff (hl : l.has_basis p s) {t : set α} : ne_bot (l ⊓ 𝓟 t) ↔ ∀ ⦃i⦄ (hi : p i), (s i ∩ t).nonempty := (hl.inf_principal t).ne_bot_iff lemma has_basis.disjoint_iff (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : disjoint l l' ↔ ∃ i (hi : p i) i' (hi' : p' i'), disjoint (s i) (s' i') := not_iff_not.mp $ by simp only [disjoint_iff, ← ne.def, ← ne_bot_iff, hl.inf_basis_ne_bot_iff hl', not_exists, bot_eq_empty, ←nonempty_iff_ne_empty, inf_eq_inter] lemma _root_.disjoint.exists_mem_filter_basis (h : disjoint l l') (hl : l.has_basis p s) (hl' : l'.has_basis p' s') : ∃ i (hi : p i) i' (hi' : p' i'), disjoint (s i) (s' i') := (hl.disjoint_iff hl').1 h lemma _root_.pairwise.exists_mem_filter_basis_of_disjoint {I : Type*} [finite I] {l : I → filter α} {ι : I → Sort*} {p : Π i, ι i → Prop} {s : Π i, ι i → set α} (hd : pairwise (disjoint on l)) (h : ∀ i, (l i).has_basis (p i) (s i)) : ∃ ind : Π i, ι i, (∀ i, p i (ind i)) ∧ pairwise (disjoint on λ i, s i (ind i)) := begin rcases hd.exists_mem_filter_of_disjoint with ⟨t, htl, hd⟩, choose ind hp ht using λ i, (h i).mem_iff.1 (htl i), exact ⟨ind, hp, hd.mono $ λ i j hij, hij.mono (ht _) (ht _)⟩ end lemma _root_.set.pairwise_disjoint.exists_mem_filter_basis {I : Type*} {l : I → filter α} {ι : I → Sort*} {p : Π i, ι i → Prop} {s : Π i, ι i → set α} {S : set I} (hd : S.pairwise_disjoint l) (hS : S.finite) (h : ∀ i, (l i).has_basis (p i) (s i)) : ∃ ind : Π i, ι i, (∀ i, p i (ind i)) ∧ S.pairwise_disjoint (λ i, s i (ind i)) := begin rcases hd.exists_mem_filter hS with ⟨t, htl, hd⟩, choose ind hp ht using λ i, (h i).mem_iff.1 (htl i), exact ⟨ind, hp, hd.mono ht⟩ end lemma inf_ne_bot_iff : ne_bot (l ⊓ l') ↔ ∀ ⦃s : set α⦄ (hs : s ∈ l) ⦃s'⦄ (hs' : s' ∈ l'), (s ∩ s').nonempty := l.basis_sets.inf_ne_bot_iff lemma inf_principal_ne_bot_iff {s : set α} : ne_bot (l ⊓ 𝓟 s) ↔ ∀ U ∈ l, (U ∩ s).nonempty := l.basis_sets.inf_principal_ne_bot_iff lemma mem_iff_inf_principal_compl {f : filter α} {s : set α} : s ∈ f ↔ f ⊓ 𝓟 sᶜ = ⊥ := begin refine not_iff_not.1 ((inf_principal_ne_bot_iff.trans _).symm.trans ne_bot_iff), exact ⟨λ h hs, by simpa [not_nonempty_empty] using h s hs, λ hs t ht, inter_compl_nonempty_iff.2 $ λ hts, hs $ mem_of_superset ht hts⟩, end lemma not_mem_iff_inf_principal_compl {f : filter α} {s : set α} : s ∉ f ↔ ne_bot (f ⊓ 𝓟 sᶜ) := (not_congr mem_iff_inf_principal_compl).trans ne_bot_iff.symm @[simp] lemma disjoint_principal_right {f : filter α} {s : set α} : disjoint f (𝓟 s) ↔ sᶜ ∈ f := by rw [mem_iff_inf_principal_compl, compl_compl, disjoint_iff] @[simp] lemma disjoint_principal_left {f : filter α} {s : set α} : disjoint (𝓟 s) f ↔ sᶜ ∈ f := by rw [disjoint.comm, disjoint_principal_right] @[simp] lemma disjoint_principal_principal {s t : set α} : disjoint (𝓟 s) (𝓟 t) ↔ disjoint s t := by simp [←subset_compl_iff_disjoint_left] alias disjoint_principal_principal ↔ _ _root_.disjoint.filter_principal @[simp] lemma disjoint_pure_pure {x y : α} : disjoint (pure x : filter α) (pure y) ↔ x ≠ y := by simp only [← principal_singleton, disjoint_principal_principal, disjoint_singleton] @[simp] lemma compl_diagonal_mem_prod {l₁ l₂ : filter α} : (diagonal α)ᶜ ∈ l₁ ×ᶠ l₂ ↔ disjoint l₁ l₂ := by simp only [mem_prod_iff, filter.disjoint_iff, prod_subset_compl_diagonal_iff_disjoint] lemma has_basis.disjoint_iff_left (h : l.has_basis p s) : disjoint l l' ↔ ∃ i (hi : p i), (s i)ᶜ ∈ l' := by simp only [h.disjoint_iff l'.basis_sets, exists_prop, id, ← disjoint_principal_left, (has_basis_principal _).disjoint_iff l'.basis_sets, unique.exists_iff] lemma has_basis.disjoint_iff_right (h : l.has_basis p s) : disjoint l' l ↔ ∃ i (hi : p i), (s i)ᶜ ∈ l' := disjoint.comm.trans h.disjoint_iff_left lemma le_iff_forall_inf_principal_compl {f g : filter α} : f ≤ g ↔ ∀ V ∈ g, f ⊓ 𝓟 Vᶜ = ⊥ := forall₂_congr $ λ _ _, mem_iff_inf_principal_compl lemma inf_ne_bot_iff_frequently_left {f g : filter α} : ne_bot (f ⊓ g) ↔ ∀ {p : α → Prop}, (∀ᶠ x in f, p x) → ∃ᶠ x in g, p x := by simpa only [inf_ne_bot_iff, frequently_iff, exists_prop, and_comm] lemma inf_ne_bot_iff_frequently_right {f g : filter α} : ne_bot (f ⊓ g) ↔ ∀ {p : α → Prop}, (∀ᶠ x in g, p x) → ∃ᶠ x in f, p x := by { rw inf_comm, exact inf_ne_bot_iff_frequently_left } lemma has_basis.eq_binfi (h : l.has_basis p s) : l = ⨅ i (_ : p i), 𝓟 (s i) := eq_binfi_of_mem_iff_exists_mem $ λ t, by simp only [h.mem_iff, mem_principal] lemma has_basis.eq_infi (h : l.has_basis (λ _, true) s) : l = ⨅ i, 𝓟 (s i) := by simpa only [infi_true] using h.eq_binfi lemma has_basis_infi_principal {s : ι → set α} (h : directed (≥) s) [nonempty ι] : (⨅ i, 𝓟 (s i)).has_basis (λ _, true) s := ⟨begin refine λ t, (mem_infi_of_directed (h.mono_comp _ _) t).trans $ by simp only [exists_prop, true_and, mem_principal], exact λ _ _, principal_mono.2 end⟩ /-- If `s : ι → set α` is an indexed family of sets, then finite intersections of `s i` form a basis of `⨅ i, 𝓟 (s i)`. -/ lemma has_basis_infi_principal_finite {ι : Type*} (s : ι → set α) : (⨅ i, 𝓟 (s i)).has_basis (λ t : set ι, t.finite) (λ t, ⋂ i ∈ t, s i) := begin refine ⟨λ U, (mem_infi_finite _).trans _⟩, simp only [infi_principal_finset, mem_Union, mem_principal, exists_prop, exists_finite_iff_finset, finset.set_bInter_coe] end lemma has_basis_binfi_principal {s : β → set α} {S : set β} (h : directed_on (s ⁻¹'o (≥)) S) (ne : S.nonempty) : (⨅ i ∈ S, 𝓟 (s i)).has_basis (λ i, i ∈ S) s := ⟨begin refine λ t, (mem_binfi_of_directed _ ne).trans $ by simp only [mem_principal], rw [directed_on_iff_directed, ← directed_comp, (∘)] at h ⊢, apply h.mono_comp _ _, exact λ _ _, principal_mono.2 end⟩ lemma has_basis_binfi_principal' {ι : Type*} {p : ι → Prop} {s : ι → set α} (h : ∀ i, p i → ∀ j, p j → ∃ k (h : p k), s k ⊆ s i ∧ s k ⊆ s j) (ne : ∃ i, p i) : (⨅ i (h : p i), 𝓟 (s i)).has_basis p s := filter.has_basis_binfi_principal h ne lemma has_basis.map (f : α → β) (hl : l.has_basis p s) : (l.map f).has_basis p (λ i, f '' (s i)) := ⟨λ t, by simp only [mem_map, image_subset_iff, hl.mem_iff, preimage]⟩ lemma has_basis.comap (f : β → α) (hl : l.has_basis p s) : (l.comap f).has_basis p (λ i, f ⁻¹' (s i)) := ⟨begin intro t, simp only [mem_comap, exists_prop, hl.mem_iff], split, { rintros ⟨t', ⟨i, hi, ht'⟩, H⟩, exact ⟨i, hi, subset.trans (preimage_mono ht') H⟩ }, { rintros ⟨i, hi, H⟩, exact ⟨s i, ⟨i, hi, subset.refl _⟩, H⟩ } end⟩ lemma comap_has_basis (f : α → β) (l : filter β) : has_basis (comap f l) (λ s : set β, s ∈ l) (λ s, f ⁻¹' s) := ⟨λ t, mem_comap⟩ lemma has_basis.forall_mem_mem (h : has_basis l p s) {x : α} : (∀ t ∈ l, x ∈ t) ↔ ∀ i, p i → x ∈ s i := begin simp only [h.mem_iff, exists_imp_distrib], exact ⟨λ h i hi, h (s i) i hi subset.rfl, λ h t i hi ht, ht (h i hi)⟩ end protected lemma has_basis.binfi_mem [complete_lattice β] {f : set α → β} (h : has_basis l p s) (hf : monotone f) : (⨅ t ∈ l, f t) = ⨅ i (hi : p i), f (s i) := le_antisymm (le_infi₂ $ λ i hi, infi₂_le (s i) (h.mem_of_mem hi)) $ le_infi₂ $ λ t ht, let ⟨i, hpi, hi⟩ := h.mem_iff.1 ht in infi₂_le_of_le i hpi (hf hi) protected lemma has_basis.bInter_mem {f : set α → set β} (h : has_basis l p s) (hf : monotone f) : (⋂ t ∈ l, f t) = ⋂ i (hi : p i), f (s i) := h.binfi_mem hf lemma has_basis.sInter_sets (h : has_basis l p s) : ⋂₀ l.sets = ⋂ i (hi : p i), s i := by { rw [sInter_eq_bInter], exact h.bInter_mem monotone_id } variables {ι'' : Type*} [preorder ι''] (l) (s'' : ι'' → set α) /-- `is_antitone_basis s` means the image of `s` is a filter basis such that `s` is decreasing. -/ @[protect_proj] structure is_antitone_basis extends is_basis (λ _, true) s'' : Prop := (antitone : antitone s'') /-- We say that a filter `l` has an antitone basis `s : ι → set α`, if `t ∈ l` if and only if `t` includes `s i` for some `i`, and `s` is decreasing. -/ @[protect_proj] structure has_antitone_basis (l : filter α) (s : ι'' → set α) extends has_basis l (λ _, true) s : Prop := (antitone : antitone s) lemma has_antitone_basis.map {l : filter α} {s : ι'' → set α} {m : α → β} (hf : has_antitone_basis l s) : has_antitone_basis (map m l) (λ n, m '' s n) := ⟨has_basis.map _ hf.to_has_basis, λ i j hij, image_subset _ $ hf.2 hij⟩ end same_type section two_types variables {la : filter α} {pa : ι → Prop} {sa : ι → set α} {lb : filter β} {pb : ι' → Prop} {sb : ι' → set β} {f : α → β} lemma has_basis.tendsto_left_iff (hla : la.has_basis pa sa) : tendsto f la lb ↔ ∀ t ∈ lb, ∃ i (hi : pa i), maps_to f (sa i) t := by { simp only [tendsto, (hla.map f).le_iff, image_subset_iff], refl } lemma has_basis.tendsto_right_iff (hlb : lb.has_basis pb sb) : tendsto f la lb ↔ ∀ i (hi : pb i), ∀ᶠ x in la, f x ∈ sb i := by simpa only [tendsto, hlb.ge_iff, mem_map, filter.eventually] lemma has_basis.tendsto_iff (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : tendsto f la lb ↔ ∀ ib (hib : pb ib), ∃ ia (hia : pa ia), ∀ x ∈ sa ia, f x ∈ sb ib := by simp [hlb.tendsto_right_iff, hla.eventually_iff] lemma tendsto.basis_left (H : tendsto f la lb) (hla : la.has_basis pa sa) : ∀ t ∈ lb, ∃ i (hi : pa i), maps_to f (sa i) t := hla.tendsto_left_iff.1 H lemma tendsto.basis_right (H : tendsto f la lb) (hlb : lb.has_basis pb sb) : ∀ i (hi : pb i), ∀ᶠ x in la, f x ∈ sb i := hlb.tendsto_right_iff.1 H lemma tendsto.basis_both (H : tendsto f la lb) (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : ∀ ib (hib : pb ib), ∃ ia (hia : pa ia), ∀ x ∈ sa ia, f x ∈ sb ib := (hla.tendsto_iff hlb).1 H lemma has_basis.prod_pprod (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : (la ×ᶠ lb).has_basis (λ i : pprod ι ι', pa i.1 ∧ pb i.2) (λ i, sa i.1 ×ˢ sb i.2) := (hla.comap prod.fst).inf' (hlb.comap prod.snd) lemma has_basis.prod {ι ι' : Type*} {pa : ι → Prop} {sa : ι → set α} {pb : ι' → Prop} {sb : ι' → set β} (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : (la ×ᶠ lb).has_basis (λ i : ι × ι', pa i.1 ∧ pb i.2) (λ i, sa i.1 ×ˢ sb i.2) := (hla.comap prod.fst).inf (hlb.comap prod.snd) lemma has_basis.prod_same_index {p : ι → Prop} {sb : ι → set β} (hla : la.has_basis p sa) (hlb : lb.has_basis p sb) (h_dir : ∀ {i j}, p i → p j → ∃ k, p k ∧ sa k ⊆ sa i ∧ sb k ⊆ sb j) : (la ×ᶠ lb).has_basis p (λ i, sa i ×ˢ sb i) := begin simp only [has_basis_iff, (hla.prod_pprod hlb).mem_iff], refine λ t, ⟨_, _⟩, { rintros ⟨⟨i, j⟩, ⟨hi, hj⟩, hsub : sa i ×ˢ sb j ⊆ t⟩, rcases h_dir hi hj with ⟨k, hk, ki, kj⟩, exact ⟨k, hk, (set.prod_mono ki kj).trans hsub⟩ }, { rintro ⟨i, hi, h⟩, exact ⟨⟨i, i⟩, ⟨hi, hi⟩, h⟩ }, end lemma has_basis.prod_same_index_mono {ι : Type*} [linear_order ι] {p : ι → Prop} {sa : ι → set α} {sb : ι → set β} (hla : la.has_basis p sa) (hlb : lb.has_basis p sb) (hsa : monotone_on sa {i | p i}) (hsb : monotone_on sb {i | p i}) : (la ×ᶠ lb).has_basis p (λ i, sa i ×ˢ sb i) := hla.prod_same_index hlb $ λ i j hi hj, have p (min i j), from min_rec' _ hi hj, ⟨min i j, this, hsa this hi $ min_le_left _ _, hsb this hj $ min_le_right _ _⟩ lemma has_basis.prod_same_index_anti {ι : Type*} [linear_order ι] {p : ι → Prop} {sa : ι → set α} {sb : ι → set β} (hla : la.has_basis p sa) (hlb : lb.has_basis p sb) (hsa : antitone_on sa {i | p i}) (hsb : antitone_on sb {i | p i}) : (la ×ᶠ lb).has_basis p (λ i, sa i ×ˢ sb i) := @has_basis.prod_same_index_mono _ _ _ _ ιᵒᵈ _ _ _ _ hla hlb hsa.dual_left hsb.dual_left lemma has_basis.prod_self (hl : la.has_basis pa sa) : (la ×ᶠ la).has_basis pa (λ i, sa i ×ˢ sa i) := hl.prod_same_index hl $ λ i j hi hj, by simpa only [exists_prop, subset_inter_iff] using hl.mem_iff.1 (inter_mem (hl.mem_of_mem hi) (hl.mem_of_mem hj)) lemma mem_prod_self_iff {s} : s ∈ la ×ᶠ la ↔ ∃ t ∈ la, t ×ˢ t ⊆ s := la.basis_sets.prod_self.mem_iff lemma has_antitone_basis.prod {ι : Type*} [linear_order ι] {f : filter α} {g : filter β} {s : ι → set α} {t : ι → set β} (hf : has_antitone_basis f s) (hg : has_antitone_basis g t) : has_antitone_basis (f ×ᶠ g) (λ n, s n ×ˢ t n) := ⟨hf.1.prod_same_index_anti hg.1 (hf.2.antitone_on _) (hg.2.antitone_on _), hf.2.set_prod hg.2⟩ lemma has_basis.coprod {ι ι' : Type*} {pa : ι → Prop} {sa : ι → set α} {pb : ι' → Prop} {sb : ι' → set β} (hla : la.has_basis pa sa) (hlb : lb.has_basis pb sb) : (la.coprod lb).has_basis (λ i : ι × ι', pa i.1 ∧ pb i.2) (λ i, prod.fst ⁻¹' sa i.1 ∪ prod.snd ⁻¹' sb i.2) := (hla.comap prod.fst).sup (hlb.comap prod.snd) end two_types lemma map_sigma_mk_comap {π : α → Type*} {π' : β → Type*} {f : α → β} (hf : function.injective f) (g : Π a, π a → π' (f a)) (a : α) (l : filter (π' (f a))) : map (sigma.mk a) (comap (g a) l) = comap (sigma.map f g) (map (sigma.mk (f a)) l) := begin refine (((basis_sets _).comap _).map _).eq_of_same_basis _, convert ((basis_sets _).map _).comap _, ext1 s, apply image_sigma_mk_preimage_sigma_map hf end end filter end sort namespace filter variables {α β γ ι : Type*} {ι' : Sort*} /-- `is_countably_generated f` means `f = generate s` for some countable `s`. -/ class is_countably_generated (f : filter α) : Prop := (out [] : ∃ s : set (set α), s.countable ∧ f = generate s) /-- `is_countable_basis p s` means the image of `s` bounded by `p` is a countable filter basis. -/ structure is_countable_basis (p : ι → Prop) (s : ι → set α) extends is_basis p s : Prop := (countable : (set_of p).countable) /-- We say that a filter `l` has a countable basis `s : ι → set α` bounded by `p : ι → Prop`, if `t ∈ l` if and only if `t` includes `s i` for some `i` such that `p i`, and the set defined by `p` is countable. -/ structure has_countable_basis (l : filter α) (p : ι → Prop) (s : ι → set α) extends has_basis l p s : Prop := (countable : (set_of p).countable) /-- A countable filter basis `B` on a type `α` is a nonempty countable collection of sets of `α` such that the intersection of two elements of this collection contains some element of the collection. -/ structure countable_filter_basis (α : Type*) extends filter_basis α := (countable : sets.countable) -- For illustration purposes, the countable filter basis defining (at_top : filter ℕ) instance nat.inhabited_countable_filter_basis : inhabited (countable_filter_basis ℕ) := ⟨{ countable := countable_range (λ n, Ici n), ..(default : filter_basis ℕ) }⟩ lemma has_countable_basis.is_countably_generated {f : filter α} {p : ι → Prop} {s : ι → set α} (h : f.has_countable_basis p s) : f.is_countably_generated := ⟨⟨{t | ∃ i, p i ∧ s i = t}, h.countable.image s, h.to_has_basis.eq_generate⟩⟩ lemma antitone_seq_of_seq (s : ℕ → set α) : ∃ t : ℕ → set α, antitone t ∧ (⨅ i, 𝓟 $ s i) = ⨅ i, 𝓟 (t i) := begin use λ n, ⋂ m ≤ n, s m, split, { exact λ i j hij, bInter_mono (Iic_subset_Iic.2 hij) (λ n hn, subset.refl _) }, apply le_antisymm; rw le_infi_iff; intro i, { rw le_principal_iff, refine (bInter_mem (finite_le_nat _)).2 (λ j hji, _), rw ← le_principal_iff, apply infi_le_of_le j _, exact le_rfl }, { apply infi_le_of_le i _, rw principal_mono, intro a, simp, intro h, apply h, refl }, end lemma countable_binfi_eq_infi_seq [complete_lattice α] {B : set ι} (Bcbl : B.countable) (Bne : B.nonempty) (f : ι → α) : ∃ (x : ℕ → ι), (⨅ t ∈ B, f t) = ⨅ i, f (x i) := let ⟨g, hg⟩ := Bcbl.exists_eq_range Bne in ⟨g, hg.symm ▸ infi_range⟩ lemma countable_binfi_eq_infi_seq' [complete_lattice α] {B : set ι} (Bcbl : B.countable) (f : ι → α) {i₀ : ι} (h : f i₀ = ⊤) : ∃ (x : ℕ → ι), (⨅ t ∈ B, f t) = ⨅ i, f (x i) := begin cases B.eq_empty_or_nonempty with hB Bnonempty, { rw [hB, infi_emptyset], use λ n, i₀, simp [h] }, { exact countable_binfi_eq_infi_seq Bcbl Bnonempty f } end lemma countable_binfi_principal_eq_seq_infi {B : set (set α)} (Bcbl : B.countable) : ∃ (x : ℕ → set α), (⨅ t ∈ B, 𝓟 t) = ⨅ i, 𝓟 (x i) := countable_binfi_eq_infi_seq' Bcbl 𝓟 principal_univ section is_countably_generated protected lemma has_antitone_basis.mem_iff [preorder ι] {l : filter α} {s : ι → set α} (hs : l.has_antitone_basis s) {t : set α} : t ∈ l ↔ ∃ i, s i ⊆ t := hs.to_has_basis.mem_iff.trans $ by simp only [exists_prop, true_and] protected lemma has_antitone_basis.mem [preorder ι] {l : filter α} {s : ι → set α} (hs : l.has_antitone_basis s) (i : ι) : s i ∈ l := hs.to_has_basis.mem_of_mem trivial lemma has_antitone_basis.has_basis_ge [preorder ι] [is_directed ι (≤)] {l : filter α} {s : ι → set α} (hs : l.has_antitone_basis s) (i : ι) : l.has_basis (λ j, i ≤ j) s := hs.1.to_has_basis (λ j _, (exists_ge_ge i j).imp $ λ k hk, ⟨hk.1, hs.2 hk.2⟩) (λ j hj, ⟨j, trivial, subset.rfl⟩) /-- If `f` is countably generated and `f.has_basis p s`, then `f` admits a decreasing basis enumerated by natural numbers such that all sets have the form `s i`. More precisely, there is a sequence `i n` such that `p (i n)` for all `n` and `s (i n)` is a decreasing sequence of sets which forms a basis of `f`-/ lemma has_basis.exists_antitone_subbasis {f : filter α} [h : f.is_countably_generated] {p : ι' → Prop} {s : ι' → set α} (hs : f.has_basis p s) : ∃ x : ℕ → ι', (∀ i, p (x i)) ∧ f.has_antitone_basis (λ i, s (x i)) := begin obtain ⟨x', hx'⟩ : ∃ x : ℕ → set α, f = ⨅ i, 𝓟 (x i), { unfreezingI { rcases h with ⟨s, hsc, rfl⟩ }, rw generate_eq_binfi, exact countable_binfi_principal_eq_seq_infi hsc }, have : ∀ i, x' i ∈ f := λ i, hx'.symm ▸ (infi_le (λ i, 𝓟 (x' i)) i) (mem_principal_self _), let x : ℕ → {i : ι' // p i} := λ n, nat.rec_on n (hs.index _ $ this 0) (λ n xn, (hs.index _ $ inter_mem (this $ n + 1) (hs.mem_of_mem xn.2))), have x_mono : antitone (λ i, s (x i)), { refine antitone_nat_of_succ_le (λ i, _), exact (hs.set_index_subset _).trans (inter_subset_right _ _) }, have x_subset : ∀ i, s (x i) ⊆ x' i, { rintro (_|i), exacts [hs.set_index_subset _, subset.trans (hs.set_index_subset _) (inter_subset_left _ _)] }, refine ⟨λ i, x i, λ i, (x i).2, _⟩, have : (⨅ i, 𝓟 (s (x i))).has_antitone_basis (λ i, s (x i)) := ⟨has_basis_infi_principal (directed_of_sup x_mono), x_mono⟩, convert this, exact le_antisymm (le_infi $ λ i, le_principal_iff.2 $ by cases i; apply hs.set_index_mem) (hx'.symm ▸ le_infi (λ i, le_principal_iff.2 $ this.to_has_basis.mem_iff.2 ⟨i, trivial, x_subset i⟩)) end /-- A countably generated filter admits a basis formed by an antitone sequence of sets. -/ lemma exists_antitone_basis (f : filter α) [f.is_countably_generated] : ∃ x : ℕ → set α, f.has_antitone_basis x := let ⟨x, hxf, hx⟩ := f.basis_sets.exists_antitone_subbasis in ⟨x, hx⟩ lemma exists_antitone_seq (f : filter α) [f.is_countably_generated] : ∃ x : ℕ → set α, antitone x ∧ ∀ {s}, (s ∈ f ↔ ∃ i, x i ⊆ s) := let ⟨x, hx⟩ := f.exists_antitone_basis in ⟨x, hx.antitone, λ s, by simp [hx.to_has_basis.mem_iff]⟩ instance inf.is_countably_generated (f g : filter α) [is_countably_generated f] [is_countably_generated g] : is_countably_generated (f ⊓ g) := begin rcases f.exists_antitone_basis with ⟨s, hs⟩, rcases g.exists_antitone_basis with ⟨t, ht⟩, exact has_countable_basis.is_countably_generated ⟨hs.to_has_basis.inf ht.to_has_basis, set.to_countable _⟩ end instance map.is_countably_generated (l : filter α) [l.is_countably_generated] (f : α → β) : (map f l).is_countably_generated := let ⟨x, hxl⟩ := l.exists_antitone_basis in has_countable_basis.is_countably_generated ⟨hxl.map.to_has_basis, to_countable _⟩ instance comap.is_countably_generated (l : filter β) [l.is_countably_generated] (f : α → β) : (comap f l).is_countably_generated := let ⟨x, hxl⟩ := l.exists_antitone_basis in has_countable_basis.is_countably_generated ⟨hxl.to_has_basis.comap _, to_countable _⟩ instance sup.is_countably_generated (f g : filter α) [is_countably_generated f] [is_countably_generated g] : is_countably_generated (f ⊔ g) := begin rcases f.exists_antitone_basis with ⟨s, hs⟩, rcases g.exists_antitone_basis with ⟨t, ht⟩, exact has_countable_basis.is_countably_generated ⟨hs.to_has_basis.sup ht.to_has_basis, set.to_countable _⟩ end instance prod.is_countably_generated (la : filter α) (lb : filter β) [is_countably_generated la] [is_countably_generated lb] : is_countably_generated (la ×ᶠ lb) := filter.inf.is_countably_generated _ _ instance coprod.is_countably_generated (la : filter α) (lb : filter β) [is_countably_generated la] [is_countably_generated lb] : is_countably_generated (la.coprod lb) := filter.sup.is_countably_generated _ _ end is_countably_generated lemma is_countably_generated_seq [countable β] (x : β → set α) : is_countably_generated (⨅ i, 𝓟 $ x i) := begin use [range x, countable_range x], rw [generate_eq_binfi, infi_range] end lemma is_countably_generated_of_seq {f : filter α} (h : ∃ x : ℕ → set α, f = ⨅ i, 𝓟 $ x i) : f.is_countably_generated := let ⟨x, h⟩ := h in by rw h ; apply is_countably_generated_seq lemma is_countably_generated_binfi_principal {B : set $ set α} (h : B.countable) : is_countably_generated (⨅ (s ∈ B), 𝓟 s) := is_countably_generated_of_seq (countable_binfi_principal_eq_seq_infi h) lemma is_countably_generated_iff_exists_antitone_basis {f : filter α} : is_countably_generated f ↔ ∃ x : ℕ → set α, f.has_antitone_basis x := begin split, { introI h, exact f.exists_antitone_basis }, { rintros ⟨x, h⟩, rw h.to_has_basis.eq_infi, exact is_countably_generated_seq x }, end @[instance] lemma is_countably_generated_principal (s : set α) : is_countably_generated (𝓟 s) := is_countably_generated_of_seq ⟨λ _, s, infi_const.symm⟩ @[instance] lemma is_countably_generated_pure (a : α) : is_countably_generated (pure a) := by { rw ← principal_singleton, exact is_countably_generated_principal _, } @[instance] lemma is_countably_generated_bot : is_countably_generated (⊥ : filter α) := @principal_empty α ▸ is_countably_generated_principal _ @[instance] lemma is_countably_generated_top : is_countably_generated (⊤ : filter α) := @principal_univ α ▸ is_countably_generated_principal _ instance infi.is_countably_generated {ι : Sort*} [countable ι] (f : ι → filter α) [∀ i, is_countably_generated (f i)] : is_countably_generated (⨅ i, f i) := begin choose s hs using λ i, exists_antitone_basis (f i), rw [← plift.down_surjective.infi_comp], refine has_countable_basis.is_countably_generated ⟨has_basis_infi (λ n, (hs _).to_has_basis), _⟩, refine (countable_range $ sigma.map (coe : finset (plift ι) → set (plift ι)) (λ _, id)).mono _, rintro ⟨I, f⟩ ⟨hI, -⟩, lift I to finset (plift ι) using hI, exact ⟨⟨I, f⟩, rfl⟩ end end filter
91fcc2935f04f1681337b293275f340235386304
f4bff2062c030df03d65e8b69c88f79b63a359d8
/src/game/sup_inf/level02.lean
588b3036654dffd0fa86da6e4ebaf8d2705f672e
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,637
lean
import game.sup_inf.level01 namespace xena -- hide -- World name : Sup and Inf /- # Chapter 3 : Sup and Inf ## Level 2 -/ /- The completeness axiom on the reals states that any non-empty subset $X \subseteq \mathbb{R}$ that is bounded above has a least upper bound. Here we explore the converse statement: any set of reals that has a supremum is non-empty and has an upper bound. The second part of the result is trivial, but showing that the set is non-empty will ask you to use techniques learned in the first world. -/ -- definition is_upper_bound' (S : set ℝ) (x : ℝ) := x ∈ upper_bounds S -- (Definition above deprecated? GT) definition is_lub (S : set ℝ) (x : ℝ) := is_upper_bound S x ∧ ∀ y : ℝ, is_upper_bound S y → x ≤ y definition has_lub (S : set ℝ) := ∃ x, is_lub S x local attribute [instance] classical.prop_decidable --hide /- Lemma Any set of reals that has a supremum is non-empty and bounded above. -/ theorem nonempty_and_bounded_of_has_LUB (S : set ℝ) (H : has_lub S) : (S ≠ ∅) ∧ (∃ x, is_upper_bound S x) := begin cases H with b Hb, -- b is LUB, Hb is proof it's LUB split, { -- first prove S is not empty, by contradiction as usual with empty sets intro Hempty, have H1 : (b-1) ∈ upper_bounds S, change ∀ x ∈ S, x ≤ (b-1), by_contradiction hn, push_neg at hn, cases hn with x h1, cases h1 with h11 h12, rw Hempty at h11, exact h11, unfold is_lub at Hb, have HH := Hb.2 (b-1) H1, -- b - 1 is an upper bound linarith, }, { existsi b, exact Hb.1, }, done end end xena -- hide
291f8c1614f594f9b74df4fa3c6643ce0ebb37b7
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/metric_space/lipschitz.lean
90442933460f918034828fb48321812594685dac
[]
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
14,098
lean
/- Copyright (c) 2018 Rohan Mitta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rohan Mitta, Kevin Buzzard, Alistair Tucker, Johannes Hölzl, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.logic.function.iterate import Mathlib.topology.metric_space.basic import Mathlib.category_theory.endomorphism import Mathlib.category_theory.types import Mathlib.PostPort universes u v w x namespace Mathlib /-! # Lipschitz continuous functions A map `f : α → β` between two (extended) metric spaces is called *Lipschitz continuous* with constant `K ≥ 0` if for all `x, y` we have `edist (f x) (f y) ≤ K * edist x y`. For a metric space, the latter inequality is equivalent to `dist (f x) (f y) ≤ K * dist x y`. There is also a version asserting this inequality only for `x` and `y` in some set `s`. In this file we provide various ways to prove that various combinations of Lipschitz continuous functions are Lipschitz continuous. We also prove that Lipschitz continuous functions are uniformly continuous. ## Main definitions and lemmas * `lipschitz_with K f`: states that `f` is Lipschitz with constant `K : ℝ≥0` * `lipschitz_on_with K f`: states that `f` is Lipschitz with constant `K : ℝ≥0` on a set `s` * `lipschitz_with.uniform_continuous`: a Lipschitz function is uniformly continuous * `lipschitz_on_with.uniform_continuous_on`: a function which is Lipschitz on a set is uniformly continuous on that set. ## Implementation notes The parameter `K` has type `ℝ≥0`. This way we avoid conjuction in the definition and have coercions both to `ℝ` and `ennreal`. Constructors whose names end with `'` take `K : ℝ` as an argument, and return `lipschitz_with (nnreal.of_real K) f`. -/ /-- A function `f` is Lipschitz continuous with constant `K ≥ 0` if for all `x, y` we have `dist (f x) (f y) ≤ K * dist x y` -/ def lipschitz_with {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (K : nnreal) (f : α → β) := ∀ (x y : α), edist (f x) (f y) ≤ ↑K * edist x y theorem lipschitz_with_iff_dist_le_mul {α : Type u} {β : Type v} [metric_space α] [metric_space β] {K : nnreal} {f : α → β} : lipschitz_with K f ↔ ∀ (x y : α), dist (f x) (f y) ≤ ↑K * dist x y := sorry theorem lipschitz_with.dist_le_mul {α : Type u} {β : Type v} [metric_space α] [metric_space β] {K : nnreal} {f : α → β} : lipschitz_with K f → ∀ (x y : α), dist (f x) (f y) ≤ ↑K * dist x y := iff.mp lipschitz_with_iff_dist_le_mul /-- A function `f` is Lipschitz continuous with constant `K ≥ 0` on `s` if for all `x, y` in `s` we have `dist (f x) (f y) ≤ K * dist x y` -/ def lipschitz_on_with {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (K : nnreal) (f : α → β) (s : set α) := ∀ {x : α}, x ∈ s → ∀ {y : α}, y ∈ s → edist (f x) (f y) ≤ ↑K * edist x y @[simp] theorem lipschitz_on_with_empty {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (K : nnreal) (f : α → β) : lipschitz_on_with K f ∅ := fun (x : α) (x_in : x ∈ ∅) (y : α) (y_in : y ∈ ∅) => false.elim x_in theorem lipschitz_on_with.mono {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {s : set α} {t : set α} {f : α → β} (hf : lipschitz_on_with K f t) (h : s ⊆ t) : lipschitz_on_with K f s := fun (x : α) (x_in : x ∈ s) (y : α) (y_in : y ∈ s) => hf (h x_in) (h y_in) theorem lipschitz_on_with_iff_dist_le_mul {α : Type u} {β : Type v} [metric_space α] [metric_space β] {K : nnreal} {s : set α} {f : α → β} : lipschitz_on_with K f s ↔ ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → dist (f x) (f y) ≤ ↑K * dist x y := sorry theorem lipschitz_on_with.dist_le_mul {α : Type u} {β : Type v} [metric_space α] [metric_space β] {K : nnreal} {s : set α} {f : α → β} : lipschitz_on_with K f s → ∀ (x : α), x ∈ s → ∀ (y : α), y ∈ s → dist (f x) (f y) ≤ ↑K * dist x y := iff.mp lipschitz_on_with_iff_dist_le_mul @[simp] theorem lipschitz_on_univ {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} : lipschitz_on_with K f set.univ ↔ lipschitz_with K f := sorry theorem lipschitz_on_with_iff_restrict {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} {s : set α} : lipschitz_on_with K f s ↔ lipschitz_with K (set.restrict f s) := sorry namespace lipschitz_with theorem edist_le_mul {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (h : lipschitz_with K f) (x : α) (y : α) : edist (f x) (f y) ≤ ↑K * edist x y := h x y theorem edist_lt_top {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) {x : α} {y : α} (h : edist x y < ⊤) : edist (f x) (f y) < ⊤ := lt_of_le_of_lt (hf x y) (ennreal.mul_lt_top ennreal.coe_lt_top h) theorem mul_edist_le {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (h : lipschitz_with K f) (x : α) (y : α) : ↑K⁻¹ * edist (f x) (f y) ≤ edist x y := sorry protected theorem of_edist_le {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {f : α → β} (h : ∀ (x y : α), edist (f x) (f y) ≤ edist x y) : lipschitz_with 1 f := sorry protected theorem weaken {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) {K' : nnreal} (h : K ≤ K') : lipschitz_with K' f := fun (x y : α) => le_trans (hf x y) (ennreal.mul_right_mono (iff.mpr ennreal.coe_le_coe h)) theorem ediam_image_le {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) (s : set α) : emetric.diam (f '' s) ≤ ↑K * emetric.diam s := sorry /-- A Lipschitz function is uniformly continuous -/ protected theorem uniform_continuous {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) : uniform_continuous f := sorry /-- A Lipschitz function is continuous -/ protected theorem continuous {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) : continuous f := uniform_continuous.continuous (lipschitz_with.uniform_continuous hf) protected theorem const {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] (b : β) : lipschitz_with 0 fun (a : α) => b := sorry protected theorem id {α : Type u} [emetric_space α] : lipschitz_with 1 id := lipschitz_with.of_edist_le fun (x y : α) => le_refl (edist (id x) (id y)) protected theorem subtype_val {α : Type u} [emetric_space α] (s : set α) : lipschitz_with 1 subtype.val := lipschitz_with.of_edist_le fun (x y : Subtype fun (x : α) => x ∈ s) => le_refl (edist (subtype.val x) (subtype.val y)) protected theorem subtype_coe {α : Type u} [emetric_space α] (s : set α) : lipschitz_with 1 coe := lipschitz_with.subtype_val s protected theorem restrict {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) (s : set α) : lipschitz_with K (set.restrict f s) := fun (x y : ↥s) => hf ↑x ↑y protected theorem comp {α : Type u} {β : Type v} {γ : Type w} [emetric_space α] [emetric_space β] [emetric_space γ] {Kf : nnreal} {Kg : nnreal} {f : β → γ} {g : α → β} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf * Kg) (f ∘ g) := sorry protected theorem prod_fst {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] : lipschitz_with 1 prod.fst := lipschitz_with.of_edist_le fun (x y : α × β) => le_max_left (edist (prod.fst x) (prod.fst y)) (edist (prod.snd x) (prod.snd y)) protected theorem prod_snd {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] : lipschitz_with 1 prod.snd := lipschitz_with.of_edist_le fun (x y : α × β) => le_max_right (edist (prod.fst x) (prod.fst y)) (edist (prod.snd x) (prod.snd y)) protected theorem prod {α : Type u} {β : Type v} {γ : Type w} [emetric_space α] [emetric_space β] [emetric_space γ] {f : α → β} {Kf : nnreal} (hf : lipschitz_with Kf f) {g : α → γ} {Kg : nnreal} (hg : lipschitz_with Kg g) : lipschitz_with (max Kf Kg) fun (x : α) => (f x, g x) := sorry protected theorem uncurry {α : Type u} {β : Type v} {γ : Type w} [emetric_space α] [emetric_space β] [emetric_space γ] {f : α → β → γ} {Kα : nnreal} {Kβ : nnreal} (hα : ∀ (b : β), lipschitz_with Kα fun (a : α) => f a b) (hβ : ∀ (a : α), lipschitz_with Kβ (f a)) : lipschitz_with (Kα + Kβ) (function.uncurry f) := sorry protected theorem iterate {α : Type u} [emetric_space α] {K : nnreal} {f : α → α} (hf : lipschitz_with K f) (n : ℕ) : lipschitz_with (K ^ n) (nat.iterate f n) := sorry theorem edist_iterate_succ_le_geometric {α : Type u} [emetric_space α] {K : nnreal} {f : α → α} (hf : lipschitz_with K f) (x : α) (n : ℕ) : edist (nat.iterate f n x) (nat.iterate f (n + 1) x) ≤ edist x (f x) * ↑K ^ n := sorry protected theorem mul {α : Type u} [emetric_space α] {f : category_theory.End α} {g : category_theory.End α} {Kf : nnreal} {Kg : nnreal} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf * Kg) (f * g) := lipschitz_with.comp hf hg /-- The product of a list of Lipschitz continuous endomorphisms is a Lipschitz continuous endomorphism. -/ protected theorem list_prod {α : Type u} {ι : Type x} [emetric_space α] (f : ι → category_theory.End α) (K : ι → nnreal) (h : ∀ (i : ι), lipschitz_with (K i) (f i)) (l : List ι) : lipschitz_with (list.prod (list.map K l)) (list.prod (list.map f l)) := sorry protected theorem pow {α : Type u} [emetric_space α] {f : category_theory.End α} {K : nnreal} (h : lipschitz_with K f) (n : ℕ) : lipschitz_with (K ^ n) (f ^ n) := sorry protected theorem of_dist_le' {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} {K : ℝ} (h : ∀ (x y : α), dist (f x) (f y) ≤ K * dist x y) : lipschitz_with (nnreal.of_real K) f := of_dist_le_mul fun (x y : α) => le_trans (h x y) (mul_le_mul_of_nonneg_right (nnreal.le_coe_of_real K) dist_nonneg) protected theorem mk_one {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} (h : ∀ (x y : α), dist (f x) (f y) ≤ dist x y) : lipschitz_with 1 f := sorry /-- For functions to `ℝ`, it suffices to prove `f x ≤ f y + K * dist x y`; this version doesn't assume `0≤K`. -/ protected theorem of_le_add_mul' {α : Type u} [metric_space α] {f : α → ℝ} (K : ℝ) (h : ∀ (x y : α), f x ≤ f y + K * dist x y) : lipschitz_with (nnreal.of_real K) f := sorry /-- For functions to `ℝ`, it suffices to prove `f x ≤ f y + K * dist x y`; this version assumes `0≤K`. -/ protected theorem of_le_add_mul {α : Type u} [metric_space α] {f : α → ℝ} (K : nnreal) (h : ∀ (x y : α), f x ≤ f y + ↑K * dist x y) : lipschitz_with K f := sorry protected theorem of_le_add {α : Type u} [metric_space α] {f : α → ℝ} (h : ∀ (x y : α), f x ≤ f y + dist x y) : lipschitz_with 1 f := sorry protected theorem le_add_mul {α : Type u} [metric_space α] {f : α → ℝ} {K : nnreal} (h : lipschitz_with K f) (x : α) (y : α) : f x ≤ f y + ↑K * dist x y := iff.mp sub_le_iff_le_add' (le_trans (le_abs_self (f x - f y)) (dist_le_mul h x y)) protected theorem iff_le_add_mul {α : Type u} [metric_space α] {f : α → ℝ} {K : nnreal} : lipschitz_with K f ↔ ∀ (x y : α), f x ≤ f y + ↑K * dist x y := { mp := lipschitz_with.le_add_mul, mpr := lipschitz_with.of_le_add_mul K } theorem nndist_le {α : Type u} {β : Type v} [metric_space α] [metric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) (x : α) (y : α) : nndist (f x) (f y) ≤ K * nndist x y := dist_le_mul hf x y theorem diam_image_le {α : Type u} {β : Type v} [metric_space α] [metric_space β] {K : nnreal} {f : α → β} (hf : lipschitz_with K f) (s : set α) (hs : metric.bounded s) : metric.diam (f '' s) ≤ ↑K * metric.diam s := sorry protected theorem dist_left {α : Type u} [metric_space α] (y : α) : lipschitz_with 1 fun (x : α) => dist x y := sorry protected theorem dist_right {α : Type u} [metric_space α] (x : α) : lipschitz_with 1 (dist x) := lipschitz_with.of_le_add fun (y z : α) => dist_triangle_right x y z protected theorem dist {α : Type u} [metric_space α] : lipschitz_with (bit0 1) (function.uncurry dist) := lipschitz_with.uncurry lipschitz_with.dist_left lipschitz_with.dist_right theorem dist_iterate_succ_le_geometric {α : Type u} [metric_space α] {K : nnreal} {f : α → α} (hf : lipschitz_with K f) (x : α) (n : ℕ) : dist (nat.iterate f n x) (nat.iterate f (n + 1) x) ≤ dist x (f x) * ↑K ^ n := sorry end lipschitz_with namespace lipschitz_on_with protected theorem uniform_continuous_on {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {s : set α} {f : α → β} (hf : lipschitz_on_with K f s) : uniform_continuous_on f s := iff.mpr uniform_continuous_on_iff_restrict (lipschitz_with.uniform_continuous (iff.mp lipschitz_on_with_iff_restrict hf)) protected theorem continuous_on {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {K : nnreal} {s : set α} {f : α → β} (hf : lipschitz_on_with K f s) : continuous_on f s := uniform_continuous_on.continuous_on (lipschitz_on_with.uniform_continuous_on hf) end lipschitz_on_with /-- If a function is locally Lipschitz around a point, then it is continuous at this point. -/ theorem continuous_at_of_locally_lipschitz {α : Type u} {β : Type v} [metric_space α] [metric_space β] {f : α → β} {x : α} {r : ℝ} (hr : 0 < r) (K : ℝ) (h : ∀ (y : α), dist y x < r → dist (f y) (f x) ≤ K * dist y x) : continuous_at f x := sorry
d405a3b920a99b7a88d53c611d2f1cf42d46a1e5
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/hott/types/pointed2.hlean
6b2d12ffda227c79659f5f6b788e449c71e8e3ca
[ "Apache-2.0" ]
permissive
tectronics/lean
ab977ba6be0fcd46047ddbb3c8e16e7c26710701
f38af35e0616f89c6e9d7e3eb1d48e47ee666efe
refs/heads/master
1,532,358,526,384
1,456,276,623,000
1,456,276,623,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,360
hlean
/- Copyright (c) 2014 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Ported from Coq HoTT -/ import .equiv cubical.square open eq is_equiv equiv equiv.ops pointed is_trunc -- structure pequiv (A B : Type*) := -- (to_pmap : A →* B) -- (is_equiv_to_pmap : is_equiv to_pmap) structure pequiv (A B : Type*) extends equiv A B, pmap A B namespace pointed attribute pequiv._trans_of_to_pmap pequiv._trans_of_to_equiv pequiv.to_pmap pequiv.to_equiv [unfold 3] variables {A B C : Type*} /- pointed equivalences -/ infix ` ≃* `:25 := pequiv attribute pequiv.to_pmap [coercion] attribute pequiv.to_is_equiv [instance] definition pequiv_of_pmap [constructor] (f : A →* B) (H : is_equiv f) : A ≃* B := pequiv.mk f _ (respect_pt f) definition pequiv_of_equiv [constructor] (f : A ≃ B) (H : f pt = pt) : A ≃* B := pequiv.mk f _ H definition equiv_of_pequiv [constructor] (f : A ≃* B) : A ≃ B := equiv.mk f _ definition to_pinv [constructor] (f : A ≃* B) : B →* A := pmap.mk f⁻¹ (ap f⁻¹ (respect_pt f)⁻¹ ⬝ !left_inv) definition pua {A B : Type*} (f : A ≃* B) : A = B := pType_eq (equiv_of_pequiv f) !respect_pt protected definition pequiv.refl [refl] [constructor] (A : Type*) : A ≃* A := pequiv_of_pmap !pid !is_equiv_id protected definition pequiv.rfl [constructor] : A ≃* A := pequiv.refl A protected definition pequiv.symm [symm] (f : A ≃* B) : B ≃* A := pequiv_of_pmap (to_pinv f) !is_equiv_inv protected definition pequiv.trans [trans] (f : A ≃* B) (g : B ≃* C) : A ≃* C := pequiv_of_pmap (pcompose g f) !is_equiv_compose postfix `⁻¹ᵉ*`:(max + 1) := pequiv.symm infix ` ⬝e* `:75 := pequiv.trans definition pequiv_rect' (f : A ≃* B) (P : A → B → Type) (g : Πb, P (f⁻¹ b) b) (a : A) : P a (f a) := left_inv f a ▸ g (f a) definition pequiv_of_eq [constructor] {A B : Type*} (p : A = B) : A ≃* B := pequiv_of_pmap (pcast p) !is_equiv_tr definition peconcat_eq {A B C : Type*} (p : A ≃* B) (q : B = C) : A ≃* C := p ⬝e* pequiv_of_eq q definition eq_peconcat {A B C : Type*} (p : A = B) (q : B ≃* C) : A ≃* C := pequiv_of_eq p ⬝e* q definition eq_of_pequiv {A B : Type*} (p : A ≃* B) : A = B := pType_eq (equiv_of_pequiv p) !respect_pt definition peap {A B : Type*} (F : Type* → Type*) (p : A ≃* B) : F A ≃* F B := pequiv_of_pmap (pcast (ap F (eq_of_pequiv p))) begin cases eq_of_pequiv p, apply is_equiv_id end definition loop_space_pequiv [constructor] (p : A ≃* B) : Ω A ≃* Ω B := pequiv_of_pmap (ap1 p) (is_equiv_ap1 p) definition pequiv_eq {p q : A ≃* B} (H : p = q :> (A →* B)) : p = q := begin cases p with f Hf, cases q with g Hg, esimp at *, exact apd011 pequiv_of_pmap H !is_prop.elim end definition loop_space_pequiv_rfl : loop_space_pequiv (@pequiv.refl A) = @pequiv.refl (Ω A) := begin apply pequiv_eq, fapply pmap_eq: esimp, { intro p, exact !idp_con ⬝ !ap_id}, { reflexivity} end infix ` ⬝e*p `:75 := peconcat_eq infix ` ⬝pe* `:75 := eq_peconcat local attribute pequiv.symm [constructor] definition pleft_inv (f : A ≃* B) : f⁻¹ᵉ* ∘* f ~* pid A := phomotopy.mk (left_inv f) abstract begin esimp, rewrite ap_inv, symmetry, apply con_inv_cancel_left end end definition pright_inv (f : A ≃* B) : f ∘* f⁻¹ᵉ* ~* pid B := phomotopy.mk (right_inv f) abstract begin induction f with f H p, esimp, rewrite [ap_con, +ap_inv, -adj f, -ap_compose], note q := natural_square (right_inv f) p, rewrite [ap_id at q], apply eq_bot_of_square, exact transpose q end end definition pcancel_left (f : B ≃* C) {g h : A →* B} (p : f ∘* g ~* f ∘* h) : g ~* h := begin refine _⁻¹* ⬝* pwhisker_left f⁻¹ᵉ* p ⬝* _: refine !passoc⁻¹* ⬝* _: refine pwhisker_right _ (pleft_inv f) ⬝* _: apply pid_comp end definition pcancel_right (f : A ≃* B) {g h : B →* C} (p : g ∘* f ~* h ∘* f) : g ~* h := begin refine _⁻¹* ⬝* pwhisker_right f⁻¹ᵉ* p ⬝* _: refine !passoc ⬝* _: refine pwhisker_left _ (pright_inv f) ⬝* _: apply comp_pid end definition phomotopy_pinv_right_of_phomotopy {f : A ≃* B} {g : B →* C} {h : A →* C} (p : g ∘* f ~* h) : g ~* h ∘* f⁻¹ᵉ* := begin refine _ ⬝* pwhisker_right _ p, symmetry, refine !passoc ⬝* _, refine pwhisker_left _ (pright_inv f) ⬝* _, apply comp_pid end definition phomotopy_of_pinv_right_phomotopy {f : B ≃* A} {g : B →* C} {h : A →* C} (p : g ∘* f⁻¹ᵉ* ~* h) : g ~* h ∘* f := begin refine _ ⬝* pwhisker_right _ p, symmetry, refine !passoc ⬝* _, refine pwhisker_left _ (pleft_inv f) ⬝* _, apply comp_pid end definition pinv_right_phomotopy_of_phomotopy {f : A ≃* B} {g : B →* C} {h : A →* C} (p : h ~* g ∘* f) : h ∘* f⁻¹ᵉ* ~* g := (phomotopy_pinv_right_of_phomotopy p⁻¹*)⁻¹* definition phomotopy_of_phomotopy_pinv_right {f : B ≃* A} {g : B →* C} {h : A →* C} (p : h ~* g ∘* f⁻¹ᵉ*) : h ∘* f ~* g := (phomotopy_of_pinv_right_phomotopy p⁻¹*)⁻¹* definition phomotopy_pinv_left_of_phomotopy {f : B ≃* C} {g : A →* B} {h : A →* C} (p : f ∘* g ~* h) : g ~* f⁻¹ᵉ* ∘* h := begin refine _ ⬝* pwhisker_left _ p, symmetry, refine !passoc⁻¹* ⬝* _, refine pwhisker_right _ (pleft_inv f) ⬝* _, apply pid_comp end definition phomotopy_of_pinv_left_phomotopy {f : C ≃* B} {g : A →* B} {h : A →* C} (p : f⁻¹ᵉ* ∘* g ~* h) : g ~* f ∘* h := begin refine _ ⬝* pwhisker_left _ p, symmetry, refine !passoc⁻¹* ⬝* _, refine pwhisker_right _ (pright_inv f) ⬝* _, apply pid_comp end definition pinv_left_phomotopy_of_phomotopy {f : B ≃* C} {g : A →* B} {h : A →* C} (p : h ~* f ∘* g) : f⁻¹ᵉ* ∘* h ~* g := (phomotopy_pinv_left_of_phomotopy p⁻¹*)⁻¹* definition phomotopy_of_phomotopy_pinv_left {f : C ≃* B} {g : A →* B} {h : A →* C} (p : h ~* f⁻¹ᵉ* ∘* g) : f ∘* h ~* g := (phomotopy_of_pinv_left_phomotopy p⁻¹*)⁻¹* end pointed
e97c7c55f077663457fa508fd30ab44068b54b29
09b3e1beaeff2641ac75019c9f735d79d508071d
/Mathlib/Data/Equiv/Basic.lean
845ea77c780161e8ab12d4e6d77b07fff04a4c56
[ "Apache-2.0" ]
permissive
abentkamp/mathlib4
b819079cc46426b3c5c77413504b07541afacc19
f8294a67548f8f3d1f5913677b070a2ef5bcf120
refs/heads/master
1,685,309,252,764
1,623,232,534,000
1,623,232,534,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,953
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 Mathlib.Logic.Function.Basic /-! # Equivalence between types * `equiv α β` a.k.a. `α ≃ β`: a bijective map `α → β` bundled with its inverse map; we use this (and not equality!) to express that various `Type`s or `Sort`s are equivalent. ## Tags equivalence, congruence, bijective map -/ open Function universes u v w variable {α : Sort u} {β : Sort v} {γ : Sort w} /-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/ structure equiv (α : Sort u) (β : Sort v) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix:25 " ≃ " => equiv namespace equiv /-- `perm α` is the type of bijections from `α` to itself. -/ @[reducible] def perm (α : Sort u) := equiv α α instance : CoeFun (α ≃ β) (λ _ => α → β):= ⟨to_fun⟩ @[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f := rfl def refl (α) : α ≃ α := ⟨id, id, λ _ => rfl, λ _ => rfl⟩ def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ def trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ := ⟨e₂ ∘ (e₁ : α → β), e₁.symm ∘ (e₂.symm : γ → β), e₂.left_inv.comp e₁.left_inv, e₂.right_inv.comp e₁.right_inv⟩ @[simp] theorem apply_symm_apply (e : α ≃ β) (x : β) : e (e.symm x) = x := e.right_inv x @[simp] theorem symm_apply_apply (e : α ≃ β) (x : α) : e.symm (e x) = x := e.left_inv x @[simp] theorem symm_comp_self (e : α ≃ β) : e.symm ∘ (e : α → β) = id := funext e.symm_apply_apply @[simp] theorem self_comp_symm (e : α ≃ β) : e ∘ (e.symm : β → α) = id := funext e.apply_symm_apply end equiv
30c53a3c39d23763cdf6cd8f5e08b4d66b6d058e
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/finset/basic.lean
3bac574906d78dc43690fc1cd0b834905ad1b7bc
[ "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
93,963
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, Minchao Wu, Mario Carneiro -/ import data.multiset.finset_ops import tactic.monotonicity import tactic.apply /-! # Finite sets mathlib has several different models for finite sets, and it can be confusing when you're first getting used to them! This file builds the basic theory of `finset α`, modelled as a `multiset α` without duplicates. It's "constructive" in the since that there is an underlying list of elements, although this is wrapped in a quotient by permutations, so anytime you actually use this list you're obligated to show you didn't depend on the ordering. There's also the typeclass `fintype α` (which asserts that there is some `finset α` containing every term of type `α`) as well as the predicate `finite` on `s : set α` (which asserts `nonempty (fintype s)`). -/ open multiset subtype nat 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, _⟩ rfl := rfl @[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 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. -/ instance : has_lift (finset α) (set α) := ⟨λ s, {x | x ∈ s}⟩ @[simp, norm_cast] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = ↑s := rfl @[simp] lemma coe_mem {s : finset α} (x : (↑s : set α)) : ↑x ∈ s := x.2 @[simp] lemma mk_coe {s : finset α} (x : (↑s : set α)) {h} : (⟨↑x, h⟩ : (↑s : set α)) = x := by { apply subtype.eq, refl, } instance decidable_mem' [decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ (↑s : set α)) := s.decidable_mem _ /-! ### extensionality -/ theorem ext_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[ext] theorem ext {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext_iff.2 @[simp, norm_cast] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ := set.ext_iff.trans ext_iff.symm lemma coe_injective {α} : function.injective (coe : finset α → set α) := λ s t, coe_inj.1 /-! ### 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_of_eq {s t : finset α} (h : s = t) : s ⊆ t := h ▸ subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem superset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊇ s₂ → s₂ ⊇ s₃ → s₁ ⊇ s₃ := λ h' h, subset.trans h h' -- TODO: these should be global attributes, but this will require fixing other files local attribute [trans] subset.trans superset.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 $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp, norm_cast] 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 _ } theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ := le_antisymm_iff @[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, norm_cast] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ := show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp only [set.ssubset_def, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff theorem ssubset_iff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ⊂ s₂ ↔ ∃ x ∈ s₂, x ∉ s₁ := set.ssubset_iff_of_subset h /-! ### Nonempty -/ /-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : finset α) : Prop := ∃ x:α, x ∈ s @[simp, norm_cast] lemma coe_nonempty {s : finset α} : (↑s:set α).nonempty ↔ s.nonempty := iff.rfl lemma nonempty.bex {s : finset α} (h : s.nonempty) : ∃ x:α, x ∈ s := h lemma nonempty.mono {s t : finset α} (hst : s ⊆ t) (hs : s.nonempty) : t.nonempty := set.nonempty.mono hst hs /-! ### empty -/ /-- The empty finset -/ 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 not_nonempty_empty : ¬(∅ : finset α).nonempty := λ ⟨x, hx⟩, not_mem_empty x hx @[simp] theorem mk_zero : (⟨0, nodup_zero⟩ : finset α) = ∅ := rfl theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ := λ e, not_mem_empty a $ e ▸ h theorem nonempty.ne_empty {s : finset α} (h : s.nonempty) : s ≠ ∅ := exists.elim h $ λ a, ne_empty_of_mem @[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) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨by rintro rfl x; exact id, λ h, eq_empty_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 nonempty_of_ne_empty {s : finset α} (h : s ≠ ∅) : s.nonempty := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem nonempty_iff_ne_empty {s : finset α} : s.nonempty ↔ s ≠ ∅ := ⟨nonempty.ne_empty, nonempty_of_ne_empty⟩ theorem eq_empty_or_nonempty (s : finset α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inl (λ h, or.inr (nonempty_of_ne_empty h)) @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := rfl /-- A `finset` for an empty type is empty. -/ lemma eq_empty_of_not_nonempty (h : ¬ nonempty α) (s : finset α) : s = ∅ := finset.eq_empty_of_forall_not_mem $ λ x, false.elim $ not_nonempty_iff_imp_false.1 h x /-! ### singleton -/ /-- `{a} : finset a` is the set `{a}` containing `a` and nothing else. This differs from `insert a ∅` in that it does not require a `decidable_eq` instance for `α`. -/ instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩ @[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = a ::ₘ 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton theorem not_mem_singleton {a b : α} : a ∉ ({b} : finset α) ↔ a ≠ b := not_congr mem_singleton theorem mem_singleton_self (a : α) : a ∈ ({a} : finset α) := or.inl rfl theorem singleton_inj {a b : α} : ({a} : finset α) = {b} ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ theorem singleton_nonempty (a : α) : ({a} : finset α).nonempty := ⟨a, mem_singleton_self a⟩ @[simp] theorem singleton_ne_empty (a : α) : ({a} : finset α) ≠ ∅ := (singleton_nonempty a).ne_empty @[simp] lemma coe_singleton (a : α) : ↑({a} : finset α) = ({a} : set α) := by { ext, simp } lemma eq_singleton_iff_unique_mem {s : finset α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := begin split; intro t, rw t, refine ⟨finset.mem_singleton_self _, λ _, finset.mem_singleton.1⟩, ext, rw finset.mem_singleton, refine ⟨t.right _, λ r, r.symm ▸ t.left⟩ end lemma singleton_iff_unique_mem (s : finset α) : (∃ a, s = {a}) ↔ ∃! a, a ∈ s := by simp only [eq_singleton_iff_unique_mem, exists_unique] lemma singleton_subset_set_iff {s : set α} {a : α} : ↑({a} : finset α) ⊆ s ↔ a ∈ s := by rw [coe_singleton, set.singleton_subset_iff] @[simp] lemma singleton_subset_iff {s : finset α} {a : α} : {a} ⊆ s ↔ a ∈ s := singleton_subset_set_iff /-! ### cons -/ /-- `cons a s h` is the set `{a} ∪ s` containing `a` and the elements of `s`. It is the same as `insert a s` when it is defined, but unlike `insert a s` it does not require `decidable_eq α`, and the union is guaranteed to be disjoint. -/ def cons {α} (a : α) (s : finset α) (h : a ∉ s) : finset α := ⟨a ::ₘ s.1, multiset.nodup_cons.2 ⟨h, s.2⟩⟩ @[simp] theorem mem_cons {α a s h b} : b ∈ @cons α a s h ↔ b = a ∨ b ∈ s := by rcases s with ⟨⟨s⟩⟩; apply list.mem_cons_iff @[simp] theorem cons_val {a : α} {s : finset α} (h : a ∉ s) : (cons a s h).1 = a ::ₘ s.1 := rfl @[simp] theorem mk_cons {a : α} {s : multiset α} (h : (a ::ₘ s).nodup) : (⟨a ::ₘ s, h⟩ : finset α) = cons a ⟨s, (multiset.nodup_cons.1 h).2⟩ (multiset.nodup_cons.1 h).1 := rfl @[simp] theorem nonempty_cons {a : α} {s : finset α} (h : a ∉ s) : (cons a s h).nonempty := ⟨a, mem_cons.2 (or.inl rfl)⟩ @[simp] lemma nonempty_mk_coe : ∀ {l : list α} {hl}, (⟨↑l, hl⟩ : finset α).nonempty ↔ l ≠ [] | [] hl := by simp | (a::l) hl := by simp [← multiset.cons_coe] /-! ### disjoint union -/ /-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`. It is the same as `s ∪ t`, but it does not require decidable equality on the type. The hypothesis ensures that the sets are disjoint. -/ def disj_union {α} (s t : finset α) (h : ∀ a ∈ s, a ∉ t) : finset α := ⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, h⟩⟩ @[simp] theorem mem_disj_union {α s t h a} : a ∈ @disj_union α s t h ↔ a ∈ s ∨ a ∈ t := by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append /-! ### 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⟩⟩ 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 rw [erase_dup_cons, erase_dup_eq_self]; refl 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 := mem_ndinsert_self a s.1 theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h 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] theorem cons_eq_insert {α} [decidable_eq α] (a s h) : @cons α a s h = insert a s := ext $ λ a, by simp @[simp, norm_cast] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) := set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩ @[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_singleton_self_eq (a : α) : ({a, a} : finset α) = {a} := insert_eq_of_mem $ mem_singleton_self _ theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext $ λ x, by simp only [mem_insert, or.left_comm] theorem insert_singleton_comm (a b : α) : ({a, b} : finset α) = {b, a} := begin ext, simp [or.comm] end @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext $ λ x, by simp only [mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := ne_empty_of_mem (mem_insert_self a s) lemma ne_insert_of_not_mem (s t : finset α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] theorem subset_insert (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 ∉ s, insert a s ⊆ t) := by exact_mod_cast @set.ssubset_iff_insert α ↑s ↑t lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[elab_as_eliminator] 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 /-- To prove a proposition about an arbitrary `finset α`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset α`, then it holds for the `finset` obtained by inserting a new element. -/ @[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 /-- Inserting an element to a finite set is equivalent to the option type. -/ def subtype_insert_equiv_option {t : finset α} {x : α} (h : x ∉ t) : {i // i ∈ insert x t} ≃ option {i // i ∈ t} := begin refine { to_fun := λ y, if h : ↑y = x then none else some ⟨y, (mem_insert.mp y.2).resolve_left h⟩, inv_fun := λ y, y.elim ⟨x, mem_insert_self _ _⟩ $ λ z, ⟨z, mem_insert_of_mem z.2⟩, .. }, { intro y, by_cases h : ↑y = x, simp only [subtype.ext_iff, h, option.elim, dif_pos, subtype.coe_mk], simp only [h, option.elim, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, { rintro (_|y), simp only [option.elim, dif_pos, subtype.coe_mk], have : ↑y ≠ x, { rintro ⟨⟩, exact h y.2 }, simp only [this, option.elim, subtype.eta, dif_neg, not_false_iff, subtype.coe_eta, subtype.coe_mk] }, end /-! ### 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 @[simp] theorem disj_union_eq_union {α} [decidable_eq α] (s t h) : @disj_union α s t h = s ∪ t := ext $ λ a, by simp theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inl h theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inr h theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ := by rw [mem_union, not_or_distrib] @[simp, norm_cast] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := set.ext $ λ x, mem_union 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 _ lemma union_subset_union {s1 t1 s2 t2 : finset α} (h1 : s1 ⊆ t1) (h2 : s2 ⊆ t2) : s1 ∪ s2 ⊆ t1 ∪ t2 := by { intros x hx, rw finset.mem_union at hx ⊢, tauto } theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := ext $ λ x, by simp only [mem_union, or_comm] instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := ext $ λ x, by simp only [mem_union, or_assoc] instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext $ λ _, mem_union.trans $ or_self _ instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext $ λ _, by simp only [mem_union, or.left_comm] theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := ext $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ s₂)] theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := ext $ λ x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := ext $ λ x, mem_union.trans $ false_or _ theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp only [insert_eq, union_left_comm] theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp only [insert_union, union_insert, insert_idem] @[simp] lemma union_eq_left_iff_subset {s t : finset α} : s ∪ t = s ↔ t ⊆ s := begin split, { assume h, have : t ⊆ s ∪ t := subset_union_right _ _, rwa h at this }, { assume h, exact subset.antisymm (union_subset (subset.refl _) h) (subset_union_left _ _) } end @[simp] lemma left_eq_union_iff_subset {s t : finset α} : s = s ∪ t ↔ t ⊆ s := by rw [← union_eq_left_iff_subset, eq_comm] @[simp] lemma union_eq_right_iff_subset {s t : finset α} : t ∪ s = s ↔ t ⊆ s := by rw [union_comm, union_eq_left_iff_subset] @[simp] lemma right_eq_union_iff_subset {s t : finset α} : s = t ∪ s ↔ t ⊆ s := by rw [← union_eq_right_iff_subset, eq_comm] /-! ### 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 only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp, norm_cast] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := set.ext $ λ _, mem_inter @[simp] theorem union_inter_cancel_left {s t : finset α} : (s ∪ t) ∩ s = s := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left] @[simp] theorem union_inter_cancel_right {s t : finset α} : (s ∪ t) ∩ t = t := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext $ λ _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and_assoc] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and.left_comm] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext $ λ _, by simp only [mem_inter, and.right_comm] @[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext $ λ _, mem_inter.trans $ and_self _ @[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext $ λ _, mem_inter.trans $ and_false _ @[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext $ λ _, mem_inter.trans $ false_and _ @[simp] lemma inter_union_self (s t : finset α) : s ∩ (t ∪ s) = s := by rw [inter_comm, union_inter_cancel_right] @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[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 $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[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 α} (H : a ∈ s) : {a} ∩ s = {a} := show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : {a} ∩ s = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[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] @[mono] lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊢, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_right {x y s : finset α} (h : x ⊆ y) : x ∩ s ⊆ y ∩ s := finset.inter_subset_inter h (finset.subset.refl _) lemma inter_subset_inter_left {x y s : finset α} (h : x ⊆ y) : s ∩ x ⊆ s ∩ y := finset.inter_subset_inter (finset.subset.refl _) h /-! ### lattice laws -/ instance : lattice (finset α) := { sup := (∪), sup_le := assume a b c, union_subset, le_sup_left := subset_union_left, le_sup_right := subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := inter_subset_left, inf_le_right := 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 } instance {α : Type*} [decidable_eq α] : semilattice_sup_bot (finset α) := { ..finset.semilattice_inf_bot, ..finset.lattice } instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice } theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right lemma union_eq_empty_iff (A B : finset α) : A ∪ B = ∅ ↔ A = ∅ ∧ B = ∅ := sup_eq_bot_iff /-! ### 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 := mem_erase_of_nodup s.2 @[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 only [mem_erase]; exact and.left 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 only [mem_erase]; exact and.intro /-- An element of `s` that is not an element of `erase s a` must be `a`. -/ lemma eq_of_mem_of_not_mem_erase {a b : α} {s : finset α} (hs : b ∈ s) (hsa : b ∉ s.erase a) : b = a := begin rw [mem_erase, not_and] at hsa, exact not_imp_not.mp hsa hs end theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := ext $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or]; apply and_iff_right_of_imp; rintro H rfl; exact h H theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h 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, norm_cast] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) := set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl 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 only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; 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 lemma not_mem_sdiff_of_mem_right {a : α} {s t : finset α} (h : a ∈ t) : a ∉ s \ t := by simp only [mem_sdiff, h, not_true, not_false_iff, and_false] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := ext $ λ a, by simpa only [mem_sdiff, mem_union, or_comm, or_and_distrib_left, dec_em, and_true] using or_iff_right_of_imp (@h a) theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) theorem inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h @[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := (inter_comm _ _).trans (inter_sdiff_self _ _) @[simp] theorem sdiff_self (s₁ : finset α) : s₁ \ s₁ = ∅ := by ext; simp theorem sdiff_inter_distrib_right (s₁ s₂ s₃ : finset α) : s₁ \ (s₂ ∩ s₃) = (s₁ \ s₂) ∪ (s₁ \ s₃) := by ext; simp only [and_or_distrib_left, mem_union, not_and_distrib, mem_sdiff, mem_inter] @[simp] theorem sdiff_inter_self_left (s₁ s₂ : finset α) : s₁ \ (s₁ ∩ s₂) = s₁ \ s₂ := by simp only [sdiff_inter_distrib_right, sdiff_self, empty_union] @[simp] theorem sdiff_inter_self_right (s₁ s₂ : finset α) : s₁ \ (s₂ ∩ s₁) = s₁ \ s₂ := by simp only [sdiff_inter_distrib_right, sdiff_self, union_empty] lemma inter_eq_sdiff_sdiff (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₁ \ (s₁ \ s₂) := begin ext a, split; intros h; simp only [not_and, not_not, finset.mem_sdiff, finset.mem_inter] at h; simp [h], end @[simp] theorem sdiff_empty {s₁ : finset α} : s₁ \ ∅ = s₁ := ext (by simp) @[mono] theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ := by simpa only [subset_iff, mem_sdiff, and_imp] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂) theorem sdiff_subset_self {s₁ s₂ : finset α} : s₁ \ s₂ ⊆ s₁ := suffices s₁ \ s₂ ⊆ s₁ \ ∅, by simpa [sdiff_empty] using this, sdiff_subset_sdiff (subset.refl _) (empty_subset _) @[simp, norm_cast] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := set.ext $ λ _, mem_sdiff @[simp] theorem union_sdiff_self_eq_union {s t : finset α} : s ∪ (t \ s) = s ∪ t := ext $ λ a, by simp only [mem_union, mem_sdiff, or_iff_not_imp_left, imp_and_distrib, and_iff_left id] @[simp] theorem sdiff_union_self_eq_union {s t : finset α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_sdiff_self_eq_union, union_comm] lemma union_sdiff_symm {s t : finset α} : s ∪ (t \ s) = t ∪ (s \ t) := by rw [union_sdiff_self_eq_union, union_sdiff_self_eq_union, union_comm] lemma sdiff_union_inter (s t : finset α) : (s \ t) ∪ (s ∩ t) = s := by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto } @[simp] lemma sdiff_idem (s t : finset α) : s \ t \ t = s \ t := by { simp only [ext_iff, mem_sdiff], tauto } lemma sdiff_eq_empty_iff_subset {s t : finset α} : s \ t = ∅ ↔ s ⊆ t := by { rw [subset_iff, ext_iff], simp } @[simp] lemma empty_sdiff (s : finset α) : ∅ \ s = ∅ := by { rw sdiff_eq_empty_iff_subset, exact empty_subset _ } lemma insert_sdiff_of_not_mem (s : finset α) {t : finset α} {x : α} (h : x ∉ t) : (insert x s) \ t = insert x (s \ t) := begin rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_not_mem ↑s h end lemma insert_sdiff_of_mem (s : finset α) {t : finset α} {x : α} (h : x ∈ t) : (insert x s) \ t = s \ t := begin rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_mem ↑s h end @[simp] lemma sdiff_subset (s t : finset α) : s \ t ⊆ s := by simp [subset_iff, mem_sdiff] {contextual := tt} lemma union_sdiff_distrib (s₁ s₂ t : finset α) : (s₁ ∪ s₂) \ t = s₁ \ t ∪ s₂ \ t := by { simp only [ext_iff, mem_sdiff, mem_union], tauto } lemma sdiff_union_distrib (s t₁ t₂ : finset α) : s \ (t₁ ∪ t₂) = (s \ t₁) ∩ (s \ t₂) := by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto } lemma union_sdiff_self (s t : finset α) : (s ∪ t) \ t = s \ t := by rw [union_sdiff_distrib, sdiff_self, union_empty] lemma sdiff_singleton_eq_erase (a : α) (s : finset α) : s \ singleton a = erase s a := by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto } lemma sdiff_sdiff_self_left (s t : finset α) : s \ (s \ t) = s ∩ t := by { simp only [ext_iff, mem_sdiff, mem_inter], tauto } lemma inter_eq_inter_of_sdiff_eq_sdiff {s t₁ t₂ : finset α} : s \ t₁ = s \ t₂ → s ∩ t₁ = s ∩ t₂ := by { simp only [ext_iff, mem_sdiff, mem_inter], intros b c, replace b := b c, split; tauto } 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⟩ theorem sizeof_lt_sizeof_of_mem [has_sizeof α] {x : α} {s : finset α} (hx : x ∈ s) : sizeof x < sizeof s := by { cases s, dsimp [sizeof, has_sizeof.sizeof, finset.sizeof], apply lt_add_left, exact multiset.sizeof_lt_sizeof_of_mem hx } @[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 /-! ### piecewise -/ section piecewise /-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its complement. -/ def piecewise {α : Type*} {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) [∀j, decidable (j ∈ s)] : Πi, δ i := λi, if i ∈ s then f i else g i variables {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) @[simp] lemma piecewise_insert_self [decidable_eq α] {j : α} [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] @[simp] lemma piecewise_empty [∀i : α, decidable (i ∈ (∅ : finset α))] : piecewise ∅ f g = g := by { ext i, simp [piecewise] } variable [∀j, decidable (j ∈ s)] @[norm_cast] lemma piecewise_coe [∀j, decidable (j ∈ (↑s : set α))] : (↑s : set α).piecewise f g = s.piecewise f g := by { ext, congr } @[simp, priority 980] lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi] @[simp, priority 980] lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i := by simp [piecewise, hi] @[simp, priority 990] lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀i, decidable (i ∈ insert j s)] (h : i ≠ j) : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] lemma piecewise_insert [decidable_eq α] (j : α) [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = function.update (s.piecewise f g) j (f j) := begin classical, rw [← piecewise_coe, ← piecewise_coe, ← set.piecewise_insert, ← coe_insert j s], congr end lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) : function.update f i v = piecewise (singleton i) (λj, v) f := begin ext j, by_cases h : j = i, { rw [h], simp }, { simp [h] } end end piecewise 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_ssubset {s : finset α} : s.filter p ⊂ s ↔ ∃ x ∈ s, ¬ p x := ⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (λ hp, mem_filter.2 ⟨hs, hp⟩) hp⟩, λ ⟨x, hs, hp⟩, ⟨s.filter_subset, λ h, hp (mem_filter.1 (h hs)).2⟩⟩ theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm] lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] : @finset.filter α (λ _, true) h s = s := by ext; simp @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext $ assume a, by simp only [mem_filter, and_false]; refl /-- If all elements of a `finset` satisfy the predicate `p`, `s.filter p` is `s`. -/ @[simp] lemma filter_true_of_mem {s : finset α} (h : ∀ x ∈ s, p x) : s.filter p = s := ext $ λ x, ⟨λ h, (mem_filter.1 h).1, λ hx, mem_filter.2 ⟨hx, h x hx⟩⟩ /-- If all elements of a `finset` fail to satisfy the predicate `p`, `s.filter p` is `∅`. -/ lemma filter_false_of_mem {s : finset α} (h : ∀ x ∈ s, ¬ p x) : s.filter p = ∅ := eq_empty_of_forall_not_mem (by simpa) lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ @[simp, norm_cast] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := set.ext $ λ _, mem_filter theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ := by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (p q : α → Prop) [decidable_pred p] [decidable_pred q] (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) := ext $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] lemma filter_mem_eq_inter {s t : finset α} [Π i, decidable (i ∈ t)] : s.filter (λ i, i ∈ t) = s ∩ t := ext $ λ i, by rw [mem_filter, mem_inter] theorem filter_inter {s t : finset α} : filter p s ∩ t = filter p (s ∩ t) := by { ext, simp only [mem_inter, mem_filter, and.right_comm] } theorem inter_filter {s t : finset α} : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : finset α) : filter p (insert a s) = if p a then insert a (filter p s) else (filter p s) := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_or [decidable_pred (λ a, p a ∨ q a)] (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and [decidable_pred (λ a, p a ∧ q a)] (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] 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 $ λ _, by simp only [mem_sdiff, mem_filter] theorem sdiff_eq_self (s₁ s₂ : finset α) : s₁ \ s₂ = s₁ ↔ s₁ ∩ s₂ ⊆ ∅ := by { simp [subset.antisymm_iff,sdiff_subset_self], split; intro h, { transitivity' ((s₁ \ s₂) ∩ s₂), mono, simp }, { calc s₁ \ s₂ ⊇ s₁ \ (s₁ ∩ s₂) : by simp [(⊇)] ... ⊇ s₁ \ ∅ : by mono using [(⊇)] ... ⊇ s₁ : by simp [(⊇)] } } theorem filter_union_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := by simp only [filter_not, union_sdiff_of_subset (filter_subset s)] theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ := by simp only [filter_not, inter_sdiff_self] lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := begin classical, refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩, { simp [filter_union_right, em] }, { intro x, simp }, { intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ } end /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable {α} (s : finset α) (p : α → Prop) (h : decidable_pred p) [decidable_pred p] : @filter α p h s = s.filter p := by congr section classical open_locale classical /-- The following instance allows us to write `{ x ∈ s | p x }` for `finset.filter s p`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{ x ∈ s | p x }` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{ x ∈ s | p x }` to `finset.filter s p`. If `p` happens to be decidable, the simp-lemma `filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl end classical /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq β] (s : finset β) (b : β) : s.filter(eq b) = ite (b ∈ s) {b} ∅ := begin split_ifs, { ext, simp only [mem_filter, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩, }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintros m ⟨e⟩, exact h m, } end /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ lemma filter_eq' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a = b) = ite (b ∈ s) {b} ∅ := trans (filter_congr (λ _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b) lemma filter_ne [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, b ≠ a) = s.erase b := by { ext, simp only [mem_filter, mem_erase, ne.def], cc, } lemma filter_ne' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a ≠ b) = s.erase b := trans (filter_congr (λ _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b) end filter /-! ### range -/ section range variables {n m l : ℕ} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_coe (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_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm theorem range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := multiset.self_mem_range_succ n @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem range_mono : monotone range := λ _ _, range_subset.2 end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] 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 only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := iff_true_intro $ λ _, false.elim 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 only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] end finset /-- Equivalence between the set of natural numbers which are `≥ k` and `ℕ`, given by `n → n - k`. -/ def not_mem_range_equiv (k : ℕ) : {n // n ∉ range k} ≃ ℕ := { to_fun := λ i, i.1 - k, inv_fun := λ j, ⟨j + k, by simp⟩, left_inv := begin assume j, rw subtype.ext_iff_val, apply nat.sub_add_cancel, simpa using j.2 end, right_inv := λ j, nat.add_sub_cancel _ _ } @[simp] lemma coe_not_mem_range_equiv (k : ℕ) : (not_mem_range_equiv k : {n // n ∉ range k} → ℕ) = (λ i, i - k) := rfl @[simp] lemma coe_not_mem_range_equiv_symm (k : ℕ) : ((not_mem_range_equiv k).symm : ℕ → {n // n ∉ range k}) = λ j, ⟨j + k, by simp⟩ := rfl namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option α) : finset α := match o with | none := ∅ | some a := {a} end @[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl @[simp] theorem to_finset_some {a : α} : (some a).to_finset = {a} := rfl @[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o := by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl end option /-! ### 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_zero : to_finset (0 : multiset α) = ∅ := rfl @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a ::ₘ s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons @[simp] lemma to_finset_add (s t : multiset α) : to_finset (s + t) = to_finset s ∪ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_nsmul (s : multiset α) : ∀(n : ℕ) (hn : n ≠ 0), (n •ℕ s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, one_nsmul] }, { rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset α) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_union (s t : multiset α) : (s ∪ t).to_finset = s.to_finset ∪ t.to_finset := by ext; simp theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 := finset.val_inj.symm.trans multiset.erase_dup_eq_zero @[simp] lemma to_finset_subset (m1 m2 : multiset α) : m1.to_finset ⊆ m2.to_finset ↔ m1 ⊆ m2 := by simp only [finset.subset_iff, multiset.subset_iff, multiset.mem_to_finset] 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] theorem to_finset_surjective : function.surjective (to_finset : list α → finset α) := begin refine λ s, ⟨quotient.out' s.val, finset.ext $ λ x, _⟩, obtain ⟨l, hl⟩ := quot.exists_rep s.val, rw [list.mem_to_finset, finset.mem_def, ←hl], exact list.perm.mem_iff (quotient.mk_out l) end end list namespace finset /-! ### map -/ section map open function /-- When `f` is an embedding of `α` in `β` and `s` is a finset in `α`, then `s.map f` is the image finset in `β`. The embedding condition guarantees that there are no duplicates in the image. -/ 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 : α ↪ β) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := mem_map.trans $ by simp only [exists_prop]; refl theorem mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_injective f.2 theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f := (mem_map' _).2 @[simp, norm_cast] theorem coe_map (f : α ↪ β) (s : finset α) : (↑(s.map f) : set β) = f '' ↑s := set.ext $ λ x, mem_map.trans set.mem_image_iff_bex.symm theorem coe_map_subset_range (f : α ↪ β) (s : finset α) : (↑(s.map f) : set β) ⊆ set.range f := calc ↑(s.map f) = f '' ↑s : coe_map f s ... ⊆ set.range f : set.image_subset_range f ↑s theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] @[simp] theorem map_refl : s.map (embedding.refl _) = s := ext $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp only [map_val, multiset.map_map]; refl theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ := ⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs, λ h, by simp [subset_def, map_subset_map h]⟩ theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ := by simp only [subset.antisymm_iff, map_subset_map] /-- Associate to an embedding `f` from `α` to `β` the embedding that maps a finset to its image under `f`. -/ def map_embedding (f : α ↪ β) : finset α ↪ finset β := ⟨map f, λ s₁ s₂, map_inj.1⟩ @[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl theorem map_filter {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext $ λ b, by simp only [mem_filter, mem_map, exists_prop, and_assoc]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, h1, h2, rfl⟩, by rintro ⟨x, h1, h2, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := ext $ λ _, by simp only [mem_map, mem_union, exists_prop, 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 $ λ b, by simp only [mem_map, mem_inter, exists_prop]; exact ⟨by rintro ⟨a, ⟨m₁, m₂⟩, rfl⟩; exact ⟨⟨a, m₁, rfl⟩, ⟨a, m₂, rfl⟩⟩, by rintro ⟨⟨a, m₁, e⟩, ⟨a', m₂, rfl⟩⟩; cases f.2 e; exact ⟨_, ⟨m₁, m₂⟩, rfl⟩⟩ @[simp] theorem map_singleton (f : α ↪ β) (a : α) : map f {a} = {f a} := ext $ λ _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact 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 only [insert_eq, map_union, map_singleton] @[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 rw [map_val, attach_val]; exact attach_map_val _ end map lemma range_add_one' (n : ℕ) : range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ.inj⟩) := by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n] /-! ### image -/ 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 only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ lemma filter_mem_image_eq_image (f : α → β) (s : finset α) (t : finset β) (h : ∀ x ∈ s, f x ∈ t) : t.filter (λ y, y ∈ s.image f) = s.image f := by { ext, rw [mem_filter, mem_image], simp only [and_imp, exists_prop, and_iff_right_iff_imp, exists_imp_distrib], rintros x xel rfl, exact h _ xel } lemma fiber_nonempty_iff_mem_image (f : α → β) (s : finset α) (y : β) : (s.filter (λ x, f x = y)).nonempty ↔ y ∈ s.image f := by simp [finset.nonempty] @[simp, norm_cast] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := set.ext $ λ _, mem_image.trans set.mem_image_iff_bex.symm lemma nonempty.image (h : s.nonempty) (f : α → β) : (s.image f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩ theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] 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) @[simp] theorem image_id [decidable_eq α] : s.image id = s := ext $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right] theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map] theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset', multiset.map_subset_map h] theorem image_subset_iff {s : finset α} {t : finset β} {f : α → β} : s.image f ⊆ t ↔ ∀ x ∈ s, f x ∈ t := calc s.image f ⊆ t ↔ f '' ↑s ⊆ ↑t : by norm_cast ... ↔ _ : set.image_subset_iff theorem image_mono (f : α → β) : monotone (finset.image f) := λ _ _, image_subset_image theorem coe_image_subset_range : ↑(s.image f) ⊆ set.range f := calc ↑(s.image f) = f '' ↑s : coe_image ... ⊆ set.range f : set.image_subset_range f ↑s theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩, by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext $ λ _, by simp only [mem_image, mem_union, exists_prop, 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 $ by simp only [mem_image, exists_prop, mem_inter]; 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 (f : α → β) (a : α) : image f {a} = {f a} := ext $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using 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 only [insert_eq, image_singleton, 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 rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self] @[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⟩)) := ext $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (λ h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h) (λ h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩), λ _, finset.mem_attach _ _⟩ 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 lemma image_const {s : finset α} (h : s.nonempty) (b : β) : s.image (λa, b) = singleton b := ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, h.bex, true_and, mem_singleton, eq_comm] /-- Because `finset.image` requires a `decidable_eq` instances for the target type, we can only construct a `functor finset` when working classically. -/ instance [Π P, decidable P] : functor finset := { map := λ α β f s, s.image f, } instance [Π P, decidable P] : is_lawful_functor finset := { id_map := λ α x, image_id, comp_map := λ α β γ f g s, image_image.symm, } /-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose elements belong to `s`. -/ protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) := (s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩, λ x y H, subtype.eq $ subtype.mk.inj H⟩ @[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} : ∀{a : subtype p}, a ∈ s.subtype p ↔ (a : α) ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] lemma subtype_eq_empty {p : α → Prop} [decidable_pred p] {s : finset α} : s.subtype p = ∅ ↔ ∀ x, p x → x ∉ s := by simp [ext_iff, subtype.forall, subtype.coe_mk]; refl /-- `s.subtype p` converts back to `s.filter p` with `embedding.subtype`. -/ @[simp] lemma subtype_map (p : α → Prop) [decidable_pred p] : (s.subtype p).map (function.embedding.subtype _) = s.filter p := begin ext x, rw mem_map, change (∃ a : {x // p x}, ∃ H, (a : α) = x) ↔ _, split, { rintros ⟨y, hy, hyval⟩, rw [mem_subtype, hyval] at hy, rw mem_filter, use hy, rw ← hyval, use y.property }, { intro hx, rw mem_filter at hx, use ⟨⟨x, hx.2⟩, mem_subtype.2 hx.1, rfl⟩ } end /-- If all elements of a `finset` satisfy the predicate `p`, `s.subtype p` converts back to `s` with `embedding.subtype`. -/ lemma subtype_map_of_mem {p : α → Prop} [decidable_pred p] (h : ∀ x ∈ s, p x) : (s.subtype p).map (function.embedding.subtype _) = s := by rw [subtype_map, filter_true_of_mem h] /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, all elements of the result have the property of the subtype. -/ lemma property_of_mem_map_subtype {p : α → Prop} (s : finset {x // p x}) {a : α} (h : a ∈ s.map (function.embedding.subtype _)) : p a := begin rcases mem_map.1 h with ⟨x, hx, rfl⟩, exact x.2 end /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result does not contain any value that does not satisfy the property of the subtype. -/ lemma not_mem_map_subtype_of_not_property {p : α → Prop} (s : finset {x // p x}) {a : α} (h : ¬ p a) : a ∉ (s.map (function.embedding.subtype _)) := mt s.property_of_mem_map_subtype h /-- If a `finset` of a subtype is converted to the main type with `embedding.subtype`, the result is a subset of the set giving the subtype. -/ lemma map_subtype_subset {t : set α} (s : finset t) : ↑(s.map (function.embedding.subtype _)) ⊆ t := begin intros a ha, rw mem_coe at ha, convert property_of_mem_map_subtype s ha end lemma subset_image_iff {f : α → β} {s : finset β} {t : set α} : ↑s ⊆ f '' t ↔ ∃s' : finset α, ↑s' ⊆ t ∧ s'.image f = s := begin classical, split, swap, { rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs }, intro h, induction s using finset.induction with a s has ih h, { refine ⟨∅, set.empty_subset _, _⟩, convert finset.image_empty _ }, rw [finset.coe_insert, set.insert_subset] at h, rcases ih h.2 with ⟨s', hst, hsi⟩, rcases h.1 with ⟨x, hxt, rfl⟩, refine ⟨insert x s', _, _⟩, { rw [finset.coe_insert, set.insert_subset], exact ⟨hxt, hst⟩ }, rw [finset.image_insert, hsi], congr end end image end finset theorem multiset.to_finset_map [decidable_eq α] [decidable_eq β] (f : α → β) (m : multiset α) : (m.map f).to_finset = m.to_finset.image f := finset.val_inj.1 (multiset.erase_dup_map_erase_dup_eq _ _).symm namespace finset /-! ### 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] lemma card_mk {m nodup} : (⟨m, nodup⟩ : finset α).card = m.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.nonempty := pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm theorem card_ne_zero_of_mem {s : finset α} {a : α} (h : a ∈ s) : card s ≠ 0 := (not_congr card_eq_zero).2 (ne_empty_of_mem h) theorem card_eq_one {s : finset α} : s.card = 1 ↔ ∃ a, s = {a} := by cases s; simp only [multiset.card_eq_one, finset.card, ← val_inj, singleton_val] @[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 := by simpa only [card_cons, card, insert_val] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_of_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∈ s) : card (insert a s) = card s := by rw insert_eq_of_mem h theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 := by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right}, rw [card_insert_of_not_mem h]] @[simp] theorem card_singleton (a : α) : card ({a} : finset α) = 1 := card_singleton _ lemma card_singleton_inter [decidable_eq α] {x : α} {s : finset α} : ({x} ∩ s).card ≤ 1 := begin cases (finset.decidable_mem x s), { simp [finset.singleton_inter_of_not_mem h] }, { simp [finset.singleton_inter_of_mem h] }, end 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_erase_lt_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) < card s := card_erase_lt_of_mem theorem card_erase_le [decidable_eq α] {a : α} {s : finset α} : card (erase s a) ≤ card s := card_erase_le theorem pred_card_le_card_erase [decidable_eq α] {a : α} {s : finset α} : card s - 1 ≤ card (erase s a) := begin by_cases h : a ∈ s, { rw [card_erase_of_mem h], refl }, { rw [erase_eq_of_not_mem h], apply nat.sub_le } end @[simp] theorem card_range (n : ℕ) : card (range n) = n := card_range n @[simp] theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach end card end finset theorem multiset.to_finset_card_le [decidable_eq α] (m : multiset α) : m.to_finset.card ≤ m.card := card_le_of_le (erase_dup_le _) theorem list.to_finset_card_le [decidable_eq α] (l : list α) : l.to_finset.card ≤ l.length := multiset.to_finset_card_le ⟦l⟧ namespace finset section card theorem card_image_le [decidable_eq β] {f : α → β} {s : finset α} : card (image f s) ≤ card s := by simpa only [card_map] using (s.1.map f).to_finset_card_le 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 only [card, image_val_of_inj_on H, card_map] 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 fiber_card_ne_zero_iff_mem_image (s : finset α) (f : α → β) [decidable_eq β] (y : β) : (s.filter (λ x, f x = y)).card ≠ 0 ↔ y ∈ s.image f := by { rw [←zero_lt_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image] } @[simp] lemma card_map {α β} (f : α ↪ β) {s : finset α} : (s.map f).card = s.card := multiset.card_map _ _ lemma card_eq_of_bijective {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 := begin classical, 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 only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and], 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 end lemma card_eq_succ [decidable_eq α] {s : finset α} {n : ℕ} : s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have 0 < card s, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [eq, card_erase_of_mem has, pred_succ]⟩) (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 {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 {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 := begin classical, 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 end /-- If there are more pigeons than pigeonholes, then there are two pigeons in the same pigeonhole. -/ lemma exists_ne_map_eq_of_card_lt_of_maps_to {s : finset α} {t : finset β} (hc : t.card < s.card) {f : α → β} (hf : ∀ a ∈ s, f a ∈ t) : ∃ (x ∈ s) (y ∈ s), x ≠ y ∧ f x = f y := begin classical, by_contra hz, push_neg at hz, refine hc.not_le (card_le_card_of_inj_on f hf _), intros x hx y hy, contrapose, exact hz x hx y hy, end lemma card_le_of_inj_on {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 simpa only [mem_range]) (by simp only [mem_range]; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂) /-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to define an object on `s`. Then one can inductively define an object on all finsets, starting from the empty set and iterating. This can be used either to define data, or to prove properties. -/ @[elab_as_eliminator] def 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 < _) lemma card_congr {s : finset α} {t : finset β} (f : Π a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.card = t.card := by haveI := classical.prop_decidable; exact calc s.card = s.attach.card : card_attach.symm ... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card : eq.symm (card_image_of_injective _ (λ a b h, subtype.eq (h₂ _ _ _ _ h))) ... = t.card : congr_arg card (finset.ext $ λ b, ⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _, λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩) lemma card_union_add_card_inter [decidable_eq α] (s t : finset α) : (s ∪ t).card + (s ∩ t).card = s.card + t.card := finset.induction_on t (by simp) $ λ a r har, by by_cases a ∈ s; simp *; cc lemma card_union_le [decidable_eq α] (s t : finset α) : (s ∪ t).card ≤ s.card + t.card := card_union_add_card_inter s t ▸ le_add_right _ _ lemma card_union_eq [decidable_eq α] {s t : finset α} (h : disjoint s t) : (s ∪ t).card = s.card + t.card := begin rw [← card_union_add_card_inter], convert (add_zero _).symm, rw [card_eq_zero], rwa [disjoint_iff] at h end lemma surj_on_of_inj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : card t ≤ card s) : (∀ b ∈ t, ∃ a ha, b = f a ha) := by haveI := classical.dec_eq β; exact λ b hb, have h : card (image (λ (a : {a // a ∈ s}), f a a.prop) (attach s)) = card s, from @card_attach _ s ▸ card_image_of_injective _ (λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h), have h₁ : image (λ a : {a // a ∈ s}, f a a.prop) s.attach = t := eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ hf _ _) (by simp [hst, h]), begin rw ← h₁ at hb, rcases mem_image.1 hb with ⟨a, ha₁, ha₂⟩, exact ⟨a, a.2, ha₂.symm⟩, end open function lemma inj_on_of_surj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hsurj : ∀ b ∈ t, ∃ a ha, b = f a ha) (hst : card s ≤ card t) ⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s) (ha₂ : a₂ ∈ s) (ha₁a₂: f a₁ ha₁ = f a₂ ha₂) : a₁ = a₂ := by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, ha₁⟩⟩; exact let f' : {x // x ∈ s} → {x // x ∈ t} := λ x, ⟨f x.1 x.2, hf x.1 x.2⟩ in let g : {x // x ∈ t} → {x // x ∈ s} := @surj_inv _ _ f' (λ x, let ⟨y, hy₁, hy₂⟩ := hsurj x.1 x.2 in ⟨⟨y, hy₁⟩, subtype.eq hy₂.symm⟩) in have hg : injective g, from function.injective_surj_inv _, have hsg : surjective g, from λ x, let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (λ (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x) (λ x _, show (g x) ∈ s.attach, from mem_attach _ _) (λ x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in ⟨y, hy.snd.symm⟩, have hif : injective f', from (left_inverse_of_surjective_of_right_inverse hsg (right_inverse_surj_inv _)).injective, subtype.ext_iff_val.1 (@hif ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ (subtype.eq ha₁a₂)) end card /-! ### bind -/ 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 only [mem_def, bind_val, mem_erase_dup, mem_bind, exists_prop] @[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t := ext $ λ x, by simp only [mem_bind, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext $ λ x, by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind {a : α} : finset.bind {a} t = t a := begin classical, rw [← insert_emptyc_eq, bind_insert, bind_empty, union_empty] end theorem bind_inter (s : finset α) (f : α → finset β) (t : finset β) : s.bind f ∩ t = s.bind (λ x, f x ∩ t) := begin ext x, simp only [mem_bind, mem_inter], tauto end theorem inter_bind (t : finset β) (s : finset α) (f : α → finset β) : t ∩ s.bind f = s.bind (λ x, t ∩ f x) := by rw [inter_comm, bind_inter]; simp [inter_comm] 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 rfl (λ a s has ih, by simp only [image_insert, bind_insert, ih]) 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 rfl (λ a s has ih, by simp only [bind_insert, image_union, ih]) 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 $ λ x, by simp only [multiset.mem_to_finset, mem_bind, multiset.mem_bind, exists_prop] 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 only [subset_iff, mem_bind, exists_imp_distrib, and_imp, exists_prop] lemma bind_subset_bind_of_subset_left {α : Type*} {s₁ s₂ : finset α} (t : α → finset β) (h : s₁ ⊆ s₂) : s₁.bind t ⊆ s₂.bind t := begin intro x, simp only [and_imp, mem_bind, exists_prop], exact Exists.imp (λ a ha, ⟨h ha.1, ha.2⟩) end lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := ext $ λ x, by simp only [mem_bind, mem_image, mem_singleton, eq_comm] @[simp] lemma bind_singleton_eq_self [decidable_eq α] : s.bind (singleton : α → finset α) = s := by { rw bind_singleton, exact image_id } lemma bind_filter_eq_of_maps_to [decidable_eq α] {s : finset α} {t : finset β} {f : α → β} (h : ∀ x ∈ s, f x ∈ t) : t.bind (λa, s.filter $ (λc, f c = a)) = s := begin ext b, suffices : (∃ a ∈ t, b ∈ s ∧ f b = a) ↔ b ∈ s, by simpa, exact ⟨λ ⟨a, ha, hb, hab⟩, hb, λ hb, ⟨f b, h b hb, hb, rfl⟩⟩ end lemma image_bind_filter_eq [decidable_eq α] (s : finset β) (g : β → α) : (s.image g).bind (λa, s.filter $ (λc, g c = a)) = s := bind_filter_eq_of_maps_to (λ x, mem_image_of_mem g) end bind /-! ### prod -/ 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 subset_product [decidable_eq α] [decidable_eq β] {s : finset (α × β)} : s ⊆ (s.image prod.fst).product (s.image prod.snd) := λ p hp, mem_product.2 ⟨mem_image_of_mem _ hp, mem_image_of_mem _ hp⟩ theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bind (λa, t.image $ λb, (a, b)) := ext $ λ ⟨x, y⟩, by simp only [mem_product, mem_bind, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] @[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ end prod /-! ### sigma -/ 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)} (H1 : s₁ ⊆ s₂) (H2 : ∀a, t₁ a ⊆ t₂ a) : s₁.sigma t₁ ⊆ s₂.sigma t₂ := λ ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩ theorem sigma_eq_bind [decidable_eq (Σ a, σ a)] (s : finset α) (t : Πa, finset (σ a)) : s.sigma t = s.bind (λa, (t a).map $ function.embedding.sigma_mk a) := by { ext ⟨x, y⟩, simp [and.left_comm] } end sigma /-! ### disjoint -/ section disjoint variable [decidable_eq α] theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and, and_imp]; 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 instance decidable_disjoint (U V : finset α) : decidable (disjoint U V) := decidable_of_decidable_of_iff (by apply_instance) eq_bot_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 only [disjoint_left, imp_not_comm, forall_eq'] 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 only [disjoint_left, mem_singleton, forall_eq] @[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s := disjoint.comm.trans singleton_disjoint @[simp] theorem disjoint_insert_left {a : α} {s t : finset α} : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[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 only [disjoint_left, mem_union, 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 only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] lemma sdiff_disjoint {s t : finset α} : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff {s t : finset α} : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_sdiff_inter (s t : finset α) : disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint lemma sdiff_eq_self_iff_disjoint {s t : finset α} : s \ t = s ↔ disjoint s t := by rw [sdiff_eq_self, subset_empty, disjoint_iff_inter_eq_empty] lemma sdiff_eq_self_of_disjoint {s t : finset α} (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h lemma disjoint_self_iff_empty (s : finset α) : disjoint s s ↔ s = ∅ := disjoint_self lemma disjoint_bind_left {ι : Type*} (s : finset ι) (f : ι → finset α) (t : finset α) : disjoint (s.bind f) t ↔ (∀i∈s, disjoint (f i) t) := begin classical, refine s.induction _ _, { simp only [forall_mem_empty_iff, bind_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bind_insert, his, forall_mem_insert, ih] } end lemma disjoint_bind_right {ι : Type*} (s : finset α) (t : finset ι) (f : ι → finset α) : disjoint s (t.bind f) ↔ (∀i∈t, disjoint s (f i)) := by simpa only [disjoint.comm] using disjoint_bind_left t f s @[simp] theorem card_disjoint_union {s t : finset α} (h : disjoint s t) : card (s ∪ t) = card s + card t := by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero] theorem card_sdiff {s t : finset α} (h : s ⊆ t) : card (t \ s) = card t - card s := suffices card (t \ s) = card ((t \ s) ∪ s) - card s, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel] lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : disjoint (s.filter p) (s.filter q) ↔ (∀ x ∈ s, p x → ¬ q x) := by split; simp [disjoint_left] {contextual := tt} lemma disjoint_filter_filter {s t : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : (disjoint s t) → disjoint (s.filter p) (t.filter q) := disjoint.mono (filter_subset _) (filter_subset _) lemma disjoint_iff_disjoint_coe {α : Type*} {a b : finset α} [decidable_eq α] : disjoint a b ↔ disjoint (↑a : set α) (↑b : set α) := by { rw [finset.disjoint_left, set.disjoint_left], refl } end disjoint /-- Given a set A and a set B inside it, we can shrink A to any appropriate size, and keep B inside it. -/ lemma exists_intermediate_set {A B : finset α} (i : ℕ) (h₁ : i + card B ≤ card A) (h₂ : B ⊆ A) : ∃ (C : finset α), B ⊆ C ∧ C ⊆ A ∧ card C = i + card B := begin classical, rcases nat.le.dest h₁ with ⟨k, _⟩, clear h₁, induction k with k ih generalizing A, { exact ⟨A, h₂, subset.refl _, h.symm⟩ }, { have : (A \ B).nonempty, { rw [← card_pos, card_sdiff h₂, ← h, nat.add_right_comm, nat.add_sub_cancel, nat.add_succ], apply nat.succ_pos }, rcases this with ⟨a, ha⟩, have z : i + card B + k = card (erase A a), { rw [card_erase_of_mem, ← h, nat.add_succ, nat.pred_succ], rw mem_sdiff at ha, exact ha.1 }, rcases ih _ z with ⟨B', hB', B'subA', cards⟩, { exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ }, { rintros t th, apply mem_erase_of_ne_of_mem _ (h₂ th), rintro rfl, exact not_mem_sdiff_of_mem_right th ha } } end /-- We can shrink A to any smaller size. -/ lemma exists_smaller_set (A : finset α) (i : ℕ) (h₁ : i ≤ card A) : ∃ (B : finset α), B ⊆ A ∧ card B = i := let ⟨B, _, x₁, x₂⟩ := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, x₂⟩ /-- `finset.fin_range k` is the finset `{0, 1, ..., k-1}`, as a `finset (fin k)`. -/ def fin_range (k : ℕ) : finset (fin k) := ⟨list.fin_range k, list.nodup_fin_range k⟩ @[simp] lemma fin_range_card {k : ℕ} : (fin_range k).card = k := by simp [fin_range] @[simp] lemma mem_fin_range {k : ℕ} (m : fin k) : m ∈ fin_range k := list.mem_fin_range m /-- Given a finset `s` of `ℕ` contained in `{0,..., n-1}`, the corresponding finset in `fin n` is `s.attach_fin h` where `h` is a proof that all elements of `s` are less than `n`. -/ def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.veq_of_eq) s.2⟩ @[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ (a : ℕ) ∈ s := ⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁, λ h, multiset.mem_pmap.2 ⟨a, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ /-! ### choose -/ section choose variables (p : α → Prop) [decidable_pred p] (l : finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose theorem lt_wf {α} : well_founded (@has_lt.lt (finset α) _) := have H : subrelation (@has_lt.lt (finset α) _) (inv_image (<) card), from λ x y hxy, card_lt_card hxy, subrelation.wf H $ inv_image.wf _ $ nat.lt_wf end finset namespace equiv /-- Given an equivalence `α` to `β`, produce an equivalence between `finset α` and `finset β`. -/ protected def finset_congr (e : α ≃ β) : finset α ≃ finset β := { to_fun := λ s, s.map e.to_embedding, inv_fun := λ s, s.map e.symm.to_embedding, left_inv := λ s, by simp [finset.map_map], right_inv := λ s, by simp [finset.map_map] } @[simp] lemma finset_congr_apply (e : α ≃ β) (s : finset α) : e.finset_congr s = s.map e.to_embedding := rfl @[simp] lemma finset_congr_symm_apply (e : α ≃ β) (s : finset β) : e.finset_congr.symm s = s.map e.symm.to_embedding := rfl end equiv namespace list variable [decidable_eq α] theorem to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length := congr_arg card $ (@multiset.erase_dup_eq_self α _ l).2 h end list namespace multiset lemma disjoint_to_finset [decidable_eq α] (m1 m2 : multiset α) : _root_.disjoint m1.to_finset m2.to_finset ↔ m1.disjoint m2 := begin rw finset.disjoint_iff_ne, split, { intro h, intros a ha1 ha2, rw ← multiset.mem_to_finset at ha1 ha2, exact h _ ha1 _ ha2 rfl }, { rintros h a ha b hb rfl, rw multiset.mem_to_finset at ha hb, exact h ha hb } end end multiset
3e9cc300c1e51c9c43b6756b69b0bc95a57830f6
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/group_theory/free_product.lean
3c673a6011b78802b673c2c90c6084f42628fbb1
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
14,403
lean
/- Copyright (c) 2021 David Wärn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Wärn -/ import algebra.free_monoid import group_theory.congruence import data.list.chain /-! # The free product of groups or monoids Given an `ι`-indexed family `M` of monoids, we define their free product (categorical coproduct) `free_product M`. When `ι` and all `M i` have decidable equality, the free product bijects with the type `word M` of reduced words. This bijection is constructed by defining an action of `free_product M` on `word M`. When `M i` are all groups, `free_product M` is also a group (and the coproduct in the category of groups). ## Main definitions - `free_product M`: the free product, defined as a quotient of a free monoid. - `free_product.of {i} : M i →* free_product M`. - `free_product.lift : (Π {i}, M i →* N) ≃ (free_product M →* N)`: the universal property. - `free_product.word M`: the type of reduced words. - `free_product.word.equiv M : free_product M ≃ word M`. ## Remarks There are many answers to the question "what is the free product of a family `M` of monoids?", and they are all equivalent but not obviously equivalent. We provide two answers. The first, almost tautological answer is given by `free_product M`, which is a quotient of the type of words in the alphabet `Σ i, M i`. It's straightforward to define and easy to prove its universal property. But this answer is not completely satisfactory, because it's difficult to tell when two elements `x y : free_product M` are distinct since `free_product M` is defined as a quotient. The second, maximally efficient answer is given by `word M`. An element of `word M` is a word in the alphabet `Σ i, M i`, where the letter `⟨i, 1⟩` doesn't occur and no adjacent letters share an index `i`. Since we only work with reduced words, there is no need for quotienting, and it is easy to tell when two elements are distinct. However it's not obvious that this is even a monoid! We prove that every element of `free_product M` can be represented by a unique reduced word, i.e. `free_product M` and `word M` are equivalent types. This means that `word M` can be given a monoid structure, and it lets us tell when two elements of `free_product M` are distinct. There is also a completely tautological, maximally inefficient answer given by `algebra.category.Mon.colimits`. Whereas `free_product M` at least ensures that (any instance of) associativity holds by reflexivity, in this answer associativity holds because of quotienting. Yet another answer, which is constructively more satisfying, could be obtained by showing that `free_product.rel` is confluent. ## References [van der Waerden, *Free products of groups*][MR25465] -/ variables {ι : Type*} (M : Π i : ι, Type*) [Π i, monoid (M i)] /-- A relation on the free monoid on alphabet `Σ i, M i`, relating `⟨i, 1⟩` with `1` and `⟨i, x⟩ * ⟨i, y⟩` with `⟨i, x * y⟩`. -/ inductive free_product.rel : free_monoid (Σ i, M i) → free_monoid (Σ i, M i) → Prop | of_one (i : ι) : free_product.rel (free_monoid.of ⟨i, 1⟩) 1 | of_mul {i : ι} (x y : M i) : free_product.rel (free_monoid.of ⟨i, x⟩ * free_monoid.of ⟨i, y⟩) (free_monoid.of ⟨i, x * y⟩) /-- The free product (categorical coproduct) of an indexed family of monoids. -/ @[derive [monoid, inhabited]] def free_product : Type* := (con_gen (free_product.rel M)).quotient namespace free_product /-- The type of reduced words. A reduced word cannot contain a letter `1`, and no two adjacent letters can come from the same summand. -/ @[ext] structure word := (to_list : list (Σ i, M i)) (ne_one : ∀ l ∈ to_list, sigma.snd l ≠ 1) (chain_ne : to_list.chain' (λ l l', sigma.fst l ≠ sigma.fst l')) variable {M} /-- The inclusion of a summand into the free product. -/ def of {i : ι} : M i →* free_product M := { to_fun := λ x, con.mk' _ (free_monoid.of $ sigma.mk i x), map_one' := (con.eq _).mpr (con_gen.rel.of _ _ (free_product.rel.of_one i)), map_mul' := λ x y, eq.symm $ (con.eq _).mpr (con_gen.rel.of _ _ (free_product.rel.of_mul x y)) } lemma of_apply {i} (m : M i) : of m = con.mk' _ (free_monoid.of $ sigma.mk i m) := rfl variables {N : Type*} [monoid N] /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma ext_hom (f g : free_product M →* N) (h : ∀ i, f.comp (of : M i →* _) = g.comp of) : f = g := (monoid_hom.cancel_right con.mk'_surjective).mp $ free_monoid.hom_eq $ λ ⟨i, x⟩, by rw [monoid_hom.comp_apply, monoid_hom.comp_apply, ←of_apply, ←monoid_hom.comp_apply, ←monoid_hom.comp_apply, h] /-- A map out of the free product corresponds to a family of maps out of the summands. This is the universal property of the free product, charaterizing it as a categorical coproduct. -/ @[simps symm_apply] def lift : (Π i, M i →* N) ≃ (free_product M →* N) := { to_fun := λ fi, con.lift _ (free_monoid.lift $ λ p : Σ i, M i, fi p.fst p.snd) $ con.con_gen_le begin simp_rw [con.rel_eq_coe, con.ker_rel], rintros _ _ (i | ⟨i, x, y⟩), { change free_monoid.lift _ (free_monoid.of _) = free_monoid.lift _ 1, simp only [monoid_hom.map_one, free_monoid.lift_eval_of], }, { change free_monoid.lift _ (free_monoid.of _ * free_monoid.of _) = free_monoid.lift _ (free_monoid.of _), simp only [monoid_hom.map_mul, free_monoid.lift_eval_of], } end, inv_fun := λ f i, f.comp of, left_inv := by { intro fi, ext i x, rw [monoid_hom.comp_apply, of_apply, con.lift_mk', free_monoid.lift_eval_of], }, right_inv := by { intro f, ext i x, simp only [monoid_hom.comp_apply, of_apply, con.lift_mk', free_monoid.lift_eval_of], } } @[simp] lemma lift_of {N} [monoid N] (fi : Π i, M i →* N) {i} (m : M i) : lift fi (of m) = fi i m := by conv_rhs { rw [←lift.symm_apply_apply fi, lift_symm_apply, monoid_hom.comp_apply] } @[elab_as_eliminator] lemma induction_on {C : free_product M → Prop} (m : free_product M) (h_one : C 1) (h_of : ∀ (i) (m : M i), C (of m)) (h_mul : ∀ (x y), C x → C y → C (x * y)) : C m := begin let S : submonoid (free_product M) := ⟨set_of C, h_one, h_mul⟩, convert subtype.prop (lift (λ i, of.cod_mrestrict S (h_of i)) m), change monoid_hom.id _ m = S.subtype.comp _ m, congr, ext, simp [monoid_hom.cod_mrestrict], end lemma of_left_inverse [decidable_eq ι] (i : ι) : function.left_inverse (lift $ function.update 1 i (monoid_hom.id (M i))) of := λ x, by simp only [lift_of, function.update_same, monoid_hom.id_apply] lemma of_injective (i : ι) : function.injective ⇑(of : M i →* _) := by { classical, exact (of_left_inverse i).injective } section group variables (G : ι → Type*) [Π i, group (G i)] instance : has_inv (free_product G) := { inv := mul_opposite.unop ∘ lift (λ i, (of : G i →* _).op.comp (mul_equiv.inv' (G i)).to_monoid_hom) } lemma inv_def (x : free_product G) : x⁻¹ = mul_opposite.unop (lift (λ i, (of : G i →* _).op.comp (mul_equiv.inv' (G i)).to_monoid_hom) x) := rfl instance : group (free_product G) := { mul_left_inv := begin intro m, rw inv_def, apply m.induction_on, { rw [monoid_hom.map_one, mul_opposite.unop_one, one_mul], }, { intros i m, change of m⁻¹ * of m = 1, rw [←of.map_mul, mul_left_inv, of.map_one], }, { intros x y hx hy, rw [monoid_hom.map_mul, mul_opposite.unop_mul, mul_assoc, ← mul_assoc _ x y, hx, one_mul, hy], }, end, ..free_product.has_inv G, ..free_product.monoid G } end group namespace word /-- The empty reduced word. -/ def empty : word M := { to_list := [], ne_one := λ _, false.elim, chain_ne := list.chain'_nil } instance : inhabited (word M) := ⟨empty⟩ /-- A reduced word determines an element of the free product, given by multiplication. -/ def prod (w : word M) : free_product M := list.prod (w.to_list.map $ λ l, of l.snd) @[simp] lemma prod_nil : prod (empty : word M) = 1 := rfl /-- `fst_idx w` is `some i` if the first letter of `w` is `⟨i, m⟩` with `m : M i`. If `w` is empty then it's `none`. -/ def fst_idx (w : word M) : option ι := w.to_list.head'.map sigma.fst lemma fst_idx_ne_iff {w : word M} {i} : fst_idx w ≠ some i ↔ ∀ l ∈ w.to_list.head', i ≠ sigma.fst l := not_iff_not.mp $ by simp [fst_idx] variable (M) /-- Given an index `i : ι`, `pair M i` is the type of pairs `(head, tail)` where `head : M i` and `tail : word M`, subject to the constraint that first letter of `tail` can't be `⟨i, m⟩`. By prepending `head` to `tail`, one obtains a new word. We'll show that any word can be uniquely obtained in this way. -/ @[ext] structure pair (i : ι) := (head : M i) (tail : word M) (fst_idx_ne : fst_idx tail ≠ some i) instance (i : ι) : inhabited (pair M i) := ⟨⟨1, empty, by tauto⟩⟩ variable {M} variables [∀ i, decidable_eq (M i)] /-- Given a pair `(head, tail)`, we can form a word by prepending `head` to `tail`, except if `head` is `1 : M i` then we have to just return `word` since we need the result to be reduced. -/ def rcons {i} (p : pair M i) : word M := if h : p.head = 1 then p.tail else { to_list := ⟨i, p.head⟩ :: p.tail.to_list, ne_one := by { rintros l (rfl | hl), exact h, exact p.tail.ne_one l hl }, chain_ne := p.tail.chain_ne.cons' (fst_idx_ne_iff.mp p.fst_idx_ne) } /-- Given a word of the form `⟨l :: ls, h1, h2⟩`, we can form a word of the form `⟨ls, _, _⟩`, dropping the first letter. -/ private def mk_aux {l} (ls : list (Σ i, M i)) (h1 : ∀ l' ∈ l :: ls, sigma.snd l' ≠ 1) (h2 : (l :: ls).chain' _) : word M := ⟨ls, λ l' hl, h1 _ (list.mem_cons_of_mem _ hl), h2.tail⟩ lemma cons_eq_rcons {i} {m : M i} {ls h1 h2} : word.mk (⟨i, m⟩ :: ls) h1 h2 = rcons ⟨m, mk_aux ls h1 h2, fst_idx_ne_iff.mpr h2.rel_head'⟩ := by { rw [rcons, dif_neg], refl, exact h1 ⟨i, m⟩ (ls.mem_cons_self _) } @[simp] lemma prod_rcons {i} (p : pair M i) : prod (rcons p) = of p.head * prod p.tail := if hm : p.head = 1 then by rw [rcons, dif_pos hm, hm, monoid_hom.map_one, one_mul] else by rw [rcons, dif_neg hm, prod, list.map_cons, list.prod_cons, prod] lemma rcons_inj {i} : function.injective (rcons : pair M i → word M) := begin rintros ⟨m, w, h⟩ ⟨m', w', h'⟩ he, by_cases hm : m = 1; by_cases hm' : m' = 1, { simp only [rcons, dif_pos hm, dif_pos hm'] at he, cc, }, { exfalso, simp only [rcons, dif_pos hm, dif_neg hm'] at he, rw he at h, exact h rfl }, { exfalso, simp only [rcons, dif_pos hm', dif_neg hm] at he, rw ←he at h', exact h' rfl, }, { have : m = m' ∧ w.to_list = w'.to_list, { simpa only [rcons, dif_neg hm, dif_neg hm', true_and, eq_self_iff_true, subtype.mk_eq_mk, heq_iff_eq, ←subtype.ext_iff_val] using he }, rcases this with ⟨rfl, h⟩, congr, exact word.ext _ _ h, } end variable [decidable_eq ι] /-- Given `i : ι`, any reduced word can be decomposed into a pair `p` such that `w = rcons p`. -/ -- This definition is computable but not very nice to look at. Thankfully we don't have to inspect -- it, since `rcons` is known to be injective. private def equiv_pair_aux (i) : Π w : word M, { p : pair M i // rcons p = w } | w@⟨[], _, _⟩ := ⟨⟨1, w, by rintro ⟨⟩⟩, dif_pos rfl⟩ | w@⟨⟨j, m⟩ :: ls, h1, h2⟩ := if ij : i = j then { val := { head := ij.symm.rec m, tail := mk_aux ls h1 h2, fst_idx_ne := by cases ij; exact fst_idx_ne_iff.mpr h2.rel_head' }, property := by cases ij; exact cons_eq_rcons.symm } else ⟨⟨1, w, (option.some_injective _).ne (ne.symm ij)⟩, dif_pos rfl⟩ /-- The equivalence between words and pairs. Given a word, it decomposes it as a pair by removing the first letter if it comes from `M i`. Given a pair, it prepends the head to the tail. -/ def equiv_pair (i) : word M ≃ pair M i := { to_fun := λ w, (equiv_pair_aux i w).val, inv_fun := rcons, left_inv := λ w, (equiv_pair_aux i w).property, right_inv := λ p, rcons_inj (equiv_pair_aux i _).property } lemma equiv_pair_symm (i) (p : pair M i) : (equiv_pair i).symm p = rcons p := rfl lemma equiv_pair_eq_of_fst_idx_ne {i} {w : word M} (h : fst_idx w ≠ some i) : equiv_pair i w = ⟨1, w, h⟩ := (equiv_pair i).apply_eq_iff_eq_symm_apply.mpr $ eq.symm (dif_pos rfl) instance summand_action (i) : mul_action (M i) (word M) := { smul := λ m w, rcons { head := m * (equiv_pair i w).head, ..equiv_pair i w }, one_smul := λ w, by { simp_rw [one_mul], apply (equiv_pair i).symm_apply_eq.mpr, ext; refl }, mul_smul := λ m m' w, by simp only [mul_assoc, ←equiv_pair_symm, equiv.apply_symm_apply], } instance : mul_action (free_product M) (word M) := mul_action.of_End_hom (lift (λ i, mul_action.to_End_hom)) lemma of_smul_def (i) (w : word M) (m : M i) : of m • w = rcons { head := m * (equiv_pair i w).head, ..equiv_pair i w } := rfl lemma cons_eq_smul {i} {m : M i} {ls h1 h2} : word.mk (⟨i, m⟩ :: ls) h1 h2 = of m • mk_aux ls h1 h2 := by rw [cons_eq_rcons, of_smul_def, equiv_pair_eq_of_fst_idx_ne _]; simp only [mul_one] lemma smul_induction {C : word M → Prop} (h_empty : C empty) (h_smul : ∀ i (m : M i) w, C w → C (of m • w)) (w : word M) : C w := begin cases w with ls h1 h2, induction ls with l ls ih, { exact h_empty }, cases l with i m, rw cons_eq_smul, exact h_smul _ _ _ (ih _ _), end @[simp] lemma prod_smul (m) : ∀ w : word M, prod (m • w) = m * prod w := begin apply m.induction_on, { intro, rw [one_smul, one_mul] }, { intros, rw [of_smul_def, prod_rcons, of.map_mul, mul_assoc, ←prod_rcons, ←equiv_pair_symm, equiv.symm_apply_apply] }, { intros x y hx hy w, rw [mul_smul, hx, hy, mul_assoc] }, end /-- Each element of the free product corresponds to a unique reduced word. -/ def equiv : free_product M ≃ word M := { to_fun := λ m, m • empty, inv_fun := λ w, prod w, left_inv := λ m, by dsimp only; rw [prod_smul, prod_nil, mul_one], right_inv := begin apply smul_induction, { dsimp only, rw [prod_nil, one_smul], }, { dsimp only, intros i m w ih, rw [prod_smul, mul_smul, ih], }, end } instance : decidable_eq (word M) := function.injective.decidable_eq word.ext instance : decidable_eq (free_product M) := word.equiv.decidable_eq end word end free_product
99bfc90520fa384576032aff59841bb926a4575e
64874bd1010548c7f5a6e3e8902efa63baaff785
/hott/init/types/prod.hlean
a9e99c5cf7144c000546defbf3b5069cedea051f
[ "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
3,390
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad -/ prelude import ..wf definition pair := @prod.mk namespace prod notation A * B := prod A B notation A × B := prod A B namespace low_precedence_times reserve infixr `*`:30 -- conflicts with notation for multiplication infixr `*` := prod end low_precedence_times -- TODO: add lemmas about flip to /hott/types/prod.hlean definition flip {A B : Type} (a : A × B) : B × A := pair (pr2 a) (pr1 a) notation `pr₁` := pr1 notation `pr₂` := pr2 -- notation for n-ary tuples notation `(` h `,` t:(foldl `,` (e r, prod.mk r e) h) `)` := t open well_founded section variables {A B : Type} variable (Ra : A → A → Type) variable (Rb : B → B → Type) -- Lexicographical order based on Ra and Rb inductive lex : A × B → A × B → Type := left : ∀{a₁ b₁} a₂ b₂, Ra a₁ a₂ → lex (a₁, b₁) (a₂, b₂), right : ∀a {b₁ b₂}, Rb b₁ b₂ → lex (a, b₁) (a, b₂) -- Relational product based on Ra and Rb inductive rprod : A × B → A × B → Type := intro : ∀{a₁ b₁ a₂ b₂}, Ra a₁ a₂ → Rb b₁ b₂ → rprod (a₁, b₁) (a₂, b₂) end context parameters {A B : Type} parameters {Ra : A → A → Type} {Rb : B → B → Type} infix `≺`:50 := lex Ra Rb definition lex.accessible {a} (aca : acc Ra a) (acb : ∀b, acc Rb b): ∀b, acc (lex Ra Rb) (a, b) := acc.rec_on aca (λxa aca (iHa : ∀y, Ra y xa → ∀b, acc (lex Ra Rb) (y, b)), λb, acc.rec_on (acb b) (λxb acb (iHb : ∀y, Rb y xb → acc (lex Ra Rb) (xa, y)), acc.intro (xa, xb) (λp (lt : p ≺ (xa, xb)), have aux : xa = xa → xb = xb → acc (lex Ra Rb) p, from @lex.rec_on A B Ra Rb (λp₁ p₂ h, pr₁ p₂ = xa → pr₂ p₂ = xb → acc (lex Ra Rb) p₁) p (xa, xb) lt (λa₁ b₁ a₂ b₂ (H : Ra a₁ a₂) (eq₂ : a₂ = xa) (eq₃ : b₂ = xb), show acc (lex Ra Rb) (a₁, b₁), from have Ra₁ : Ra a₁ xa, from eq.rec_on eq₂ H, iHa a₁ Ra₁ b₁) (λa b₁ b₂ (H : Rb b₁ b₂) (eq₂ : a = xa) (eq₃ : b₂ = xb), show acc (lex Ra Rb) (a, b₁), from have Rb₁ : Rb b₁ xb, from eq.rec_on eq₃ H, have eq₂' : xa = a, from eq.rec_on eq₂ rfl, eq.rec_on eq₂' (iHb b₁ Rb₁)), aux rfl rfl))) -- The lexicographical order of well founded relations is well-founded definition lex.wf (Ha : well_founded Ra) (Hb : well_founded Rb) : well_founded (lex Ra Rb) := well_founded.intro (λp, destruct p (λa b, lex.accessible (Ha a) (well_founded.apply Hb) b)) -- Relational product is a subrelation of the lex definition rprod.sub_lex : ∀ a b, rprod Ra Rb a b → lex Ra Rb a b := λa b H, rprod.rec_on H (λ a₁ b₁ a₂ b₂ H₁ H₂, lex.left Rb a₂ b₂ H₁) -- The relational product of well founded relations is well-founded definition rprod.wf (Ha : well_founded Ra) (Hb : well_founded Rb) : well_founded (rprod Ra Rb) := subrelation.wf (rprod.sub_lex) (lex.wf Ha Hb) end end prod
7374da48fb2ce972f9bd83a03c9ab685708b787b
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/linear_algebra/tensor_algebra.lean
234b55c0c6bc3e30190f56975b7a72ac37bf211c
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
4,401
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Adam Topaz. -/ import algebra.free_algebra import algebra.ring_quot /-! # Tensor Algebras Given a commutative semiring `R`, and an `R`-module `M`, we construct the tensor algebra of `M`. This is the free `R`-algebra generated (`R`-linearly) by the module `M`. ## Notation 1. `tensor_algebra R M` is the tensor algebra itself. It is endowed with an R-algebra structure. 2. `tensor_algebra.ι R M` is the canonical R-linear map `M → tensor_algebra R M`. 3. Given a linear map `f : M → A` to an R-algebra `A`, `lift R M f` is the lift of `f` to an `R`-algebra morphism `tensor_algebra R M → A`. ## Theorems 1. `ι_comp_lift` states that the composition `(lift R M f) ∘ (ι R M)` is identical to `f`. 2. `lift_unique` states that whenever an R-algebra morphism `g : tensor_algebra R M → A` is given whose composition with `ι R M` is `f`, then one has `g = lift R M f`. 3. `hom_ext` is a variant of `lift_unique` in the form of an extensionality theorem. 4. `lift_comp_ι` is a combination of `ι_comp_lift` and `lift_unique`. It states that the lift of the composition of an algebra morphism with `ι` is the algebra morphism itself. ## Implementation details As noted above, the tensor algebra of `M` is constructed as the free `R`-algebra generated by `M`, modulo the additional relations making the inclusion of `M` into an `R`-linear map. -/ variables (R : Type*) [comm_semiring R] variables (M : Type*) [add_comm_group M] [semimodule R M] namespace tensor_algebra /-- An inductively defined relation on `pre R M` used to force the initial algebra structure on the associated quotient. -/ inductive rel : free_algebra R M → free_algebra R M → Prop -- force `ι` to be linear | add {a b : M} : rel (free_algebra.ι R M (a+b)) (free_algebra.ι R M a + free_algebra.ι R M b) | smul {r : R} {a : M} : rel (free_algebra.ι R M (r • a)) (algebra_map R (free_algebra R M) r * free_algebra.ι R M a) end tensor_algebra /-- The tensor algebra of the module `M` over the commutative semiring `R`. -/ @[derive [inhabited, semiring, algebra R]] def tensor_algebra := ring_quot (tensor_algebra.rel R M) namespace tensor_algebra /-- The canonical linear map `M →ₗ[R] tensor_algebra R M`. -/ def ι : M →ₗ[R] (tensor_algebra R M) := { to_fun := λ m, (ring_quot.mk_alg_hom R _ (free_algebra.ι R M m)), map_add' := λ x y, by { rw [←alg_hom.map_add], exact ring_quot.mk_alg_hom_rel R rel.add, }, map_smul' := λ r x, by { rw [←alg_hom.map_smul], exact ring_quot.mk_alg_hom_rel R rel.smul, } } lemma ring_quot_mk_alg_hom_free_algebra_ι_eq_ι (m : M) : ring_quot.mk_alg_hom R (rel R M) (free_algebra.ι R M m) = ι R M m := rfl /-- Given a linear map `f : M → A` where `A` is an `R`-algebra, `lift R M f` is the unique lift of `f` to a morphism of `R`-algebras `tensor_algebra R M → A`. -/ def lift {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) : tensor_algebra R M →ₐ[R] A := ring_quot.lift_alg_hom R (free_algebra.lift R M ⇑f) (λ x y h, by induction h; simp [algebra.smul_def]) variables {R M} @[simp] theorem ι_comp_lift {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) : (lift R M f).to_linear_map.comp (ι R M) = f := by { ext, simp [lift, ι], } @[simp] theorem lift_ι_apply {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) (x) : lift R M f (ι R M x) = f x := by { dsimp [lift, ι], refl, } @[simp] theorem lift_unique {A : Type*} [semiring A] [algebra R A] (f : M →ₗ[R] A) (g : tensor_algebra R M →ₐ[R] A) : g.to_linear_map.comp (ι R M) = f ↔ g = lift R M f := begin refine ⟨λ hyp, _, λ hyp, by rw [hyp, ι_comp_lift]⟩, ext, rw ←hyp, simp [lift], refl, end attribute [irreducible] tensor_algebra ι lift @[simp] theorem lift_comp_ι {A : Type*} [semiring A] [algebra R A] (g : tensor_algebra R M →ₐ[R] A) : lift R M (g.to_linear_map.comp (ι R M)) = g := by {symmetry, rw ←lift_unique} @[ext] theorem hom_ext {A : Type*} [semiring A] [algebra R A] {f g : tensor_algebra R M →ₐ[R] A} (w : f.to_linear_map.comp (ι R M) = g.to_linear_map.comp (ι R M)) : f = g := begin let h := g.to_linear_map.comp (ι R M), have : g = lift R M h, by rw ←lift_unique, rw [this, ←lift_unique, w], end end tensor_algebra
0385139ed9f73d18273e10e121e0a31ee2e9a637
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/data/padics/padic_numbers.lean
06845a7d246bb846a489423e9271b38e3df28500
[ "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
32,458
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 -/ import data.padics.padic_norm import analysis.normed_space.basic /-! # p-adic numbers This file defines the p-adic numbers (rationals) ℚ_p as the completion of ℚ with respect to the p-adic norm. We show that the p-adic norm on ℚ extends to ℚ_p, that ℚ is embedded in ℚ_p, and that ℚ_p is Cauchy complete. ## Important definitions * `padic` : the type of p-adic numbers * `padic_norm_e` : the rational valued p-adic norm on ℚ_p ## Notation We introduce the notation ℚ_[p] for the p-adic numbers. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact (prime p)]` as a type class argument. We use the same concrete Cauchy sequence construction that is used to construct ℝ. ℚ_p inherits a field structure from this construction. The extension of the norm on ℚ to ℚ_p is *not* analogous to extending the absolute value to ℝ, and hence the proof that ℚ_p is complete is different from the proof that ℝ is complete. A small special-purpose simplification tactic, `padic_index_simp`, is used to manipulate sequence indices in the proof that the norm extends. `padic_norm_e` is the rational-valued p-adic norm on ℚ_p. To instantiate ℚ_p as a normed field, we must cast this into a ℝ-valued norm. The ℝ-valued norm, using notation ∥ ∥ from normed spaces, is the canonical representation of this norm. Coercions from ℚ to ℚ_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, norm, valuation, cauchy, completion, p-adic completion -/ noncomputable theory open_locale classical open nat multiplicity padic_norm cau_seq cau_seq.completion metric /-- The type of Cauchy sequences of rationals with respect to the p-adic norm. -/ @[reducible] def padic_seq (p : ℕ) [fact p.prime] := cau_seq _ (padic_norm p) namespace padic_seq section variables {p : ℕ} [fact p.prime] /-- The p-adic norm of the entries of a nonzero Cauchy sequence of rationals is eventually constant. -/ lemma stationary {f : cau_seq ℚ (padic_norm p)} (hf : ¬ f ≈ 0) : ∃ N, ∀ m n, N ≤ m → N ≤ n → padic_norm p (f n) = padic_norm p (f m) := have ∃ ε > 0, ∃ N1, ∀ j ≥ N1, ε ≤ padic_norm p (f j), from cau_seq.abv_pos_of_not_lim_zero $ not_lim_zero_of_not_congr_zero hf, let ⟨ε, hε, N1, hN1⟩ := this, ⟨N2, hN2⟩ := cau_seq.cauchy₂ f hε in ⟨ max N1 N2, λ n m hn hm, have padic_norm p (f n - f m) < ε, from hN2 _ _ (max_le_iff.1 hn).2 (max_le_iff.1 hm).2, have padic_norm p (f n - f m) < padic_norm p (f n), from lt_of_lt_of_le this $ hN1 _ (max_le_iff.1 hn).1, have padic_norm p (f n - f m) < max (padic_norm p (f n)) (padic_norm p (f m)), from lt_max_iff.2 (or.inl this), begin by_contradiction hne, rw ←padic_norm.neg p (f m) at hne, have hnam := add_eq_max_of_ne p hne, rw [padic_norm.neg, max_comm] at hnam, rw [←hnam, sub_eq_add_neg, add_comm] at this, apply _root_.lt_irrefl _ this end ⟩ /-- For all n ≥ stationary_point f hf, the p-adic norm of f n is the same. -/ def stationary_point {f : padic_seq p} (hf : ¬ f ≈ 0) : ℕ := classical.some $ stationary hf lemma stationary_point_spec {f : padic_seq p} (hf : ¬ f ≈ 0) : ∀ {m n}, m ≥ stationary_point hf → n ≥ stationary_point hf → padic_norm p (f n) = padic_norm p (f m) := classical.some_spec $ stationary hf /-- Since the norm of the entries of a Cauchy sequence is eventually stationary, we can lift the norm to sequences. -/ def norm (f : padic_seq p) : ℚ := if hf : f ≈ 0 then 0 else padic_norm p (f (stationary_point hf)) lemma norm_zero_iff (f : padic_seq p) : f.norm = 0 ↔ f ≈ 0 := begin constructor, { intro h, by_contradiction hf, unfold norm at h, split_ifs at h, apply hf, intros ε hε, existsi stationary_point hf, intros j hj, have heq := stationary_point_spec hf (le_refl _) hj, simpa [h, heq] }, { intro h, simp [norm, h] } end end section embedding open cau_seq variables {p : ℕ} [fact p.prime] lemma equiv_zero_of_val_eq_of_equiv_zero {f g : padic_seq p} (h : ∀ k, padic_norm p (f k) = padic_norm p (g k)) (hf : f ≈ 0) : g ≈ 0 := λ ε hε, let ⟨i, hi⟩ := hf _ hε in ⟨i, λ j hj, by simpa [h] using hi _ hj⟩ lemma norm_nonzero_of_not_equiv_zero {f : padic_seq p} (hf : ¬ f ≈ 0) : f.norm ≠ 0 := hf ∘ f.norm_zero_iff.1 lemma norm_eq_norm_app_of_nonzero {f : padic_seq p} (hf : ¬ f ≈ 0) : ∃ k, f.norm = padic_norm p k ∧ k ≠ 0 := have heq : f.norm = padic_norm p (f $ stationary_point hf), by simp [norm, hf], ⟨f $ stationary_point hf, heq, λ h, norm_nonzero_of_not_equiv_zero hf (by simpa [h] using heq)⟩ lemma not_lim_zero_const_of_nonzero {q : ℚ} (hq : q ≠ 0) : ¬ lim_zero (const (padic_norm p) q) := λ h', hq $ const_lim_zero.1 h' lemma not_equiv_zero_const_of_nonzero {q : ℚ} (hq : q ≠ 0) : ¬ (const (padic_norm p) q) ≈ 0 := λ h : lim_zero (const (padic_norm p) q - 0), not_lim_zero_const_of_nonzero hq $ by simpa using h lemma norm_nonneg (f : padic_seq p) : f.norm ≥ 0 := if hf : f ≈ 0 then by simp [hf, norm] else by simp [norm, hf, padic_norm.nonneg] /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_left_left {f : padic_seq p} (hf : ¬ f ≈ 0) (v2 v3 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max (stationary_point hf) (max v2 v3))) := let i := max (stationary_point hf) (max v2 v3) in begin apply stationary_point_spec hf, { apply le_max_left }, { apply le_refl } end /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_left {f : padic_seq p} (hf : ¬ f ≈ 0) (v1 v3 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max v1 (max (stationary_point hf) v3))) := let i := max v1 (max (stationary_point hf) v3) in begin apply stationary_point_spec hf, { apply le_trans, { apply le_max_left _ v3 }, { apply le_max_right } }, { apply le_refl } end /-- An auxiliary lemma for manipulating sequence indices. -/ lemma lift_index_right {f : padic_seq p} (hf : ¬ f ≈ 0) (v1 v2 : ℕ) : padic_norm p (f (stationary_point hf)) = padic_norm p (f (max v1 (max v2 (stationary_point hf)))) := let i := max v1 (max v2 (stationary_point hf)) in begin apply stationary_point_spec hf, { apply le_trans, { apply le_max_right v2 }, { apply le_max_right } }, { apply le_refl } end end embedding end padic_seq section open padic_seq private meta def index_simp_core (hh hf hg : expr) (at_ : interactive.loc := interactive.loc.ns [none]) : tactic unit := do [v1, v2, v3] ← [hh, hf, hg].mmap (λ n, tactic.mk_app ``stationary_point [n] <|> return n), e1 ← tactic.mk_app ``lift_index_left_left [hh, v2, v3] <|> return `(true), e2 ← tactic.mk_app ``lift_index_left [hf, v1, v3] <|> return `(true), e3 ← tactic.mk_app ``lift_index_right [hg, v1, v2] <|> return `(true), sl ← [e1, e2, e3].mfoldl (λ s e, simp_lemmas.add s e) simp_lemmas.mk, when at_.include_goal (tactic.simp_target sl), hs ← at_.get_locals, hs.mmap' (tactic.simp_hyp sl []) /-- This is a special-purpose tactic that lifts padic_norm (f (stationary_point f)) to padic_norm (f (max _ _ _)). -/ meta def tactic.interactive.padic_index_simp (l : interactive.parse interactive.types.pexpr_list) (at_ : interactive.parse interactive.types.location) : tactic unit := do [h, f, g] ← l.mmap tactic.i_to_expr, index_simp_core h f g at_ end namespace padic_seq section embedding open cau_seq variables {p : ℕ} [hp : fact p.prime] include hp lemma norm_mul (f g : padic_seq p) : (f * g).norm = f.norm * g.norm := if hf : f ≈ 0 then have hg : f * g ≈ 0, from mul_equiv_zero' _ hf, by simp [hf, hg, norm] else if hg : g ≈ 0 then have hf : f * g ≈ 0, from mul_equiv_zero _ hg, by simp [hf, hg, norm] else have hfg : ¬ f * g ≈ 0, by apply mul_not_equiv_zero; assumption, begin unfold norm, split_ifs, padic_index_simp [hfg, hf, hg], apply padic_norm.mul end lemma eq_zero_iff_equiv_zero (f : padic_seq p) : mk f = 0 ↔ f ≈ 0 := mk_eq lemma ne_zero_iff_nequiv_zero (f : padic_seq p) : mk f ≠ 0 ↔ ¬ f ≈ 0 := not_iff_not.2 (eq_zero_iff_equiv_zero _) lemma norm_const (q : ℚ) : norm (const (padic_norm p) q) = padic_norm p q := if hq : q = 0 then have (const (padic_norm p) q) ≈ 0, by simp [hq]; apply setoid.refl (const (padic_norm p) 0), by subst hq; simp [norm, this] else have ¬ (const (padic_norm p) q) ≈ 0, from not_equiv_zero_const_of_nonzero hq, by simp [norm, this] lemma norm_image (a : padic_seq p) (ha : ¬ a ≈ 0) : (∃ (n : ℤ), a.norm = ↑p ^ (-n)) := let ⟨k, hk, hk'⟩ := norm_eq_norm_app_of_nonzero ha in by simpa [hk] using padic_norm.image p hk' lemma norm_one : norm (1 : padic_seq p) = 1 := have h1 : ¬ (1 : padic_seq p) ≈ 0, from one_not_equiv_zero _, by simp [h1, norm, hp.one_lt] private lemma norm_eq_of_equiv_aux {f g : padic_seq p} (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) (hfg : f ≈ g) (h : padic_norm p (f (stationary_point hf)) ≠ padic_norm p (g (stationary_point hg))) (hgt : padic_norm p (f (stationary_point hf)) > padic_norm p (g (stationary_point hg))) : false := begin have hpn : padic_norm p (f (stationary_point hf)) - padic_norm p (g (stationary_point hg)) > 0, from sub_pos_of_lt hgt, cases hfg _ hpn with N hN, let i := max N (max (stationary_point hf) (stationary_point hg)), have hi : i ≥ N, from le_max_left _ _, have hN' := hN _ hi, padic_index_simp [N, hf, hg] at hN' h hgt, have hpne : padic_norm p (f i) ≠ padic_norm p (-(g i)), by rwa [ ←padic_norm.neg p (g i)] at h, let hpnem := add_eq_max_of_ne p hpne, have hpeq : padic_norm p ((f - g) i) = max (padic_norm p (f i)) (padic_norm p (g i)), { rwa padic_norm.neg at hpnem }, rw [hpeq, max_eq_left_of_lt hgt] at hN', have : padic_norm p (f i) < padic_norm p (f i), { apply lt_of_lt_of_le hN', apply sub_le_self, apply padic_norm.nonneg }, exact lt_irrefl _ this end private lemma norm_eq_of_equiv {f g : padic_seq p} (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) (hfg : f ≈ g) : padic_norm p (f (stationary_point hf)) = padic_norm p (g (stationary_point hg)) := begin by_contradiction h, cases (decidable.em (padic_norm p (f (stationary_point hf)) > padic_norm p (g (stationary_point hg)))) with hgt hngt, { exact norm_eq_of_equiv_aux hf hg hfg h hgt }, { apply norm_eq_of_equiv_aux hg hf (setoid.symm hfg) (ne.symm h), apply lt_of_le_of_ne, apply le_of_not_gt hngt, apply h } end theorem norm_equiv {f g : padic_seq p} (hfg : f ≈ g) : f.norm = g.norm := if hf : f ≈ 0 then have hg : g ≈ 0, from setoid.trans (setoid.symm hfg) hf, by simp [norm, hf, hg] else have hg : ¬ g ≈ 0, from hf ∘ setoid.trans hfg, by unfold norm; split_ifs; exact norm_eq_of_equiv hf hg hfg private lemma norm_nonarchimedean_aux {f g : padic_seq p} (hfg : ¬ f + g ≈ 0) (hf : ¬ f ≈ 0) (hg : ¬ g ≈ 0) : (f + g).norm ≤ max (f.norm) (g.norm) := begin unfold norm, split_ifs, padic_index_simp [hfg, hf, hg], apply padic_norm.nonarchimedean end theorem norm_nonarchimedean (f g : padic_seq p) : (f + g).norm ≤ max (f.norm) (g.norm) := if hfg : f + g ≈ 0 then have 0 ≤ max (f.norm) (g.norm), from le_max_left_of_le (norm_nonneg _), by simpa [hfg, norm] else if hf : f ≈ 0 then have hfg' : f + g ≈ g, { change lim_zero (f - 0) at hf, show lim_zero (f + g - g), by simpa using hf }, have hcfg : (f + g).norm = g.norm, from norm_equiv hfg', have hcl : f.norm = 0, from (norm_zero_iff f).2 hf, have max (f.norm) (g.norm) = g.norm, by rw hcl; exact max_eq_right (norm_nonneg _), by rw [this, hcfg] else if hg : g ≈ 0 then have hfg' : f + g ≈ f, { change lim_zero (g - 0) at hg, show lim_zero (f + g - f), by simpa [add_sub_cancel'] using hg }, have hcfg : (f + g).norm = f.norm, from norm_equiv hfg', have hcl : g.norm = 0, from (norm_zero_iff g).2 hg, have max (f.norm) (g.norm) = f.norm, by rw hcl; exact max_eq_left (norm_nonneg _), by rw [this, hcfg] else norm_nonarchimedean_aux hfg hf hg lemma norm_eq {f g : padic_seq p} (h : ∀ k, padic_norm p (f k) = padic_norm p (g k)) : f.norm = g.norm := if hf : f ≈ 0 then have hg : g ≈ 0, from equiv_zero_of_val_eq_of_equiv_zero h hf, by simp [hf, hg, norm] else have hg : ¬ g ≈ 0, from λ hg, hf $ equiv_zero_of_val_eq_of_equiv_zero (by simp [h]) hg, begin simp [hg, hf, norm], let i := max (stationary_point hf) (stationary_point hg), have hpf : padic_norm p (f (stationary_point hf)) = padic_norm p (f i), { apply stationary_point_spec, apply le_max_left, apply le_refl }, have hpg : padic_norm p (g (stationary_point hg)) = padic_norm p (g i), { apply stationary_point_spec, apply le_max_right, apply le_refl }, rw [hpf, hpg, h] end lemma norm_neg (a : padic_seq p) : (-a).norm = a.norm := norm_eq $ by simp lemma norm_eq_of_add_equiv_zero {f g : padic_seq p} (h : f + g ≈ 0) : f.norm = g.norm := have lim_zero (f + g - 0), from h, have f ≈ -g, from show lim_zero (f - (-g)), by simpa, have f.norm = (-g).norm, from norm_equiv this, by simpa [norm_neg] using this lemma add_eq_max_of_ne {f g : padic_seq p} (hfgne : f.norm ≠ g.norm) : (f + g).norm = max f.norm g.norm := have hfg : ¬f + g ≈ 0, from mt norm_eq_of_add_equiv_zero hfgne, if hf : f ≈ 0 then have lim_zero (f - 0), from hf, have f + g ≈ g, from show lim_zero ((f + g) - g), by simpa, have h1 : (f+g).norm = g.norm, from norm_equiv this, have h2 : f.norm = 0, from (norm_zero_iff _).2 hf, by rw [h1, h2]; rw max_eq_right (norm_nonneg _) else if hg : g ≈ 0 then have lim_zero (g - 0), from hg, have f + g ≈ f, from show lim_zero ((f + g) - f), by rw [add_sub_cancel']; simpa, have h1 : (f+g).norm = f.norm, from norm_equiv this, have h2 : g.norm = 0, from (norm_zero_iff _).2 hg, by rw [h1, h2]; rw max_eq_left (norm_nonneg _) else begin unfold norm at ⊢ hfgne, split_ifs at ⊢ hfgne, padic_index_simp [hfg, hf, hg] at ⊢ hfgne, apply padic_norm.add_eq_max_of_ne, simpa [hf, hg, norm] using hfgne end end embedding end padic_seq /-- The p-adic numbers `Q_[p]` are the Cauchy completion of `ℚ` with respect to the p-adic norm. -/ def padic (p : ℕ) [fact p.prime] := @cau_seq.completion.Cauchy _ _ _ _ (padic_norm p) _ notation `ℚ_[` p `]` := padic p namespace padic section completion variables {p : ℕ} [fact p.prime] /-- The discrete field structure on ℚ_p is inherited from the Cauchy completion construction. -/ instance field : field (ℚ_[p]) := cau_seq.completion.field instance : inhabited ℚ_[p] := ⟨0⟩ -- short circuits instance : has_zero ℚ_[p] := by apply_instance instance : has_one ℚ_[p] := by apply_instance instance : has_add ℚ_[p] := by apply_instance instance : has_mul ℚ_[p] := by apply_instance instance : has_sub ℚ_[p] := by apply_instance instance : has_neg ℚ_[p] := by apply_instance instance : has_div ℚ_[p] := by apply_instance instance : add_comm_group ℚ_[p] := by apply_instance instance : comm_ring ℚ_[p] := by apply_instance /-- Builds the equivalence class of a Cauchy sequence of rationals. -/ def mk : padic_seq p → ℚ_[p] := quotient.mk end completion section completion variables (p : ℕ) [fact p.prime] lemma mk_eq {f g : padic_seq p} : mk f = mk g ↔ f ≈ g := quotient.eq /-- Embeds the rational numbers in the p-adic numbers. -/ def of_rat : ℚ → ℚ_[p] := cau_seq.completion.of_rat @[simp] lemma of_rat_add : ∀ (x y : ℚ), of_rat p (x + y) = of_rat p x + of_rat p y := cau_seq.completion.of_rat_add @[simp] lemma of_rat_neg : ∀ (x : ℚ), of_rat p (-x) = -of_rat p x := cau_seq.completion.of_rat_neg @[simp] lemma of_rat_mul : ∀ (x y : ℚ), of_rat p (x * y) = of_rat p x * of_rat p y := cau_seq.completion.of_rat_mul @[simp] lemma of_rat_sub : ∀ (x y : ℚ), of_rat p (x - y) = of_rat p x - of_rat p y := cau_seq.completion.of_rat_sub @[simp] lemma of_rat_div : ∀ (x y : ℚ), of_rat p (x / y) = of_rat p x / of_rat p y := cau_seq.completion.of_rat_div @[simp] lemma of_rat_one : of_rat p 1 = 1 := rfl @[simp] lemma of_rat_zero : of_rat p 0 = 0 := rfl @[simp] lemma cast_eq_of_rat_of_nat (n : ℕ) : (↑n : ℚ_[p]) = of_rat p n := begin induction n with n ih, { refl }, { simpa using ih } end @[simp] lemma cast_eq_of_rat_of_int (n : ℤ) : ↑n = of_rat p n := by induction n; simp lemma cast_eq_of_rat : ∀ (q : ℚ), (↑q : ℚ_[p]) = of_rat p q | ⟨n, d, h1, h2⟩ := show ↑n / ↑d = _, from have (⟨n, d, h1, h2⟩ : ℚ) = rat.mk n d, from rat.num_denom', by simp [this, rat.mk_eq_div, of_rat_div] @[norm_cast] lemma coe_add : ∀ {x y : ℚ}, (↑(x + y) : ℚ_[p]) = ↑x + ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_neg : ∀ {x : ℚ}, (↑(-x) : ℚ_[p]) = -↑x := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_mul : ∀ {x y : ℚ}, (↑(x * y) : ℚ_[p]) = ↑x * ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_sub : ∀ {x y : ℚ}, (↑(x - y) : ℚ_[p]) = ↑x - ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_div : ∀ {x y : ℚ}, (↑(x / y) : ℚ_[p]) = ↑x / ↑y := by simp [cast_eq_of_rat] @[norm_cast] lemma coe_one : (↑1 : ℚ_[p]) = 1 := rfl @[norm_cast] lemma coe_zero : (↑0 : ℚ_[p]) = 0 := rfl lemma const_equiv {q r : ℚ} : const (padic_norm p) q ≈ const (padic_norm p) r ↔ q = r := ⟨ λ heq : lim_zero (const (padic_norm p) (q - r)), eq_of_sub_eq_zero $ const_lim_zero.1 heq, λ heq, by rw heq; apply setoid.refl _ ⟩ lemma of_rat_eq {q r : ℚ} : of_rat p q = of_rat p r ↔ q = r := ⟨(const_equiv p).1 ∘ quotient.eq.1, λ h, by rw h⟩ @[norm_cast] lemma coe_inj {q r : ℚ} : (↑q : ℚ_[p]) = ↑r ↔ q = r := by simp [cast_eq_of_rat, of_rat_eq] instance : char_zero ℚ_[p] := ⟨λ m n, by { rw ← rat.cast_coe_nat, norm_cast, exact id }⟩ end completion end padic /-- The rational-valued p-adic norm on ℚ_p is lifted from the norm on Cauchy sequences. The canonical form of this function is the normed space instance, with notation `∥ ∥`. -/ def padic_norm_e {p : ℕ} [hp : fact p.prime] : ℚ_[p] → ℚ := quotient.lift padic_seq.norm $ @padic_seq.norm_equiv _ _ namespace padic_norm_e section embedding open padic_seq variables {p : ℕ} [fact p.prime] lemma defn (f : padic_seq p) {ε : ℚ} (hε : ε > 0) : ∃ N, ∀ i ≥ N, padic_norm_e (⟦f⟧ - f i) < ε := begin simp only [padic.cast_eq_of_rat], change ∃ N, ∀ i ≥ N, (f - const _ (f i)).norm < ε, by_contradiction h, cases cauchy₂ f hε with N hN, have : ∀ N, ∃ i ≥ N, (f - const _ (f i)).norm ≥ ε, by simpa [not_forall] using h, rcases this N with ⟨i, hi, hge⟩, have hne : ¬ (f - const (padic_norm p) (f i)) ≈ 0, { intro h, unfold padic_seq.norm at hge; split_ifs at hge, exact not_lt_of_ge hge hε }, unfold padic_seq.norm at hge; split_ifs at hge, apply not_le_of_gt _ hge, cases decidable.em ((stationary_point hne) ≥ N) with hgen hngen, { apply hN; assumption }, { have := stationary_point_spec hne (le_refl _) (le_of_not_le hngen), rw ←this, apply hN, apply le_refl, assumption } end protected lemma nonneg (q : ℚ_[p]) : padic_norm_e q ≥ 0 := quotient.induction_on q $ norm_nonneg lemma zero_def : (0 : ℚ_[p]) = ⟦0⟧ := rfl lemma zero_iff (q : ℚ_[p]) : padic_norm_e q = 0 ↔ q = 0 := quotient.induction_on q $ by simpa only [zero_def, quotient.eq] using norm_zero_iff @[simp] protected lemma zero : padic_norm_e (0 : ℚ_[p]) = 0 := (zero_iff _).2 rfl /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ @[simp] protected lemma one' : padic_norm_e (1 : ℚ_[p]) = 1 := norm_one @[simp] protected lemma neg (q : ℚ_[p]) : padic_norm_e (-q) = padic_norm_e q := quotient.induction_on q $ norm_neg /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ theorem nonarchimedean' (q r : ℚ_[p]) : padic_norm_e (q + r) ≤ max (padic_norm_e q) (padic_norm_e r) := quotient.induction_on₂ q r $ norm_nonarchimedean /-- Theorems about `padic_norm_e` are named with a `'` so the names do not conflict with the equivalent theorems about `norm` (`∥ ∥`). -/ theorem add_eq_max_of_ne' {q r : ℚ_[p]} : padic_norm_e q ≠ padic_norm_e r → padic_norm_e (q + r) = max (padic_norm_e q) (padic_norm_e r) := quotient.induction_on₂ q r $ λ _ _, padic_seq.add_eq_max_of_ne lemma triangle_ineq (x y z : ℚ_[p]) : padic_norm_e (x - z) ≤ padic_norm_e (x - y) + padic_norm_e (y - z) := calc padic_norm_e (x - z) = padic_norm_e ((x - y) + (y - z)) : by rw sub_add_sub_cancel ... ≤ max (padic_norm_e (x - y)) (padic_norm_e (y - z)) : padic_norm_e.nonarchimedean' _ _ ... ≤ padic_norm_e (x - y) + padic_norm_e (y - z) : max_le_add_of_nonneg (padic_norm_e.nonneg _) (padic_norm_e.nonneg _) protected lemma add (q r : ℚ_[p]) : padic_norm_e (q + r) ≤ (padic_norm_e q) + (padic_norm_e r) := calc padic_norm_e (q + r) ≤ max (padic_norm_e q) (padic_norm_e r) : nonarchimedean' _ _ ... ≤ (padic_norm_e q) + (padic_norm_e r) : max_le_add_of_nonneg (padic_norm_e.nonneg _) (padic_norm_e.nonneg _) protected lemma mul' (q r : ℚ_[p]) : padic_norm_e (q * r) = (padic_norm_e q) * (padic_norm_e r) := quotient.induction_on₂ q r $ norm_mul instance : is_absolute_value (@padic_norm_e p _) := { abv_nonneg := padic_norm_e.nonneg, abv_eq_zero := zero_iff, abv_add := padic_norm_e.add, abv_mul := padic_norm_e.mul' } @[simp] lemma eq_padic_norm' (q : ℚ) : padic_norm_e (padic.of_rat p q) = padic_norm p q := norm_const _ protected theorem image' {q : ℚ_[p]} : q ≠ 0 → ∃ n : ℤ, padic_norm_e q = p ^ (-n) := quotient.induction_on q $ λ f hf, have ¬ f ≈ 0, from (ne_zero_iff_nequiv_zero f).1 hf, norm_image f this lemma sub_rev (q r : ℚ_[p]) : padic_norm_e (q - r) = padic_norm_e (r - q) := by rw ←(padic_norm_e.neg); simp end embedding end padic_norm_e namespace padic section complete open padic_seq padic theorem rat_dense' {p : ℕ} [fact p.prime] (q : ℚ_[p]) {ε : ℚ} (hε : ε > 0) : ∃ r : ℚ, padic_norm_e (q - r) < ε := quotient.induction_on q $ λ q', have ∃ N, ∀ m n ≥ N, padic_norm p (q' m - q' n) < ε, from cauchy₂ _ hε, let ⟨N, hN⟩ := this in ⟨q' N, begin simp only [padic.cast_eq_of_rat], change padic_seq.norm (q' - const _ (q' N)) < ε, cases decidable.em ((q' - const (padic_norm p) (q' N)) ≈ 0) with heq hne', { simpa only [heq, padic_seq.norm, dif_pos] }, { simp only [padic_seq.norm, dif_neg hne'], change padic_norm p (q' _ - q' _) < ε, have := stationary_point_spec hne', cases decidable.em (N ≥ stationary_point hne') with hle hle, { have := eq.symm (this (le_refl _) hle), simp at this, simpa [this] }, { apply hN, apply le_of_lt, apply lt_of_not_ge, apply hle, apply le_refl }} end⟩ variables {p : ℕ} [fact p.prime] (f : cau_seq _ (@padic_norm_e p _)) open classical private lemma div_nat_pos (n : ℕ) : (1 / ((n + 1): ℚ)) > 0 := div_pos zero_lt_one (by exact_mod_cast succ_pos _) def lim_seq : ℕ → ℚ := λ n, classical.some (rat_dense' (f n) (div_nat_pos n)) lemma exi_rat_seq_conv {ε : ℚ} (hε : 0 < ε) : ∃ N, ∀ i ≥ N, padic_norm_e (f i - ((lim_seq f) i : ℚ_[p])) < ε := begin refine (exists_nat_gt (1/ε)).imp (λ N hN i hi, _), have h := classical.some_spec (rat_dense' (f i) (div_nat_pos i)), refine lt_of_lt_of_le h (div_le_of_le_mul (by exact_mod_cast succ_pos _) _), rw right_distrib, apply le_add_of_le_of_nonneg, { exact le_mul_of_div_le hε (le_trans (le_of_lt hN) (by exact_mod_cast hi)) }, { apply le_of_lt, simpa } end lemma exi_rat_seq_conv_cauchy : is_cau_seq (padic_norm p) (lim_seq f) := assume ε hε, have hε3 : ε / 3 > 0, from div_pos hε (by norm_num), let ⟨N, hN⟩ := exi_rat_seq_conv f hε3, ⟨N2, hN2⟩ := f.cauchy₂ hε3 in begin existsi max N N2, intros j hj, suffices : padic_norm_e ((↑(lim_seq f j) - f (max N N2)) + (f (max N N2) - lim_seq f (max N N2))) < ε, { ring at this ⊢, rw [← padic_norm_e.eq_padic_norm', ← padic.cast_eq_of_rat], exact_mod_cast this }, { apply lt_of_le_of_lt, { apply padic_norm_e.add }, { have : (3 : ℚ) ≠ 0, by norm_num, have : ε = ε / 3 + ε / 3 + ε / 3, { apply eq_of_mul_eq_mul_left this, simp [left_distrib, mul_div_cancel' _ this ], ring }, rw this, apply add_lt_add, { suffices : padic_norm_e ((↑(lim_seq f j) - f j) + (f j - f (max N N2))) < ε / 3 + ε / 3, by simpa [sub_add_sub_cancel], apply lt_of_le_of_lt, { apply padic_norm_e.add }, { apply add_lt_add, { rw [padic_norm_e.sub_rev], apply_mod_cast hN, exact le_of_max_le_left hj }, { apply hN2, exact le_of_max_le_right hj, apply le_max_right }}}, { apply_mod_cast hN, apply le_max_left }}} end private def lim' : padic_seq p := ⟨_, exi_rat_seq_conv_cauchy f⟩ private def lim : ℚ_[p] := ⟦lim' f⟧ theorem complete' : ∃ q : ℚ_[p], ∀ ε > 0, ∃ N, ∀ i ≥ N, padic_norm_e (q - f i) < ε := ⟨ lim f, λ ε hε, let ⟨N, hN⟩ := exi_rat_seq_conv f (show ε / 2 > 0, from div_pos hε (by norm_num)), ⟨N2, hN2⟩ := padic_norm_e.defn (lim' f) (show ε / 2 > 0, from div_pos hε (by norm_num)) in begin existsi max N N2, intros i hi, suffices : padic_norm_e ((lim f - lim' f i) + (lim' f i - f i)) < ε, { ring at this; exact this }, { apply lt_of_le_of_lt, { apply padic_norm_e.add }, { have : ε = ε / 2 + ε / 2, by rw ←(add_self_div_two ε); simp, rw this, apply add_lt_add, { apply hN2, exact le_of_max_le_right hi }, { rw_mod_cast [padic_norm_e.sub_rev], apply hN, exact le_of_max_le_left hi }}} end ⟩ end complete section normed_space variables (p : ℕ) [fact p.prime] instance : has_dist ℚ_[p] := ⟨λ x y, padic_norm_e (x - y)⟩ instance : metric_space ℚ_[p] := { dist_self := by simp [dist], dist_comm := λ x y, by unfold dist; rw ←padic_norm_e.neg (x - y); simp, dist_triangle := begin intros, unfold dist, exact_mod_cast padic_norm_e.triangle_ineq _ _ _, end, eq_of_dist_eq_zero := begin unfold dist, intros _ _ h, apply eq_of_sub_eq_zero, apply (padic_norm_e.zero_iff _).1, exact_mod_cast h end } instance : has_norm ℚ_[p] := ⟨λ x, padic_norm_e x⟩ instance : normed_field ℚ_[p] := { dist_eq := λ _ _, rfl, norm_mul' := by simp [has_norm.norm, padic_norm_e.mul'] } instance : is_absolute_value (λ a : ℚ_[p], ∥a∥) := { abv_nonneg := norm_nonneg, abv_eq_zero := λ _, norm_eq_zero, abv_add := norm_add_le, abv_mul := by simp [has_norm.norm, padic_norm_e.mul'] } theorem rat_dense {p : ℕ} {hp : fact p.prime} (q : ℚ_[p]) {ε : ℝ} (hε : ε > 0) : ∃ r : ℚ, ∥q - r∥ < ε := let ⟨ε', hε'l, hε'r⟩ := exists_rat_btwn hε, ⟨r, hr⟩ := rat_dense' q (by simpa using hε'l) in ⟨r, lt.trans (by simpa [has_norm.norm] using hr) hε'r⟩ end normed_space end padic namespace padic_norm_e section normed_space variables {p : ℕ} [hp : fact p.prime] include hp @[simp] protected lemma mul (q r : ℚ_[p]) : ∥q * r∥ = ∥q∥ * ∥r∥ := by simp [has_norm.norm, padic_norm_e.mul'] protected lemma is_norm (q : ℚ_[p]) : ↑(padic_norm_e q) = ∥q∥ := rfl theorem nonarchimedean (q r : ℚ_[p]) : ∥q + r∥ ≤ max (∥q∥) (∥r∥) := begin unfold has_norm.norm, exact_mod_cast nonarchimedean' _ _ end theorem add_eq_max_of_ne {q r : ℚ_[p]} (h : ∥q∥ ≠ ∥r∥) : ∥q+r∥ = max (∥q∥) (∥r∥) := begin unfold has_norm.norm, apply_mod_cast add_eq_max_of_ne', intro h', apply h, unfold has_norm.norm, exact_mod_cast h' end @[simp] lemma eq_padic_norm (q : ℚ) : ∥(↑q : ℚ_[p])∥ = padic_norm p q := begin unfold has_norm.norm, rw [← padic_norm_e.eq_padic_norm', ← padic.cast_eq_of_rat] end instance : nondiscrete_normed_field ℚ_[p] := { non_trivial := ⟨padic.of_rat p (p⁻¹), begin have h0 : p ≠ 0 := ne_of_gt (hp.pos), have h1 : 1 < p := hp.one_lt, rw [← padic.cast_eq_of_rat, eq_padic_norm], simp only [padic_norm, inv_eq_zero], simp only [if_neg] {discharger := `[exact_mod_cast h0]}, norm_cast, simp only [padic_val_rat.inv] {discharger := `[exact_mod_cast h0]}, rw [neg_neg, padic_val_rat.padic_val_rat_self h1], erw _root_.pow_one, exact_mod_cast h1, end⟩ } protected theorem image {q : ℚ_[p]} : q ≠ 0 → ∃ n : ℤ, ∥q∥ = ↑((↑p : ℚ) ^ (-n)) := quotient.induction_on q $ λ f hf, have ¬ f ≈ 0, from (padic_seq.ne_zero_iff_nequiv_zero f).1 hf, let ⟨n, hn⟩ := padic_seq.norm_image f this in ⟨n, congr_arg coe hn⟩ protected lemma is_rat (q : ℚ_[p]) : ∃ q' : ℚ, ∥q∥ = ↑q' := if h : q = 0 then ⟨0, by simp [h]⟩ else let ⟨n, hn⟩ := padic_norm_e.image h in ⟨_, hn⟩ def rat_norm (q : ℚ_[p]) : ℚ := classical.some (padic_norm_e.is_rat q) lemma eq_rat_norm (q : ℚ_[p]) : ∥q∥ = rat_norm q := classical.some_spec (padic_norm_e.is_rat q) theorem norm_rat_le_one : ∀ {q : ℚ} (hq : ¬ p ∣ q.denom), ∥(q : ℚ_[p])∥ ≤ 1 | ⟨n, d, hn, hd⟩ := λ hq : ¬ p ∣ d, if hnz : n = 0 then have (⟨n, d, hn, hd⟩ : ℚ) = 0, from rat.zero_iff_num_zero.mpr hnz, by norm_num [this] else begin have hnz' : {rat . num := n, denom := d, pos := hn, cop := hd} ≠ 0, from mt rat.zero_iff_num_zero.1 hnz, rw [padic_norm_e.eq_padic_norm], norm_cast, rw [padic_norm.eq_fpow_of_nonzero p hnz', padic_val_rat_def p hnz'], have h : (multiplicity p d).get _ = 0, by simp [multiplicity_eq_zero_of_not_dvd, hq], simp only, norm_cast, rw_mod_cast [h, sub_zero], apply fpow_le_one_of_nonpos, { exact_mod_cast le_of_lt hp.one_lt, }, { apply neg_nonpos_of_nonneg, norm_cast, simp, } end lemma eq_of_norm_add_lt_right {p : ℕ} {hp : fact p.prime} {z1 z2 : ℚ_[p]} (h : ∥z1 + z2∥ < ∥z2∥) : ∥z1∥ = ∥z2∥ := by_contradiction $ λ hne, not_lt_of_ge (by rw padic_norm_e.add_eq_max_of_ne hne; apply le_max_right) h lemma eq_of_norm_add_lt_left {p : ℕ} {hp : fact p.prime} {z1 z2 : ℚ_[p]} (h : ∥z1 + z2∥ < ∥z1∥) : ∥z1∥ = ∥z2∥ := by_contradiction $ λ hne, not_lt_of_ge (by rw padic_norm_e.add_eq_max_of_ne hne; apply le_max_left) h end normed_space end padic_norm_e namespace padic variables {p : ℕ} [fact p.prime] set_option eqn_compiler.zeta true instance complete : cau_seq.is_complete ℚ_[p] norm := begin split, intro f, have cau_seq_norm_e : is_cau_seq padic_norm_e f, { intros ε hε, let h := is_cau f ε (by exact_mod_cast hε), unfold norm at h, apply_mod_cast h }, cases padic.complete' ⟨f, cau_seq_norm_e⟩ with q hq, existsi q, intros ε hε, cases exists_rat_btwn hε with ε' hε', norm_cast at hε', cases hq ε' hε'.1 with N hN, existsi N, intros i hi, let h := hN i hi, unfold norm, rw_mod_cast [cau_seq.sub_apply, padic_norm_e.sub_rev], refine lt.trans _ hε'.2, exact_mod_cast hN i hi end lemma padic_norm_e_lim_le {f : cau_seq ℚ_[p] norm} {a : ℝ} (ha : a > 0) (hf : ∀ i, ∥f i∥ ≤ a) : ∥f.lim∥ ≤ a := let ⟨N, hN⟩ := setoid.symm (cau_seq.equiv_lim f) _ ha in calc ∥f.lim∥ = ∥f.lim - f N + f N∥ : by simp ... ≤ max (∥f.lim - f N∥) (∥f N∥) : padic_norm_e.nonarchimedean _ _ ... ≤ a : max_le (le_of_lt (hN _ (le_refl _))) (hf _) end padic
2d04fbf45cb59a6250e74f4c2fffd2c6eb40226e
e0f9ba56b7fedc16ef8697f6caeef5898b435143
/src/group_theory/free_group.lean
48306569c462764505887e9fae285cd88f77173f
[ "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
30,901
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau Free groups as a quotient over the reduction relation `a * x * x⁻¹ * b = a * b`. First we introduce the one step reduction relation `free_group.red.step`: w * x * x⁻¹ * v ~> w * v its reflexive transitive closure: `free_group.red.trans` and proof that its join is an equivalence relation. Then we introduce `free_group α` as a quotient over `free_group.red.step`. -/ import data.fintype.basic import group_theory.subgroup open relation universes u v w variables {α : Type u} local attribute [simp] list.append_eq_has_append namespace free_group variables {L L₁ L₂ L₃ L₄ : list (α × bool)} /-- Reduction step: `w * x * x⁻¹ * v ~> w * v` -/ inductive red.step : list (α × bool) → list (α × bool) → Prop | bnot {L₁ L₂ x b} : red.step (L₁ ++ (x, b) :: (x, bnot b) :: L₂) (L₁ ++ L₂) attribute [simp] red.step.bnot /-- Reflexive-transitive closure of red.step -/ def red : list (α × bool) → list (α × bool) → Prop := refl_trans_gen red.step @[refl] lemma red.refl : red L L := refl_trans_gen.refl @[trans] lemma red.trans : red L₁ L₂ → red L₂ L₃ → red L₁ L₃ := refl_trans_gen.trans namespace red /-- Predicate asserting that word `w₁` can be reduced to `w₂` in one step, i.e. there are words `w₃ w₄` and letter `x` such that `w₁ = w₃xx⁻¹w₄` and `w₂ = w₃w₄` -/ theorem step.length : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.length + 2 = L₁.length | _ _ (@red.step.bnot _ L1 L2 x b) := by rw [list.length_append, list.length_append]; refl @[simp] lemma step.bnot_rev {x b} : step (L₁ ++ (x, bnot b) :: (x, b) :: L₂) (L₁ ++ L₂) := by cases b; from step.bnot @[simp] lemma step.cons_bnot {x b} : red.step ((x, b) :: (x, bnot b) :: L) L := @step.bnot _ [] _ _ _ @[simp] lemma step.cons_bnot_rev {x b} : red.step ((x, bnot b) :: (x, b) :: L) L := @red.step.bnot_rev _ [] _ _ _ theorem step.append_left : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₂ L₃ → step (L₁ ++ L₂) (L₁ ++ L₃) | _ _ _ red.step.bnot := by rw [← list.append_assoc, ← list.append_assoc]; constructor theorem step.cons {x} (H : red.step L₁ L₂) : red.step (x :: L₁) (x :: L₂) := @step.append_left _ [x] _ _ H theorem step.append_right : ∀ {L₁ L₂ L₃ : list (α × bool)}, step L₁ L₂ → step (L₁ ++ L₃) (L₂ ++ L₃) | _ _ _ red.step.bnot := by simp lemma not_step_nil : ¬ step [] L := begin generalize h' : [] = L', assume h, cases h with L₁ L₂, simp [list.nil_eq_append_iff] at h', contradiction end lemma step.cons_left_iff {a : α} {b : bool} : step ((a, b) :: L₁) L₂ ↔ (∃L, step L₁ L ∧ L₂ = (a, b) :: L) ∨ (L₁ = (a, bnot b)::L₂) := begin split, { generalize hL : ((a, b) :: L₁ : list _) = L, assume h, rcases h with ⟨_ | ⟨p, s'⟩, e, a', b'⟩, { simp at hL, simp [*] }, { simp at hL, rcases hL with ⟨rfl, rfl⟩, refine or.inl ⟨s' ++ e, step.bnot, _⟩, simp } }, { assume h, rcases h with ⟨L, h, rfl⟩ | rfl, { exact step.cons h }, { exact step.cons_bnot } } end lemma not_step_singleton : ∀ {p : α × bool}, ¬ step [p] L | (a, b) := by simp [step.cons_left_iff, not_step_nil] lemma step.cons_cons_iff : ∀{p : α × bool}, step (p :: L₁) (p :: L₂) ↔ step L₁ L₂ := by simp [step.cons_left_iff, iff_def, or_imp_distrib] {contextual := tt} lemma step.append_left_iff : ∀L, step (L ++ L₁) (L ++ L₂) ↔ step L₁ L₂ | [] := by simp | (p :: l) := by simp [step.append_left_iff l, step.cons_cons_iff] private theorem step.diamond_aux : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)} {x1 b1 x2 b2}, L₁ ++ (x1, b1) :: (x1, bnot b1) :: L₂ = L₃ ++ (x2, b2) :: (x2, bnot b2) :: L₄ → L₁ ++ L₂ = L₃ ++ L₄ ∨ ∃ L₅, red.step (L₁ ++ L₂) L₅ ∧ red.step (L₃ ++ L₄) L₅ | [] _ [] _ _ _ _ _ H := by injections; subst_vars; simp | [] _ [(x3,b3)] _ _ _ _ _ H := by injections; subst_vars; simp | [(x3,b3)] _ [] _ _ _ _ _ H := by injections; subst_vars; simp | [] _ ((x3,b3)::(x4,b4)::tl) _ _ _ _ _ H := by injections; subst_vars; simp; right; exact ⟨_, red.step.bnot, red.step.cons_bnot⟩ | ((x3,b3)::(x4,b4)::tl) _ [] _ _ _ _ _ H := by injections; subst_vars; simp; right; exact ⟨_, red.step.cons_bnot, red.step.bnot⟩ | ((x3,b3)::tl) _ ((x4,b4)::tl2) _ _ _ _ _ H := let ⟨H1, H2⟩ := list.cons.inj H in match step.diamond_aux H2 with | or.inl H3 := or.inl $ by simp [H1, H3] | or.inr ⟨L₅, H3, H4⟩ := or.inr ⟨_, step.cons H3, by simpa [H1] using step.cons H4⟩ end theorem step.diamond : ∀ {L₁ L₂ L₃ L₄ : list (α × bool)}, red.step L₁ L₃ → red.step L₂ L₄ → L₁ = L₂ → L₃ = L₄ ∨ ∃ L₅, red.step L₃ L₅ ∧ red.step L₄ L₅ | _ _ _ _ red.step.bnot red.step.bnot H := step.diamond_aux H lemma step.to_red : step L₁ L₂ → red L₁ L₂ := refl_trans_gen.single /-- Church-Rosser theorem for word reduction: If `w1 w2 w3` are words such that `w1` reduces to `w2` and `w3` respectively, then there is a word `w4` such that `w2` and `w3` reduce to `w4` respectively. -/ theorem church_rosser : red L₁ L₂ → red L₁ L₃ → join red L₂ L₃ := relation.church_rosser (assume a b c hab hac, match b, c, red.step.diamond hab hac rfl with | b, _, or.inl rfl := ⟨b, by refl, by refl⟩ | b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, hcd.to_red⟩ end) lemma cons_cons {p} : red L₁ L₂ → red (p :: L₁) (p :: L₂) := refl_trans_gen_lift (list.cons p) (assume a b, step.cons) lemma cons_cons_iff (p) : red (p :: L₁) (p :: L₂) ↔ red L₁ L₂ := iff.intro begin generalize eq₁ : (p :: L₁ : list _) = LL₁, generalize eq₂ : (p :: L₂ : list _) = LL₂, assume h, induction h using relation.refl_trans_gen.head_induction_on with L₁ L₂ h₁₂ h ih generalizing L₁ L₂, { subst_vars, cases eq₂, constructor }, { subst_vars, cases p with a b, rw [step.cons_left_iff] at h₁₂, rcases h₁₂ with ⟨L, h₁₂, rfl⟩ | rfl, { exact (ih rfl rfl).head h₁₂ }, { exact (cons_cons h).tail step.cons_bnot_rev } } end cons_cons lemma append_append_left_iff : ∀L, red (L ++ L₁) (L ++ L₂) ↔ red L₁ L₂ | [] := iff.rfl | (p :: L) := by simp [append_append_left_iff L, cons_cons_iff] lemma append_append (h₁ : red L₁ L₃) (h₂ : red L₂ L₄) : red (L₁ ++ L₂) (L₃ ++ L₄) := (refl_trans_gen_lift (λL, L ++ L₂) (assume a b, step.append_right) h₁).trans ((append_append_left_iff _).2 h₂) lemma to_append_iff : red L (L₁ ++ L₂) ↔ (∃L₃ L₄, L = L₃ ++ L₄ ∧ red L₃ L₁ ∧ red L₄ L₂) := iff.intro begin generalize eq : L₁ ++ L₂ = L₁₂, assume h, induction h with L' L₁₂ hLL' h ih generalizing L₁ L₂, { exact ⟨_, _, eq.symm, by refl, by refl⟩ }, { cases h with s e a b, rcases list.append_eq_append_iff.1 eq with ⟨s', rfl, rfl⟩ | ⟨e', rfl, rfl⟩, { have : L₁ ++ (s' ++ ((a, b) :: (a, bnot b) :: e)) = (L₁ ++ s') ++ ((a, b) :: (a, bnot b) :: e), { simp }, rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩, exact ⟨w₁, w₂, rfl, h₁, h₂.tail step.bnot⟩ }, { have : (s ++ ((a, b) :: (a, bnot b) :: e')) ++ L₂ = s ++ ((a, b) :: (a, bnot b) :: (e' ++ L₂)), { simp }, rcases ih this with ⟨w₁, w₂, rfl, h₁, h₂⟩, exact ⟨w₁, w₂, rfl, h₁.tail step.bnot, h₂⟩ }, } end (assume ⟨L₃, L₄, eq, h₃, h₄⟩, eq.symm ▸ append_append h₃ h₄) /-- The empty word `[]` only reduces to itself. -/ theorem nil_iff : red [] L ↔ L = [] := refl_trans_gen_iff_eq (assume l, red.not_step_nil) /-- A letter only reduces to itself. -/ theorem singleton_iff {x} : red [x] L₁ ↔ L₁ = [x] := refl_trans_gen_iff_eq (assume l, not_step_singleton) /-- If `x` is a letter and `w` is a word such that `xw` reduces to the empty word, then `w` reduces to `x⁻¹` -/ theorem cons_nil_iff_singleton {x b} : red ((x, b) :: L) [] ↔ red L [(x, bnot b)] := iff.intro (assume h, have h₁ : red ((x, bnot b) :: (x, b) :: L) [(x, bnot b)], from cons_cons h, have h₂ : red ((x, bnot b) :: (x, b) :: L) L, from refl_trans_gen.single step.cons_bnot_rev, let ⟨L', h₁, h₂⟩ := church_rosser h₁ h₂ in by rw [singleton_iff] at h₁; subst L'; assumption) (assume h, (cons_cons h).tail step.cons_bnot) theorem red_iff_irreducible {x1 b1 x2 b2} (h : (x1, b1) ≠ (x2, b2)) : red [(x1, bnot b1), (x2, b2)] L ↔ L = [(x1, bnot b1), (x2, b2)] := begin apply refl_trans_gen_iff_eq, generalize eq : [(x1, bnot b1), (x2, b2)] = L', assume L h', cases h', simp [list.cons_eq_append_iff, list.nil_eq_append_iff] at eq, rcases eq with ⟨rfl, ⟨rfl, rfl⟩, ⟨rfl, rfl⟩, rfl⟩, subst_vars, simp at h, contradiction end /-- If `x` and `y` are distinct letters and `w₁ w₂` are words such that `xw₁` reduces to `yw₂`, then `w₁` reduces to `x⁻¹yw₂`. -/ theorem inv_of_red_of_ne {x1 b1 x2 b2} (H1 : (x1, b1) ≠ (x2, b2)) (H2 : red ((x1, b1) :: L₁) ((x2, b2) :: L₂)) : red L₁ ((x1, bnot b1) :: (x2, b2) :: L₂) := begin have : red ((x1, b1) :: L₁) ([(x2, b2)] ++ L₂), from H2, rcases to_append_iff.1 this with ⟨_ | ⟨p, L₃⟩, L₄, eq, h₁, h₂⟩, { simp [nil_iff] at h₁, contradiction }, { cases eq, show red (L₃ ++ L₄) ([(x1, bnot b1), (x2, b2)] ++ L₂), apply append_append _ h₂, have h₁ : red ((x1, bnot b1) :: (x1, b1) :: L₃) [(x1, bnot b1), (x2, b2)], { exact cons_cons h₁ }, have h₂ : red ((x1, bnot b1) :: (x1, b1) :: L₃) L₃, { exact step.cons_bnot_rev.to_red }, rcases church_rosser h₁ h₂ with ⟨L', h₁, h₂⟩, rw [red_iff_irreducible H1] at h₁, rwa [h₁] at h₂ } end theorem step.sublist (H : red.step L₁ L₂) : L₂ <+ L₁ := by cases H; simp; constructor; constructor; refl /-- If `w₁ w₂` are words such that `w₁` reduces to `w₂`, then `w₂` is a sublist of `w₁`. -/ theorem sublist : red L₁ L₂ → L₂ <+ L₁ := refl_trans_gen_of_transitive_reflexive (λl, list.sublist.refl l) (λa b c hab hbc, list.sublist.trans hbc hab) (λa b, red.step.sublist) theorem sizeof_of_step : ∀ {L₁ L₂ : list (α × bool)}, step L₁ L₂ → L₂.sizeof < L₁.sizeof | _ _ (@step.bnot _ L1 L2 x b) := begin induction L1 with hd tl ih, case list.nil { dsimp [list.sizeof], have H : 1 + sizeof (x, b) + (1 + sizeof (x, bnot b) + list.sizeof L2) = (list.sizeof L2 + 1) + (sizeof (x, b) + sizeof (x, bnot b) + 1), { ac_refl }, rw H, exact nat.le_add_right _ _ }, case list.cons { dsimp [list.sizeof], exact nat.add_lt_add_left ih _ } end theorem length (h : red L₁ L₂) : ∃ n, L₁.length = L₂.length + 2 * n := begin induction h with L₂ L₃ h₁₂ h₂₃ ih, { exact ⟨0, rfl⟩ }, { rcases ih with ⟨n, eq⟩, existsi (1 + n), simp [mul_add, eq, (step.length h₂₃).symm] } end theorem antisymm (h₁₂ : red L₁ L₂) : red L₂ L₁ → L₁ = L₂ := match L₁, h₁₂.cases_head with | _, or.inl rfl := assume h, rfl | L₁, or.inr ⟨L₃, h₁₃, h₃₂⟩ := assume h₂₁, let ⟨n, eq⟩ := length (h₃₂.trans h₂₁) in have list.length L₃ + 0 = list.length L₃ + (2 * n + 2), by simpa [(step.length h₁₃).symm, add_comm, add_assoc] using eq, (nat.no_confusion $ nat.add_left_cancel this) end end red theorem equivalence_join_red : equivalence (join (@red α)) := equivalence_join_refl_trans_gen $ assume a b c hab hac, (match b, c, red.step.diamond hab hac rfl with | b, _, or.inl rfl := ⟨b, by refl, by refl⟩ | b, c, or.inr ⟨d, hbd, hcd⟩ := ⟨d, refl_gen.single hbd, refl_trans_gen.single hcd⟩ end) theorem join_red_of_step (h : red.step L₁ L₂) : join red L₁ L₂ := join_of_single reflexive_refl_trans_gen h.to_red theorem eqv_gen_step_iff_join_red : eqv_gen red.step L₁ L₂ ↔ join red L₁ L₂ := iff.intro (assume h, have eqv_gen (join red) L₁ L₂ := eqv_gen_mono (assume a b, join_red_of_step) h, (eqv_gen_iff_of_equivalence $ equivalence_join_red).1 this) (join_of_equivalence (eqv_gen.is_equivalence _) $ assume a b, refl_trans_gen_of_equivalence (eqv_gen.is_equivalence _) eqv_gen.rel) end free_group /-- The free group over a type, i.e. the words formed by the elements of the type and their formal inverses, quotient by one step reduction. -/ def free_group (α : Type u) : Type u := quot $ @free_group.red.step α namespace free_group variables {α} {L L₁ L₂ L₃ L₄ : list (α × bool)} def mk (L) : free_group α := quot.mk red.step L @[simp] lemma quot_mk_eq_mk : quot.mk red.step L = mk L := rfl @[simp] lemma quot_lift_mk (β : Type v) (f : list (α × bool) → β) (H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) : quot.lift f H (mk L) = f L := rfl @[simp] lemma quot_lift_on_mk (β : Type v) (f : list (α × bool) → β) (H : ∀ L₁ L₂, red.step L₁ L₂ → f L₁ = f L₂) : quot.lift_on (mk L) f H = f L := rfl instance : has_one (free_group α) := ⟨mk []⟩ lemma one_eq_mk : (1 : free_group α) = mk [] := rfl instance : inhabited (free_group α) := ⟨1⟩ instance : has_mul (free_group α) := ⟨λ x y, quot.lift_on x (λ L₁, quot.lift_on y (λ L₂, mk $ L₁ ++ L₂) (λ L₂ L₃ H, quot.sound $ red.step.append_left H)) (λ L₁ L₂ H, quot.induction_on y $ λ L₃, quot.sound $ red.step.append_right H)⟩ @[simp] lemma mul_mk : mk L₁ * mk L₂ = mk (L₁ ++ L₂) := rfl instance : has_inv (free_group α) := ⟨λx, quot.lift_on x (λ L, mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse) (assume a b h, quot.sound $ by cases h; simp)⟩ @[simp] lemma inv_mk : (mk L)⁻¹ = mk (L.map $ λ x : α × bool, (x.1, bnot x.2)).reverse := rfl instance : group (free_group α) := { mul := (*), one := 1, inv := has_inv.inv, mul_assoc := by rintros ⟨L₁⟩ ⟨L₂⟩ ⟨L₃⟩; simp, one_mul := by rintros ⟨L⟩; refl, mul_one := by rintros ⟨L⟩; simp [one_eq_mk], mul_left_inv := by rintros ⟨L⟩; exact (list.rec_on L rfl $ λ ⟨x, b⟩ tl ih, eq.trans (quot.sound $ by simp [one_eq_mk]) ih) } /-- `of x` is the canonical injection from the type to the free group over that type by sending each element to the equivalence class of the letter that is the element. -/ def of (x : α) : free_group α := mk [(x, tt)] theorem red.exact : mk L₁ = mk L₂ ↔ join red L₁ L₂ := calc (mk L₁ = mk L₂) ↔ eqv_gen red.step L₁ L₂ : iff.intro (quot.exact _) quot.eqv_gen_sound ... ↔ join red L₁ L₂ : eqv_gen_step_iff_join_red /-- The canonical injection from the type to the free group is an injection. -/ theorem of.inj {x y : α} (H : of x = of y) : x = y := let ⟨L₁, hx, hy⟩ := red.exact.1 H in by simp [red.singleton_iff] at hx hy; cc section to_group variables {β : Type v} [group β] (f : α → β) {x y : free_group α} def to_group.aux : list (α × bool) → β := λ L, list.prod $ L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹ theorem red.step.to_group {f : α → β} (H : red.step L₁ L₂) : to_group.aux f L₁ = to_group.aux f L₂ := by cases H with _ _ _ b; cases b; simp [to_group.aux] /-- If `β` is a group, then any function from `α` to `β` extends uniquely to a group homomorphism from the free group over `α` to `β` -/ def to_group : free_group α → β := quot.lift (to_group.aux f) $ λ L₁ L₂ H, red.step.to_group H variable {f} @[simp] lemma to_group.mk : to_group f (mk L) = list.prod (L.map $ λ x, cond x.2 (f x.1) (f x.1)⁻¹) := rfl @[simp] lemma to_group.of {x} : to_group f (of x) = f x := one_mul _ instance to_group.is_group_hom : is_group_hom (to_group f) := { map_mul := by rintros ⟨L₁⟩ ⟨L₂⟩; simp } @[simp] lemma to_group.mul : to_group f (x * y) = to_group f x * to_group f y := is_mul_hom.map_mul _ _ _ @[simp] lemma to_group.one : to_group f 1 = 1 := is_group_hom.map_one _ @[simp] lemma to_group.inv : to_group f x⁻¹ = (to_group f x)⁻¹ := is_group_hom.map_inv _ _ theorem to_group.unique (g : free_group α → β) [is_group_hom g] (hg : ∀ x, g (of x) = f x) : ∀{x}, g x = to_group f x := by rintros ⟨L⟩; exact list.rec_on L (is_group_hom.map_one g) (λ ⟨x, b⟩ t (ih : g (mk t) = _), bool.rec_on b (show g ((of x)⁻¹ * mk t) = to_group f (mk ((x, ff) :: t)), by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih, to_group, to_group.aux]) (show g (of x * mk t) = to_group f (mk ((x, tt) :: t)), by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih, to_group, to_group.aux])) theorem to_group.of_eq (x : free_group α) : to_group of x = x := eq.symm $ to_group.unique id (λ x, rfl) theorem to_group.range_subset {s : set β} [is_subgroup s] (H : set.range f ⊆ s) : set.range (to_group f) ⊆ s := by rintros _ ⟨⟨L⟩, rfl⟩; exact list.rec_on L is_submonoid.one_mem (λ ⟨x, b⟩ tl ih, bool.rec_on b (by simp at ih ⊢; from is_submonoid.mul_mem (is_subgroup.inv_mem $ H ⟨x, rfl⟩) ih) (by simp at ih ⊢; from is_submonoid.mul_mem (H ⟨x, rfl⟩) ih)) theorem to_group.range_eq_closure : set.range (to_group f) = group.closure (set.range f) := set.subset.antisymm (to_group.range_subset group.subset_closure) (group.closure_subset $ λ y ⟨x, hx⟩, ⟨of x, by simpa⟩) end to_group section map variables {β : Type v} (f : α → β) {x y : free_group α} def map.aux (L : list (α × bool)) : list (β × bool) := L.map $ λ x, (f x.1, x.2) /-- Any function from `α` to `β` extends uniquely to a group homomorphism from the free group ver `α` to the free group over `β`. -/ def map (x : free_group α) : free_group β := x.lift_on (λ L, mk $ map.aux f L) $ λ L₁ L₂ H, quot.sound $ by cases H; simp [map.aux] instance map.is_group_hom : is_group_hom (map f) := { map_mul := by rintros ⟨L₁⟩ ⟨L₂⟩; simp [map, map.aux] } variable {f} @[simp] lemma map.mk : map f (mk L) = mk (L.map (λ x, (f x.1, x.2))) := rfl @[simp] lemma map.id : map id x = x := have H1 : (λ (x : α × bool), x) = id := rfl, by rcases x with ⟨L⟩; simp [H1] @[simp] lemma map.id' : map (λ z, z) x = x := map.id theorem map.comp {γ : Type w} {f : α → β} {g : β → γ} {x} : map g (map f x) = map (g ∘ f) x := by rcases x with ⟨L⟩; simp @[simp] lemma map.of {x} : map f (of x) = of (f x) := rfl @[simp] lemma map.mul : map f (x * y) = map f x * map f y := is_mul_hom.map_mul _ x y @[simp] lemma map.one : map f 1 = 1 := is_group_hom.map_one _ @[simp] lemma map.inv : map f x⁻¹ = (map f x)⁻¹ := is_group_hom.map_inv _ x theorem map.unique (g : free_group α → free_group β) [is_group_hom g] (hg : ∀ x, g (of x) = of (f x)) : ∀{x}, g x = map f x := by rintros ⟨L⟩; exact list.rec_on L (is_group_hom.map_one g) (λ ⟨x, b⟩ t (ih : g (mk t) = map f (mk t)), bool.rec_on b (show g ((of x)⁻¹ * mk t) = map f ((of x)⁻¹ * mk t), by simp [is_mul_hom.map_mul g, is_group_hom.map_inv g, hg, ih]) (show g (of x * mk t) = map f (of x * mk t), by simp [is_mul_hom.map_mul g, hg, ih])) /-- Equivalent types give rise to equivalent free groups. -/ def free_group_congr {α β} (e : α ≃ β) : free_group α ≃ free_group β := ⟨map e, map e.symm, λ x, by simp [function.comp, map.comp], λ x, by simp [function.comp, map.comp]⟩ theorem map_eq_to_group : map f x = to_group (of ∘ f) x := eq.symm $ map.unique _ $ λ x, by simp end map section prod variables [group α] (x y : free_group α) /-- If `α` is a group, then any function from `α` to `α` extends uniquely to a homomorphism from the free group over `α` to `α`. This is the multiplicative version of `sum`. -/ def prod : α := to_group id x variables {x y} @[simp] lemma prod_mk : prod (mk L) = list.prod (L.map $ λ x, cond x.2 x.1 x.1⁻¹) := rfl @[simp] lemma prod.of {x : α} : prod (of x) = x := to_group.of instance prod.is_group_hom : is_group_hom (@prod α _) := to_group.is_group_hom @[simp] lemma prod.mul : prod (x * y) = prod x * prod y := to_group.mul @[simp] lemma prod.one : prod (1:free_group α) = 1 := to_group.one @[simp] lemma prod.inv : prod x⁻¹ = (prod x)⁻¹ := to_group.inv lemma prod.unique (g : free_group α → α) [is_group_hom g] (hg : ∀ x, g (of x) = x) {x} : g x = prod x := to_group.unique g hg end prod theorem to_group_eq_prod_map {β : Type v} [group β] {f : α → β} {x} : to_group f x = prod (map f x) := have is_group_hom (prod ∘ map f) := is_group_hom.comp _ _, by exactI (eq.symm $ to_group.unique (prod ∘ map f) $ λ _, by simp) section sum variables [add_group α] (x y : free_group α) /-- If `α` is a group, then any function from `α` to `α` extends uniquely to a homomorphism from the free group over `α` to `α`. This is the additive version of `prod`. -/ def sum : α := @prod (multiplicative _) _ x variables {x y} @[simp] lemma sum_mk : sum (mk L) = list.sum (L.map $ λ x, cond x.2 x.1 (-x.1)) := rfl @[simp] lemma sum.of {x : α} : sum (of x) = x := prod.of instance sum.is_group_hom : is_group_hom (@sum α _) := prod.is_group_hom @[simp] lemma sum.mul : sum (x * y) = sum x + sum y := prod.mul @[simp] lemma sum.one : sum (1:free_group α) = 0 := prod.one @[simp] lemma sum.inv : sum x⁻¹ = -sum x := prod.inv end sum def free_group_empty_equiv_unit : free_group empty ≃ unit := { to_fun := λ _, (), inv_fun := λ _, 1, left_inv := by rintros ⟨_ | ⟨⟨⟨⟩, _⟩, _⟩⟩; refl, right_inv := λ ⟨⟩, rfl } def free_group_unit_equiv_int : free_group unit ≃ int := { to_fun := λ x, sum $ map (λ _, 1) x, inv_fun := λ x, of () ^ x, left_inv := by rintros ⟨L⟩; exact list.rec_on L rfl (λ ⟨⟨⟩, b⟩ tl ih, by cases b; simp [gpow_add] at ih ⊢; rw ih; refl), right_inv := λ x, int.induction_on x (by simp) (λ i ih, by simp at ih; simp [gpow_add, ih]) (λ i ih, by simp at ih; simp [gpow_add, ih, sub_eq_add_neg]) } section category variables {β : Type u} instance : monad free_group.{u} := { pure := λ α, of, map := λ α β, map, bind := λ α β x f, to_group f x } @[elab_as_eliminator] protected theorem induction_on {C : free_group α → Prop} (z : free_group α) (C1 : C 1) (Cp : ∀ x, C $ pure x) (Ci : ∀ x, C (pure x) → C (pure x)⁻¹) (Cm : ∀ x y, C x → C y → C (x * y)) : C z := quot.induction_on z $ λ L, list.rec_on L C1 $ λ ⟨x, b⟩ tl ih, bool.rec_on b (Cm _ _ (Ci _ $ Cp x) ih) (Cm _ _ (Cp x) ih) @[simp] lemma map_pure (f : α → β) (x : α) : f <$> (pure x : free_group α) = pure (f x) := map.of @[simp] lemma map_one (f : α → β) : f <$> (1 : free_group α) = 1 := map.one @[simp] lemma map_mul (f : α → β) (x y : free_group α) : f <$> (x * y) = f <$> x * f <$> y := map.mul @[simp] lemma map_inv (f : α → β) (x : free_group α) : f <$> (x⁻¹) = (f <$> x)⁻¹ := map.inv @[simp] lemma pure_bind (f : α → free_group β) (x) : pure x >>= f = f x := to_group.of @[simp] lemma one_bind (f : α → free_group β) : 1 >>= f = 1 := @@to_group.one _ f @[simp] lemma mul_bind (f : α → free_group β) (x y : free_group α) : x * y >>= f = (x >>= f) * (y >>= f) := to_group.mul @[simp] lemma inv_bind (f : α → free_group β) (x : free_group α) : x⁻¹ >>= f = (x >>= f)⁻¹ := to_group.inv instance : is_lawful_monad free_group.{u} := { id_map := λ α x, free_group.induction_on x (map_one id) (λ x, map_pure id x) (λ x ih, by rw [map_inv, ih]) (λ x y ihx ihy, by rw [map_mul, ihx, ihy]), pure_bind := λ α β x f, pure_bind f x, bind_assoc := λ α β γ x f g, free_group.induction_on x (by iterate 3 { rw one_bind }) (λ x, by iterate 2 { rw pure_bind }) (λ x ih, by iterate 3 { rw inv_bind }; rw ih) (λ x y ihx ihy, by iterate 3 { rw mul_bind }; rw [ihx, ihy]), bind_pure_comp_eq_map := λ α β f x, free_group.induction_on x (by rw [one_bind, map_one]) (λ x, by rw [pure_bind, map_pure]) (λ x ih, by rw [inv_bind, map_inv, ih]) (λ x y ihx ihy, by rw [mul_bind, map_mul, ihx, ihy]) } end category section reduce variable [decidable_eq α] /-- The maximal reduction of a word. It is computable iff `α` has decidable equality. -/ def reduce (L : list (α × bool)) : list (α × bool) := list.rec_on L [] $ λ hd1 tl1 ih, list.cases_on ih [hd1] $ λ hd2 tl2, if hd1.1 = hd2.1 ∧ hd1.2 = bnot hd2.2 then tl2 else hd1 :: hd2 :: tl2 @[simp] lemma reduce.cons (x) : reduce (x :: L) = list.cases_on (reduce L) [x] (λ hd tl, if x.1 = hd.1 ∧ x.2 = bnot hd.2 then tl else x :: hd :: tl) := rfl /-- The first theorem that characterises the function `reduce`: a word reduces to its maximal reduction. -/ theorem reduce.red : red L (reduce L) := begin induction L with hd1 tl1 ih, case list.nil { constructor }, case list.cons { dsimp, revert ih, generalize htl : reduce tl1 = TL, intro ih, cases TL with hd2 tl2, case list.nil { exact red.cons_cons ih }, case list.cons { dsimp, by_cases h : hd1.fst = hd2.fst ∧ hd1.snd = bnot (hd2.snd), { rw [if_pos h], transitivity, { exact red.cons_cons ih }, { cases hd1, cases hd2, cases h, dsimp at *, subst_vars, exact red.step.cons_bnot_rev.to_red } }, { rw [if_neg h], exact red.cons_cons ih } } } end theorem reduce.not {p : Prop} : ∀ {L₁ L₂ L₃ : list (α × bool)} {x b}, reduce L₁ = L₂ ++ (x, b) :: (x, bnot b) :: L₃ → p | [] L2 L3 _ _ := λ h, by cases L2; injections | ((x,b)::L1) L2 L3 x' b' := begin dsimp, cases r : reduce L1, { dsimp, intro h, have := congr_arg list.length h, simp [-add_comm] at this, exact absurd this dec_trivial }, cases hd with y c, by_cases x = y ∧ b = bnot c; simp [h]; intro H, { rw H at r, exact @reduce.not L1 ((y,c)::L2) L3 x' b' r }, rcases L2 with _|⟨a, L2⟩, { injections, subst_vars, simp at h, cc }, { refine @reduce.not L1 L2 L3 x' b' _, injection H with _ H, rw [r, H], refl } end /-- The second theorem that characterises the function `reduce`: the maximal reduction of a word only reduces to itself. -/ theorem reduce.min (H : red (reduce L₁) L₂) : reduce L₁ = L₂ := begin induction H with L1 L' L2 H1 H2 ih, { refl }, { cases H1 with L4 L5 x b, exact reduce.not H2 } end /-- `reduce` is idempotent, i.e. the maximal reduction of the maximal reduction of a word is the maximal reduction of the word. -/ theorem reduce.idem : reduce (reduce L) = reduce L := eq.symm $ reduce.min reduce.red theorem reduce.step.eq (H : red.step L₁ L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (reduce.red.head H) in (reduce.min HR13).trans (reduce.min HR23).symm /-- If a word reduces to another word, then they have a common maximal reduction. -/ theorem reduce.eq_of_red (H : red L₁ L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, HR13, HR23⟩ := red.church_rosser reduce.red (red.trans H reduce.red) in (reduce.min HR13).trans (reduce.min HR23).symm /-- If two words correspond to the same element in the free group, then they have a common maximal reduction. This is the proof that the function that sends an element of the free group to its maximal reduction is well-defined. -/ theorem reduce.sound (H : mk L₁ = mk L₂) : reduce L₁ = reduce L₂ := let ⟨L₃, H13, H23⟩ := red.exact.1 H in (reduce.eq_of_red H13).trans (reduce.eq_of_red H23).symm /-- If two words have a common maximal reduction, then they correspond to the same element in the free group. -/ theorem reduce.exact (H : reduce L₁ = reduce L₂) : mk L₁ = mk L₂ := red.exact.2 ⟨reduce L₂, H ▸ reduce.red, reduce.red⟩ /-- A word and its maximal reduction correspond to the same element of the free group. -/ theorem reduce.self : mk (reduce L) = mk L := reduce.exact reduce.idem /-- If words `w₁ w₂` are such that `w₁` reduces to `w₂`, then `w₂` reduces to the maximal reduction of `w₁`. -/ theorem reduce.rev (H : red L₁ L₂) : red L₂ (reduce L₁) := (reduce.eq_of_red H).symm ▸ reduce.red /-- The function that sends an element of the free group to its maximal reduction. -/ def to_word : free_group α → list (α × bool) := quot.lift reduce $ λ L₁ L₂ H, reduce.step.eq H lemma to_word.mk : ∀{x : free_group α}, mk (to_word x) = x := by rintros ⟨L⟩; exact reduce.self lemma to_word.inj : ∀(x y : free_group α), to_word x = to_word y → x = y := by rintros ⟨L₁⟩ ⟨L₂⟩; exact reduce.exact /-- Constructive Church-Rosser theorem (compare `church_rosser`). -/ def reduce.church_rosser (H12 : red L₁ L₂) (H13 : red L₁ L₃) : { L₄ // red L₂ L₄ ∧ red L₃ L₄ } := ⟨reduce L₁, reduce.rev H12, reduce.rev H13⟩ instance : decidable_eq (free_group α) := function.injective.decidable_eq to_word.inj instance red.decidable_rel : decidable_rel (@red α) | [] [] := is_true red.refl | [] (hd2::tl2) := is_false $ λ H, list.no_confusion (red.nil_iff.1 H) | ((x,b)::tl) [] := match red.decidable_rel tl [(x, bnot b)] with | is_true H := is_true $ red.trans (red.cons_cons H) $ (@red.step.bnot _ [] [] _ _).to_red | is_false H := is_false $ λ H2, H $ red.cons_nil_iff_singleton.1 H2 end | ((x1,b1)::tl1) ((x2,b2)::tl2) := if h : (x1, b1) = (x2, b2) then match red.decidable_rel tl1 tl2 with | is_true H := is_true $ h ▸ red.cons_cons H | is_false H := is_false $ λ H2, H $ h ▸ (red.cons_cons_iff _).1 $ H2 end else match red.decidable_rel tl1 ((x1,bnot b1)::(x2,b2)::tl2) with | is_true H := is_true $ (red.cons_cons H).tail red.step.cons_bnot | is_false H := is_false $ λ H2, H $ red.inv_of_red_of_ne h H2 end /-- A list containing every word that `w₁` reduces to. -/ def red.enum (L₁ : list (α × bool)) : list (list (α × bool)) := list.filter (λ L₂, red L₁ L₂) (list.sublists L₁) theorem red.enum.sound (H : L₂ ∈ red.enum L₁) : red L₁ L₂ := list.of_mem_filter H theorem red.enum.complete (H : red L₁ L₂) : L₂ ∈ red.enum L₁ := list.mem_filter_of_mem (list.mem_sublists.2 $ red.sublist H) H instance : fintype { L₂ // red L₁ L₂ } := fintype.subtype (list.to_finset $ red.enum L₁) $ λ L₂, ⟨λ H, red.enum.sound $ list.mem_to_finset.1 H, λ H, list.mem_to_finset.2 $ red.enum.complete H⟩ end reduce end free_group
18bbeb140297f3a8141c5eeb4ce55c4c718a2e1c
abd85493667895c57a7507870867b28124b3998f
/src/group_theory/submonoid.lean
50ef34c7b166071fd76251b0d0d814fd78527466
[ "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
32,090
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import algebra.big_operators import algebra.free_monoid import algebra.group.prod import data.equiv.mul_add /-! # Submonoids This file defines bundled multiplicative and additive submonoids. We prove submonoids of a monoid form a complete lattice, and results about images and preimages of submonoids under monoid homomorphisms. There are also theorems about the submonoids generated by an element or a subset of a monoid, defined as the infimum of the set of submonoids containing a given element/subset. ## Implementation notes Submonoid inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as membership of a submonoid's underlying set. ## Tags submonoid, submonoids -/ variables {M : Type*} [monoid M] {s : set M} variables {A : Type*} [add_monoid A] {t : set A} /-- A submonoid of a monoid `M` is a subset containing 1 and closed under multiplication. -/ structure submonoid (M : Type*) [monoid M] := (carrier : set M) (one_mem' : (1 : M) ∈ carrier) (mul_mem' {a b} : a ∈ carrier → b ∈ carrier → a * b ∈ carrier) /-- An additive submonoid of an additive monoid `M` is a subset containing 0 and closed under addition. -/ structure add_submonoid (M : Type*) [add_monoid M] := (carrier : set M) (zero_mem' : (0 : M) ∈ carrier) (add_mem' {a b} : a ∈ carrier → b ∈ carrier → a + b ∈ carrier) attribute [to_additive add_submonoid] submonoid /-- Map from submonoids of monoid `M` to `add_submonoid`s of `additive M`. -/ def submonoid.to_add_submonoid {M : Type*} [monoid M] (S : submonoid M) : add_submonoid (additive M) := { carrier := S.carrier, zero_mem' := S.one_mem', add_mem' := S.mul_mem' } /-- Map from `add_submonoid`s of `additive M` to submonoids of `M`. -/ def submonoid.of_add_submonoid {M : Type*} [monoid M] (S : add_submonoid (additive M)) : submonoid M := { carrier := S.carrier, one_mem' := S.zero_mem', mul_mem' := S.add_mem' } /-- Map from `add_submonoid`s of `add_monoid M` to submonoids of `multiplicative M`. -/ def add_submonoid.to_submonoid {M : Type*} [add_monoid M] (S : add_submonoid M) : submonoid (multiplicative M) := { carrier := S.carrier, one_mem' := S.zero_mem', mul_mem' := S.add_mem' } /-- Map from submonoids of `multiplicative M` to `add_submonoid`s of `add_monoid M`. -/ def add_submonoid.of_submonoid {M : Type*} [add_monoid M] (S : submonoid (multiplicative M)) : add_submonoid M := { carrier := S.carrier, zero_mem' := S.one_mem', add_mem' := S.mul_mem' } /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/ def submonoid.add_submonoid_equiv (M : Type*) [monoid M] : submonoid M ≃ add_submonoid (additive M) := { to_fun := submonoid.to_add_submonoid, inv_fun := submonoid.of_add_submonoid, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl } namespace submonoid @[to_additive] instance : has_coe (submonoid M) (set M) := ⟨submonoid.carrier⟩ @[to_additive] instance : has_coe_to_sort (submonoid M) := ⟨Type*, λ S, S.carrier⟩ @[to_additive] instance : has_mem M (submonoid M) := ⟨λ m S, m ∈ (S:set M)⟩ @[simp, to_additive] lemma mem_carrier {s : submonoid M} {x : M} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[simp, norm_cast, to_additive] lemma mem_coe {S : submonoid M} {m : M} : m ∈ (S : set M) ↔ m ∈ S := iff.rfl @[simp, norm_cast, to_additive] lemma coe_coe (s : submonoid M) : ↥(s : set M) = s := rfl attribute [norm_cast] add_submonoid.mem_coe add_submonoid.coe_coe end submonoid @[to_additive] protected lemma submonoid.exists {s : submonoid M} {p : s → Prop} : (∃ x : s, p x) ↔ ∃ x ∈ s, p ⟨x, ‹x ∈ s›⟩ := set_coe.exists @[to_additive] protected lemma submonoid.forall {s : submonoid M} {p : s → Prop} : (∀ x : s, p x) ↔ ∀ x ∈ s, p ⟨x, ‹x ∈ s›⟩ := set_coe.forall namespace submonoid variables (S : submonoid M) /-- Two submonoids are equal if the underlying subsets are equal. -/ @[to_additive "Two `add_submonoid`s are equal if the underlying subsets are equal."] theorem ext' ⦃S T : submonoid M⦄ (h : (S : set M) = T) : S = T := by cases S; cases T; congr' /-- Two submonoids are equal if and only if the underlying subsets are equal. -/ @[to_additive "Two `add_submonoid`s are equal if and only if the underlying subsets are equal."] protected theorem ext'_iff {S T : submonoid M} : S = T ↔ (S : set M) = T := ⟨λ h, h ▸ rfl, λ h, ext' h⟩ /-- Two submonoids are equal if they have the same elements. -/ @[ext, to_additive "Two `add_submonoid`s are equal if they have the same elements."] theorem ext {S T : submonoid M} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h attribute [ext] add_submonoid.ext /-- Copy a submonoid replacing `carrier` with a set that is equal to it. -/ @[to_additive "Copy an additive submonoid replacing `carrier` with a set that is equal to it."] def copy (S : submonoid M) (s : set M) (hs : s = S) : submonoid M := { carrier := s, one_mem' := hs.symm ▸ S.one_mem', mul_mem' := hs.symm ▸ S.mul_mem' } @[simp, to_additive] lemma coe_copy {S : submonoid M} {s : set M} (hs : s = S) : (S.copy s hs : set M) = s := rfl @[to_additive] lemma copy_eq {S : submonoid M} {s : set M} (hs : s = S) : S.copy s hs = S := ext' hs /-- A submonoid contains the monoid's 1. -/ @[to_additive "An `add_submonoid` contains the monoid's 0."] theorem one_mem : (1 : M) ∈ S := S.one_mem' /-- A submonoid is closed under multiplication. -/ @[to_additive "An `add_submonoid` is closed under addition."] theorem mul_mem {x y : M} : x ∈ S → y ∈ S → x * y ∈ S := submonoid.mul_mem' S /-- Product of a list of elements in a submonoid is in the submonoid. -/ @[to_additive "Sum of a list of elements in an `add_submonoid` is in the `add_submonoid`."] lemma list_prod_mem : ∀ {l : list M}, (∀x ∈ l, x ∈ S) → l.prod ∈ S | [] h := S.one_mem | (a::l) h := suffices a * l.prod ∈ S, by rwa [list.prod_cons], have a ∈ S ∧ (∀ x ∈ l, x ∈ S), from list.forall_mem_cons.1 h, S.mul_mem this.1 (list_prod_mem this.2) /-- Product of a multiset of elements in a submonoid of a `comm_monoid` is in the submonoid. -/ @[to_additive "Sum of a multiset of elements in an `add_submonoid` of an `add_comm_monoid` is in the `add_submonoid`."] lemma multiset_prod_mem {M} [comm_monoid M] (S : submonoid M) (m : multiset M) : (∀a ∈ m, a ∈ S) → m.prod ∈ S := begin refine quotient.induction_on m (assume l hl, _), rw [multiset.quot_mk_to_coe, multiset.coe_prod], exact S.list_prod_mem hl end /-- Product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is in the submonoid. -/ @[to_additive "Sum of elements in an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is in the `add_submonoid`."] lemma prod_mem {M : Type*} [comm_monoid M] (S : submonoid M) {ι : Type*} {t : finset ι} {f : ι → M} (h : ∀c ∈ t, f c ∈ S) : t.prod f ∈ S := S.multiset_prod_mem (t.1.map f) $ λ x hx, let ⟨i, hi, hix⟩ := multiset.mem_map.1 hx in hix ▸ h i hi lemma pow_mem {x : M} (hx : x ∈ S) : ∀ n:ℕ, x^n ∈ S | 0 := S.one_mem | (n+1) := S.mul_mem hx (pow_mem n) /-- A submonoid of a monoid inherits a multiplication. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an addition."] instance has_mul : has_mul S := ⟨λ a b, ⟨a.1 * b.1, S.mul_mem a.2 b.2⟩⟩ /-- A submonoid of a monoid inherits a 1. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits a zero."] instance has_one : has_one S := ⟨⟨_, S.one_mem⟩⟩ @[simp, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl @[simp, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl @[simp, to_additive] lemma coe_eq_coe (x y : S) : (x : M) = y ↔ x = y := set_coe.ext_iff attribute [norm_cast] coe_eq_coe coe_mul coe_one attribute [norm_cast] add_submonoid.coe_eq_coe add_submonoid.coe_add add_submonoid.coe_zero /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive to_add_monoid "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."] instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S := by refine { mul := (*), one := 1, .. }; simp [mul_assoc, ← submonoid.coe_eq_coe] /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive to_add_comm_monoid "An `add_submonoid` of an `add_comm_monoid` is an `add_comm_monoid`."] instance to_comm_monoid {M} [comm_monoid M] (S : submonoid M) : comm_monoid S := { mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..S.to_monoid} /-- The natural monoid hom from a submonoid of monoid `M` to `M`. -/ @[to_additive "The natural monoid hom from an `add_submonoid` of `add_monoid` `M` to `M`."] def subtype : S →* M := ⟨coe, rfl, λ _ _, rfl⟩ @[simp, to_additive] theorem coe_subtype : ⇑S.subtype = coe := rfl @[to_additive] instance : has_le (submonoid M) := ⟨λ S T, ∀ ⦃x⦄, x ∈ S → x ∈ T⟩ @[to_additive] lemma le_def {S T : submonoid M} : S ≤ T ↔ ∀ ⦃x : M⦄, x ∈ S → x ∈ T := iff.rfl @[simp, norm_cast, to_additive] lemma coe_subset_coe {S T : submonoid M} : (S : set M) ⊆ T ↔ S ≤ T := iff.rfl @[to_additive] instance : partial_order (submonoid M) := { le := λ S T, ∀ ⦃x⦄, x ∈ S → x ∈ T, .. partial_order.lift (coe : submonoid M → set M) ext' infer_instance } @[simp, norm_cast, to_additive] lemma coe_ssubset_coe {S T : submonoid M} : (S : set M) ⊂ T ↔ S < T := iff.rfl attribute [norm_cast] add_submonoid.coe_subset_coe add_submonoid.coe_ssubset_coe /-- The submonoid `M` of the monoid `M`. -/ @[to_additive "The `add_submonoid M` of the `add_monoid M`."] instance : has_top (submonoid M) := ⟨{ carrier := set.univ, one_mem' := set.mem_univ 1, mul_mem' := λ _ _ _ _, set.mem_univ _ }⟩ /-- The trivial submonoid `{1}` of an monoid `M`. -/ @[to_additive "The trivial `add_submonoid` `{0}` of an `add_monoid` `M`."] instance : has_bot (submonoid M) := ⟨{ carrier := {1}, one_mem' := set.mem_singleton 1, mul_mem' := λ a b ha hb, by { simp only [set.mem_singleton_iff] at *, rw [ha, hb, mul_one] }}⟩ @[to_additive] instance : inhabited (submonoid M) := ⟨⊥⟩ @[simp, to_additive] lemma mem_bot {x : M} : x ∈ (⊥ : submonoid M) ↔ x = 1 := set.mem_singleton_iff @[simp, to_additive] lemma mem_top (x : M) : x ∈ (⊤ : submonoid M) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊤ : submonoid M) : set M) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((⊥ : submonoid M) : set M) = {1} := rfl /-- The inf of two submonoids is their intersection. -/ @[to_additive "The inf of two `add_submonoid`s is their intersection."] instance : has_inf (submonoid M) := ⟨λ S₁ S₂, { carrier := S₁ ∩ S₂, one_mem' := ⟨S₁.one_mem, S₂.one_mem⟩, mul_mem' := λ _ _ ⟨hx, hx'⟩ ⟨hy, hy'⟩, ⟨S₁.mul_mem hx hy, S₂.mul_mem hx' hy'⟩ }⟩ @[simp, to_additive] lemma coe_inf (p p' : submonoid M) : ((p ⊓ p' : submonoid M) : set M) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : submonoid M} {x : M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (submonoid M) := ⟨λ s, { carrier := ⋂ t ∈ s, ↑t, one_mem' := set.mem_bInter $ λ i h, i.one_mem, mul_mem' := λ x y hx hy, set.mem_bInter $ λ i h, i.mul_mem (by apply set.mem_bInter_iff.1 hx i h) (by apply set.mem_bInter_iff.1 hy i h) }⟩ @[simp, to_additive] lemma coe_Inf (S : set (submonoid M)) : ((Inf S : submonoid M) : set M) = ⋂ s ∈ S, ↑s := rfl @[to_additive] lemma mem_Inf {S : set (submonoid M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_bInter_iff @[to_additive] lemma mem_infi {ι : Sort*} {S : ι → submonoid M} {x : M} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, to_additive] lemma coe_infi {ι : Sort*} {S : ι → submonoid M} : (↑(⨅ i, S i) : set M) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] attribute [norm_cast] coe_Inf coe_infi /-- Submonoids of a monoid form a complete lattice. -/ @[to_additive "The `add_submonoid`s of an `add_monoid` form a complete lattice."] instance : complete_lattice (submonoid M) := { le := (≤), lt := (<), bot := (⊥), bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), Inf := has_Inf.Inf, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (submonoid M) $ λ s, is_glb.of_image (λ S T, show (S : set M) ≤ T ↔ S ≤ T, from coe_subset_coe) is_glb_binfi } /-- The `submonoid` generated by a set. -/ @[to_additive "The `add_submonoid` generated by a set"] def closure (s : set M) : submonoid M := Inf {S | s ⊆ S} @[to_additive] lemma mem_closure {x : M} : x ∈ closure s ↔ ∀ S : submonoid M, s ⊆ S → x ∈ S := mem_Inf /-- The submonoid generated by a set includes the set. -/ @[simp, to_additive "The `add_submonoid` generated by a set includes the set."] lemma subset_closure : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx variable {S} open set /-- A submonoid `S` includes `closure s` if and only if it includes `s`. -/ @[simp, to_additive "An additive submonoid `S` includes `closure s` if and only if it includes `s`"] lemma closure_le : closure s ≤ S ↔ s ⊆ S := ⟨subset.trans subset_closure, λ h, Inf_le h⟩ /-- Submonoid closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ @[to_additive "Additive submonoid closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`"] lemma closure_mono ⦃s t : set M⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ subset.trans h subset_closure @[to_additive] lemma closure_eq_of_le (h₁ : s ⊆ S) (h₂ : S ≤ closure s) : closure s = S := le_antisymm (closure_le.2 h₁) h₂ variable (S) /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p` holds for all elements of the closure of `s`. -/ @[to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p` holds for all elements of the additive closure of `s`."] lemma closure_induction {p : M → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, H1, Hmul⟩).2 Hs h attribute [elab_as_eliminator] submonoid.closure_induction add_submonoid.closure_induction variable (M) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure M _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {M} /-- Closure of a submonoid `S` equals `S`. -/ @[simp, to_additive "Additive closure of an additive submonoid `S` equals `S`"] lemma closure_eq : closure (S : set M) = S := (submonoid.gi M).l_u_eq S @[simp, to_additive] lemma closure_empty : closure (∅ : set M) = ⊥ := (submonoid.gi M).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set M) = ⊤ := @coe_top M _ ▸ closure_eq ⊤ @[to_additive] lemma closure_union (s t : set M) : closure (s ∪ t) = closure s ⊔ closure t := (submonoid.gi M).gc.l_sup @[to_additive] lemma closure_Union {ι} (s : ι → set M) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (submonoid.gi M).gc.l_supr @[to_additive] lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) {x : M} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (le_def.1 $ le_supr S i) hi⟩, suffices : x ∈ closure (⋃ i, (S i : set M)) → ∃ i, x ∈ S i, by simpa only [closure_Union, closure_eq (S _)] using this, refine (λ hx, closure_induction hx (λ _, mem_Union.1) _ _), { exact hι.elim (λ i, ⟨i, (S i).one_mem⟩) }, { rintros x y ⟨i, hi⟩ ⟨j, hj⟩, rcases hS i j with ⟨k, hki, hkj⟩, exact ⟨k, (S k).mul_mem (hki hi) (hkj hj)⟩ } end @[to_additive] lemma coe_supr_of_directed {ι} [nonempty ι] {S : ι → submonoid M} (hS : directed (≤) S) : ((⨆ i, S i : submonoid M) : set M) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] @[to_additive] lemma mem_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : M} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, rw [Sup_eq_supr, supr_subtype', mem_supr_of_directed, subtype.exists], exact (directed_on_iff_directed _).1 hS end @[to_additive] lemma coe_Sup_of_directed_on {S : set (submonoid M)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set M) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] variables {N : Type*} [monoid N] {P : Type*} [monoid P] /-- The preimage of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The preimage of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def comap (f : M →* N) (S : submonoid N) : submonoid M := { carrier := (f ⁻¹' S), one_mem' := show f 1 ∈ S, by rw f.map_one; exact S.one_mem, mul_mem' := λ a b ha hb, show f (a * b) ∈ S, by rw f.map_mul; exact S.mul_mem ha hb } @[simp, to_additive] lemma coe_comap (S : submonoid N) (f : M →* N) : (S.comap f : set M) = f ⁻¹' S := rfl @[simp, to_additive] lemma mem_comap {S : submonoid N} {f : M →* N} {x : M} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl @[to_additive] lemma comap_comap (S : submonoid P) (g : N →* P) (f : M →* N) : (S.comap g).comap f = S.comap (g.comp f) := rfl /-- The image of a submonoid along a monoid homomorphism is a submonoid. -/ @[to_additive "The image of an `add_submonoid` along an `add_monoid` homomorphism is an `add_submonoid`."] def map (f : M →* N) (S : submonoid M) : submonoid N := { carrier := (f '' S), one_mem' := ⟨1, S.one_mem, f.map_one⟩, mul_mem' := begin rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, exact ⟨x * y, S.mul_mem hx hy, by rw f.map_mul; refl⟩ end } @[simp, to_additive] lemma coe_map (f : M →* N) (S : submonoid M) : (S.map f : set N) = f '' S := rfl @[simp, to_additive] lemma mem_map {f : M →* N} {S : submonoid M} {y : N} : y ∈ S.map f ↔ ∃ x ∈ S, f x = y := mem_image_iff_bex @[to_additive] lemma map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) := ext' $ image_image _ _ _ @[to_additive] lemma map_le_iff_le_comap {f : M →* N} {S : submonoid M} {T : submonoid N} : S.map f ≤ T ↔ S ≤ T.comap f := image_subset_iff @[to_additive] lemma gc_map_comap (f : M →* N) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap @[to_additive] lemma map_sup (S T : submonoid M) (f : M →* N) : (S ⊔ T).map f = S.map f ⊔ T.map f := (gc_map_comap f).l_sup @[to_additive] lemma map_supr {ι : Sort*} (f : M →* N) (s : ι → submonoid M) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr @[to_additive] lemma comap_inf (S T : submonoid N) (f : M →* N) : (S ⊓ T).comap f = S.comap f ⊓ T.comap f := (gc_map_comap f).u_inf @[to_additive] lemma comap_infi {ι : Sort*} (f : M →* N) (s : ι → submonoid N) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp, to_additive] lemma map_bot (f : M →* N) : (⊥ : submonoid M).map f = ⊥ := (gc_map_comap f).l_bot @[simp, to_additive] lemma comap_top (f : M →* N) : (⊤ : submonoid N).comap f = ⊤ := (gc_map_comap f).u_top @[simp, to_additive] lemma map_id (S : submonoid M) : S.map (monoid_hom.id M) = S := ext (λ x, ⟨λ ⟨_, h, rfl⟩, h, λ h, ⟨_, h, rfl⟩⟩) /-- Given `submonoid`s `s`, `t` of monoids `M`, `N` respectively, `s × t` as a submonoid of `M × N`. -/ @[to_additive prod "Given `add_submonoid`s `s`, `t` of `add_monoid`s `A`, `B` respectively, `s × t` as an `add_submonoid` of `A × B`."] def prod (s : submonoid M) (t : submonoid N) : submonoid (M × N) := { carrier := (s : set M).prod t, one_mem' := ⟨s.one_mem, t.one_mem⟩, mul_mem' := λ p q hp hq, ⟨s.mul_mem hp.1 hq.1, t.mul_mem hp.2 hq.2⟩ } @[to_additive coe_prod] lemma coe_prod (s : submonoid M) (t : submonoid N) : (s.prod t : set (M × N)) = (s : set M).prod (t : set N) := rfl @[to_additive mem_prod] lemma mem_prod {s : submonoid M} {t : submonoid N} {p : M × N} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[to_additive prod_mono] lemma prod_mono : ((≤) ⇒ (≤) ⇒ (≤)) (@prod M _ N _) (@prod M _ N _) := λ s s' hs t t' ht, set.prod_mono hs ht @[to_additive prod_mono_right] lemma prod_mono_right (s : submonoid M) : monotone (λ t : submonoid N, s.prod t) := prod_mono (le_refl s) @[to_additive prod_mono_left] lemma prod_mono_left (t : submonoid N) : monotone (λ s : submonoid M, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) @[to_additive prod_top] lemma prod_top (s : submonoid M) : s.prod (⊤ : submonoid N) = s.comap (monoid_hom.fst M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] @[to_additive top_prod] lemma top_prod (s : submonoid N) : (⊤ : submonoid M).prod s = s.comap (monoid_hom.snd M N) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp, to_additive top_prod_top] lemma top_prod_top : (⊤ : submonoid M).prod (⊤ : submonoid N) = ⊤ := (top_prod _).trans $ comap_top _ @[to_additive] lemma bot_prod_bot : (⊥ : submonoid M).prod (⊥ : submonoid N) = ⊥ := ext' $ by simp [coe_prod, prod.one_eq_mk] /-- Product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prod_equiv "Product of additive submonoids is isomorphic to their product as additive monoids"] def prod_equiv (s : submonoid M) (t : submonoid N) : s.prod t ≃* s × t := { map_mul' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } end submonoid namespace monoid_hom variables {N : Type*} {P : Type*} [monoid N] [monoid P] (S : submonoid M) open submonoid /-- The range of a monoid homomorphism is a submonoid. -/ @[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."] def mrange (f : M →* N) : submonoid N := (⊤ : submonoid M).map f @[simp, to_additive] lemma coe_mrange (f : M →* N) : (f.mrange : set N) = set.range f := set.image_univ @[simp, to_additive] lemma mem_mrange {f : M →* N} {y : N} : y ∈ f.mrange ↔ ∃ x, f x = y := by simp [mrange] @[to_additive] lemma map_mrange (g : N →* P) (f : M →* N) : f.mrange.map g = (g.comp f).mrange := (⊤ : submonoid M).map_map g f /-- Restriction of a monoid hom to a submonoid of the domain. -/ @[to_additive "Restriction of an add_monoid hom to an `add_submonoid` of the domain."] def mrestrict {N : Type*} [monoid N] (f : M →* N) (S : submonoid M) : S →* N := f.comp S.subtype @[simp, to_additive] lemma mrestrict_apply {N : Type*} [monoid N] (f : M →* N) (x : S) : f.mrestrict S x = f x := rfl /-- Restriction of a monoid hom to a submonoid of the codomain. -/ @[to_additive "Restriction of an `add_monoid` hom to an `add_submonoid` of the codomain."] def cod_mrestrict (f : M →* N) (S : submonoid N) (h : ∀ x, f x ∈ S) : M →* S := { to_fun := λ n, ⟨f n, h n⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- Restriction of a monoid hom to its range iterpreted as a submonoid. -/ @[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."] def mrange_restrict {N} [monoid N] (f : M →* N) : M →* f.mrange := f.cod_mrestrict f.mrange $ λ x, ⟨x, submonoid.mem_top x, rfl⟩ @[simp, to_additive] lemma coe_mrange_restrict {N} [monoid N] (f : M →* N) (x : M) : (f.mrange_restrict x : N) = f x := rfl @[to_additive] lemma mrange_top_iff_surjective {N} [monoid N] {f : M →* N} : f.mrange = (⊤ : submonoid N) ↔ function.surjective f := submonoid.ext'_iff.trans $ iff.trans (by rw [coe_mrange, coe_top]) set.range_iff_surjective /-- The range of a surjective monoid hom is the whole of the codomain. -/ @[to_additive "The range of a surjective `add_monoid` hom is the whole of the codomain."] lemma mrange_top_of_surjective {N} [monoid N] (f : M →* N) (hf : function.surjective f) : f.mrange = (⊤ : submonoid N) := mrange_top_iff_surjective.2 hf /-- The submonoid of elements `x : M` such that `f x = g x` -/ @[to_additive "The additive submonoid of elements `x : M` such that `f x = g x`"] def eq_mlocus (f g : M →* N) : submonoid M := { carrier := {x | f x = g x}, one_mem' := by rw [set.mem_set_of_eq, f.map_one, g.map_one], mul_mem' := λ x y (hx : _ = _) (hy : _ = _), by simp [*] } /-- If two monoid homomorphisms are equal on a set, then they are equal on its submonoid closure. -/ @[to_additive] lemma eq_on_mclosure {f g : M →* N} {s : set M} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_mlocus g, from closure_le.2 h @[to_additive] lemma eq_of_eq_on_mtop {f g : M →* N} (h : set.eq_on f g (⊤ : submonoid M)) : f = g := ext $ λ x, h trivial @[to_additive] lemma eq_of_eq_on_mdense {s : set M} (hs : closure s = ⊤) {f g : M →* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_mtop $ hs ▸ eq_on_mclosure h @[to_additive] lemma mclosure_preimage_le (f : M →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set."] lemma map_mclosure (f : M →* N) (s : set M) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (mclosure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end monoid_hom namespace free_monoid variables {α : Type*} open submonoid @[to_additive] theorem closure_range_of : closure (set.range $ @of α) = ⊤ := eq_top_iff.2 $ λ x hx, free_monoid.rec_on x (one_mem _) $ λ x xs hxs, mul_mem _ (subset_closure $ set.mem_range_self _) hxs end free_monoid namespace submonoid variables {N : Type*} [monoid N] open monoid_hom /-- The monoid hom associated to an inclusion of submonoids. -/ @[to_additive "The `add_monoid` hom associated to an inclusion of submonoids."] def inclusion {S T : submonoid M} (h : S ≤ T) : S →* T := S.subtype.cod_mrestrict _ (λ x, h x.2) @[simp, to_additive] lemma range_subtype (s : submonoid M) : s.subtype.mrange = s := ext' $ (coe_mrange _).trans $ set.range_coe_subtype s lemma closure_singleton_eq (x : M) : closure ({x} : set M) = (powers_hom M x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨multiplicative.of_add 1, trivial, pow_one x⟩) $ λ x ⟨n, _, hn⟩, hn ▸ pow_mem _ (subset_closure $ set.mem_singleton _) _ /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ lemma mem_closure_singleton {x y : M} : y ∈ closure ({x} : set M) ↔ ∃ n:ℕ, x^n=y := by rw [closure_singleton_eq, mem_mrange]; refl @[to_additive] lemma closure_eq_mrange (s : set M) : closure s = (free_monoid.lift (coe : s → M)).mrange := by rw [mrange, ← free_monoid.closure_range_of, map_mclosure, ← set.range_comp, free_monoid.lift_comp_of, set.range_coe_subtype] @[to_additive] lemma exists_list_of_mem_closure {s : set M} {x : M} (hx : x ∈ closure s) : ∃ (l : list M) (hl : ∀ y ∈ l, y ∈ s), l.prod = x := begin rw [closure_eq_mrange, mem_mrange] at hx, rcases hx with ⟨l, hx⟩, exact ⟨list.map coe l, λ y hy, let ⟨z, hz, hy⟩ := list.mem_map.1 hy in hy ▸ z.2, hx⟩ end @[to_additive] lemma map_inl (s : submonoid M) : s.map (inl M N) = s.prod ⊥ := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨hx, set.mem_singleton 1⟩, λ ⟨hps, hp1⟩, ⟨p.1, hps, prod.ext rfl $ (set.eq_of_mem_singleton hp1).symm⟩⟩ @[to_additive] lemma map_inr (s : submonoid N) : s.map (inr M N) = prod ⊥ s := ext $ λ p, ⟨λ ⟨x, hx, hp⟩, hp ▸ ⟨set.mem_singleton 1, hx⟩, λ ⟨hp1, hps⟩, ⟨p.2, hps, prod.ext (set.eq_of_mem_singleton hp1).symm rfl⟩⟩ @[to_additive] lemma range_inl : (inl M N).mrange = prod ⊤ ⊥ := map_inl ⊤ @[to_additive] lemma range_inr : (inr M N).mrange = prod ⊥ ⊤ := map_inr ⊤ @[to_additive] lemma range_inl' : (inl M N).mrange = comap (snd M N) ⊥ := range_inl.trans (top_prod _) @[to_additive] lemma range_inr' : (inr M N).mrange = comap (fst M N) ⊥ := range_inr.trans (prod_top _) @[simp, to_additive] lemma range_fst : (fst M N).mrange = ⊤ := (fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma range_snd : (snd M N).mrange = ⊤ := (snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩ @[simp, to_additive prod_bot_sup_bot_prod] lemma prod_bot_sup_bot_prod (s : submonoid M) (t : submonoid N) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem _ ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set.mem_singleton 1⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set.mem_singleton 1, hp.2⟩) @[simp, to_additive] lemma range_inl_sup_range_inr : (inl M N).mrange ⊔ (inr M N).mrange = ⊤ := by simp only [range_inl, range_inr, prod_bot_sup_bot_prod, top_prod_top] end submonoid namespace submonoid variables {N : Type*} [comm_monoid N] open monoid_hom @[to_additive] lemma sup_eq_range (s t : submonoid N) : s ⊔ t = (s.subtype.coprod t.subtype).mrange := by rw [mrange, ← range_inl_sup_range_inr, map_sup, map_mrange, coprod_comp_inl, map_mrange, coprod_comp_inr, range_subtype, range_subtype] @[to_additive] lemma mem_sup {s t : submonoid N} {x : N} : x ∈ s ⊔ t ↔ ∃ (y ∈ s) (z ∈ t), y * z = x := by simp only [sup_eq_range, mem_mrange, coprod_apply, prod.exists, submonoid.exists, coe_subtype, subtype.coe_mk] end submonoid namespace add_submonoid open set lemma smul_mem (S : add_submonoid A) {x : A} (hx : x ∈ S) : ∀ n : ℕ, n •ℕ x ∈ S | 0 := S.zero_mem | (n+1) := S.add_mem hx (smul_mem n) lemma closure_singleton_eq (x : A) : closure ({x} : set A) = (multiples_hom A x).mrange := closure_eq_of_le (set.singleton_subset_iff.2 ⟨1, trivial, one_nsmul x⟩) $ λ x ⟨n, _, hn⟩, hn ▸ smul_mem _ (subset_closure $ set.mem_singleton _) _ /-- The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element. -/ lemma mem_closure_singleton {x y : A} : y ∈ closure ({x} : set A) ↔ ∃ n:ℕ, n •ℕ x = y := by rw [closure_singleton_eq, add_monoid_hom.mem_mrange]; refl end add_submonoid namespace mul_equiv variables {S T : submonoid M} /-- Makes the identity isomorphism from a proof two submonoids of a multiplicative monoid are equal. -/ @[to_additive add_submonoid_congr "Makes the identity additive isomorphism from a proof two submonoids of an additive monoid are equal."] def submonoid_congr (h : S = T) : S ≃* T := { map_mul' := λ _ _, rfl, ..equiv.set_congr $ submonoid.ext'_iff.1 h } end mul_equiv
3ce460c78162b56166d24b8969b9680cbcf42fc2
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/formatTerm.lean
b8c9dfea540febe39cf02358047bb9a102a8790d
[ "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,170
lean
import Lean open Lean PrettyPrinter def fmt (stx : CoreM Syntax) : CoreM Format := do PrettyPrinter.ppTerm ⟨← stx⟩ #eval fmt `(if c then do t else e) #eval fmt `(if c then do t; t else e) #eval fmt `(if c then do t else do e) #eval fmt `(if let x := c then do t else do e) #eval fmt `(if c then do t else if c then do t else do e) -- FIXME: make this cascade better? #eval fmt `(do if c then t else e) #eval fmt `(do if c then t else if c then t else e) #eval fmt `(def foo := by · skip; skip · skip; skip skip (skip; skip) (skip; skip try skip; skip try skip skip skip)) #eval fmt `(by try skip skip) set_option format.indent 3 in #eval fmt `(by try skip skip) set_option format.indent 4 in #eval fmt `(by try skip skip) set_option format.indent 4 in #eval fmt `(by try skip skip skip) #eval fmt `({ foo := bar bar := foo + bar }) #eval fmt `(let x := { foo := bar bar := foo + bar } x) #eval fmt `(command| def foo : a b c d e f g a b c d e f g h where h := 42 i := 42 j := 42 k := 42) #eval fmt `(calc 1 = 1 := rfl 1 = 1 := rfl)
534142767694683db777feb4e55fd03583a50816
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/integral/interval_average.lean
f1d2f75d3678974b6dbfee38839587254e4a5a10
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,994
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.integral.interval_integral import measure_theory.integral.average /-! # Integral average over an interval In this file we introduce notation `⨍ x in a..b, f x` for the average `⨍ x in Ι a b, f x` of `f` over the interval `Ι a b = set.Ioc (min a b) (max a b)` w.r.t. the Lebesgue measure, then prove formulas for this average: * `interval_average_eq`: `⨍ x in a..b, f x = (b - a)⁻¹ • ∫ x in a..b, f x`; * `interval_average_eq_div`: `⨍ x in a..b, f x = (∫ x in a..b, f x) / (b - a)`. We also prove that `⨍ x in a..b, f x = ⨍ x in b..a, f x`, see `interval_average_symm`. ## Notation `⨍ x in a..b, f x`: average of `f` over the interval `Ι a b` w.r.t. the Lebesgue measure. -/ open measure_theory set topological_space open_locale interval variables {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] notation `⨍` binders ` in ` a `..` b `, ` r:(scoped:60 f, average (measure.restrict volume (Ι a b)) f) := r lemma interval_average_symm (f : ℝ → E) (a b : ℝ) : ⨍ x in a..b, f x = ⨍ x in b..a, f x := by rw [set_average_eq, set_average_eq, interval_oc_swap] lemma interval_average_eq (f : ℝ → E) (a b : ℝ) : ⨍ x in a..b, f x = (b - a)⁻¹ • ∫ x in a..b, f x := begin cases le_or_lt a b with h h, { rw [set_average_eq, interval_oc_of_le h, real.volume_Ioc, interval_integral.integral_of_le h, ennreal.to_real_of_real (sub_nonneg.2 h)] }, { rw [set_average_eq, interval_oc_of_lt h, real.volume_Ioc, interval_integral.integral_of_ge h.le, ennreal.to_real_of_real (sub_nonneg.2 h.le), smul_neg, ← neg_smul, ← inv_neg, neg_sub] } end lemma interval_average_eq_div (f : ℝ → ℝ) (a b : ℝ) : ⨍ x in a..b, f x = (∫ x in a..b, f x) / (b - a) := by rw [interval_average_eq, smul_eq_mul, div_eq_inv_mul]
fdcfe289a2e4d339ebea1a9544d75f1a265c52ca
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/normed_space/M_structure.lean
82bcc2458dad4c405804239f781dd9fdbaf0c5ff
[ "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
13,699
lean
/- Copyright (c) 2022 Christopher Hoskin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Christopher Hoskin -/ import algebra.ring.idempotents import tactic.noncomm_ring import analysis.normed.group.basic /-! # M-structure > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A projection P on a normed space X is said to be an L-projection (`is_Lprojection`) if, for all `x` in `X`, $\|x\| = \|P x\| + \|(1 - P) x\|$. A projection P on a normed space X is said to be an M-projection if, for all `x` in `X`, $\|x\| = max(\|P x\|,\|(1 - P) x\|)$. The L-projections on `X` form a Boolean algebra (`is_Lprojection.subtype.boolean_algebra`). ## TODO (Motivational background) The M-projections on a normed space form a Boolean algebra. The range of an L-projection on a normed space `X` is said to be an L-summand of `X`. The range of an M-projection is said to be an M-summand of `X`. When `X` is a Banach space, the Boolean algebra of L-projections is complete. Let `X` be a normed space with dual `X^*`. A closed subspace `M` of `X` is said to be an M-ideal if the topological annihilator `M^∘` is an L-summand of `X^*`. M-ideal, M-summands and L-summands were introduced by Alfsen and Effros in [alfseneffros1972] to study the structure of general Banach spaces. When `A` is a JB*-triple, the M-ideals of `A` are exactly the norm-closed ideals of `A`. When `A` is a JBW*-triple with predual `X`, the M-summands of `A` are exactly the weak*-closed ideals, and their pre-duals can be identified with the L-summands of `X`. In the special case when `A` is a C*-algebra, the M-ideals are exactly the norm-closed two-sided ideals of `A`, when `A` is also a W*-algebra the M-summands are exactly the weak*-closed two-sided ideals of `A`. ## Implementation notes The approach to showing that the L-projections form a Boolean algebra is inspired by `measure_theory.measurable_space`. Instead of using `P : X →L[𝕜] X` to represent projections, we use an arbitrary ring `M` with a faithful action on `X`. `continuous_linear_map.apply_module` can be used to recover the `X →L[𝕜] X` special case. ## References * [Behrends, M-structure and the Banach-Stone Theorem][behrends1979] * [Harmand, Werner, Werner, M-ideals in Banach spaces and Banach algebras][harmandwernerwerner1993] ## Tags M-summand, M-projection, L-summand, L-projection, M-ideal, M-structure -/ variables (X : Type*) [normed_add_comm_group X] variables {M : Type*} [ring M] [module M X] /-- A projection on a normed space `X` is said to be an L-projection if, for all `x` in `X`, $\|x\| = \|P x\| + \|(1 - P) x\|$. Note that we write `P • x` instead of `P x` for reasons described in the module docstring. -/ structure is_Lprojection (P : M) : Prop := (proj : is_idempotent_elem P) (Lnorm : ∀ (x : X), ‖x‖ = ‖P • x‖ + ‖(1 - P) • x‖) /-- A projection on a normed space `X` is said to be an M-projection if, for all `x` in `X`, $\|x\| = max(\|P x\|,\|(1 - P) x\|)$. Note that we write `P • x` instead of `P x` for reasons described in the module docstring. -/ structure is_Mprojection (P : M) : Prop := (proj : is_idempotent_elem P) (Mnorm : ∀ (x : X), ‖x‖ = (max ‖P • x‖ ‖(1 - P) • x‖)) variables {X} namespace is_Lprojection lemma Lcomplement {P : M} (h: is_Lprojection X P) : is_Lprojection X (1 - P) := ⟨h.proj.one_sub, λ x, by { rw [add_comm, sub_sub_cancel], exact h.Lnorm x }⟩ lemma Lcomplement_iff (P : M) : is_Lprojection X P ↔ is_Lprojection X (1 - P) := ⟨Lcomplement, λ h, sub_sub_cancel 1 P ▸ h.Lcomplement⟩ lemma commute [has_faithful_smul M X] {P Q : M} (h₁ : is_Lprojection X P) (h₂ : is_Lprojection X Q) : commute P Q := begin have PR_eq_RPR : ∀ R : M, is_Lprojection X R → P * R = R * P * R := λ R h₃, begin refine @eq_of_smul_eq_smul _ X _ _ _ _ (λ x, _), rw ← norm_sub_eq_zero_iff, have e1 : ‖R • x‖ ≥ ‖R • x‖ + 2 • ‖ (P * R) • x - (R * P * R) • x‖ := calc ‖R • x‖ = ‖R • (P • (R • x))‖ + ‖(1 - R) • (P • (R • x))‖ + (‖(R * R) • x - R • (P • (R • x))‖ + ‖(1 - R) • ((1 - P) • (R • x))‖) : by rw [h₁.Lnorm, h₃.Lnorm, h₃.Lnorm ((1 - P) • (R • x)), sub_smul 1 P, one_smul, smul_sub, mul_smul] ... = ‖R • (P • (R • x))‖ + ‖(1 - R) • (P • (R • x))‖ + (‖R • x - R • (P • (R • x))‖ + ‖((1 - R) * R) • x - (1 - R) • (P • (R • x))‖) : by rw [h₃.proj.eq, sub_smul 1 P, one_smul, smul_sub, mul_smul] ... = ‖R • (P • (R • x))‖ + ‖(1 - R) • (P • (R • x))‖ + (‖R • x - R • (P • (R • x))‖ + ‖(1 - R) • (P • (R • x))‖) : by rw [sub_mul, h₃.proj.eq, one_mul, sub_self, zero_smul, zero_sub, norm_neg] ... = ‖R • (P • (R • x))‖ + ‖R • x - R • (P • (R • x))‖ + 2•‖(1 - R) • (P • (R • x))‖ : by abel ... ≥ ‖R • x‖ + 2 • ‖ (P * R) • x - (R * P * R) • x‖ : by { rw ge, have := add_le_add_right (norm_le_insert' (R • x) (R • (P • (R • x)))) (2•‖(1 - R) • (P • (R • x))‖), simpa only [mul_smul, sub_smul, one_smul] using this }, rw ge at e1, nth_rewrite_rhs 0 ← add_zero (‖R • x‖) at e1, rw [add_le_add_iff_left, two_smul, ← two_mul] at e1, rw le_antisymm_iff, refine ⟨_, norm_nonneg _⟩, rwa [←mul_zero (2 : ℝ), mul_le_mul_left (show (0 : ℝ) < 2, by norm_num)] at e1 end, have QP_eq_QPQ : Q * P = Q * P * Q, { have e1 : P * (1 - Q) = P * (1 - Q) - (Q * P - Q * P * Q) := calc P * (1 - Q) = (1 - Q) * P * (1 - Q) : by rw PR_eq_RPR (1 - Q) h₂.Lcomplement ... = P * (1 - Q) - (Q * P - Q * P * Q) : by noncomm_ring, rwa [eq_sub_iff_add_eq, add_right_eq_self, sub_eq_zero] at e1 }, show P * Q = Q * P, by rw [QP_eq_QPQ, PR_eq_RPR Q h₂] end lemma mul [has_faithful_smul M X] {P Q : M} (h₁ : is_Lprojection X P) (h₂ : is_Lprojection X Q) : is_Lprojection X (P * Q) := begin refine ⟨is_idempotent_elem.mul_of_commute (h₁.commute h₂) h₁.proj h₂.proj, _⟩, intro x, refine le_antisymm _ _, { calc ‖ x ‖ = ‖(P * Q) • x + (x - (P * Q) • x)‖ : by rw add_sub_cancel'_right ((P * Q) • x) x ... ≤ ‖(P * Q) • x‖ + ‖ x - (P * Q) • x ‖ : by apply norm_add_le ... = ‖(P * Q) • x‖ + ‖(1 - P * Q) • x‖ : by rw [sub_smul, one_smul] }, { calc ‖x‖ = ‖P • (Q • x)‖ + (‖Q • x - P • (Q • x)‖ + ‖x - Q • x‖) : by rw [h₂.Lnorm x, h₁.Lnorm (Q • x), sub_smul, one_smul, sub_smul, one_smul, add_assoc] ... ≥ ‖P • (Q • x)‖ + ‖(Q • x - P • (Q • x)) + (x - Q • x)‖ : (add_le_add_iff_left (‖P • (Q • x)‖)).mpr (norm_add_le (Q • x - P • (Q • x)) (x - Q • x)) ... = ‖(P * Q) • x‖ + ‖(1 - P * Q) • x‖ : by rw [sub_add_sub_cancel', sub_smul, one_smul, mul_smul] } end lemma join [has_faithful_smul M X] {P Q : M} (h₁ : is_Lprojection X P) (h₂ : is_Lprojection X Q) : is_Lprojection X (P + Q - P * Q) := begin convert (Lcomplement_iff _).mp (h₁.Lcomplement.mul h₂.Lcomplement) using 1, noncomm_ring, end instance : has_compl { f : M // is_Lprojection X f } := ⟨λ P, ⟨1 - P, P.prop.Lcomplement⟩⟩ @[simp] lemma coe_compl (P : {P : M // is_Lprojection X P}) : ↑(Pᶜ) = (1 : M) - ↑P := rfl instance [has_faithful_smul M X] : has_inf {P : M // is_Lprojection X P} := ⟨λ P Q, ⟨P * Q, P.prop.mul Q.prop⟩ ⟩ @[simp] lemma coe_inf [has_faithful_smul M X] (P Q : {P : M // is_Lprojection X P}) : ↑(P ⊓ Q) = ((↑P : (M)) * ↑Q) := rfl instance [has_faithful_smul M X] : has_sup {P : M // is_Lprojection X P} := ⟨λ P Q, ⟨P + Q - P * Q, P.prop.join Q.prop⟩ ⟩ @[simp] lemma coe_sup [has_faithful_smul M X] (P Q : {P : M // is_Lprojection X P}) : ↑(P ⊔ Q) = ((↑P : M) + ↑Q - ↑P * ↑Q) := rfl instance [has_faithful_smul M X] : has_sdiff {P : M // is_Lprojection X P} := ⟨λ P Q, ⟨P * (1 - Q), by exact P.prop.mul Q.prop.Lcomplement ⟩⟩ @[simp] lemma coe_sdiff [has_faithful_smul M X] (P Q : {P : M // is_Lprojection X P}) : ↑(P \ Q) = (↑P : M) * (1 - ↑Q) := rfl instance [has_faithful_smul M X] : partial_order {P : M // is_Lprojection X P} := { le := λ P Q, (↑P : M) = ↑(P ⊓ Q), le_refl := λ P, by simpa only [coe_inf, ←sq] using (P.prop.proj.eq).symm, le_trans := λ P Q R h₁ h₂, by { simp only [coe_inf] at ⊢ h₁ h₂, rw [h₁, mul_assoc, ←h₂] }, le_antisymm := λ P Q h₁ h₂, subtype.eq (by convert (P.prop.commute Q.prop).eq) } lemma le_def [has_faithful_smul M X] (P Q : {P : M // is_Lprojection X P}) : P ≤ Q ↔ (P : M) = ↑(P ⊓ Q) := iff.rfl instance : has_zero {P : M // is_Lprojection X P} := ⟨⟨0, ⟨by rw [is_idempotent_elem, zero_mul], λ x, by simp only [zero_smul, norm_zero, sub_zero, one_smul, zero_add]⟩⟩⟩ @[simp] lemma coe_zero : ↑(0 : {P : M // is_Lprojection X P}) = (0 : M) := rfl instance : has_one {P : M // is_Lprojection X P} := ⟨⟨1, sub_zero (1 : M) ▸ (0 : {P : M // is_Lprojection X P}).prop.Lcomplement⟩⟩ @[simp] lemma coe_one : ↑(1 : {P : M // is_Lprojection X P}) = (1 : M) := rfl instance [has_faithful_smul M X] : bounded_order {P : M // is_Lprojection X P} := { top := 1, le_top := λ P, (mul_one (P : M)).symm, bot := 0, bot_le := λ P, (zero_mul (P : M)).symm, } @[simp] lemma coe_bot [has_faithful_smul M X] : ↑(bounded_order.bot : {P : M // is_Lprojection X P}) = (0 : M) := rfl @[simp] lemma coe_top [has_faithful_smul M X] : ↑(bounded_order.top : {P : M // is_Lprojection X P}) = (1 : M) := rfl lemma compl_mul {P : {P : M // is_Lprojection X P}} {Q : M} : ↑Pᶜ * Q = Q - ↑P * Q := by rw [coe_compl, sub_mul, one_mul] lemma mul_compl_self {P : {P : M // is_Lprojection X P}} : (↑P : M) * (↑Pᶜ) = 0 := by rw [coe_compl, mul_sub, mul_one, P.prop.proj.eq, sub_self] lemma distrib_lattice_lemma [has_faithful_smul M X] {P Q R : {P : M // is_Lprojection X P}} : ((↑P : M) + ↑Pᶜ * R) * (↑P + ↑Q * ↑R * ↑Pᶜ) = (↑P + ↑Q * ↑R * ↑Pᶜ) := by rw [add_mul, mul_add, mul_add, mul_assoc ↑Pᶜ ↑R (↑Q * ↑R * ↑Pᶜ), ← mul_assoc ↑R (↑Q * ↑R) ↑Pᶜ, ← coe_inf Q, (Pᶜ.prop.commute R.prop).eq, ((Q ⊓ R).prop.commute Pᶜ.prop).eq, (R.prop.commute (Q ⊓ R).prop).eq, coe_inf Q, mul_assoc ↑Q, ← mul_assoc, mul_assoc ↑R, (Pᶜ.prop.commute P.prop).eq, mul_compl_self, zero_mul, mul_zero, zero_add, add_zero, ← mul_assoc, P.prop.proj.eq, R.prop.proj.eq, ← coe_inf Q, mul_assoc, ((Q ⊓ R).prop.commute Pᶜ.prop).eq, ← mul_assoc, Pᶜ.prop.proj.eq] instance [has_faithful_smul M X] : distrib_lattice {P : M // is_Lprojection X P} := { le_sup_left := λ P Q, by rw [le_def, coe_inf, coe_sup, ← add_sub, mul_add, mul_sub, ← mul_assoc, P.prop.proj.eq, sub_self, add_zero], le_sup_right := λ P Q, begin rw [le_def, coe_inf, coe_sup, ← add_sub, mul_add, mul_sub, commute.eq (commute P.prop Q.prop), ← mul_assoc, Q.prop.proj.eq, add_sub_cancel'_right], end, sup_le := λ P Q R, begin rw [le_def, le_def, le_def, coe_inf, coe_inf, coe_sup, coe_inf, coe_sup, ← add_sub, add_mul, sub_mul, mul_assoc], intros h₁ h₂, rw [← h₂, ← h₁], end, inf_le_left := λ P Q, by rw [le_def, coe_inf, coe_inf, coe_inf, mul_assoc, (Q.prop.commute P.prop).eq, ← mul_assoc, P.prop.proj.eq], inf_le_right := λ P Q, by rw [le_def, coe_inf, coe_inf, coe_inf, mul_assoc, Q.prop.proj.eq], le_inf := λ P Q R, begin rw [le_def, le_def, le_def, coe_inf, coe_inf, coe_inf, coe_inf, ← mul_assoc], intros h₁ h₂, rw [← h₁, ← h₂], end, le_sup_inf := λ P Q R, begin have e₁: ↑((P ⊔ Q) ⊓ (P ⊔ R)) = ↑P + ↑Q * ↑R * ↑Pᶜ := by rw [coe_inf, coe_sup, coe_sup, ← add_sub, ← add_sub, ←compl_mul, ←compl_mul, add_mul, mul_add, (Pᶜ.prop.commute Q.prop).eq, mul_add, ← mul_assoc, mul_assoc ↑Q, (Pᶜ.prop.commute P.prop).eq, mul_compl_self, zero_mul, mul_zero, zero_add, add_zero, ← mul_assoc, mul_assoc ↑Q, P.prop.proj.eq, Pᶜ.prop.proj.eq, mul_assoc, (Pᶜ.prop.commute R.prop).eq, ← mul_assoc], have e₂ : ↑((P ⊔ Q) ⊓ (P ⊔ R)) * ↑(P ⊔ Q ⊓ R) = ↑P + ↑Q * ↑R * ↑Pᶜ := by rw [coe_inf, coe_sup, coe_sup, coe_sup, ← add_sub, ← add_sub, ← add_sub, ←compl_mul, ←compl_mul, ←compl_mul, (Pᶜ.prop.commute (Q⊓R).prop).eq, coe_inf, mul_assoc, distrib_lattice_lemma, (Q.prop.commute R.prop).eq, distrib_lattice_lemma], rw [le_def, e₁, coe_inf, e₂], end, .. is_Lprojection.subtype.has_inf, .. is_Lprojection.subtype.has_sup, .. is_Lprojection.subtype.partial_order } instance [has_faithful_smul M X] : boolean_algebra {P : M // is_Lprojection X P} := { inf_compl_le_bot := λ P, (subtype.ext (by rw [coe_inf, coe_compl, coe_bot, ← coe_compl, mul_compl_self])).le, top_le_sup_compl := λ P, (subtype.ext(by rw [coe_top, coe_sup, coe_compl, add_sub_cancel'_right, ← coe_compl, mul_compl_self, sub_zero])).le, sdiff_eq := λ P Q, subtype.ext $ by rw [coe_sdiff, ← coe_compl, coe_inf], .. is_Lprojection.subtype.has_compl, .. is_Lprojection.subtype.has_sdiff, .. is_Lprojection.subtype.bounded_order, .. is_Lprojection.subtype.distrib_lattice } end is_Lprojection
268e82e2bb26e65f10375e261cadcb1d5ff2391d
1d265c7dd8cb3d0e1d645a19fd6157a2084c3921
/src/other/dispf_inclass.lean
8ab018d6a89b2bedee5f43bb9a0a6aafb77d9750
[ "MIT" ]
permissive
hanzhi713/lean-proofs
de432372f220d302be09b5ca4227f8986567e4fd
4d8356a878645b9ba7cb036f87737f3f1e68ede5
refs/heads/master
1,585,580,245,658
1,553,646,623,000
1,553,646,623,000
151,342,188
0
1
null
null
null
null
UTF-8
Lean
false
false
1,355
lean
theorem all_nne : ∀ (α : Type 1) (p : α → Prop), (∀ x, p x) → ¬ (∃ x, ¬ p x) := begin assume α p, assume h, assume e, show false, apply exists.elim e, intros, have : p a := h a, show false, from a_1 this, end example : ¬ (∀ (X : Type), ∀ (p q : X → Prop), (∃ x, p x) ∧ (∃ x, q x) → ∃ x, p x ∧ q x) := begin assume h, have ne : ¬ ∃ X : Type, ¬ (∀ (p q : X → Prop), (∃ x, p x) ∧ (∃ x, q x) → ∃ x, p x ∧ q x), apply all_nne, assumption, have : ∃ X : Type, ¬ (∀ (p q : X → Prop), (∃ x, p x) ∧ (∃ x, q x) → ∃ x, p x ∧ q x), apply exists.intro ℕ, assume h2, have h3 : (∃ x, x = 0) ∧ (∃ x, x = 1) → (∃ x, x = 0 ∧ x = 1), from h2 (λ a, a = 0) (λ b, b = 1), have pfx0 : (∃ x, x = 0) := ⟨0, rfl⟩, have pfx1 : (∃ x, x = 1) := ⟨1, rfl⟩, have : (∃ x, x = 0 ∧ x = 1) := h3 ⟨pfx0, pfx1⟩, apply exists.elim this, intros x x0x1, have l : x = 0 := x0x1.1, have r : x = 1 := x0x1.2, have : 0 = 1 := eq.subst r (eq.symm l), show false, from nat.no_confusion this, contradiction, end
250c613f71355f4727aa17153374e5acec3c1b0c
42c01158c2730cc6ac3e058c1339c18cb90366e2
/M1F/2017-18/Example_Sheet_01/Question_06/solution_6.lean
04259771fdbb86e188646b3220c315758ea0e61d
[]
no_license
ChrisHughes24/xena
c80d94355d0c2ae8deddda9d01e6d31bc21c30ae
337a0d7c9f0e255e08d6d0a383e303c080c6ec0c
refs/heads/master
1,631,059,898,392
1,511,200,551,000
1,511,200,551,000
111,468,589
1
0
null
null
null
null
UTF-8
Lean
false
false
2,011
lean
inductive zfc : Type | empty : zfc | insert : zfc → zfc → zfc instance : has_emptyc zfc := ⟨zfc.empty⟩ instance : has_insert zfc zfc := ⟨zfc.insert⟩ instance : has_zero zfc := ⟨{}⟩ instance : has_one zfc := ⟨{0}⟩ def succ (a : zfc) : zfc := has_insert.insert a a def zfc_add : zfc → zfc → zfc | m 0 := m | m (zfc.insert n p) := succ (zfc_add m n) instance : has_add zfc := ⟨zfc_add⟩ inductive member : zfc → zfc → Prop | left {a b c : zfc} : member a c → member a (has_insert.insert b c) | right {a b : zfc} : member a (has_insert.insert a b) instance : has_mem zfc zfc := ⟨member⟩ instance : has_subset zfc := ⟨(λ a b:zfc, (∀ z:zfc, z ∈ a → z ∈ b))⟩ axiom member.ext (a b : zfc) : (∀ x, x ∈ a ↔ x ∈ b) ↔ a=b export zfc export member definition A : zfc := {1,2,{1,2}} definition B : zfc := {1,2,A} theorem M1F_Sheet01_Q06a_is_true : (1:zfc) ∈ A := begin left, left, right end theorem M1F_Sheet01_Q06b_is_false: ({1}:zfc) ∉ A := begin intro h, cases h, cases a_1, cases a_2, cases a_3 end theorem M1F_Sheet01_Q06c_is_true: ({1,2}:zfc) ∈ A := begin right end theorem M1F_Sheet01_Q06d_is_true: ({1,2}:zfc) ⊆ A := begin intro x, intro h, cases h, cases a_1, cases a_2, left, left, right, left, right end theorem M1F_Sheet01_Q06e_is_true: (1:zfc) ∈ B := begin left, left, right end theorem M1F_Sheet01_Q06f_is_false: ({1}:zfc) ∉ B := begin intro h, cases h, cases a_1, cases a_2, cases a_3 end theorem M1F_Sheet01_Q06g_is_true: ({1,2}:zfc) ∈ B → (1:zfc) ∈ A := begin intro h, cases h, cases a_1, cases a_2, cases a_3 end theorem M1F_Sheet01_Q06h_is_true: ({1,2}:zfc) ⊆ B ∨ (1:zfc) ∉ A := begin left, intro x, intro h, cases h, cases a_1, cases a_2, left, left, right, left, right end
cb57712677aec7528ee3df392b6cfb463e9857ac
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/nat/choose/basic.lean
edfdaf90edd8aef5c3434a60611629bbc8c06244
[]
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,908
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.factorial import Mathlib.PostPort namespace Mathlib /-! # Binomial coefficients This file contains a definition of binomial coefficients and simple lemmas (i.e. those not requiring more imports). ## Main definition and results - `nat.choose`: binomial coefficients, defined inductively - `nat.choose_eq_factorial_div_factorial`: a proof that `choose n k = n! / (k! * (n - k)!)` - `nat.choose_symm`: symmetry of binomial coefficients - `nat.choose_le_succ_of_lt_half_left`: `choose n k` is increasing for small values of `k` - `nat.choose_le_middle`: `choose n r` is maximised when `r` is `n/2` -/ namespace nat /-- `choose n k` is the number of `k`-element subsets in an `n`-element set. Also known as binomial coefficients. -/ def choose : ℕ → ℕ → ℕ := sorry @[simp] theorem choose_zero_right (n : ℕ) : choose n 0 = 1 := nat.cases_on n (Eq.refl (choose 0 0)) fun (n : ℕ) => Eq.refl (choose (Nat.succ n) 0) @[simp] theorem choose_zero_succ (k : ℕ) : choose 0 (Nat.succ k) = 0 := rfl theorem choose_succ_succ (n : ℕ) (k : ℕ) : choose (Nat.succ n) (Nat.succ k) = choose n k + choose n (Nat.succ k) := rfl theorem choose_eq_zero_of_lt {n : ℕ} {k : ℕ} : n < k → choose n k = 0 := sorry @[simp] theorem choose_self (n : ℕ) : choose n n = 1 := sorry @[simp] theorem choose_succ_self (n : ℕ) : choose n (Nat.succ n) = 0 := choose_eq_zero_of_lt (lt_succ_self n) @[simp] theorem choose_one_right (n : ℕ) : choose n 1 = n := sorry /- The `n+1`-st triangle number is `n` more than the `n`-th triangle number -/ theorem triangle_succ (n : ℕ) : (n + 1) * (n + 1 - 1) / bit0 1 = n * (n - 1) / bit0 1 + n := sorry /-- `choose n 2` is the `n`-th triangle number. -/ theorem choose_two_right (n : ℕ) : choose n (bit0 1) = n * (n - 1) / bit0 1 := sorry theorem choose_pos {n : ℕ} {k : ℕ} : k ≤ n → 0 < choose n k := sorry theorem succ_mul_choose_eq (n : ℕ) (k : ℕ) : Nat.succ n * choose n k = choose (Nat.succ n) (Nat.succ k) * Nat.succ k := sorry theorem choose_mul_factorial_mul_factorial {n : ℕ} {k : ℕ} : k ≤ n → choose n k * factorial k * factorial (n - k) = factorial n := sorry theorem choose_eq_factorial_div_factorial {n : ℕ} {k : ℕ} (hk : k ≤ n) : choose n k = factorial n / (factorial k * factorial (n - k)) := sorry theorem factorial_mul_factorial_dvd_factorial {n : ℕ} {k : ℕ} (hk : k ≤ n) : factorial k * factorial (n - k) ∣ factorial n := sorry @[simp] theorem choose_symm {n : ℕ} {k : ℕ} (hk : k ≤ n) : choose n (n - k) = choose n k := sorry theorem choose_symm_of_eq_add {n : ℕ} {a : ℕ} {b : ℕ} (h : n = a + b) : choose n a = choose n b := sorry theorem choose_symm_add {a : ℕ} {b : ℕ} : choose (a + b) a = choose (a + b) b := choose_symm_of_eq_add rfl theorem choose_symm_half (m : ℕ) : choose (bit0 1 * m + 1) (m + 1) = choose (bit0 1 * m + 1) m := sorry theorem choose_succ_right_eq (n : ℕ) (k : ℕ) : choose n (k + 1) * (k + 1) = choose n k * (n - k) := sorry @[simp] theorem choose_succ_self_right (n : ℕ) : choose (n + 1) n = n + 1 := sorry theorem choose_mul_succ_eq (n : ℕ) (k : ℕ) : choose n k * (n + 1) = choose (n + 1) k * (n + 1 - k) := sorry /-! ### Inequalities -/ /-- Show that `nat.choose` is increasing for small values of the right argument. -/ theorem choose_le_succ_of_lt_half_left {r : ℕ} {n : ℕ} (h : r < n / bit0 1) : choose n r ≤ choose n (r + 1) := sorry /-- Show that for small values of the right argument, the middle value is largest. -/ /-- `choose n r` is maximised when `r` is `n/2`. -/ theorem choose_le_middle (r : ℕ) (n : ℕ) : choose n r ≤ choose n (n / bit0 1) := sorry
5c92392631a2fb1d044302b5d902de338c23e7a5
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/convex/extrema.lean
4de3ca9a378926901e83c6d51b90830c7c66f545
[]
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,232
lean
/- Copyright (c) 2020 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.local_extr import Mathlib.topology.algebra.affine import Mathlib.PostPort universes u_2 u_1 namespace Mathlib /-! # Minima and maxima of convex functions We show that if a function `f : E → β` is convex, then a local minimum is also a global minimum, and likewise for concave functions. -/ /-- Helper lemma for the more general case: `is_min_on.of_is_local_min_on_of_convex_on`. -/ theorem is_min_on.of_is_local_min_on_of_convex_on_Icc {β : Type u_2} [linear_ordered_add_comm_group β] [semimodule ℝ β] [ordered_semimodule ℝ β] {f : ℝ → β} {a : ℝ} {b : ℝ} (a_lt_b : a < b) (h_local_min : is_local_min_on f (set.Icc a b) a) (h_conv : convex_on (set.Icc a b) f) (x : ℝ) (H : x ∈ set.Icc a b) : f a ≤ f x := sorry /-- A local minimum of a convex function is a global minimum, restricted to a set `s`. -/ theorem is_min_on.of_is_local_min_on_of_convex_on {E : Type u_1} {β : Type u_2} [add_comm_group E] [topological_space E] [module ℝ E] [topological_add_group E] [topological_vector_space ℝ E] [linear_ordered_add_comm_group β] [semimodule ℝ β] [ordered_semimodule ℝ β] {s : set E} {f : E → β} {a : E} (a_in_s : a ∈ s) (h_localmin : is_local_min_on f s a) (h_conv : convex_on s f) (x : E) (H : x ∈ s) : f a ≤ f x := sorry /-- A local maximum of a concave function is a global maximum, restricted to a set `s`. -/ theorem is_max_on.of_is_local_max_on_of_concave_on {E : Type u_1} {β : Type u_2} [add_comm_group E] [topological_space E] [module ℝ E] [topological_add_group E] [topological_vector_space ℝ E] [linear_ordered_add_comm_group β] [semimodule ℝ β] [ordered_semimodule ℝ β] {s : set E} {f : E → β} {a : E} (a_in_s : a ∈ s) (h_localmax : is_local_max_on f s a) (h_conc : concave_on s f) (x : E) (H : x ∈ s) : f x ≤ f a := is_min_on.of_is_local_min_on_of_convex_on a_in_s h_localmax h_conc /-- A local minimum of a convex function is a global minimum. -/ theorem is_min_on.of_is_local_min_of_convex_univ {E : Type u_1} {β : Type u_2} [add_comm_group E] [topological_space E] [module ℝ E] [topological_add_group E] [topological_vector_space ℝ E] [linear_ordered_add_comm_group β] [semimodule ℝ β] [ordered_semimodule ℝ β] {f : E → β} {a : E} (h_local_min : is_local_min f a) (h_conv : convex_on set.univ f) (x : E) : f a ≤ f x := is_min_on.of_is_local_min_on_of_convex_on (set.mem_univ a) (is_local_min.on h_local_min set.univ) h_conv x (set.mem_univ x) /-- A local maximum of a concave function is a global maximum. -/ theorem is_max_on.of_is_local_max_of_convex_univ {E : Type u_1} {β : Type u_2} [add_comm_group E] [topological_space E] [module ℝ E] [topological_add_group E] [topological_vector_space ℝ E] [linear_ordered_add_comm_group β] [semimodule ℝ β] [ordered_semimodule ℝ β] {f : E → β} {a : E} (h_local_max : is_local_max f a) (h_conc : concave_on set.univ f) (x : E) : f x ≤ f a := is_min_on.of_is_local_min_of_convex_univ h_local_max h_conc
f4fcedba2856a68d79413ce8ccf2ae2b81f64658
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Elab/Tactic/Injection.lean
54822820648476f24f2ddfba17ccf733e35079f6
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
1,202
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Injection import Lean.Elab.Tactic.ElabTerm namespace Lean.Elab.Tactic -- optional (" with " >> many1 ident') private def getInjectionNewIds (stx : Syntax) : List Name := if stx.isNone then [] else stx[1].getArgs.toList.map getNameOfIdent' private def checkUnusedIds (mvarId : MVarId) (unusedIds : List Name) : MetaM Unit := unless unusedIds.isEmpty do Meta.throwTacticEx `injection mvarId m!"too many identifiers provided, unused: {unusedIds}" @[builtinTactic «injection»] def evalInjection : Tactic := fun stx => do -- leading_parser nonReservedSymbol "injection " >> termParser >> withIds let fvarId ← elabAsFVar stx[1] let ids := getInjectionNewIds stx[2] liftMetaTactic fun mvarId => do match (← Meta.injection mvarId fvarId ids (!ids.isEmpty)) with | Meta.InjectionResult.solved => checkUnusedIds mvarId ids; pure [] | Meta.InjectionResult.subgoal mvarId' _ unusedIds => checkUnusedIds mvarId unusedIds; pure [mvarId'] end Lean.Elab.Tactic