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
c87e45d88e633c8c50478fa561c15c38bb97f0d8
e898bfefd5cb60a60220830c5eba68cab8d02c79
/uexp/src/uexp/rules/index.lean
ecfec740c0fdc90dcbc8eefaaa539d11c0711d44
[ "BSD-2-Clause" ]
permissive
kkpapa/Cosette
9ed09e2dc4c1ecdef815c30b5501f64a7383a2ce
fda8fdbbf0de6c1be9b4104b87bbb06cede46329
refs/heads/master
1,584,573,128,049
1,526,370,422,000
1,526,370,422,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,131
lean
import ..sql import ..u_semiring import ..extra_constants import ..ucongr import ..TDP ..canonize set_option profiler true open Expr open Proj open Pred open SQL open tree notation `int` := datatypes.int variable integer_10: const datatypes.int lemma IndexQ0: forall Γ r (R: relation r) t0 (l: const t0) (a: Column t0 r) t1 (k: Column t1 r) (ik: isKey k R), denoteSQL ((SELECT * FROM1 (table R) WHERE equal (uvariable (right⋅a)) (constantExpr l)): SQL Γ _) = denoteSQL ((project (right⋅right) (FROM1 (product (Index (@table Γ r R) k a) (table R)) WHERE and (equal (uvariable (right⋅left⋅right⋅left⋅star)) (constantExpr l)) (equal (uvariable (right⋅left⋅left⋅star)) (uvariable (right⋅right⋅k))) )) :SQL Γ _) := begin delta Index, intros, delta select2, delta projectCons, unfold_all_denotations, simp, funext, apply ueq_symm, remove_dup_sigs_lhs, dsimp, remove_dup_sigs_lhs, dsimp, remove_dup_sigs_lhs, canonize, remove_dup_sigs_lhs, refl end
82862f29eba462c51133bb82da9206218f883a87
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/analysis/convex/basic.lean
611b70fc3511dee7896df7a20d9c512ab9b3f447
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
39,559
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov, Yaël Dillies -/ import algebra.module.ordered import linear_algebra.affine_space.affine_subspace /-! # Convex sets and functions in vector spaces In a 𝕜-vector space, we define the following objects and properties. * `segment 𝕜 x y`: Closed segment joining `x` and `y`. * `open_segment 𝕜 x y`: Open segment joining `x` and `y`. * `convex 𝕜 s`: A set `s` is convex if for any two points `x y ∈ s` it includes `segment 𝕜 x y`. * `std_simplex 𝕜 ι`: The standard simplex in `ι → 𝕜` (currently requires `fintype ι`). It is the intersection of the positive quadrant with the hyperplane `s.sum = 1`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex. ## Notations We provide the following notation: * `[x -[𝕜] y] = segment 𝕜 x y` in locale `convex` ## TODO Generalize all this file to affine spaces. Should we rename `segment` and `open_segment` to `convex.Icc` and `convex.Ioo`? Should we also define `clopen_segment`/`convex.Ico`/`convex.Ioc`? -/ variables {𝕜 E F β : Type*} open linear_map set open_locale big_operators classical pointwise /-! ### Segment -/ section ordered_semiring variables [ordered_semiring 𝕜] [add_comm_monoid E] section has_scalar variables (𝕜) [has_scalar 𝕜 E] /-- Segments in a vector space. -/ def segment (x y : E) : set E := {z : E | ∃ (a b : 𝕜) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), a • x + b • y = z} /-- Open segment in a vector space. Note that `open_segment 𝕜 x x = {x}` instead of being `∅` when the base semiring has some element between `0` and `1`. -/ def open_segment (x y : E) : set E := {z : E | ∃ (a b : 𝕜) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1), a • x + b • y = z} localized "notation `[` x ` -[` 𝕜 `] ` y `]` := segment 𝕜 x y" in convex lemma segment_symm (x y : E) : [x -[𝕜] y] = [y -[𝕜] x] := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma open_segment_symm (x y : E) : open_segment 𝕜 x y = open_segment 𝕜 y x := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma open_segment_subset_segment (x y : E) : open_segment 𝕜 x y ⊆ [x -[𝕜] y] := λ z ⟨a, b, ha, hb, hab, hz⟩, ⟨a, b, ha.le, hb.le, hab, hz⟩ end has_scalar open_locale convex section mul_action_with_zero variables (𝕜) [mul_action_with_zero 𝕜 E] lemma left_mem_segment (x y : E) : x ∈ [x -[𝕜] y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x -[𝕜] y] := segment_symm 𝕜 y x ▸ left_mem_segment 𝕜 y x end mul_action_with_zero section module variables (𝕜) [module 𝕜 E] lemma segment_same (x : E) : [x -[𝕜] x] = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ h, mem_singleton_iff.1 h ▸ left_mem_segment 𝕜 z z⟩ lemma mem_open_segment_of_ne_left_right {x y z : E} (hx : x ≠ z) (hy : y ≠ z) (hz : z ∈ [x -[𝕜] y]) : z ∈ open_segment 𝕜 x y := begin obtain ⟨a, b, ha, hb, hab, hz⟩ := hz, by_cases ha' : a = 0, { rw [ha', zero_add] at hab, rw [ha', hab, zero_smul, one_smul, zero_add] at hz, exact (hy hz).elim }, by_cases hb' : b = 0, { rw [hb', add_zero] at hab, rw [hb', hab, zero_smul, one_smul, add_zero] at hz, exact (hx hz).elim }, exact ⟨a, b, ha.lt_of_ne (ne.symm ha'), hb.lt_of_ne (ne.symm hb'), hab, hz⟩, end variables {𝕜} lemma open_segment_subset_iff_segment_subset {x y : E} {s : set E} (hx : x ∈ s) (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s ↔ [x -[𝕜] y] ⊆ s := begin refine ⟨λ h z hz, _, (open_segment_subset_segment 𝕜 x y).trans⟩, obtain rfl | hxz := eq_or_ne x z, { exact hx }, obtain rfl | hyz := eq_or_ne y z, { exact hy }, exact h (mem_open_segment_of_ne_left_right 𝕜 hxz hyz hz), end lemma convex.combo_self {a b : 𝕜} (h : a + b = 1) (x : E) : a • x + b • x = x := by rw [←add_smul, h, one_smul] end module end ordered_semiring open_locale convex section ordered_ring variables [ordered_ring 𝕜] section add_comm_group variables (𝕜) [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] section densely_ordered variables [nontrivial 𝕜] [densely_ordered 𝕜] @[simp] lemma open_segment_same (x : E) : open_segment 𝕜 x x = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [← add_smul, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ (h : z = x), begin obtain ⟨a, ha₀, ha₁⟩ := densely_ordered.dense (0 : 𝕜) 1 zero_lt_one, refine ⟨a, 1 - a, ha₀, sub_pos_of_lt ha₁, add_sub_cancel'_right _ _, _⟩, rw [←add_smul, add_sub_cancel'_right, one_smul, h], end⟩ end densely_ordered lemma segment_eq_image (x y : E) : [x -[𝕜] y] = (λ θ : 𝕜, (1 - θ) • x + θ • y) '' Icc (0 : 𝕜) 1 := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ le_add_of_nonneg_left ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1-θ, θ, sub_nonneg.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma open_segment_eq_image (x y : E) : open_segment 𝕜 x y = (λ (θ : 𝕜), (1 - θ) • x + θ • y) '' Ioo (0 : 𝕜) 1 := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ lt_add_of_pos_left _ ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1 - θ, θ, sub_pos.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_image₂ (x y : E) : [x -[𝕜] y] = (λ p : 𝕜 × 𝕜, p.1 • x + p.2 • y) '' {p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma open_segment_eq_image₂ (x y : E) : open_segment 𝕜 x y = (λ p : 𝕜 × 𝕜, p.1 • x + p.2 • y) '' {p | 0 < p.1 ∧ 0 < p.2 ∧ p.1 + p.2 = 1} := by simp only [open_segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_eq_image' (x y : E) : [x -[𝕜] y] = (λ (θ : 𝕜), x + θ • (y - x)) '' Icc (0 : 𝕜) 1 := by { convert segment_eq_image 𝕜 x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma open_segment_eq_image' (x y : E) : open_segment 𝕜 x y = (λ (θ : 𝕜), x + θ • (y - x)) '' Ioo (0 : 𝕜) 1 := by { convert open_segment_eq_image 𝕜 x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_image (f : E →ₗ[𝕜] F) (a b : E) : f '' [a -[𝕜] b] = [f a -[𝕜] f b] := set.ext (λ x, by simp_rw [segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul]) @[simp] lemma open_segment_image (f : E →ₗ[𝕜] F) (a b : E) : f '' open_segment 𝕜 a b = open_segment 𝕜 (f a) (f b) := set.ext (λ x, by simp_rw [open_segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul]) lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b -[𝕜] a + c] ↔ x ∈ [b -[𝕜] c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj], end @[simp] lemma mem_open_segment_translate (a : E) {x b c : E} : a + x ∈ open_segment 𝕜 (a + b) (a + c) ↔ x ∈ open_segment 𝕜 b c := begin rw [open_segment_eq_image', open_segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj], end lemma segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' [a + b -[𝕜] a + c] = [b -[𝕜] c] := set.ext $ λ x, mem_segment_translate 𝕜 a lemma open_segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' open_segment 𝕜 (a + b) (a + c) = open_segment 𝕜 b c := set.ext $ λ x, mem_open_segment_translate 𝕜 a lemma segment_translate_image (a b c : E) : (λ x, a + x) '' [b -[𝕜] c] = [a + b -[𝕜] a + c] := segment_translate_preimage 𝕜 a b c ▸ image_preimage_eq _ $ add_left_surjective a lemma open_segment_translate_image (a b c : E) : (λ x, a + x) '' open_segment 𝕜 b c = open_segment 𝕜 (a + b) (a + c) := open_segment_translate_preimage 𝕜 a b c ▸ image_preimage_eq _ $ add_left_surjective a end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] @[simp] lemma left_mem_open_segment_iff [no_zero_smul_divisors 𝕜 E] {x y : E} : x ∈ open_segment 𝕜 x y ↔ x = y := begin split, { rintro ⟨a, b, ha, hb, hab, hx⟩, refine smul_right_injective _ hb.ne' ((add_right_inj (a • x)).1 _), rw [hx, ←add_smul, hab, one_smul] }, { rintro rfl, rw open_segment_same, exact mem_singleton _ } end @[simp] lemma right_mem_open_segment_iff {x y : E} : y ∈ open_segment 𝕜 x y ↔ x = y := by rw [open_segment_symm, left_mem_open_segment_iff, eq_comm] end add_comm_group end linear_ordered_field /-! #### Segments in an ordered space Relates `segment`, `open_segment` and `set.Icc`, `set.Ico`, `set.Ioc`, `set.Ioo` -/ section ordered_semiring variables [ordered_semiring 𝕜] section ordered_add_comm_monoid variables [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] lemma segment_subset_Icc {x y : E} (h : x ≤ y) : [x -[𝕜] y] ⊆ Icc x y := begin rintro z ⟨a, b, ha, hb, hab, rfl⟩, split, calc x = a • x + b • x :(convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_left (smul_le_smul_of_nonneg h hb) _, calc a • x + b • y ≤ a • y + b • y : add_le_add_right (smul_le_smul_of_nonneg h ha) _ ... = y : convex.combo_self hab _, end end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] lemma open_segment_subset_Ioo {x y : E} (h : x < y) : open_segment 𝕜 x y ⊆ Ioo x y := begin rintro z ⟨a, b, ha, hb, hab, rfl⟩, split, calc x = a • x + b • x : (convex.combo_self hab _).symm ... < a • x + b • y : add_lt_add_left (smul_lt_smul_of_pos h hb) _, calc a • x + b • y < a • y + b • y : add_lt_add_right (smul_lt_smul_of_pos h ha) _ ... = y : convex.combo_self hab _, end end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {𝕜} lemma segment_subset_interval (x y : E) : [x -[𝕜] y] ⊆ interval x y := begin cases le_total x y, { rw interval_of_le h, exact segment_subset_Icc h }, { rw [interval_of_ge h, segment_symm], exact segment_subset_Icc h } end lemma convex.min_le_combo (x y : E) {a b : 𝕜} (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min x y ≤ a • x + b • y := (segment_subset_interval x y ⟨_, _, ha, hb, hab, rfl⟩).1 lemma convex.combo_le_max (x y : E) {a b : 𝕜} (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : a • x + b • y ≤ max x y := (segment_subset_interval x y ⟨_, _, ha, hb, hab, rfl⟩).2 end linear_ordered_add_comm_monoid end ordered_semiring section linear_ordered_field variables [linear_ordered_field 𝕜] lemma Icc_subset_segment {x y : 𝕜} : Icc x y ⊆ [x -[𝕜] y] := begin rintro z ⟨hxz, hyz⟩, obtain rfl | h := (hxz.trans hyz).eq_or_lt, { rw segment_same, exact hyz.antisymm hxz }, rw ←sub_nonneg at hxz hyz, rw ←sub_pos at h, refine ⟨(y - z) / (y - x), (z - x) / (y - x), div_nonneg hyz h.le, div_nonneg hxz h.le, _, _⟩, { rw [←add_div, sub_add_sub_cancel, div_self h.ne'] }, { rw [smul_eq_mul, smul_eq_mul, ←mul_div_right_comm, ←mul_div_right_comm, ←add_div, div_eq_iff h.ne', add_comm, sub_mul, sub_mul, mul_comm x, sub_add_sub_cancel, mul_sub] } end @[simp] lemma segment_eq_Icc {x y : 𝕜} (h : x ≤ y) : [x -[𝕜] y] = Icc x y := (segment_subset_Icc h).antisymm Icc_subset_segment lemma Ioo_subset_open_segment {x y : 𝕜} : Ioo x y ⊆ open_segment 𝕜 x y := λ z hz, mem_open_segment_of_ne_left_right _ hz.1.ne hz.2.ne' (Icc_subset_segment $ Ioo_subset_Icc_self hz) @[simp] lemma open_segment_eq_Ioo {x y : 𝕜} (h : x < y) : open_segment 𝕜 x y = Ioo x y := (open_segment_subset_Ioo h).antisymm Ioo_subset_open_segment lemma segment_eq_Icc' (x y : 𝕜) : [x -[𝕜] y] = Icc (min x y) (max x y) := begin cases le_total x y, { rw [segment_eq_Icc h, max_eq_right h, min_eq_left h] }, { rw [segment_symm, segment_eq_Icc h, max_eq_left h, min_eq_right h] } end lemma open_segment_eq_Ioo' {x y : 𝕜} (hxy : x ≠ y) : open_segment 𝕜 x y = Ioo (min x y) (max x y) := begin cases hxy.lt_or_lt, { rw [open_segment_eq_Ioo h, max_eq_right h.le, min_eq_left h.le] }, { rw [open_segment_symm, open_segment_eq_Ioo h, max_eq_left h.le, min_eq_right h.le] } end lemma segment_eq_interval (x y : 𝕜) : [x -[𝕜] y] = interval x y := segment_eq_Icc' _ _ /-- A point is in an `Icc` iff it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Icc {x y : 𝕜} (h : x ≤ y) {z : 𝕜} : z ∈ Icc x y ↔ ∃ (a b : 𝕜), 0 ≤ a ∧ 0 ≤ b ∧ a + b = 1 ∧ a * x + b * y = z := begin rw ←segment_eq_Icc h, simp_rw [←exists_prop], refl, end /-- A point is in an `Ioo` iff it can be expressed as a strict convex combination of the endpoints. -/ lemma convex.mem_Ioo {x y : 𝕜} (h : x < y) {z : 𝕜} : z ∈ Ioo x y ↔ ∃ (a b : 𝕜), 0 < a ∧ 0 < b ∧ a + b = 1 ∧ a * x + b * y = z := begin rw ←open_segment_eq_Ioo h, simp_rw [←exists_prop], refl, end /-- A point is in an `Ioc` iff it can be expressed as a semistrict convex combination of the endpoints. -/ lemma convex.mem_Ioc {x y : 𝕜} (h : x < y) {z : 𝕜} : z ∈ Ioc x y ↔ ∃ (a b : 𝕜), 0 ≤ a ∧ 0 < b ∧ a + b = 1 ∧ a * x + b * y = z := begin split, { rintro hz, obtain ⟨a, b, ha, hb, hab, rfl⟩ := (convex.mem_Icc h.le).1 (Ioc_subset_Icc_self hz), obtain rfl | hb' := hb.eq_or_lt, { rw add_zero at hab, rw [hab, one_mul, zero_mul, add_zero] at hz, exact (hz.1.ne rfl).elim }, { exact ⟨a, b, ha, hb', hab, rfl⟩ } }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rwa [hab, one_mul, zero_mul, zero_add, right_mem_Ioc] }, { exact Ioo_subset_Ioc_self ((convex.mem_Ioo h).2 ⟨a, b, ha', hb, hab, rfl⟩) } } end /-- A point is in an `Ico` iff it can be expressed as a semistrict convex combination of the endpoints. -/ lemma convex.mem_Ico {x y : 𝕜} (h : x < y) {z : 𝕜} : z ∈ Ico x y ↔ ∃ (a b : 𝕜), 0 < a ∧ 0 ≤ b ∧ a + b = 1 ∧ a * x + b * y = z := begin split, { rintro hz, obtain ⟨a, b, ha, hb, hab, rfl⟩ := (convex.mem_Icc h.le).1 (Ico_subset_Icc_self hz), obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rw [hab, one_mul, zero_mul, zero_add] at hz, exact (hz.2.ne rfl).elim }, { exact ⟨a, b, ha', hb, hab, rfl⟩ } }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, obtain rfl | hb' := hb.eq_or_lt, { rw add_zero at hab, rwa [hab, one_mul, zero_mul, add_zero, left_mem_Ico] }, { exact Ioo_subset_Ico_self ((convex.mem_Ioo h).2 ⟨a, b, ha, hb', hab, rfl⟩) } } end end linear_ordered_field /-! ### Convexity of sets -/ section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section has_scalar variables (𝕜) [has_scalar 𝕜 E] [has_scalar 𝕜 F] (s : set E) /-- Convexity of sets. -/ def convex : Prop := ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s variables {𝕜 s} lemma convex_iff_segment_subset : convex 𝕜 s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → [x -[𝕜] y] ⊆ s := begin split, { rintro h x y hx hy z ⟨a, b, ha, hb, hab, rfl⟩, exact h hx hy ha hb hab }, { rintro h x y hx hy a b ha hb hab, exact h hx hy ⟨a, b, ha, hb, hab, rfl⟩ } end lemma convex.segment_subset (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : [x -[𝕜] y] ⊆ s := convex_iff_segment_subset.1 h hx hy lemma convex.open_segment_subset (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s := (open_segment_subset_segment 𝕜 x y).trans (h.segment_subset hx hy) /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff_pointwise_add_subset : convex 𝕜 s ↔ ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s := iff.intro begin rintro hA a b ha hb hab w ⟨au, bv, ⟨u, hu, rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (λ h x y hx hy a b ha hb hab, (h ha hb hab) (set.add_mem_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) lemma convex_empty : convex 𝕜 (∅ : set E) := by finish lemma convex_univ : convex 𝕜 (set.univ : set E) := λ _ _ _ _ _ _ _ _ _, trivial lemma convex.inter {t : set E} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s ∩ t) := λ x y (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) a b (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex 𝕜 s) : convex 𝕜 (⋂₀ S) := assume x y hx hy a b ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {ι : Sort*} {s : ι → set E} (h : ∀ i : ι, convex 𝕜 (s i)) : convex 𝕜 (⋂ i, s i) := (sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h lemma convex.prod {s : set E} {t : set F} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s.prod t) := begin intros x y hx hy a b ha hb hab, apply mem_prod.2, exact ⟨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma directed.convex_Union {ι : Sort*} {s : ι → set E} (hdir : directed (⊆) s) (hc : ∀ ⦃i : ι⦄, convex 𝕜 (s i)) : convex 𝕜 (⋃ i, s i) := begin rintro x y hx hy a b ha hb hab, rw mem_Union at ⊢ hx hy, obtain ⟨i, hx⟩ := hx, obtain ⟨j, hy⟩ := hy, obtain ⟨k, hik, hjk⟩ := hdir i j, exact ⟨k, hc (hik hx) (hjk hy) ha hb hab⟩, end lemma directed_on.convex_sUnion {c : set (set E)} (hdir : directed_on (⊆) c) (hc : ∀ ⦃A : set E⦄, A ∈ c → convex 𝕜 A) : convex 𝕜 (⋃₀c) := begin rw sUnion_eq_Union, exact (directed_on_iff_directed.1 hdir).convex_Union (λ A, hc A.2), end end has_scalar section module variables [module 𝕜 E] [module 𝕜 F] {s : set E} lemma convex_iff_forall_pos : convex 𝕜 s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h x y hx hy a b ha hb hab, h hx hy ha.le hb.le hab, _⟩, intros h x y hx hy a b ha hb hab, cases ha.eq_or_lt with ha ha, { subst a, rw [zero_add] at hab, simp [hab, hy] }, cases hb.eq_or_lt with hb hb, { subst b, rw [add_zero] at hab, simp [hab, hx] }, exact h hx hy ha hb hab end lemma convex_iff_pairwise_on_pos : convex 𝕜 s ↔ s.pairwise_on (λ x y, ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s) := begin refine ⟨λ h x hx y hy _ a b ha hb hab, h hx hy ha.le hb.le hab, _⟩, intros h x y hx hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_add] at hab, rwa [hab, zero_smul, one_smul, zero_add] }, obtain rfl | hb' := hb.eq_or_lt, { rw [add_zero] at hab, rwa [hab, zero_smul, one_smul, add_zero] }, obtain rfl | hxy := eq_or_ne x y, { rwa convex.combo_self hab }, exact h _ hx _ hy hxy ha' hb' hab, end lemma convex_iff_open_segment_subset : convex 𝕜 s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → open_segment 𝕜 x y ⊆ s := begin rw convex_iff_segment_subset, exact forall₂_congr (λ x y, forall₂_congr $ λ hx hy, (open_segment_subset_iff_segment_subset hx hy).symm), end lemma convex_singleton (c : E) : convex 𝕜 ({c} : set E) := begin intros x y hx hy a b ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab, one_smul], exact mem_singleton c end lemma convex.linear_image (hs : convex 𝕜 s) (f : E →ₗ[𝕜] F) : convex 𝕜 (s.image f) := begin intros x y hx hy a b ha hb hab, obtain ⟨x', hx', rfl⟩ := mem_image_iff_bex.1 hx, obtain ⟨y', hy', rfl⟩ := mem_image_iff_bex.1 hy, exact ⟨a • x' + b • y', hs hx' hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]⟩, end lemma convex.is_linear_image (hs : convex 𝕜 s) {f : E → F} (hf : is_linear_map 𝕜 f) : convex 𝕜 (f '' s) := hs.linear_image $ hf.mk' f lemma convex.linear_preimage {s : set F} (hs : convex 𝕜 s) (f : E →ₗ[𝕜] F) : convex 𝕜 (s.preimage f) := begin intros x y hx hy a b ha hb hab, rw [mem_preimage, f.map_add, f.map_smul, f.map_smul], exact hs hx hy ha hb hab, end lemma convex.is_linear_preimage {s : set F} (hs : convex 𝕜 s) {f : E → F} (hf : is_linear_map 𝕜 f) : convex 𝕜 (preimage f s) := hs.linear_preimage $ hf.mk' f lemma convex.add {t : set E} (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 (s + t) := by { rw ← add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma convex.translate (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, z + x) '' s) := begin intros x y hx hy a b ha hb hab, obtain ⟨x', hx', rfl⟩ := mem_image_iff_bex.1 hx, obtain ⟨y', hy', rfl⟩ := mem_image_iff_bex.1 hy, refine ⟨a • x' + b • y', hs hx' hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_right (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, z + x) ⁻¹' s) := begin intros x y hx hy a b ha hb hab, have h := hs hx hy ha hb hab, rwa [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul] at h, end /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_left (hs : convex 𝕜 s) (z : E) : convex 𝕜 ((λ x, x + z) ⁻¹' s) := by simpa only [add_comm] using hs.translate_preimage_right z section ordered_add_comm_monoid variables [ordered_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_Iic (r : β) : convex 𝕜 (Iic r) := λ x y hx hy a b ha hb hab, calc a • x + b • y ≤ a • r + b • r : add_le_add (smul_le_smul_of_nonneg hx ha) (smul_le_smul_of_nonneg hy hb) ... = r : convex.combo_self hab _ lemma convex_Ici (r : β) : convex 𝕜 (Ici r) := @convex_Iic 𝕜 (order_dual β) _ _ _ _ r lemma convex_Icc (r s : β) : convex 𝕜 (Icc r s) := Ici_inter_Iic.subst ((convex_Ici r).inter $ convex_Iic s) lemma convex_halfspace_le {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | f w ≤ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_ge {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | r ≤ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | f w = r} := begin simp_rw le_antisymm_iff, exact (convex_halfspace_le h r).inter (convex_halfspace_ge h r), end end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_Iio (r : β) : convex 𝕜 (Iio r) := begin intros x y hx hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rwa [zero_smul, zero_add, hab, one_smul] }, rw mem_Iio at hx hy, calc a • x + b • y < a • r + b • r : add_lt_add_of_lt_of_le (smul_lt_smul_of_pos hx ha') (smul_le_smul_of_nonneg hy.le hb) ... = r : convex.combo_self hab _ end lemma convex_Ioi (r : β) : convex 𝕜 (Ioi r) := @convex_Iio 𝕜 (order_dual β) _ _ _ _ r lemma convex_Ioo (r s : β) : convex 𝕜 (Ioo r s) := Ioi_inter_Iio.subst ((convex_Ioi r).inter $ convex_Iio s) lemma convex_Ico (r s : β) : convex 𝕜 (Ico r s) := Ici_inter_Iio.subst ((convex_Ici r).inter $ convex_Iio s) lemma convex_Ioc (r s : β) : convex 𝕜 (Ioc r s) := Ioi_inter_Iic.subst ((convex_Ioi r).inter $ convex_Iic s) lemma convex_halfspace_lt {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_gt {f : E → β} (h : is_linear_map 𝕜 f) (r : β) : convex 𝕜 {w | r < f w} := (convex_Ioi r).is_linear_preimage h end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid β] [module 𝕜 β] [ordered_smul 𝕜 β] lemma convex_interval (r s : β) : convex 𝕜 (interval r s) := convex_Icc _ _ end linear_ordered_add_comm_monoid end module end add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [ordered_add_comm_monoid β] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} {f : E → β} lemma monotone_on.convex_le (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x ≤ r} := λ x y hx hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab, (hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans (max_rec' _ hx.2 hy.2)⟩ lemma monotone_on.convex_lt (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x < r} := λ x y hx hy a b ha hb hab, ⟨hs hx.1 hy.1 ha hb hab, (hf (hs hx.1 hy.1 ha hb hab) (max_rec' s hx.1 hy.1) (convex.combo_le_max x y ha hb hab)).trans_lt (max_rec' _ hx.2 hy.2)⟩ lemma monotone_on.convex_ge (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r ≤ f x} := @monotone_on.convex_le 𝕜 (order_dual E) (order_dual β) _ _ _ _ _ _ _ hf.dual hs r lemma monotone_on.convex_gt (hf : monotone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r < f x} := @monotone_on.convex_lt 𝕜 (order_dual E) (order_dual β) _ _ _ _ _ _ _ hf.dual hs r lemma antitone_on.convex_le (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x ≤ r} := @monotone_on.convex_ge 𝕜 E (order_dual β) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_lt (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | f x < r} := @monotone_on.convex_gt 𝕜 E (order_dual β) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_ge (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r ≤ f x} := @monotone_on.convex_le 𝕜 E (order_dual β) _ _ _ _ _ _ _ hf hs r lemma antitone_on.convex_gt (hf : antitone_on f s) (hs : convex 𝕜 s) (r : β) : convex 𝕜 {x ∈ s | r < f x} := @monotone_on.convex_lt 𝕜 E (order_dual β) _ _ _ _ _ _ _ hf hs r lemma monotone.convex_le (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma monotone.convex_lt (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma monotone.convex_ge (hf : monotone f ) (r : β) : convex 𝕜 {x | r ≤ f x} := set.sep_univ.subst ((hf.monotone_on univ).convex_ge convex_univ r) lemma monotone.convex_gt (hf : monotone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.monotone_on univ).convex_le convex_univ r) lemma antitone.convex_le (hf : antitone f) (r : β) : convex 𝕜 {x | f x ≤ r} := set.sep_univ.subst ((hf.antitone_on univ).convex_le convex_univ r) lemma antitone.convex_lt (hf : antitone f) (r : β) : convex 𝕜 {x | f x < r} := set.sep_univ.subst ((hf.antitone_on univ).convex_lt convex_univ r) lemma antitone.convex_ge (hf : antitone f) (r : β) : convex 𝕜 {x | r ≤ f x} := set.sep_univ.subst ((hf.antitone_on univ).convex_ge convex_univ r) lemma antitone.convex_gt (hf : antitone f) (r : β) : convex 𝕜 {x | r < f x} := set.sep_univ.subst ((hf.antitone_on univ).convex_gt convex_univ r) end linear_ordered_add_comm_monoid section add_comm_group variables [add_comm_group E] [module 𝕜 E] {s t : set E} lemma convex.combo_eq_vadd {a b : 𝕜} {x y : E} (h : a + b = 1) : a • x + b • y = b • (y - x) + x := calc a • x + b • y = (b • y - b • x) + (a • x + b • x) : by abel ... = b • (y - x) + x : by rw [smul_sub, convex.combo_self h] lemma convex.sub (hs : convex 𝕜 s) (ht : convex 𝕜 t) : convex 𝕜 ((λ x : E × E, x.1 - x.2) '' (s.prod t)) := (hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub lemma convex_segment (x y : E) : convex 𝕜 [x -[𝕜] y] := begin rintro p q ⟨ap, bp, hap, hbp, habp, rfl⟩ ⟨aq, bq, haq, hbq, habq, rfl⟩ a b ha hb hab, refine ⟨a * ap + b * aq, a * bp + b * bq, add_nonneg (mul_nonneg ha hap) (mul_nonneg hb haq), add_nonneg (mul_nonneg ha hbp) (mul_nonneg hb hbq), _, _⟩, { rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] }, { simp_rw [add_smul, mul_smul, smul_add], exact add_add_add_comm _ _ _ _ } end lemma convex_open_segment (a b : E) : convex 𝕜 (open_segment 𝕜 a b) := begin rw convex_iff_open_segment_subset, rintro p q ⟨ap, bp, hap, hbp, habp, rfl⟩ ⟨aq, bq, haq, hbq, habq, rfl⟩ z ⟨a, b, ha, hb, hab, rfl⟩, refine ⟨a * ap + b * aq, a * bp + b * bq, add_pos (mul_pos ha hap) (mul_pos hb haq), add_pos (mul_pos ha hbp) (mul_pos hb hbq), _, _⟩, { rw [add_add_add_comm, ←mul_add, ←mul_add, habp, habq, mul_one, mul_one, hab] }, { simp_rw [add_smul, mul_smul, smul_add], exact add_add_add_comm _ _ _ _ } end end add_comm_group end ordered_semiring section ordered_comm_semiring variables [ordered_comm_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] [module 𝕜 E] [module 𝕜 F] {s : set E} lemma convex.smul (hs : convex 𝕜 s) (c : 𝕜) : convex 𝕜 (c • s) := hs.linear_image (linear_map.lsmul _ _ c) lemma convex.smul_preimage (hs : convex 𝕜 s) (c : 𝕜) : convex 𝕜 ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma convex.affinity (hs : convex 𝕜 s) (z : E) (c : 𝕜) : convex 𝕜 ((λ x, z + c • x) '' s) := begin have h := (hs.smul c).translate z, rwa [←image_smul, image_image] at h, end end add_comm_monoid end ordered_comm_semiring section ordered_ring variables [ordered_ring 𝕜] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {s : set E} lemma convex.add_smul_mem (hs : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : x + y ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : x + t • y ∈ s := begin have h : x + t • y = (1 - t) • x + t • (x + y), { rw [smul_add, ←add_assoc, ←add_smul, sub_add_cancel, one_smul] }, rw h, exact hs hx hy (sub_nonneg_of_le ht.2) ht.1 (sub_add_cancel _ _), end lemma convex.smul_mem_of_zero_mem (hs : convex 𝕜 s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : t • x ∈ s := by simpa using hs.add_smul_mem zero_mem (by simpa using hx) ht lemma convex.add_smul_sub_mem (h : convex 𝕜 s) {x y : E} (hx : x ∈ s) (hy : y ∈ s) {t : 𝕜} (ht : t ∈ Icc (0 : 𝕜) 1) : x + t • (y - x) ∈ s := begin apply h.segment_subset hx hy, rw segment_eq_image', exact mem_image_of_mem _ ht, end /-- Affine subspaces are convex. -/ lemma affine_subspace.convex (Q : affine_subspace 𝕜 E) : convex 𝕜 (Q : set E) := begin intros x y hx hy a b ha hb hab, rw [eq_sub_of_add_eq hab, ← affine_map.line_map_apply_module], exact affine_map.line_map_mem b hx hy, end /-- Applying an affine map to an affine combination of two points yields an affine combination of the images. -/ lemma convex.combo_affine_apply {a b : 𝕜} {x y : E} {f : E →ᵃ[𝕜] F} (h : a + b = 1) : f (a • x + b • y) = a • f x + b • f y := begin simp only [convex.combo_eq_vadd h, ← vsub_eq_sub], exact f.apply_line_map _ _ _, end /-- The preimage of a convex set under an affine map is convex. -/ lemma convex.affine_preimage (f : E →ᵃ[𝕜] F) {s : set F} (hs : convex 𝕜 s) : convex 𝕜 (f ⁻¹' s) := begin intros x y xs ys a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs xs ys ha hb hab, end /-- The image of a convex set under an affine map is convex. -/ lemma convex.affine_image (f : E →ᵃ[𝕜] F) {s : set E} (hs : convex 𝕜 s) : convex 𝕜 (f '' s) := begin rintro x y ⟨x', ⟨hx', hx'f⟩⟩ ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a • x' + b • y', ⟨hs hx' hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hx'f, hy'f] end lemma convex.neg (hs : convex 𝕜 s) : convex 𝕜 ((λ z, -z) '' s) := hs.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hs : convex 𝕜 s) : convex 𝕜 ((λ z, -z) ⁻¹' s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {s : set E} /-- Alternative definition of set convexity, using division. -/ lemma convex_iff_div : convex 𝕜 s ↔ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ s := ⟨λ h x y hx hy a b ha hb hab, begin apply h hx hy, { have ha', from mul_le_mul_of_nonneg_left ha (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at ha' }, { have hb', from mul_le_mul_of_nonneg_left hb (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at hb' }, { rw ←add_div, exact div_self hab.ne' } end, λ h x y hx hy a b ha hb hab, begin have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ lemma convex.mem_smul_of_zero_mem (h : convex 𝕜 s) {x : E} (zero_mem : (0 : E) ∈ s) (hx : x ∈ s) {t : 𝕜} (ht : 1 ≤ t) : x ∈ t • s := begin rw mem_smul_set_iff_inv_smul_mem₀ (zero_lt_one.trans_le ht).ne', exact h.smul_mem_of_zero_mem zero_mem hx ⟨inv_nonneg.2 (zero_le_one.trans ht), inv_le_one ht⟩, end lemma convex.add_smul (h_conv : convex 𝕜 s) {p q : 𝕜} (hp : 0 ≤ p) (hq : 0 ≤ q) : (p + q) • s = p • s + q • s := begin obtain rfl | hs := s.eq_empty_or_nonempty, { simp_rw [smul_set_empty, add_empty] }, obtain rfl | hp' := hp.eq_or_lt, { rw [zero_add, zero_smul_set hs, zero_add] }, obtain rfl | hq' := hq.eq_or_lt, { rw [add_zero, zero_smul_set hs, add_zero] }, ext, split, { rintro ⟨v, hv, rfl⟩, exact ⟨p • v, q • v, smul_mem_smul_set hv, smul_mem_smul_set hv, (add_smul _ _ _).symm⟩ }, { rintro ⟨v₁, v₂, ⟨v₁₁, h₁₂, rfl⟩, ⟨v₂₁, h₂₂, rfl⟩, rfl⟩, have hpq := add_pos hp' hq', exact mem_smul_set.2 ⟨_, h_conv h₁₂ h₂₂ (div_pos hp' hpq).le (div_pos hq' hpq).le (by rw [←div_self hpq.ne', add_div] : p / (p + q) + q / (p + q) = 1), by simp only [← mul_smul, smul_add, mul_div_cancel' _ hpq.ne']⟩ } end end add_comm_group end linear_ordered_field /-! #### Convex sets in an ordered space Relates `convex` and `ord_connected`. -/ section lemma set.ord_connected.convex_of_chain [ordered_semiring 𝕜] [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} (hs : s.ord_connected) (h : zorn.chain (≤) s) : convex 𝕜 s := begin intros x y hx hy a b ha hb hab, obtain hxy | hyx := h.total_of_refl hx hy, { refine hs.out hx hy (mem_Icc.2 ⟨_, _⟩), calc x = a • x + b • x : (convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_left (smul_le_smul_of_nonneg hxy hb) _, calc a • x + b • y ≤ a • y + b • y : add_le_add_right (smul_le_smul_of_nonneg hxy ha) _ ... = y : convex.combo_self hab _ }, { refine hs.out hy hx (mem_Icc.2 ⟨_, _⟩), calc y = a • y + b • y : (convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_right (smul_le_smul_of_nonneg hyx ha) _, calc a • x + b • y ≤ a • x + b • x : add_le_add_left (smul_le_smul_of_nonneg hyx hb) _ ... = x : convex.combo_self hab _ } end lemma set.ord_connected.convex [ordered_semiring 𝕜] [linear_ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} (hs : s.ord_connected) : convex 𝕜 s := hs.convex_of_chain (zorn.chain_of_trichotomous s) lemma convex_iff_ord_connected [linear_ordered_field 𝕜] {s : set 𝕜} : convex 𝕜 s ↔ s.ord_connected := begin simp_rw [convex_iff_segment_subset, segment_eq_interval, ord_connected_iff_interval_subset], exact forall_congr (λ x, forall_swap) end alias convex_iff_ord_connected ↔ convex.ord_connected _ end /-! #### Convexity of submodules/subspaces -/ section submodule open submodule lemma submodule.convex [ordered_semiring 𝕜] [add_comm_monoid E] [module 𝕜 E] (K : submodule 𝕜 E) : convex 𝕜 (↑K : set E) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex [linear_ordered_field 𝕜] [add_comm_group E] [module 𝕜 E] (K : subspace 𝕜 E) : convex 𝕜 (↑K : set E) := K.convex end submodule /-! ### Simplex -/ section simplex variables (𝕜) (ι : Type*) [ordered_semiring 𝕜] [fintype ι] /-- The standard simplex in the space of functions `ι → 𝕜` is the set of vectors with non-negative coordinates with total sum `1`. This is the free object in the category of convex spaces. -/ def std_simplex : set (ι → 𝕜) := {f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1} lemma std_simplex_eq_inter : std_simplex 𝕜 ι = (⋂ x, {f | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] } lemma convex_std_simplex : convex 𝕜 (std_simplex 𝕜 ι) := begin refine λ f g hf hg a b ha hb hab, ⟨λ x, _, _⟩, { apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] }, { erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2, smul_eq_mul, smul_eq_mul, mul_one, mul_one], exact hab } end variable {ι} lemma ite_eq_mem_std_simplex (i : ι) : (λ j, ite (i = j) (1:𝕜) 0) ∈ std_simplex 𝕜 ι := ⟨λ j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)]⟩ end simplex
b8cd3b163147c55c7f66664a803b87444f516db0
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/preadditive/of_biproducts.lean
6deac1483f48fa909f91c2cb50036ac375d9377c
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,856
lean
/- Copyright (c) 2022 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.shapes.biproducts import group_theory.eckmann_hilton /-! # Constructing a semiadditive structure from binary biproducts > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We show that any category with zero morphisms and binary biproducts is enriched over the category of commutative monoids. -/ noncomputable theory universes v u open category_theory open category_theory.limits namespace category_theory.semiadditive_of_binary_biproducts variables {C : Type u} [category.{v} C] [has_zero_morphisms C] [has_binary_biproducts C] section variables (X Y : C) /-- `f +ₗ g` is the composite `X ⟶ Y ⊞ Y ⟶ Y`, where the first map is `(f, g)` and the second map is `(𝟙 𝟙)`. -/ @[simp] def left_add (f g : X ⟶ Y) : X ⟶ Y := biprod.lift f g ≫ biprod.desc (𝟙 Y) (𝟙 Y) /-- `f +ᵣ g` is the composite `X ⟶ X ⊞ X ⟶ Y`, where the first map is `(𝟙, 𝟙)` and the second map is `(f g)`. -/ @[simp] def right_add (f g : X ⟶ Y) : X ⟶ Y := biprod.lift (𝟙 X) (𝟙 X) ≫ biprod.desc f g local infixr ` +ₗ `:65 := left_add X Y local infixr ` +ᵣ `:65 := right_add X Y lemma is_unital_left_add : eckmann_hilton.is_unital (+ₗ) 0 := ⟨⟨λ f, by simp [show biprod.lift (0 : X ⟶ Y) f = f ≫ biprod.inr, by ext; simp]⟩, ⟨λ f, by simp [show biprod.lift f (0 : X ⟶ Y) = f ≫ biprod.inl, by ext; simp]⟩⟩ lemma is_unital_right_add : eckmann_hilton.is_unital (+ᵣ) 0 := ⟨⟨λ f, by simp [show biprod.desc (0 : X ⟶ Y) f = biprod.snd ≫ f, by ext; simp]⟩, ⟨λ f, by simp [show biprod.desc f (0 : X ⟶ Y) = biprod.fst ≫ f, by ext; simp]⟩⟩ lemma distrib (f g h k : X ⟶ Y) : (f +ᵣ g) +ₗ (h +ᵣ k) = (f +ₗ h) +ᵣ (g +ₗ k) := begin let diag : X ⊞ X ⟶ Y ⊞ Y := biprod.lift (biprod.desc f g) (biprod.desc h k), have hd₁ : biprod.inl ≫ diag = biprod.lift f h := by { ext; simp }, have hd₂ : biprod.inr ≫ diag = biprod.lift g k := by { ext; simp }, have h₁ : biprod.lift (f +ᵣ g) (h +ᵣ k) = biprod.lift (𝟙 X) (𝟙 X) ≫ diag := by { ext; simp }, have h₂ : diag ≫ biprod.desc (𝟙 Y) (𝟙 Y) = biprod.desc (f +ₗ h) (g +ₗ k), { ext; simp [reassoc_of hd₁, reassoc_of hd₂] }, rw [left_add, h₁, category.assoc, h₂, right_add] end /-- In a category with binary biproducts, the morphisms form a commutative monoid. -/ def add_comm_monoid_hom_of_has_binary_biproducts : add_comm_monoid (X ⟶ Y) := { add := (+ᵣ), add_assoc := (eckmann_hilton.mul_assoc (is_unital_left_add X Y) (is_unital_right_add X Y) (distrib X Y)).assoc, zero := 0, zero_add := (is_unital_right_add X Y).left_id, add_zero := (is_unital_right_add X Y).right_id, add_comm := (eckmann_hilton.mul_comm (is_unital_left_add X Y) (is_unital_right_add X Y) (distrib X Y)).comm } end section variables {X Y Z : C} local attribute [instance] add_comm_monoid_hom_of_has_binary_biproducts lemma add_eq_right_addition (f g : X ⟶ Y) : f + g = biprod.lift (𝟙 X) (𝟙 X) ≫ biprod.desc f g := rfl lemma add_eq_left_addition (f g : X ⟶ Y) : f + g = biprod.lift f g ≫ biprod.desc (𝟙 Y) (𝟙 Y) := congr_fun₂ (eckmann_hilton.mul (is_unital_left_add X Y) (is_unital_right_add X Y) (distrib X Y)).symm f g lemma add_comp (f g : X ⟶ Y) (h : Y ⟶ Z) : (f + g) ≫ h = f ≫ h + g ≫ h := by { simp only [add_eq_right_addition, category.assoc], congr, ext; simp } lemma comp_add (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g + h) = f ≫ g + f ≫ h := by { simp only [add_eq_left_addition, ← category.assoc], congr, ext; simp } end end category_theory.semiadditive_of_binary_biproducts
1a1418e98248fc56244d8fbe3904c609820cfcc8
0942a74cc0b397d09bcd1059762fa076b1a5d308
/src/less-trivial.lean
9ee4a8ed28772452c9d9c2a17bfb5af8e36d7d80
[ "Unlicense" ]
permissive
sguzman/lean-examples
81df2010e204be1a9fde6ae18295e299e69cebca
c7428b2982d0468d0adb4453766a27e1550a72e8
refs/heads/master
1,668,438,655,034
1,594,156,605,000
1,594,156,605,000
277,626,098
0
0
null
null
null
null
UTF-8
Lean
false
false
640
lean
-- anything after two minus signs like this is a comment. variables P Q R : Prop -- P, Q, R are now true/false statements. -- Reminder : ∧ means "and". theorem basic_logic : (P → Q) ∧ (Q → R) → (P → R) := begin intro H, -- H is "P implies Q and Q implies R" have HPQ : P → Q, exact H.left, have HQR : Q → R, exact H.right, clear H, -- we don't need it any more -- We now have hypothesis HPQ, which is P -> Q -- and hypothesis HQR, which is Q -> R. -- We want to prove P -> R so let's assume P -- and deduce R. intro HP, have HQ : Q, exact HPQ HP, -- apply P->Q to P to get Q. -- now we can get the goal exact HQR HQ end
cd2f8096a336e2c91bc48070d6a2e6e298ba086d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/graded_algebra/radical.lean
a4953692a153bf58ce1b86b16283610c6103304a
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
8,838
lean
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Eric Wieser -/ import ring_theory.graded_algebra.homogeneous_ideal /-! This file contains a proof that the radical of any homogeneous ideal is a homogeneous ideal ## Main statements * `ideal.is_homogeneous.is_prime_iff`: for any `I : ideal A`, if `I` is homogeneous, then `I` is prime if and only if `I` is homogeneously prime, i.e. `I ≠ ⊤` and if `x, y` are homogeneous elements such that `x * y ∈ I`, then at least one of `x,y` is in `I`. * `ideal.is_prime.homogeneous_core`: for any `I : ideal A`, if `I` is prime, then `I.homogeneous_core 𝒜` (i.e. the largest homogeneous ideal contained in `I`) is also prime. * `ideal.is_homogeneous.radical`: for any `I : ideal A`, if `I` is homogeneous, then the radical of `I` is homogeneous as well. * `homogeneous_ideal.radical`: for any `I : homogeneous_ideal 𝒜`, `I.radical` is the the radical of `I` as a `homogeneous_ideal 𝒜` ## Implementation details Throughout this file, the indexing type `ι` of grading is assumed to be a `linear_ordered_cancel_add_comm_monoid`. This might be stronger than necessary but cancelling property is strictly necessary; for a counterexample of how `ideal.is_homogeneous.is_prime_iff` fails for a non-cancellative set see `counterexample/homogeneous_prime_not_prime.lean`. ## Tags homogeneous, radical -/ open graded_ring direct_sum set_like finset open_locale big_operators variables {ι σ A : Type*} variables [comm_ring A] variables [linear_ordered_cancel_add_comm_monoid ι] variables [set_like σ A] [add_submonoid_class σ A] {𝒜 : ι → σ} [graded_ring 𝒜] include A lemma ideal.is_homogeneous.is_prime_of_homogeneous_mem_or_mem {I : ideal A} (hI : I.is_homogeneous 𝒜) (I_ne_top : I ≠ ⊤) (homogeneous_mem_or_mem : ∀ {x y : A}, is_homogeneous 𝒜 x → is_homogeneous 𝒜 y → (x * y ∈ I → x ∈ I ∨ y ∈ I)) : ideal.is_prime I := ⟨I_ne_top, begin intros x y hxy, by_contradiction rid, obtain ⟨rid₁, rid₂⟩ := not_or_distrib.mp rid, /- The idea of the proof is the following : since `x * y ∈ I` and `I` homogeneous, then `proj i (x * y) ∈ I` for any `i : ι`. Then consider two sets `{i ∈ x.support | xᵢ ∉ I}` and `{j ∈ y.support | yⱼ ∉ J}`; let `max₁, max₂` be the maximum of the two sets, then `proj (max₁ + max₂) (x * y) ∈ I`. Then, `proj max₁ x ∉ I` and `proj max₂ j ∉ I` but `proj i x ∈ I` for all `max₁ < i` and `proj j y ∈ I` for all `max₂ < j`. ` proj (max₁ + max₂) (x * y)` `= ∑ {(i, j) ∈ supports | i + j = max₁ + max₂}, xᵢ * yⱼ` `= proj max₁ x * proj max₂ y` ` + ∑ {(i, j) ∈ supports \ {(max₁, max₂)} | i + j = max₁ + max₂}, xᵢ * yⱼ`. This is a contradiction, because both `proj (max₁ + max₂) (x * y) ∈ I` and the sum on the right hand side is in `I` however `proj max₁ x * proj max₂ y` is not in `I`. -/ classical, set set₁ := (decompose 𝒜 x).support.filter (λ i, proj 𝒜 i x ∉ I) with set₁_eq, set set₂ := (decompose 𝒜 y).support.filter (λ i, proj 𝒜 i y ∉ I) with set₂_eq, have nonempty : ∀ (x : A), (x ∉ I) → ((decompose 𝒜 x).support.filter (λ i, proj 𝒜 i x ∉ I)).nonempty, { intros x hx, rw filter_nonempty_iff, contrapose! hx, simp_rw proj_apply at hx, rw ← sum_support_decompose 𝒜 x, exact ideal.sum_mem _ hx, }, set max₁ := set₁.max' (nonempty x rid₁) with max₁_eq, set max₂ := set₂.max' (nonempty y rid₂) with max₂_eq, have mem_max₁ : max₁ ∈ set₁ := max'_mem set₁ (nonempty x rid₁), have mem_max₂ : max₂ ∈ set₂ := max'_mem set₂ (nonempty y rid₂), replace hxy : proj 𝒜 (max₁ + max₂) (x * y) ∈ I := hI _ hxy, have mem_I : proj 𝒜 max₁ x * proj 𝒜 max₂ y ∈ I, { set antidiag := ((decompose 𝒜 x).support ×ˢ (decompose 𝒜 y).support) .filter (λ z : ι × ι, z.1 + z.2 = max₁ + max₂) with ha, have mem_antidiag : (max₁, max₂) ∈ antidiag, { simp only [add_sum_erase, mem_filter, mem_product], exact ⟨⟨mem_of_mem_filter _ mem_max₁, mem_of_mem_filter _ mem_max₂⟩, rfl⟩ }, have eq_add_sum := calc proj 𝒜 (max₁ + max₂) (x * y) = ∑ ij in antidiag, proj 𝒜 ij.1 x * proj 𝒜 ij.2 y : by simp_rw [ha, proj_apply, direct_sum.decompose_mul, direct_sum.coe_mul_apply 𝒜] ... = proj 𝒜 max₁ x * proj 𝒜 max₂ y + ∑ ij in antidiag.erase (max₁, max₂), proj 𝒜 ij.1 x * proj 𝒜 ij.2 y : (add_sum_erase _ _ mem_antidiag).symm, rw eq_sub_of_add_eq eq_add_sum.symm, refine ideal.sub_mem _ hxy (ideal.sum_mem _ (λ z H, _)), rcases z with ⟨i, j⟩, simp only [mem_erase, prod.mk.inj_iff, ne.def, mem_filter, mem_product] at H, rcases H with ⟨H₁, ⟨H₂, H₃⟩, H₄⟩, have max_lt : max₁ < i ∨ max₂ < j, { rcases lt_trichotomy max₁ i with h | rfl | h, { exact or.inl h }, { refine false.elim (H₁ ⟨rfl, add_left_cancel H₄⟩), }, { apply or.inr, have := add_lt_add_right h j, rw H₄ at this, exact lt_of_add_lt_add_left this, }, }, cases max_lt, { -- in this case `max₁ < i`, then `xᵢ ∈ I`; for otherwise `i ∈ set₁` then `i ≤ max₁`. have not_mem : i ∉ set₁ := λ h, lt_irrefl _ ((max'_lt_iff set₁ (nonempty x rid₁)).mp max_lt i h), rw set₁_eq at not_mem, simp only [not_and, not_not, ne.def, mem_filter] at not_mem, exact ideal.mul_mem_right _ I (not_mem H₂), }, { -- in this case `max₂ < j`, then `yⱼ ∈ I`; for otherwise `j ∈ set₂`, then `j ≤ max₂`. have not_mem : j ∉ set₂ := λ h, lt_irrefl _ ((max'_lt_iff set₂ (nonempty y rid₂)).mp max_lt j h), rw set₂_eq at not_mem, simp only [not_and, not_not, ne.def, mem_filter] at not_mem, exact ideal.mul_mem_left I _ (not_mem H₃), }, }, have not_mem_I : proj 𝒜 max₁ x * proj 𝒜 max₂ y ∉ I, { have neither_mem : proj 𝒜 max₁ x ∉ I ∧ proj 𝒜 max₂ y ∉ I, { rw mem_filter at mem_max₁ mem_max₂, exact ⟨mem_max₁.2, mem_max₂.2⟩, }, intro rid, cases homogeneous_mem_or_mem ⟨max₁, set_like.coe_mem _⟩ ⟨max₂, set_like.coe_mem _⟩ mem_I, { apply neither_mem.1 h }, { apply neither_mem.2 h }, }, exact not_mem_I mem_I, end⟩ lemma ideal.is_homogeneous.is_prime_iff {I : ideal A} (h : I.is_homogeneous 𝒜) : I.is_prime ↔ (I ≠ ⊤) ∧ ∀ {x y : A}, set_like.is_homogeneous 𝒜 x → set_like.is_homogeneous 𝒜 y → (x * y ∈ I → x ∈ I ∨ y ∈ I) := ⟨λ HI, ⟨ne_of_apply_ne _ HI.ne_top, λ x y hx hy hxy, ideal.is_prime.mem_or_mem HI hxy⟩, λ ⟨I_ne_top, homogeneous_mem_or_mem⟩, h.is_prime_of_homogeneous_mem_or_mem I_ne_top @homogeneous_mem_or_mem⟩ lemma ideal.is_prime.homogeneous_core {I : ideal A} (h : I.is_prime) : (I.homogeneous_core 𝒜).to_ideal.is_prime := begin apply (ideal.homogeneous_core 𝒜 I).is_homogeneous.is_prime_of_homogeneous_mem_or_mem, { exact ne_top_of_le_ne_top h.ne_top (ideal.to_ideal_homogeneous_core_le 𝒜 I) }, rintros x y hx hy hxy, have H := h.mem_or_mem (ideal.to_ideal_homogeneous_core_le 𝒜 I hxy), refine H.imp _ _, { exact ideal.mem_homogeneous_core_of_is_homogeneous_of_mem hx, }, { exact ideal.mem_homogeneous_core_of_is_homogeneous_of_mem hy, }, end lemma ideal.is_homogeneous.radical_eq {I : ideal A} (hI : I.is_homogeneous 𝒜) : I.radical = Inf { J | J.is_homogeneous 𝒜 ∧ I ≤ J ∧ J.is_prime } := begin rw ideal.radical_eq_Inf, apply le_antisymm, { exact Inf_le_Inf (λ J, and.right), }, { refine Inf_le_Inf_of_forall_exists_le _, rintros J ⟨HJ₁, HJ₂⟩, refine ⟨(J.homogeneous_core 𝒜).to_ideal, _, J.to_ideal_homogeneous_core_le _⟩, refine ⟨homogeneous_ideal.is_homogeneous _, _, HJ₂.homogeneous_core⟩, refine hI.to_ideal_homogeneous_core_eq_self.symm.trans_le (ideal.homogeneous_core_mono _ HJ₁), } end lemma ideal.is_homogeneous.radical {I : ideal A} (h : I.is_homogeneous 𝒜) : I.radical.is_homogeneous 𝒜 := by { rw h.radical_eq, exact ideal.is_homogeneous.Inf (λ _, and.left) } /-- The radical of a homogenous ideal, as another homogenous ideal. -/ def homogeneous_ideal.radical (I : homogeneous_ideal 𝒜) : homogeneous_ideal 𝒜 := ⟨I.to_ideal.radical, I.is_homogeneous.radical⟩ @[simp] lemma homogeneous_ideal.coe_radical (I : homogeneous_ideal 𝒜) : I.radical.to_ideal = I.to_ideal.radical := rfl
421d510885b4867bedcc072b65f8483026f1de81
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/control/monad/basic_auto.lean
064c1c0923e6c192ef5c4189da17fabd32120c9a
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,642
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author(s): Simon Hudon -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.basic import Mathlib.data.equiv.basic import Mathlib.PostPort universes u v u₀ u₁ v₀ v₁ namespace Mathlib /-! # Monad ## Attributes * ext * functor_norm * monad_norm ## Implementation Details Set of rewrite rules and automation for monads in general and `reader_t`, `state_t`, `except_t` and `option_t` in particular. The rewrite rules for monads are carefully chosen so that `simp with functor_norm` will not introduce monadic vocabulary in a context where applicatives would do just fine but will handle monadic notation already present in an expression. In a context where monadic reasoning is desired `simp with monad_norm` will translate functor and applicative notation into monad notation and use regular `functor_norm` rules as well. ## Tags functor, applicative, monad, simp -/ theorem map_eq_bind_pure_comp (m : Type u → Type v) [Monad m] [is_lawful_monad m] {α : Type u} {β : Type u} (f : α → β) (x : m α) : f <$> x = x >>= pure ∘ f := eq.mpr (id (Eq._oldrec (Eq.refl (f <$> x = x >>= pure ∘ f)) (bind_pure_comp_eq_map f x))) (Eq.refl (f <$> x)) /-- run a `state_t` program and discard the final state -/ def state_t.eval {m : Type u → Type v} [Functor m] {σ : Type u} {α : Type u} (cmd : state_t σ m α) (s : σ) : m α := prod.fst <$> state_t.run cmd s /-- reduce the equivalence between two state monads to the equivalence between their respective function spaces -/ def state_t.equiv {m₁ : Type u₀ → Type v₀} {m₂ : Type u₁ → Type v₁} {α₁ : Type u₀} {σ₁ : Type u₀} {α₂ : Type u₁} {σ₂ : Type u₁} (F : (σ₁ → m₁ (α₁ × σ₁)) ≃ (σ₂ → m₂ (α₂ × σ₂))) : state_t σ₁ m₁ α₁ ≃ state_t σ₂ m₂ α₂ := equiv.mk (fun (_x : state_t σ₁ m₁ α₁) => sorry) (fun (_x : state_t σ₂ m₂ α₂) => sorry) sorry sorry /-- reduce the equivalence between two reader monads to the equivalence between their respective function spaces -/ def reader_t.equiv {m₁ : Type u₀ → Type v₀} {m₂ : Type u₁ → Type v₁} {α₁ : Type u₀} {ρ₁ : Type u₀} {α₂ : Type u₁} {ρ₂ : Type u₁} (F : (ρ₁ → m₁ α₁) ≃ (ρ₂ → m₂ α₂)) : reader_t ρ₁ m₁ α₁ ≃ reader_t ρ₂ m₂ α₂ := equiv.mk (fun (_x : reader_t ρ₁ m₁ α₁) => sorry) (fun (_x : reader_t ρ₂ m₂ α₂) => sorry) sorry sorry end Mathlib
8c8864d2e9197760fc664f1d67f61b8924bf9d82
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/group_theory/perm/sign.lean
a5bdb53fa9c021a14553bd467fd6a77442932e7b
[ "Apache-2.0" ]
permissive
LibertasSpZ/mathlib
b9fcd46625eb940611adb5e719a4b554138dade6
33f7870a49d7cc06d2f3036e22543e6ec5046e68
refs/heads/master
1,672,066,539,347
1,602,429,158,000
1,602,429,158,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
38,006
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.fintype.basic import data.finset.sort import algebra.group.conj import algebra.big_operators.basic universes u v open equiv function fintype finset open_locale big_operators variables {α : Type u} {β : Type v} namespace equiv.perm /-- If the permutation `f` fixes the subtype `{x // p x}`, then this returns the permutation on `{x // p x}` induced by `f`. -/ def subtype_perm (f : perm α) {p : α → Prop} (h : ∀ x, p x ↔ p (f x)) : perm {x // p x} := ⟨λ x, ⟨f x, (h _).1 x.2⟩, λ x, ⟨f⁻¹ x, (h (f⁻¹ x)).2 $ by simpa using x.2⟩, λ _, by simp only [perm.inv_apply_self, subtype.coe_eta, subtype.coe_mk], λ _, by simp only [perm.apply_inv_self, subtype.coe_eta, subtype.coe_mk]⟩ @[simp] lemma subtype_perm_one (p : α → Prop) (h : ∀ x, p x ↔ p ((1 : perm α) x)) : @subtype_perm α 1 p h = 1 := equiv.ext $ λ ⟨_, _⟩, rfl /-- The inclusion map of permutations on a subtype of `α` into permutations of `α`, fixing the other points. -/ def of_subtype {p : α → Prop} [decidable_pred p] : perm (subtype p) →* perm α := { to_fun := λ f, ⟨λ x, if h : p x then f ⟨x, h⟩ else x, λ x, if h : p x then f⁻¹ ⟨x, h⟩ else x, λ x, have h : ∀ h : p x, p (f ⟨x, h⟩), from λ h, (f ⟨x, h⟩).2, by { simp only [], split_ifs at *; simp only [perm.inv_apply_self, subtype.coe_eta, subtype.coe_mk, not_true, *] at * }, λ x, have h : ∀ h : p x, p (f⁻¹ ⟨x, h⟩), from λ h, (f⁻¹ ⟨x, h⟩).2, by { simp only [], split_ifs at *; simp only [perm.apply_inv_self, subtype.coe_eta, subtype.coe_mk, not_true, *] at *}⟩, map_one' := begin ext, dsimp, split_ifs; refl, end, map_mul' := λ f g, equiv.ext $ λ x, begin by_cases h : p x, { have h₁ : p (f (g ⟨x, h⟩)), from (f (g ⟨x, h⟩)).2, have h₂ : p (g ⟨x, h⟩), from (g ⟨x, h⟩).2, simp only [h, h₂, coe_fn_mk, perm.mul_apply, dif_pos, subtype.coe_eta] }, { simp only [h, coe_fn_mk, perm.mul_apply, dif_neg, not_false_iff] } end } lemma eq_inv_iff_eq {f : perm α} {x y : α} : x = f⁻¹ y ↔ f x = y := by conv {to_lhs, rw [← injective.eq_iff f.injective, apply_inv_self]} lemma inv_eq_iff_eq {f : perm α} {x y : α} : f⁻¹ x = y ↔ x = f y := by rw [eq_comm, eq_inv_iff_eq, eq_comm] /-- Two permutations `f` and `g` are `disjoint` if their supports are disjoint, i.e., every element is fixed either by `f`, or by `g`. -/ def disjoint (f g : perm α) := ∀ x, f x = x ∨ g x = x @[symm] lemma disjoint.symm {f g : perm α} : disjoint f g → disjoint g f := by simp only [disjoint, or.comm, imp_self] lemma disjoint_comm {f g : perm α} : disjoint f g ↔ disjoint g f := ⟨disjoint.symm, disjoint.symm⟩ lemma disjoint_mul_comm {f g : perm α} (h : disjoint f g) : f * g = g * f := equiv.ext $ λ x, (h x).elim (λ hf, (h (g x)).elim (λ hg, by simp [mul_apply, hf, hg]) (λ hg, by simp [mul_apply, hf, g.injective hg])) (λ hg, (h (f x)).elim (λ hf, by simp [mul_apply, f.injective hf, hg]) (λ hf, by simp [mul_apply, hf, hg])) @[simp] lemma disjoint_one_left (f : perm α) : disjoint 1 f := λ _, or.inl rfl @[simp] lemma disjoint_one_right (f : perm α) : disjoint f 1 := λ _, or.inr rfl lemma disjoint_mul_left {f g h : perm α} (H1 : disjoint f h) (H2 : disjoint g h) : disjoint (f * g) h := λ x, by cases H1 x; cases H2 x; simp * lemma disjoint_mul_right {f g h : perm α} (H1 : disjoint f g) (H2 : disjoint f h) : disjoint f (g * h) := by rw disjoint_comm; exact disjoint_mul_left H1.symm H2.symm lemma disjoint_prod_right {f : perm α} (l : list (perm α)) (h : ∀ g ∈ l, disjoint f g) : disjoint f l.prod := begin induction l with g l ih, { exact disjoint_one_right _ }, { rw list.prod_cons; exact disjoint_mul_right (h _ (list.mem_cons_self _ _)) (ih (λ g hg, h g (list.mem_cons_of_mem _ hg))) } end lemma disjoint_prod_perm {l₁ l₂ : list (perm α)} (hl : l₁.pairwise disjoint) (hp : l₁ ~ l₂) : l₁.prod = l₂.prod := hp.prod_eq' $ hl.imp $ λ f g, disjoint_mul_comm lemma of_subtype_subtype_perm {f : perm α} {p : α → Prop} [decidable_pred p] (h₁ : ∀ x, p x ↔ p (f x)) (h₂ : ∀ x, f x ≠ x → p x) : of_subtype (subtype_perm f h₁) = f := equiv.ext $ λ x, begin rw [of_subtype, subtype_perm], by_cases hx : p x, { simp only [hx, coe_fn_mk, dif_pos, monoid_hom.coe_mk, subtype.coe_mk]}, { haveI := classical.prop_decidable, simp only [hx, not_not.mp (mt (h₂ x) hx), coe_fn_mk, dif_neg, not_false_iff, monoid_hom.coe_mk] } end lemma of_subtype_apply_of_not_mem {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) {x : α} (hx : ¬ p x) : of_subtype f x = x := dif_neg hx lemma mem_iff_of_subtype_apply_mem {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) (x : α) : p x ↔ p ((of_subtype f : α → α) x) := if h : p x then by dsimp [of_subtype]; simpa [h] using (f ⟨x, h⟩).2 else by simp [h, of_subtype_apply_of_not_mem f h] @[simp] lemma subtype_perm_of_subtype {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) : subtype_perm (of_subtype f) (mem_iff_of_subtype_apply_mem f) = f := equiv.ext $ λ ⟨x, hx⟩, by dsimp [subtype_perm, of_subtype]; simp [show p x, from hx] lemma pow_apply_eq_self_of_apply_eq_self {f : perm α} {x : α} (hfx : f x = x) : ∀ n : ℕ, (f ^ n) x = x | 0 := rfl | (n+1) := by rw [pow_succ', mul_apply, hfx, pow_apply_eq_self_of_apply_eq_self] lemma gpow_apply_eq_self_of_apply_eq_self {f : perm α} {x : α} (hfx : f x = x) : ∀ n : ℤ, (f ^ n) x = x | (n : ℕ) := pow_apply_eq_self_of_apply_eq_self hfx n | -[1+ n] := by rw [gpow_neg_succ_of_nat, inv_eq_iff_eq, pow_apply_eq_self_of_apply_eq_self hfx] lemma pow_apply_eq_of_apply_apply_eq_self {f : perm α} {x : α} (hffx : f (f x) = x) : ∀ n : ℕ, (f ^ n) x = x ∨ (f ^ n) x = f x | 0 := or.inl rfl | (n+1) := (pow_apply_eq_of_apply_apply_eq_self n).elim (λ h, or.inr (by rw [pow_succ, mul_apply, h])) (λ h, or.inl (by rw [pow_succ, mul_apply, h, hffx])) lemma gpow_apply_eq_of_apply_apply_eq_self {f : perm α} {x : α} (hffx : f (f x) = x) : ∀ i : ℤ, (f ^ i) x = x ∨ (f ^ i) x = f x | (n : ℕ) := pow_apply_eq_of_apply_apply_eq_self hffx n | -[1+ n] := by rw [gpow_neg_succ_of_nat, inv_eq_iff_eq, ← injective.eq_iff f.injective, ← mul_apply, ← pow_succ, eq_comm, inv_eq_iff_eq, ← mul_apply, ← pow_succ', @eq_comm _ x, or.comm]; exact pow_apply_eq_of_apply_apply_eq_self hffx _ variable [decidable_eq α] /-- The `finset` of nonfixed points of a permutation. -/ def support [fintype α] (f : perm α) := univ.filter (λ x, f x ≠ x) @[simp] lemma mem_support [fintype α] {f : perm α} {x : α} : x ∈ f.support ↔ f x ≠ x := by simp only [support, true_and, mem_filter, mem_univ] /-- `f.is_swap` indicates that the permutation `f` is a transposition of two elements. -/ def is_swap (f : perm α) := ∃ x y, x ≠ y ∧ f = swap x y lemma swap_mul_eq_mul_swap (f : perm α) (x y : α) : swap x y * f = f * swap (f⁻¹ x) (f⁻¹ y) := equiv.ext $ λ z, begin simp only [perm.mul_apply, swap_apply_def], split_ifs; simp only [perm.apply_inv_self, *, eq_inv_iff_eq,eq_self_iff_true, not_true] at * end lemma mul_swap_eq_swap_mul (f : perm α) (x y : α) : f * swap x y = swap (f x) (f y) * f := by rw [swap_mul_eq_mul_swap, inv_apply_self, inv_apply_self] /-- Multiplying a permutation with `swap i j` twice gives the original permutation. This specialization of `swap_mul_self` is useful when using cosets of permutations. -/ @[simp] lemma swap_mul_self_mul (i j : α) (σ : perm α) : equiv.swap i j * (equiv.swap i j * σ) = σ := by rw [←mul_assoc (swap i j) (swap i j) σ, equiv.swap_mul_self, one_mul] lemma swap_mul_eq_iff {i j : α} {σ : perm α} : swap i j * σ = σ ↔ i = j := ⟨(assume h, have swap_id : swap i j = 1 := mul_right_cancel (trans h (one_mul σ).symm), by {rw [←swap_apply_right i j, swap_id], refl}), (assume h, by erw [h, swap_self, one_mul])⟩ lemma is_swap_of_subtype {p : α → Prop} [decidable_pred p] {f : perm (subtype p)} (h : is_swap f) : is_swap (of_subtype f) := let ⟨⟨x, hx⟩, ⟨y, hy⟩, hxy⟩ := h in ⟨x, y, by simp only [ne.def] at hxy; tauto, equiv.ext $ λ z, begin rw [hxy.2, of_subtype], simp only [swap_apply_def, coe_fn_mk, swap_inv, subtype.mk_eq_mk, monoid_hom.coe_mk], split_ifs; rw subtype.coe_mk <|> cc, end⟩ lemma ne_and_ne_of_swap_mul_apply_ne_self {f : perm α} {x y : α} (hy : (swap x (f x) * f) y ≠ y) : f y ≠ y ∧ y ≠ x := begin simp only [swap_apply_def, mul_apply, injective.eq_iff f.injective] at *, by_cases h : f y = x, { split; intro; simp only [*, if_true, eq_self_iff_true, not_true, ne.def] at *}, { split_ifs at hy; cc } end lemma support_swap_mul_eq [fintype α] {f : perm α} {x : α} (hffx : f (f x) ≠ x) : (swap x (f x) * f).support = f.support.erase x := have hfx : f x ≠ x, from λ hfx, by simpa [hfx] using hffx, finset.ext $ λ y, ⟨λ hy, have hy' : (swap x (f x) * f) y ≠ y, from mem_support.1 hy, mem_erase.2 ⟨λ hyx, by simp [hyx, mul_apply, *] at *, mem_support.2 $ λ hfy, by simp only [mul_apply, swap_apply_def, hfy] at hy'; split_ifs at hy'; simp only [*, eq_self_iff_true, not_true, ne.def, apply_eq_iff_eq] at *⟩, λ hy, by simp only [mem_erase, mem_support, swap_apply_def, mul_apply] at *; intro; split_ifs at *; simp only [*, eq_self_iff_true, not_true, ne.def] at *⟩ lemma card_support_swap_mul [fintype α] {f : perm α} {x : α} (hx : f x ≠ x) : (swap x (f x) * f).support.card < f.support.card := finset.card_lt_card ⟨λ z hz, mem_support.2 (ne_and_ne_of_swap_mul_apply_ne_self (mem_support.1 hz)).1, λ h, absurd (h (mem_support.2 hx)) (mt mem_support.1 (by simp))⟩ /-- Given a list `l : list α` and a permutation `f : perm α` such that the nonfixed points of `f` are in `l`, recursively factors `f` as a product of transpositions. -/ def swap_factors_aux : Π (l : list α) (f : perm α), (∀ {x}, f x ≠ x → x ∈ l) → {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} | [] := λ f h, ⟨[], equiv.ext $ λ x, by rw [list.prod_nil]; exact eq.symm (not_not.1 (mt h (list.not_mem_nil _))), by simp⟩ | (x :: l) := λ f h, if hfx : x = f x then swap_factors_aux l f (λ y hy, list.mem_of_ne_of_mem (λ h : y = x, by simpa [h, hfx.symm] using hy) (h hy)) else let m := swap_factors_aux l (swap x (f x) * f) (λ y hy, have f y ≠ y ∧ y ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hy, list.mem_of_ne_of_mem this.2 (h this.1)) in ⟨swap x (f x) :: m.1, by rw [list.prod_cons, m.2.1, ← mul_assoc, mul_def (swap x (f x)), swap_swap, ← one_def, one_mul], λ g hg, ((list.mem_cons_iff _ _ _).1 hg).elim (λ h, ⟨x, f x, hfx, h⟩) (m.2.2 _)⟩ /-- `swap_factors` represents a permutation as a product of a list of transpositions. The representation is non unique and depends on the linear order structure. For types without linear order `trunc_swap_factors` can be used -/ def swap_factors [fintype α] [decidable_linear_order α] (f : perm α) : {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} := swap_factors_aux ((@univ α _).sort (≤)) f (λ _ _, (mem_sort _).2 (mem_univ _)) /-- This computably represents the fact that any permutation can be represented as the product of a list of transpositions. -/ def trunc_swap_factors [fintype α] (f : perm α) : trunc {l : list (perm α) // l.prod = f ∧ ∀ g ∈ l, is_swap g} := quotient.rec_on_subsingleton (@univ α _).1 (λ l h, trunc.mk (swap_factors_aux l f h)) (show ∀ x, f x ≠ x → x ∈ (@univ α _).1, from λ _ _, mem_univ _) @[elab_as_eliminator] lemma swap_induction_on [fintype α] {P : perm α → Prop} (f : perm α) : P 1 → (∀ f x y, x ≠ y → P f → P (swap x y * f)) → P f := begin cases trunc.out (trunc_swap_factors f) with l hl, induction l with g l ih generalizing f, { simp only [hl.left.symm, list.prod_nil, forall_true_iff] {contextual := tt}}, { assume h1 hmul_swap, rcases hl.2 g (by simp) with ⟨x, y, hxy⟩, rw [← hl.1, list.prod_cons, hxy.2], exact hmul_swap _ _ _ hxy.1 (ih _ ⟨rfl, λ v hv, hl.2 _ (list.mem_cons_of_mem _ hv)⟩ h1 hmul_swap) } end lemma swap_mul_swap_mul_swap {x y z : α} (hwz: x ≠ y) (hxz : x ≠ z) : swap y z * swap x y * swap y z = swap z x := equiv.ext $ λ n, by simp only [swap_apply_def, mul_apply]; split_ifs; cc lemma is_conj_swap {w x y z : α} (hwx : w ≠ x) (hyz : y ≠ z) : is_conj (swap w x) (swap y z) := have h : ∀ {y z : α}, y ≠ z → w ≠ z → (swap w y * swap x z) * swap w x * (swap w y * swap x z)⁻¹ = swap y z := λ y z hyz hwz, by rw [mul_inv_rev, swap_inv, swap_inv, mul_assoc (swap w y), mul_assoc (swap w y), ← mul_assoc _ (swap x z), swap_mul_swap_mul_swap hwx hwz, ← mul_assoc, swap_mul_swap_mul_swap hwz.symm hyz.symm], if hwz : w = z then have hwy : w ≠ y, by cc, ⟨swap w z * swap x y, by rw [swap_comm y z, h hyz.symm hwy]⟩ else ⟨swap w y * swap x z, h hyz hwz⟩ /-- set of all pairs (⟨a, b⟩ : Σ a : fin n, fin n) such that b < a -/ def fin_pairs_lt (n : ℕ) : finset (Σ a : fin n, fin n) := (univ : finset (fin n)).sigma (λ a, (range a).attach_fin (λ m hm, lt_trans (mem_range.1 hm) a.2)) lemma mem_fin_pairs_lt {n : ℕ} {a : Σ a : fin n, fin n} : a ∈ fin_pairs_lt n ↔ a.2 < a.1 := by simp only [fin_pairs_lt, fin.lt_iff_coe_lt_coe, true_and, mem_attach_fin, mem_range, mem_univ, mem_sigma] /-- `sign_aux σ` is the sign of a permutation on `fin n`, defined as the parity of the number of pairs `(x₁, x₂)` such that `x₂ < x₁` but `σ x₁ ≤ σ x₂` -/ def sign_aux {n : ℕ} (a : perm (fin n)) : units ℤ := ∏ x in fin_pairs_lt n, if a x.1 ≤ a x.2 then -1 else 1 @[simp] lemma sign_aux_one (n : ℕ) : sign_aux (1 : perm (fin n)) = 1 := begin unfold sign_aux, conv { to_rhs, rw ← @finset.prod_const_one _ (units ℤ) (fin_pairs_lt n) }, exact finset.prod_congr rfl (λ a ha, if_neg (not_le_of_gt (mem_fin_pairs_lt.1 ha))) end /-- `sign_bij_aux f ⟨a, b⟩` returns the pair consisting of `f a` and `f b` in decreasing order. -/ def sign_bij_aux {n : ℕ} (f : perm (fin n)) (a : Σ a : fin n, fin n) : Σ a : fin n, fin n := if hxa : f a.2 < f a.1 then ⟨f a.1, f a.2⟩ else ⟨f a.2, f a.1⟩ lemma sign_bij_aux_inj {n : ℕ} {f : perm (fin n)} : ∀ a b : Σ a : fin n, fin n, a ∈ fin_pairs_lt n → b ∈ fin_pairs_lt n → sign_bij_aux f a = sign_bij_aux f b → a = b := λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h, begin unfold sign_bij_aux at h, rw mem_fin_pairs_lt at *, have : ¬b₁ < b₂ := not_lt_of_ge (le_of_lt hb), split_ifs at h; simp only [*, (equiv.injective f).eq_iff, eq_self_iff_true, and_self, heq_iff_eq] at *, end lemma sign_bij_aux_surj {n : ℕ} {f : perm (fin n)} : ∀ a ∈ fin_pairs_lt n, ∃ b ∈ fin_pairs_lt n, a = sign_bij_aux f b := λ ⟨a₁, a₂⟩ ha, if hxa : f⁻¹ a₂ < f⁻¹ a₁ then ⟨⟨f⁻¹ a₁, f⁻¹ a₂⟩, mem_fin_pairs_lt.2 hxa, by dsimp [sign_bij_aux]; rw [apply_inv_self, apply_inv_self, dif_pos (mem_fin_pairs_lt.1 ha)]⟩ else ⟨⟨f⁻¹ a₂, f⁻¹ a₁⟩, mem_fin_pairs_lt.2 $ lt_of_le_of_ne (le_of_not_gt hxa) $ λ h, by simpa [mem_fin_pairs_lt, (f⁻¹).injective h, lt_irrefl] using ha, by dsimp [sign_bij_aux]; rw [apply_inv_self, apply_inv_self, dif_neg (not_lt_of_ge (le_of_lt (mem_fin_pairs_lt.1 ha)))]⟩ lemma sign_bij_aux_mem {n : ℕ} {f : perm (fin n)}: ∀ a : Σ a : fin n, fin n, a ∈ fin_pairs_lt n → sign_bij_aux f a ∈ fin_pairs_lt n := λ ⟨a₁, a₂⟩ ha, begin unfold sign_bij_aux, split_ifs with h, { exact mem_fin_pairs_lt.2 h }, { exact mem_fin_pairs_lt.2 (lt_of_le_of_ne (le_of_not_gt h) (λ h, ne_of_lt (mem_fin_pairs_lt.1 ha) (f.injective h.symm))) } end @[simp] lemma sign_aux_inv {n : ℕ} (f : perm (fin n)) : sign_aux f⁻¹ = sign_aux f := prod_bij (λ a ha, sign_bij_aux f⁻¹ a) sign_bij_aux_mem (λ ⟨a, b⟩ hab, if h : f⁻¹ b < f⁻¹ a then by rw [sign_bij_aux, dif_pos h, if_neg (not_le_of_gt h), apply_inv_self, apply_inv_self, if_neg (not_le_of_gt $ mem_fin_pairs_lt.1 hab)] else by rw [sign_bij_aux, if_pos (le_of_not_gt h), dif_neg h, apply_inv_self, apply_inv_self, if_pos (le_of_lt $ mem_fin_pairs_lt.1 hab)]) sign_bij_aux_inj sign_bij_aux_surj lemma sign_aux_mul {n : ℕ} (f g : perm (fin n)) : sign_aux (f * g) = sign_aux f * sign_aux g := begin rw ← sign_aux_inv g, unfold sign_aux, rw ← prod_mul_distrib, refine prod_bij (λ a ha, sign_bij_aux g a) sign_bij_aux_mem _ sign_bij_aux_inj sign_bij_aux_surj, rintros ⟨a, b⟩ hab, rw [sign_bij_aux, mul_apply, mul_apply], rw mem_fin_pairs_lt at hab, by_cases h : g b < g a, { rw dif_pos h, simp only [not_le_of_gt hab, mul_one, perm.inv_apply_self, if_false] }, { rw [dif_neg h, inv_apply_self, inv_apply_self, if_pos (le_of_lt hab)], by_cases h₁ : f (g b) ≤ f (g a), { have : f (g b) ≠ f (g a), { rw [ne.def, injective.eq_iff f.injective, injective.eq_iff g.injective]; exact ne_of_lt hab }, rw [if_pos h₁, if_neg (not_le_of_gt (lt_of_le_of_ne h₁ this))], refl }, { rw [if_neg h₁, if_pos (le_of_lt (lt_of_not_ge h₁))], refl } } end private lemma sign_aux_swap_zero_one {n : ℕ} (hn : 2 ≤ n) : sign_aux (swap (⟨0, lt_of_lt_of_le dec_trivial hn⟩ : fin n) ⟨1, lt_of_lt_of_le dec_trivial hn⟩) = -1 := let zero : fin n := ⟨0, lt_of_lt_of_le dec_trivial hn⟩ in let one : fin n := ⟨1, lt_of_lt_of_le dec_trivial hn⟩ in have hzo : zero < one := dec_trivial, show _ = ∏ x : Σ a : fin n, fin n in {(⟨one, zero⟩ : Σ a : fin n, fin n)}, if (equiv.swap zero one) x.1 ≤ swap zero one x.2 then (-1 : units ℤ) else 1, begin refine eq.symm (prod_subset (λ ⟨x₁, x₂⟩, by simp [mem_fin_pairs_lt, hzo] {contextual := tt}) (λ a ha₁ ha₂, _)), rcases a with ⟨⟨a₁, ha₁⟩, ⟨a₂, ha₂⟩⟩, replace ha₁ : a₂ < a₁ := mem_fin_pairs_lt.1 ha₁, simp only [swap_apply_def], have : ¬ 1 ≤ a₂ → a₂ = 0, from λ h, nat.le_zero_iff.1 (nat.le_of_lt_succ (lt_of_not_ge h)), have : a₁ ≤ 1 → a₁ = 0 ∨ a₁ = 1, from nat.cases_on a₁ (λ _, or.inl rfl) (λ a₁, nat.cases_on a₁ (λ _, or.inr rfl) (λ _ h, absurd h dec_trivial)), split_ifs; simp only [*, not_le.symm, iff.intro fin.veq_of_eq fin.eq_of_veq, nat.le_zero_iff, eq_self_iff_true, not_true, fin.le_def, one, nat.zero_le, and_self, heq_iff_eq, mem_singleton, forall_prop_of_true, or_self, le_refl] at *, end lemma sign_aux_swap : ∀ {n : ℕ} {x y : fin n} (hxy : x ≠ y), sign_aux (swap x y) = -1 | 0 := dec_trivial | 1 := dec_trivial | (n+2) := λ x y hxy, have h2n : 2 ≤ n + 2 := dec_trivial, by rw [← is_conj_iff_eq, ← sign_aux_swap_zero_one h2n]; exact (monoid_hom.mk' sign_aux sign_aux_mul).map_is_conj (is_conj_swap hxy dec_trivial) /-- When the list `l : list α` contains all nonfixed points of the permutation `f : perm α`, `sign_aux2 l f` recursively calculates the sign of `f`. -/ def sign_aux2 : list α → perm α → units ℤ | [] f := 1 | (x::l) f := if x = f x then sign_aux2 l f else -sign_aux2 l (swap x (f x) * f) lemma sign_aux_eq_sign_aux2 {n : ℕ} : ∀ (l : list α) (f : perm α) (e : α ≃ fin n) (h : ∀ x, f x ≠ x → x ∈ l), sign_aux ((e.symm.trans f).trans e) = sign_aux2 l f | [] f e h := have f = 1, from equiv.ext $ λ y, not_not.1 (mt (h y) (list.not_mem_nil _)), by rw [this, one_def, equiv.trans_refl, equiv.symm_trans, ← one_def, sign_aux_one, sign_aux2] | (x::l) f e h := begin rw sign_aux2, by_cases hfx : x = f x, { rw if_pos hfx, exact sign_aux_eq_sign_aux2 l f _ (λ y (hy : f y ≠ y), list.mem_of_ne_of_mem (λ h : y = x, by simpa [h, hfx.symm] using hy) (h y hy) ) }, { have hy : ∀ y : α, (swap x (f x) * f) y ≠ y → y ∈ l, from λ y hy, have f y ≠ y ∧ y ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hy, list.mem_of_ne_of_mem this.2 (h _ this.1), have : (e.symm.trans (swap x (f x) * f)).trans e = (swap (e x) (e (f x))) * (e.symm.trans f).trans e, by ext; simp [← equiv.symm_trans_swap_trans, mul_def], have hefx : e x ≠ e (f x), from mt (injective.eq_iff e.injective).1 hfx, rw [if_neg hfx, ← sign_aux_eq_sign_aux2 _ _ e hy, this, sign_aux_mul, sign_aux_swap hefx], simp only [units.neg_neg, one_mul, units.neg_mul]} end /-- When the multiset `s : multiset α` contains all nonfixed points of the permutation `f : perm α`, `sign_aux2 f _` recursively calculates the sign of `f`. -/ def sign_aux3 [fintype α] (f : perm α) {s : multiset α} : (∀ x, x ∈ s) → units ℤ := quotient.hrec_on s (λ l h, sign_aux2 l f) (trunc.induction_on (equiv_fin α) (λ e l₁ l₂ h, function.hfunext (show (∀ x, x ∈ l₁) = ∀ x, x ∈ l₂, by simp only [h.mem_iff]) (λ h₁ h₂ _, by rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, h₁ _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, h₂ _)]))) lemma sign_aux3_mul_and_swap [fintype α] (f g : perm α) (s : multiset α) (hs : ∀ x, x ∈ s) : sign_aux3 (f * g) hs = sign_aux3 f hs * sign_aux3 g hs ∧ ∀ x y, x ≠ y → sign_aux3 (swap x y) hs = -1 := let ⟨l, hl⟩ := quotient.exists_rep s in let ⟨e, _⟩ := trunc.exists_rep (equiv_fin α) in begin clear _let_match _let_match, subst hl, show sign_aux2 l (f * g) = sign_aux2 l f * sign_aux2 l g ∧ ∀ x y, x ≠ y → sign_aux2 l (swap x y) = -1, have hfg : (e.symm.trans (f * g)).trans e = (e.symm.trans f).trans e * (e.symm.trans g).trans e, from equiv.ext (λ h, by simp [mul_apply]), split, { rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), ← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), hfg, sign_aux_mul] }, { assume x y hxy, have hexy : e x ≠ e y, from mt (injective.eq_iff e.injective).1 hxy, rw [← sign_aux_eq_sign_aux2 _ _ e (λ _ _, hs _), equiv.symm_trans_swap_trans, sign_aux_swap hexy] } end /-- `sign` of a permutation returns the signature or parity of a permutation, `1` for even permutations, `-1` for odd permutations. It is the unique surjective group homomorphism from `perm α` to the group with two elements.-/ def sign [fintype α] : perm α →* units ℤ := monoid_hom.mk' (λ f, sign_aux3 f mem_univ) (λ f g, (sign_aux3_mul_and_swap f g _ mem_univ).1) section sign variable [fintype α] @[simp] lemma sign_mul (f g : perm α) : sign (f * g) = sign f * sign g := monoid_hom.map_mul sign f g @[simp] lemma sign_one : (sign (1 : perm α)) = 1 := monoid_hom.map_one sign @[simp] lemma sign_refl : sign (equiv.refl α) = 1 := monoid_hom.map_one sign @[simp] lemma sign_inv (f : perm α) : sign f⁻¹ = sign f := by rw [monoid_hom.map_inv sign f, int.units_inv_eq_self] lemma sign_swap {x y : α} (h : x ≠ y) : sign (swap x y) = -1 := (sign_aux3_mul_and_swap 1 1 _ mem_univ).2 x y h @[simp] lemma sign_swap' {x y : α} : (swap x y).sign = if x = y then 1 else -1 := if H : x = y then by simp [H, swap_self] else by simp [sign_swap H, H] lemma sign_eq_of_is_swap {f : perm α} (h : is_swap f) : sign f = -1 := let ⟨x, y, hxy⟩ := h in hxy.2.symm ▸ sign_swap hxy.1 lemma sign_aux3_symm_trans_trans [decidable_eq β] [fintype β] (f : perm α) (e : α ≃ β) {s : multiset α} {t : multiset β} (hs : ∀ x, x ∈ s) (ht : ∀ x, x ∈ t) : sign_aux3 ((e.symm.trans f).trans e) ht = sign_aux3 f hs := quotient.induction_on₂ t s (λ l₁ l₂ h₁ h₂, show sign_aux2 _ _ = sign_aux2 _ _, from let n := trunc.out (equiv_fin β) in by rw [← sign_aux_eq_sign_aux2 _ _ n (λ _ _, h₁ _), ← sign_aux_eq_sign_aux2 _ _ (e.trans n) (λ _ _, h₂ _)]; exact congr_arg sign_aux (equiv.ext (λ x, by simp only [equiv.coe_trans, apply_eq_iff_eq, symm_trans_apply]))) ht hs lemma sign_symm_trans_trans [decidable_eq β] [fintype β] (f : perm α) (e : α ≃ β) : sign ((e.symm.trans f).trans e) = sign f := sign_aux3_symm_trans_trans f e mem_univ mem_univ lemma sign_prod_list_swap {l : list (perm α)} (hl : ∀ g ∈ l, is_swap g) : sign l.prod = (-1) ^ l.length := have h₁ : l.map sign = list.repeat (-1) l.length := list.eq_repeat.2 ⟨by simp, λ u hu, let ⟨g, hg⟩ := list.mem_map.1 hu in hg.2 ▸ sign_eq_of_is_swap (hl _ hg.1)⟩, by rw [← list.prod_repeat, ← h₁, list.prod_hom _ (@sign α _ _)] lemma sign_surjective (hα : 1 < fintype.card α) : function.surjective (sign : perm α → units ℤ) := λ a, (int.units_eq_one_or a).elim (λ h, ⟨1, by simp [h]⟩) (λ h, let ⟨x⟩ := fintype.card_pos_iff.1 (lt_trans zero_lt_one hα) in let ⟨y, hxy⟩ := fintype.exists_ne_of_one_lt_card hα x in ⟨swap y x, by rw [sign_swap hxy, h]⟩ ) lemma eq_sign_of_surjective_hom {s : perm α →* units ℤ} (hs : surjective s) : s = sign := have ∀ {f}, is_swap f → s f = -1 := λ f ⟨x, y, hxy, hxy'⟩, hxy'.symm ▸ by_contradiction (λ h, have ∀ f, is_swap f → s f = 1 := λ f ⟨a, b, hab, hab'⟩, by rw [← is_conj_iff_eq, ← or.resolve_right (int.units_eq_one_or _) h, hab']; exact (monoid_hom.of s).map_is_conj (is_conj_swap hab hxy), let ⟨g, hg⟩ := hs (-1) in let ⟨l, hl⟩ := trunc.out (trunc_swap_factors g) in have ∀ a ∈ l.map s, a = (1 : units ℤ) := λ a ha, let ⟨g, hg⟩ := list.mem_map.1 ha in hg.2 ▸ this _ (hl.2 _ hg.1), have s l.prod = 1, by rw [← l.prod_hom s, list.eq_repeat'.2 this, list.prod_repeat, one_pow], by rw [hl.1, hg] at this; exact absurd this dec_trivial), monoid_hom.ext $ λ f, let ⟨l, hl₁, hl₂⟩ := trunc.out (trunc_swap_factors f) in have hsl : ∀ a ∈ l.map s, a = (-1 : units ℤ) := λ a ha, let ⟨g, hg⟩ := list.mem_map.1 ha in hg.2 ▸ this (hl₂ _ hg.1), by rw [← hl₁, ← l.prod_hom s, list.eq_repeat'.2 hsl, list.length_map, list.prod_repeat, sign_prod_list_swap hl₂] lemma sign_subtype_perm (f : perm α) {p : α → Prop} [decidable_pred p] (h₁ : ∀ x, p x ↔ p (f x)) (h₂ : ∀ x, f x ≠ x → p x) : sign (subtype_perm f h₁) = sign f := let l := trunc.out (trunc_swap_factors (subtype_perm f h₁)) in have hl' : ∀ g' ∈ l.1.map of_subtype, is_swap g' := λ g' hg', let ⟨g, hg⟩ := list.mem_map.1 hg' in hg.2 ▸ is_swap_of_subtype (l.2.2 _ hg.1), have hl'₂ : (l.1.map of_subtype).prod = f, by rw [l.1.prod_hom of_subtype, l.2.1, of_subtype_subtype_perm _ h₂], by conv {congr, rw ← l.2.1, skip, rw ← hl'₂}; rw [sign_prod_list_swap l.2.2, sign_prod_list_swap hl', list.length_map] @[simp] lemma sign_of_subtype {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) : sign (of_subtype f) = sign f := have ∀ x, of_subtype f x ≠ x → p x, from λ x, not_imp_comm.1 (of_subtype_apply_of_not_mem f), by conv {to_rhs, rw [← subtype_perm_of_subtype f, sign_subtype_perm _ _ this]} lemma sign_eq_sign_of_equiv [decidable_eq β] [fintype β] (f : perm α) (g : perm β) (e : α ≃ β) (h : ∀ x, e (f x) = g (e x)) : sign f = sign g := have hg : g = (e.symm.trans f).trans e, from equiv.ext $ by simp [h], by rw [hg, sign_symm_trans_trans] lemma sign_bij [decidable_eq β] [fintype β] {f : perm α} {g : perm β} (i : Π x : α, f x ≠ x → β) (h : ∀ x hx hx', i (f x) hx' = g (i x hx)) (hi : ∀ x₁ x₂ hx₁ hx₂, i x₁ hx₁ = i x₂ hx₂ → x₁ = x₂) (hg : ∀ y, g y ≠ y → ∃ x hx, i x hx = y) : sign f = sign g := calc sign f = sign (@subtype_perm _ f (λ x, f x ≠ x) (by simp)) : eq.symm (sign_subtype_perm _ _ (λ _, id)) ... = sign (@subtype_perm _ g (λ x, g x ≠ x) (by simp)) : sign_eq_sign_of_equiv _ _ (equiv.of_bijective (λ x : {x // f x ≠ x}, (⟨i x.1 x.2, have f (f x) ≠ f x, from mt (λ h, f.injective h) x.2, by rw [← h _ x.2 this]; exact mt (hi _ _ this x.2) x.2⟩ : {y // g y ≠ y})) ⟨λ ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq (hi _ _ _ _ (subtype.mk.inj h)), λ ⟨y, hy⟩, let ⟨x, hfx, hx⟩ := hg y hy in ⟨⟨x, hfx⟩, subtype.eq hx⟩⟩) (λ ⟨x, _⟩, subtype.eq (h x _ _)) ... = sign g : sign_subtype_perm _ _ (λ _, id) /-- A permutation is a cycle when any two nonfixed points of the permutation are related by repeated application of the permutation. -/ def is_cycle (f : perm β) := ∃ x, f x ≠ x ∧ ∀ y, f y ≠ y → ∃ i : ℤ, (f ^ i) x = y lemma is_cycle_swap {α : Type*} [decidable_eq α] {x y : α} (hxy : x ≠ y) : is_cycle (swap x y) := ⟨y, by rwa swap_apply_right, λ a (ha : ite (a = x) y (ite (a = y) x a) ≠ a), if hya : y = a then ⟨0, hya⟩ else ⟨1, by rw [gpow_one, swap_apply_def]; split_ifs at *; cc⟩⟩ lemma is_cycle_inv {f : perm β} (hf : is_cycle f) : is_cycle (f⁻¹) := let ⟨x, hx⟩ := hf in ⟨x, by simp only [inv_eq_iff_eq, *, forall_prop_of_true, ne.def] at *; cc, λ y hy, let ⟨i, hi⟩ := hx.2 y (by simp only [inv_eq_iff_eq, *, forall_prop_of_true, ne.def] at *; cc) in ⟨-i, by rwa [gpow_neg, inv_gpow, inv_inv]⟩⟩ lemma exists_gpow_eq_of_is_cycle {f : perm β} (hf : is_cycle f) {x y : β} (hx : f x ≠ x) (hy : f y ≠ y) : ∃ i : ℤ, (f ^ i) x = y := let ⟨g, hg⟩ := hf in let ⟨a, ha⟩ := hg.2 x hx in let ⟨b, hb⟩ := hg.2 y hy in ⟨b - a, by rw [← ha, ← mul_apply, ← gpow_add, sub_add_cancel, hb]⟩ lemma is_cycle_swap_mul_aux₁ {α : Type*} [decidable_eq α] : ∀ (n : ℕ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | 0 := λ b x f hb h, ⟨0, h⟩ | (n+1 : ℕ) := λ b x f hb h, if hfbx : f x = b then ⟨0, hfbx⟩ else have f b ≠ b ∧ b ≠ x, from ne_and_ne_of_swap_mul_apply_ne_self hb, have hb' : (swap x (f x) * f) (f⁻¹ b) ≠ f⁻¹ b, by rw [mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx), ne.def, ← injective.eq_iff f.injective, apply_inv_self]; exact this.1, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb' (f.injective $ by rw [apply_inv_self]; rwa [pow_succ, mul_apply] at h) in ⟨i + 1, by rw [add_comm, gpow_add, mul_apply, hi, gpow_one, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne (ne_and_ne_of_swap_mul_apply_ne_self hb).2 (ne.symm hfbx)]⟩ lemma is_cycle_swap_mul_aux₂ {α : Type*} [decidable_eq α] : ∀ (n : ℤ) {b x : α} {f : perm α} (hb : (swap x (f x) * f) b ≠ b) (h : (f ^ n) (f x) = b), ∃ i : ℤ, ((swap x (f x) * f) ^ i) (f x) = b | (n : ℕ) := λ b x f, is_cycle_swap_mul_aux₁ n | -[1+ n] := λ b x f hb h, if hfbx : f⁻¹ x = b then ⟨-1, by rwa [gpow_neg, gpow_one, mul_inv_rev, mul_apply, swap_inv, swap_apply_right]⟩ else if hfbx' : f x = b then ⟨0, hfbx'⟩ else have f b ≠ b ∧ b ≠ x := ne_and_ne_of_swap_mul_apply_ne_self hb, have hb : (swap x (f⁻¹ x) * f⁻¹) (f⁻¹ b) ≠ f⁻¹ b, by rw [mul_apply, swap_apply_def]; split_ifs; simp only [inv_eq_iff_eq, perm.mul_apply, gpow_neg_succ_of_nat, ne.def, perm.apply_inv_self] at *; cc, let ⟨i, hi⟩ := is_cycle_swap_mul_aux₁ n hb (show (f⁻¹ ^ n) (f⁻¹ x) = f⁻¹ b, by rw [← gpow_coe_nat, ← h, ← mul_apply, ← mul_apply, ← mul_apply, gpow_neg_succ_of_nat, ← inv_pow, pow_succ', mul_assoc, mul_assoc, inv_mul_self, mul_one, gpow_coe_nat, ← pow_succ', ← pow_succ]) in have h : (swap x (f⁻¹ x) * f⁻¹) (f x) = f⁻¹ x, by rw [mul_apply, inv_apply_self, swap_apply_left], ⟨-i, by rw [← add_sub_cancel i 1, neg_sub, sub_eq_add_neg, gpow_add, gpow_one, gpow_neg, ← inv_gpow, mul_inv_rev, swap_inv, mul_swap_eq_swap_mul, inv_apply_self, swap_comm _ x, gpow_add, gpow_one, mul_apply, mul_apply (_ ^ i), h, hi, mul_apply, apply_inv_self, swap_apply_of_ne_of_ne this.2 (ne.symm hfbx')]⟩ lemma eq_swap_of_is_cycle_of_apply_apply_eq_self {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α} (hfx : f x ≠ x) (hffx : f (f x) = x) : f = swap x (f x) := equiv.ext $ λ y, let ⟨z, hz⟩ := hf in let ⟨i, hi⟩ := hz.2 x hfx in if hyx : y = x then by simp [hyx] else if hfyx : y = f x then by simp [hfyx, hffx] else begin rw [swap_apply_of_ne_of_ne hyx hfyx], refine by_contradiction (λ hy, _), cases hz.2 y hy with j hj, rw [← sub_add_cancel j i, gpow_add, mul_apply, hi] at hj, cases gpow_apply_eq_of_apply_apply_eq_self hffx (j - i) with hji hji, { rw [← hj, hji] at hyx, cc }, { rw [← hj, hji] at hfyx, cc } end lemma is_cycle_swap_mul {α : Type*} [decidable_eq α] {f : perm α} (hf : is_cycle f) {x : α} (hx : f x ≠ x) (hffx : f (f x) ≠ x) : is_cycle (swap x (f x) * f) := ⟨f x, by simp only [swap_apply_def, mul_apply]; split_ifs; simp [injective.eq_iff f.injective] at *; cc, λ y hy, let ⟨i, hi⟩ := exists_gpow_eq_of_is_cycle hf hx (ne_and_ne_of_swap_mul_apply_ne_self hy).1 in have hi : (f ^ (i - 1)) (f x) = y, from calc (f ^ (i - 1)) (f x) = (f ^ (i - 1) * f ^ (1 : ℤ)) x : by rw [gpow_one, mul_apply] ... = y : by rwa [← gpow_add, sub_add_cancel], is_cycle_swap_mul_aux₂ (i - 1) hy hi⟩ @[simp] lemma support_swap {x y : α} (hxy : x ≠ y) : (swap x y).support = {x, y} := finset.ext $ λ a, by simp [swap_apply_def]; split_ifs; cc lemma card_support_swap {x y : α} (hxy : x ≠ y) : (swap x y).support.card = 2 := show (swap x y).support.card = finset.card ⟨x::y::0, by simp [hxy]⟩, from congr_arg card $ by rw [support_swap hxy]; simp [*, finset.ext_iff]; cc lemma sign_cycle : ∀ {f : perm α} (hf : is_cycle f), sign f = -(-1) ^ f.support.card | f := λ hf, let ⟨x, hx⟩ := hf in calc sign f = sign (swap x (f x) * (swap x (f x) * f)) : by rw [← mul_assoc, mul_def, mul_def, swap_swap, trans_refl] ... = -(-1) ^ f.support.card : if h1 : f (f x) = x then have h : swap x (f x) * f = 1, begin rw eq_swap_of_is_cycle_of_apply_apply_eq_self hf hx.1 h1, simp only [perm.mul_def, perm.one_def, swap_apply_left, swap_swap] end, by rw [sign_mul, sign_swap hx.1.symm, h, sign_one, eq_swap_of_is_cycle_of_apply_apply_eq_self hf hx.1 h1, card_support_swap hx.1.symm]; refl else have h : card (support (swap x (f x) * f)) + 1 = card (support f), by rw [← insert_erase (mem_support.2 hx.1), support_swap_mul_eq h1, card_insert_of_not_mem (not_mem_erase _ _)], have wf : card (support (swap x (f x) * f)) < card (support f), from card_support_swap_mul hx.1, by rw [sign_mul, sign_swap hx.1.symm, sign_cycle (is_cycle_swap_mul hf hx.1 h1), ← h]; simp only [pow_add, mul_one, units.neg_neg, one_mul, units.mul_neg, eq_self_iff_true, pow_one, units.neg_mul_neg] using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ f, f.support.card)⟩]} /-- If we apply `prod_extend_right a (σ a)` for all `a : α` in turn, we get `prod_congr_right σ`. -/ lemma prod_prod_extend_right {α : Type*} [decidable_eq α] (σ : α → perm β) {l : list α} (hl : l.nodup) (mem_l : ∀ a, a ∈ l) : (l.map (λ a, prod_extend_right a (σ a))).prod = prod_congr_right σ := begin ext ⟨a, b⟩ : 1, -- We'll use induction on the list of elements, -- but we have to keep track of whether we already passed `a` in the list. suffices : (a ∈ l ∧ (l.map (λ a, prod_extend_right a (σ a))).prod (a, b) = (a, σ a b)) ∨ (a ∉ l ∧ (l.map (λ a, prod_extend_right a (σ a))).prod (a, b) = (a, b)), { obtain ⟨_, prod_eq⟩ := or.resolve_right this (not_and.mpr (λ h _, h (mem_l a))), rw [prod_eq, prod_congr_right_apply] }, clear mem_l, induction l with a' l ih, { refine or.inr ⟨list.not_mem_nil _, _⟩, rw [list.map_nil, list.prod_nil, one_apply] }, rw [list.map_cons, list.prod_cons, mul_apply], rcases ih (list.nodup_cons.mp hl).2 with ⟨mem_l, prod_eq⟩ | ⟨not_mem_l, prod_eq⟩; rw prod_eq, { refine or.inl ⟨list.mem_cons_of_mem _ mem_l, _⟩, rw prod_extend_right_apply_ne _ (λ (h : a = a'), (list.nodup_cons.mp hl).1 (h ▸ mem_l)) }, by_cases ha' : a = a', { rw ← ha' at *, refine or.inl ⟨l.mem_cons_self a, _⟩, rw prod_extend_right_apply_eq }, { refine or.inr ⟨λ h, not_or ha' not_mem_l ((list.mem_cons_iff _ _ _).mp h), _⟩, rw prod_extend_right_apply_ne _ ha' }, end section open_locale classical lemma sign_prod_extend_right [fintype β] (a : α) (σ : perm β) : (prod_extend_right a σ).sign = σ.sign := sign_bij (λ (ab : α × β) _, ab.snd) (λ ⟨a', b⟩ hab hab', by simp [eq_of_prod_extend_right_ne hab]) (λ ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ hab₁ hab₂ h, by simpa [eq_of_prod_extend_right_ne hab₁, eq_of_prod_extend_right_ne hab₂] using h) (λ y hy, ⟨(a, y), by simpa, by simp⟩) lemma sign_prod_congr_right [fintype β] (σ : α → perm β) : sign (prod_congr_right σ) = ∏ k, (σ k).sign := begin obtain ⟨l, hl, mem_l⟩ := fintype.exists_univ_list α, have l_to_finset : l.to_finset = finset.univ, { apply eq_top_iff.mpr, intros b _, exact list.mem_to_finset.mpr (mem_l b) }, rw [← prod_prod_extend_right σ hl mem_l, sign.map_list_prod, list.map_map, ← l_to_finset, list.prod_to_finset _ hl], simp_rw ← λ a, sign_prod_extend_right a (σ a) end lemma sign_prod_congr_left [fintype β] (σ : α → perm β) : sign (prod_congr_left σ) = ∏ k, (σ k).sign := begin refine (sign_eq_sign_of_equiv _ _ (prod_comm β α) _).trans (sign_prod_congr_right σ), rintro ⟨b, α⟩, refl end end end sign end equiv.perm
7263eafa0659d930a50a82e02d1f7b6d5e81175d
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/measure_theory/prod_group_auto.lean
053822aa0748720b6b7538c3a8f691e091259cf4
[]
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
9,742
lean
/- Copyright (c) 2021 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.measure_theory.prod import Mathlib.measure_theory.group import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Measure theory in the product of groups In this file we show properties about measure theory in products of topological groups and properties of iterated integrals in topological groups. These lemmas show the uniqueness of left invariant measures on locally compact groups, up to scaling. In this file we follow the proof and refer to the book *Measure Theory* by Paul Halmos. The idea of the proof is to use the translation invariance of measures to prove `μ(F) = c * μ(E)` for two sets `E` and `F`, where `c` is a constant that does not depend on `μ`. Let `e` and `f` be the characteristic functions of `E` and `F`. Assume that `μ` and `ν` are left-invariant measures. Then the map `(x, y) ↦ (y * x, x⁻¹)` preserves the measure `μ.prod ν`, which means that ``` ∫ x, ∫ y, h x y ∂ν ∂μ = ∫ x, ∫ y, h (y * x) x⁻¹ ∂ν ∂μ ``` If we apply this to `h x y := e x * f y⁻¹ / ν ((λ h, h * y⁻¹) ⁻¹' E)`, we can rewrite the RHS to `μ(F)`, and the LHS to `c * μ(E)`, where `c = c(ν)` does not depend on `μ`. Applying this to `μ` and to `ν` gives `μ (F) / μ (E) = ν (F) / ν (E)`, which is the uniqueness up to scalar multiplication. The proof in [Halmos] seems to contain an omission in §60 Th. A, see `measure_theory.measure_lintegral_div_measure` and https://math.stackexchange.com/questions/3974485/does-right-translation-preserve-finiteness-for-a-left-invariant-measure -/ namespace measure_theory /-- This condition is part of the definition of a measurable group in [Halmos, §59]. There, the map in this lemma is called `S`. -/ theorem map_prod_mul_eq {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} {ν : measure G} [sigma_finite ν] [sigma_finite μ] (hν : is_mul_left_invariant ⇑ν) : coe_fn (measure.map fun (z : G × G) => (prod.fst z, prod.fst z * prod.snd z)) (measure.prod μ ν) = measure.prod μ ν := sorry /-- The function we are mapping along is `SR` in [Halmos, §59], where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/ theorem map_prod_mul_eq_swap {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} {ν : measure G} [sigma_finite ν] [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) : coe_fn (measure.map fun (z : G × G) => (prod.snd z, prod.snd z * prod.fst z)) (measure.prod μ ν) = measure.prod ν μ := sorry /-- The function we are mapping along is `S⁻¹` in [Halmos, §59], where `S` is the map in `map_prod_mul_eq`. -/ theorem map_prod_inv_mul_eq {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} {ν : measure G} [sigma_finite ν] [sigma_finite μ] (hν : is_mul_left_invariant ⇑ν) : coe_fn (measure.map fun (z : G × G) => (prod.fst z, prod.fst z⁻¹ * prod.snd z)) (measure.prod μ ν) = measure.prod μ ν := iff.mp (measurable_equiv.map_apply_eq_iff_map_symm_apply_eq (homeomorph.to_measurable_equiv (homeomorph.shear_mul_right G))) (map_prod_mul_eq hν) /-- The function we are mapping along is `S⁻¹R` in [Halmos, §59], where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/ theorem map_prod_inv_mul_eq_swap {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} {ν : measure G} [sigma_finite ν] [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) : coe_fn (measure.map fun (z : G × G) => (prod.snd z, prod.snd z⁻¹ * prod.fst z)) (measure.prod μ ν) = measure.prod ν μ := sorry /-- The function we are mapping along is `S⁻¹RSR` in [Halmos, §59], where `S` is the map in `map_prod_mul_eq` and `R` is `prod.swap`. -/ theorem map_prod_mul_inv_eq {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} {ν : measure G} [sigma_finite ν] [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) (hν : is_mul_left_invariant ⇑ν) : coe_fn (measure.map fun (z : G × G) => (prod.snd z * prod.fst z, prod.fst z⁻¹)) (measure.prod μ ν) = measure.prod μ ν := sorry theorem measure_null_of_measure_inv_null {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) {E : set G} (hE : is_measurable E) (h2E : coe_fn μ ((fun (x : G) => x⁻¹) ⁻¹' E) = 0) : coe_fn μ E = 0 := sorry theorem measure_inv_null {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) {E : set G} (hE : is_measurable E) : coe_fn μ ((fun (x : G) => x⁻¹) ⁻¹' E) = 0 ↔ coe_fn μ E = 0 := sorry theorem measurable_measure_mul_right {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} [sigma_finite μ] {E : set G} (hE : is_measurable E) : measurable fun (x : G) => coe_fn μ ((fun (y : G) => y * x) ⁻¹' E) := sorry theorem lintegral_lintegral_mul_inv {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} {ν : measure G} [sigma_finite ν] [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) (hν : is_mul_left_invariant ⇑ν) (f : G → G → ennreal) (hf : measurable (function.uncurry f)) : (lintegral μ fun (x : G) => lintegral ν fun (y : G) => f (y * x) (x⁻¹)) = lintegral μ fun (x : G) => lintegral ν fun (y : G) => f x y := sorry theorem measure_mul_right_null {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) {E : set G} (hE : is_measurable E) (y : G) : coe_fn μ ((fun (x : G) => x * y) ⁻¹' E) = 0 ↔ coe_fn μ E = 0 := sorry theorem measure_mul_right_ne_zero {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) {E : set G} (hE : is_measurable E) (h2E : coe_fn μ E ≠ 0) (y : G) : coe_fn μ ((fun (x : G) => x * y) ⁻¹' E) ≠ 0 := iff.mpr (not_iff_not_of_iff (measure_mul_right_null hμ hE y)) h2E /-- A technical lemma relating two different measures. This is basically [Halmos, §60 Th. A]. Note that if `f` is the characteristic function of a measurable set `F` this states that `μ F = c * μ E` for a constant `c` that does not depend on `μ`. There seems to be a gap in the last step of the proof in [Halmos]. In the last line, the equality `g(x⁻¹)ν(Ex⁻¹) = f(x)` holds if we can prove that `0 < ν(Ex⁻¹) < ∞`. The first inequality follows from §59, Th. D, but I couldn't find the second inequality. For this reason, we use a compact `E` instead of a measurable `E` as in [Halmos], and additionally assume that `ν` is a regular measure (we only need that it is finite on compact sets). -/ theorem measure_lintegral_div_measure {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} {ν : measure G} [sigma_finite ν] [sigma_finite μ] [t2_space G] (hμ : is_mul_left_invariant ⇑μ) (hν : is_mul_left_invariant ⇑ν) (h2ν : measure.regular ν) {E : set G} (hE : is_compact E) (h2E : coe_fn ν E ≠ 0) (f : G → ennreal) (hf : measurable f) : (coe_fn μ E * lintegral ν fun (y : G) => f (y⁻¹) / coe_fn ν ((fun (h : G) => h * (y⁻¹)) ⁻¹' E)) = lintegral μ fun (x : G) => f x := sorry /-- This is roughly the uniqueness (up to a scalar) of left invariant Borel measures on a second countable locally compact group. The uniqueness of Haar measure is proven from this in `measure_theory.measure.haar_measure_unique` -/ theorem measure_mul_measure_eq {G : Type u_1} [topological_space G] [measurable_space G] [topological_space.second_countable_topology G] [borel_space G] [group G] [topological_group G] {μ : measure G} {ν : measure G} [sigma_finite ν] [sigma_finite μ] [t2_space G] (hμ : is_mul_left_invariant ⇑μ) (hν : is_mul_left_invariant ⇑ν) (h2ν : measure.regular ν) {E : set G} {F : set G} (hE : is_compact E) (hF : is_measurable F) (h2E : coe_fn ν E ≠ 0) : coe_fn μ E * coe_fn ν F = coe_fn ν E * coe_fn μ F := sorry end Mathlib
ef3b967ba09bef911af8ecfdfe33f4dc4674741f
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/monoidal/discrete.lean
ead5f4a967ee861a6785dcd3520d545e23ec3f19
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
2,184
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.hom.group import category_theory.discrete_category import category_theory.monoidal.natural_transformation /-! # Monoids as discrete monoidal categories The discrete category on a monoid is a monoidal category. Multiplicative morphisms induced monoidal functors. -/ universes u open category_theory open category_theory.discrete variables (M : Type u) [monoid M] namespace category_theory @[to_additive] instance monoid_discrete : monoid (discrete M) := by { dsimp [discrete], apply_instance } @[to_additive discrete.add_monoidal] instance discrete.monoidal : monoidal_category (discrete M) := { tensor_unit := 1, tensor_obj := λ X Y, X * Y, tensor_hom := λ W X Y Z f g, eq_to_hom (by rw [eq_of_hom f, eq_of_hom g]), left_unitor := λ X, eq_to_iso (one_mul X), right_unitor := λ X, eq_to_iso (mul_one X), associator := λ X Y Z, eq_to_iso (mul_assoc _ _ _), } variables {M} {N : Type u} [monoid N] /-- A multiplicative morphism between monoids gives a monoidal functor between the corresponding discrete monoidal categories. -/ @[to_additive discrete.add_monoidal_functor "An additive morphism between add_monoids gives a monoidal functor between the corresponding discrete monoidal categories.", simps] def discrete.monoidal_functor (F : M →* N) : monoidal_functor (discrete M) (discrete N) := { obj := F, map := λ X Y f, eq_to_hom (F.congr_arg (eq_of_hom f)), ε := eq_to_hom F.map_one.symm, μ := λ X Y, eq_to_hom (F.map_mul X Y).symm, } variables {K : Type u} [monoid K] /-- The monoidal natural isomorphism corresponding to composing two multiplicative morphisms. -/ @[to_additive discrete.add_monoidal_functor_comp "The monoidal natural isomorphism corresponding to composing two additive morphisms."] def discrete.monoidal_functor_comp (F : M →* N) (G : N →* K) : discrete.monoidal_functor F ⊗⋙ discrete.monoidal_functor G ≅ discrete.monoidal_functor (G.comp F) := { hom := { app := λ X, 𝟙 _, }, inv := { app := λ X, 𝟙 _, }, } end category_theory
b7ba882a5df5ebe1c8a1201efe26846577c468fd
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/group_theory/monoid_localization.lean
4037a110fe3ea6083118d7af85189f9807a1ed73
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
60,928
lean
/- Copyright (c) 2019 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import group_theory.congruence import group_theory.submonoid import algebra.group.units import algebra.punit_instances /-! # Localizations of commutative monoids Localizing a commutative ring at one of its submonoids does not rely on the ring's addition, so we can generalize localizations to commutative monoids. We characterize the localization of a commutative monoid `M` at a submonoid `S` up to isomorphism; that is, a commutative monoid `N` is the localization of `M` at `S` iff we can find a monoid homomorphism `f : M →* N` satisfying 3 properties: 1. For all `y ∈ S`, `f y` is a unit; 2. For all `z : N`, there exists `(x, y) : M × S` such that `z * f y = f x`; 3. For all `x, y : M`, `f x = f y` iff there exists `c ∈ S` such that `x * c = y * c`. Given such a localization map `f : M →* N`, we can define the surjection `localization_map.mk'` sending `(x, y) : M × S` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `N` induced by a homomorphism from `M` which maps elements of `S` to invertible elements of the codomain. Similarly, given commutative monoids `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : M →* P` such that `g(S) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `N` to `Q`. We treat the special case of localizing away from an element in the sections `away_map` and `away`. We also define the quotient of `M × S` by the unique congruence relation (equivalence relation preserving a binary operation) `r` such that for any other congruence relation `s` on `M × S` satisfying '`∀ y ∈ S`, `(1, 1) ∼ (y, y)` under `s`', we have that `(x₁, y₁) ∼ (x₂, y₂)` by `s` whenever `(x₁, y₁) ∼ (x₂, y₂)` by `r`. We show this relation is equivalent to the standard localization relation. This defines the localization as a quotient type, `localization`, but the majority of subsequent lemmas in the file are given in terms of localizations up to isomorphism, using maps which satisfy the characteristic predicate. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. The infimum form of the localization congruence relation is chosen as 'canonical' here, since it shortens some proofs. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. To reason about the localization as a quotient type, use `mk_eq_monoid_of_mk'` and associated lemmas. These show the quotient map `mk : M → S → localization S` equals the surjection `localization_map.mk'` induced by the map `monoid_of : localization_map S (localization S)` (where `of` establishes the localization as a quotient type satisfies the characteristic predicate). The lemma `mk_eq_monoid_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. ## Tags localization, monoid localization, quotient monoid, congruence relation, characteristic predicate, commutative monoid -/ namespace add_submonoid variables {M : Type*} [add_comm_monoid M] (S : add_submonoid M) (N : Type*) [add_comm_monoid N] /-- The type of add_monoid homomorphisms satisfying the characteristic predicate: if `f : M →+ N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/ @[nolint has_inhabited_instance] structure localization_map extends add_monoid_hom M N := (map_add_units' : ∀ y : S, is_add_unit (to_fun y)) (surj' : ∀ z : N, ∃ x : M × S, z + to_fun x.2 = to_fun x.1) (eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x + c = y + c) /-- The add_monoid hom underlying a `localization_map` of `add_comm_monoid`s. -/ add_decl_doc localization_map.to_add_monoid_hom end add_submonoid variables {M : Type*} [comm_monoid M] (S : submonoid M) (N : Type*) [comm_monoid N] {P : Type*} [comm_monoid P] namespace submonoid /-- The type of monoid homomorphisms satisfying the characteristic predicate: if `f : M →* N` satisfies this predicate, then `N` is isomorphic to the localization of `M` at `S`. -/ @[nolint has_inhabited_instance] structure localization_map extends monoid_hom M N := (map_units' : ∀ y : S, is_unit (to_fun y)) (surj' : ∀ z : N, ∃ x : M × S, z * to_fun x.2 = to_fun x.1) (eq_iff_exists' : ∀ x y, to_fun x = to_fun y ↔ ∃ c : S, x * c = y * c) attribute [to_additive add_submonoid.localization_map] submonoid.localization_map attribute [to_additive add_submonoid.localization_map.to_add_monoid_hom] submonoid.localization_map.to_monoid_hom /-- The monoid hom underlying a `localization_map`. -/ add_decl_doc localization_map.to_monoid_hom end submonoid namespace localization run_cmd to_additive.map_namespace `localization `add_localization /-- The congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(1, 1) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`. -/ @[to_additive "The congruence relation on `M × S`, `M` an `add_comm_monoid` and `S` an `add_submonoid` of `M`, whose quotient is the localization of `M` at `S`, defined as the unique congruence relation on `M × S` such that for any other congruence relation `s` on `M × S` where for all `y ∈ S`, `(0, 0) ∼ (y, y)` under `s`, we have that `(x₁, y₁) ∼ (x₂, y₂)` by `r` implies `(x₁, y₁) ∼ (x₂, y₂)` by `s`."] def r (S : submonoid M) : con (M × S) := Inf {c | ∀ y : S, c 1 (y, y)} /-- An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`. -/ @[to_additive "An alternate form of the congruence relation on `M × S`, `M` a `comm_monoid` and `S` a submonoid of `M`, whose quotient is the localization of `M` at `S`."] def r' : con (M × S) := begin refine { r := λ a b : M × S, ∃ c : S, a.1 * b.2 * c = b.1 * a.2 * c, iseqv := ⟨λ a, ⟨1, rfl⟩, λ a b ⟨c, hc⟩, ⟨c, hc.symm⟩, _⟩, .. }, { rintros a b c ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, use b.2 * t₁ * t₂, simp only [submonoid.coe_mul], calc a.1 * c.2 * (b.2 * t₁ * t₂) = a.1 * b.2 * t₁ * c.2 * t₂ : by ac_refl ... = b.1 * c.2 * t₂ * a.2 * t₁ : by { rw ht₁, ac_refl } ... = c.1 * a.2 * (b.2 * t₁ * t₂) : by { rw ht₂, ac_refl } }, { rintros a b c d ⟨t₁, ht₁⟩ ⟨t₂, ht₂⟩, use t₁ * t₂, calc (a.1 * c.1) * (b.2 * d.2) * (t₁ * t₂) = (a.1 * b.2 * t₁) * (c.1 * d.2 * t₂) : by ac_refl ... = (b.1 * d.1) * (a.2 * c.2) * (t₁ * t₂) : by { rw [ht₁, ht₂], ac_refl } } end /-- The congruence relation used to localize a `comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `localization.r`) or explicitly (see `localization.r'`). -/ @[to_additive "The additive congruence relation used to localize an `add_comm_monoid` at a submonoid can be expressed equivalently as an infimum (see `add_localization.r`) or explicitly (see `add_localization.r'`)."] theorem r_eq_r' : r S = r' S := le_antisymm (Inf_le $ λ _, ⟨1, by simp⟩) $ le_Inf $ λ b H ⟨p, q⟩ y ⟨t, ht⟩, begin rw [← mul_one (p, q), ← mul_one y], refine b.trans (b.mul (b.refl _) (H (y.2 * t))) _, convert b.symm (b.mul (b.refl y) (H (q * t))) using 1, rw [prod.mk_mul_mk, submonoid.coe_mul, ← mul_assoc, ht, mul_left_comm, mul_assoc], refl end variables {S} @[to_additive] lemma r_iff_exists {x y : M × S} : r S x y ↔ ∃ c : S, x.1 * y.2 * c = y.1 * x.2 * c := by rw r_eq_r' S; refl end localization /-- The localization of a `comm_monoid` at one of its submonoids (as a quotient type). -/ @[to_additive add_localization "The localization of an `add_comm_monoid` at one of its submonoids (as a quotient type)."] def localization := (localization.r S).quotient namespace localization @[to_additive] instance inhabited : inhabited (localization S) := con.quotient.inhabited /-- Multiplication in a localization is defined as `⟨a, b⟩ * ⟨c, d⟩ = ⟨a * c, b * d⟩`. -/ @[to_additive "Addition in an `add_localization` is defined as `⟨a, b⟩ + ⟨c, d⟩ = ⟨a + c, b + d⟩`. Should not be confused with the ring localization counterpart `localization.add`, which maps `⟨a, b⟩ + ⟨c, d⟩` to `⟨d * a + b * c, b * d⟩`.", irreducible] protected def mul : localization S → localization S → localization S := (r S).comm_monoid.mul @[to_additive] instance : has_mul (localization S) := ⟨localization.mul S⟩ /-- The identity element of a localization is defined as `⟨1, 1⟩`. -/ @[to_additive "The identity element of an `add_localization` is defined as `⟨0, 0⟩`. Should not be confused with the ring localization counterpart `localization.zero`, which is defined as `⟨0, 1⟩`.", irreducible] protected def one : localization S := (r S).comm_monoid.one @[to_additive] instance : has_one (localization S) := ⟨localization.one S⟩ /-- Exponentiation in a localization is defined as `⟨a, b⟩ ^ n = ⟨a ^ n, b ^ n⟩`. This is a separate `irreducible` def to ensure the elaborator doesn't waste its time trying to unify some huge recursive definition with itself, but unfolded one step less. -/ @[to_additive "Multiplication with a natural in an `add_localization` is defined as `n • ⟨a, b⟩ = ⟨n • a, n • b⟩`. This is a separate `irreducible` def to ensure the elaborator doesn't waste its time trying to unify some huge recursive definition with itself, but unfolded one step less.", irreducible] protected def npow : ℕ → localization S → localization S := (r S).comm_monoid.npow local attribute [semireducible] localization.mul localization.one localization.npow @[to_additive] instance : comm_monoid (localization S) := { mul := (*), one := 1, mul_assoc := show ∀ (x y z : localization S), x * y * z = x * (y * z), from (r S).comm_monoid.mul_assoc, mul_comm := show ∀ (x y : localization S), x * y = y * x, from (r S).comm_monoid.mul_comm, mul_one := show ∀ (x : localization S), x * 1 = x, from (r S).comm_monoid.mul_one, one_mul := show ∀ (x : localization S), 1 * x = x, from (r S).comm_monoid.one_mul, npow := localization.npow S, npow_zero' := show ∀ (x : localization S), localization.npow S 0 x = 1, from pow_zero, npow_succ' := show ∀ (n : ℕ) (x : localization S), localization.npow S n.succ x = x * localization.npow S n x, from λ n x, pow_succ x n } variables {S} /-- Given a `comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to the equivalence class of `(x, y)` in the localization of `M` at `S`. -/ @[to_additive "Given an `add_comm_monoid` `M` and submonoid `S`, `mk` sends `x : M`, `y ∈ S` to the equivalence class of `(x, y)` in the localization of `M` at `S`."] def mk (x : M) (y : S) : localization S := (r S).mk' (x, y) @[to_additive] theorem mk_eq_mk_iff {a c : M} {b d : S} : mk a b = mk c d ↔ r S ⟨a, b⟩ ⟨c, d⟩ := (r S).eq universes u /-- Dependent recursion principle for localizations: given elements `f a b : p (mk a b)` for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d` (wih the correct coercions), then `f` is defined on the whole `localization S`. -/ @[elab_as_eliminator, to_additive "Dependent recursion principle for `add_localizations`: given elements `f a b : p (mk a b)` for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d` (wih the correct coercions), then `f` is defined on the whole `add_localization S`."] def rec {p : localization S → Sort u} (f : ∀ (a : M) (b : S), p (mk a b)) (H : ∀ {a c : M} {b d : S} (h : r S (a, b) (c, d)), (eq.rec (f a b) (mk_eq_mk_iff.mpr h) : p (mk c d)) = f c d) (x) : p x := quot.rec (λ y, eq.rec (f y.1 y.2) (prod.mk.eta : (y.1, y.2) = y)) (λ y z h, by { cases y, cases z, exact H h }) x attribute [irreducible] localization @[to_additive] lemma mk_mul (a c : M) (b d : S) : mk a b * mk c d = mk (a * c) (b * d) := rfl @[to_additive] lemma mk_one : mk 1 (1 : S) = 1 := rfl @[simp, to_additive] lemma rec_mk {p : localization S → Sort u} (f : ∀ (a : M) (b : S), p (mk a b)) (H) (a : M) (b : S) : (rec f H (mk a b) : p (mk a b)) = f a b := rfl /-- Non-dependent recursion principle for localizations: given elements `f a b : p` for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d`, then `f` is defined on the whole `localization S`. -/ @[elab_as_eliminator, to_additive "Non-dependent recursion principle for `add_localizations`: given elements `f a b : p` for all `a b`, such that `r S (a, b) (c, d)` implies `f a b = f c d`, then `f` is defined on the whole `localization S`."] def lift_on {p : Sort u} (x : localization S) (f : M → S → p) (H : ∀ {a c : M} {b d : S} (h : r S (a, b) (c, d)), f a b = f c d) : p := rec f (λ a c b d h, by rw [eq_rec_constant, H h]) x @[to_additive] lemma lift_on_mk {p : Sort u} (f : ∀ (a : M) (b : S), p) (H) (a : M) (b : S) : lift_on (mk a b) f H = f a b := rfl @[elab_as_eliminator, to_additive] theorem ind {p : localization S → Prop} (H : ∀ (y : M × S), p (mk y.1 y.2)) (x) : p x := rec (λ a b, H (a, b)) (λ _ _ _ _ _, rfl) x @[elab_as_eliminator, to_additive] theorem induction_on {p : localization S → Prop} (x) (H : ∀ (y : M × S), p (mk y.1 y.2)) : p x := ind H x /-- Non-dependent recursion principle for localizations: given elements `f x y : p` for all `x` and `y`, such that `r S x x'` and `r S y y'` implies `f x y = f x' y'`, then `f` is defined on the whole `localization S`. -/ @[elab_as_eliminator, to_additive "Non-dependent recursion principle for localizations: given elements `f x y : p` for all `x` and `y`, such that `r S x x'` and `r S y y'` implies `f x y = f x' y'`, then `f` is defined on the whole `localization S`."] def lift_on₂ {p : Sort u} (x y : localization S) (f : M → S → M → S → p) (H : ∀ {a a' b b' c c' d d'} (hx : r S (a, b) (a', b')) (hy : r S (c, d) (c', d')), f a b c d = f a' b' c' d') : p := lift_on x (λ a b, lift_on y (f a b) (λ c c' d d' hy, H ((r S).refl _) hy)) (λ a a' b b' hx, induction_on y (λ ⟨c, d⟩, H hx ((r S).refl _))) @[to_additive] lemma lift_on₂_mk {p : Sort*} (f : M → S → M → S → p) (H) (a c : M) (b d : S) : lift_on₂ (mk a b) (mk c d) f H = f a b c d := rfl @[elab_as_eliminator, to_additive] theorem induction_on₂ {p : localization S → localization S → Prop} (x y) (H : ∀ (x y : M × S), p (mk x.1 x.2) (mk y.1 y.2)) : p x y := induction_on x $ λ x, induction_on y $ H x @[elab_as_eliminator, to_additive] theorem induction_on₃ {p : localization S → localization S → localization S → Prop} (x y z) (H : ∀ (x y z : M × S), p (mk x.1 x.2) (mk y.1 y.2) (mk z.1 z.2)) : p x y z := induction_on₂ x y $ λ x y, induction_on z $ H x y @[to_additive] lemma one_rel (y : S) : r S 1 (y, y) := λ b hb, hb y @[to_additive] theorem r_of_eq {x y : M × S} (h : y.1 * x.2 = x.1 * y.2) : r S x y := r_iff_exists.2 ⟨1, by rw h⟩ @[to_additive] lemma mk_self (a : S) : mk (a : M) a = 1 := by { symmetry, rw [← mk_one, mk_eq_mk_iff], exact one_rel a } end localization variables {S N} namespace monoid_hom /-- Makes a localization map from a `comm_monoid` hom satisfying the characteristic predicate. -/ @[to_additive "Makes a localization map from an `add_comm_monoid` hom satisfying the characteristic predicate."] def to_localization_map (f : M →* N) (H1 : ∀ y : S, is_unit (f y)) (H2 : ∀ z, ∃ x : M × S, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : S, x * c = y * c) : submonoid.localization_map S N := { map_units' := H1, surj' := H2, eq_iff_exists' := H3, .. f } end monoid_hom namespace submonoid namespace localization_map /-- Short for `to_monoid_hom`; used to apply a localization map as a function. -/ @[to_additive "Short for `to_add_monoid_hom`; used to apply a localization map as a function."] abbreviation to_map (f : localization_map S N) := f.to_monoid_hom @[ext, to_additive] lemma ext {f g : localization_map S N} (h : ∀ x, f.to_map x = g.to_map x) : f = g := by { rcases f with ⟨⟨⟩⟩, rcases g with ⟨⟨⟩⟩, simp only, exact funext h, } @[to_additive] lemma ext_iff {f g : localization_map S N} : f = g ↔ ∀ x, f.to_map x = g.to_map x := ⟨λ h x, h ▸ rfl, ext⟩ @[to_additive] lemma to_map_injective : function.injective (@localization_map.to_map _ _ S N _) := λ _ _ h, ext $ monoid_hom.ext_iff.1 h @[to_additive] lemma map_units (f : localization_map S N) (y : S) : is_unit (f.to_map y) := f.2 y @[to_additive] lemma surj (f : localization_map S N) (z : N) : ∃ x : M × S, z * f.to_map x.2 = f.to_map x.1 := f.3 z @[to_additive] lemma eq_iff_exists (f : localization_map S N) {x y} : f.to_map x = f.to_map y ↔ ∃ c : S, x * c = y * c := f.4 x y /-- Given a localization map `f : M →* N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x * (f y)⁻¹ = z`. -/ @[to_additive "Given a localization map `f : M →+ N`, a section function sending `z : N` to some `(x, y) : M × S` such that `f x - f y = z`."] noncomputable def sec (f : localization_map S N) (z : N) : M × S := classical.some $ f.surj z @[to_additive] lemma sec_spec {f : localization_map S N} (z : N) : z * f.to_map (f.sec z).2 = f.to_map (f.sec z).1 := classical.some_spec $ f.surj z @[to_additive] lemma sec_spec' {f : localization_map S N} (z : N) : f.to_map (f.sec z).1 = f.to_map (f.sec z).2 * z := by rw [mul_comm, sec_spec] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `w : M, z : N` and `y ∈ S`, we have `w * (f y)⁻¹ = z ↔ w = f y * z`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `w - f y = z ↔ w = f y + z`."] lemma mul_inv_left {f : M →* N} (h : ∀ y : S, is_unit (f y)) (y : S) (w z) : w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z ↔ w = f y * z := by rw mul_comm; convert units.inv_mul_eq_iff_eq_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) h _).symm /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w * (f y)⁻¹ ↔ z * f y = w`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `w : M, z : N` and `y ∈ S`, we have `z = w - f y ↔ z + f y = w`."] lemma mul_inv_right {f : M →* N} (h : ∀ y : S, is_unit (f y)) (y : S) (w z) : z = w * ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ ↔ z * f y = w := by rw [eq_comm, mul_inv_left h, mul_comm, eq_comm] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have `f x₁ * (f y₁)⁻¹ = f x₂ * (f y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁)`. -/ @[simp, to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `x₁ x₂ : M` and `y₁, y₂ ∈ S`, we have `f x₁ - f y₁ = f x₂ - f y₂ ↔ f (x₁ + y₂) = f (x₂ + y₁)`."] lemma mul_inv {f : M →* N} (h : ∀ y : S, is_unit (f y)) {x₁ x₂} {y₁ y₂ : S} : f x₁ * ↑(is_unit.lift_right (f.mrestrict S) h y₁)⁻¹ = f x₂ * ↑(is_unit.lift_right (f.mrestrict S) h y₂)⁻¹ ↔ f (x₁ * y₂) = f (x₂ * y₁) := by rw [mul_inv_right h, mul_assoc, mul_comm _ (f y₂), ←mul_assoc, mul_inv_left h, mul_comm x₂, f.map_mul, f.map_mul] /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `y, z ∈ S`, we have `(f y)⁻¹ = (f z)⁻¹ → f y = f z`. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `y, z ∈ S`, we have `- (f y) = - (f z) → f y = f z`."] lemma inv_inj {f : M →* N} (hf : ∀ y : S, is_unit (f y)) {y z} (h : (is_unit.lift_right (f.mrestrict S) hf y)⁻¹ = (is_unit.lift_right (f.mrestrict S) hf z)⁻¹) : f y = f z := by rw [←mul_one (f y), eq_comm, ←mul_inv_left hf y (f z) 1, h]; convert units.inv_mul _; exact (is_unit.coe_lift_right (f.mrestrict S) hf _).symm /-- Given a monoid hom `f : M →* N` and submonoid `S ⊆ M` such that `f(S) ⊆ units N`, for all `y ∈ S`, `(f y)⁻¹` is unique. -/ @[to_additive "Given an add_monoid hom `f : M →+ N` and submonoid `S ⊆ M` such that `f(S) ⊆ add_units N`, for all `y ∈ S`, `- (f y)` is unique."] lemma inv_unique {f : M →* N} (h : ∀ y : S, is_unit (f y)) {y : S} {z} (H : f y * z = 1) : ↑(is_unit.lift_right (f.mrestrict S) h y)⁻¹ = z := by rw [←one_mul ↑(_)⁻¹, mul_inv_left, ←H] variables (f : localization_map S N) @[to_additive] lemma map_right_cancel {x y} {c : S} (h : f.to_map (c * x) = f.to_map (c * y)) : f.to_map x = f.to_map y := begin rw [f.to_map.map_mul, f.to_map.map_mul] at h, cases f.map_units c with u hu, rw ←hu at h, exact (units.mul_right_inj u).1 h, end @[to_additive] lemma map_left_cancel {x y} {c : S} (h : f.to_map (x * c) = f.to_map (y * c)) : f.to_map x = f.to_map y := f.map_right_cancel $ by rw [mul_comm _ x, mul_comm _ y, h] /-- Given a localization map `f : M →* N`, the surjection sending `(x, y) : M × S` to `f x * (f y)⁻¹`. -/ @[to_additive "Given a localization map `f : M →+ N`, the surjection sending `(x, y) : M × S` to `f x - f y`."] noncomputable def mk' (f : localization_map S N) (x : M) (y : S) : N := f.to_map x * ↑(is_unit.lift_right (f.to_map.mrestrict S) f.map_units y)⁻¹ @[to_additive] lemma mk'_mul (x₁ x₂ : M) (y₁ y₂ : S) : f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ := (mul_inv_left f.map_units _ _ _).2 $ show _ = _ * (_ * _ * (_ * _)), by rw [←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, mul_assoc, mul_assoc, mul_comm _ (f.to_map x₂), ←mul_assoc, ←mul_assoc, mul_inv_right f.map_units, submonoid.coe_mul, f.to_map.map_mul, f.to_map.map_mul]; ac_refl @[to_additive] lemma mk'_one (x) : f.mk' x (1 : S) = f.to_map x := by rw [mk', monoid_hom.map_one]; exact mul_one _ /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `z : N` we have that if `x : M, y ∈ S` are such that `z * f y = f x`, then `f x * (f y)⁻¹ = z`. -/ @[simp, to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `z : N` we have that if `x : M, y ∈ S` are such that `z + f y = f x`, then `f x - f y = z`."] lemma mk'_sec (z : N) : f.mk' (f.sec z).1 (f.sec z).2 = z := show _ * _ = _, by rw [←sec_spec, mul_inv_left, mul_comm] @[to_additive] lemma mk'_surjective (z : N) : ∃ x (y : S), f.mk' x y = z := ⟨(f.sec z).1, (f.sec z).2, f.mk'_sec z⟩ @[to_additive] lemma mk'_spec (x) (y : S) : f.mk' x y * f.to_map y = f.to_map x := show _ * _ * _ = _, by rw [mul_assoc, mul_comm _ (f.to_map y), ←mul_assoc, mul_inv_left, mul_comm] @[to_additive] lemma mk'_spec' (x) (y : S) : f.to_map y * f.mk' x y = f.to_map x := by rw [mul_comm, mk'_spec] @[to_additive] theorem eq_mk'_iff_mul_eq {x} {y : S} {z} : z = f.mk' x y ↔ z * f.to_map y = f.to_map x := ⟨λ H, by rw [H, mk'_spec], λ H, by erw [mul_inv_right, H]; refl⟩ @[to_additive] theorem mk'_eq_iff_eq_mul {x} {y : S} {z} : f.mk' x y = z ↔ f.to_map x = z * f.to_map y := by rw [eq_comm, eq_mk'_iff_mul_eq, eq_comm] @[to_additive] lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) := ⟨λ H, by rw [f.to_map.map_mul, f.mk'_eq_iff_eq_mul.1 H, mul_assoc, mul_comm (f.to_map _), ←mul_assoc, mk'_spec, f.to_map.map_mul], λ H, by rw [mk'_eq_iff_eq_mul, mk', mul_assoc, mul_comm _ (f.to_map y₁), ←mul_assoc, ←f.to_map.map_mul, ←H, f.to_map.map_mul, mul_inv_right f.map_units]⟩ @[to_additive] protected lemma eq {a₁ b₁} {a₂ b₂ : S} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : S, a₁ * b₂ * c = b₁ * a₂ * c := f.mk'_eq_iff_eq.trans $ f.eq_iff_exists @[to_additive] protected lemma eq' {a₁ b₁} {a₂ b₂ : S} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ localization.r S (a₁, a₂) (b₁, b₂) := by rw [f.eq, localization.r_iff_exists] @[to_additive] lemma eq_iff_eq (g : localization_map S P) {x y} : f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y := f.eq_iff_exists.trans g.eq_iff_exists.symm @[to_additive] lemma mk'_eq_iff_mk'_eq (g : localization_map S P) {x₁ x₂} {y₁ y₂ : S} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ := f.eq'.trans g.eq'.symm /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `f x₁ * (f y₁)⁻¹ * f y₂ = f x₂`, then there exists `c ∈ S` such that `x₁ * y₂ * c = x₂ * y₁ * c`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, for all `x₁ : M` and `y₁ ∈ S`, if `x₂ : M, y₂ ∈ S` are such that `(f x₁ - f y₁) + f y₂ = f x₂`, then there exists `c ∈ S` such that `x₁ + y₂ + c = x₂ + y₁ + c`."] lemma exists_of_sec_mk' (x) (y : S) : ∃ c : S, x * (f.sec $ f.mk' x y).2 * c = (f.sec $ f.mk' x y).1 * y * c := f.eq_iff_exists.1 $ f.mk'_eq_iff_eq.1 $ (mk'_sec _ _).symm @[to_additive] lemma mk'_eq_of_eq {a₁ b₁ : M} {a₂ b₂ : S} (H : b₁ * a₂ = a₁ * b₂) : f.mk' a₁ a₂ = f.mk' b₁ b₂ := f.mk'_eq_iff_eq.2 $ H ▸ rfl @[simp, to_additive] lemma mk'_self' (y : S) : f.mk' (y : M) y = 1 := show _ * _ = _, by rw [mul_inv_left, mul_one] @[simp, to_additive] lemma mk'_self (x) (H : x ∈ S) : f.mk' x ⟨x, H⟩ = 1 := by convert mk'_self' _ _; refl @[to_additive] lemma mul_mk'_eq_mk'_of_mul (x₁ x₂) (y : S) : f.to_map x₁ * f.mk' x₂ y = f.mk' (x₁ * x₂) y := by rw [←mk'_one, ←mk'_mul, one_mul] @[to_additive] lemma mk'_mul_eq_mk'_of_mul (x₁ x₂) (y : S) : f.mk' x₂ y * f.to_map x₁ = f.mk' (x₁ * x₂) y := by rw [mul_comm, mul_mk'_eq_mk'_of_mul] @[to_additive] lemma mul_mk'_one_eq_mk' (x) (y : S) : f.to_map x * f.mk' 1 y = f.mk' x y := by rw [mul_mk'_eq_mk'_of_mul, mul_one] @[simp, to_additive] lemma mk'_mul_cancel_right (x : M) (y : S) : f.mk' (x * y) y = f.to_map x := by rw [←mul_mk'_one_eq_mk', f.to_map.map_mul, mul_assoc, mul_mk'_one_eq_mk', mk'_self', mul_one] @[to_additive] lemma mk'_mul_cancel_left (x) (y : S) : f.mk' ((y : M) * x) y = f.to_map x := by rw [mul_comm, mk'_mul_cancel_right] @[to_additive] lemma is_unit_comp (j : N →* P) (y : S) : is_unit (j.comp f.to_map y) := ⟨units.map j $ is_unit.lift_right (f.to_map.mrestrict S) f.map_units y, show j _ = j _, from congr_arg j $ (is_unit.coe_lift_right (f.to_map.mrestrict S) f.map_units _)⟩ variables {g : M →* P} /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g(S) ⊆ units P`, `f x = f y → g x = g y` for all `x y : M`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g(S) ⊆ add_units P`, `f x = f y → g x = g y` for all `x y : M`."] lemma eq_of_eq (hg : ∀ y : S, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) : g x = g y := begin obtain ⟨c, hc⟩ := f.eq_iff_exists.1 h, rw [←mul_one (g x), ←is_unit.mul_lift_right_inv (g.mrestrict S) hg c], show _ * (g c * _) = _, rw [←mul_assoc, ←g.map_mul, hc, mul_inv_left hg, g.map_mul, mul_comm], end /-- Given `comm_monoid`s `M, P`, localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, and `g : M →* P` such that `g(S) ⊆ T`, `f x = f y` implies `k (g x) = k (g y)`. -/ @[to_additive "Given `add_comm_monoid`s `M, P`, localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, and `g : M →+ P` such that `g(S) ⊆ T`, `f x = f y` implies `k (g x) = k (g y)`."] lemma comp_eq_of_eq {T : submonoid P} {Q : Type*} [comm_monoid Q] (hg : ∀ y : S, g y ∈ T) (k : localization_map T Q) {x y} (h : f.to_map x = f.to_map y) : k.to_map (g x) = k.to_map (g y) := f.eq_of_eq (λ y : S, show is_unit (k.to_map.comp g y), from k.map_units ⟨g y, hg y⟩) h variables (hg : ∀ y : S, is_unit (g y)) /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` sending `z : N` to `g x * (g y)⁻¹`, where `(x, y) : M × S` are such that `z = f x * (f y)⁻¹`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` sending `z : N` to `g x - g y`, where `(x, y) : M × S` are such that `z = f x - f y`."] noncomputable def lift : N →* P := { to_fun := λ z, g (f.sec z).1 * ↑(is_unit.lift_right (g.mrestrict S) hg (f.sec z).2)⁻¹, map_one' := by rw [mul_inv_left, mul_one]; exact f.eq_of_eq hg (by rw [←sec_spec, one_mul]), map_mul' := λ x y, begin rw [mul_inv_left hg, ←mul_assoc, ←mul_assoc, mul_inv_right hg, mul_comm _ (g (f.sec y).1), ←mul_assoc, ←mul_assoc, mul_inv_right hg], repeat { rw ←g.map_mul }, exact f.eq_of_eq hg (by repeat { rw f.to_map.map_mul <|> rw sec_spec' }; ac_refl) end } variables {S g} /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M` and a map of `comm_monoid`s `g : M →* P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : M, y ∈ S`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M` and a map of `add_comm_monoid`s `g : M →+ P` such that `g y` is invertible for all `y : S`, the homomorphism induced from `N` to `P` maps `f x - f y` to `g x - g y` for all `x : M, y ∈ S`."] lemma lift_mk' (x y) : f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.mrestrict S) hg y)⁻¹ := (mul_inv hg).2 $ f.eq_of_eq hg $ by rw [f.to_map.map_mul, f.to_map.map_mul, sec_spec', mul_assoc, f.mk'_spec, mul_comm] /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N, v : P`, we have `f.lift hg z = v ↔ g x = g y + v`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_spec (z v) : f.lift hg z = v ↔ g (f.sec z).1 = g (f.sec z).2 * v := mul_inv_left hg _ _ v /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N, v w : P`, we have `f.lift hg z * w = v ↔ g x * w = g y * v`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N, v w : P`, we have `f.lift hg z + w = v ↔ g x + w = g y + v`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_spec_mul (z w v) : f.lift hg z * w = v ↔ g (f.sec z).1 * w = g (f.sec z).2 * v := begin rw mul_comm, show _ * (_ * _) = _ ↔ _, rw [←mul_assoc, mul_inv_left hg, mul_comm], end @[to_additive] lemma lift_mk'_spec (x v) (y : S) : f.lift hg (f.mk' x y) = v ↔ g x = g y * v := by rw f.lift_mk' hg; exact mul_inv_left hg _ _ _ /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have `f.lift hg z * g y = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we have `f.lift hg z + g y = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_mul_right (z) : f.lift hg z * g (f.sec z).2 = g (f.sec z).1 := show _ * _ * _ = _, by erw [mul_assoc, is_unit.lift_right_inv_mul, mul_one] /-- Given a localization map `f : M →* N` for a submonoid `S ⊆ M`, if a `comm_monoid` map `g : M →* P` induces a map `f.lift hg : N →* P` then for all `z : N`, we have `g y * f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S ⊆ M`, if an `add_comm_monoid` map `g : M →+ P` induces a map `f.lift hg : N →+ P` then for all `z : N`, we have `g y + f.lift hg z = g x`, where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma lift_mul_left (z) : g (f.sec z).2 * f.lift hg z = g (f.sec z).1 := by rw [mul_comm, lift_mul_right] @[simp, to_additive] lemma lift_eq (x : M) : f.lift hg (f.to_map x) = g x := by rw [lift_spec, ←g.map_mul]; exact f.eq_of_eq hg (by rw [sec_spec', f.to_map.map_mul]) @[to_additive] lemma lift_eq_iff {x y : M × S} : f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := by rw [lift_mk', lift_mk', mul_inv hg] @[simp, to_additive] lemma lift_comp : (f.lift hg).comp f.to_map = g := by ext; exact f.lift_eq hg _ @[simp, to_additive] lemma lift_of_comp (j : N →* P) : f.lift (f.is_unit_comp j) = j := begin ext, rw lift_spec, show j _ = j _ * _, erw [←j.map_mul, sec_spec'], end @[to_additive] lemma epic_of_localization_map {j k : N →* P} (h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k := begin rw [←f.lift_of_comp j, ←f.lift_of_comp k], congr' 1 with x, exact h x, end @[to_additive] lemma lift_unique {j : N →* P} (hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j := begin ext, rw [lift_spec, ←hj, ←hj, ←j.map_mul], apply congr_arg, rw ←sec_spec', end @[simp, to_additive] lemma lift_id (x) : f.lift f.map_units x = x := monoid_hom.ext_iff.1 (f.lift_of_comp $ monoid_hom.id N) x /-- Given two localization maps `f : M →* N, k : M →* P` for a submonoid `S ⊆ M`, the hom from `P` to `N` induced by `f` is left inverse to the hom from `N` to `P` induced by `k`. -/ @[simp, to_additive] lemma lift_left_inverse {k : localization_map S P} (z : N) : k.lift f.map_units (f.lift k.map_units z) = z := begin rw lift_spec, cases f.surj z with x hx, conv_rhs {congr, skip, rw f.eq_mk'_iff_mul_eq.2 hx}, rw [mk', ←mul_assoc, mul_inv_right f.map_units, ←f.to_map.map_mul, ←f.to_map.map_mul], apply k.eq_of_eq f.map_units, rw [k.to_map.map_mul, k.to_map.map_mul, ←sec_spec, mul_assoc, lift_spec_mul], repeat { rw ←k.to_map.map_mul }, apply f.eq_of_eq k.map_units, repeat { rw f.to_map.map_mul }, rw [sec_spec', ←hx], ac_refl, end @[to_additive] lemma lift_surjective_iff : function.surjective (f.lift hg) ↔ ∀ v : P, ∃ x : M × S, v * g x.2 = g x.1 := begin split, { intros H v, obtain ⟨z, hz⟩ := H v, obtain ⟨x, hx⟩ := f.surj z, use x, rw [←hz, f.eq_mk'_iff_mul_eq.2 hx, lift_mk', mul_assoc, mul_comm _ (g ↑x.2)], erw [is_unit.mul_lift_right_inv (g.mrestrict S) hg, mul_one] }, { intros H v, obtain ⟨x, hx⟩ := H v, use f.mk' x.1 x.2, rw [lift_mk', mul_inv_left hg, mul_comm, ←hx] } end @[to_additive] lemma lift_injective_iff : function.injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y := begin split, { intros H x y, split, { exact f.eq_of_eq hg }, { intro h, rw [←f.lift_eq hg, ←f.lift_eq hg] at h, exact H h } }, { intros H z w h, obtain ⟨x, hx⟩ := f.surj z, obtain ⟨y, hy⟩ := f.surj w, rw [←f.mk'_sec z, ←f.mk'_sec w], exact (mul_inv f.map_units).2 ((H _ _).2 $ (mul_inv hg).1 h) } end variables {T : submonoid P} (hy : ∀ y : S, g y ∈ T) {Q : Type*} [comm_monoid Q] (k : localization_map T Q) /-- Given a `comm_monoid` homomorphism `g : M →* P` where for submonoids `S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced monoid homomorphism from the localization of `M` at `S` to the localization of `P` at `T`: if `f : M →* N` and `k : P →* Q` are localization maps for `S` and `T` respectively, we send `z : N` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : M × S` are such that `z = f x * (f y)⁻¹`. -/ @[to_additive "Given a `add_comm_monoid` homomorphism `g : M →+ P` where for submonoids `S ⊆ M, T ⊆ P` we have `g(S) ⊆ T`, the induced add_monoid homomorphism from the localization of `M` at `S` to the localization of `P` at `T`: if `f : M →+ N` and `k : P →+ Q` are localization maps for `S` and `T` respectively, we send `z : N` to `k (g x) - k (g y)`, where `(x, y) : M × S` are such that `z = f x - f y`."] noncomputable def map : N →* Q := @lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩ variables {k} @[to_additive] lemma map_eq (x) : f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x @[simp, to_additive] lemma map_comp : (f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩ @[to_additive] lemma map_mk' (x) (y : S) : f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ := begin rw [map, lift_mk', mul_inv_left], { show k.to_map (g x) = k.to_map (g y) * _, rw mul_mk'_eq_mk'_of_mul, exact (k.mk'_mul_cancel_left (g x) ⟨(g y), hy y⟩).symm }, end /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) * u` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, `u : Q`, we have `f.map hy k z = u ↔ k (g x) = k (g y) + u` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_spec (z u) : f.map hy k z = u ↔ k.to_map (g (f.sec z).1) = k.to_map (g (f.sec z).2) * u := f.lift_spec (λ y, k.map_units ⟨g y, hy y⟩) _ _ /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, we have `f.map hy k z * k (g y) = k (g x)` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, we have `f.map hy k z + k (g y) = k (g x)` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_mul_right (z) : f.map hy k z * (k.to_map (g (f.sec z).2)) = k.to_map (g (f.sec z).1) := f.lift_mul_right (λ y, k.map_units ⟨g y, hy y⟩) _ /-- Given localization maps `f : M →* N, k : P →* Q` for submonoids `S, T` respectively, if a `comm_monoid` homomorphism `g : M →* P` induces a `f.map hy k : N →* Q`, then for all `z : N`, we have `k (g y) * f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that `z * f y = f x`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ Q` for submonoids `S, T` respectively, if an `add_comm_monoid` homomorphism `g : M →+ P` induces a `f.map hy k : N →+ Q`, then for all `z : N`, we have `k (g y) + f.map hy k z = k (g x)` where `x : M, y ∈ S` are such that `z + f y = f x`."] lemma map_mul_left (z) : k.to_map (g (f.sec z).2) * f.map hy k z = k.to_map (g (f.sec z).1) := by rw [mul_comm, f.map_mul_right] @[simp, to_additive] lemma map_id (z : N) : f.map (λ y, show monoid_hom.id M y ∈ S, from y.2) f z = z := f.lift_id z /-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ @[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`."] lemma map_comp_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R] (j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) : (k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j := begin ext z, show j.to_map _ * _ = j.to_map (l _) * _, { rw [mul_inv_left, ←mul_assoc, mul_inv_right], show j.to_map _ * j.to_map (l (g _)) = j.to_map (l _) * _, rw [←j.to_map.map_mul, ←j.to_map.map_mul, ←l.map_mul, ←l.map_mul], exact k.comp_eq_of_eq hl j (by rw [k.to_map.map_mul, k.to_map.map_mul, sec_spec', mul_assoc, map_mul_right]) }, end /-- If `comm_monoid` homs `g : M →* P, l : P →* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ @[to_additive "If `add_comm_monoid` homs `g : M →+ P, l : P →+ A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`."] lemma map_map {A : Type*} [comm_monoid A] {U : submonoid A} {R} [comm_monoid R] (j : localization_map U R) {l : P →* A} (hl : ∀ w : T, l w ∈ U) (x) : k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x := by rw ←f.map_comp_map hy j hl; refl section away_map variables (x : M) /-- Given `x : M`, the type of `comm_monoid` homomorphisms `f : M →* N` such that `N` is isomorphic to the localization of `M` at the submonoid generated by `x`. -/ @[reducible, to_additive "Given `x : M`, the type of `add_comm_monoid` homomorphisms `f : M →+ N` such that `N` is isomorphic to the localization of `M` at the submonoid generated by `x`."] def away_map (N' : Type*) [comm_monoid N'] := localization_map (powers x) N' variables (F : away_map x N) /-- Given `x : M` and a localization map `F : M →* N` away from `x`, `inv_self` is `(F x)⁻¹`. -/ noncomputable def away_map.inv_self : N := F.mk' 1 ⟨x, mem_powers _⟩ /-- Given `x : M`, a localization map `F : M →* N` away from `x`, and a map of `comm_monoid`s `g : M →* P` such that `g x` is invertible, the homomorphism induced from `N` to `P` sending `z : N` to `g y * (g x)⁻ⁿ`, where `y : M, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/ noncomputable def away_map.lift (hg : is_unit (g x)) : N →* P := F.lift $ λ y, show is_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_pow], exact is_unit.pow n hg, end @[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : M) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : M` and localization maps `F : M →* N, G : M →* P` away from `x` and `x * y` respectively, the homomorphism induced from `N` to `P`. -/ noncomputable def away_to_away_right (y : M) (G : away_map (x * y) P) : N →* P := F.lift x $ show is_unit (G.to_map x), from is_unit_of_mul_eq_one (G.to_map x) (G.mk' y ⟨x * y, mem_powers _⟩) $ by rw [mul_mk'_eq_mk'_of_mul, mk'_self] end away_map end localization_map end submonoid namespace add_submonoid namespace localization_map section away_map variables {A : Type*} [add_comm_monoid A] (x : A) {B : Type*} [add_comm_monoid B] (F : away_map x B) {C : Type*} [add_comm_monoid C] {g : A →+ C} /-- Given `x : A` and a localization map `F : A →+ B` away from `x`, `neg_self` is `- (F x)`. -/ noncomputable def away_map.neg_self : B := F.mk' 0 ⟨x, mem_multiples _⟩ /-- Given `x : A`, a localization map `F : A →+ B` away from `x`, and a map of `add_comm_monoid`s `g : A →+ C` such that `g x` is invertible, the homomorphism induced from `B` to `C` sending `z : B` to `g y - n • g x`, where `y : A, n : ℕ` are such that `z = F y - n • F x`. -/ noncomputable def away_map.lift (hg : is_add_unit (g x)) : B →+ C := F.lift $ λ y, show is_add_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw ← hn, dsimp, rw [g.map_nsmul], exact is_add_unit.map (nsmul_add_monoid_hom n) hg, end @[simp] lemma away_map.lift_eq (hg : is_add_unit (g x)) (a : A) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_add_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : A` and localization maps `F : A →+ B, G : A →+ C` away from `x` and `x + y` respectively, the homomorphism induced from `B` to `C`. -/ noncomputable def away_to_away_right (y : A) (G : away_map (x + y) C) : B →+ C := F.lift x $ show is_add_unit (G.to_map x), from is_add_unit_of_add_eq_zero (G.to_map x) (G.mk' y ⟨x + y, mem_multiples _⟩) $ by rw [add_mk'_eq_mk'_of_add, mk'_self] end away_map end localization_map end add_submonoid namespace submonoid namespace localization_map variables (f : S.localization_map N) {g : M →* P} (hg : ∀ (y : S), is_unit (g y)) {T : submonoid P} {Q : Type*} [comm_monoid Q] /-- If `f : M →* N` and `k : M →* P` are localization maps for a submonoid `S`, we get an isomorphism of `N` and `P`. -/ @[to_additive "If `f : M →+ N` and `k : M →+ R` are localization maps for a submonoid `S`, we get an isomorphism of `N` and `R`."] noncomputable def mul_equiv_of_localizations (k : localization_map S P) : N ≃* P := ⟨f.lift k.map_units, k.lift f.map_units, f.lift_left_inverse, k.lift_left_inverse, monoid_hom.map_mul _⟩ @[simp, to_additive] lemma mul_equiv_of_localizations_apply {k : localization_map S P} {x} : f.mul_equiv_of_localizations k x = f.lift k.map_units x := rfl @[simp, to_additive] lemma mul_equiv_of_localizations_symm_apply {k : localization_map S P} {x} : (f.mul_equiv_of_localizations k).symm x = k.lift f.map_units x := rfl @[to_additive] lemma mul_equiv_of_localizations_symm_eq_mul_equiv_of_localizations {k : localization_map S P} : (k.mul_equiv_of_localizations f).symm = f.mul_equiv_of_localizations k := rfl /-- If `f : M →* N` is a localization map for a submonoid `S` and `k : N ≃* P` is an isomorphism of `comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`. -/ @[to_additive "If `f : M →+ N` is a localization map for a submonoid `S` and `k : N ≃+ P` is an isomorphism of `add_comm_monoid`s, `k ∘ f` is a localization map for `M` at `S`."] def of_mul_equiv_of_localizations (k : N ≃* P) : localization_map S P := (k.to_monoid_hom.comp f.to_map).to_localization_map (λ y, is_unit_comp f k.to_monoid_hom y) (λ v, let ⟨z, hz⟩ := k.to_equiv.surjective v in let ⟨x, hx⟩ := f.surj z in ⟨x, show v * k _ = k _, by rw [←hx, k.map_mul, ←hz]; refl⟩) (λ x y, k.apply_eq_iff_eq.trans f.eq_iff_exists) @[simp, to_additive] lemma of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) : (f.of_mul_equiv_of_localizations k).to_map x = k (f.to_map x) := rfl @[to_additive] lemma of_mul_equiv_of_localizations_eq {k : N ≃* P} : (f.of_mul_equiv_of_localizations k).to_map = k.to_monoid_hom.comp f.to_map := rfl @[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply {k : N ≃* P} (x) : k.symm ((f.of_mul_equiv_of_localizations k).to_map x) = f.to_map x := k.symm_apply_apply (f.to_map x) @[to_additive] lemma symm_comp_of_mul_equiv_of_localizations_apply' {k : P ≃* N} (x) : k ((f.of_mul_equiv_of_localizations k.symm).to_map x) = f.to_map x := k.apply_symm_apply (f.to_map x) @[to_additive] lemma of_mul_equiv_of_localizations_eq_iff_eq {k : N ≃* P} {x y} : (f.of_mul_equiv_of_localizations k).to_map x = y ↔ f.to_map x = k.symm y := k.to_equiv.eq_symm_apply.symm @[to_additive add_equiv_of_localizations_right_inv] lemma mul_equiv_of_localizations_right_inv (k : localization_map S P) : f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k) = k := to_map_injective $ f.lift_comp k.map_units @[simp, to_additive add_equiv_of_localizations_right_inv_apply] lemma mul_equiv_of_localizations_right_inv_apply {k : localization_map S P} {x} : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_localizations k)).to_map x = k.to_map x := ext_iff.1 (f.mul_equiv_of_localizations_right_inv k) x @[to_additive] lemma mul_equiv_of_localizations_left_inv (k : N ≃* P) : f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) = k := mul_equiv.ext $ monoid_hom.ext_iff.1 $ f.lift_of_comp k.to_monoid_hom @[simp, to_additive] lemma mul_equiv_of_localizations_left_inv_apply {k : N ≃* P} (x) : f.mul_equiv_of_localizations (f.of_mul_equiv_of_localizations k) x = k x := by rw mul_equiv_of_localizations_left_inv @[simp, to_additive] lemma of_mul_equiv_of_localizations_id : f.of_mul_equiv_of_localizations (mul_equiv.refl N) = f := by ext; refl @[to_additive] lemma of_mul_equiv_of_localizations_comp {k : N ≃* P} {j : P ≃* Q} : (f.of_mul_equiv_of_localizations (k.trans j)).to_map = j.to_monoid_hom.comp (f.of_mul_equiv_of_localizations k).to_map := by ext; refl /-- Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k` is a localization map for `T`. -/ @[to_additive "Given `comm_monoid`s `M, P` and submonoids `S ⊆ M, T ⊆ P`, if `f : M →* N` is a localization map for `S` and `k : P ≃* M` is an isomorphism of `comm_monoid`s such that `k(T) = S`, `f ∘ k` is a localization map for `T`."] def of_mul_equiv_of_dom {k : P ≃* M} (H : T.map k.to_monoid_hom = S) : localization_map T N := let H' : S.comap k.to_monoid_hom = T := H ▸ (set_like.coe_injective $ T.1.preimage_image_eq k.to_equiv.injective) in (f.to_map.comp k.to_monoid_hom).to_localization_map (λ y, let ⟨z, hz⟩ := f.map_units ⟨k y, H ▸ set.mem_image_of_mem k y.2⟩ in ⟨z, hz⟩) (λ z, let ⟨x, hx⟩ := f.surj z in let ⟨v, hv⟩ := k.to_equiv.surjective x.1 in let ⟨w, hw⟩ := k.to_equiv.surjective x.2 in ⟨(v, ⟨w, H' ▸ show k w ∈ S, from hw.symm ▸ x.2.2⟩), show z * f.to_map (k.to_equiv w) = f.to_map (k.to_equiv v), by erw [hv, hw, hx]; refl⟩) (λ x y, show f.to_map _ = f.to_map _ ↔ _, by erw f.eq_iff_exists; exact ⟨λ ⟨c, hc⟩, let ⟨d, hd⟩ := k.to_equiv.surjective c in ⟨⟨d, H' ▸ show k d ∈ S, from hd.symm ▸ c.2⟩, by erw [←hd, ←k.map_mul, ←k.map_mul] at hc; exact k.to_equiv.injective hc⟩, λ ⟨c, hc⟩, ⟨⟨k c, H ▸ set.mem_image_of_mem k c.2⟩, by erw ←k.map_mul; rw [hc, k.map_mul]; refl⟩⟩) @[simp, to_additive] lemma of_mul_equiv_of_dom_apply {k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map x = f.to_map (k x) := rfl @[to_additive] lemma of_mul_equiv_of_dom_eq {k : P ≃* M} (H : T.map k.to_monoid_hom = S) : (f.of_mul_equiv_of_dom H).to_map = f.to_map.comp k.to_monoid_hom := rfl @[to_additive] lemma of_mul_equiv_of_dom_comp_symm {k : P ≃* M} (H : T.map k.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map (k.symm x) = f.to_map x := congr_arg f.to_map $ k.apply_symm_apply x @[to_additive] lemma of_mul_equiv_of_dom_comp {k : M ≃* P} (H : T.map k.symm.to_monoid_hom = S) (x) : (f.of_mul_equiv_of_dom H).to_map (k x) = f.to_map x := congr_arg f.to_map $ k.symm_apply_apply x /-- A special case of `f ∘ id = f`, `f` a localization map. -/ @[simp, to_additive "A special case of `f ∘ id = f`, `f` a localization map."] lemma of_mul_equiv_of_dom_id : f.of_mul_equiv_of_dom (show S.map (mul_equiv.refl M).to_monoid_hom = S, from submonoid.ext $ λ x, ⟨λ ⟨y, hy, h⟩, h ▸ hy, λ h, ⟨x, h, rfl⟩⟩) = f := by ext; refl /-- Given localization maps `f : M →* N, k : P →* U` for submonoids `S, T` respectively, an isomorphism `j : M ≃* P` such that `j(S) = T` induces an isomorphism of localizations `N ≃* U`. -/ @[to_additive "Given localization maps `f : M →+ N, k : P →+ U` for submonoids `S, T` respectively, an isomorphism `j : M ≃+ P` such that `j(S) = T` induces an isomorphism of localizations `N ≃+ U`."] noncomputable def mul_equiv_of_mul_equiv (k : localization_map T Q) {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : N ≃* Q := f.mul_equiv_of_localizations $ k.of_mul_equiv_of_dom H @[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq_map_apply {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : f.mul_equiv_of_mul_equiv k H x = f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl @[to_additive] lemma mul_equiv_of_mul_equiv_eq_map {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : (f.mul_equiv_of_mul_equiv k H).to_monoid_hom = f.map (λ y : S, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl @[simp, to_additive] lemma mul_equiv_of_mul_equiv_eq {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : f.mul_equiv_of_mul_equiv k H (f.to_map x) = k.to_map (j x) := f.map_eq (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ @[simp, to_additive] lemma mul_equiv_of_mul_equiv_mk' {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x y) : f.mul_equiv_of_mul_equiv k H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ := f.map_mk' (λ y : S, H ▸ set.mem_image_of_mem j y.2) _ _ @[simp, to_additive] lemma of_mul_equiv_of_mul_equiv_apply {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) (x) : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map x = k.to_map (j x) := ext_iff.1 (f.mul_equiv_of_localizations_right_inv (k.of_mul_equiv_of_dom H)) x @[to_additive] lemma of_mul_equiv_of_mul_equiv {k : localization_map T Q} {j : M ≃* P} (H : S.map j.to_monoid_hom = T) : (f.of_mul_equiv_of_localizations (f.mul_equiv_of_mul_equiv k H)).to_map = k.to_map.comp j.to_monoid_hom := monoid_hom.ext $ f.of_mul_equiv_of_mul_equiv_apply H end localization_map end submonoid namespace localization variables (S) /-- Natural hom sending `x : M`, `M` a `comm_monoid`, to the equivalence class of `(x, 1)` in the localization of `M` at a submonoid. -/ @[to_additive "Natural homomorphism sending `x : M`, `M` an `add_comm_monoid`, to the equivalence class of `(x, 0)` in the localization of `M` at a submonoid."] def monoid_of : submonoid.localization_map S (localization S) := { to_fun := λ x, mk x 1, map_one' := mk_one, map_mul' := λ x y, by rw [mk_mul, mul_one], map_units' := λ y, is_unit_iff_exists_inv.2 ⟨mk 1 y, by rw [mk_mul, mul_one, one_mul, mk_self]⟩, surj' := λ z, induction_on z $ λ x, ⟨x, by rw [mk_mul, mul_comm x.fst, ← mk_mul, mk_self, one_mul]⟩, eq_iff_exists' := λ x y, mk_eq_mk_iff.trans $ r_iff_exists.trans $ show (∃ (c : S), x * 1 * c = y * 1 * c) ↔ _, by rw [mul_one, mul_one], ..(r S).mk'.comp $ monoid_hom.inl M S } variables {S} @[to_additive] lemma mk_one_eq_monoid_of_mk (x) : mk x 1 = (monoid_of S).to_map x := rfl @[to_additive] lemma mk_eq_monoid_of_mk'_apply (x y) : mk x y = (monoid_of S).mk' x y := show _ = _ * _, from (submonoid.localization_map.mul_inv_right (monoid_of S).map_units _ _ _).2 $ begin rw [←mk_one_eq_monoid_of_mk, ←mk_one_eq_monoid_of_mk, show mk x y * mk y 1 = mk (x * y) (1 * y), by rw [mul_comm 1 y, mk_mul], show mk x 1 = mk (x * 1) ((1 : S) * 1), by rw [mul_one, mul_one]], exact mk_eq_mk_iff.2 (con.symm _ $ (localization.r S).mul (con.refl _ (x, 1)) $ one_rel _), end @[simp, to_additive] lemma mk_eq_monoid_of_mk' : mk = (monoid_of S).mk' := funext $ λ _, funext $ λ _, mk_eq_monoid_of_mk'_apply _ _ universes u @[simp, to_additive] lemma lift_on_mk' {p : Sort u} (f : ∀ (a : M) (b : S), p) (H) (a : M) (b : S) : lift_on ((monoid_of S).mk' a b) f H = f a b := by rw [← mk_eq_monoid_of_mk', lift_on_mk] @[simp, to_additive] lemma lift_on₂_mk' {p : Sort*} (f : M → S → M → S → p) (H) (a c : M) (b d : S) : lift_on₂ ((monoid_of S).mk' a b) ((monoid_of S).mk' c d) f H = f a b c d := by rw [← mk_eq_monoid_of_mk', lift_on₂_mk] variables (f : submonoid.localization_map S N) /-- Given a localization map `f : M →* N` for a submonoid `S`, we get an isomorphism between the localization of `M` at `S` as a quotient type and `N`. -/ @[to_additive "Given a localization map `f : M →+ N` for a submonoid `S`, we get an isomorphism between the localization of `M` at `S` as a quotient type and `N`."] noncomputable def mul_equiv_of_quotient (f : submonoid.localization_map S N) : localization S ≃* N := (monoid_of S).mul_equiv_of_localizations f variables {f} @[simp, to_additive] lemma mul_equiv_of_quotient_apply (x) : mul_equiv_of_quotient f x = (monoid_of S).lift f.map_units x := rfl @[simp, to_additive] lemma mul_equiv_of_quotient_mk' (x y) : mul_equiv_of_quotient f ((monoid_of S).mk' x y) = f.mk' x y := (monoid_of S).lift_mk' _ _ _ @[to_additive] lemma mul_equiv_of_quotient_mk (x y) : mul_equiv_of_quotient f (mk x y) = f.mk' x y := by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_mk' _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_monoid_of (x) : mul_equiv_of_quotient f ((monoid_of S).to_map x) = f.to_map x := (monoid_of S).lift_eq _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_symm_mk' (x y) : (mul_equiv_of_quotient f).symm (f.mk' x y) = (monoid_of S).mk' x y := f.lift_mk' _ _ _ @[to_additive] lemma mul_equiv_of_quotient_symm_mk (x y) : (mul_equiv_of_quotient f).symm (f.mk' x y) = mk x y := by rw mk_eq_monoid_of_mk'_apply; exact mul_equiv_of_quotient_symm_mk' _ _ @[simp, to_additive] lemma mul_equiv_of_quotient_symm_monoid_of (x) : (mul_equiv_of_quotient f).symm (f.to_map x) = (monoid_of S).to_map x := f.lift_eq _ _ section away variables (x : M) /-- Given `x : M`, the localization of `M` at the submonoid generated by `x`, as a quotient. -/ @[reducible, to_additive "Given `x : M`, the localization of `M` at the submonoid generated by `x`, as a quotient."] def away := localization (submonoid.powers x) /-- Given `x : M`, `inv_self` is `x⁻¹` in the localization (as a quotient type) of `M` at the submonoid generated by `x`. -/ @[to_additive "Given `x : M`, `neg_self` is `-x` in the localization (as a quotient type) of `M` at the submonoid generated by `x`."] def away.inv_self : away x := mk 1 ⟨x, submonoid.mem_powers _⟩ /-- Given `x : M`, the natural hom sending `y : M`, `M` a `comm_monoid`, to the equivalence class of `(y, 1)` in the localization of `M` at the submonoid generated by `x`. -/ @[reducible, to_additive "Given `x : M`, the natural hom sending `y : M`, `M` an `add_comm_monoid`, to the equivalence class of `(y, 0)` in the localization of `M` at the submonoid generated by `x`."] def away.monoid_of : submonoid.localization_map.away_map x (away x) := monoid_of (submonoid.powers x) @[simp, to_additive] lemma away.mk_eq_monoid_of_mk' : mk = (away.monoid_of x).mk' := mk_eq_monoid_of_mk' /-- Given `x : M` and a localization map `f : M →* N` away from `x`, we get an isomorphism between the localization of `M` at the submonoid generated by `x` as a quotient type and `N`. -/ @[to_additive "Given `x : M` and a localization map `f : M →+ N` away from `x`, we get an isomorphism between the localization of `M` at the submonoid generated by `x` as a quotient type and `N`."] noncomputable def away.mul_equiv_of_quotient (f : submonoid.localization_map.away_map x N) : away x ≃* N := mul_equiv_of_quotient f end away end localization
613c6c2cabcf494eaa5d1f0802607bd3cc8a1b5c
842b7df4a999c5c50bbd215b8617dd705e43c2e1
/nat_num_game/src/Advanced_Addition_World/adv_add_wrld4.lean
8ae50fb5001ba566414d9aaa02eccce9d43362e4
[]
no_license
Samyak-Surti/LeanCode
1c245631f74b00057d20483c8ac75916e8643b14
944eac3e5f43e2614ed246083b97fbdf24181d83
refs/heads/master
1,669,023,730,828
1,595,534,784,000
1,595,534,784,000
282,037,186
0
0
null
null
null
null
UTF-8
Lean
false
false
195
lean
theorem succ_eq_succ_iff (a b : ℕ) : nat.succ a = nat.succ b ↔ a = b := begin split, intro h, exact nat.succ.inj h, intro f, repeat {rw nat.succ_eq_add_one}, rw f, end
1e92cf6d848f7dfb6b1317de7a13cda684b02c3b
9b9a16fa2cb737daee6b2785474678b6fa91d6d4
/src/topology/instances/polynomial.lean
dde624d19a2143750f79e7240ffe1532a813ae0c
[ "Apache-2.0" ]
permissive
johoelzl/mathlib
253f46daa30b644d011e8e119025b01ad69735c4
592e3c7a2dfbd5826919b4605559d35d4d75938f
refs/heads/master
1,625,657,216,488
1,551,374,946,000
1,551,374,946,000
98,915,829
0
0
Apache-2.0
1,522,917,267,000
1,501,524,499,000
Lean
UTF-8
Lean
false
false
940
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis Analytic facts about polynomials. -/ import topology.algebra.topological_structures data.polynomial lemma polynomial.continuous_eval {α} [comm_semiring α] [decidable_eq α] [topological_space α] [topological_semiring α] (p : polynomial α) : continuous (λ x, p.eval x) := begin apply p.induction, { convert continuous_const, ext, show polynomial.eval x 0 = 0, from rfl }, { intros a b f haf hb hcts, simp only [polynomial.eval_add], refine continuous_add _ hcts, have : ∀ x, finsupp.sum (finsupp.single a b) (λ (e : ℕ) (a : α), a * x ^ e) = b * x ^a, from λ x, finsupp.sum_single_index (by simp), convert continuous_mul _ _, { ext, apply this }, { apply_instance }, { apply continuous_const }, { apply continuous_pow }} end
7bcd267cf91ffcd9d73388085233b7123e4a6d65
b074a51e20fdb737b2d4c635dd292fc54685e010
/src/algebra/ring.lean
556dd16dd2fc6710c24bf860fccd08c5cccc6838
[ "Apache-2.0" ]
permissive
minchaowu/mathlib
2daf6ffdb5a56eeca403e894af88bcaaf65aec5e
879da1cf04c2baa9eaa7bd2472100bc0335e5c73
refs/heads/master
1,609,628,676,768
1,564,310,105,000
1,564,310,105,000
99,461,307
0
0
null
null
null
null
UTF-8
Lean
false
false
11,805
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn -/ import algebra.group universes u v variable {α : Type u} section variable [semiring α] theorem mul_two (n : α) : n * 2 = n + n := (left_distrib n 1 1).trans (by simp) theorem bit0_eq_two_mul (n : α) : bit0 n = 2 * n := (two_mul _).symm variable (α) lemma zero_ne_one_or_forall_eq_0 : (0 : α) ≠ 1 ∨ (∀a:α, a = 0) := by haveI := classical.dec; refine not_or_of_imp (λ h a, _); simpa using congr_arg ((*) a) h.symm lemma eq_zero_of_zero_eq_one (h : (0 : α) = 1) : (∀a:α, a = 0) := (zero_ne_one_or_forall_eq_0 α).neg_resolve_left h theorem subsingleton_of_zero_eq_one (h : (0 : α) = 1) : subsingleton α := ⟨λa b, by rw [eq_zero_of_zero_eq_one α h a, eq_zero_of_zero_eq_one α h b]⟩ end namespace units variables [ring α] {a b : α} instance : has_neg (units α) := ⟨λu, ⟨-↑u, -↑u⁻¹, by simp, by simp⟩ ⟩ @[simp] protected theorem coe_neg (u : units α) : (↑-u : α) = -u := rfl @[simp] protected theorem neg_inv (u : units α) : (-u)⁻¹ = -u⁻¹ := rfl @[simp] protected theorem neg_neg (u : units α) : - -u = u := units.ext $ neg_neg _ @[simp] protected theorem neg_mul (u₁ u₂ : units α) : -u₁ * u₂ = -(u₁ * u₂) := units.ext $ neg_mul_eq_neg_mul_symm _ _ @[simp] protected theorem mul_neg (u₁ u₂ : units α) : u₁ * -u₂ = -(u₁ * u₂) := units.ext $ (neg_mul_eq_mul_neg _ _).symm @[simp] protected theorem neg_mul_neg (u₁ u₂ : units α) : -u₁ * -u₂ = u₁ * u₂ := by simp protected theorem neg_eq_neg_one_mul (u : units α) : -u = -1 * u := by simp end units instance [semiring α] : semiring (with_zero α) := { left_distrib := λ a b c, begin cases a with a, {refl}, cases b with b; cases c with c; try {refl}, exact congr_arg some (left_distrib _ _ _) end, right_distrib := λ a b c, begin cases c with c, { change (a + b) * 0 = a * 0 + b * 0, simp }, cases a with a; cases b with b; try {refl}, exact congr_arg some (right_distrib _ _ _) end, ..with_zero.add_comm_monoid, ..with_zero.mul_zero_class, ..with_zero.monoid } attribute [refl] dvd_refl attribute [trans] dvd.trans class is_semiring_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) : Prop := (map_zero : f 0 = 0) (map_one : f 1 = 1) (map_add : ∀ {x y}, f (x + y) = f x + f y) (map_mul : ∀ {x y}, f (x * y) = f x * f y) namespace is_semiring_hom variables {β : Type v} [semiring α] [semiring β] variables (f : α → β) [is_semiring_hom f] {x y : α} instance id : is_semiring_hom (@id α) := by refine {..}; intros; refl instance comp {γ} [semiring γ] (g : β → γ) [is_semiring_hom g] : is_semiring_hom (g ∘ f) := { map_zero := by simp [map_zero f]; exact map_zero g, map_one := by simp [map_one f]; exact map_one g, map_add := λ x y, by simp [map_add f]; rw map_add g; refl, map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl } instance : is_add_monoid_hom f := { ..‹is_semiring_hom f› } instance : is_monoid_hom f := { ..‹is_semiring_hom f› } end is_semiring_hom section variables [ring α] (a b c d e : α) lemma mul_neg_one (a : α) : a * -1 = -a := by simp lemma neg_one_mul (a : α) : -1 * a = -a := by simp theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp ... ↔ a * e + c - b * e = d : iff.intro (λ h, begin simp [h] end) (λ h, begin simp [h.symm] end) ... ↔ (a - b) * e + c = d : begin simp [@sub_eq_add_neg α, @right_distrib α] end theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := assume h, calc (a - b) * e + c = (a * e + c) - b * e : begin simp [@sub_eq_add_neg α, @right_distrib α] end ... = d : begin rw h, simp [@add_sub_cancel α] end theorem ne_zero_and_ne_zero_of_mul_ne_zero {a b : α} (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 := begin split, { intro ha, apply h, simp [ha] }, { intro hb, apply h, simp [hb] } end end @[simp] lemma zero_dvd_iff [comm_semiring α] {a : α} : 0 ∣ a ↔ a = 0 := ⟨eq_zero_of_zero_dvd, λ h, by rw h⟩ section comm_ring variable [comm_ring α] theorem mul_self_sub_mul_self (a b : α) : a * a - b * b = (a + b) * (a - b) := by rw [add_mul, mul_sub, mul_sub, mul_comm a b, sub_add_sub_cancel] @[simp] lemma dvd_neg (a b : α) : (a ∣ -b) ↔ (a ∣ b) := ⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩ @[simp] lemma neg_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) := ⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩ theorem dvd_add_left {a b c : α} (h : a ∣ c) : a ∣ b + c ↔ a ∣ b := (dvd_add_iff_left h).symm theorem dvd_add_right {a b c : α} (h : a ∣ b) : a ∣ b + c ↔ a ∣ c := (dvd_add_iff_right h).symm end comm_ring class is_ring_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) : Prop := (map_one : f 1 = 1) (map_mul : ∀ {x y}, f (x * y) = f x * f y) (map_add : ∀ {x y}, f (x + y) = f x + f y) namespace is_ring_hom variables {β : Type v} [ring α] [ring β] def of_semiring (f : α → β) [H : is_semiring_hom f] : is_ring_hom f := {..H} variables (f : α → β) [is_ring_hom f] {x y : α} lemma map_zero : f 0 = 0 := calc f 0 = f (0 + 0) - f 0 : by rw [map_add f]; simp ... = 0 : by simp lemma map_neg : f (-x) = -f x := calc f (-x) = f (-x + x) - f x : by rw [map_add f]; simp ... = -f x : by simp [map_zero f] lemma map_sub : f (x - y) = f x - f y := by simp [map_add f, map_neg f] instance id : is_ring_hom (@id α) := by refine {..}; intros; refl instance comp {γ} [ring γ] (g : β → γ) [is_ring_hom g] : is_ring_hom (g ∘ f) := { map_add := λ x y, by simp [map_add f]; rw map_add g; refl, map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl, map_one := by simp [map_one f]; exact map_one g } instance : is_semiring_hom f := { map_zero := map_zero f, ..‹is_ring_hom f› } instance : is_add_group_hom f := { } end is_ring_hom set_option old_structure_cmd true class nonzero_comm_semiring (α : Type*) extends comm_semiring α, zero_ne_one_class α class nonzero_comm_ring (α : Type*) extends comm_ring α, zero_ne_one_class α instance nonzero_comm_ring.to_nonzero_comm_semiring {α : Type*} [I : nonzero_comm_ring α] : nonzero_comm_semiring α := { zero_ne_one := by convert zero_ne_one, ..show comm_semiring α, by apply_instance } instance integral_domain.to_nonzero_comm_ring (α : Type*) [id : integral_domain α] : nonzero_comm_ring α := { ..id } lemma units.coe_ne_zero [nonzero_comm_semiring α] (u : units α) : (u : α) ≠ 0 := λ h : u.1 = 0, by simpa [h, zero_ne_one] using u.3 def nonzero_comm_ring.of_ne [comm_ring α] {x y : α} (h : x ≠ y) : nonzero_comm_ring α := { one := 1, zero := 0, zero_ne_one := λ h01, h $ by rw [← one_mul x, ← one_mul y, ← h01, zero_mul, zero_mul], ..show comm_ring α, by apply_instance } def nonzero_comm_semiring.of_ne [comm_semiring α] {x y : α} (h : x ≠ y) : nonzero_comm_semiring α := { one := 1, zero := 0, zero_ne_one := λ h01, h $ by rw [← one_mul x, ← one_mul y, ← h01, zero_mul, zero_mul], ..show comm_semiring α, by apply_instance } /-- this is needed for compatibility between Lean 3.4.2 and Lean 3.5.0c -/ def has_div_of_division_ring [division_ring α] : has_div α := division_ring_has_div /-- A domain is a ring with no zero divisors, i.e. satisfying the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, a domain is an integral domain without assuming commutativity of multiplication. -/ class domain (α : Type u) extends ring α, no_zero_divisors α, zero_ne_one_class α section domain variable [domain α] @[simp] theorem mul_eq_zero {a b : α} : a * b = 0 ↔ a = 0 ∨ b = 0 := ⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo, or.elim o (λh, by rw h; apply zero_mul) (λh, by rw h; apply mul_zero)⟩ @[simp] theorem zero_eq_mul {a b : α} : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, mul_eq_zero] theorem mul_ne_zero' {a b : α} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 := λ h, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) h₁ h₂ theorem domain.mul_right_inj {a b c : α} (ha : a ≠ 0) : b * a = c * a ↔ b = c := by rw [← sub_eq_zero, ← mul_sub_right_distrib, mul_eq_zero]; simp [ha]; exact sub_eq_zero theorem domain.mul_left_inj {a b c : α} (ha : a ≠ 0) : a * b = a * c ↔ b = c := by rw [← sub_eq_zero, ← mul_sub_left_distrib, mul_eq_zero]; simp [ha]; exact sub_eq_zero theorem eq_zero_of_mul_eq_self_right' {a b : α} (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 := by apply (mul_eq_zero.1 _).resolve_right (sub_ne_zero.2 h₁); rw [mul_sub_left_distrib, mul_one, sub_eq_zero, h₂] theorem eq_zero_of_mul_eq_self_left' {a b : α} (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 := by apply (mul_eq_zero.1 _).resolve_left (sub_ne_zero.2 h₁); rw [mul_sub_right_distrib, one_mul, sub_eq_zero, h₂] theorem mul_ne_zero_comm' {a b : α} (h : a * b ≠ 0) : b * a ≠ 0 := mul_ne_zero' (ne_zero_of_mul_ne_zero_left h) (ne_zero_of_mul_ne_zero_right h) end domain /- integral domains -/ section variables [s : integral_domain α] (a b c d e : α) include s instance integral_domain.to_domain : domain α := {..s} theorem eq_of_mul_eq_mul_right_of_ne_zero {a b c : α} (ha : a ≠ 0) (h : b * a = c * a) : b = c := have b * a - c * a = 0, by simp [h], have (b - c) * a = 0, by rw [mul_sub_right_distrib, this], have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right ha, eq_of_sub_eq_zero this theorem eq_of_mul_eq_mul_left_of_ne_zero {a b c : α} (ha : a ≠ 0) (h : a * b = a * c) : b = c := have a * b - a * c = 0, by simp [h], have a * (b - c) = 0, by rw [mul_sub_left_distrib, this], have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_left ha, eq_of_sub_eq_zero this theorem mul_dvd_mul_iff_left {a b c : α} (ha : a ≠ 0) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ λ d, by rw [mul_assoc, domain.mul_left_inj ha] theorem mul_dvd_mul_iff_right {a b c : α} (hc : c ≠ 0) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ λ d, by rw [mul_right_comm, domain.mul_right_inj hc] lemma units.inv_eq_self_iff (u : units α) : u⁻¹ = u ↔ u = 1 ∨ u = -1 := by conv {to_lhs, rw [inv_eq_iff_mul_eq_one, ← mul_one (1 : units α), units.ext_iff, units.coe_mul, units.coe_mul, mul_self_eq_mul_self_iff, ← units.ext_iff, ← units.coe_neg, ← units.ext_iff] } end /- units in various rings -/ namespace units section comm_semiring variables [comm_semiring α] (a b : α) (u : units α) @[simp] lemma coe_dvd : ↑u ∣ a := ⟨↑u⁻¹ * a, by simp⟩ @[simp] lemma dvd_coe_mul : a ∣ b * u ↔ a ∣ b := iff.intro (assume ⟨c, eq⟩, ⟨c * ↑u⁻¹, by rw [← mul_assoc, ← eq, units.mul_inv_cancel_right]⟩) (assume ⟨c, eq⟩, eq.symm ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _) @[simp] lemma dvd_coe : a ∣ ↑u ↔ a ∣ 1 := suffices a ∣ 1 * ↑u ↔ a ∣ 1, by simpa, dvd_coe_mul _ _ _ @[simp] lemma coe_mul_dvd : a * u ∣ b ↔ a ∣ b := iff.intro (assume ⟨c, eq⟩, ⟨c * ↑u, eq.symm ▸ by ac_refl⟩) (assume h, suffices a * ↑u ∣ b * 1, by simpa, mul_dvd_mul h (coe_dvd _ _)) end comm_semiring section domain variables [domain α] @[simp] theorem ne_zero : ∀(u : units α), (↑u : α) ≠ 0 | ⟨u, v, (huv : 0 * v = 1), hvu⟩ rfl := by simpa using huv end domain end units
45e599ac1faced7f07a544ecbfc0e00d7e160098
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/group/units_hom.lean
f6f7b5c6380bb218d7bfdc62827bea58aaeb3168
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
5,057
lean
/- Copyright (c) 2018 Johan Commelin All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Chris Hughes, Kevin Buzzard -/ import algebra.group.hom /-! # Lift monoid homomorphisms to group homomorphisms of their units subgroups. -/ universes u v w namespace units variables {M : Type u} {N : Type v} {P : Type w} [monoid M] [monoid N] [monoid P] /-- The group homomorphism on units induced by a `monoid_hom`. -/ @[to_additive "The `add_group` homomorphism on `add_unit`s induced by an `add_monoid_hom`."] def map (f : M →* N) : units M →* units N := monoid_hom.mk' (λ u, ⟨f u.val, f u.inv, by rw [← f.map_mul, u.val_inv, f.map_one], by rw [← f.map_mul, u.inv_val, f.map_one]⟩) (λ x y, ext (f.map_mul x y)) @[simp, to_additive] lemma coe_map (f : M →* N) (x : units M) : ↑(map f x) = f x := rfl @[simp, to_additive] lemma coe_map_inv (f : M →* N) (u : units M) : ↑(map f u)⁻¹ = f ↑u⁻¹ := rfl @[simp, to_additive] lemma map_comp (f : M →* N) (g : N →* P) : map (g.comp f) = (map g).comp (map f) := rfl variables (M) @[simp, to_additive] lemma map_id : map (monoid_hom.id M) = monoid_hom.id (units M) := by ext; refl /-- Coercion `units M → M` as a monoid homomorphism. -/ @[to_additive "Coercion `add_units M → M` as an add_monoid homomorphism."] def coe_hom : units M →* M := ⟨coe, coe_one, coe_mul⟩ variable {M} @[simp, to_additive] lemma coe_hom_apply (x : units M) : coe_hom M x = ↑x := rfl /-- If a map `g : M → units N` agrees with a homomorphism `f : M →* N`, then this map is a monoid homomorphism too. -/ @[to_additive "If a map `g : M → add_units N` agrees with a homomorphism `f : M →+ N`, then this map is an add_monoid homomorphism too."] def lift_right (f : M →* N) (g : M → units N) (h : ∀ x, ↑(g x) = f x) : M →* units N := { to_fun := g, map_one' := units.ext $ (h 1).symm ▸ f.map_one, map_mul' := λ x y, units.ext $ by simp only [h, coe_mul, f.map_mul] } @[simp, to_additive] lemma coe_lift_right {f : M →* N} {g : M → units N} (h : ∀ x, ↑(g x) = f x) (x) : (lift_right f g h x : N) = f x := h x @[simp, to_additive] lemma mul_lift_right_inv {f : M →* N} {g : M → units N} (h : ∀ x, ↑(g x) = f x) (x) : f x * ↑(lift_right f g h x)⁻¹ = 1 := by rw [units.mul_inv_eq_iff_eq_mul, one_mul, coe_lift_right] @[simp, to_additive] lemma lift_right_inv_mul {f : M →* N} {g : M → units N} (h : ∀ x, ↑(g x) = f x) (x) : ↑(lift_right f g h x)⁻¹ * f x = 1 := by rw [units.inv_mul_eq_iff_eq_mul, mul_one, coe_lift_right] end units namespace monoid_hom /-- If `f` is a homomorphism from a group `G` to a monoid `M`, then its image lies in the units of `M`, and `f.to_hom_units` is the corresponding monoid homomorphism from `G` to `units M`. -/ @[to_additive "If `f` is a homomorphism from an additive group `G` to an additive monoid `M`, then its image lies in the `add_units` of `M`, and `f.to_hom_units` is the corresponding homomorphism from `G` to `add_units M`."] def to_hom_units {G M : Type*} [group G] [monoid M] (f : G →* M) : G →* units M := { to_fun := λ g, ⟨f g, f (g⁻¹), by rw [← f.map_mul, mul_inv_self, f.map_one], by rw [← f.map_mul, inv_mul_self, f.map_one]⟩, map_one' := units.ext (f.map_one), map_mul' := λ _ _, units.ext (f.map_mul _ _) } @[simp] lemma coe_to_hom_units {G M : Type*} [group G] [monoid M] (f : G →* M) (g : G): (f.to_hom_units g : M) = f g := rfl end monoid_hom section is_unit variables {M : Type*} {N : Type*} @[to_additive] lemma is_unit.map [monoid M] [monoid N] (f : M →* N) {x : M} (h : is_unit x) : is_unit (f x) := by rcases h with ⟨y, rfl⟩; exact (units.map f y).is_unit /-- If a homomorphism `f : M →* N` sends each element to an `is_unit`, then it can be lifted to `f : M →* units N`. See also `units.lift_right` for a computable version. -/ @[to_additive "If a homomorphism `f : M →+ N` sends each element to an `is_add_unit`, then it can be lifted to `f : M →+ add_units N`. See also `add_units.lift_right` for a computable version."] noncomputable def is_unit.lift_right [monoid M] [monoid N] (f : M →* N) (hf : ∀ x, is_unit (f x)) : M →* units N := units.lift_right f (λ x, classical.some (hf x)) $ λ x, classical.some_spec (hf x) @[to_additive] lemma is_unit.coe_lift_right [monoid M] [monoid N] (f : M →* N) (hf : ∀ x, is_unit (f x)) (x) : (is_unit.lift_right f hf x : N) = f x := units.coe_lift_right _ x @[simp, to_additive] lemma is_unit.mul_lift_right_inv [monoid M] [monoid N] (f : M →* N) (h : ∀ x, is_unit (f x)) (x) : f x * ↑(is_unit.lift_right f h x)⁻¹ = 1 := units.mul_lift_right_inv (λ y, classical.some_spec $ h y) x @[simp, to_additive] lemma is_unit.lift_right_inv_mul [monoid M] [monoid N] (f : M →* N) (h : ∀ x, is_unit (f x)) (x) : ↑(is_unit.lift_right f h x)⁻¹ * f x = 1 := units.lift_right_inv_mul (λ y, classical.some_spec $ h y) x end is_unit
cf7fc143336d55eb21d23971a246fc6576884f5f
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/algebra/order/pi.lean
0a81e04f1c30ef0476705b02fcf95494c5dc812a
[ "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
2,338
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import algebra.group.pi import algebra.order.group import tactic.pi_instances /-! # Pi instances for ordered groups and monoids This file defines instances for ordered group, monoid, and related structures on Pi types. -/ universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) namespace pi /-- The product of a family of ordered commutative monoids is an ordered commutative monoid. -/ @[to_additive "The product of a family of ordered additive commutative monoids is an ordered additive commutative monoid."] instance ordered_comm_monoid {ι : Type*} {Z : ι → Type*} [∀ i, ordered_comm_monoid (Z i)] : ordered_comm_monoid (Π i, Z i) := { mul_le_mul_left := λ f g w h i, mul_le_mul_left' (w i) _, ..pi.partial_order, ..pi.comm_monoid, } /-- The product of a family of canonically ordered monoids is a canonically ordered monoid. -/ @[to_additive "The product of a family of canonically ordered additive monoids is a canonically ordered additive monoid."] instance {ι : Type*} {Z : ι → Type*} [∀ i, canonically_ordered_monoid (Z i)] : canonically_ordered_monoid (Π i, Z i) := { le_iff_exists_mul := λ f g, begin fsplit, { intro w, fsplit, { exact λ i, (le_iff_exists_mul.mp (w i)).some, }, { ext i, exact (le_iff_exists_mul.mp (w i)).some_spec, }, }, { rintro ⟨h, rfl⟩, exact λ i, le_mul_right (le_refl _), }, end, ..pi.order_bot, ..pi.ordered_comm_monoid, } @[to_additive] instance ordered_cancel_comm_monoid [∀ i, ordered_cancel_comm_monoid $ f i] : ordered_cancel_comm_monoid (Π i : I, f i) := by refine_struct { mul := (*), one := (1 : Π i, f i), le := (≤), lt := (<), npow := λ n x i, npow n (x i), .. pi.partial_order, .. pi.monoid }; tactic.pi_instance_derive_field @[to_additive] instance ordered_comm_group [∀ i, ordered_comm_group $ f i] : ordered_comm_group (Π i : I, f i) := { mul := (*), one := (1 : Π i, f i), le := (≤), lt := (<), npow := λ n x i, npow n (x i), ..pi.comm_group, ..pi.ordered_comm_monoid, } end pi
2a30e3a3ba5711f4f3decd5f4c80c2d3ca8a8e9e
32da3d0f92cab08875472ef6cacc1931c2b3eafa
/src/data/equiv/basic.lean
46134a8b65eb254903dbd5bc6e72b3aa88b55e1c
[ "Apache-2.0" ]
permissive
karthiknadig/mathlib
b6073c3748860bfc9a3e55da86afcddba62dc913
33a86cfff12d7f200d0010cd03b95e9b69a6c1a5
refs/heads/master
1,676,389,371,851
1,610,061,127,000
1,610,061,127,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
85,181
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Mario Carneiro -/ import data.set.function /-! # Equivalence between types In this file we define two 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. * `equiv.perm α`: the group of permutations `α ≃ α`. More lemmas about `equiv.perm` can be found in `group_theory/perm`. Then we define * canonical isomorphisms between various types: e.g., - `equiv.refl α` is the identity map interpreted as `α ≃ α`; - `equiv.sum_equiv_sigma_bool` is the canonical equivalence between the sum of two types `α ⊕ β` and the sigma-type `Σ b : bool, cond b α β`; - `equiv.prod_sum_distrib : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ)` shows that type product and type sum satisfy the distributive law up to a canonical equivalence; * operations on equivalences: e.g., - `equiv.symm e : β ≃ α` is the inverse of `e : α ≃ β`; - `equiv.trans e₁ e₂ : α ≃ γ` is the composition of `e₁ : α ≃ β` and `e₂ : β ≃ γ` (note the order of the arguments!); - `equiv.prod_congr ea eb : α₁ × β₁ ≃ α₂ × β₂`: combine two equivalences `ea : α₁ ≃ α₂` and `eb : β₁ ≃ β₂` using `prod.map`. * definitions that transfer some instances along an equivalence. By convention, we transfer instances from right to left. - `equiv.inhabited` takes `e : α ≃ β` and `[inhabited β]` and returns `inhabited α`; - `equiv.unique` takes `e : α ≃ β` and `[unique β]` and returns `unique α`; - `equiv.decidable_eq` takes `e : α ≃ β` and `[decidable_eq β]` and returns `decidable_eq α`. More definitions of this kind can be found in other files. E.g., `data/equiv/transfer_instance` does it for many algebraic type classes like `group`, `module`, etc. ## Tags equivalence, congruence, bijective map -/ open function universes u v w z variables {α : Sort u} {β : Sort v} {γ : Sort w} /-- `α ≃ β` is the type of functions from `α → β` with a two-sided inverse. -/ @[nolint has_inhabited_instance] structure equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` ≃ `:25 := equiv /-- Convert an involutive function `f` to an equivalence with `to_fun = inv_fun = f`. -/ def function.involutive.to_equiv (f : α → α) (h : involutive f) : α ≃ α := ⟨f, f, h.left_inverse, h.right_inverse⟩ namespace equiv /-- `perm α` is the type of bijections from `α` to itself. -/ @[reducible] def perm (α : Sort*) := equiv α α instance : has_coe_to_fun (α ≃ β) := ⟨_, to_fun⟩ @[simp] theorem coe_fn_mk (f : α → β) (g l r) : (equiv.mk f g l r : α → β) = f := rfl /-- The map `coe_fn : (r ≃ s) → (r → s)` is injective. -/ theorem injective_coe_fn : function.injective (λ (e : α ≃ β) (x : α), e x) | ⟨f₁, g₁, l₁, r₁⟩ ⟨f₂, g₂, l₂, r₂⟩ h := have f₁ = f₂, from h, have g₁ = g₂, from l₁.eq_right_inverse (this.symm ▸ r₂), by simp * @[simp, norm_cast] protected lemma coe_inj {e₁ e₂ : α ≃ β} : ⇑e₁ = e₂ ↔ e₁ = e₂ := injective_coe_fn.eq_iff @[ext] lemma ext {f g : equiv α β} (H : ∀ x, f x = g x) : f = g := injective_coe_fn (funext H) protected lemma congr_arg {f : equiv α β} : Π {x x' : α}, x = x' → f x = f x' | _ _ rfl := rfl protected lemma congr_fun {f g : equiv α β} (h : f = g) (x : α) : f x = g x := h ▸ rfl lemma ext_iff {f g : equiv α β} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, ext⟩ @[ext] lemma perm.ext {σ τ : equiv.perm α} (H : ∀ x, σ x = τ x) : σ = τ := equiv.ext H protected lemma perm.congr_arg {f : equiv.perm α} {x x' : α} : x = x' → f x = f x' := equiv.congr_arg protected lemma perm.congr_fun {f g : equiv.perm α} (h : f = g) (x : α) : f x = g x := equiv.congr_fun h x lemma perm.ext_iff {σ τ : equiv.perm α} : σ = τ ↔ ∀ x, σ x = τ x := ext_iff /-- Any type is equivalent to itself. -/ @[refl] protected def refl (α : Sort*) : α ≃ α := ⟨id, id, λ x, rfl, λ x, rfl⟩ instance inhabited' : inhabited (α ≃ α) := ⟨equiv.refl α⟩ /-- Inverse of an equivalence `e : α ≃ β`. -/ @[symm] protected def symm (e : α ≃ β) : β ≃ α := ⟨e.inv_fun, e.to_fun, e.right_inv, e.left_inv⟩ /-- See Note [custom simps projection] -/ def simps.inv_fun (e : α ≃ β) : β → α := e.symm initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply) /-- Composition of equivalences `e₁ : α ≃ β` and `e₂ : β ≃ γ`. -/ @[trans] protected 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] lemma to_fun_as_coe (e : α ≃ β) : e.to_fun = e := rfl @[simp] lemma inv_fun_as_coe (e : α ≃ β) : e.inv_fun = e.symm := rfl protected theorem injective (e : α ≃ β) : injective e := e.left_inv.injective protected theorem surjective (e : α ≃ β) : surjective e := e.right_inv.surjective protected theorem bijective (f : α ≃ β) : bijective f := ⟨f.injective, f.surjective⟩ @[simp] lemma range_eq_univ {α : Type*} {β : Type*} (e : α ≃ β) : set.range e = set.univ := set.eq_univ_of_forall e.surjective protected theorem subsingleton (e : α ≃ β) [subsingleton β] : subsingleton α := e.injective.subsingleton /-- Transfer `decidable_eq` across an equivalence. -/ protected def decidable_eq (e : α ≃ β) [decidable_eq β] : decidable_eq α := e.injective.decidable_eq lemma nonempty_iff_nonempty (e : α ≃ β) : nonempty α ↔ nonempty β := nonempty.congr e e.symm /-- If `α ≃ β` and `β` is inhabited, then so is `α`. -/ protected def inhabited [inhabited β] (e : α ≃ β) : inhabited α := ⟨e.symm (default _)⟩ /-- If `α ≃ β` and `β` is a singleton type, then so is `α`. -/ protected def unique [unique β] (e : α ≃ β) : unique α := e.symm.surjective.unique /-- Equivalence between equal types. -/ protected def cast {α β : Sort*} (h : α = β) : α ≃ β := ⟨cast h, cast h.symm, λ x, by { cases h, refl }, λ x, by { cases h, refl }⟩ @[simp] theorem coe_fn_symm_mk (f : α → β) (g l r) : ((equiv.mk f g l r).symm : β → α) = g := rfl @[simp] theorem coe_refl : ⇑(equiv.refl α) = id := rfl theorem refl_apply (x : α) : equiv.refl α x = x := rfl @[simp] theorem coe_trans (f : α ≃ β) (g : β ≃ γ) : ⇑(f.trans g) = g ∘ f := rfl theorem trans_apply (f : α ≃ β) (g : β ≃ γ) (a : α) : (f.trans g) a = g (f a) := rfl @[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 @[simp] lemma symm_trans_apply (f : α ≃ β) (g : β ≃ γ) (a : γ) : (f.trans g).symm a = f.symm (g.symm a) := rfl -- The `simp` attribute is needed to make this a `dsimp` lemma. -- `simp` will always rewrite with `equiv.symm_symm` before this has a chance to fire. @[simp, nolint simp_nf] theorem symm_symm_apply (f : α ≃ β) (b : α) : f.symm.symm b = f b := rfl @[simp] theorem apply_eq_iff_eq (f : α ≃ β) {x y : α} : f x = f y ↔ x = y := f.injective.eq_iff theorem apply_eq_iff_eq_symm_apply {α β : Sort*} (f : α ≃ β) {x : α} {y : β} : f x = y ↔ x = f.symm y := begin conv_lhs { rw ←apply_symm_apply f y, }, rw apply_eq_iff_eq, end @[simp] theorem cast_apply {α β} (h : α = β) (x : α) : equiv.cast h x = cast h x := rfl lemma symm_apply_eq {α β} (e : α ≃ β) {x y} : e.symm x = y ↔ x = e y := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_symm_apply {α β} (e : α ≃ β) {x y} : y = e.symm x ↔ e y = x := (eq_comm.trans e.symm_apply_eq).trans eq_comm @[simp] theorem symm_symm (e : α ≃ β) : e.symm.symm = e := by { cases e, refl } @[simp] theorem trans_refl (e : α ≃ β) : e.trans (equiv.refl β) = e := by { cases e, refl } @[simp] theorem refl_symm : (equiv.refl α).symm = equiv.refl α := rfl @[simp] theorem refl_trans (e : α ≃ β) : (equiv.refl α).trans e = e := by { cases e, refl } @[simp] theorem symm_trans (e : α ≃ β) : e.symm.trans e = equiv.refl β := ext (by simp) @[simp] theorem trans_symm (e : α ≃ β) : e.trans e.symm = equiv.refl α := ext (by simp) lemma trans_assoc {δ} (ab : α ≃ β) (bc : β ≃ γ) (cd : γ ≃ δ) : (ab.trans bc).trans cd = ab.trans (bc.trans cd) := equiv.ext $ assume a, rfl theorem left_inverse_symm (f : equiv α β) : left_inverse f.symm f := f.left_inv theorem right_inverse_symm (f : equiv α β) : function.right_inverse f.symm f := f.right_inv /-- If `α` is equivalent to `β` and `γ` is equivalent to `δ`, then the type of equivalences `α ≃ γ` is equivalent to the type of equivalences `β ≃ δ`. -/ def equiv_congr {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (α ≃ γ) ≃ (β ≃ δ) := ⟨ λac, (ab.symm.trans ac).trans cd, λbd, ab.trans $ bd.trans $ cd.symm, assume ac, by { ext x, simp }, assume ac, by { ext x, simp } ⟩ @[simp] lemma equiv_congr_refl {α β} : (equiv.refl α).equiv_congr (equiv.refl β) = equiv.refl (α ≃ β) := by { ext, refl } @[simp] lemma equiv_congr_symm {δ} (ab : α ≃ β) (cd : γ ≃ δ) : (ab.equiv_congr cd).symm = ab.symm.equiv_congr cd.symm := by { ext, refl } @[simp] lemma equiv_congr_trans {δ ε ζ} (ab : α ≃ β) (de : δ ≃ ε) (bc : β ≃ γ) (ef : ε ≃ ζ) : (ab.equiv_congr de).trans (bc.equiv_congr ef) = (ab.trans bc).equiv_congr (de.trans ef) := by { ext, refl } @[simp] lemma equiv_congr_apply_apply {δ} (ab : α ≃ β) (cd : γ ≃ δ) (e : α ≃ γ) (x) : ab.equiv_congr cd e x = cd (e (ab.symm x)) := rfl /-- If `α` is equivalent to `β`, then `perm α` is equivalent to `perm β`. -/ def perm_congr {α : Type*} {β : Type*} (e : α ≃ β) : perm α ≃ perm β := equiv_congr e e @[simp] lemma perm_congr_apply {α β : Type*} (e : α ≃ β) (p : equiv.perm α) (x) : e.perm_congr p x = e (p (e.symm x)) := rfl protected lemma image_eq_preimage {α β} (e : α ≃ β) (s : set α) : e '' s = e.symm ⁻¹' s := set.ext $ assume x, set.mem_image_iff_of_inverse e.left_inv e.right_inv protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) : t ⊆ e '' s ↔ e.symm '' t ⊆ s := by rw [set.image_subset_iff, e.image_eq_preimage] @[simp] lemma symm_image_image {α β} (f : equiv α β) (s : set α) : f.symm '' (f '' s) = s := by { rw [← set.image_comp], simp } @[simp] lemma image_preimage {α β} (e : α ≃ β) (s : set β) : e '' (e ⁻¹' s) = s := e.surjective.image_preimage s @[simp] lemma preimage_image {α β} (e : α ≃ β) (s : set α) : e ⁻¹' (e '' s) = s := set.preimage_image_eq s e.injective protected lemma image_compl {α β} (f : equiv α β) (s : set α) : f '' sᶜ = (f '' s)ᶜ := set.image_compl_eq f.bijective /-- If `α` is an empty type, then it is equivalent to the `empty` type. -/ def equiv_empty (h : α → false) : α ≃ empty := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ /-- `false` is equivalent to `empty`. -/ def false_equiv_empty : false ≃ empty := equiv_empty _root_.id /-- If `α` is an empty type, then it is equivalent to the `pempty` type in any universe. -/ def {u' v'} equiv_pempty {α : Sort v'} (h : α → false) : α ≃ pempty.{u'} := ⟨λ x, (h x).elim, λ e, e.rec _, λ x, (h x).elim, λ e, e.rec _⟩ /-- `false` is equivalent to `pempty`. -/ def false_equiv_pempty : false ≃ pempty := equiv_pempty _root_.id /-- `empty` is equivalent to `pempty`. -/ def empty_equiv_pempty : empty ≃ pempty := equiv_pempty $ empty.rec _ /-- `pempty` types from any two universes are equivalent. -/ def pempty_equiv_pempty : pempty.{v} ≃ pempty.{w} := equiv_pempty pempty.elim /-- If `α` is not `nonempty`, then it is equivalent to `empty`. -/ def empty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ empty := equiv_empty $ assume a, h ⟨a⟩ /-- If `α` is not `nonempty`, then it is equivalent to `pempty`. -/ def pempty_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : α ≃ pempty := equiv_pempty $ assume a, h ⟨a⟩ /-- The `Sort` of proofs of a true proposition is equivalent to `punit`. -/ def prop_equiv_punit {p : Prop} (h : p) : p ≃ punit := ⟨λ x, (), λ x, h, λ _, rfl, λ ⟨⟩, rfl⟩ /-- `true` is equivalent to `punit`. -/ def true_equiv_punit : true ≃ punit := prop_equiv_punit trivial /-- `ulift α` is equivalent to `α`. -/ @[simps apply symm_apply {fully_applied := ff}] protected def ulift {α : Type v} : ulift.{u} α ≃ α := ⟨ulift.down, ulift.up, ulift.up_down, λ a, rfl⟩ /-- `plift α` is equivalent to `α`. -/ @[simps apply symm_apply {fully_applied := ff}] protected def plift : plift α ≃ α := ⟨plift.down, plift.up, plift.up_down, plift.down_up⟩ /-- equivalence of propositions is the same as iff -/ def of_iff {P Q : Prop} (h : P ↔ Q) : P ≃ Q := { to_fun := h.mp, inv_fun := h.mpr, left_inv := λ x, rfl, right_inv := λ y, rfl } /-- If `α₁` is equivalent to `α₂` and `β₁` is equivalent to `β₂`, then the type of maps `α₁ → β₁` is equivalent to the type of maps `α₂ → β₂`. -/ @[congr, simps apply] def arrow_congr {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := { to_fun := λ f, e₂ ∘ f ∘ e₁.symm, inv_fun := λ f, e₂.symm ∘ f ∘ e₁, left_inv := λ f, funext $ λ x, by simp, right_inv := λ f, funext $ λ x, by simp } lemma arrow_congr_comp {α₁ β₁ γ₁ α₂ β₂ γ₂ : Sort*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) (ec : γ₁ ≃ γ₂) (f : α₁ → β₁) (g : β₁ → γ₁) : arrow_congr ea ec (g ∘ f) = (arrow_congr eb ec g) ∘ (arrow_congr ea eb f) := by { ext, simp only [comp, arrow_congr_apply, eb.symm_apply_apply] } @[simp] lemma arrow_congr_refl {α β : Sort*} : arrow_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Sort*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr e₁ e₁').trans (arrow_congr e₂ e₂') := rfl @[simp] lemma arrow_congr_symm {α₁ β₁ α₂ β₂ : Sort*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr e₁ e₂).symm = arrow_congr e₁.symm e₂.symm := rfl /-- A version of `equiv.arrow_congr` in `Type`, rather than `Sort`. The `equiv_rw` tactic is not able to use the default `Sort` level `equiv.arrow_congr`, because Lean's universe rules will not unify `?l_1` with `imax (1 ?m_1)`. -/ @[congr, simps apply] def arrow_congr' {α₁ β₁ α₂ β₂ : Type*} (hα : α₁ ≃ α₂) (hβ : β₁ ≃ β₂) : (α₁ → β₁) ≃ (α₂ → β₂) := equiv.arrow_congr hα hβ @[simp] lemma arrow_congr'_refl {α β : Type*} : arrow_congr' (equiv.refl α) (equiv.refl β) = equiv.refl (α → β) := rfl @[simp] lemma arrow_congr'_trans {α₁ β₁ α₂ β₂ α₃ β₃ : Type*} (e₁ : α₁ ≃ α₂) (e₁' : β₁ ≃ β₂) (e₂ : α₂ ≃ α₃) (e₂' : β₂ ≃ β₃) : arrow_congr' (e₁.trans e₂) (e₁'.trans e₂') = (arrow_congr' e₁ e₁').trans (arrow_congr' e₂ e₂') := rfl @[simp] lemma arrow_congr'_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (arrow_congr' e₁ e₂).symm = arrow_congr' e₁.symm e₂.symm := rfl /-- Conjugate a map `f : α → α` by an equivalence `α ≃ β`. -/ @[simps apply] def conj (e : α ≃ β) : (α → α) ≃ (β → β) := arrow_congr e e @[simp] lemma conj_refl : conj (equiv.refl α) = equiv.refl (α → α) := rfl @[simp] lemma conj_symm (e : α ≃ β) : e.conj.symm = e.symm.conj := rfl @[simp] lemma conj_trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : (e₁.trans e₂).conj = e₁.conj.trans e₂.conj := rfl -- This should not be a simp lemma as long as `(∘)` is reducible: -- when `(∘)` is reducible, Lean can unify `f₁ ∘ f₂` with any `g` using -- `f₁ := g` and `f₂ := λ x, x`. This causes nontermination. lemma conj_comp (e : α ≃ β) (f₁ f₂ : α → α) : e.conj (f₁ ∘ f₂) = (e.conj f₁) ∘ (e.conj f₂) := by apply arrow_congr_comp section binary_op variables {α₁ β₁ : Type*} (e : α₁ ≃ β₁) (f : α₁ → α₁ → α₁) lemma semiconj_conj (f : α₁ → α₁) : semiconj e f (e.conj f) := λ x, by simp lemma semiconj₂_conj : semiconj₂ e f (e.arrow_congr e.conj f) := λ x y, by simp instance [is_associative α₁ f] : is_associative β₁ (e.arrow_congr (e.arrow_congr e) f) := (e.semiconj₂_conj f).is_associative_right e.surjective instance [is_idempotent α₁ f] : is_idempotent β₁ (e.arrow_congr (e.arrow_congr e) f) := (e.semiconj₂_conj f).is_idempotent_right e.surjective instance [is_left_cancel α₁ f] : is_left_cancel β₁ (e.arrow_congr (e.arrow_congr e) f) := ⟨e.surjective.forall₃.2 $ λ x y z, by simpa using @is_left_cancel.left_cancel _ f _ x y z⟩ instance [is_right_cancel α₁ f] : is_right_cancel β₁ (e.arrow_congr (e.arrow_congr e) f) := ⟨e.surjective.forall₃.2 $ λ x y z, by simpa using @is_right_cancel.right_cancel _ f _ x y z⟩ end binary_op /-- `punit` sorts in any two universes are equivalent. -/ def punit_equiv_punit : punit.{v} ≃ punit.{w} := ⟨λ _, punit.star, λ _, punit.star, λ u, by { cases u, refl }, λ u, by { cases u, reflexivity }⟩ section /-- The sort of maps to `punit.{v}` is equivalent to `punit.{w}`. -/ def arrow_punit_equiv_punit (α : Sort*) : (α → punit.{v}) ≃ punit.{w} := ⟨λ f, punit.star, λ u f, punit.star, λ f, by { funext x, cases f x, refl }, λ u, by { cases u, reflexivity }⟩ /-- The sort of maps from `punit` is equivalent to the codomain. -/ def punit_arrow_equiv (α : Sort*) : (punit.{u} → α) ≃ α := ⟨λ f, f punit.star, λ a u, a, λ f, by { ext ⟨⟩, refl }, λ u, rfl⟩ /-- The sort of maps from `true` is equivalent to the codomain. -/ def true_arrow_equiv (α : Sort*) : (true → α) ≃ α := ⟨λ f, f trivial, λ a u, a, λ f, by { ext ⟨⟩, refl }, λ u, rfl⟩ /-- The sort of maps from `empty` is equivalent to `punit`. -/ def empty_arrow_equiv_punit (α : Sort*) : (empty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩ /-- The sort of maps from `pempty` is equivalent to `punit`. -/ def pempty_arrow_equiv_punit (α : Sort*) : (pempty → α) ≃ punit.{u} := ⟨λ f, punit.star, λ u e, e.rec _, λ f, funext $ λ x, x.rec _, λ u, by { cases u, refl }⟩ /-- The sort of maps from `false` is equivalent to `punit`. -/ def false_arrow_equiv_punit (α : Sort*) : (false → α) ≃ punit.{u} := calc (false → α) ≃ (empty → α) : arrow_congr false_equiv_empty (equiv.refl _) ... ≃ punit : empty_arrow_equiv_punit _ end /-- Product of two equivalences. If `α₁ ≃ α₂` and `β₁ ≃ β₂`, then `α₁ × β₁ ≃ α₂ × β₂`. -/ @[congr, simps apply] def prod_congr {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : α₁ × β₁ ≃ α₂ × β₂ := ⟨prod.map e₁ e₂, prod.map e₁.symm e₂.symm, λ ⟨a, b⟩, by simp, λ ⟨a, b⟩, by simp⟩ @[simp] theorem prod_congr_symm {α₁ β₁ α₂ β₂ : Type*} (e₁ : α₁ ≃ α₂) (e₂ : β₁ ≃ β₂) : (prod_congr e₁ e₂).symm = prod_congr e₁.symm e₂.symm := rfl /-- Type product is commutative up to an equivalence: `α × β ≃ β × α`. -/ @[simps apply] def prod_comm (α β : Type*) : α × β ≃ β × α := ⟨prod.swap, prod.swap, λ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩ @[simp] lemma prod_comm_symm (α β) : (prod_comm α β).symm = prod_comm β α := rfl /-- Type product is associative up to an equivalence. -/ @[simps] def prod_assoc (α β γ : Sort*) : (α × β) × γ ≃ α × (β × γ) := ⟨λ p, (p.1.1, p.1.2, p.2), λp, ((p.1, p.2.1), p.2.2), λ ⟨⟨a, b⟩, c⟩, rfl, λ ⟨a, ⟨b, c⟩⟩, rfl⟩ lemma prod_assoc_preimage {α β γ} {s : set α} {t : set β} {u : set γ} : equiv.prod_assoc α β γ ⁻¹' s.prod (t.prod u) = (s.prod t).prod u := by { ext, simp [and_assoc] } section /-- `punit` is a right identity for type product up to an equivalence. -/ @[simps apply] def prod_punit (α : Type*) : α × punit.{u+1} ≃ α := ⟨λ p, p.1, λ a, (a, punit.star), λ ⟨_, punit.star⟩, rfl, λ a, rfl⟩ /-- `punit` is a left identity for type product up to an equivalence. -/ @[simps apply] def punit_prod (α : Type*) : punit.{u+1} × α ≃ α := calc punit × α ≃ α × punit : prod_comm _ _ ... ≃ α : prod_punit _ /-- `empty` type is a right absorbing element for type product up to an equivalence. -/ def prod_empty (α : Type*) : α × empty ≃ empty := equiv_empty (λ ⟨_, e⟩, e.rec _) /-- `empty` type is a left absorbing element for type product up to an equivalence. -/ def empty_prod (α : Type*) : empty × α ≃ empty := equiv_empty (λ ⟨e, _⟩, e.rec _) /-- `pempty` type is a right absorbing element for type product up to an equivalence. -/ def prod_pempty (α : Type*) : α × pempty ≃ pempty := equiv_pempty (λ ⟨_, e⟩, e.rec _) /-- `pempty` type is a left absorbing element for type product up to an equivalence. -/ def pempty_prod (α : Type*) : pempty × α ≃ pempty := equiv_pempty (λ ⟨e, _⟩, e.rec _) end section open sum /-- `psum` is equivalent to `sum`. -/ def psum_equiv_sum (α β : Type*) : psum α β ≃ α ⊕ β := ⟨λ s, psum.cases_on s inl inr, λ s, sum.cases_on s psum.inl psum.inr, λ s, by cases s; refl, λ s, by cases s; refl⟩ /-- If `α ≃ α'` and `β ≃ β'`, then `α ⊕ β ≃ α' ⊕ β'`. -/ @[simps apply] def sum_congr {α₁ β₁ α₂ β₂ : Type*} (ea : α₁ ≃ α₂) (eb : β₁ ≃ β₂) : α₁ ⊕ β₁ ≃ α₂ ⊕ β₂ := ⟨sum.map ea eb, sum.map ea.symm eb.symm, λ x, by simp, λ x, by simp⟩ @[simp] lemma sum_congr_trans {α₁ α₂ β₁ β₂ γ₁ γ₂ : Sort*} (e : α₁ ≃ β₁) (f : α₂ ≃ β₂) (g : β₁ ≃ γ₁) (h : β₂ ≃ γ₂) : (equiv.sum_congr e f).trans (equiv.sum_congr g h) = (equiv.sum_congr (e.trans g) (f.trans h)) := by { ext i, cases i; refl } @[simp] lemma sum_congr_symm {α β γ δ : Sort*} (e : α ≃ β) (f : γ ≃ δ) : (equiv.sum_congr e f).symm = (equiv.sum_congr (e.symm) (f.symm)) := rfl @[simp] lemma sum_congr_refl {α β : Sort*} : equiv.sum_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α ⊕ β) := by { ext i, cases i; refl } namespace perm /-- Combine a permutation of `α` and of `β` into a permutation of `α ⊕ β`. -/ @[reducible] def sum_congr {α β : Type*} (ea : equiv.perm α) (eb : equiv.perm β) : equiv.perm (α ⊕ β) := equiv.sum_congr ea eb @[simp] lemma sum_congr_apply {α β : Type*} (ea : equiv.perm α) (eb : equiv.perm β) (x : α ⊕ β) : sum_congr ea eb x = sum.map ⇑ea ⇑eb x := equiv.sum_congr_apply ea eb x @[simp] lemma sum_congr_trans {α β : Sort*} (e : equiv.perm α) (f : equiv.perm β) (g : equiv.perm α) (h : equiv.perm β) : (sum_congr e f).trans (sum_congr g h) = sum_congr (e.trans g) (f.trans h) := equiv.sum_congr_trans e f g h @[simp] lemma sum_congr_symm {α β : Sort*} (e : equiv.perm α) (f : equiv.perm β) : (sum_congr e f).symm = sum_congr (e.symm) (f.symm) := equiv.sum_congr_symm e f @[simp] lemma sum_congr_refl {α β : Sort*} : sum_congr (equiv.refl α) (equiv.refl β) = equiv.refl (α ⊕ β) := equiv.sum_congr_refl end perm /-- `bool` is equivalent the sum of two `punit`s. -/ def bool_equiv_punit_sum_punit : bool ≃ punit.{u+1} ⊕ punit.{v+1} := ⟨λ b, cond b (inr punit.star) (inl punit.star), λ s, sum.rec_on s (λ_, ff) (λ_, tt), λ b, by cases b; refl, λ s, by rcases s with ⟨⟨⟩⟩ | ⟨⟨⟩⟩; refl⟩ /-- `Prop` is noncomputably equivalent to `bool`. -/ noncomputable def Prop_equiv_bool : Prop ≃ bool := ⟨λ p, @to_bool p (classical.prop_decidable _), λ b, b, λ p, by simp, λ b, by simp⟩ /-- Sum of types is commutative up to an equivalence. -/ @[simps apply] def sum_comm (α β : Sort*) : α ⊕ β ≃ β ⊕ α := ⟨sum.swap, sum.swap, sum.swap_swap, sum.swap_swap⟩ @[simp] lemma sum_comm_symm (α β) : (sum_comm α β).symm = sum_comm β α := rfl /-- Sum of types is associative up to an equivalence. -/ def sum_assoc (α β γ : Sort*) : (α ⊕ β) ⊕ γ ≃ α ⊕ (β ⊕ γ) := ⟨sum.elim (sum.elim sum.inl (sum.inr ∘ sum.inl)) (sum.inr ∘ sum.inr), sum.elim (sum.inl ∘ sum.inl) $ sum.elim (sum.inl ∘ sum.inr) sum.inr, by rintros (⟨_ | _⟩ | _); refl, by rintros (_ | ⟨_ | _⟩); refl⟩ @[simp] theorem sum_assoc_apply_in1 {α β γ} (a) : sum_assoc α β γ (inl (inl a)) = inl a := rfl @[simp] theorem sum_assoc_apply_in2 {α β γ} (b) : sum_assoc α β γ (inl (inr b)) = inr (inl b) := rfl @[simp] theorem sum_assoc_apply_in3 {α β γ} (c) : sum_assoc α β γ (inr c) = inr (inr c) := rfl /-- Sum with `empty` is equivalent to the original type. -/ def sum_empty (α : Type*) : α ⊕ empty ≃ α := ⟨sum.elim id (empty.rec _), inl, λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, λ a, rfl⟩ @[simp] lemma sum_empty_apply_inl {α} (a) : sum_empty α (sum.inl a) = a := rfl /-- The sum of `empty` with any `Sort*` is equivalent to the right summand. -/ def empty_sum (α : Sort*) : empty ⊕ α ≃ α := (sum_comm _ _).trans $ sum_empty _ @[simp] lemma empty_sum_apply_inr {α} (a) : empty_sum α (sum.inr a) = a := rfl /-- Sum with `pempty` is equivalent to the original type. -/ def sum_pempty (α : Type*) : α ⊕ pempty ≃ α := ⟨sum.elim id (pempty.rec _), inl, λ s, by { rcases s with _ | ⟨⟨⟩⟩, refl }, λ a, rfl⟩ @[simp] lemma sum_pempty_apply_inl {α} (a) : sum_pempty α (sum.inl a) = a := rfl /-- The sum of `pempty` with any `Sort*` is equivalent to the right summand. -/ def pempty_sum (α : Sort*) : pempty ⊕ α ≃ α := (sum_comm _ _).trans $ sum_pempty _ @[simp] lemma pempty_sum_apply_inr {α} (a) : pempty_sum α (sum.inr a) = a := rfl /-- `option α` is equivalent to `α ⊕ punit` -/ def option_equiv_sum_punit (α : Type*) : option α ≃ α ⊕ punit.{u+1} := ⟨λ o, match o with none := inr punit.star | some a := inl a end, λ s, match s with inr _ := none | inl a := some a end, λ o, by cases o; refl, λ s, by rcases s with _ | ⟨⟨⟩⟩; refl⟩ @[simp] lemma option_equiv_sum_punit_none {α} : option_equiv_sum_punit α none = sum.inr punit.star := rfl @[simp] lemma option_equiv_sum_punit_some {α} (a) : option_equiv_sum_punit α (some a) = sum.inl a := rfl @[simp] lemma option_equiv_sum_punit_coe {α} (a : α) : option_equiv_sum_punit α a = sum.inl a := rfl @[simp] lemma option_equiv_sum_punit_symm_inl {α} (a) : (option_equiv_sum_punit α).symm (sum.inl a) = a := rfl @[simp] lemma option_equiv_sum_punit_symm_inr {α} (a) : (option_equiv_sum_punit α).symm (sum.inr a) = none := rfl /-- The set of `x : option α` such that `is_some x` is equivalent to `α`. -/ def option_is_some_equiv (α : Type*) : {x : option α // x.is_some} ≃ α := { to_fun := λ o, option.get o.2, inv_fun := λ x, ⟨some x, dec_trivial⟩, left_inv := λ o, subtype.eq $ option.some_get _, right_inv := λ x, option.get_some _ _ } /-- `α ⊕ β` is equivalent to a `sigma`-type over `bool`. Note that this definition assumes `α` and `β` to be types from the same universe, so it cannot by used directly to transfer theorems about sigma types to theorems about sum types. In many cases one can use `ulift` to work around this difficulty. -/ def sum_equiv_sigma_bool (α β : Type u) : α ⊕ β ≃ (Σ b: bool, cond b α β) := ⟨λ s, s.elim (λ x, ⟨tt, x⟩) (λ x, ⟨ff, x⟩), λ s, match s with ⟨tt, a⟩ := inl a | ⟨ff, b⟩ := inr b end, λ s, by cases s; refl, λ s, by rcases s with ⟨_|_, _⟩; refl⟩ /-- `sigma_preimage_equiv f` for `f : α → β` is the natural equivalence between the type of all fibres of `f` and the total space `α`. -/ @[simps] def sigma_preimage_equiv {α β : Type*} (f : α → β) : (Σ y : β, {x // f x = y}) ≃ α := ⟨λ x, ↑x.2, λ x, ⟨f x, x, rfl⟩, λ ⟨y, x, rfl⟩, rfl, λ x, rfl⟩ /-- A set `s` in `α × β` is equivalent to the sigma-type `Σ x, {y | (x, y) ∈ s}`. -/ def set_prod_equiv_sigma {α β : Type*} (s : set (α × β)) : s ≃ Σ x : α, {y | (x, y) ∈ s} := { to_fun := λ x, ⟨x.1.1, x.1.2, by simp⟩, inv_fun := λ x, ⟨(x.1, x.2.1), x.2.2⟩, left_inv := λ ⟨⟨x, y⟩, h⟩, rfl, right_inv := λ ⟨x, y, h⟩, rfl } end section sum_compl /-- For any predicate `p` on `α`, the sum of the two subtypes `{a // p a}` and its complement `{a // ¬ p a}` is naturally equivalent to `α`. -/ def sum_compl {α : Type*} (p : α → Prop) [decidable_pred p] : {a // p a} ⊕ {a // ¬ p a} ≃ α := { to_fun := sum.elim coe coe, inv_fun := λ a, if h : p a then sum.inl ⟨a, h⟩ else sum.inr ⟨a, h⟩, left_inv := by { rintros (⟨x,hx⟩|⟨x,hx⟩); dsimp; [rw dif_pos, rw dif_neg], }, right_inv := λ a, by { dsimp, split_ifs; refl } } @[simp] lemma sum_compl_apply_inl {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // p a}) : sum_compl p (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type*} (p : α → Prop) [decidable_pred p] (x : {a // ¬ p a}) : sum_compl p (sum.inr x) = x := rfl @[simp] lemma sum_compl_apply_symm_of_pos {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : p a) : (sum_compl p).symm a = sum.inl ⟨a, h⟩ := dif_pos h @[simp] lemma sum_compl_apply_symm_of_neg {α : Type*} (p : α → Prop) [decidable_pred p] (a : α) (h : ¬ p a) : (sum_compl p).symm a = sum.inr ⟨a, h⟩ := dif_neg h end sum_compl section subtype_preimage variables (p : α → Prop) [decidable_pred p] (x₀ : {a // p a} → β) /-- For a fixed function `x₀ : {a // p a} → β` defined on a subtype of `α`, the subtype of functions `x : α → β` that agree with `x₀` on the subtype `{a // p a}` is naturally equivalent to the type of functions `{a // ¬ p a} → β`. -/ @[simps] def subtype_preimage : {x : α → β // x ∘ coe = x₀} ≃ ({a // ¬ p a} → β) := { to_fun := λ (x : {x : α → β // x ∘ coe = x₀}) a, (x : α → β) a, inv_fun := λ x, ⟨λ a, if h : p a then x₀ ⟨a, h⟩ else x ⟨a, h⟩, funext $ λ ⟨a, h⟩, dif_pos h⟩, left_inv := λ ⟨x, hx⟩, subtype.val_injective $ funext $ λ a, (by { dsimp, split_ifs; [ rw ← hx, skip ]; refl }), right_inv := λ x, funext $ λ ⟨a, h⟩, show dite (p a) _ _ = _, by { dsimp, rw [dif_neg h] } } lemma subtype_preimage_symm_apply_coe_pos (x : {a // ¬ p a} → β) (a : α) (h : p a) : ((subtype_preimage p x₀).symm x : α → β) a = x₀ ⟨a, h⟩ := dif_pos h lemma subtype_preimage_symm_apply_coe_neg (x : {a // ¬ p a} → β) (a : α) (h : ¬ p a) : ((subtype_preimage p x₀).symm x : α → β) a = x ⟨a, h⟩ := dif_neg h end subtype_preimage section fun_unique variables (α β) [unique α] /-- If `α` has a unique term, then the type of function `α → β` is equivalent to `β`. -/ @[simps] def fun_unique : (α → β) ≃ β := { to_fun := λ f, f (default α), inv_fun := λ b a, b, left_inv := λ f, funext $ λ a, congr_arg f $ subsingleton.elim _ _, right_inv := λ b, rfl } end fun_unique section /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Π a, β₁ a` and `Π a, β₂ a`. -/ def Pi_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Π a, β₁ a) ≃ (Π a, β₂ a) := ⟨λ H a, F a (H a), λ H a, (F a).symm (H a), λ H, funext $ by simp, λ H, funext $ by simp⟩ /-- Dependent `curry` equivalence: the type of dependent functions on `Σ i, β i` is equivalent to the type of dependent functions of two arguments (i.e., functions to the space of functions). -/ def Pi_curry {α} {β : α → Sort*} (γ : Π a, β a → Sort*) : (Π x : Σ i, β i, γ x.1 x.2) ≃ (Π a b, γ a b) := { to_fun := λ f x y, f ⟨x,y⟩, inv_fun := λ f x, f x.1 x.2, left_inv := λ f, funext $ λ ⟨x,y⟩, rfl, right_inv := λ f, funext $ λ x, funext $ λ y, rfl } end section /-- A `psigma`-type is equivalent to the corresponding `sigma`-type. -/ @[simps apply symm_apply] def psigma_equiv_sigma {α} (β : α → Sort*) : (Σ' i, β i) ≃ Σ i, β i := ⟨λ a, ⟨a.1, a.2⟩, λ a, ⟨a.1, a.2⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ /-- A family of equivalences `Π a, β₁ a ≃ β₂ a` generates an equivalence between `Σ a, β₁ a` and `Σ a, β₂ a`. -/ @[simps apply] def sigma_congr_right {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (Σ a, β₁ a) ≃ Σ a, β₂ a := ⟨λ a, ⟨a.1, F a.1 a.2⟩, λ a, ⟨a.1, (F a.1).symm a.2⟩, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ symm_apply_apply (F a) b, λ ⟨a, b⟩, congr_arg (sigma.mk a) $ apply_symm_apply (F a) b⟩ @[simp] lemma sigma_congr_right_trans {α} {β₁ β₂ β₃ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) (G : Π a, β₂ a ≃ β₃ a) : (sigma_congr_right F).trans (sigma_congr_right G) = sigma_congr_right (λ a, (F a).trans (G a)) := by { ext1 x, cases x, refl } @[simp] lemma sigma_congr_right_symm {α} {β₁ β₂ : α → Sort*} (F : Π a, β₁ a ≃ β₂ a) : (sigma_congr_right F).symm = sigma_congr_right (λ a, (F a).symm) := by { ext1 x, cases x, refl } @[simp] lemma sigma_congr_right_refl {α} {β : α → Sort*} : (sigma_congr_right (λ a, equiv.refl (β a))) = equiv.refl (Σ a, β a) := by { ext1 x, cases x, refl } namespace perm /-- A family of permutations `Π a, perm (β a)` generates a permuation `perm (Σ a, β₁ a)`. -/ @[reducible] def sigma_congr_right {α} {β : α → Sort*} (F : Π a, perm (β a)) : perm (Σ a, β a) := equiv.sigma_congr_right F @[simp] lemma sigma_congr_right_trans {α} {β : α → Sort*} (F : Π a, perm (β a)) (G : Π a, perm (β a)) : (sigma_congr_right F).trans (sigma_congr_right G) = sigma_congr_right (λ a, (F a).trans (G a)) := equiv.sigma_congr_right_trans F G @[simp] lemma sigma_congr_right_symm {α} {β : α → Sort*} (F : Π a, perm (β a)) : (sigma_congr_right F).symm = sigma_congr_right (λ a, (F a).symm) := equiv.sigma_congr_right_symm F @[simp] lemma sigma_congr_right_refl {α} {β : α → Sort*} : (sigma_congr_right (λ a, equiv.refl (β a))) = equiv.refl (Σ a, β a) := equiv.sigma_congr_right_refl end perm /-- An equivalence `f : α₁ ≃ α₂` generates an equivalence between `Σ a, β (f a)` and `Σ a, β a`. -/ @[simps apply] def sigma_congr_left {α₁ α₂} {β : α₂ → Sort*} (e : α₁ ≃ α₂) : (Σ a:α₁, β (e a)) ≃ (Σ a:α₂, β a) := ⟨λ a, ⟨e a.1, a.2⟩, λ a, ⟨e.symm a.1, @@eq.rec β a.2 (e.right_inv a.1).symm⟩, λ ⟨a, b⟩, match e.symm (e a), e.left_inv a : ∀ a' (h : a' = a), @sigma.mk _ (β ∘ e) _ (@@eq.rec β b (congr_arg e h.symm)) = ⟨a, b⟩ with | _, rfl := rfl end, λ ⟨a, b⟩, match e (e.symm a), _ : ∀ a' (h : a' = a), sigma.mk a' (@@eq.rec β b h.symm) = ⟨a, b⟩ with | _, rfl := rfl end⟩ /-- Transporting a sigma type through an equivalence of the base -/ def sigma_congr_left' {α₁ α₂} {β : α₁ → Sort*} (f : α₁ ≃ α₂) : (Σ a:α₁, β a) ≃ (Σ a:α₂, β (f.symm a)) := (sigma_congr_left f.symm).symm /-- Transporting a sigma type through an equivalence of the base and a family of equivalences of matching fibers -/ def sigma_congr {α₁ α₂} {β₁ : α₁ → Sort*} {β₂ : α₂ → Sort*} (f : α₁ ≃ α₂) (F : ∀ a, β₁ a ≃ β₂ (f a)) : sigma β₁ ≃ sigma β₂ := (sigma_congr_right F).trans (sigma_congr_left f) /-- `sigma` type with a constant fiber is equivalent to the product. -/ @[simps apply symm_apply] def sigma_equiv_prod (α β : Type*) : (Σ_:α, β) ≃ α × β := ⟨λ a, ⟨a.1, a.2⟩, λ a, ⟨a.1, a.2⟩, λ ⟨a, b⟩, rfl, λ ⟨a, b⟩, rfl⟩ /-- If each fiber of a `sigma` type is equivalent to a fixed type, then the sigma type is equivalent to the product. -/ def sigma_equiv_prod_of_equiv {α β} {β₁ : α → Sort*} (F : Π a, β₁ a ≃ β) : sigma β₁ ≃ α × β := (sigma_congr_right F).trans (sigma_equiv_prod α β) end section prod_congr variables {α₁ β₁ β₂ : Type*} (e : α₁ → β₁ ≃ β₂) /-- A family of equivalences `Π (a : α₁), β₁ ≃ β₂` generates an equivalence between `β₁ × α₁` and `β₂ × α₁`. -/ def prod_congr_left : β₁ × α₁ ≃ β₂ × α₁ := { to_fun := λ ab, ⟨e ab.2 ab.1, ab.2⟩, inv_fun := λ ab, ⟨(e ab.2).symm ab.1, ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_left_apply (b : β₁) (a : α₁) : prod_congr_left e (b, a) = (e a b, a) := rfl lemma prod_congr_refl_right (e : β₁ ≃ β₂) : prod_congr e (equiv.refl α₁) = prod_congr_left (λ _, e) := by { ext ⟨a, b⟩ : 1, simp } /-- A family of equivalences `Π (a : α₁), β₁ ≃ β₂` generates an equivalence between `α₁ × β₁` and `α₁ × β₂`. -/ def prod_congr_right : α₁ × β₁ ≃ α₁ × β₂ := { to_fun := λ ab, ⟨ab.1, e ab.1 ab.2⟩, inv_fun := λ ab, ⟨ab.1, (e ab.1).symm ab.2⟩, left_inv := by { rintros ⟨a, b⟩, simp }, right_inv := by { rintros ⟨a, b⟩, simp } } @[simp] lemma prod_congr_right_apply (a : α₁) (b : β₁) : prod_congr_right e (a, b) = (a, e a b) := rfl lemma prod_congr_refl_left (e : β₁ ≃ β₂) : prod_congr (equiv.refl α₁) e = prod_congr_right (λ _, e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_left_trans_prod_comm : (prod_congr_left e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } @[simp] lemma prod_congr_right_trans_prod_comm : (prod_congr_right e).trans (prod_comm _ _) = (prod_comm _ _).trans (prod_congr_left e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_congr_right_sigma_equiv_prod : (sigma_congr_right e).trans (sigma_equiv_prod α₁ β₂) = (sigma_equiv_prod α₁ β₁).trans (prod_congr_right e) := by { ext ⟨a, b⟩ : 1, simp } lemma sigma_equiv_prod_sigma_congr_right : (sigma_equiv_prod α₁ β₁).symm.trans (sigma_congr_right e) = (prod_congr_right e).trans (sigma_equiv_prod α₁ β₂).symm := by { ext ⟨a, b⟩ : 1, simp } end prod_congr namespace perm variables {α₁ β₁ β₂ : Type*} [decidable_eq α₁] (a : α₁) (e : perm β₁) /-- `prod_extend_right a e` extends `e : perm β` to `perm (α × β)` by sending `(a, b)` to `(a, e b)` and keeping the other `(a', b)` fixed. -/ def prod_extend_right : perm (α₁ × β₁) := { to_fun := λ ab, if ab.fst = a then (a, e ab.snd) else ab, inv_fun := λ ab, if ab.fst = a then (a, e.symm ab.snd) else ab, left_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] }, right_inv := by { rintros ⟨k', x⟩, simp only, split_ifs with h; simp [h] } } @[simp] lemma prod_extend_right_apply_eq (b : β₁) : prod_extend_right a e (a, b) = (a, e b) := if_pos rfl lemma prod_extend_right_apply_ne {a a' : α₁} (h : a' ≠ a) (b : β₁) : prod_extend_right a e (a', b) = (a', b) := if_neg h lemma eq_of_prod_extend_right_ne {e : perm β₁} {a a' : α₁} {b : β₁} (h : prod_extend_right a e (a', b) ≠ (a', b)) : a' = a := by { contrapose! h, exact prod_extend_right_apply_ne _ h _ } @[simp] lemma fst_prod_extend_right (ab : α₁ × β₁) : (prod_extend_right a e ab).fst = ab.fst := begin rw [prod_extend_right, coe_fn_mk], split_ifs with h, { rw h }, { refl } end end perm section /-- The type of functions to a product `α × β` is equivalent to the type of pairs of functions `γ → α` and `γ → β`. -/ def arrow_prod_equiv_prod_arrow (α β γ : Type*) : (γ → α × β) ≃ (γ → α) × (γ → β) := ⟨λ f, (λ c, (f c).1, λ c, (f c).2), λ p c, (p.1 c, p.2 c), λ f, funext $ λ c, prod.mk.eta, λ p, by { cases p, refl }⟩ /-- Functions `α → β → γ` are equivalent to functions on `α × β`. -/ def arrow_arrow_equiv_prod_arrow (α β γ : Sort*) : (α → β → γ) ≃ (α × β → γ) := ⟨uncurry, curry, curry_uncurry, uncurry_curry⟩ open sum /-- The type of functions on a sum type `α ⊕ β` is equivalent to the type of pairs of functions on `α` and on `β`. -/ def sum_arrow_equiv_prod_arrow (α β γ : Type*) : ((α ⊕ β) → γ) ≃ (α → γ) × (β → γ) := ⟨λ f, (f ∘ inl, f ∘ inr), λ p, sum.elim p.1 p.2, λ f, by { ext ⟨⟩; refl }, λ p, by { cases p, refl }⟩ /-- Type product is right distributive with respect to type sum up to an equivalence. -/ def sum_prod_distrib (α β γ : Sort*) : (α ⊕ β) × γ ≃ (α × γ) ⊕ (β × γ) := ⟨λ p, match p with (inl a, c) := inl (a, c) | (inr b, c) := inr (b, c) end, λ s, match s with inl q := (inl q.1, q.2) | inr q := (inr q.1, q.2) end, λ p, by rcases p with ⟨_ | _, _⟩; refl, λ s, by rcases s with ⟨_, _⟩ | ⟨_, _⟩; refl⟩ @[simp] theorem sum_prod_distrib_apply_left {α β γ} (a : α) (c : γ) : sum_prod_distrib α β γ (sum.inl a, c) = sum.inl (a, c) := rfl @[simp] theorem sum_prod_distrib_apply_right {α β γ} (b : β) (c : γ) : sum_prod_distrib α β γ (sum.inr b, c) = sum.inr (b, c) := rfl /-- Type product is left distributive with respect to type sum up to an equivalence. -/ def prod_sum_distrib (α β γ : Sort*) : α × (β ⊕ γ) ≃ (α × β) ⊕ (α × γ) := calc α × (β ⊕ γ) ≃ (β ⊕ γ) × α : prod_comm _ _ ... ≃ (β × α) ⊕ (γ × α) : sum_prod_distrib _ _ _ ... ≃ (α × β) ⊕ (α × γ) : sum_congr (prod_comm _ _) (prod_comm _ _) @[simp] theorem prod_sum_distrib_apply_left {α β γ} (a : α) (b : β) : prod_sum_distrib α β γ (a, sum.inl b) = sum.inl (a, b) := rfl @[simp] theorem prod_sum_distrib_apply_right {α β γ} (a : α) (c : γ) : prod_sum_distrib α β γ (a, sum.inr c) = sum.inr (a, c) := rfl /-- The product of an indexed sum of types (formally, a `sigma`-type `Σ i, α i`) by a type `β` is equivalent to the sum of products `Σ i, (α i × β)`. -/ def sigma_prod_distrib {ι : Type*} (α : ι → Type*) (β : Type*) : ((Σ i, α i) × β) ≃ (Σ i, (α i × β)) := ⟨λ p, ⟨p.1.1, (p.1.2, p.2)⟩, λ p, (⟨p.1, p.2.1⟩, p.2.2), λ p, by { rcases p with ⟨⟨_, _⟩, _⟩, refl }, λ p, by { rcases p with ⟨_, ⟨_, _⟩⟩, refl }⟩ /-- The product `bool × α` is equivalent to `α ⊕ α`. -/ def bool_prod_equiv_sum (α : Type u) : bool × α ≃ α ⊕ α := calc bool × α ≃ (unit ⊕ unit) × α : prod_congr bool_equiv_punit_sum_punit (equiv.refl _) ... ≃ (unit × α) ⊕ (unit × α) : sum_prod_distrib _ _ _ ... ≃ α ⊕ α : sum_congr (punit_prod _) (punit_prod _) /-- The function type `bool → α` is equivalent to `α × α`. -/ def bool_to_equiv_prod (α : Type u) : (bool → α) ≃ α × α := calc (bool → α) ≃ ((unit ⊕ unit) → α) : (arrow_congr bool_equiv_punit_sum_punit (equiv.refl α)) ... ≃ (unit → α) × (unit → α) : sum_arrow_equiv_prod_arrow _ _ _ ... ≃ α × α : prod_congr (punit_arrow_equiv _) (punit_arrow_equiv _) @[simp] lemma bool_to_equiv_prod_apply {α : Type u} (f : bool → α) : bool_to_equiv_prod α f = (f ff, f tt) := rfl @[simp] lemma bool_to_equiv_prod_symm_apply_ff {α : Type u} (p : α × α) : (bool_to_equiv_prod α).symm p ff = p.1 := rfl @[simp] lemma bool_to_equiv_prod_symm_apply_tt {α : Type u} (p : α × α) : (bool_to_equiv_prod α).symm p tt = p.2 := rfl end section open sum nat /-- The set of natural numbers is equivalent to `ℕ ⊕ punit`. -/ def nat_equiv_nat_sum_punit : ℕ ≃ ℕ ⊕ punit.{u+1} := ⟨λ n, match n with zero := inr punit.star | succ a := inl a end, λ s, match s with inl n := succ n | inr punit.star := zero end, λ n, begin cases n, repeat { refl } end, λ s, begin cases s with a u, { refl }, {cases u, { refl }} end⟩ /-- `ℕ ⊕ punit` is equivalent to `ℕ`. -/ def nat_sum_punit_equiv_nat : ℕ ⊕ punit.{u+1} ≃ ℕ := nat_equiv_nat_sum_punit.symm /-- The type of integer numbers is equivalent to `ℕ ⊕ ℕ`. -/ def int_equiv_nat_sum_nat : ℤ ≃ ℕ ⊕ ℕ := by refine ⟨_, _, _, _⟩; intro z; {cases z; [left, right]; assumption} <|> {cases z; refl} end /-- An equivalence between `α` and `β` generates an equivalence between `list α` and `list β`. -/ def list_equiv_of_equiv {α β : Type*} (e : α ≃ β) : list α ≃ list β := { to_fun := list.map e, inv_fun := list.map e.symm, left_inv := λ l, by rw [list.map_map, e.symm_comp_self, list.map_id], right_inv := λ l, by rw [list.map_map, e.self_comp_symm, list.map_id] } /-- `fin n` is equivalent to `{m // m < n}`. -/ def fin_equiv_subtype (n : ℕ) : fin n ≃ {m // m < n} := ⟨λ x, ⟨x.1, x.2⟩, λ x, ⟨x.1, x.2⟩, λ ⟨a, b⟩, rfl,λ ⟨a, b⟩, rfl⟩ /-- If `α` is equivalent to `β`, then `unique α` is equivalent to `β`. -/ def unique_congr (e : α ≃ β) : unique α ≃ unique β := { to_fun := λ h, @equiv.unique _ _ h e.symm, inv_fun := λ h, @equiv.unique _ _ h e, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } section open subtype /-- If `α` is equivalent to `β` and the predicates `p : α → Prop` and `q : β → Prop` are equivalent at corresponding points, then `{a // p a}` is equivalent to `{b // q b}`. -/ def subtype_congr {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ a, p a ↔ q (e a)) : {a : α // p a} ≃ {b : β // q b} := ⟨λ x, ⟨e x, (h _).1 x.2⟩, λ y, ⟨e.symm y, (h _).2 (by { simp, exact y.2 })⟩, λ ⟨x, h⟩, subtype.ext_val $ by simp, λ ⟨y, h⟩, subtype.ext_val $ by simp⟩ @[simp] lemma subtype_congr_apply {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ (a : α), p a ↔ q (e a)) (x : {x // p x}) : e.subtype_congr h x = ⟨e x, (h _).1 x.2⟩ := rfl @[simp] lemma subtype_congr_symm_apply {p : α → Prop} {q : β → Prop} (e : α ≃ β) (h : ∀ (a : α), p a ↔ q (e a)) (y : {y // q y}) : (e.subtype_congr h).symm y = ⟨e.symm y, (h _).2 $ (e.apply_symm_apply y).symm ▸ y.2⟩ := rfl /-- If two predicates `p` and `q` are pointwise equivalent, then `{x // p x}` is equivalent to `{x // q x}`. -/ @[simps] def subtype_congr_right {p q : α → Prop} (e : ∀x, p x ↔ q x) : {x // p x} ≃ {x // q x} := subtype_congr (equiv.refl _) e /-- If `α ≃ β`, then for any predicate `p : β → Prop` the subtype `{a // p (e a)}` is equivalent to the subtype `{b // p b}`. -/ def subtype_equiv_of_subtype {p : β → Prop} (e : α ≃ β) : {a : α // p (e a)} ≃ {b : β // p b} := subtype_congr e $ by simp /-- If `α ≃ β`, then for any predicate `p : α → Prop` the subtype `{a // p a}` is equivalent to the subtype `{b // p (e.symm b)}`. This version is used by `equiv_rw`. -/ def subtype_equiv_of_subtype' {p : α → Prop} (e : α ≃ β) : {a : α // p a} ≃ {b : β // p (e.symm b)} := e.symm.subtype_equiv_of_subtype.symm /-- If two predicates are equal, then the corresponding subtypes are equivalent. -/ def subtype_congr_prop {α : Type*} {p q : α → Prop} (h : p = q) : subtype p ≃ subtype q := subtype_congr (equiv.refl α) (assume a, h ▸ iff.rfl) /-- The subtypes corresponding to equal sets are equivalent. -/ @[simps apply] def set_congr {α : Type*} {s t : set α} (h : s = t) : s ≃ t := subtype_congr_prop h /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. This version allows the “inner” predicate to depend on `h : p a`. -/ def subtype_subtype_equiv_subtype_exists {α : Type u} (p : α → Prop) (q : subtype p → Prop) : subtype q ≃ {a : α // ∃h:p a, q ⟨a, h⟩ } := ⟨λ⟨⟨a, ha⟩, ha'⟩, ⟨a, ha, ha'⟩, λ⟨a, ha⟩, ⟨⟨a, ha.cases_on $ assume h _, h⟩, by { cases ha, exact ha_h }⟩, assume ⟨⟨a, ha⟩, h⟩, rfl, assume ⟨a, h₁, h₂⟩, rfl⟩ /-- A subtype of a subtype is equivalent to the subtype of elements satisfying both predicates. -/ def subtype_subtype_equiv_subtype_inter {α : Type u} (p q : α → Prop) : {x : subtype p // q x.1} ≃ subtype (λ x, p x ∧ q x) := (subtype_subtype_equiv_subtype_exists p _).trans $ subtype_congr_right $ λ x, exists_prop /-- If the outer subtype has more restrictive predicate than the inner one, then we can drop the latter. -/ def subtype_subtype_equiv_subtype {α : Type u} {p q : α → Prop} (h : ∀ {x}, q x → p x) : {x : subtype p // q x.1} ≃ subtype q := (subtype_subtype_equiv_subtype_inter p _).trans $ subtype_congr_right $ assume x, ⟨and.right, λ h₁, ⟨h h₁, h₁⟩⟩ /-- If a proposition holds for all elements, then the subtype is equivalent to the original type. -/ def subtype_univ_equiv {α : Type u} {p : α → Prop} (h : ∀ x, p x) : subtype p ≃ α := ⟨λ x, x, λ x, ⟨x, h x⟩, λ x, subtype.eq rfl, λ x, rfl⟩ /-- A subtype of a sigma-type is a sigma-type over a subtype. -/ def subtype_sigma_equiv {α : Type u} (p : α → Type v) (q : α → Prop) : { y : sigma p // q y.1 } ≃ Σ(x : subtype q), p x.1 := ⟨λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ x, ⟨⟨x.1.1, x.2⟩, x.1.2⟩, λ ⟨⟨x, h⟩, y⟩, rfl, λ ⟨⟨x, y⟩, h⟩, rfl⟩ /-- A sigma type over a subtype is equivalent to the sigma set over the original type, if the fiber is empty outside of the subset -/ def sigma_subtype_equiv_of_subset {α : Type u} (p : α → Type v) (q : α → Prop) (h : ∀ x, p x → q x) : (Σ x : subtype q, p x) ≃ Σ x : α, p x := (subtype_sigma_equiv p q).symm.trans $ subtype_univ_equiv $ λ x, h x.1 x.2 /-- If a predicate `p : β → Prop` is true on the range of a map `f : α → β`, then `Σ y : {y // p y}, {x // f x = y}` is equivalent to `α`. -/ def sigma_subtype_preimage_equiv {α : Type u} {β : Type v} (f : α → β) (p : β → Prop) (h : ∀ x, p (f x)) : (Σ y : subtype p, {x : α // f x = y}) ≃ α := calc _ ≃ Σ y : β, {x : α // f x = y} : sigma_subtype_equiv_of_subset _ p (λ y ⟨x, h'⟩, h' ▸ h x) ... ≃ α : sigma_preimage_equiv f /-- If for each `x` we have `p x ↔ q (f x)`, then `Σ y : {y // q y}, f ⁻¹' {y}` is equivalent to `{x // p x}`. -/ def sigma_subtype_preimage_equiv_subtype {α : Type u} {β : Type v} (f : α → β) {p : α → Prop} {q : β → Prop} (h : ∀ x, p x ↔ q (f x)) : (Σ y : subtype q, {x : α // f x = y}) ≃ subtype p := calc (Σ y : subtype q, {x : α // f x = y}) ≃ Σ y : subtype q, {x : subtype p // subtype.mk (f x) ((h x).1 x.2) = y} : begin apply sigma_congr_right, assume y, symmetry, refine (subtype_subtype_equiv_subtype_exists _ _).trans (subtype_congr_right _), assume x, exact ⟨λ ⟨hp, h'⟩, congr_arg subtype.val h', λ h', ⟨(h x).2 (h'.symm ▸ y.2), subtype.eq h'⟩⟩ end ... ≃ subtype p : sigma_preimage_equiv (λ x : subtype p, (⟨f x, (h x).1 x.property⟩ : subtype q)) /-- The `pi`-type `Π i, π i` is equivalent to the type of sections `f : ι → Σ i, π i` of the `sigma` type such that for all `i` we have `(f i).fst = i`. -/ def pi_equiv_subtype_sigma (ι : Type*) (π : ι → Type*) : (Πi, π i) ≃ {f : ι → Σi, π i | ∀i, (f i).1 = i } := ⟨ λf, ⟨λi, ⟨i, f i⟩, assume i, rfl⟩, λf i, begin rw ← f.2 i, exact (f.1 i).2 end, assume f, funext $ assume i, rfl, assume ⟨f, hf⟩, subtype.eq $ funext $ assume i, sigma.eq (hf i).symm $ eq_of_heq $ rec_heq_of_heq _ $ rec_heq_of_heq _ $ heq.refl _⟩ /-- The set of functions `f : Π a, β a` such that for all `a` we have `p a (f a)` is equivalent to the set of functions `Π a, {b : β a // p a b}`. -/ def subtype_pi_equiv_pi {α : Sort u} {β : α → Sort v} {p : Πa, β a → Prop} : {f : Πa, β a // ∀a, p a (f a) } ≃ Πa, { b : β a // p a b } := ⟨λf a, ⟨f.1 a, f.2 a⟩, λf, ⟨λa, (f a).1, λa, (f a).2⟩, by { rintro ⟨f, h⟩, refl }, by { rintro f, funext a, exact subtype.ext_val rfl }⟩ /-- A subtype of a product defined by componentwise conditions is equivalent to a product of subtypes. -/ def subtype_prod_equiv_prod {α : Type u} {β : Type v} {p : α → Prop} {q : β → Prop} : {c : α × β // p c.1 ∧ q c.2} ≃ ({a // p a} × {b // q b}) := ⟨λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ x, ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl, λ ⟨⟨_, _⟩, ⟨_, _⟩⟩, rfl⟩ end section subtype_equiv_codomain variables {X : Type*} {Y : Type*} [decidable_eq X] {x : X} /-- The type of all functions `X → Y` with prescribed values for all `x' ≠ x` is equivalent to the codomain `Y`. -/ def subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : {g : X → Y // g ∘ coe = f} ≃ Y := (subtype_preimage _ f).trans $ @fun_unique {x' // ¬ x' ≠ x} _ $ show unique {x' // ¬ x' ≠ x}, from @equiv.unique _ _ (show unique {x' // x' = x}, from { default := ⟨x, rfl⟩, uniq := λ ⟨x', h⟩, subtype.val_injective h }) (subtype_congr_right $ λ a, not_not) @[simp] lemma coe_subtype_equiv_codomain (f : {x' // x' ≠ x} → Y) : (subtype_equiv_codomain f : {g : X → Y // g ∘ coe = f} → Y) = λ g, (g : X → Y) x := rfl @[simp] lemma subtype_equiv_codomain_apply (f : {x' // x' ≠ x} → Y) (g : {g : X → Y // g ∘ coe = f}) : subtype_equiv_codomain f g = (g : X → Y) x := rfl lemma coe_subtype_equiv_codomain_symm (f : {x' // x' ≠ x} → Y) : ((subtype_equiv_codomain f).symm : Y → {g : X → Y // g ∘ coe = f}) = λ y, ⟨λ x', if h : x' ≠ x then f ⟨x', h⟩ else y, by { funext x', dsimp, erw [dif_pos x'.2, subtype.coe_eta] }⟩ := rfl @[simp] lemma subtype_equiv_codomain_symm_apply (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) : ((subtype_equiv_codomain f).symm y : X → Y) x' = if h : x' ≠ x then f ⟨x', h⟩ else y := rfl @[simp] lemma subtype_equiv_codomain_symm_apply_eq (f : {x' // x' ≠ x} → Y) (y : Y) : ((subtype_equiv_codomain f).symm y : X → Y) x = y := dif_neg (not_not.mpr rfl) lemma subtype_equiv_codomain_symm_apply_ne (f : {x' // x' ≠ x} → Y) (y : Y) (x' : X) (h : x' ≠ x) : ((subtype_equiv_codomain f).symm y : X → Y) x' = f ⟨x', h⟩ := dif_pos h end subtype_equiv_codomain namespace set open set /-- `univ α` is equivalent to `α`. -/ @[simps apply symm_apply] protected def univ (α) : @univ α ≃ α := ⟨coe, λ a, ⟨a, trivial⟩, λ ⟨a, _⟩, rfl, λ a, rfl⟩ /-- An empty set is equivalent to the `empty` type. -/ protected def empty (α) : (∅ : set α) ≃ empty := equiv_empty $ λ ⟨x, h⟩, not_mem_empty x h /-- An empty set is equivalent to a `pempty` type. -/ protected def pempty (α) : (∅ : set α) ≃ pempty := equiv_pempty $ λ ⟨x, h⟩, not_mem_empty x h /-- If sets `s` and `t` are separated by a decidable predicate, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union' {α} {s t : set α} (p : α → Prop) [decidable_pred p] (hs : ∀ x ∈ s, p x) (ht : ∀ x ∈ t, ¬ p x) : (s ∪ t : set α) ≃ s ⊕ t := { to_fun := λ x, if hp : p x then sum.inl ⟨_, x.2.resolve_right (λ xt, ht _ xt hp)⟩ else sum.inr ⟨_, x.2.resolve_left (λ xs, hp (hs _ xs))⟩, inv_fun := λ o, match o with | (sum.inl x) := ⟨x, or.inl x.2⟩ | (sum.inr x) := ⟨x, or.inr x.2⟩ end, left_inv := λ ⟨x, h'⟩, by by_cases p x; simp [union'._match_1, h]; congr, right_inv := λ o, begin rcases o with ⟨x, h⟩ | ⟨x, h⟩; dsimp [union'._match_1]; [simp [hs _ h], simp [ht _ h]] end } /-- If sets `s` and `t` are disjoint, then `s ∪ t` is equivalent to `s ⊕ t`. -/ protected def union {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) : (s ∪ t : set α) ≃ s ⊕ t := set.union' (λ x, x ∈ s) (λ _, id) (λ x xt xs, H ⟨xs, xt⟩) lemma union_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ s) : equiv.set.union H a = sum.inl ⟨a, ha⟩ := dif_pos ha lemma union_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) {a : (s ∪ t : set α)} (ha : ↑a ∈ t) : equiv.set.union H a = sum.inr ⟨a, ha⟩ := dif_neg $ λ h, H ⟨h, ha⟩ @[simp] lemma union_symm_apply_left {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : s) : (equiv.set.union H).symm (sum.inl a) = ⟨a, subset_union_left _ _ a.2⟩ := rfl @[simp] lemma union_symm_apply_right {α} {s t : set α} [decidable_pred (λ x, x ∈ s)] (H : s ∩ t ⊆ ∅) (a : t) : (equiv.set.union H).symm (sum.inr a) = ⟨a, subset_union_right _ _ a.2⟩ := rfl -- TODO: Any reason to use the same universe? /-- A singleton set is equivalent to a `punit` type. -/ protected def singleton {α} (a : α) : ({a} : set α) ≃ punit.{u} := ⟨λ _, punit.star, λ _, ⟨a, mem_singleton _⟩, λ ⟨x, h⟩, by { simp at h, subst x }, λ ⟨⟩, rfl⟩ /-- Equal sets are equivalent. -/ @[simps apply symm_apply] protected def of_eq {α : Type u} {s t : set α} (h : s = t) : s ≃ t := { to_fun := λ x, ⟨x, h ▸ x.2⟩, inv_fun := λ x, ⟨x, h.symm ▸ x.2⟩, left_inv := λ _, subtype.eq rfl, right_inv := λ _, subtype.eq rfl } /-- If `a ∉ s`, then `insert a s` is equivalent to `s ⊕ punit`. -/ protected def insert {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) : (insert a s : set α) ≃ s ⊕ punit.{u+1} := calc (insert a s : set α) ≃ ↥(s ∪ {a}) : equiv.set.of_eq (by simp) ... ≃ s ⊕ ({a} : set α) : equiv.set.union (by finish [set.subset_def]) ... ≃ s ⊕ punit.{u+1} : sum_congr (equiv.refl _) (equiv.set.singleton _) @[simp] lemma insert_symm_apply_inl {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) (b : s) : (equiv.set.insert H).symm (sum.inl b) = ⟨b, or.inr b.2⟩ := rfl @[simp] lemma insert_symm_apply_inr {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) (b : punit.{u+1}) : (equiv.set.insert H).symm (sum.inr b) = ⟨a, or.inl rfl⟩ := rfl @[simp] lemma insert_apply_left {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) : equiv.set.insert H ⟨a, or.inl rfl⟩ = sum.inr punit.star := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl @[simp] lemma insert_apply_right {α} {s : set.{u} α} [decidable_pred s] {a : α} (H : a ∉ s) (b : s) : equiv.set.insert H ⟨b, or.inr b.2⟩ = sum.inl b := (equiv.set.insert H).apply_eq_iff_eq_symm_apply.2 rfl /-- If `s : set α` is a set with decidable membership, then `s ⊕ sᶜ` is equivalent to `α`. -/ protected def sum_compl {α} (s : set α) [decidable_pred s] : s ⊕ (sᶜ : set α) ≃ α := calc s ⊕ (sᶜ : set α) ≃ ↥(s ∪ sᶜ) : (equiv.set.union (by simp [set.ext_iff])).symm ... ≃ @univ α : equiv.set.of_eq (by simp) ... ≃ α : equiv.set.univ _ @[simp] lemma sum_compl_apply_inl {α : Type u} (s : set α) [decidable_pred s] (x : s) : equiv.set.sum_compl s (sum.inl x) = x := rfl @[simp] lemma sum_compl_apply_inr {α : Type u} (s : set α) [decidable_pred s] (x : sᶜ) : equiv.set.sum_compl s (sum.inr x) = x := rfl lemma sum_compl_symm_apply_of_mem {α : Type u} {s : set α} [decidable_pred s] {x : α} (hx : x ∈ s) : (equiv.set.sum_compl s).symm x = sum.inl ⟨x, hx⟩ := have ↑(⟨x, or.inl hx⟩ : (s ∪ sᶜ : set α)) ∈ s, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_left _ this } lemma sum_compl_symm_apply_of_not_mem {α : Type u} {s : set α} [decidable_pred s] {x : α} (hx : x ∉ s) : (equiv.set.sum_compl s).symm x = sum.inr ⟨x, hx⟩ := have ↑(⟨x, or.inr hx⟩ : (s ∪ sᶜ : set α)) ∈ sᶜ, from hx, by { rw [equiv.set.sum_compl], simpa using set.union_apply_right _ this } @[simp] lemma sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred s] {x : s} : (equiv.set.sum_compl s).symm x = sum.inl x := by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx @[simp] lemma sum_compl_symm_apply_compl {α : Type*} {s : set α} [decidable_pred s] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x := by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx /-- `sum_diff_subset s t` is the natural equivalence between `s ⊕ (t \ s)` and `t`, where `s` and `t` are two sets. -/ protected def sum_diff_subset {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] : s ⊕ (t \ s : set α) ≃ t := calc s ⊕ (t \ s : set α) ≃ (s ∪ (t \ s) : set α) : (equiv.set.union (by simp [inter_diff_self])).symm ... ≃ t : equiv.set.of_eq (by { simp [union_diff_self, union_eq_self_of_subset_left h] }) @[simp] lemma sum_diff_subset_apply_inl {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] (x : s) : equiv.set.sum_diff_subset h (sum.inl x) = inclusion h x := rfl @[simp] lemma sum_diff_subset_apply_inr {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] (x : t \ s) : equiv.set.sum_diff_subset h (sum.inr x) = inclusion (diff_subset t s) x := rfl lemma sum_diff_subset_symm_apply_of_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] {x : t} (hx : x.1 ∈ s) : (equiv.set.sum_diff_subset h).symm x = sum.inl ⟨x, hx⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inl], exact subtype.eq rfl, end lemma sum_diff_subset_symm_apply_of_not_mem {α} {s t : set α} (h : s ⊆ t) [decidable_pred s] {x : t} (hx : x.1 ∉ s) : (equiv.set.sum_diff_subset h).symm x = sum.inr ⟨x, ⟨x.2, hx⟩⟩ := begin apply (equiv.set.sum_diff_subset h).injective, simp only [apply_symm_apply, sum_diff_subset_apply_inr], exact subtype.eq rfl, end /-- If `s` is a set with decidable membership, then the sum of `s ∪ t` and `s ∩ t` is equivalent to `s ⊕ t`. -/ protected def union_sum_inter {α : Type u} (s t : set α) [decidable_pred s] : (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ s ⊕ t := calc (s ∪ t : set α) ⊕ (s ∩ t : set α) ≃ (s ∪ t \ s : set α) ⊕ (s ∩ t : set α) : by rw [union_diff_self] ... ≃ (s ⊕ (t \ s : set α)) ⊕ (s ∩ t : set α) : sum_congr (set.union $ subset_empty_iff.2 (inter_diff_self _ _)) (equiv.refl _) ... ≃ s ⊕ (t \ s : set α) ⊕ (s ∩ t : set α) : sum_assoc _ _ _ ... ≃ s ⊕ (t \ s ∪ s ∩ t : set α) : sum_congr (equiv.refl _) begin refine (set.union' (∉ s) _ _).symm, exacts [λ x hx, hx.2, λ x hx, not_not_intro hx.1] end ... ≃ s ⊕ t : by { rw (_ : t \ s ∪ s ∩ t = t), rw [union_comm, inter_comm, inter_union_diff] } /-- Given an equivalence `e₀` between sets `s : set α` and `t : set β`, the set of equivalences `e : α ≃ β` such that `e ↑x = ↑(e₀ x)` for each `x : s` is equivalent to the set of equivalences between `sᶜ` and `tᶜ`. -/ protected def compl {α : Type u} {β : Type v} {s : set α} {t : set β} [decidable_pred s] [decidable_pred t] (e₀ : s ≃ t) : {e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) := { to_fun := λ e, subtype_congr e (λ a, not_congr $ iff.symm $ maps_to.mem_iff (maps_to_iff_exists_map_subtype.2 ⟨e₀, e.2⟩) (surj_on.maps_to_compl (surj_on_iff_exists_map_subtype.2 ⟨t, e₀, subset.refl t, e₀.surjective, e.2⟩) e.1.injective)), inv_fun := λ e₁, subtype.mk (calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm ... ≃ t ⊕ (tᶜ : set β) : e₀.sum_congr e₁ ... ≃ β : set.sum_compl t) (λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply, set.sum_compl_apply_inl, set.sum_compl_symm_apply]), left_inv := λ e, begin ext x, by_cases hx : x ∈ s, { simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩, sum.map_inl, sum_congr_apply, trans_apply, subtype.coe_mk, set.sum_compl_apply_inl] }, { simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr, subtype_congr_apply, set.sum_compl_apply_inr, trans_apply, sum_congr_apply, subtype.coe_mk] }, end, right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_congr_apply, set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans, subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] } /-- The set product of two sets is equivalent to the type product of their coercions to types. -/ protected def prod {α β} (s : set α) (t : set β) : s.prod t ≃ s × t := @subtype_prod_equiv_prod α β s t /-- If a function `f` is injective on a set `s`, then `s` is equivalent to `f '' s`. -/ protected noncomputable def image_of_inj_on {α β} (f : α → β) (s : set α) (H : inj_on f s) : s ≃ (f '' s) := ⟨λ p, ⟨f p, mem_image_of_mem f p.2⟩, λ p, ⟨classical.some p.2, (classical.some_spec p.2).1⟩, λ ⟨x, h⟩, subtype.eq (H (classical.some_spec (mem_image_of_mem f h)).1 h (classical.some_spec (mem_image_of_mem f h)).2), λ ⟨y, h⟩, subtype.eq (classical.some_spec h).2⟩ /-- If `f` is an injective function, then `s` is equivalent to `f '' s`. -/ @[simps apply] protected noncomputable def image {α β} (f : α → β) (s : set α) (H : injective f) : s ≃ (f '' s) := equiv.set.image_of_inj_on f s (H.inj_on s) lemma image_symm_preimage {α β} {f : α → β} (hf : injective f) (u s : set α) : (λ x, (set.image f s hf).symm x : f '' s → α) ⁻¹' u = coe ⁻¹' (f '' u) := begin ext ⟨b, a, has, rfl⟩, have : ∀(h : ∃a', a' ∈ s ∧ a' = a), classical.some h = a := λ h, (classical.some_spec h).2, simp [equiv.set.image, equiv.set.image_of_inj_on, hf, this], end /-- If `f : α → β` is an injective function, then `α` is equivalent to the range of `f`. -/ @[simps apply] protected noncomputable def range {α β} (f : α → β) (H : injective f) : α ≃ range f := { to_fun := λ x, ⟨f x, mem_range_self _⟩, inv_fun := λ x, classical.some x.2, left_inv := λ x, H (classical.some_spec (show f x ∈ range f, from mem_range_self _)), right_inv := λ x, subtype.eq $ classical.some_spec x.2 } theorem apply_range_symm {α β} (f : α → β) (H : injective f) (b : range f) : f ((set.range f H).symm b) = b := begin conv_rhs { rw ←((set.range f H).right_inv b), }, simp, end /-- If `α` is equivalent to `β`, then `set α` is equivalent to `set β`. -/ protected def congr {α β : Type*} (e : α ≃ β) : set α ≃ set β := ⟨λ s, e '' s, λ t, e.symm '' t, symm_image_image e, symm_image_image e.symm⟩ /-- The set `{x ∈ s | t x}` is equivalent to the set of `x : s` such that `t x`. -/ protected def sep {α : Type u} (s : set α) (t : α → Prop) : ({ x ∈ s | t x } : set α) ≃ { x : s | t x } := (equiv.subtype_subtype_equiv_subtype_inter s t).symm /-- The set `𝒫 S := {x | x ⊆ S}` is equivalent to the type `set S`. -/ protected def powerset {α} (S : set α) : 𝒫 S ≃ set S := { to_fun := λ x : 𝒫 S, coe ⁻¹' (x : set α), inv_fun := λ x : set S, ⟨coe '' x, by rintro _ ⟨a : S, _, rfl⟩; exact a.2⟩, left_inv := λ x, by ext y; exact ⟨λ ⟨⟨_, _⟩, h, rfl⟩, h, λ h, ⟨⟨_, x.2 h⟩, h, rfl⟩⟩, right_inv := λ x, by ext; simp } end set /-- If `f` is a bijective function, then its domain is equivalent to its codomain. -/ @[simps apply] noncomputable def of_bijective {α β} (f : α → β) (hf : bijective f) : α ≃ β := (equiv.set.range f hf.1).trans $ (set_congr hf.2.range_eq).trans $ equiv.set.univ β lemma of_bijective_apply_symm_apply {α β} (f : α → β) (hf : bijective f) (x : β) : f ((of_bijective f hf).symm x) = x := (of_bijective f hf).apply_symm_apply x @[simp] lemma of_bijective_symm_apply_apply {α β} (f : α → β) (hf : bijective f) (x : α) : (of_bijective f hf).symm (f x) = x := (of_bijective f hf).symm_apply_apply x /-- If `f` is an injective function, then its domain is equivalent to its range. -/ @[simps apply] noncomputable def of_injective {α β} (f : α → β) (hf : injective f) : α ≃ _root_.set.range f := of_bijective (λ x, ⟨f x, set.mem_range_self x⟩) ⟨λ x y hxy, hf $ by injections, λ ⟨_, x, rfl⟩, ⟨x, rfl⟩⟩ /-- Subtype of the quotient is equivalent to the quotient of the subtype. Let `α` be a setoid with equivalence relation `~`. Let `p₂` be a predicate on the quotient type `α/~`, and `p₁` be the lift of this predicate to `α`: `p₁ a ↔ p₂ ⟦a⟧`. Let `~₂` be the restriction of `~` to `{x // p₁ x}`. Then `{x // p₂ x}` is equivalent to the quotient of `{x // p₁ x}` by `~₂`. -/ def subtype_quotient_equiv_quotient_subtype (p₁ : α → Prop) [s₁ : setoid α] [s₂ : setoid (subtype p₁)] (p₂ : quotient s₁ → Prop) (hp₂ : ∀ a, p₁ a ↔ p₂ ⟦a⟧) (h : ∀ x y : subtype p₁, @setoid.r _ s₂ x y ↔ (x : α) ≈ y) : {x // p₂ x} ≃ quotient s₂ := { to_fun := λ a, quotient.hrec_on a.1 (λ a h, ⟦⟨a, (hp₂ _).2 h⟩⟧) (λ a b hab, hfunext (by rw quotient.sound hab) (λ h₁ h₂ _, heq_of_eq (quotient.sound ((h _ _).2 hab)))) a.2, inv_fun := λ a, quotient.lift_on a (λ a, (⟨⟦a.1⟧, (hp₂ _).1 a.2⟩ : {x // p₂ x})) (λ a b hab, subtype.ext_val (quotient.sound ((h _ _).1 hab))), left_inv := λ ⟨a, ha⟩, quotient.induction_on a (λ a ha, rfl) ha, right_inv := λ a, quotient.induction_on a (λ ⟨a, ha⟩, rfl) } section swap variable [decidable_eq α] /-- A helper function for `equiv.swap`. -/ def swap_core (a b r : α) : α := if r = a then b else if r = b then a else r theorem swap_core_self (r a : α) : swap_core a a r = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_swap_core (r a b : α) : swap_core a b (swap_core a b r) = r := by { unfold swap_core, split_ifs; cc } theorem swap_core_comm (r a b : α) : swap_core a b r = swap_core b a r := by { unfold swap_core, split_ifs; cc } /-- `swap a b` is the permutation that swaps `a` and `b` and leaves other values as is. -/ def swap (a b : α) : perm α := ⟨swap_core a b, swap_core a b, λr, swap_core_swap_core r a b, λr, swap_core_swap_core r a b⟩ theorem swap_self (a : α) : swap a a = equiv.refl _ := ext $ λ r, swap_core_self r a theorem swap_comm (a b : α) : swap a b = swap b a := ext $ λ r, swap_core_comm r _ _ theorem swap_apply_def (a b x : α) : swap a b x = if x = a then b else if x = b then a else x := rfl @[simp] theorem swap_apply_left (a b : α) : swap a b a = b := if_pos rfl @[simp] theorem swap_apply_right (a b : α) : swap a b b = a := by { by_cases h : b = a; simp [swap_apply_def, h], } theorem swap_apply_of_ne_of_ne {a b x : α} : x ≠ a → x ≠ b → swap a b x = x := by simp [swap_apply_def] {contextual := tt} @[simp] theorem swap_swap (a b : α) : (swap a b).trans (swap a b) = equiv.refl _ := ext $ λ x, swap_core_swap_core _ _ _ theorem swap_comp_apply {a b x : α} (π : perm α) : π.trans (swap a b) x = if π x = a then b else if π x = b then a else π x := by { cases π, refl } lemma swap_eq_update (i j : α) : ⇑(equiv.swap i j) = update (update id j i) i j := funext $ λ x, by rw [update_apply _ i j, update_apply _ j i, equiv.swap_apply_def, id.def] lemma comp_swap_eq_update (i j : α) (f : α → β) : f ∘ equiv.swap i j = update (update f j (f i)) i (f j) := by rw [swap_eq_update, comp_update, comp_update, comp.right_id] @[simp] lemma symm_trans_swap_trans [decidable_eq β] (a b : α) (e : α ≃ β) : (e.symm.trans (swap a b)).trans e = swap (e a) (e b) := equiv.ext (λ x, begin have : ∀ a, e.symm x = a ↔ x = e a := λ a, by { rw @eq_comm _ (e.symm x), split; intros; simp * at * }, simp [swap_apply_def, this], split_ifs; simp end) @[simp] lemma swap_apply_self (i j a : α) : swap i j (swap i j a) = a := by rw [← equiv.trans_apply, equiv.swap_swap, equiv.refl_apply] /-- A function is invariant to a swap if it is equal at both elements -/ lemma apply_swap_eq_self {v : α → β} {i j : α} (hv : v i = v j) (k : α) : v (swap i j k) = v k := begin by_cases hi : k = i, { rw [hi, swap_apply_left, hv] }, by_cases hj : k = j, { rw [hj, swap_apply_right, hv] }, rw swap_apply_of_ne_of_ne hi hj, end namespace perm @[simp] lemma sum_congr_swap_refl {α β : Sort*} [decidable_eq α] [decidable_eq β] (i j : α) : equiv.perm.sum_congr (equiv.swap i j) (equiv.refl β) = equiv.swap (sum.inl i) (sum.inl j) := begin ext x, cases x, { simp [sum.map, swap_apply_def], split_ifs; refl}, { simp [sum.map, swap_apply_of_ne_of_ne] }, end @[simp] lemma sum_congr_refl_swap {α β : Sort*} [decidable_eq α] [decidable_eq β] (i j : β) : equiv.perm.sum_congr (equiv.refl α) (equiv.swap i j) = equiv.swap (sum.inr i) (sum.inr j) := begin ext x, cases x, { simp [sum.map, swap_apply_of_ne_of_ne] }, { simp [sum.map, swap_apply_def], split_ifs; refl}, end end perm /-- Augment an equivalence with a prescribed mapping `f a = b` -/ def set_value (f : α ≃ β) (a : α) (b : β) : α ≃ β := (swap a (f.symm b)).trans f @[simp] theorem set_value_eq (f : α ≃ β) (a : α) (b : β) : set_value f a b a = b := by { dsimp [set_value], simp [swap_apply_left] } end swap protected lemma exists_unique_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p x ↔ q (f x)) : (∃! x, p x) ↔ ∃! y, q y := begin split, { rintro ⟨a, ha₁, ha₂⟩, exact ⟨f a, h.1 ha₁, λ b hb, f.symm_apply_eq.1 (ha₂ (f.symm b) (h.2 (by simpa using hb)))⟩ }, { rintro ⟨b, hb₁, hb₂⟩, exact ⟨f.symm b, h.2 (by simpa using hb₁), λ y hy, (eq_symm_apply f).2 (hb₂ _ (h.1 hy))⟩ } end protected lemma exists_unique_congr_left' {p : α → Prop} (f : α ≃ β) : (∃! x, p x) ↔ (∃! y, p (f.symm y)) := equiv.exists_unique_congr f (λx, by simp) protected lemma exists_unique_congr_left {p : β → Prop} (f : α ≃ β) : (∃! x, p (f x)) ↔ (∃! y, p y) := (equiv.exists_unique_congr_left' f.symm).symm protected lemma forall_congr {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p x ↔ q (f x)) : (∀x, p x) ↔ (∀y, q y) := begin split; intros h₂ x, { rw [←f.right_inv x], apply h.mp, apply h₂ }, apply h.mpr, apply h₂ end protected lemma forall_congr' {p : α → Prop} {q : β → Prop} (f : α ≃ β) (h : ∀{x}, p (f.symm x) ↔ q x) : (∀x, p x) ↔ (∀y, q y) := (equiv.forall_congr f.symm (λ x, h.symm)).symm -- We next build some higher arity versions of `equiv.forall_congr`. -- Although they appear to just be repeated applications of `equiv.forall_congr`, -- unification of metavariables works better with these versions. -- In particular, they are necessary in `equiv_rw`. -- (Stopping at ternary functions seems reasonable: at least in 1-categorical mathematics, -- it's rare to have axioms involving more than 3 elements at once.) universes ua1 ua2 ub1 ub2 ug1 ug2 variables {α₁ : Sort ua1} {α₂ : Sort ua2} {β₁ : Sort ub1} {β₂ : Sort ub2} {γ₁ : Sort ug1} {γ₂ : Sort ug2} protected lemma forall₂_congr {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p x y ↔ q (eα x) (eβ y)) : (∀x y, p x y) ↔ (∀x y, q x y) := begin apply equiv.forall_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₂_congr' {p : α₁ → β₁ → Prop} {q : α₂ → β₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (h : ∀{x y}, p (eα.symm x) (eβ.symm y) ↔ q x y) : (∀x y, p x y) ↔ (∀x y, q x y) := (equiv.forall₂_congr eα.symm eβ.symm (λ x y, h.symm)).symm protected lemma forall₃_congr {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p x y z ↔ q (eα x) (eβ y) (eγ z)) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := begin apply equiv.forall₂_congr, intros, apply equiv.forall_congr, intros, apply h, end protected lemma forall₃_congr' {p : α₁ → β₁ → γ₁ → Prop} {q : α₂ → β₂ → γ₂ → Prop} (eα : α₁ ≃ α₂) (eβ : β₁ ≃ β₂) (eγ : γ₁ ≃ γ₂) (h : ∀{x y z}, p (eα.symm x) (eβ.symm y) (eγ.symm z) ↔ q x y z) : (∀x y z, p x y z) ↔ (∀x y z, q x y z) := (equiv.forall₃_congr eα.symm eβ.symm eγ.symm (λ x y z, h.symm)).symm protected lemma forall_congr_left' {p : α → Prop} (f : α ≃ β) : (∀x, p x) ↔ (∀y, p (f.symm y)) := equiv.forall_congr f (λx, by simp) protected lemma forall_congr_left {p : β → Prop} (f : α ≃ β) : (∀x, p (f x)) ↔ (∀y, p y) := (equiv.forall_congr_left' f.symm).symm section variables (P : α → Sort w) (e : α ≃ β) /-- Transport dependent functions through an equivalence of the base space. -/ @[simps] def Pi_congr_left' : (Π a, P a) ≃ (Π b, P (e.symm b)) := { to_fun := λ f x, f (e.symm x), inv_fun := λ f x, begin rw [← e.symm_apply_apply x], exact f (e x) end, left_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { dsimp, rw e.symm_apply_apply })), right_inv := λ f, funext $ λ x, eq_of_heq ((eq_rec_heq _ _).trans (by { rw e.apply_symm_apply })) } end section variables (P : β → Sort w) (e : α ≃ β) /-- Transporting dependent functions through an equivalence of the base, expressed as a "simplification". -/ def Pi_congr_left : (Π a, P (e a)) ≃ (Π b, P b) := (Pi_congr_left' P e.symm).symm end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π a : α, (W a ≃ Z (h₁ a))) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibers. -/ def Pi_congr : (Π a, W a) ≃ (Π b, Z b) := (equiv.Pi_congr_right h₂).trans (equiv.Pi_congr_left _ h₁) end section variables {W : α → Sort w} {Z : β → Sort z} (h₁ : α ≃ β) (h₂ : Π b : β, (W (h₁.symm b) ≃ Z b)) /-- Transport dependent functions through an equivalence of the base spaces and a family of equivalences of the matching fibres. -/ def Pi_congr' : (Π a, W a) ≃ (Π b, Z b) := (Pi_congr h₁.symm (λ b, (h₂ b).symm)).symm end end equiv instance {α} [subsingleton α] : subsingleton (ulift α) := equiv.ulift.subsingleton instance {α} [subsingleton α] : subsingleton (plift α) := equiv.plift.subsingleton instance {α} [decidable_eq α] : decidable_eq (ulift α) := equiv.ulift.decidable_eq instance {α} [decidable_eq α] : decidable_eq (plift α) := equiv.plift.decidable_eq /-- If both `α` and `β` are singletons, then `α ≃ β`. -/ def equiv_of_unique_of_unique [unique α] [unique β] : α ≃ β := { to_fun := λ _, default β, inv_fun := λ _, default α, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- If `α` is a singleton, then it is equivalent to any `punit`. -/ def equiv_punit_of_unique [unique α] : α ≃ punit.{v} := equiv_of_unique_of_unique /-- If `α` is a subsingleton, then it is equivalent to `α × α`. -/ def subsingleton_prod_self_equiv {α : Type*} [subsingleton α] : α × α ≃ α := { to_fun := λ p, p.1, inv_fun := λ a, (a, a), left_inv := λ p, subsingleton.elim _ _, right_inv := λ p, subsingleton.elim _ _, } /-- To give an equivalence between two subsingleton types, it is sufficient to give any two functions between them. -/ def equiv_of_subsingleton_of_subsingleton [subsingleton α] [subsingleton β] (f : α → β) (g : β → α) : α ≃ β := { to_fun := f, inv_fun := g, left_inv := λ _, subsingleton.elim _ _, right_inv := λ _, subsingleton.elim _ _ } /-- `unique (unique α)` is equivalent to `unique α`. -/ def unique_unique_equiv : unique (unique α) ≃ unique α := equiv_of_subsingleton_of_subsingleton (λ h, h.default) (λ h, { default := h, uniq := λ _, subsingleton.elim _ _ }) namespace quot /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : α → α → Prop} {rb : β → β → Prop} (e : α ≃ β) (eq : ∀a₁ a₂, ra a₁ a₂ ↔ rb (e a₁) (e a₂)) : quot ra ≃ quot rb := { to_fun := quot.map e (assume a₁ a₂, (eq a₁ a₂).1), inv_fun := quot.map e.symm (assume b₁ b₂ h, (eq (e.symm b₁) (e.symm b₂)).2 ((e.apply_symm_apply b₁).symm ▸ (e.apply_symm_apply b₂).symm ▸ h)), left_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.symm_apply_apply] }, right_inv := by { rintros ⟨a⟩, dunfold quot.map, simp only [equiv.apply_symm_apply] } } /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : α → α → Prop} (eq : ∀a₁ a₂, r a₁ a₂ ↔ r' a₁ a₂) : quot r ≃ quot r' := quot.congr (equiv.refl α) eq /-- An equivalence `e : α ≃ β` generates an equivalence between the quotient space of `α` by a relation `ra` and the quotient space of `β` by the image of this relation under `e`. -/ protected def congr_left {r : α → α → Prop} (e : α ≃ β) : quot r ≃ quot (λ b b', r (e.symm b) (e.symm b')) := @quot.congr α β r (λ b b', r (e.symm b) (e.symm b')) e (λ a₁ a₂, by simp only [e.symm_apply_apply]) end quot namespace quotient /-- An equivalence `e : α ≃ β` generates an equivalence between quotient spaces, if `ra a₁ a₂ ↔ rb (e a₁) (e a₂). -/ protected def congr {ra : setoid α} {rb : setoid β} (e : α ≃ β) (eq : ∀a₁ a₂, @setoid.r α ra a₁ a₂ ↔ @setoid.r β rb (e a₁) (e a₂)) : quotient ra ≃ quotient rb := quot.congr e eq /-- Quotients are congruent on equivalences under equality of their relation. An alternative is just to use rewriting with `eq`, but then computational proofs get stuck. -/ protected def congr_right {r r' : setoid α} (eq : ∀a₁ a₂, @setoid.r α r a₁ a₂ ↔ @setoid.r α r' a₁ a₂) : quotient r ≃ quotient r' := quot.congr_right eq end quotient /-- If a function is a bijection between two sets `s` and `t`, then it induces an equivalence between the the types `↥s` and ``↥t`. -/ noncomputable def set.bij_on.equiv {α : Type*} {β : Type*} {s : set α} {t : set β} (f : α → β) (h : set.bij_on f s t) : s ≃ t := equiv.of_bijective _ h.bijective namespace function lemma update_comp_equiv {α β α' : Sort*} [decidable_eq α'] [decidable_eq α] (f : α → β) (g : α' ≃ α) (a : α) (v : β) : update f a v ∘ g = update (f ∘ g) (g.symm a) v := by rw [←update_comp _ g.injective, g.apply_symm_apply] lemma update_apply_equiv_apply {α β α' : Sort*} [decidable_eq α'] [decidable_eq α] (f : α → β) (g : α' ≃ α) (a : α) (v : β) (a' : α') : update f a v (g a') = update (f ∘ g) (g.symm a) v a' := congr_fun (update_comp_equiv f g a v) a' end function /-- The composition of an updated function with an equiv on a subset can be expressed as an updated function. -/ lemma dite_comp_equiv_update {α : Type*} {β : Sort*} {γ : Sort*} {s : set α} (e : β ≃ s) (v : β → γ) (w : α → γ) (j : β) (x : γ) [decidable_eq β] [decidable_eq α] [∀ j, decidable (j ∈ s)] : (λ (i : α), if h : i ∈ s then (function.update v j x) (e.symm ⟨i, h⟩) else w i) = function.update (λ (i : α), if h : i ∈ s then v (e.symm ⟨i, h⟩) else w i) (e j) x := begin ext i, by_cases h : i ∈ s, { rw [dif_pos h, function.update_apply_equiv_apply, equiv.symm_symm, function.comp, function.update_apply, function.update_apply, dif_pos h], have h_coe : (⟨i, h⟩ : s) = e j ↔ i = e j := subtype.ext_iff.trans (by rw subtype.coe_mk), simp_rw h_coe, congr, }, { have : i ≠ e j, by { contrapose! h, have : (e j : α) ∈ s := (e j).2, rwa ← h at this }, simp [h, this] } end
5b2a440f3db016027b470953854fec8f5e6107dd
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/lean3RefineBug.lean
3b4452f4afcebbdb99e19aca4aea6301f18d2cd7
[ "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
619
lean
example (p q : Prop) : p ∧ q → p := by refine fun ⟨a, b⟩ => a example (p q : Prop) : p ∧ q → p := by refine fun a => ?hp trace_state exact a.1 example (p q : Prop) : p ∧ q → p := by refine fun ⟨a, b⟩ => a example (p q : Prop) : p ∧ q → p := by refine fun ⟨a, b⟩ => ?hp trace_state exact a example (p q : Prop) : p ∧ q → p := by refine fun a => ?hp case hp => exact a.1 example (p q : Prop) : p ∧ q → p := by refine fun ⟨a, b⟩ => ?hp case hp => exact a example (p q : Prop) : p ∧ q → p := by refine λ ⟨a, b⟩ => ?hp trace_state exact a
229ab6e506b158104f0509da3caced9e9c4246b0
1717bcbca047a0d25d687e7e9cd482fba00d058f
/src/data/set/basic.lean
b33c4c3c97986205dc5a7b4544d095e0bb7cff59
[ "Apache-2.0" ]
permissive
swapnilkapoor22/mathlib
51ad5804e6a0635ed5c7611cee73e089ab271060
3e7efd4ecd5d379932a89212eebd362beb01309e
refs/heads/master
1,676,467,741,465
1,610,301,556,000
1,610,301,556,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
97,293
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 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 coersion 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`) * `prod s 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 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 (s t : set α) : s ⊔ t = s ∪ t := rfl @[simp] lemma inf_eq_inter (s t : set α) : s ⊓ t = s ∩ t := rfl @[simp] lemma le_eq_subset (s t : set α) : s ≤ t = (s ⊆ t) := rfl /-! `set.lt_eq_ssubset` is defined further down -/ /-- Coercion from a set to the corresponding subtype. -/ instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩ 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_mem (s : set α) [H : decidable_pred s] : ∀ a, decidable (a ∈ s) := H 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 := ext (λ x, iff.intro (λ ina, h₁ ina) (λ inb, h₂ inb)) theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := ⟨λ e, e ▸ ⟨subset.refl _, subset.refl _⟩, λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩ -- an alternative name theorem eq_of_subset_of_subset {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := subset.antisymm h₁ h₂ theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := assume h₁ h₂, h₁ h₂ theorem not_subset : (¬ s ⊆ t) ↔ ∃a ∈ s, a ∉ t := by simp [subset_def, not_forall] /-! ### Definition of strict subsets `s ⊂ t` and basic properties. -/ instance : has_ssubset (set α) := ⟨(<)⟩ @[simp] lemma lt_eq_ssubset (s t : set α) : s < t = (s ⊂ t) := rfl theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ ¬ (t ⊆ s)) := rfl theorem eq_or_ssubset_of_subset (h : s ⊆ t) : s = t ∨ s ⊂ t := classical.by_cases (λ H : t ⊆ s, or.inl $ subset.antisymm h H) (λ H, or.inr ⟨h, 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 := by split; simp [set.ssubset_def, ne.def, set.subset.antisymm_iff] {contextual := tt} 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⟩⟩ theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) := assume h : x ∈ ∅, h @[simp] theorem not_not_mem : ¬ (a ∉ s) ↔ a ∈ s := by { classical, exact 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 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.nonempty → s ≠ ∅ | ⟨x, hx⟩ hs := by { rw hs at hx, exact hx } /-- 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 theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s := by simp [ext_iff] @[simp] theorem empty_subset (s : set α) : ∅ ⊆ s := assume x, assume h, false.elim h theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ := by simp [subset.antisymm_iff] theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ := subset_empty_iff.1 theorem eq_empty_of_not_nonempty (h : ¬nonempty α) (s : set α) : s = ∅ := eq_empty_of_subset_empty $ λ x hx, h ⟨x⟩ 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 := not_nonempty_iff_eq_empty.2 rfl lemma eq_empty_or_nonempty (s : set α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inr (λ h, or.inl $ not_nonempty_iff_eq_empty.1 h) theorem ne_empty_iff_nonempty : s ≠ ∅ ↔ s.nonempty := (not_congr not_nonempty_iff_eq_empty.symm).trans not_not 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 := by simp [iff_def] /-! ### 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 theorem empty_ne_univ [h : nonempty α] : (∅ : set α) ≠ univ := by simp [ext_iff] @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ := by simp [subset.antisymm_iff] 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 := by simp [ext_iff] 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 @[simp] lemma univ_eq_empty_iff : (univ : set α) = ∅ ↔ ¬ nonempty α := eq_empty_iff_forall_not_mem.trans ⟨λ H ⟨x⟩, H x trivial, λ H x _, H ⟨x⟩⟩ lemma exists_mem_of_nonempty (α) : ∀ [nonempty α], ∃x:α, x ∈ (univ : set α) | ⟨x⟩ := ⟨x, trivial⟩ instance univ_decidable : decidable_pred (@set.univ α) := λ x, is_true trivial /-- `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 (assume x, or_self _) @[simp] theorem union_empty (a : set α) : a ∪ ∅ = a := ext (assume x, or_false _) @[simp] theorem empty_union (a : set α) : ∅ ∪ a = a := ext (assume x, false_or _) theorem union_comm (a b : set α) : a ∪ b = b ∪ a := ext (assume x, or.comm) theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) := ext (assume 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₃) := by finish theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by finish theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := by finish [subset_def, ext_iff, iff_def] @[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 := by finish [subset_def, union_def] @[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := by finish [iff_def, subset_def] theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := by finish [subset_def] theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h (by refl) theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union (by refl) 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 finish [ext_iff], by finish [ext_iff]⟩ /-! ### 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 (assume x, and_self _) @[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ := ext (assume x, and_false _) @[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ := ext (assume x, false_and _) theorem inter_comm (a b : set α) : a ∩ b = b ∩ a := ext (assume x, and.comm) theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext (assume 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₃) := by finish theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := by finish @[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H @[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := by finish [subset_def, inter_def] @[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := ⟨λ h, ⟨subset.trans h (inter_subset_left _ _), subset.trans h (inter_subset_right _ _)⟩, λ ⟨h₁, h₂⟩, subset_inter h₁ h₂⟩ @[simp] theorem inter_univ (a : set α) : a ∩ univ = a := ext (assume x, and_true _) @[simp] theorem univ_inter (a : set α) : univ ∩ a = a := ext (assume x, true_and _) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := by finish [subset_def] theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := by finish [subset_def] theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := by finish [subset_def] theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s := by finish [subset_def, ext_iff, iff_def] theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t := by finish [subset_def, ext_iff, iff_def] theorem subset_iff_inter_eq_self {s t : set α} : s ⊆ t ↔ s ∩ t = s := ⟨λ h, inter_eq_self_of_subset_left h, λ h x h1, set.mem_of_mem_inter_right (by {rw h, exact h1})⟩ lemma inter_compl_nonempty_iff {s t : set α} : (s ∩ tᶜ).nonempty ↔ ¬ s ⊆ t := begin split, { rintros ⟨x ,xs, xt⟩ sub, exact xt (sub xs) }, { intros h, rcases not_subset.mp h with ⟨x, xs, xt⟩, exact ⟨x, xs, xt⟩ } end theorem union_inter_cancel_left {s t : set α} : (s ∪ t) ∩ s = s := by finish [ext_iff, iff_def] theorem union_inter_cancel_right {s t : set α} : (s ∪ t) ∩ t = t := by finish [ext_iff, iff_def] /-! ### Distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (assume x, and_or_distrib_left) theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (assume x, or_and_distrib_right) theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (assume x, or_and_distrib_left) theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (assume x, and_or_distrib_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 := assume y ys, or.inr ys 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 α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := by finish [insert_def] @[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 := by finish [ext_iff, iff_def] lemma ne_insert_of_not_mem {s : set α} (t : set α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) := by simp [subset_def, or_imp_distrib, forall_and_distrib] theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t := assume a', or.imp_right (@h a') 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) := by { ext, simp [or.left_comm] } theorem insert_union : insert a s ∪ t = insert a (s ∪ t) := by { ext, simp [or.comm, or.left_comm] } @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := by { ext, simp [or.comm, 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 := by { ext y, simp [←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, x ∈ s → P x := by finish theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) : ∀ x, x ∈ insert a s → P x := by finish theorem bex_insert_iff {P : α → Prop} {a : α} {s : set α} : (∃ x ∈ insert a s, P x) ↔ (∃ x ∈ s, P x) ∨ P a := by finish [iff_def] theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := by finish [iff_def] /-! ### 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} := set.ext $ λ n, (set.mem_singleton_iff).symm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := by finish theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := by finish @[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y := by finish [ext_iff, iff_def] theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := by finish theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := by finish [ext_iff, or_comm] @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := by finish theorem pair_comm (a b : α) : ({a, b} : set α) = {b, a} := ext $ λ x, or_comm _ _ @[simp] theorem singleton_nonempty (a : α) : ({a} : set α).nonempty := ⟨a, rfl⟩ @[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s := ⟨λh, h (by simp), λh b e, by { rw [mem_singleton_iff] at e, simp [*] }⟩ theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} := by { ext, simp } @[simp] theorem singleton_union : {a} ∪ s = insert a s := rfl @[simp] theorem union_singleton : s ∪ {a} = insert a s := by rw [union_comm, singleton_union] @[simp] theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s := by simp [eq_empty_iff_forall_not_mem] @[simp] theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s := by rw [inter_comm, singleton_inter_eq_empty] @[simp] theorem singleton_inter_nonempty : ({a} ∩ s).nonempty ↔ a ∈ s := by rw [← ne_empty_iff_nonempty, ne.def, singleton_inter_eq_empty, not_not] @[simp] theorem inter_singleton_nonempty : (s ∩ {a}).nonempty ↔ a ∈ s := by rw [inter_comm, singleton_inter_nonempty] lemma nmem_singleton_empty {s : set α} : s ∉ ({∅} : set (set α)) ↔ s.nonempty := by rw [mem_singleton_iff, ← ne.def, ne_empty_iff_nonempty] instance unique_singleton (a : α) : unique ↥({a} : set α) := { default := ⟨a, mem_singleton a⟩, uniq := begin intros x, apply subtype.ext, apply eq_of_mem_singleton (subtype.mem x), end} lemma eq_singleton_iff_unique_mem {s : set α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := by simp [ext_iff, @iff_def (_ ∈ s), forall_and_distrib, and_comm] lemma eq_singleton_iff_nonempty_unique_mem {s : set α} {a : α} : s = {a} ↔ s.nonempty ∧ ∀ x ∈ s, x = a := begin split, { intros h, subst h, simp, }, { rintros ⟨hne, h_uniq⟩, rw eq_singleton_iff_unique_mem, refine ⟨_, h_uniq⟩, rw ← h_uniq hne.some hne.some_spec, apply hne.some_spec, }, end /-! ### 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 α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := by finish [ext_iff, iff_def, subset_def] theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := assume x, and.left theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) : ∀ x ∈ s, ¬ p x := by finish [ext_iff] @[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} := by { ext, simp } @[simp] lemma subset_singleton_iff {α : Type*} {s : set α} {x : α} : s ⊆ {x} ↔ ∀ y ∈ s, y = x := iff.rfl /-! ### 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ᶜ = ∅ := by finish [ext_iff] @[simp] theorem compl_inter_self (s : set α) : sᶜ ∩ s = ∅ := by finish [ext_iff] @[simp] theorem compl_empty : (∅ : set α)ᶜ = univ := by finish [ext_iff] @[simp] theorem compl_union (s t : set α) : (s ∪ t)ᶜ = sᶜ ∩ tᶜ := by finish [ext_iff] local attribute [simp] -- Will be generalized to lattices in `compl_compl'` theorem compl_compl (s : set α) : sᶜᶜ = s := by finish [ext_iff] -- ditto theorem compl_inter (s t : set α) : (s ∩ t)ᶜ = sᶜ ∪ tᶜ := by finish [ext_iff] @[simp] theorem compl_univ : (univ : set α)ᶜ = ∅ := by finish [ext_iff] lemma compl_empty_iff {s : set α} : sᶜ = ∅ ↔ s = univ := by { split, intro h, rw [←compl_compl s, h, compl_empty], intro h, rw [h, compl_univ] } lemma compl_univ_iff {s : set α} : sᶜ = univ ↔ s = ∅ := by rw [←compl_empty_iff, compl_compl] 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_iff_not_of_iff mem_singleton_iff lemma compl_singleton_eq (a : α) : ({a} : set α)ᶜ = {x | x ≠ a} := ext $ λ x, mem_compl_singleton_iff theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = (sᶜ ∩ tᶜ)ᶜ := by simp [compl_inter, compl_compl] theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = (sᶜ ∪ tᶜ)ᶜ := by simp [compl_compl] @[simp] theorem union_compl_self (s : set α) : s ∪ sᶜ = univ := by finish [ext_iff] @[simp] theorem compl_union_self (s : set α) : sᶜ ∪ s = univ := by finish [ext_iff] theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext compl_compl theorem compl_subset_comm {s t : set α} : sᶜ ⊆ t ↔ tᶜ ⊆ s := by haveI := classical.prop_decidable; exact forall_congr (λ a, not_imp_comm) lemma compl_subset_compl {s t : set α} : sᶜ ⊆ tᶜ ↔ t ⊆ s := by rw [compl_subset_comm, compl_compl] theorem compl_subset_iff_union {s t : set α} : sᶜ ⊆ t ↔ s ∪ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a, by haveI := classical.prop_decidable; exact 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 := by { rw subset_compl_comm, simp } theorem inter_subset (a b c : set α) : a ∩ b ⊆ c ↔ a ⊆ bᶜ ∪ c := begin classical, split, { intros h x xa, by_cases h' : x ∈ b, simp [h ⟨xa, h'⟩], simp [h'] }, intros h x, rintro ⟨xa, xb⟩, cases h xa, contradiction, assumption end /-! ### 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) := ⟨λ ⟨x, xs, xt⟩, not_subset.2 ⟨x, xs, xt⟩, λ h, let ⟨x, xs, xt⟩ := not_subset.1 h in ⟨x, xs, xt⟩⟩ theorem union_diff_cancel' {s t u : set α} (h₁ : s ⊆ t) (h₂ : t ⊆ u) : t ∪ (u \ s) = u := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := union_diff_cancel' (subset.refl s) h theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := by finish [ext_iff, iff_def, subset_def] @[simp] theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s := by finish [ext_iff, iff_def] @[simp] theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t := by finish [ext_iff, iff_def] theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := inter_distrib_right _ _ _ theorem inter_union_distrib_left {s t u : set α} : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := set.ext $ λ _, and_or_distrib_left theorem inter_union_distrib_right {s t u : set α} : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := set.ext $ λ _, and_or_distrib_right theorem union_inter_distrib_left {s t u : set α} : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := set.ext $ λ _, or_and_distrib_left theorem union_inter_distrib_right {s t u : set α} : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := set.ext $ λ _, or_and_distrib_right theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) := inter_assoc _ _ _ @[simp] theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ := by finish [ext_iff] @[simp] theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s := by finish [ext_iff, iff_def] @[simp] theorem inter_union_compl (s t : set α) : (s ∩ t) ∪ (s ∩ tᶜ) = s := inter_union_diff _ _ theorem diff_subset (s t : set α) : s \ t ⊆ s := by finish [subset_def] theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := by finish [subset_def] theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := diff_subset_diff h (by refl) theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := diff_subset_diff (subset.refl s) h theorem compl_eq_univ_diff (s : set α) : sᶜ = univ \ s := by finish [ext_iff] @[simp] lemma empty_diff (s : set α) : (∅ \ s : set α) = ∅ := eq_empty_of_subset_empty $ assume x ⟨hx, _⟩, hx theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t := ⟨assume h x hx, classical.by_contradiction $ assume : x ∉ t, show x ∈ (∅ : set α), from h ▸ ⟨hx, this⟩, assume h, eq_empty_of_subset_empty $ assume x ⟨hx, hnx⟩, hnx $ h hx⟩ @[simp] theorem diff_empty {s : set α} : s \ ∅ = s := ext $ assume x, ⟨assume ⟨hx, _⟩, hx, assume h, ⟨h, not_false⟩⟩ theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) := ext $ by simp [not_or_distrib, and.comm, and.left_comm] -- the following statement contains parentheses to help the reader lemma diff_diff_comm {s t u : set α} : (s \ t) \ u = (s \ u) \ t := by simp_rw [diff_diff, union_comm] lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := ⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)), assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩ lemma subset_diff_union (s t : set α) : s ⊆ (s \ t) ∪ t := by rw [union_comm, ←diff_subset_iff] @[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 := by rw [diff_subset_iff, diff_subset_iff, union_comm] lemma diff_inter {s t u : set α} : s \ (t ∩ u) = (s \ t) ∪ (s \ u) := ext $ λ x, by simp [not_and_distrib, and_or_distrib_left] lemma diff_inter_diff {s t u : set α} : s \ t ∩ (s \ u) = s \ (t ∪ u) := by { ext x, simp only [mem_inter_eq, mem_union_eq, mem_diff, not_or_distrib, and.left_comm, and.assoc, and_self_left] } lemma diff_compl : s \ tᶜ = s ∩ t := by rw [diff_eq, compl_compl] lemma diff_diff_right {s t u : set α} : s \ (t \ u) = (s \ t) ∪ (s ∩ u) := by rw [diff_eq t u, diff_inter, diff_compl] @[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)] } theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t := by finish [ext_iff, iff_def] theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_diff_self, union_comm] theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ := by { ext, by simp [iff_def] {contextual:=tt} } theorem diff_inter_self_eq_diff {s t : set α} : s \ (t ∩ s) = s \ t := by { ext, simp [iff_def] {contextual := tt} } theorem diff_self_inter {s t : set α} : s \ (s ∩ t) = s \ t := by rw [inter_comm, diff_inter_self_eq_diff] theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := by finish [ext_iff, iff_def, subset_def] @[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 = ∅ := by { ext, simp } lemma diff_diff_cancel_left {s t : set α} (h : s ⊆ t) : t \ (t \ s) = s := by simp only [diff_diff_right, diff_self, inter_eq_self_of_subset_right h, empty_union] 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 /-! ### 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 /-! ### 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_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 theorem preimage_const_of_mem {b : β} {s : set β} (h : b ∈ s) : (λ (x : α), b) ⁻¹' s = univ := eq_univ_of_forall $ λ x, h 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 mem_image_of_injective {f : α → β} {a : α} {s : set α} (hf : injective f) : f a ∈ f '' s ↔ a ∈ s := iff.intro (assume ⟨b, hb, eq⟩, (hf eq) ▸ hb) (assume h, mem_image_of_mem _ h) 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 := by finish [ext_iff, iff_def, mem_image_eq] @[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⟩ 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⟩ lemma subsingleton_empty : (∅ : set α).subsingleton := λ x, false.elim 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.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_univ [subsingleton α] : (univ : set α).subsingleton := λ x hx y hy, subsingleton.elim x y /-- `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 /-- `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 : subsingleton (f '' s)) : subsingleton s := subsingleton.intro $ λ ⟨a, ha⟩ ⟨b, hb⟩, subtype.ext $ hf (by {simpa using @subsingleton.elim _ hs ⟨f a, ⟨a, ha, rfl⟩⟩ ⟨f b, ⟨b, hb, rfl⟩⟩}) 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 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 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 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 {ι : Type*} {f : ι → β} : f '' univ = range f := by { ext, simp [image, range] } theorem image_subset_range {ι : Type*} (f : ι → β) (s : set ι) : f '' s ⊆ range f := by rw ← image_univ; exact image_subset _ (subset_univ _) 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 {s : set α} : 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 {f : ι → α} : range f = ∅ ↔ ¬ nonempty ι := not_nonempty_iff_eq_empty.symm.trans $ not_congr range_nonempty_iff_nonempty 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] 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 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'⟩ 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 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 : ∀ a b, r a b → r' a b) (hp : pairwise_on s r) : pairwise_on s r' := λ x xs y ys h, H _ _ (hp x xs y ys h) /-- If and only if `f` takes pairwise equal values on `s`, there is some value it takes everywhere on `s`. -/ 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 := begin split, { intro h, rcases eq_empty_or_nonempty s with rfl | ⟨x, hx⟩, { exact ⟨classical.arbitrary β, λ x hx, false.elim hx⟩ }, { use f x, intros y hy, by_cases hyx : y = x, { rw hyx }, { exact h y hy x hx hyx } } }, { rintros ⟨z, hz⟩ x hx y hy hne, rw [hz x hx, hz y hy] } end 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 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 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] 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⟩⟩ 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 : δ → β} : (preimage f s).prod (preimage g t) = preimage (λp, (f p.1, g p.2)) (s.prod 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] } 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₂ : β → δ} : (image m₁ s).prod (image 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] 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] 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⟩ /-- 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 `i` and a family of sets `s : Π i, set (α i)`, `pi i s` is the set of dependent functions `f : Πa, π a` such that `f a` belongs to `π a` whenever `a ∈ i`. -/ 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_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 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 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⟩ 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 lemma eq_of_inclusion_surjective {s t : set α} {h : s ⊆ t} (h_surj : function.surjective (inclusion h)) : s = t := begin apply set.subset.antisymm h, intros x hx, cases h_surj ⟨x, hx⟩ with y key, rw [←subtype.coe_mk x hx, ←key, coe_inclusion], exact subtype.mem y, end lemma range_inclusion {s t : set α} (h : s ⊆ t) : range (inclusion h) = {x : t | (x:α) ∈ s} := by { ext ⟨x, hx⟩, simp [inclusion] } 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 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⟩; refine ⟨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 end subsingleton
bbfeb991e43006e2cc7120d6e82ad7c50ae46869
1a61aba1b67cddccce19532a9596efe44be4285f
/library/data/pnat.lean
4723c85b95df5b5eb97e30d78e5426f5918e8a59
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
10,343
lean
/- Copyright (c) 2015 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis Basic facts about the positive natural numbers. Developed primarily for use in the construction of ℝ. For the most part, the only theorems here are those needed for that construction. -/ import data.rat.order data.nat open nat rat subtype eq.ops namespace pnat definition pnat := { n : ℕ | n > 0 } notation `ℕ+` := pnat definition pos (n : ℕ) (H : n > 0) : ℕ+ := tag n H definition nat_of_pnat (p : ℕ+) : ℕ := elt_of p reserve postfix `~`:std.prec.max_plus local postfix ~ := nat_of_pnat theorem pnat_pos (p : ℕ+) : p~ > 0 := has_property p definition add (p q : ℕ+) : ℕ+ := tag (p~ + q~) (nat.add_pos (pnat_pos p) (pnat_pos q)) infix `+` := add definition mul (p q : ℕ+) : ℕ+ := tag (p~ * q~) (nat.mul_pos (pnat_pos p) (pnat_pos q)) infix `*` := mul definition le (p q : ℕ+) := p~ ≤ q~ infix `≤` := le notation p `≥` q := q ≤ p definition lt (p q : ℕ+) := p~ < q~ infix `<` := lt protected theorem pnat.eq {p q : ℕ+} : p~ = q~ → p = q := subtype.eq definition pnat_le_decidable [instance] (p q : ℕ+) : decidable (p ≤ q) := nat.decidable_le p~ q~ definition pnat_lt_decidable [instance] {p q : ℕ+} : decidable (p < q) := nat.decidable_lt p~ q~ theorem le.trans {p q r : ℕ+} (H1 : p ≤ q) (H2 : q ≤ r) : p ≤ r := nat.le.trans H1 H2 definition max (p q : ℕ+) := tag (nat.max p~ q~) (nat.lt_of_lt_of_le (!pnat_pos) (!le_max_right)) theorem max_right (a b : ℕ+) : max a b ≥ b := !le_max_right theorem max_left (a b : ℕ+) : max a b ≥ a := !le_max_left theorem max_eq_right {a b : ℕ+} (H : a < b) : max a b = b := pnat.eq (nat.max_eq_right_of_lt H) theorem max_eq_left {a b : ℕ+} (H : ¬ a < b) : max a b = a := pnat.eq (nat.max_eq_left (le_of_not_gt H)) theorem le_of_lt {a b : ℕ+} : a < b → a ≤ b := nat.le_of_lt theorem not_lt_of_ge {a b : ℕ+} : a ≤ b → ¬ (b < a) := nat.not_lt_of_ge theorem le_of_not_gt {a b : ℕ+} : ¬ a < b → b ≤ a := nat.le_of_not_gt theorem eq_of_le_of_ge {a b : ℕ+} (H1 : a ≤ b) (H2 : b ≤ a) : a = b := pnat.eq (nat.eq_of_le_of_ge H1 H2) theorem le.refl (a : ℕ+) : a ≤ a := !nat.le.refl notation 2 := (tag 2 dec_trivial : ℕ+) notation 3 := (tag 3 dec_trivial : ℕ+) definition pone : ℕ+ := tag 1 dec_trivial definition rat_of_pnat [reducible] (n : ℕ+) : ℚ := n~ theorem pnat.to_rat_of_nat (n : ℕ+) : rat_of_pnat n = of_nat n~ := rfl -- these will come in rat theorem rat_of_nat_nonneg (n : ℕ) : 0 ≤ of_nat n := trivial theorem rat_of_pnat_ge_one (n : ℕ+) : rat_of_pnat n ≥ 1 := (iff.mpr !of_nat_le_of_nat) (pnat_pos n) theorem rat_of_pnat_is_pos (n : ℕ+) : rat_of_pnat n > 0 := (iff.mpr !of_nat_pos) (pnat_pos n) theorem of_nat_le_of_nat_of_le {m n : ℕ} (H : m ≤ n) : of_nat m ≤ of_nat n := (iff.mpr !of_nat_le_of_nat) H theorem of_nat_lt_of_nat_of_lt {m n : ℕ} (H : m < n) : of_nat m < of_nat n := (iff.mpr !of_nat_lt_of_nat) H theorem rat_of_pnat_le_of_pnat_le {m n : ℕ+} (H : m ≤ n) : rat_of_pnat m ≤ rat_of_pnat n := of_nat_le_of_nat_of_le H theorem rat_of_pnat_lt_of_pnat_lt {m n : ℕ+} (H : m < n) : rat_of_pnat m < rat_of_pnat n := of_nat_lt_of_nat_of_lt H theorem pnat_le_of_rat_of_pnat_le {m n : ℕ+} (H : rat_of_pnat m ≤ rat_of_pnat n) : m ≤ n := (iff.mp !of_nat_le_of_nat) H definition inv (n : ℕ+) : ℚ := (1 : ℚ) / rat_of_pnat n postfix `⁻¹` := inv theorem inv_pos (n : ℕ+) : n⁻¹ > 0 := one_div_pos_of_pos !rat_of_pnat_is_pos theorem inv_le_one (n : ℕ+) : n⁻¹ ≤ (1 : ℚ) := begin rewrite [↑inv, -one_div_one], apply div_le_div_of_le, apply rat.zero_lt_one, apply rat_of_pnat_ge_one end theorem inv_lt_one_of_gt {n : ℕ+} (H : n~ > 1) : n⁻¹ < (1 : ℚ) := begin rewrite [↑inv, -one_div_one], apply div_lt_div_of_lt, apply rat.zero_lt_one, rewrite pnat.to_rat_of_nat, apply (of_nat_lt_of_nat_of_lt H) end theorem pone_inv : pone⁻¹ = 1 := rfl theorem add_invs_nonneg (m n : ℕ+) : 0 ≤ m⁻¹ + n⁻¹ := begin apply rat.le_of_lt, apply rat.add_pos, repeat apply inv_pos end theorem one_mul (n : ℕ+) : pone * n = n := begin apply pnat.eq, rewrite [↑pone, ↑mul, ↑nat_of_pnat, one_mul] end theorem pone_le (n : ℕ+) : pone ≤ n := succ_le_of_lt (pnat_pos n) theorem pnat_to_rat_mul (a b : ℕ+) : rat_of_pnat (a * b) = rat_of_pnat a * rat_of_pnat b := rfl theorem mul_lt_mul_left (a b c : ℕ+) (H : a < b) : a * c < b * c := nat.mul_lt_mul_of_pos_right H !pnat_pos theorem one_lt_two : pone < 2 := !nat.le.refl theorem inv_two_mul_lt_inv (n : ℕ+) : (2 * n)⁻¹ < n⁻¹ := begin rewrite ↑inv, apply div_lt_div_of_lt, apply rat_of_pnat_is_pos, have H : n~ < (2 * n)~, begin rewrite -one_mul at {1}, apply mul_lt_mul_left, apply one_lt_two end, apply of_nat_lt_of_nat_of_lt, apply H end theorem inv_two_mul_le_inv (n : ℕ+) : (2 * n)⁻¹ ≤ n⁻¹ := rat.le_of_lt !inv_two_mul_lt_inv theorem inv_ge_of_le {p q : ℕ+} (H : p ≤ q) : q⁻¹ ≤ p⁻¹ := div_le_div_of_le !rat_of_pnat_is_pos (rat_of_pnat_le_of_pnat_le H) theorem inv_gt_of_lt {p q : ℕ+} (H : p < q) : q⁻¹ < p⁻¹ := div_lt_div_of_lt !rat_of_pnat_is_pos (rat_of_pnat_lt_of_pnat_lt H) theorem ge_of_inv_le {p q : ℕ+} (H : p⁻¹ ≤ q⁻¹) : q ≤ p := pnat_le_of_rat_of_pnat_le (le_of_one_div_le_one_div !rat_of_pnat_is_pos H) theorem two_mul (p : ℕ+) : rat_of_pnat (2 * p) = (1 + 1) * rat_of_pnat p := by rewrite pnat_to_rat_mul theorem add_halves (p : ℕ+) : (2 * p)⁻¹ + (2 * p)⁻¹ = p⁻¹ := begin rewrite [↑inv, -(@add_halves (1 / (rat_of_pnat p))), rat.div_div_eq_div_mul], have H : rat_of_pnat (2 * p) = rat_of_pnat p * (1 + 1), by rewrite [rat.mul.comm, two_mul], rewrite *H end theorem add_halves_double (m n : ℕ+) : m⁻¹ + n⁻¹ = ((2 * m)⁻¹ + (2 * n)⁻¹) + ((2 * m)⁻¹ + (2 * n)⁻¹) := have hsimp [visible] : ∀ a b : ℚ, (a + a) + (b + b) = (a + b) + (a + b), by intros; rewrite [rat.add.assoc, -(rat.add.assoc a b b), {_+b}rat.add.comm, -*rat.add.assoc], by rewrite [-add_halves m, -add_halves n, hsimp] theorem inv_mul_eq_mul_inv {p q : ℕ+} : (p * q)⁻¹ = p⁻¹ * q⁻¹ := by rewrite [↑inv, pnat_to_rat_mul, one_div_mul_one_div] theorem inv_mul_le_inv (p q : ℕ+) : (p * q)⁻¹ ≤ q⁻¹ := begin rewrite [inv_mul_eq_mul_inv, -{q⁻¹}rat.one_mul at {2}], apply rat.mul_le_mul, apply inv_le_one, apply rat.le.refl, apply rat.le_of_lt, apply inv_pos, apply rat.le_of_lt rat.zero_lt_one end theorem pnat_mul_le_mul_left' (a b c : ℕ+) (H : a ≤ b) : c * a ≤ c * b := nat.mul_le_mul_of_nonneg_left H (nat.le_of_lt !pnat_pos) theorem mul.assoc (a b c : ℕ+) : a * b * c = a * (b * c) := pnat.eq !nat.mul.assoc theorem mul.comm (a b : ℕ+) : a * b = b * a := pnat.eq !nat.mul.comm theorem add.assoc (a b c : ℕ+) : a + b + c = a + (b + c) := pnat.eq !nat.add.assoc theorem mul_le_mul_left (p q : ℕ+) : q ≤ p * q := begin rewrite [-one_mul at {1}, mul.comm, mul.comm p], apply pnat_mul_le_mul_left', apply pone_le end theorem mul_le_mul_right (p q : ℕ+) : p ≤ p * q := by rewrite mul.comm; apply mul_le_mul_left theorem pnat.lt_of_not_le {p q : ℕ+} (H : ¬ p ≤ q) : q < p := nat.lt_of_not_ge H theorem inv_cancel_left (p : ℕ+) : rat_of_pnat p * p⁻¹ = (1 : ℚ) := mul_one_div_cancel (ne.symm (rat.ne_of_lt !rat_of_pnat_is_pos)) theorem inv_cancel_right (p : ℕ+) : p⁻¹ * rat_of_pnat p = (1 : ℚ) := by rewrite rat.mul.comm; apply inv_cancel_left theorem lt_add_left (p q : ℕ+) : p < p + q := begin have H : p~ < p~ + q~, begin rewrite -nat.add_zero at {1}, apply nat.add_lt_add_left, apply pnat_pos end, apply H end theorem inv_add_lt_left (p q : ℕ+) : (p + q)⁻¹ < p⁻¹ := by apply inv_gt_of_lt; apply lt_add_left theorem div_le_pnat (q : ℚ) (n : ℕ+) (H : q ≥ n⁻¹) : 1 / q ≤ rat_of_pnat n := begin apply rat.div_le_of_le_mul, apply rat.lt_of_lt_of_le, apply inv_pos, rotate 1, apply H, apply rat.le_mul_of_div_le, apply rat_of_pnat_is_pos, apply H end theorem pnat_cancel' (n m : ℕ+) : (n * n * m)⁻¹ * (rat_of_pnat n * rat_of_pnat n) = m⁻¹ := assert hsimp : ∀ a b c : ℚ, (a * a * (b * b * c)) = (a * b) * (a * b) * c, from λa b c, by rewrite[-*rat.mul.assoc]; exact (!rat.mul.right_comm ▸ rfl), by rewrite [rat.mul.comm, *inv_mul_eq_mul_inv, hsimp, *inv_cancel_left, *rat.one_mul] definition pceil (a : ℚ) : ℕ+ := tag (ubound a) !ubound_pos theorem pceil_helper {a : ℚ} {n : ℕ+} (H : pceil a ≤ n) (Ha : a > 0) : n⁻¹ ≤ 1 / a := rat.le.trans (inv_ge_of_le H) (div_le_div_of_le Ha (ubound_ge a)) theorem inv_pceil_div (a b : ℚ) (Ha : a > 0) (Hb : b > 0) : (pceil (a / b))⁻¹ ≤ b / a := !one_div_one_div ▸ div_le_div_of_le (one_div_pos_of_pos (div_pos_of_pos_of_pos Hb Ha)) (!div_div_eq_mul_div⁻¹ ▸ !rat.one_mul⁻¹ ▸ !ubound_ge) theorem sep_by_inv {a b : ℚ} (H : a > b) : ∃ N : ℕ+, a > (b + N⁻¹ + N⁻¹) := begin apply exists.elim (exists_add_lt_and_pos_of_lt H), intro c Hc, existsi (pceil ((1 + 1 + 1) / c)), apply rat.lt.trans, rotate 1, apply and.left Hc, rewrite rat.add.assoc, apply rat.add_lt_add_left, rewrite -(@rat.add_halves c) at {3}, apply rat.add_lt_add, repeat (apply rat.lt_of_le_of_lt; apply inv_pceil_div; apply dec_trivial; apply and.right Hc; apply div_lt_div_of_pos_of_lt_of_pos; repeat (apply two_pos); apply and.right Hc) end theorem nonneg_of_ge_neg_invs (a : ℚ) (H : ∀ n : ℕ+, -n⁻¹ ≤ a) : 0 ≤ a := rat.le_of_not_gt (suppose a < 0, have H2 : 0 < -a, from neg_pos_of_neg this, (rat.not_lt_of_ge !H) (iff.mp !lt_neg_iff_lt_neg (calc (pceil (of_num 2 / -a))⁻¹ ≤ -a / of_num 2 : !inv_pceil_div dec_trivial H2 ... < -a / 1 : div_lt_div_of_pos_of_lt_of_pos dec_trivial dec_trivial H2 ... = -a : !div_one))) end pnat
a3e71a6476cf184951e36bb415a353b761849041
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/tests/lean/ppMotives.lean
0a42aa62b76611a205be342fe3e90aa5e2ff44b8
[ "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
472
lean
set_option pp.motives.pi false #print Nat.add set_option pp.motives.pi true #print Nat.add theorem ex : ∀ {α β : Sort u} (h : α = β) (a : α), cast h a ≅ a | α, _, rfl, a => HEq.refl a set_option pp.motives.nonConst false #print ex set_option pp.motives.nonConst true #print ex noncomputable def fact (n : Nat) : Nat := Nat.recOn n 1 (fun n acc => (n+1)*acc) set_option pp.motives.all false #print fact set_option pp.motives.all true #print fact
cea2fd5538678d8cf4ab0f43ea23bd41046bb873
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/12_Axioms.org.27.lean
194088c3983e550e13d41b1261c59ae9688baf33
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
290
lean
import standard open classical namespace hide -- BEGIN theorem prop_complete (a : Prop) : a = true ∨ a = false := or.elim (em a) (λ t, or.inl (propext (iff.intro (λ h, trivial) (λ h, t)))) (λ f, or.inr (propext (iff.intro (λ h, absurd h f) (λ h, false.elim h)))) -- END end hide
ca7ceb6515b2dd6ef0e2745f021405fbbe68d46f
649957717d58c43b5d8d200da34bf374293fe739
/src/data/option/basic.lean
fcd3b6f58bf9eee10f9a61029da96336c81b720c
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
5,616
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import logic.basic data.bool data.option.defs tactic.basic namespace option variables {α : Type*} {β : Type*} {γ : Type*} @[simp] theorem get_mem : ∀ {o : option α} (h : is_some o), option.get h ∈ o | (some a) _ := rfl theorem get_of_mem {a : α} : ∀ {o : option α} (h : is_some o), a ∈ o → option.get h = a | _ _ rfl := rfl @[simp] lemma not_mem_none (a : α) : a ∉ (none : option α) := λ h, option.no_confusion h @[simp] lemma some_get : ∀ {x : option α} (h : is_some x), some (option.get h) = x | (some x) hx := rfl @[simp] lemma get_some (x : α) (h : is_some (some x)) : option.get h = x := rfl theorem mem_unique {o : option α} {a b : α} (ha : a ∈ o) (hb : b ∈ o) : a = b := option.some.inj $ ha.symm.trans hb theorem injective_some (α : Type*) : function.injective (@some α) := λ _ _, some_inj.mp /-- `option.map f` is injective if `f` is injective. -/ theorem injective_map {f : α → β} (Hf : function.injective f) : function.injective (option.map f) | none none H := rfl | (some a₁) (some a₂) H := by rw Hf (option.some.inj H) @[extensionality] theorem ext : ∀ {o₁ o₂ : option α}, (∀ a, a ∈ o₁ ↔ a ∈ o₂) → o₁ = o₂ | none none H := rfl | (some a) o H := ((H _).1 rfl).symm | o (some b) H := (H _).2 rfl theorem eq_none_iff_forall_not_mem {o : option α} : o = none ↔ (∀ a, a ∉ o) := ⟨λ e a h, by rw e at h; cases h, λ h, ext $ by simpa⟩ @[simp] theorem none_bind {α β} (f : α → option β) : none >>= f = none := rfl @[simp] theorem some_bind {α β} (a : α) (f : α → option β) : some a >>= f = f a := rfl @[simp] theorem none_bind' (f : α → option β) : none.bind f = none := rfl @[simp] theorem some_bind' (a : α) (f : α → option β) : (some a).bind f = f a := rfl @[simp] theorem bind_some : ∀ x : option α, x >>= some = x := @bind_pure α option _ _ @[simp] theorem bind_eq_some {α β} {x : option α} {f : α → option β} {b : β} : x >>= f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp @[simp] theorem bind_eq_some' {x : option α} {f : α → option β} {b : β} : x.bind f = some b ↔ ∃ a, x = some a ∧ f a = some b := by cases x; simp lemma bind_comm {α β γ} {f : α → β → option γ} (a : option α) (b : option β) : a.bind (λx, b.bind (f x)) = b.bind (λy, a.bind (λx, f x y)) := by cases a; cases b; refl lemma bind_assoc (x : option α) (f : α → option β) (g : β → option γ) : (x.bind f).bind g = x.bind (λ y, (f y).bind g) := by cases x; refl @[simp] theorem map_none {α β} {f : α → β} : f <$> none = none := rfl @[simp] theorem map_some {α β} {a : α} {f : α → β} : f <$> some a = some (f a) := rfl @[simp] theorem map_none' {f : α → β} : option.map f none = none := rfl @[simp] theorem map_some' {a : α} {f : α → β} : option.map f (some a) = some (f a) := rfl @[simp] theorem map_eq_some {α β} {x : option α} {f : α → β} {b : β} : f <$> x = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_eq_some' {x : option α} {f : α → β} {b : β} : x.map f = some b ↔ ∃ a, x = some a ∧ f a = b := by cases x; simp @[simp] theorem map_id' : option.map (@id α) = id := map_id @[simp] theorem seq_some {α β} {a : α} {f : α → β} : some f <*> some a = some (f a) := rfl @[simp] theorem some_orelse' (a : α) (x : option α) : (some a).orelse x = some a := rfl @[simp] theorem some_orelse (a : α) (x : option α) : (some a <|> x) = some a := rfl @[simp] theorem none_orelse' (x : option α) : none.orelse x = x := by cases x; refl @[simp] theorem none_orelse (x : option α) : (none <|> x) = x := none_orelse' x @[simp] theorem orelse_none' (x : option α) : x.orelse none = x := by cases x; refl @[simp] theorem orelse_none (x : option α) : (x <|> none) = x := orelse_none' x @[simp] theorem is_some_none : @is_some α none = ff := rfl @[simp] theorem is_some_some {a : α} : is_some (some a) = tt := rfl theorem is_some_iff_exists {x : option α} : is_some x ↔ ∃ a, x = some a := by cases x; simp [is_some]; exact ⟨_, rfl⟩ @[simp] theorem is_none_none : @is_none α none = tt := rfl @[simp] theorem is_none_some {a : α} : is_none (some a) = ff := rfl @[simp] theorem not_is_some {a : option α} : is_some a = ff ↔ a.is_none = tt := by cases a; simp lemma not_is_some_iff_eq_none {o : option α} : ¬o.is_some ↔ o = none := by cases o; simp lemma ne_none_iff_is_some {o : option α} : o ≠ none ↔ o.is_some := by cases o; simp theorem iget_mem [inhabited α] : ∀ {o : option α}, is_some o → o.iget ∈ o | (some a) _ := rfl theorem iget_of_mem [inhabited α] {a : α} : ∀ {o : option α}, a ∈ o → o.iget = a | _ rfl := rfl @[simp] theorem guard_eq_some {p : α → Prop} [decidable_pred p] {a b : α} : guard p a = some b ↔ a = b ∧ p a := by by_cases p a; simp [option.guard, h]; intro; contradiction @[simp] theorem guard_eq_some' {p : Prop} [decidable p] : ∀ u, _root_.guard p = some u ↔ p | () := by by_cases p; simp [guard, h, pure]; intro; contradiction theorem lift_or_get_choice {f : α → α → α} (h : ∀ a b, f a b = a ∨ f a b = b) : ∀ o₁ o₂, lift_or_get f o₁ o₂ = o₁ ∨ lift_or_get f o₁ o₂ = o₂ | none none := or.inl rfl | (some a) none := or.inl rfl | none (some b) := or.inr rfl | (some a) (some b) := by simpa [lift_or_get] using h a b end option
7280a77af67fee41644d2087f5ca3011d84b84c4
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/hott/init/trunc.hlean
0cb548ae5cff5b0e188aa99ff1e7c7fa1ffba5fb
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,529
hlean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: init.trunc Authors: Jeremy Avigad, Floris van Doorn Ported from Coq HoTT. TODO: can we replace some definitions with a hprop as codomain by theorems? -/ prelude import .logic .equiv .types.empty .types.sigma open eq nat sigma unit namespace is_trunc /- Truncation levels -/ inductive trunc_index : Type₁ := | minus_two : trunc_index | succ : trunc_index → trunc_index /- notation for trunc_index is -2, -1, 0, 1, ... from 0 and up this comes from a coercion from num to trunc_index (via nat) -/ postfix `.+1`:(max+1) := trunc_index.succ postfix `.+2`:(max+1) := λn, (n .+1 .+1) notation `-2` := trunc_index.minus_two notation `-1` := -2.+1 -- ISSUE: -1 gets printed as -2.+1 export [coercions] nat namespace trunc_index definition add (n m : trunc_index) : trunc_index := trunc_index.rec_on m n (λ k l, l .+1) definition leq (n m : trunc_index) : Type₁ := trunc_index.rec_on n (λm, unit) (λ n p m, trunc_index.rec_on m (λ p, empty) (λ m q p, p m) p) m end trunc_index infix `+2+`:65 := trunc_index.add notation x <= y := trunc_index.leq x y notation x ≤ y := trunc_index.leq x y namespace trunc_index definition succ_le_succ {n m : trunc_index} (H : n ≤ m) : n.+1 ≤ m.+1 := H definition le_of_succ_le_succ {n m : trunc_index} (H : n.+1 ≤ m.+1) : n ≤ m := H definition minus_two_le (n : trunc_index) : -2 ≤ n := star definition empty_of_succ_le_minus_two {n : trunc_index} (H : n .+1 ≤ -2) : empty := H end trunc_index definition trunc_index.of_nat [coercion] [reducible] (n : nat) : trunc_index := nat.rec_on n (-1.+1) (λ n k, k.+1) /- truncated types -/ /- Just as in Coq HoTT we define an internal version of contractibility and is_trunc, but we only use `is_trunc` and `is_contr` -/ structure contr_internal (A : Type) := (center : A) (contr : Π(a : A), center = a) definition is_trunc_internal (n : trunc_index) : Type → Type := trunc_index.rec_on n (λA, contr_internal A) (λn trunc_n A, (Π(x y : A), trunc_n (x = y))) end is_trunc open is_trunc structure is_trunc [class] (n : trunc_index) (A : Type) := (to_internal : is_trunc_internal n A) open nat num is_trunc.trunc_index namespace is_trunc abbreviation is_contr := is_trunc -2 abbreviation is_hprop := is_trunc -1 abbreviation is_hset := is_trunc 0 variables {A B : Type} definition is_trunc_succ_intro (A : Type) (n : trunc_index) [H : ∀x y : A, is_trunc n (x = y)] : is_trunc n.+1 A := is_trunc.mk (λ x y, !is_trunc.to_internal) definition is_trunc_eq (n : trunc_index) [H : is_trunc (n.+1) A] (x y : A) : is_trunc n (x = y) := is_trunc.mk (!is_trunc.to_internal x y) /- contractibility -/ definition is_contr.mk (center : A) (contr : Π(a : A), center = a) : is_contr A := is_trunc.mk (contr_internal.mk center contr) definition center (A : Type) [H : is_contr A] : A := @contr_internal.center A !is_trunc.to_internal definition contr [H : is_contr A] (a : A) : !center = a := @contr_internal.contr A !is_trunc.to_internal a --TODO: rename definition center_eq [H : is_contr A] (x y : A) : x = y := (contr x)⁻¹ ⬝ (contr y) definition hprop_eq_of_is_contr {A : Type} [H : is_contr A] {x y : A} (p q : x = y) : p = q := have K : ∀ (r : x = y), center_eq x y = r, from (λ r, eq.rec_on r !con.left_inv), (K p)⁻¹ ⬝ K q definition is_contr_eq {A : Type} [H : is_contr A] (x y : A) : is_contr (x = y) := is_contr.mk !center_eq (λ p, !hprop_eq_of_is_contr) local attribute is_contr_eq [instance] /- truncation is upward close -/ -- n-types are also (n+1)-types definition is_trunc_succ [instance] [priority 100] (A : Type) (n : trunc_index) [H : is_trunc n A] : is_trunc (n.+1) A := trunc_index.rec_on n (λ A (H : is_contr A), !is_trunc_succ_intro) (λ n IH A (H : is_trunc (n.+1) A), @is_trunc_succ_intro _ _ (λ x y, IH _ !is_trunc_eq)) A H --in the proof the type of H is given explicitly to make it available for class inference definition is_trunc_of_leq (A : Type) (n m : trunc_index) (Hnm : n ≤ m) [Hn : is_trunc n A] : is_trunc m A := have base : ∀k A, k ≤ -2 → is_trunc k A → (is_trunc -2 A), from λ k A, trunc_index.cases_on k (λh1 h2, h2) (λk h1 h2, empty.elim (is_trunc -2 A) (trunc_index.empty_of_succ_le_minus_two h1)), have step : Π (m : trunc_index) (IHm : Π (n : trunc_index) (A : Type), n ≤ m → is_trunc n A → is_trunc m A) (n : trunc_index) (A : Type) (Hnm : n ≤ m .+1) (Hn : is_trunc n A), is_trunc m .+1 A, from λm IHm n, trunc_index.rec_on n (λA Hnm Hn, @is_trunc_succ A m (IHm -2 A star Hn)) (λn IHn A Hnm (Hn : is_trunc n.+1 A), @is_trunc_succ_intro A m (λx y, IHm n (x = y) (trunc_index.le_of_succ_le_succ Hnm) !is_trunc_eq)), trunc_index.rec_on m base step n A Hnm Hn -- the following cannot be instances in their current form, because they are looping definition is_trunc_of_is_contr (A : Type) (n : trunc_index) [H : is_contr A] : is_trunc n A := trunc_index.rec_on n H _ definition is_trunc_succ_of_is_hprop (A : Type) (n : trunc_index) [H : is_hprop A] : is_trunc (n.+1) A := is_trunc_of_leq A -1 (n.+1) star definition is_trunc_succ_succ_of_is_hset (A : Type) (n : trunc_index) [H : is_hset A] : is_trunc (n.+2) A := is_trunc_of_leq A nat.zero (n.+2) star /- hprops -/ definition is_hprop.elim [H : is_hprop A] (x y : A) : x = y := @center _ !is_trunc_eq definition is_contr_of_inhabited_hprop {A : Type} [H : is_hprop A] (x : A) : is_contr A := is_contr.mk x (λy, !is_hprop.elim) --Coq has the following as instance, but doesn't look too useful definition is_hprop_of_imp_is_contr {A : Type} (H : A → is_contr A) : is_hprop A := @is_trunc_succ_intro A -2 (λx y, have H2 [visible] : is_contr A, from H x, !is_contr_eq) definition is_hprop.mk {A : Type} (H : ∀x y : A, x = y) : is_hprop A := is_hprop_of_imp_is_contr (λ x, is_contr.mk x (H x)) /- hsets -/ definition is_hset.mk (A : Type) (H : ∀(x y : A) (p q : x = y), p = q) : is_hset A := @is_trunc_succ_intro _ _ (λ x y, is_hprop.mk (H x y)) definition is_hset.elim [H : is_hset A] ⦃x y : A⦄ (p q : x = y) : p = q := @is_hprop.elim _ !is_trunc_eq p q /- instances -/ definition is_contr_sigma_eq [instance] {A : Type} (a : A) : is_contr (Σ(x : A), a = x) := is_contr.mk (sigma.mk a idp) (λp, sigma.rec_on p (λ b q, eq.rec_on q idp)) definition is_contr_unit [instance] : is_contr unit := is_contr.mk star (λp, unit.rec_on p idp) definition is_hprop_empty [instance] : is_hprop empty := is_hprop.mk (λx, !empty.elim x) /- truncated universe -/ structure trunctype (n : trunc_index) := (carrier : Type) (struct : is_trunc n carrier) attribute trunctype.carrier [coercion] attribute trunctype.struct [instance] notation n `-Type` := trunctype n abbreviation hprop := -1-Type abbreviation hset := 0-Type protected definition hprop.mk := @trunctype.mk -1 protected definition hset.mk := @trunctype.mk (-1.+1) /- interaction with equivalences -/ section open is_equiv equiv --should we remove the following two theorems as they are special cases of --"is_trunc_is_equiv_closed" definition is_contr_is_equiv_closed (f : A → B) [Hf : is_equiv f] [HA: is_contr A] : (is_contr B) := is_contr.mk (f (center A)) (λp, eq_of_eq_inv !contr) theorem is_contr_equiv_closed (H : A ≃ B) [HA: is_contr A] : is_contr B := @is_contr_is_equiv_closed _ _ (to_fun H) (to_is_equiv H) _ definition equiv_of_is_contr_of_is_contr [HA : is_contr A] [HB : is_contr B] : A ≃ B := equiv.mk (λa, center B) (is_equiv.adjointify (λa, center B) (λb, center A) contr contr) definition is_trunc_is_equiv_closed (n : trunc_index) (f : A → B) [H : is_equiv f] [HA : is_trunc n A] : is_trunc n B := trunc_index.rec_on n (λA (HA : is_contr A) B f (H : is_equiv f), !is_contr_is_equiv_closed) (λn IH A (HA : is_trunc n.+1 A) B f (H : is_equiv f), @is_trunc_succ_intro _ _ (λ x y : B, IH (f⁻¹ x = f⁻¹ y) !is_trunc_eq (x = y) (ap f⁻¹)⁻¹ !is_equiv_inv)) A HA B f H definition is_trunc_equiv_closed (n : trunc_index) (f : A ≃ B) [HA : is_trunc n A] : is_trunc n B := is_trunc_is_equiv_closed n (to_fun f) definition is_equiv_of_is_hprop [HA : is_hprop A] [HB : is_hprop B] (f : A → B) (g : B → A) : is_equiv f := is_equiv.mk g (λb, !is_hprop.elim) (λa, !is_hprop.elim) (λa, !is_hset.elim) definition equiv_of_is_hprop [HA : is_hprop A] [HB : is_hprop B] (f : A → B) (g : B → A) : A ≃ B := equiv.mk f (is_equiv_of_is_hprop f g) definition equiv_of_iff_of_is_hprop [HA : is_hprop A] [HB : is_hprop B] (H : A ↔ B) : A ≃ B := equiv_of_is_hprop (iff.elim_left H) (iff.elim_right H) end /- interaction with the Unit type -/ -- A contractible type is equivalent to [Unit]. *) definition equiv_unit_of_is_contr [H : is_contr A] : A ≃ unit := equiv.mk (λ (x : A), ⋆) (is_equiv.mk (λ (u : unit), center A) (λ (u : unit), unit.rec_on u idp) (λ (x : A), contr x) (λ (x : A), !ap_constant⁻¹)) -- TODO: port "Truncated morphisms" end is_trunc
e4dcd3f9b114dbf22cf0921af3c4a587a5d0855a
cbcb0199842f03e7606d4e43666573fc15dd07a5
/src/topology/metric_space/gromov_hausdorff.lean
5ab0a4f67c587cc9d17843d52f405ae633fd7a71
[ "Apache-2.0" ]
permissive
truonghoangle/mathlib
a6a7c14b3767ec71156239d8ea97f6921fe79627
673bae584febcd830c2c9256eb7e7a81e27ed303
refs/heads/master
1,590,347,998,944
1,559,728,860,000
1,559,728,860,000
187,431,971
0
0
null
1,558,238,525,000
1,558,238,525,000
null
UTF-8
Lean
false
false
56,049
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sébastien Gouëzel The Gromov-Hausdorff distance on the space of nonempty compact metric spaces up to isometry. We introduces the space of all nonempty compact metric spaces, up to isometry, called `GH_space`, and endow it with a metric space structure. The distance, known as the Gromov-Hausdorff distance, is defined as follows: given two nonempty compact spaces X and Y, their distance is the minimum Hausdorff distance between all possible isometric embeddings of X and Y in all metric spaces. To define properly the Gromov-Hausdorff space, we consider the non-empty compact subsets of ℓ^∞(ℝ) up to isometry, which is a well-defined type, and define the distance as the infimum of the Hausdorff distance over all embeddings in ℓ^∞(ℝ). We prove that this coincides with the previous description, as all separable metric spaces embed isometrically into ℓ^∞(ℝ), through an embedding called the Kuratowski embedding. To prove that we have a distance, we should show that if spaces can be coupled to be arbitrarily close, then they are isometric. More generally, the Gromov-Hausdorff distance is realized, i.e., there is a coupling for which the Hausdorff distance is exactly the Gromov-Hausdorff distance. This follows from a compactness argument, essentially following from Arzela-Ascoli. We prove the most important properties of the Gromov-Hausdorff space: it is a polish space, i.e., it is complete and second countable. We also prove the Gromov compactness criterion. -/ import topology.metric_space.closeds set_theory.cardinal topology.metric_space.gromov_hausdorff_realized topology.metric_space.completion noncomputable theory local attribute [instance, priority 0] classical.prop_decidable universes u v w open classical lattice set function topological_space filter metric quotient open bounded_continuous_function nat Kuratowski_embedding open sum (inl inr) set_option class.instance_max_depth 50 local attribute [instance] metric_space_sum namespace Gromov_Hausdorff section GH_space /- In this section, we define the Gromov-Hausdorff space, denoted `GH_space` as the quotient of nonempty compact subsets of ℓ^∞(ℝ) by identifying isometric sets. Using the Kuratwoski embedding, we get a canonical map `to_GH_space` mapping any nonempty compact type to GH_space. -/ /-- Equivalence relation identifying two nonempty compact sets which are isometric -/ private definition isometry_rel : nonempty_compacts ℓ_infty_ℝ → nonempty_compacts ℓ_infty_ℝ → Prop := λx y, nonempty (x.val ≃ᵢ y.val) /-- This is indeed an equivalence relation -/ private lemma is_equivalence_isometry_rel : equivalence isometry_rel := ⟨λx, ⟨isometric.refl _⟩, λx y ⟨e⟩, ⟨e.symm⟩, λ x y z ⟨e⟩ ⟨f⟩, ⟨e.trans f⟩⟩ /-- setoid instance identifying two isometric nonempty compact subspaces of ℓ^∞(ℝ) -/ instance isometry_rel.setoid : setoid (nonempty_compacts ℓ_infty_ℝ) := setoid.mk isometry_rel is_equivalence_isometry_rel /-- The Gromov-Hausdorff space -/ definition GH_space : Type := quotient (isometry_rel.setoid) /-- Map any nonempty compact type to GH_space -/ definition to_GH_space (α : Type u) [metric_space α] [compact_space α] [nonempty α] : GH_space := ⟦nonempty_compacts.Kuratowski_embedding α⟧ /-- A metric space representative of any abstract point in GH_space -/ definition GH_space.rep (p : GH_space) : Type := (quot.out p).val lemma eq_to_GH_space_iff {α : Type u} [metric_space α] [compact_space α] [nonempty α] {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space α ↔ ∃Ψ : α → ℓ_infty_ℝ, isometry Ψ ∧ range Ψ = p.val := begin simp only [to_GH_space, quotient.eq], split, { assume h, rcases setoid.symm h with ⟨e⟩, have f := (Kuratowski_embedding_isometry α).isometric_on_range.trans e, use λx, f x, split, { apply isometry_subtype_val.comp f.isometry }, { rw [range_comp, f.range_coe, set.image_univ, set.range_coe_subtype] } }, { rintros ⟨Ψ, ⟨isomΨ, rangeΨ⟩⟩, have f := ((Kuratowski_embedding_isometry α).isometric_on_range.symm.trans isomΨ.isometric_on_range).symm, have E : (range Ψ) ≃ᵢ (nonempty_compacts.Kuratowski_embedding α).val = (p.val ≃ᵢ range (Kuratowski_embedding α)), by { dunfold nonempty_compacts.Kuratowski_embedding, rw [rangeΨ]; refl }, have g := cast E f, exact ⟨g⟩ } end lemma eq_to_GH_space {p : nonempty_compacts ℓ_infty_ℝ} : ⟦p⟧ = to_GH_space p.val := begin refine eq_to_GH_space_iff.2 ⟨((λx, x) : p.val → ℓ_infty_ℝ), _, subtype.val_range⟩, apply isometry_subtype_val end section local attribute [reducible] GH_space.rep instance rep_GH_space_metric_space {p : GH_space} : metric_space (p.rep) := by apply_instance instance rep_GH_space_compact_space {p : GH_space} : compact_space (p.rep) := by apply_instance instance rep_GH_space_nonempty {p : GH_space} : nonempty (p.rep) := by apply_instance end lemma GH_space.to_GH_space_rep (p : GH_space) : to_GH_space (p.rep) = p := begin change to_GH_space (quot.out p).val = p, rw ← eq_to_GH_space, exact quot.out_eq p end /-- Two nonempty compact spaces have the same image in GH_space if and only if they are isometric -/ lemma to_GH_space_eq_to_GH_space_iff_isometric {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type u} [metric_space β] [compact_space β] [nonempty β] : to_GH_space α = to_GH_space β ↔ nonempty (α ≃ᵢ β) := ⟨begin simp only [to_GH_space, quotient.eq], assume h, rcases h with e, have I : ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val) = ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have e' := cast I e, have f := (Kuratowski_embedding_isometry α).isometric_on_range, have g := (Kuratowski_embedding_isometry β).isometric_on_range.symm, have h := (f.trans e').trans g, exact ⟨h⟩ end, begin rintros ⟨e⟩, simp only [to_GH_space, quotient.eq], have f := (Kuratowski_embedding_isometry α).isometric_on_range.symm, have g := (Kuratowski_embedding_isometry β).isometric_on_range, have h := (f.trans e).trans g, have I : ((range (Kuratowski_embedding α)) ≃ᵢ (range (Kuratowski_embedding β))) = ((nonempty_compacts.Kuratowski_embedding α).val ≃ᵢ (nonempty_compacts.Kuratowski_embedding β).val), by { dunfold nonempty_compacts.Kuratowski_embedding, refl }, have h' := cast I h, exact ⟨h'⟩ end⟩ /-- Distance on GH_space : the distance between two nonempty compact spaces is the infimum Hausdorff distance between isometric copies of the two spaces in a metric space. For the definition, we only consider embeddings in ℓ^∞(ℝ), but we will prove below that it works for all spaces. -/ instance : has_dist (GH_space) := { dist := λx y, Inf ((λp : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ, Hausdorff_dist p.1.val p.2.val) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y})) } def GH_dist (α : Type u) (β : Type v) [metric_space α] [nonempty α] [compact_space α] [metric_space β] [nonempty β] [compact_space β] : ℝ := dist (to_GH_space α) (to_GH_space β) lemma dist_GH_dist (p q : GH_space) : dist p q = GH_dist (p.rep) (q.rep) := by rw [GH_dist, p.to_GH_space_rep, q.to_GH_space_rep] /-- The Gromov-Hausdorff distance between two spaces is bounded by the Hausdorff distance of isometric copies of the spaces, in any metric space. -/ theorem GH_dist_le_Hausdorff_dist {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] {γ : Type w} [metric_space γ] {Φ : α → γ} {Ψ : β → γ} (ha : isometry Φ) (hb : isometry Ψ) : GH_dist α β ≤ Hausdorff_dist (range Φ) (range Ψ) := begin /- For the proof, we want to embed γ in ℓ^∞(ℝ), to say that the Hausdorff distance is realized in ℓ^∞(ℝ) and therefore bounded below by the Gromov-Hausdorff-distance. However, γ is not separable in general. We restrict to the union of the images of α and β in γ, which is separable and therefore embeddable in ℓ^∞(ℝ). -/ rcases exists_mem_of_nonempty α with ⟨xα, _⟩, letI : inhabited α := ⟨xα⟩, letI : inhabited β := classical.inhabited_of_nonempty (by assumption), let s : set γ := (range Φ) ∪ (range Ψ), let Φ' : α → subtype s := λy, ⟨Φ y, mem_union_left _ (mem_range_self _)⟩, let Ψ' : β → subtype s := λy, ⟨Ψ y, mem_union_right _ (mem_range_self _)⟩, have IΦ' : isometry Φ' := λx y, ha x y, have IΨ' : isometry Ψ' := λx y, hb x y, have : compact s, { apply compact_union_of_compact, { rw ← image_univ, apply compact_image compact_univ ha.continuous }, { rw ← image_univ, apply compact_image compact_univ hb.continuous } }, letI : metric_space (subtype s) := by apply_instance, haveI : compact_space (subtype s) := ⟨compact_iff_compact_univ.1 ‹compact s›⟩, haveI : nonempty (subtype s) := ⟨Φ' xα⟩, have ΦΦ' : Φ = subtype.val ∘ Φ', by { funext, refl }, have ΨΨ' : Ψ = subtype.val ∘ Ψ', by { funext, refl }, have : Hausdorff_dist (range Φ) (range Ψ) = Hausdorff_dist (range Φ') (range Ψ'), { rw [ΦΦ', ΨΨ', range_comp, range_comp], exact Hausdorff_dist_image (isometry_subtype_val) }, rw this, -- Embed s in ℓ^∞(ℝ) through its Kuratowski embedding let F := Kuratowski_embedding (subtype s), have : Hausdorff_dist (F '' (range Φ')) (F '' (range Ψ')) = Hausdorff_dist (range Φ') (range Ψ') := Hausdorff_dist_image (Kuratowski_embedding_isometry _), rw ← this, -- Let A and B be the images of α and β under this embedding. They are in ℓ^∞(ℝ), and -- their Hausdorff distance is the same as in the original space. let A : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Φ'), ⟨by simp, begin rw [← range_comp, ← image_univ], exact compact_image compact_univ ((Kuratowski_embedding_isometry _).continuous.comp IΦ'.continuous), end⟩⟩, let B : nonempty_compacts ℓ_infty_ℝ := ⟨F '' (range Ψ'), ⟨by simp, begin rw [← range_comp, ← image_univ], exact compact_image compact_univ ((Kuratowski_embedding_isometry _).continuous.comp IΨ'.continuous), end⟩⟩, have Aα : ⟦A⟧ = to_GH_space α, { rw eq_to_GH_space_iff, exact ⟨λx, F (Φ' x), ⟨(Kuratowski_embedding_isometry _).comp IΦ', by rw range_comp⟩⟩ }, have Bβ : ⟦B⟧ = to_GH_space β, { rw eq_to_GH_space_iff, exact ⟨λx, F (Ψ' x), ⟨(Kuratowski_embedding_isometry _).comp IΨ', by rw range_comp⟩⟩ }, refine cInf_le ⟨0, begin simp, assume t _ _ _ _ ht, rw ← ht, exact Hausdorff_dist_nonneg end⟩ _, apply (mem_image _ _ _).2, existsi (⟨A, B⟩ : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), simp [Aα, Bβ] end local attribute [instance, priority 0] inhabited_of_nonempty' /-- The optimal coupling constructed above realizes exactly the Gromov-Hausdorff distance, essentially by design. -/ lemma Hausdorff_dist_optimal {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) = GH_dist α β := begin /- we only need to check the inequality ≤, as the other one follows from the previous lemma. As the Gromov-Hausdorff distance is an infimum, we need to check that the Hausdorff distance in the optimal coupling is smaller than the Hausdorff distance of any coupling. First, we check this for couplings which already have small Hausdorff distance: in this case, the induced "distance" on α ⊕ β belongs to the candidates family introduced in the definition of the optimal coupling, and the conclusion follows from the optimality of the optimal coupling within this family. -/ have A : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β → Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β) → Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val), { assume p q hp hq bound, rcases eq_to_GH_space_iff.1 hp with ⟨Φ, ⟨Φisom, Φrange⟩⟩, rcases eq_to_GH_space_iff.1 hq with ⟨Ψ, ⟨Ψisom, Ψrange⟩⟩, have I : diam (range Φ ∪ range Ψ) ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β), { rcases exists_mem_of_nonempty α with ⟨xα, _⟩, have : ∃y ∈ range Ψ, dist (Φ xα) y < diam (univ : set α) + 1 + diam (univ : set β), { rw Ψrange, have : Φ xα ∈ p.val := begin rw ← Φrange, exact mem_range_self _ end, exact exists_dist_lt_of_Hausdorff_dist_lt this bound (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 (bounded_of_compact p.2.2) (bounded_of_compact q.2.2)) }, rcases this with ⟨y, hy, dy⟩, rcases mem_range.1 hy with ⟨z, hzy⟩, rw ← hzy at dy, have DΦ : diam (range Φ) = diam (univ : set α) := begin rw [← image_univ], apply metric.isometry.diam_image Φisom end, have DΨ : diam (range Ψ) = diam (univ : set β) := begin rw [← image_univ], apply metric.isometry.diam_image Ψisom end, calc diam (range Φ ∪ range Ψ) ≤ diam (range Φ) + dist (Φ xα) (Ψ z) + diam (range Ψ) : diam_union (mem_range_self _) (mem_range_self _) ... ≤ diam (univ : set α) + (diam (univ : set α) + 1 + diam (univ : set β)) + diam (univ : set β) : by { rw [DΦ, DΨ], apply add_le_add (add_le_add (le_refl _) (le_of_lt dy)) (le_refl _) } ... = 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : by ring }, let f : α ⊕ β → ℓ_infty_ℝ := λx, match x with | inl y := Φ y | inr z := Ψ z end, let F : (α ⊕ β) × (α ⊕ β) → ℝ := λp, dist (f p.1) (f p.2), -- check that the induced "distance" is a candidate have Fgood : F ∈ candidates α β, { simp only [candidates, forall_const, and_true, add_comm, eq_self_iff_true, dist_eq_zero, and_self, set.mem_set_of_eq], repeat {split}, { exact λx y, calc F (inl x, inl y) = dist (Φ x) (Φ y) : rfl ... = dist x y : Φisom.dist_eq }, { exact λx y, calc F (inr x, inr y) = dist (Ψ x) (Ψ y) : rfl ... = dist x y : Ψisom.dist_eq }, { exact λx y, dist_comm _ _ }, { exact λx y z, dist_triangle _ _ _ }, { exact λx y, calc F (x, y) ≤ diam (range Φ ∪ range Ψ) : begin have A : ∀z : α ⊕ β, f z ∈ range Φ ∪ range Ψ, { assume z, cases z, { apply mem_union_left, apply mem_range_self }, { apply mem_union_right, apply mem_range_self } }, refine dist_le_diam_of_mem _ (A _) (A _), rw [Φrange, Ψrange], exact bounded_of_compact (compact_union_of_compact p.2.2 q.2.2), end ... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : I } }, let Fb := candidates_b_of_candidates F Fgood, have : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD Fb := Hausdorff_dist_optimal_le_HD _ _ (candidates_b_of_candidates_mem F Fgood), refine le_trans this (le_of_forall_le_of_dense (λr hr, _)), have I1 : ∀x : α, infi (λy:β, Fb (inl x, inr y)) ≤ r, { assume x, have : f (inl x) ∈ p.val, by { rw [← Φrange], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt this hr (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 (bounded_of_compact p.2.2) (bounded_of_compact q.2.2)) with ⟨z, zq, hz⟩, have : z ∈ range Ψ, by rwa [← Ψrange] at zq, rcases mem_range.1 this with ⟨y, hy⟩, calc infi (λy:β, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux1 0) ... = dist (Φ x) (Ψ y) : rfl ... = dist (f (inl x)) z : by rw hy ... ≤ r : le_of_lt hz }, have I2 : ∀y : β, infi (λx:α, Fb (inl x, inr y)) ≤ r, { assume y, have : f (inr y) ∈ q.val, by { rw [← Ψrange], apply mem_range_self }, rcases exists_dist_lt_of_Hausdorff_dist_lt' this hr (Hausdorff_edist_ne_top_of_ne_empty_of_bounded p.2.1 q.2.1 (bounded_of_compact p.2.2) (bounded_of_compact q.2.2)) with ⟨z, zq, hz⟩, have : z ∈ range Φ, by rwa [← Φrange] at zq, rcases mem_range.1 this with ⟨x, hx⟩, calc infi (λx:α, Fb (inl x, inr y)) ≤ Fb (inl x, inr y) : cinfi_le (by simpa using HD_below_aux2 0) ... = dist (Φ x) (Ψ y) : rfl ... = dist z (f (inr y)) : by rw hx ... ≤ r : le_of_lt hz }, simp [HD, csupr_le I1, csupr_le I2] }, /- Get the same inequality for any coupling. If the coupling is quite good, the desired inequality has been proved above. If it is bad, then the inequality is obvious. -/ have B : ∀p q : nonempty_compacts (ℓ_infty_ℝ), ⟦p⟧ = to_GH_space α → ⟦q⟧ = to_GH_space β → Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ Hausdorff_dist (p.val) (q.val), { assume p q hp hq, by_cases h : Hausdorff_dist (p.val) (q.val) < diam (univ : set α) + 1 + diam (univ : set β), { exact A p q hp hq h }, { calc Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD (candidates_b_dist α β) : Hausdorff_dist_optimal_le_HD _ _ (candidates_b_dist_mem_candidates_b) ... ≤ diam (univ : set α) + 1 + diam (univ : set β) : HD_candidates_b_dist_le ... ≤ Hausdorff_dist (p.val) (q.val) : not_lt.1 h } }, refine le_antisymm _ _, { apply le_cInf, { rw ne_empty_iff_exists_mem, simp only [set.mem_image, nonempty_of_inhabited, set.mem_set_of_eq, prod.exists], existsi [Hausdorff_dist (nonempty_compacts.Kuratowski_embedding α).val (nonempty_compacts.Kuratowski_embedding β).val, nonempty_compacts.Kuratowski_embedding α, nonempty_compacts.Kuratowski_embedding β], simp [to_GH_space, -quotient.eq] }, { rintro b ⟨⟨p, q⟩, ⟨hp, hq⟩, rfl⟩, exact B p q hp hq } }, { exact GH_dist_le_Hausdorff_dist (isometry_optimal_GH_injl α β) (isometry_optimal_GH_injr α β) } end /-- The Gromov-Hausdorff distance can also be realized by a coupling in ℓ^∞(ℝ), by embedding the optimal coupling through its Kuratowski embedding. -/ theorem GH_dist_eq_Hausdorff_dist (α : Type u) [metric_space α] [compact_space α] [nonempty α] (β : Type v) [metric_space β] [compact_space β] [nonempty β] : ∃Φ : α → ℓ_infty_ℝ, ∃Ψ : β → ℓ_infty_ℝ, isometry Φ ∧ isometry Ψ ∧ GH_dist α β = Hausdorff_dist (range Φ) (range Ψ) := begin let F := Kuratowski_embedding (optimal_GH_coupling α β), let Φ := F ∘ optimal_GH_injl α β, let Ψ := F ∘ optimal_GH_injr α β, refine ⟨Φ, Ψ, _, _, _⟩, { exact (Kuratowski_embedding_isometry _).comp (isometry_optimal_GH_injl α β) }, { exact (Kuratowski_embedding_isometry _).comp (isometry_optimal_GH_injr α β) }, { rw [← image_univ, ← image_univ, image_comp F, image_univ, image_comp F (optimal_GH_injr α β), image_univ, ← Hausdorff_dist_optimal], exact (Hausdorff_dist_image (Kuratowski_embedding_isometry _)).symm }, end -- without the next two lines, { exact closed_of_compact (range Φ) hΦ } in the next -- proof is very slow, as the t2_space instance is very hard to find local attribute [instance, priority 0] orderable_topology.t2_space local attribute [instance, priority 0] ordered_topology.to_t2_space /-- The Gromov-Hausdorff distance defines a genuine distance on the Gromov-Hausdorff space. -/ instance GH_space_metric_space : metric_space GH_space := { dist_self := λx, begin rcases exists_rep x with ⟨y, hy⟩, refine le_antisymm _ _, { apply cInf_le, { exact ⟨0, by { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } ⟩}, { simp, existsi [y, y], simpa } }, { apply le_cInf, { simp only [set.image_eq_empty, ne.def], apply ne_empty_iff_exists_mem.2, existsi (⟨y, y⟩ : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), simpa }, { rintro b ⟨⟨u, v⟩, ⟨hu, hv⟩, rfl⟩, exact Hausdorff_dist_nonneg } }, end, dist_comm := λx y, begin have A : (λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) = ((λ (p : nonempty_compacts ℓ_infty_ℝ × nonempty_compacts ℓ_infty_ℝ), Hausdorff_dist ((p.fst).val) ((p.snd).val)) ∘ prod.swap) '' (set.prod {a | ⟦a⟧ = x} {b | ⟦b⟧ = y}) := by { congr, funext, simp, rw Hausdorff_dist_comm }, simp only [dist, A, image_comp, prod.swap, image_swap_prod], end, eq_of_dist_eq_zero := λx y hxy, begin /- To show that two spaces at zero distance are isometric, we argue that the distance is realized by some coupling. In this coupling, the two spaces are at zero Hausdorff distance, i.e., they coincide. Therefore, the original spaces are isometric. -/ rcases GH_dist_eq_Hausdorff_dist x.rep y.rep with ⟨Φ, Ψ, Φisom, Ψisom, DΦΨ⟩, rw [← dist_GH_dist, hxy] at DΦΨ, have : range Φ = range Ψ, { have hΦ : compact (range Φ) := by { rw [← image_univ], exact compact_image compact_univ Φisom.continuous }, have hΨ : compact (range Ψ) := by { rw [← image_univ], exact compact_image compact_univ Ψisom.continuous }, apply (Hausdorff_dist_zero_iff_eq_of_closed _ _ _).1 (DΦΨ.symm), { exact closed_of_compact (range Φ) hΦ }, { exact closed_of_compact (range Ψ) hΨ }, { exact Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simp [-nonempty_subtype]) (by simp [-nonempty_subtype]) (bounded_of_compact hΦ) (bounded_of_compact hΨ) } }, have T : ((range Ψ) ≃ᵢ y.rep) = ((range Φ) ≃ᵢ y.rep), by rw this, have eΨ := cast T Ψisom.isometric_on_range.symm, have e := Φisom.isometric_on_range.trans eΨ, rw [← x.to_GH_space_rep, ← y.to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], exact ⟨e⟩ end, dist_triangle := λx y z, begin /- To show the triangular inequality between X, Y and Z, realize an optimal coupling between X and Y in a space γ1, and an optimal coupling between Y and Z in a space γ2. Then, glue these metric spaces along Y. We get a new space γ in which X and Y are optimally coupled, as well as Y and Z. Apply the triangle inequality for the Hausdorff distance in γ to conclude. -/ let X := x.rep, let Y := y.rep, let Z := z.rep, let γ1 := optimal_GH_coupling X Y, let γ2 := optimal_GH_coupling Y Z, let Φ : Y → γ1 := optimal_GH_injr X Y, have hΦ : isometry Φ := isometry_optimal_GH_injr X Y, let Ψ : Y → γ2 := optimal_GH_injl Y Z, have hΨ : isometry Ψ := isometry_optimal_GH_injl Y Z, let γ := glue_space hΦ hΨ, letI : metric_space γ := metric.metric_space_glue_space hΦ hΨ, have Comm : (to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y) = (to_glue_r hΦ hΨ) ∘ (optimal_GH_injl Y Z) := to_glue_commute hΦ hΨ, calc dist x z = dist (to_GH_space X) (to_GH_space Z) : by rw [x.to_GH_space_rep, z.to_GH_space_rep] ... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y))) (range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) : GH_dist_le_Hausdorff_dist ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y)) ((to_glue_r_isometry hΦ hΨ).comp (isometry_optimal_GH_injr Y Z)) ... ≤ Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injl X Y))) (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y))) + Hausdorff_dist (range ((to_glue_l hΦ hΨ) ∘ (optimal_GH_injr X Y))) (range ((to_glue_r hΦ hΨ) ∘ (optimal_GH_injr Y Z))) : begin refine Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simp [-nonempty_subtype]) (by simp [-nonempty_subtype]) _ _), { rw [← image_univ], exact bounded_of_compact (compact_image compact_univ (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injl X Y)))) }, { rw [← image_univ], exact bounded_of_compact (compact_image compact_univ (isometry.continuous ((to_glue_l_isometry hΦ hΨ).comp (isometry_optimal_GH_injr X Y)))) } end ... = Hausdorff_dist ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injl X Y))) ((to_glue_l hΦ hΨ) '' (range (optimal_GH_injr X Y))) + Hausdorff_dist ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injl Y Z))) ((to_glue_r hΦ hΨ) '' (range (optimal_GH_injr Y Z))) : by simp only [eq.symm range_comp, Comm, eq_self_iff_true, add_right_inj] ... = Hausdorff_dist (range (optimal_GH_injl X Y)) (range (optimal_GH_injr X Y)) + Hausdorff_dist (range (optimal_GH_injl Y Z)) (range (optimal_GH_injr Y Z)) : by rw [Hausdorff_dist_image (to_glue_l_isometry hΦ hΨ), Hausdorff_dist_image (to_glue_r_isometry hΦ hΨ)] ... = dist (to_GH_space X) (to_GH_space Y) + dist (to_GH_space Y) (to_GH_space Z) : by rw [Hausdorff_dist_optimal, Hausdorff_dist_optimal, GH_dist, GH_dist] ... = dist x y + dist y z: by rw [x.to_GH_space_rep, y.to_GH_space_rep, z.to_GH_space_rep] end } end GH_space --section end Gromov_Hausdorff /-- In particular, nonempty compacts of a metric space map to GH_space. We register this in the topological_space namespace to take advantage of the notation p.to_GH_space -/ definition topological_space.nonempty_compacts.to_GH_space {α : Type u} [metric_space α] (p : nonempty_compacts α) : Gromov_Hausdorff.GH_space := Gromov_Hausdorff.to_GH_space p.val open topological_space namespace Gromov_Hausdorff section nonempty_compacts variables {α : Type u} [metric_space α] theorem GH_dist_le_nonempty_compacts_dist (p q : nonempty_compacts α) : dist p.to_GH_space q.to_GH_space ≤ dist p q := begin have ha : isometry (subtype.val : p.val → α) := isometry_subtype_val, have hb : isometry (subtype.val : q.val → α) := isometry_subtype_val, have A : dist p q = Hausdorff_dist p.val q.val := rfl, have I : p.val = range (subtype.val : p.val → α), by simp, have J : q.val = range (subtype.val : q.val → α), by simp, rw [I, J] at A, rw A, exact GH_dist_le_Hausdorff_dist ha hb end lemma to_GH_space_lipschitz : lipschitz_with 1 (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) := ⟨zero_le_one, by { simp, exact GH_dist_le_nonempty_compacts_dist } ⟩ lemma to_GH_space_continuous : continuous (nonempty_compacts.to_GH_space : nonempty_compacts α → GH_space) := to_GH_space_lipschitz.to_continuous end nonempty_compacts section /- In this section, we show that if two metric spaces are isometric up to ε2, then their Gromov-Hausdorff distance is bounded by ε2 / 2. More generally, if there are subsets which are ε1-dense and ε3-dense in two spaces, and isometric up to ε2, then the Gromov-Hausdorff distance between the spaces is bounded by ε1 + ε2/2 + ε3. For this, we construct a suitable coupling between the two spaces, by gluing them (approximately) along the two matching subsets. -/ variables {α : Type u} [metric_space α] [compact_space α] [nonempty α] {β : Type v} [metric_space β] [compact_space β] [nonempty β] /-- If there are subsets which are ε1-dense and ε3-dense in two spaces, and isometric up to ε2, then the Gromov-Hausdorff distance between the spaces is bounded by ε1 + ε2/2 + ε3. -/ theorem GH_dist_le_of_approx_subsets {s : set α} (Φ : s → β) {ε1 ε2 ε3 : ℝ} (hs : ∀x : α, ∃y ∈ s, dist x y ≤ ε1) (hs' : ∀x : β, ∃y : s, dist x (Φ y) ≤ ε3) (H : ∀x y : s, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε2) : GH_dist α β ≤ ε1 + ε2 / 2 + ε3 := begin refine real.le_of_forall_epsilon_le (λδ δ0, _), rcases exists_mem_of_nonempty α with ⟨xα, _⟩, rcases hs xα with ⟨xs, hxs, Dxs⟩, have sne : s ≠ ∅ := ne_empty_of_mem hxs, letI : nonempty (subtype s) := ⟨⟨xs, hxs⟩⟩, have : 0 ≤ ε2 := le_trans (abs_nonneg _) (H ⟨xs, hxs⟩ ⟨xs, hxs⟩), have : ∀ p q : s, abs (dist p q - dist (Φ p) (Φ q)) ≤ 2 * (ε2/2 + δ) := λp q, calc abs (dist p q - dist (Φ p) (Φ q)) ≤ ε2 : H p q ... ≤ 2 * (ε2/2 + δ) : by linarith, -- glue α and β along the almost matching subsets letI : metric_space (α ⊕ β) := glue_metric_approx (@subtype.val α s) (λx, Φ x) (ε2/2 + δ) (by linarith) this, let Fl := @sum.inl α β, let Fr := @sum.inr α β, have Il : isometry Fl := isometry_emetric_iff_metric.2 (λx y, rfl), have Ir : isometry Fr := isometry_emetric_iff_metric.2 (λx y, rfl), /- The proof goes as follows : the GH_dist is bounded by the Hausdorff distance of the images in the coupling, which is bounded (using the triangular inequality) by the sum of the Hausdorff distances of α and s (in the coupling or, equivalently in the original space), of s and Φ s, and of Φ s and β (in the coupling or, equivalently, in the original space). The first term is bounded by ε1, by ε1-density. The third one is bounded by ε3. And the middle one is bounded by ε2/2 as in the coupling the points x and Φ x are at distance ε2/2 by construction of the coupling (in fact ε2/2 + δ where δ is an arbitrarily small positive constant where positivity is used to ensure that the coupling is really a metric space and not a premetric space on α ⊕ β). -/ have : GH_dist α β ≤ Hausdorff_dist (range Fl) (range Fr) := GH_dist_le_Hausdorff_dist Il Ir, have : Hausdorff_dist (range Fl) (range Fr) ≤ Hausdorff_dist (range Fl) (Fl '' s) + Hausdorff_dist (Fl '' s) (range Fr), { have B : bounded (range Fl) := bounded_of_compact (compact_range Il.continuous), exact Hausdorff_dist_triangle (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simpa) (by simpa) B (bounded.subset (image_subset_range _ _) B)) }, have : Hausdorff_dist (Fl '' s) (range Fr) ≤ Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) + Hausdorff_dist (Fr '' (range Φ)) (range Fr), { have B : bounded (range Fr) := bounded_of_compact (compact_range Ir.continuous), exact Hausdorff_dist_triangle' (Hausdorff_edist_ne_top_of_ne_empty_of_bounded (by simpa [-nonempty_subtype]) (by simpa) (bounded.subset (image_subset_range _ _) B) B) }, have : Hausdorff_dist (range Fl) (Fl '' s) ≤ ε1, { rw [← image_univ, Hausdorff_dist_image Il], have : 0 ≤ ε1 := le_trans dist_nonneg Dxs, refine Hausdorff_dist_le_of_mem_dist this (λx hx, hs x) (λx hx, ⟨x, mem_univ _, by simpa⟩) }, have : Hausdorff_dist (Fl '' s) (Fr '' (range Φ)) ≤ ε2/2 + δ, { refine Hausdorff_dist_le_of_mem_dist (by linarith) _ _, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨x, ⟨x_in_s, xx'⟩⟩, rw ← xx', use [Fr (Φ ⟨x, x_in_s⟩), mem_image_of_mem Fr (mem_range_self _)], exact le_of_eq (glue_dist_glued_points (@subtype.val α s) Φ (ε2/2 + δ) ⟨x, x_in_s⟩) }, { assume x' hx', rcases (set.mem_image _ _ _).1 hx' with ⟨y, ⟨y_in_s', yx'⟩⟩, rcases mem_range.1 y_in_s' with ⟨x, xy⟩, use [Fl x, mem_image_of_mem _ x.2], rw [← yx', ← xy, dist_comm], exact le_of_eq (glue_dist_glued_points (@subtype.val α s) Φ (ε2/2 + δ) x) } }, have : Hausdorff_dist (Fr '' (range Φ)) (range Fr) ≤ ε3, { rw [← @image_univ _ _ Fr, Hausdorff_dist_image Ir], rcases exists_mem_of_nonempty β with ⟨xβ, _⟩, rcases hs' xβ with ⟨xs', Dxs'⟩, have : 0 ≤ ε3 := le_trans dist_nonneg Dxs', refine Hausdorff_dist_le_of_mem_dist this (λx hx, ⟨x, mem_univ _, by simpa⟩) (λx _, _), rcases hs' x with ⟨y, Dy⟩, exact ⟨Φ y, mem_range_self _, Dy⟩ }, linarith end end --section /-- The Gromov-Hausdorff space is second countable. -/ lemma second_countable : second_countable_topology GH_space := begin refine second_countable_of_countable_discretization (λδ δpos, _), let ε := (2/5) * δ, have εpos : 0 < ε := mul_pos (by norm_num) δpos, have : ∀p:GH_space, ∃s : set (p.rep), finite s ∧ (univ ⊆ (⋃x∈s, ball x ε)) := λp, by simpa using finite_cover_balls_of_compact (@compact_univ (p.rep) _ _ _) εpos, -- for each p, s p is a finite ε-dense subset of p (or rather the metric space -- p.rep representing p) choose s hs using this, have : ∀p:GH_space, ∀t:set (p.rep), finite t → ∃n:ℕ, ∃e:equiv t (fin n), true, { assume p t ht, letI : fintype t := finite.fintype ht, rcases fintype.exists_equiv_fin t with ⟨n, hn⟩, rcases hn with e, exact ⟨n, e, trivial⟩ }, choose N e hne using this, -- cardinality of the nice finite subset s p of p.rep, called N p let N := λp:GH_space, N p (s p) (hs p).1, -- equiv from s p, a nice finite subset of p.rep, to fin (N p), called E p let E := λp:GH_space, e p (s p) (hs p).1, -- A function F associating to p ∈ GH_space the data of all distances of points -- in the ε-dense set s p. let F : GH_space → Σn:ℕ, (fin n → fin n → ℤ) := λp, ⟨N p, λa b, floor (ε⁻¹ * dist ((E p).inv_fun a) ((E p).inv_fun b))⟩, refine ⟨_, by apply_instance, F, λp q hpq, _⟩, /- As the target space of F is countable, it suffices to show that two points p and q with F p = F q are at distance ≤ δ. For this, we construct a map Φ from s p ⊆ p.rep (representing p) to q.rep (representing q) which is almost an isometry on s p, and with image s q. For this, we compose the identification of s p with fin (N p) and the inverse of the identification of s q with fin (N q). Together with the fact that N p = N q, this constructs Ψ between s p and s q, and then composing with the canonical inclusion we get Φ. -/ have Npq : N p = N q := (sigma.mk.inj_iff.1 hpq).1, let Ψ : s p → s q := λx, (E q).inv_fun (fin.cast Npq ((E p).to_fun x)), let Φ : s p → q.rep := λx, Ψ x, -- Use the almost isometry Φ to show that p.rep and q.rep -- are within controlled Gromov-Hausdorff distance. have main : GH_dist p.rep q.rep ≤ ε + ε/2 + ε, { refine GH_dist_le_of_approx_subsets Φ _ _ _, show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε, { -- by construction, s p is ε-dense assume x, have : x ∈ ⋃y∈(s p), ball y ε := (hs p).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, exact ⟨y, ys, le_of_lt hy⟩ }, show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε, { -- by construction, s q is ε-dense, and it is the range of Φ assume x, have : x ∈ ⋃y∈(s q), ball y ε := (hs q).2 (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, let i := ((E q).to_fun ⟨y, ys⟩).1, let hi := ((E q).to_fun ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q).to_fun ⟨y, ys⟩, by rw fin.ext_iff, have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).inv_fun ⟨i, hip⟩, use z, have C1 : (E p).to_fun z = ⟨i, hip⟩ := (E p).right_inv ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).inv_fun ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).left_inv ⟨y, ys⟩ }, have : Φ z = y := by { simp only [Φ, Ψ], rw [C1, C2, C3], refl }, rw this, exact le_of_lt hy }, show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε, { /- the distance between x and y is encoded in F p, and the distance between Φ x and Φ y (two points of s q) is encoded in F q, all this up to ε. As F p = F q, the distances are almost equal. -/ assume x y, have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl, rw this, -- introduce i, that codes both x and Φ x in fin (N p) = fin (N q) let i := ((E p).to_fun x).1, have hip : i < N p := ((E p).to_fun x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q).to_fun (Ψ x)).1, by { simp [Ψ, (E q).right_inv _] }, -- introduce j, that codes both y and Φ y in fin (N p) = fin (N q) let j := ((E p).to_fun y).1, have hjp : j < N p := ((E p).to_fun y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q).to_fun (Ψ y)).1, by { simp [Ψ, (E q).right_inv _] }, -- Express dist x y in terms of F p have : (F p).2 ((E p).to_fun x) ((E p).to_fun y) = floor (ε⁻¹ * dist x y), by simp only [F, (E p).left_inv _], have Ap : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = floor (ε⁻¹ * dist x y), by { rw ← this, congr; apply (fin.ext_iff _ _).2; refl }, -- Express dist (Φ x) (Φ y) in terms of F q have : (F q).2 ((E q).to_fun (Ψ x)) ((E q).to_fun (Ψ y)) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), by simp only [F, (E q).left_inv _], have Aq : (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩ = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), by { rw ← this, congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] }, -- use the equality between F p and F q to deduce that the distances have equal -- integer parts have : (F p).2 ⟨i, hip⟩ ⟨j, hjp⟩ = (F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant f -- then subst revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊢, subst hpq, intros, refl }, rw [Ap, Aq] at this, -- deduce that the distances coincide up to ε, by a straightforward computation -- that should be automated have I := calc abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) = abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm ... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring } ... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) : by rw [mul_inv_cancel (ne_of_gt εpos), one_mul] ... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) : by rw [abs_of_nonneg (le_of_lt (inv_pos εpos)), mul_assoc] ... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos) ... = ε : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≤ ε + ε/2 + ε : main ... = δ : by { simp [ε], ring } end /-- Compactness criterion : a closed set of compact metric spaces is compact if the spaces have a uniformly bounded diameter, and for all ε the number of balls of radius ε required to cover the space is uniformly bounded. This is an equivalence, but we only prove the interesting direction that these conditions imply compactness. -/ lemma totally_bounded {t : set GH_space} {C : ℝ} {u : ℕ → ℝ} {K : ℕ → ℕ} (ulim : tendsto u at_top (nhds 0)) (hdiam : ∀p ∈ t, diam (univ : set (GH_space.rep p)) ≤ C) (hcov : ∀p ∈ t, ∀n:ℕ, ∃s : set (GH_space.rep p), cardinal.mk s ≤ K n ∧ univ ⊆ ⋃x∈s, ball x (u n)) : totally_bounded t := begin /- Let δ>0, and ε = δ/5. For each p, we construct a finite subset s p of p, which is ε-dense and has cardinality at most K n. Encoding the mutual distances of points in s p, up to ε, we will get a map F associating to p finitely many data, and making it possible to reconstruct p up to ε. This is enough to prove total boundedness. -/ refine metric.totally_bounded_of_finite_discretization (λδ δpos, _), let ε := (1/5) * δ, have εpos : 0 < ε := mul_pos (by norm_num) δpos, -- choose n for which ε < u n rcases metric.tendsto_at_top.1 ulim ε εpos with ⟨n, hn⟩, have u_le_ε : u n ≤ ε, { have := hn n (le_refl _), simp only [real.dist_eq, add_zero, sub_eq_add_neg, neg_zero] at this, exact le_of_lt (lt_of_le_of_lt (le_abs_self _) this) }, -- construct a finite subset s p of p which is ε-dense and has cardinal ≤ K n have : ∀p:GH_space, ∃s : set (p.rep), ∃N ≤ K n, ∃E : equiv s (fin N), p ∈ t → univ ⊆ ⋃x∈s, ball x (u n), { assume p, by_cases hp : p ∉ t, { have : nonempty (equiv (∅ : set (p.rep)) (fin 0)), { rw ← fintype.card_eq, simp }, use [∅, 0, bot_le, choice (this)] }, { rcases hcov _ (set.not_not_mem.1 hp) n with ⟨s, ⟨scard, scover⟩⟩, rcases cardinal.lt_omega.1 (lt_of_le_of_lt scard (cardinal.nat_lt_omega _)) with ⟨N, hN⟩, rw [hN, cardinal.nat_cast_le] at scard, have : cardinal.mk s = cardinal.mk (fin N), by rw [hN, cardinal.mk_fin], cases quotient.exact this with E, use [s, N, scard, E], simp [hp, scover] } }, choose s N hN E hs using this, -- Define a function F taking values in a finite type and associating to p enough data -- to reconstruct it up to ε, namely the (discretized) distances between elements of s p. let M := (floor (ε⁻¹ * max C 0)).to_nat, let F : GH_space → (Σk:fin ((K n).succ), (fin k → fin k → fin (M.succ))) := λp, ⟨⟨N p, lt_of_le_of_lt (hN p) (nat.lt_succ_self _)⟩, λa b, ⟨min M (floor (ε⁻¹ * dist ((E p).inv_fun a) ((E p).inv_fun b))).to_nat, lt_of_le_of_lt ( min_le_left _ _) (nat.lt_succ_self _) ⟩ ⟩, refine ⟨_, by apply_instance, (λp, F p), _⟩, -- It remains to show that if F p = F q, then p and q are ε-close rintros ⟨p, pt⟩ ⟨q, qt⟩ hpq, have Npq : N p = N q := (fin.ext_iff _ _).1 (sigma.mk.inj_iff.1 hpq).1, let Ψ : s p → s q := λx, (E q).inv_fun (fin.cast Npq ((E p).to_fun x)), let Φ : s p → q.rep := λx, Ψ x, have main : GH_dist (p.rep) (q.rep) ≤ ε + ε/2 + ε, { -- to prove the main inequality, argue that s p is ε-dense in p, and s q is ε-dense in q, -- and s p and s q are almost isometric. Then closeness follows -- from GH_dist_le_of_approx_subsets refine GH_dist_le_of_approx_subsets Φ _ _ _, show ∀x : p.rep, ∃ (y : p.rep) (H : y ∈ s p), dist x y ≤ ε, { -- by construction, s p is ε-dense assume x, have : x ∈ ⋃y∈(s p), ball y (u n) := (hs p pt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, exact ⟨y, ys, le_trans (le_of_lt hy) u_le_ε⟩ }, show ∀x : q.rep, ∃ (z : s p), dist x (Φ z) ≤ ε, { -- by construction, s q is ε-dense, and it is the range of Φ assume x, have : x ∈ ⋃y∈(s q), ball y (u n) := (hs q qt) (mem_univ _), rcases mem_bUnion_iff.1 this with ⟨y, ⟨ys, hy⟩⟩, let i := ((E q).to_fun ⟨y, ys⟩).1, let hi := ((E q).to_fun ⟨y, ys⟩).2, have ihi_eq : (⟨i, hi⟩ : fin (N q)) = (E q).to_fun ⟨y, ys⟩, by rw fin.ext_iff, have hiq : i < N q := hi, have hip : i < N p, { rwa Npq.symm at hiq }, let z := (E p).inv_fun ⟨i, hip⟩, use z, have C1 : (E p).to_fun z = ⟨i, hip⟩ := (E p).right_inv ⟨i, hip⟩, have C2 : fin.cast Npq ⟨i, hip⟩ = ⟨i, hi⟩ := rfl, have C3 : (E q).inv_fun ⟨i, hi⟩ = ⟨y, ys⟩, by { rw ihi_eq, exact (E q).left_inv ⟨y, ys⟩ }, have : Φ z = y := by { simp only [Φ, Ψ], rw [C1, C2, C3], refl }, rw this, exact le_trans (le_of_lt hy) u_le_ε }, show ∀x y : s p, abs (dist x y - dist (Φ x) (Φ y)) ≤ ε, { /- the distance between x and y is encoded in F p, and the distance between Φ x and Φ y (two points of s q) is encoded in F q, all this up to ε. As F p = F q, the distances are almost equal. -/ assume x y, have : dist (Φ x) (Φ y) = dist (Ψ x) (Ψ y) := rfl, rw this, -- introduce i, that codes both x and Φ x in fin (N p) = fin (N q) let i := ((E p).to_fun x).1, have hip : i < N p := ((E p).to_fun x).2, have hiq : i < N q, by rwa Npq at hip, have i' : i = ((E q).to_fun (Ψ x)).1, by { simp [Ψ, (E q).right_inv _] }, -- introduce j, that codes both y and Φ y in fin (N p) = fin (N q) let j := ((E p).to_fun y).1, have hjp : j < N p := ((E p).to_fun y).2, have hjq : j < N q, by rwa Npq at hjp, have j' : j = ((E q).to_fun (Ψ y)).1, by { simp [Ψ, (E q).right_inv _] }, -- Express dist x y in terms of F p have Ap : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = (floor (ε⁻¹ * dist x y)).to_nat := calc ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F p).2 ((E p).to_fun x) ((E p).to_fun y)).1 : by { congr; apply (fin.ext_iff _ _).2; refl } ... = min M (floor (ε⁻¹ * dist x y)).to_nat : by simp only [F, (E p).left_inv _] ... = (floor (ε⁻¹ * dist x y)).to_nat : begin refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos εpos)), change dist (x : p.rep) y ≤ C, refine le_trans (dist_le_diam_of_mem (bounded_of_compact compact_univ) (mem_univ _) (mem_univ _)) _, exact hdiam p pt end, -- Express dist (Φ x) (Φ y) in terms of F q have Aq : ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat := calc ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1 = ((F q).2 ((E q).to_fun (Ψ x)) ((E q).to_fun (Ψ y))).1 : by { congr; apply (fin.ext_iff _ _).2; [exact i', exact j'] } ... = min M (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat : by simp only [F, (E q).left_inv _] ... = (floor (ε⁻¹ * dist (Ψ x) (Ψ y))).to_nat : begin refine min_eq_right (int.to_nat_le_to_nat (floor_mono _)), refine mul_le_mul_of_nonneg_left (le_trans _ (le_max_left _ _)) (le_of_lt (inv_pos εpos)), change dist (Ψ x : q.rep) (Ψ y) ≤ C, refine le_trans (dist_le_diam_of_mem (bounded_of_compact compact_univ) (mem_univ _) (mem_univ _)) _, exact hdiam q qt end, -- use the equality between F p and F q to deduce that the distances have equal -- integer parts have : ((F p).2 ⟨i, hip⟩ ⟨j, hjp⟩).1 = ((F q).2 ⟨i, hiq⟩ ⟨j, hjq⟩).1, { -- we want to `subst hpq` where `hpq : F p = F q`, except that `subst` only works -- with a constant, so replace `F q` (and everything that depends on it) by a constant f -- then subst revert hiq hjq, change N q with (F q).1, generalize_hyp : F q = f at hpq ⊢, subst hpq, intros, refl }, have : floor (ε⁻¹ * dist x y) = floor (ε⁻¹ * dist (Ψ x) (Ψ y)), { rw [Ap, Aq] at this, have D : 0 ≤ floor (ε⁻¹ * dist x y) := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos εpos)) dist_nonneg), have D' : floor (ε⁻¹ * dist (Ψ x) (Ψ y)) ≥ 0 := floor_nonneg.2 (mul_nonneg (le_of_lt (inv_pos εpos)) dist_nonneg), rw [← int.to_nat_of_nonneg D, ← int.to_nat_of_nonneg D', this] }, -- deduce that the distances coincide up to ε, by a straightforward computation -- that should be automated have I := calc abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) = abs (ε⁻¹ * (dist x y - dist (Ψ x) (Ψ y))) : (abs_mul _ _).symm ... = abs ((ε⁻¹ * dist x y) - (ε⁻¹ * dist (Ψ x) (Ψ y))) : by { congr, ring } ... ≤ 1 : le_of_lt (abs_sub_lt_one_of_floor_eq_floor this), calc abs (dist x y - dist (Ψ x) (Ψ y)) = (ε * ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y)) : by rw [mul_inv_cancel (ne_of_gt εpos), one_mul] ... = ε * (abs (ε⁻¹) * abs (dist x y - dist (Ψ x) (Ψ y))) : by rw [abs_of_nonneg (le_of_lt (inv_pos εpos)), mul_assoc] ... ≤ ε * 1 : mul_le_mul_of_nonneg_left I (le_of_lt εpos) ... = ε : mul_one _ } }, calc dist p q = GH_dist (p.rep) (q.rep) : dist_GH_dist p q ... ≤ ε + ε/2 + ε : main ... = δ/2 : by { simp [ε], ring } ... < δ : half_lt_self δpos end section complete /- We will show that a sequence `u n` of compact metric spaces satisfying `dist (u n) (u (n+1)) < 1/2^n` converges, which implies completeness of the Gromov-Hausdorff space. We need to exhibit the limiting compact metric space. For this, start from a sequence `X n` of representatives of `u n`, and glue in an optimal way `X n` to `X (n+1)` for all `n`, in a common metric space. Formally, this is done as follows. Start from `Y 0 = X 0`. Then, glue `X 0` to `X 1` in an optimal way, yielding a space `Y 1` (with an embedding of `X 1`). Then, consider an optimal gluing of `X 1` and `X 2`, and glue it to `Y 1` along their common subspace `X 1`. This gives a new space `Y 2`, with an embedding of `X 2`. Go on, to obtain a sequence of spaces `Y n`. Let `Z0` be the inductive limit of the `Y n`, and finally let `Z` be the completion of `Z0`. The images `X2 n` of `X n` in `Z` are at Hausdorff distance `< 1/2^n` by construction, hence they form a Cauchy sequence for the Hausdorff distance. By completeness (of `Z`, and therefore of its set of nonempty compact subsets), they converge to a limit `L`. This is the nonempty compact metric space we are looking for. -/ variables (X : ℕ → Type) [∀n, metric_space (X n)] [∀n, compact_space (X n)] [∀n, nonempty (X n)] structure aux_gluing_struct (A : Type) [metric_space A] : Type 1 := (space : Type) (metric : metric_space space) (embed : A → space) (isom : isometry embed) def aux_gluing (n : ℕ) : aux_gluing_struct (X n) := nat.rec_on n { space := X 0, metric := by apply_instance, embed := id, isom := λx y, rfl } (λn a, by letI : metric_space a.space := a.metric; exact { space := glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)), metric := metric.metric_space_glue_space a.isom (isometry_optimal_GH_injl (X n) (X n.succ)), embed := (to_glue_r a.isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injr (X n) (X n.succ)), isom := (to_glue_r_isometry _ _).comp (isometry_optimal_GH_injr (X n) (X n.succ)) }) /-- The Gromov-Hausdorff space is complete. -/ instance : complete_space (GH_space) := begin have : ∀ (n : ℕ), 0 < ((1:ℝ) / 2) ^ n, by { apply _root_.pow_pos, norm_num }, -- start from a sequence of nonempty compact metric spaces within distance 1/2^n of each other refine metric.complete_of_convergent_controlled_sequences (λn, (1/2)^n) this (λu hu, _), -- X n is a representative of u n let X := λn, (u n).rep, -- glue them together successively in an optimal way, getting a sequence of metric spaces Y n let Y := aux_gluing X, letI : ∀n, metric_space (Y n).space := λn, (Y n).metric, have E : ∀n:ℕ, glue_space (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)) = (Y n.succ).space := λn, by { simp [Y, aux_gluing], refl }, let c := λn, cast (E n), have ic : ∀n, isometry (c n) := λn x y, rfl, -- there is a canonical embedding of Y n in Y (n+1), by construction let f : Πn, (Y n).space → (Y n.succ).space := λn, (c n) ∘ (to_glue_l (aux_gluing X n).isom (isometry_optimal_GH_injl (X n) (X n.succ))), have I : ∀n, isometry (f n), { assume n, apply isometry.comp, { assume x y, refl }, { apply to_glue_l_isometry } }, -- consider the inductive limit Z0 of the Y n, and then its completion Z let Z0 := metric.inductive_limit I, let Z := uniform_space.completion Z0, let Φ := to_inductive_limit I, let coeZ := (coe : Z0 → Z), -- let X2 n be the image of X n in the space Z let X2 := λn, range (coeZ ∘ (Φ n) ∘ (Y n).embed), have isom : ∀n, isometry (coeZ ∘ (Φ n) ∘ (Y n).embed), { assume n, apply isometry.comp completion.coe_isometry _, apply isometry.comp _ (Y n).isom, apply to_inductive_limit_isometry }, -- The Hausdorff distance of `X2 n` and `X2 (n+1)` is by construction the distance between -- `u n` and `u (n+1)`, therefore bounded by 1/2^n have D2 : ∀n, Hausdorff_dist (X2 n) (X2 n.succ) < (1/2)^n, { assume n, have X2n : X2 n = range ((coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injl (X n) (X n.succ))), { change X2 n = range (coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ))) ∘ (optimal_GH_injl (X n) (X n.succ))), simp only [X2, Φ], rw [← to_inductive_limit_commute I], simp only [f], rw ← to_glue_commute }, rw range_comp at X2n, have X2nsucc : X2 n.succ = range ((coeZ ∘ (Φ n.succ) ∘ (c n) ∘ (to_glue_r (Y n).isom (isometry_optimal_GH_injl (X n) (X n.succ)))) ∘ (optimal_GH_injr (X n) (X n.succ))), by refl, rw range_comp at X2nsucc, rw [X2n, X2nsucc, Hausdorff_dist_image, Hausdorff_dist_optimal, ← dist_GH_dist], { exact hu n n n.succ (le_refl n) (le_succ n) }, { apply isometry.comp completion.coe_isometry _, apply isometry.comp _ ((ic n).comp (to_glue_r_isometry _ _)), apply to_inductive_limit_isometry } }, -- consider `X2 n` as a member `X3 n` of the type of nonempty compact subsets of `Z`, which -- is a metric space let X3 : ℕ → nonempty_compacts Z := λn, ⟨X2 n, ⟨by { simp only [X2, set.range_eq_empty, not_not, ne.def], apply_instance }, compact_range (isom n).continuous ⟩⟩, -- `X3 n` is a Cauchy sequence by construction, as the successive distances are -- bounded by (1/2)^n have : cauchy_seq X3, { refine cauchy_seq_of_le_geometric (1/2) 1 (by norm_num) (λn, _), rw one_mul, exact le_of_lt (D2 n) }, -- therefore, it converges to a limit `L` rcases cauchy_seq_tendsto_of_complete this with ⟨L, hL⟩, -- the images of `X3 n` in the Gromov-Hausdorff space converge to the image of `L` have M : tendsto (λn, (X3 n).to_GH_space) at_top (nhds L.to_GH_space) := tendsto.comp hL (to_GH_space_continuous.tendsto _), -- By construction, the image of `X3 n` in the Gromov-Hausdorff space is `u n`. have : ∀n, (X3 n).to_GH_space = u n, { assume n, rw [nonempty_compacts.to_GH_space, ← (u n).to_GH_space_rep, to_GH_space_eq_to_GH_space_iff_isometric], exact ⟨(isom n).isometric_on_range.symm⟩ }, -- Finally, we have proved the convergence of `u n` exact ⟨L.to_GH_space, by simpa [this] using M⟩ end end complete--section end Gromov_Hausdorff --namespace
b3cc046703c8ea578f5db9ce18db5f42563e19d0
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/data/quotient/classical.lean
7f17d669c9fc4dd7cc7c39d7837dd5bc9154d896
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,048
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn A classical treatment of quotients, using Hilbert choice. -/ import algebra.relation import .basic namespace quotient open relation nonempty subtype classical /- abstract quotient -/ noncomputable definition prelim_map {A : Type} (R : A → A → Prop) (a : A) := -- TODO: it is interesting how the elaborator fails here -- epsilon (fun b, R a b) @epsilon _ (nonempty.intro a) (fun b, R a b) -- TODO: only needed R reflexive (or weaker: R a a) theorem prelim_map_rel {A : Type} {R : A → A → Prop} (H : is_equivalence R) (a : A) : R a (prelim_map R a) := have reflR : reflexive R, from is_equivalence.refl R, epsilon_spec (exists.intro a (reflR a)) -- TODO: only needed: R PER theorem prelim_map_congr {A : Type} {R : A → A → Prop} (H1 : is_equivalence R) {a b : A} (H2 : R a b) : prelim_map R a = prelim_map R b := have symmR : relation.symmetric R, from is_equivalence.symm R, have transR : relation.transitive R, from is_equivalence.trans R, have H3 : ∀c, R a c ↔ R b c, from take c, iff.intro (assume H4 : R a c, transR (symmR H2) H4) (assume H4 : R b c, transR H2 H4), have H4 : (fun c, R a c) = (fun c, R b c), from funext (take c, eq.of_iff (H3 c)), assert H5 : nonempty A, from nonempty.intro a, show epsilon (λc, R a c) = epsilon (λc, R b c), from congr_arg _ H4 noncomputable definition quotient {A : Type} (R : A → A → Prop) : Type := image (prelim_map R) noncomputable definition quotient_abs {A : Type} (R : A → A → Prop) : A → quotient R := fun_image (prelim_map R) noncomputable definition quotient_elt_of {A : Type} (R : A → A → Prop) : quotient R → A := elt_of theorem quotient_is_quotient {A : Type} (R : A → A → Prop) (H : is_equivalence R) : is_quotient R (quotient_abs R) (quotient_elt_of R) := representative_map_to_quotient_equiv H (prelim_map_rel H) (@prelim_map_congr _ _ H) end quotient
578585b7df493d343347d2eba527843ca77d399b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/category/Module/basic.lean
72ea05ddfb8adecafab68e0217b9a4aa2c889277
[ "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
11,183
lean
/- Copyright (c) 2019 Robert A. Spencer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert A. Spencer, Markus Himmel -/ import algebra.category.Group.preadditive import category_theory.linear.basic import category_theory.elementwise import linear_algebra.basic import category_theory.conj import category_theory.preadditive.additive_functor /-! # The category of `R`-modules > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. `Module.{v} R` is the category of bundled `R`-modules with carrier in the universe `v`. We show that it is preadditive and show that being an isomorphism, monomorphism and epimorphism is equivalent to being a linear equivalence, an injective linear map and a surjective linear map, respectively. ## Implementation details To construct an object in the category of `R`-modules from a type `M` with an instance of the `module` typeclass, write `of R M`. There is a coercion in the other direction. Similarly, there is a coercion from morphisms in `Module R` to linear maps. Unfortunately, Lean is not smart enough to see that, given an object `M : Module R`, the expression `of R M`, where we coerce `M` to the carrier type, is definitionally equal to `M` itself. This means that to go the other direction, i.e., from linear maps/equivalences to (iso)morphisms in the category of `R`-modules, we have to take care not to inadvertently end up with an `of R M` where `M` is already an object. Hence, given `f : M →ₗ[R] N`, * if `M N : Module R`, simply use `f`; * if `M : Module R` and `N` is an unbundled `R`-module, use `↿f` or `as_hom_left f`; * if `M` is an unbundled `R`-module and `N : Module R`, use `↾f` or `as_hom_right f`; * if `M` and `N` are unbundled `R`-modules, use `↟f` or `as_hom f`. Similarly, given `f : M ≃ₗ[R] N`, use `to_Module_iso`, `to_Module_iso'_left`, `to_Module_iso'_right` or `to_Module_iso'`, respectively. The arrow notations are localized, so you may have to `open_locale Module` to use them. Note that the notation for `as_hom_left` clashes with the notation used to promote functions between types to morphisms in the category `Type`, so to avoid confusion, it is probably a good idea to avoid having the locales `Module` and `category_theory.Type` open at the same time. If you get an error when trying to apply a theorem and the `convert` tactic produces goals of the form `M = of R M`, then you probably used an incorrect variant of `as_hom` or `to_Module_iso`. -/ open category_theory open category_theory.limits open category_theory.limits.walking_parallel_pair universes v u variables (R : Type u) [ring R] /-- The category of R-modules and their morphisms. Note that in the case of `R = ℤ`, we can not impose here that the `ℤ`-multiplication field from the module structure is defeq to the one coming from the `is_add_comm_group` structure (contrary to what we do for all module structures in mathlib), which creates some difficulties down the road. -/ structure Module := (carrier : Type v) [is_add_comm_group : add_comm_group carrier] [is_module : module R carrier] attribute [instance] Module.is_add_comm_group Module.is_module namespace Module instance : has_coe_to_sort (Module.{v} R) (Type v) := ⟨Module.carrier⟩ instance Module_category : category (Module.{v} R) := { hom := λ M N, M →ₗ[R] N, id := λ M, 1, comp := λ A B C f g, g.comp f, id_comp' := λ X Y f, linear_map.id_comp _, comp_id' := λ X Y f, linear_map.comp_id _, assoc' := λ W X Y Z f g h, linear_map.comp_assoc _ _ _ } instance Module_concrete_category : concrete_category.{v} (Module.{v} R) := { forget := { obj := λ R, R, map := λ R S f, (f : R → S) }, forget_faithful := { } } instance has_forget_to_AddCommGroup : has_forget₂ (Module R) AddCommGroup := { forget₂ := { obj := λ M, AddCommGroup.of M, map := λ M₁ M₂ f, linear_map.to_add_monoid_hom f } } instance (M N : Module R) : linear_map_class (M ⟶ N) R M N := { coe := λ f, f, .. linear_map.semilinear_map_class } /-- The object in the category of R-modules associated to an R-module -/ def of (X : Type v) [add_comm_group X] [module R X] : Module R := ⟨X⟩ @[simp] lemma forget₂_obj (X : Module R) : (forget₂ (Module R) AddCommGroup).obj X = AddCommGroup.of X := rfl @[simp] lemma forget₂_obj_Module_of (X : Type v) [add_comm_group X] [module R X] : (forget₂ (Module R) AddCommGroup).obj (of R X) = AddCommGroup.of X := rfl @[simp] lemma forget₂_map (X Y : Module R) (f : X ⟶ Y) : (forget₂ (Module R) AddCommGroup).map f = linear_map.to_add_monoid_hom f := rfl /-- Typecheck a `linear_map` as a morphism in `Module R`. -/ def of_hom {R : Type u} [ring R] {X Y : Type v} [add_comm_group X] [module R X] [add_comm_group Y] [module R Y] (f : X →ₗ[R] Y) : of R X ⟶ of R Y := f @[simp] lemma of_hom_apply {R : Type u} [ring R] {X Y : Type v} [add_comm_group X] [module R X] [add_comm_group Y] [module R Y] (f : X →ₗ[R] Y) (x : X) : of_hom f x = f x := rfl instance : inhabited (Module R) := ⟨of R punit⟩ instance of_unique {X : Type v} [add_comm_group X] [module R X] [i : unique X] : unique (of R X) := i @[simp] lemma coe_of (X : Type v) [add_comm_group X] [module R X] : (of R X : Type v) = X := rfl variables {R} /-- Forgetting to the underlying type and then building the bundled object returns the original module. -/ @[simps] def of_self_iso (M : Module R) : Module.of R M ≅ M := { hom := 𝟙 M, inv := 𝟙 M } lemma is_zero_of_subsingleton (M : Module R) [subsingleton M] : is_zero M := begin refine ⟨λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩, λ X, ⟨⟨⟨0⟩, λ f, _⟩⟩⟩, { ext, have : x = 0 := subsingleton.elim _ _, rw [this, map_zero, map_zero], }, { ext, apply subsingleton.elim } end instance : has_zero_object (Module.{v} R) := ⟨⟨of R punit, is_zero_of_subsingleton _⟩⟩ variables {R} {M N U : Module.{v} R} @[simp] lemma id_apply (m : M) : (𝟙 M : M → M) m = m := rfl @[simp] lemma coe_comp (f : M ⟶ N) (g : N ⟶ U) : ((f ≫ g) : M → U) = g ∘ f := rfl lemma comp_def (f : M ⟶ N) (g : N ⟶ U) : f ≫ g = g.comp f := rfl end Module variables {R} variables {X₁ X₂ : Type v} /-- Reinterpreting a linear map in the category of `R`-modules. -/ def Module.as_hom [add_comm_group X₁] [module R X₁] [add_comm_group X₂] [module R X₂] : (X₁ →ₗ[R] X₂) → (Module.of R X₁ ⟶ Module.of R X₂) := id localized "notation (name := Module.as_hom) `↟` f : 1024 := Module.as_hom f" in Module /-- Reinterpreting a linear map in the category of `R`-modules. -/ def Module.as_hom_right [add_comm_group X₁] [module R X₁] {X₂ : Module.{v} R} : (X₁ →ₗ[R] X₂) → (Module.of R X₁ ⟶ X₂) := id localized "notation (name := Module.as_hom_right) `↾` f : 1024 := Module.as_hom_right f" in Module /-- Reinterpreting a linear map in the category of `R`-modules. -/ def Module.as_hom_left {X₁ : Module.{v} R} [add_comm_group X₂] [module R X₂] : (X₁ →ₗ[R] X₂) → (X₁ ⟶ Module.of R X₂) := id localized "notation (name := Module.as_hom_left) `↿` f : 1024 := Module.as_hom_left f" in Module /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. -/ @[simps] def linear_equiv.to_Module_iso {g₁ : add_comm_group X₁} {g₂ : add_comm_group X₂} {m₁ : module R X₁} {m₂ : module R X₂} (e : X₁ ≃ₗ[R] X₂) : Module.of R X₁ ≅ Module.of R X₂ := { hom := (e : X₁ →ₗ[R] X₂), inv := (e.symm : X₂ →ₗ[R] X₁), hom_inv_id' := begin ext, exact e.left_inv x, end, inv_hom_id' := begin ext, exact e.right_inv x, end, } /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see `Module.of R M` is defeq to `M` when `M : Module R`. -/ @[simps] def linear_equiv.to_Module_iso' {M N : Module.{v} R} (i : M ≃ₗ[R] N) : M ≅ N := { hom := i, inv := i.symm, hom_inv_id' := linear_map.ext $ λ x, by simp, inv_hom_id' := linear_map.ext $ λ x, by simp } /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see `Module.of R M` is defeq to `M` when `M : Module R`. -/ @[simps] def linear_equiv.to_Module_iso'_left {X₁ : Module.{v} R} {g₂ : add_comm_group X₂} {m₂ : module R X₂} (e : X₁ ≃ₗ[R] X₂) : X₁ ≅ Module.of R X₂ := { hom := (e : X₁ →ₗ[R] X₂), inv := (e.symm : X₂ →ₗ[R] X₁), hom_inv_id' := linear_map.ext $ λ x, by simp, inv_hom_id' := linear_map.ext $ λ x, by simp } /-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see `Module.of R M` is defeq to `M` when `M : Module R`. -/ @[simps] def linear_equiv.to_Module_iso'_right {g₁ : add_comm_group X₁} {m₁ : module R X₁} {X₂ : Module.{v} R} (e : X₁ ≃ₗ[R] X₂) : Module.of R X₁ ≅ X₂ := { hom := (e : X₁ →ₗ[R] X₂), inv := (e.symm : X₂ →ₗ[R] X₁), hom_inv_id' := linear_map.ext $ λ x, by simp, inv_hom_id' := linear_map.ext $ λ x, by simp } namespace category_theory.iso /-- Build a `linear_equiv` from an isomorphism in the category `Module R`. -/ @[simps] def to_linear_equiv {X Y : Module R} (i : X ≅ Y) : X ≃ₗ[R] Y := { to_fun := i.hom, inv_fun := i.inv, left_inv := by tidy, right_inv := by tidy, map_add' := by tidy, map_smul' := by tidy, }. end category_theory.iso /-- linear equivalences between `module`s are the same as (isomorphic to) isomorphisms in `Module` -/ @[simps] def linear_equiv_iso_Module_iso {X Y : Type u} [add_comm_group X] [add_comm_group Y] [module R X] [module R Y] : (X ≃ₗ[R] Y) ≅ (Module.of R X ≅ Module.of R Y) := { hom := λ e, e.to_Module_iso, inv := λ i, i.to_linear_equiv, } namespace Module instance : preadditive (Module.{v} R) := { add_comp' := λ P Q R f f' g, show (f + f') ≫ g = f ≫ g + f' ≫ g, by { ext, simp }, comp_add' := λ P Q R f g g', show f ≫ (g + g') = f ≫ g + f ≫ g', by { ext, simp } } instance forget₂_AddCommGroup_additive : (forget₂ (Module.{v} R) AddCommGroup).additive := {} section variables {S : Type u} [comm_ring S] instance : linear S (Module.{v} S) := { hom_module := λ X Y, linear_map.module, smul_comp' := by { intros, ext, simp }, comp_smul' := by { intros, ext, simp }, } variables {X Y X' Y' : Module.{v} S} lemma iso.hom_congr_eq_arrow_congr (i : X ≅ X') (j : Y ≅ Y') (f : X ⟶ Y) : iso.hom_congr i j f = linear_equiv.arrow_congr i.to_linear_equiv j.to_linear_equiv f := rfl lemma iso.conj_eq_conj (i : X ≅ X') (f : End X) : iso.conj i f = linear_equiv.conj i.to_linear_equiv f := rfl end end Module instance (M : Type u) [add_comm_group M] [module R M] : has_coe (submodule R M) (Module R) := ⟨ λ N, Module.of R N ⟩
9c545f5ec9ebab93c2fd3ecf3b9fe4b0ed897253
9dc8cecdf3c4634764a18254e94d43da07142918
/src/probability/stopping.lean
93263f9bc8810271ac2b3be72a40f4c3107de385
[ "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
65,957
lean
/- Copyright (c) 2021 Kexing Ying. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying -/ import measure_theory.function.conditional_expectation.real import topology.instances.discrete /-! # Filtration and stopping time This file defines some standard definition from the theory of stochastic processes including filtrations and stopping times. These definitions are used to model the amount of information at a specific time and is the first step in formalizing stochastic processes. ## Main definitions * `measure_theory.filtration`: a filtration on a measurable space * `measure_theory.adapted`: a sequence of functions `u` is said to be adapted to a filtration `f` if at each point in time `i`, `u i` is `f i`-strongly measurable * `measure_theory.prog_measurable`: a sequence of functions `u` is said to be progressively measurable with respect to a filtration `f` if at each point in time `i`, `u` restricted to `set.Iic i × Ω` is strongly measurable with respect to the product `measurable_space` structure where the σ-algebra used for `Ω` is `f i`. * `measure_theory.filtration.natural`: the natural filtration with respect to a sequence of measurable functions is the smallest filtration to which it is adapted to * `measure_theory.is_stopping_time`: a stopping time with respect to some filtration `f` is a function `τ` such that for all `i`, the preimage of `{j | j ≤ i}` along `τ` is `f i`-measurable * `measure_theory.is_stopping_time.measurable_space`: the σ-algebra associated with a stopping time ## Main results * `adapted.prog_measurable_of_continuous`: a continuous adapted process is progressively measurable. * `prog_measurable.stopped_process`: the stopped process of a progressively measurable process is progressively measurable. * `mem_ℒp_stopped_process`: if a process belongs to `ℒp` at every time in `ℕ`, then its stopped process belongs to `ℒp` as well. ## Tags filtration, stopping time, stochastic process -/ open filter order topological_space open_locale classical measure_theory nnreal ennreal topological_space big_operators namespace measure_theory /-! ### Filtrations -/ /-- A `filtration` on measurable space `Ω` with σ-algebra `m` is a monotone sequence of sub-σ-algebras of `m`. -/ structure filtration {Ω : Type*} (ι : Type*) [preorder ι] (m : measurable_space Ω) := (seq : ι → measurable_space Ω) (mono' : monotone seq) (le' : ∀ i : ι, seq i ≤ m) variables {Ω β ι : Type*} {m : measurable_space Ω} instance [preorder ι] : has_coe_to_fun (filtration ι m) (λ _, ι → measurable_space Ω) := ⟨λ f, f.seq⟩ namespace filtration variables [preorder ι] protected lemma mono {i j : ι} (f : filtration ι m) (hij : i ≤ j) : f i ≤ f j := f.mono' hij protected lemma le (f : filtration ι m) (i : ι) : f i ≤ m := f.le' i @[ext] protected lemma ext {f g : filtration ι m} (h : (f : ι → measurable_space Ω) = g) : f = g := by { cases f, cases g, simp only, exact h, } variable (ι) /-- The constant filtration which is equal to `m` for all `i : ι`. -/ def const (m' : measurable_space Ω) (hm' : m' ≤ m) : filtration ι m := ⟨λ _, m', monotone_const, λ _, hm'⟩ variable {ι} @[simp] lemma const_apply {m' : measurable_space Ω} {hm' : m' ≤ m} (i : ι) : const ι m' hm' i = m' := rfl instance : inhabited (filtration ι m) := ⟨const ι m le_rfl⟩ instance : has_le (filtration ι m) := ⟨λ f g, ∀ i, f i ≤ g i⟩ instance : has_bot (filtration ι m) := ⟨const ι ⊥ bot_le⟩ instance : has_top (filtration ι m) := ⟨const ι m le_rfl⟩ instance : has_sup (filtration ι m) := ⟨λ f g, { seq := λ i, f i ⊔ g i, mono' := λ i j hij, sup_le ((f.mono hij).trans le_sup_left) ((g.mono hij).trans le_sup_right), le' := λ i, sup_le (f.le i) (g.le i) }⟩ @[norm_cast] lemma coe_fn_sup {f g : filtration ι m} : ⇑(f ⊔ g) = f ⊔ g := rfl instance : has_inf (filtration ι m) := ⟨λ f g, { seq := λ i, f i ⊓ g i, mono' := λ i j hij, le_inf (inf_le_left.trans (f.mono hij)) (inf_le_right.trans (g.mono hij)), le' := λ i, inf_le_left.trans (f.le i) }⟩ @[norm_cast] lemma coe_fn_inf {f g : filtration ι m} : ⇑(f ⊓ g) = f ⊓ g := rfl instance : has_Sup (filtration ι m) := ⟨λ s, { seq := λ i, Sup ((λ f : filtration ι m, f i) '' s), mono' := λ i j hij, begin refine Sup_le (λ m' hm', _), rw [set.mem_image] at hm', obtain ⟨f, hf_mem, hfm'⟩ := hm', rw ← hfm', refine (f.mono hij).trans _, have hfj_mem : f j ∈ ((λ g : filtration ι m, g j) '' s), from ⟨f, hf_mem, rfl⟩, exact le_Sup hfj_mem, end, le' := λ i, begin refine Sup_le (λ m' hm', _), rw [set.mem_image] at hm', obtain ⟨f, hf_mem, hfm'⟩ := hm', rw ← hfm', exact f.le i, end, }⟩ lemma Sup_def (s : set (filtration ι m)) (i : ι) : Sup s i = Sup ((λ f : filtration ι m, f i) '' s) := rfl noncomputable instance : has_Inf (filtration ι m) := ⟨λ s, { seq := λ i, if set.nonempty s then Inf ((λ f : filtration ι m, f i) '' s) else m, mono' := λ i j hij, begin by_cases h_nonempty : set.nonempty s, swap, { simp only [h_nonempty, set.nonempty_image_iff, if_false, le_refl], }, simp only [h_nonempty, if_true, le_Inf_iff, set.mem_image, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂], refine λ f hf_mem, le_trans _ (f.mono hij), have hfi_mem : f i ∈ ((λ g : filtration ι m, g i) '' s), from ⟨f, hf_mem, rfl⟩, exact Inf_le hfi_mem, end, le' := λ i, begin by_cases h_nonempty : set.nonempty s, swap, { simp only [h_nonempty, if_false, le_refl], }, simp only [h_nonempty, if_true], obtain ⟨f, hf_mem⟩ := h_nonempty, exact le_trans (Inf_le ⟨f, hf_mem, rfl⟩) (f.le i), end, }⟩ lemma Inf_def (s : set (filtration ι m)) (i : ι) : Inf s i = if set.nonempty s then Inf ((λ f : filtration ι m, f i) '' s) else m := rfl noncomputable instance : complete_lattice (filtration ι m) := { le := (≤), le_refl := λ f i, le_rfl, le_trans := λ f g h h_fg h_gh i, (h_fg i).trans (h_gh i), le_antisymm := λ f g h_fg h_gf, filtration.ext $ funext $ λ i, (h_fg i).antisymm (h_gf i), sup := (⊔), le_sup_left := λ f g i, le_sup_left, le_sup_right := λ f g i, le_sup_right, sup_le := λ f g h h_fh h_gh i, sup_le (h_fh i) (h_gh _), inf := (⊓), inf_le_left := λ f g i, inf_le_left, inf_le_right := λ f g i, inf_le_right, le_inf := λ f g h h_fg h_fh i, le_inf (h_fg i) (h_fh i), Sup := Sup, le_Sup := λ s f hf_mem i, le_Sup ⟨f, hf_mem, rfl⟩, Sup_le := λ s f h_forall i, Sup_le $ λ m' hm', begin obtain ⟨g, hg_mem, hfm'⟩ := hm', rw ← hfm', exact h_forall g hg_mem i, end, Inf := Inf, Inf_le := λ s f hf_mem i, begin have hs : s.nonempty := ⟨f, hf_mem⟩, simp only [Inf_def, hs, if_true], exact Inf_le ⟨f, hf_mem, rfl⟩, end, le_Inf := λ s f h_forall i, begin by_cases hs : s.nonempty, swap, { simp only [Inf_def, hs, if_false], exact f.le i, }, simp only [Inf_def, hs, if_true, le_Inf_iff, set.mem_image, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂], exact λ g hg_mem, h_forall g hg_mem i, end, top := ⊤, bot := ⊥, le_top := λ f i, f.le' i, bot_le := λ f i, bot_le, } end filtration lemma measurable_set_of_filtration [preorder ι] {f : filtration ι m} {s : set Ω} {i : ι} (hs : measurable_set[f i] s) : measurable_set[m] s := f.le i s hs /-- A measure is σ-finite with respect to filtration if it is σ-finite with respect to all the sub-σ-algebra of the filtration. -/ class sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m) : Prop := (sigma_finite : ∀ i : ι, sigma_finite (μ.trim (f.le i))) instance sigma_finite_of_sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m) [hf : sigma_finite_filtration μ f] (i : ι) : sigma_finite (μ.trim (f.le i)) := by apply hf.sigma_finite -- can't exact here @[priority 100] instance is_finite_measure.sigma_finite_filtration [preorder ι] (μ : measure Ω) (f : filtration ι m) [is_finite_measure μ] : sigma_finite_filtration μ f := ⟨λ n, by apply_instance⟩ /-- Given a integrable function `g`, the conditional expectations of `g` with respect to a filtration is uniformly integrable. -/ lemma integrable.uniform_integrable_condexp_filtration [preorder ι] {μ : measure Ω} [is_finite_measure μ] {f : filtration ι m} {g : Ω → ℝ} (hg : integrable g μ) : uniform_integrable (λ i, μ[g | f i]) 1 μ := hg.uniform_integrable_condexp f.le section adapted_process variables [topological_space β] [preorder ι] {u v : ι → Ω → β} {f : filtration ι m} /-- A sequence of functions `u` is adapted to a filtration `f` if for all `i`, `u i` is `f i`-measurable. -/ def adapted (f : filtration ι m) (u : ι → Ω → β) : Prop := ∀ i : ι, strongly_measurable[f i] (u i) namespace adapted @[protected, to_additive] lemma mul [has_mul β] [has_continuous_mul β] (hu : adapted f u) (hv : adapted f v) : adapted f (u * v) := λ i, (hu i).mul (hv i) @[protected, to_additive] lemma div [has_div β] [has_continuous_div β] (hu : adapted f u) (hv : adapted f v) : adapted f (u / v) := λ i, (hu i).div (hv i) @[protected, to_additive] lemma inv [group β] [topological_group β] (hu : adapted f u) : adapted f u⁻¹ := λ i, (hu i).inv @[protected] lemma smul [has_smul ℝ β] [has_continuous_smul ℝ β] (c : ℝ) (hu : adapted f u) : adapted f (c • u) := λ i, (hu i).const_smul c @[protected] lemma strongly_measurable {i : ι} (hf : adapted f u) : strongly_measurable[m] (u i) := (hf i).mono (f.le i) lemma strongly_measurable_le {i j : ι} (hf : adapted f u) (hij : i ≤ j) : strongly_measurable[f j] (u i) := (hf i).mono (f.mono hij) end adapted lemma adapted_const (f : filtration ι m) (x : β) : adapted f (λ _ _, x) := λ i, strongly_measurable_const variable (β) lemma adapted_zero [has_zero β] (f : filtration ι m) : adapted f (0 : ι → Ω → β) := λ i, @strongly_measurable_zero Ω β (f i) _ _ variable {β} /-- Progressively measurable process. A sequence of functions `u` is said to be progressively measurable with respect to a filtration `f` if at each point in time `i`, `u` restricted to `set.Iic i × Ω` is measurable with respect to the product `measurable_space` structure where the σ-algebra used for `Ω` is `f i`. The usual definition uses the interval `[0,i]`, which we replace by `set.Iic i`. We recover the usual definition for index types `ℝ≥0` or `ℕ`. -/ def prog_measurable [measurable_space ι] (f : filtration ι m) (u : ι → Ω → β) : Prop := ∀ i, strongly_measurable[subtype.measurable_space.prod (f i)] (λ p : set.Iic i × Ω, u p.1 p.2) lemma prog_measurable_const [measurable_space ι] (f : filtration ι m) (b : β) : prog_measurable f ((λ _ _, b) : ι → Ω → β) := λ i, @strongly_measurable_const _ _ (subtype.measurable_space.prod (f i)) _ _ namespace prog_measurable variables [measurable_space ι] protected lemma adapted (h : prog_measurable f u) : adapted f u := begin intro i, have : u i = (λ p : set.Iic i × Ω, u p.1 p.2) ∘ (λ x, (⟨i, set.mem_Iic.mpr le_rfl⟩, x)) := rfl, rw this, exact (h i).comp_measurable measurable_prod_mk_left, end protected lemma comp {t : ι → Ω → ι} [topological_space ι] [borel_space ι] [metrizable_space ι] (h : prog_measurable f u) (ht : prog_measurable f t) (ht_le : ∀ i ω, t i ω ≤ i) : prog_measurable f (λ i ω, u (t i ω) ω) := begin intro i, have : (λ p : ↥(set.Iic i) × Ω, u (t (p.fst : ι) p.snd) p.snd) = (λ p : ↥(set.Iic i) × Ω, u (p.fst : ι) p.snd) ∘ (λ p : ↥(set.Iic i) × Ω, (⟨t (p.fst : ι) p.snd, set.mem_Iic.mpr ((ht_le _ _).trans p.fst.prop)⟩, p.snd)) := rfl, rw this, exact (h i).comp_measurable ((ht i).measurable.subtype_mk.prod_mk measurable_snd), end section arithmetic @[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β] (hu : prog_measurable f u) (hv : prog_measurable f v) : prog_measurable f (λ i ω, u i ω * v i ω) := λ i, (hu i).mul (hv i) @[to_additive] protected lemma finset_prod' {γ} [comm_monoid β] [has_continuous_mul β] {U : γ → ι → Ω → β} {s : finset γ} (h : ∀ c ∈ s, prog_measurable f (U c)) : prog_measurable f (∏ c in s, U c) := finset.prod_induction U (prog_measurable f) (λ _ _, prog_measurable.mul) (prog_measurable_const _ 1) h @[to_additive] protected lemma finset_prod {γ} [comm_monoid β] [has_continuous_mul β] {U : γ → ι → Ω → β} {s : finset γ} (h : ∀ c ∈ s, prog_measurable f (U c)) : prog_measurable f (λ i a, ∏ c in s, U c i a) := by { convert prog_measurable.finset_prod' h, ext i a, simp only [finset.prod_apply], } @[to_additive] protected lemma inv [group β] [topological_group β] (hu : prog_measurable f u) : prog_measurable f (λ i ω, (u i ω)⁻¹) := λ i, (hu i).inv @[to_additive] protected lemma div [group β] [topological_group β] (hu : prog_measurable f u) (hv : prog_measurable f v) : prog_measurable f (λ i ω, u i ω / v i ω) := λ i, (hu i).div (hv i) end arithmetic end prog_measurable lemma prog_measurable_of_tendsto' {γ} [measurable_space ι] [metrizable_space β] (fltr : filter γ) [fltr.ne_bot] [fltr.is_countably_generated] {U : γ → ι → Ω → β} (h : ∀ l, prog_measurable f (U l)) (h_tendsto : tendsto U fltr (𝓝 u)) : prog_measurable f u := begin assume i, apply @strongly_measurable_of_tendsto (set.Iic i × Ω) β γ (measurable_space.prod _ (f i)) _ _ fltr _ _ _ _ (λ l, h l i), rw tendsto_pi_nhds at h_tendsto ⊢, intro x, specialize h_tendsto x.fst, rw tendsto_nhds at h_tendsto ⊢, exact λ s hs h_mem, h_tendsto {g | g x.snd ∈ s} (hs.preimage (continuous_apply x.snd)) h_mem, end lemma prog_measurable_of_tendsto [measurable_space ι] [metrizable_space β] {U : ℕ → ι → Ω → β} (h : ∀ l, prog_measurable f (U l)) (h_tendsto : tendsto U at_top (𝓝 u)) : prog_measurable f u := prog_measurable_of_tendsto' at_top h h_tendsto /-- A continuous and adapted process is progressively measurable. -/ theorem adapted.prog_measurable_of_continuous [topological_space ι] [metrizable_space ι] [measurable_space ι] [second_countable_topology ι] [opens_measurable_space ι] [metrizable_space β] (h : adapted f u) (hu_cont : ∀ ω, continuous (λ i, u i ω)) : prog_measurable f u := λ i, @strongly_measurable_uncurry_of_continuous_of_strongly_measurable _ _ (set.Iic i) _ _ _ _ _ _ _ (f i) _ (λ ω, (hu_cont ω).comp continuous_induced_dom) (λ j, (h j).mono (f.mono j.prop)) end adapted_process namespace filtration variables [topological_space β] [metrizable_space β] [mβ : measurable_space β] [borel_space β] [preorder ι] include mβ /-- Given a sequence of functions, the natural filtration is the smallest sequence of σ-algebras such that that sequence of functions is measurable with respect to the filtration. -/ def natural (u : ι → Ω → β) (hum : ∀ i, strongly_measurable (u i)) : filtration ι m := { seq := λ i, ⨆ j ≤ i, measurable_space.comap (u j) mβ, mono' := λ i j hij, bsupr_mono $ λ k, ge_trans hij, le' := λ i, begin refine supr₂_le _, rintros j hj s ⟨t, ht, rfl⟩, exact (hum j).measurable ht, end } lemma adapted_natural {u : ι → Ω → β} (hum : ∀ i, strongly_measurable[m] (u i)) : adapted (natural u hum) u := begin assume i, refine strongly_measurable.mono _ (le_supr₂_of_le i (le_refl i) le_rfl), rw strongly_measurable_iff_measurable_separable, exact ⟨measurable_iff_comap_le.2 le_rfl, (hum i).is_separable_range⟩ end section limit omit mβ variables {E : Type*} [has_zero E] [topological_space E] {ℱ : filtration ι m} {f : ι → Ω → E} {μ : measure Ω} /-- Given a process `f` and a filtration `ℱ`, if `f` converges to some `g` almost everywhere and `g` is `⨆ n, ℱ n`-measurable, then `limit_process f ℱ μ` chooses said `g`, else it returns 0. This definition is used to phrase the a.e. martingale convergence theorem `submartingale.ae_tendsto_limit_process` where an L¹-bounded submartingale `f` adapted to `ℱ` converges to `limit_process f ℱ μ` `μ`-almost everywhere. -/ noncomputable def limit_process (f : ι → Ω → E) (ℱ : filtration ι m) (μ : measure Ω . volume_tac) := if h : ∃ g : Ω → E, strongly_measurable[⨆ n, ℱ n] g ∧ ∀ᵐ ω ∂μ, tendsto (λ n, f n ω) at_top (𝓝 (g ω)) then classical.some h else 0 lemma strongly_measurable_limit_process : strongly_measurable[⨆ n, ℱ n] (limit_process f ℱ μ) := begin rw limit_process, split_ifs with h h, exacts [(classical.some_spec h).1, strongly_measurable_zero] end lemma strongly_measurable_limit_process' : strongly_measurable[m] (limit_process f ℱ μ) := strongly_measurable_limit_process.mono (Sup_le (λ m ⟨n, hn⟩, hn ▸ ℱ.le _)) lemma mem_ℒp_limit_process_of_snorm_bdd {R : ℝ≥0} {p : ℝ≥0∞} {F : Type*} [normed_add_comm_group F] {ℱ : filtration ℕ m} {f : ℕ → Ω → F} (hfm : ∀ n, ae_strongly_measurable (f n) μ) (hbdd : ∀ n, snorm (f n) p μ ≤ R) : mem_ℒp (limit_process f ℱ μ) p μ := begin rw limit_process, split_ifs with h, { refine ⟨strongly_measurable.ae_strongly_measurable ((classical.some_spec h).1.mono (Sup_le (λ m ⟨n, hn⟩, hn ▸ ℱ.le _))), lt_of_le_of_lt (Lp.snorm_lim_le_liminf_snorm hfm _ (classical.some_spec h).2) (lt_of_le_of_lt _ (ennreal.coe_lt_top : ↑R < ∞))⟩, simp_rw [liminf_eq, eventually_at_top], exact Sup_le (λ b ⟨a, ha⟩, (ha a le_rfl).trans (hbdd _)) }, { exact zero_mem_ℒp } end end limit end filtration /-! ### Stopping times -/ /-- A stopping time with respect to some filtration `f` is a function `τ` such that for all `i`, the preimage of `{j | j ≤ i}` along `τ` is measurable with respect to `f i`. Intuitively, the stopping time `τ` describes some stopping rule such that at time `i`, we may determine it with the information we have at time `i`. -/ def is_stopping_time [preorder ι] (f : filtration ι m) (τ : Ω → ι) := ∀ i : ι, measurable_set[f i] $ {ω | τ ω ≤ i} lemma is_stopping_time_const [preorder ι] (f : filtration ι m) (i : ι) : is_stopping_time f (λ ω, i) := λ j, by simp only [measurable_set.const] section measurable_set section preorder variables [preorder ι] {f : filtration ι m} {τ : Ω → ι} protected lemma is_stopping_time.measurable_set_le (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω ≤ i} := hτ i lemma is_stopping_time.measurable_set_lt_of_pred [pred_order ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω < i} := begin by_cases hi_min : is_min i, { suffices : {ω : Ω | τ ω < i} = ∅, by { rw this, exact @measurable_set.empty _ (f i), }, ext1 ω, simp only [set.mem_set_of_eq, set.mem_empty_eq, iff_false], rw is_min_iff_forall_not_lt at hi_min, exact hi_min (τ ω), }, have : {ω : Ω | τ ω < i} = τ ⁻¹' (set.Iio i) := rfl, rw [this, ←Iic_pred_of_not_is_min hi_min], exact f.mono (pred_le i) _ (hτ.measurable_set_le $ pred i), end end preorder section countable_stopping_time namespace is_stopping_time variables [partial_order ι] {τ : Ω → ι} {f : filtration ι m} protected lemma measurable_set_eq_of_countable_range (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[f i] {ω | τ ω = i} := begin have : {ω | τ ω = i} = {ω | τ ω ≤ i} \ (⋃ (j ∈ set.range τ) (hj : j < i), {ω | τ ω ≤ j}), { ext1 a, simp only [set.mem_set_of_eq, set.mem_range, set.Union_exists, set.Union_Union_eq', set.mem_diff, set.mem_Union, exists_prop, not_exists, not_and, not_le], split; intro h, { simp only [h, lt_iff_le_not_le, le_refl, and_imp, imp_self, implies_true_iff, and_self], }, { have h_lt_or_eq : τ a < i ∨ τ a = i := lt_or_eq_of_le h.1, rcases h_lt_or_eq with h_lt | rfl, { exfalso, exact h.2 a h_lt (le_refl (τ a)), }, { refl, }, }, }, rw this, refine (hτ.measurable_set_le i).diff _, refine measurable_set.bUnion h_countable (λ j hj, _), by_cases hji : j < i, { simp only [hji, set.Union_true], exact f.mono hji.le _ (hτ.measurable_set_le j), }, { simp only [hji, set.Union_false], exact @measurable_set.empty _ (f i), }, end protected lemma measurable_set_eq_of_countable [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω = i} := hτ.measurable_set_eq_of_countable_range (set.to_countable _) i protected lemma measurable_set_lt_of_countable_range (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[f i] {ω | τ ω < i} := begin have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i}, { ext1 ω, simp [lt_iff_le_and_ne], }, rw this, exact (hτ.measurable_set_le i).diff (hτ.measurable_set_eq_of_countable_range h_countable i), end protected lemma measurable_set_lt_of_countable [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω < i} := hτ.measurable_set_lt_of_countable_range (set.to_countable _) i protected lemma measurable_set_ge_of_countable_range {ι} [linear_order ι] {τ : Ω → ι} {f : filtration ι m} (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[f i] {ω | i ≤ τ ω} := begin have : {ω | i ≤ τ ω} = {ω | τ ω < i}ᶜ, { ext1 ω, simp only [set.mem_set_of_eq, set.mem_compl_eq, not_lt], }, rw this, exact (hτ.measurable_set_lt_of_countable_range h_countable i).compl, end protected lemma measurable_set_ge_of_countable {ι} [linear_order ι] {τ : Ω → ι} {f : filtration ι m} [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | i ≤ τ ω} := hτ.measurable_set_ge_of_countable_range (set.to_countable _) i end is_stopping_time end countable_stopping_time section linear_order variables [linear_order ι] {f : filtration ι m} {τ : Ω → ι} lemma is_stopping_time.measurable_set_gt (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | i < τ ω} := begin have : {ω | i < τ ω} = {ω | τ ω ≤ i}ᶜ, { ext1 ω, simp only [set.mem_set_of_eq, set.mem_compl_eq, not_le], }, rw this, exact (hτ.measurable_set_le i).compl, end section topological_space variables [topological_space ι] [order_topology ι] [first_countable_topology ι] /-- Auxiliary lemma for `is_stopping_time.measurable_set_lt`. -/ lemma is_stopping_time.measurable_set_lt_of_is_lub (hτ : is_stopping_time f τ) (i : ι) (h_lub : is_lub (set.Iio i) i) : measurable_set[f i] {ω | τ ω < i} := begin by_cases hi_min : is_min i, { suffices : {ω | τ ω < i} = ∅, by { rw this, exact @measurable_set.empty _ (f i), }, ext1 ω, simp only [set.mem_set_of_eq, set.mem_empty_eq, iff_false], exact is_min_iff_forall_not_lt.mp hi_min (τ ω), }, obtain ⟨seq, -, -, h_tendsto, h_bound⟩ : ∃ seq : ℕ → ι, monotone seq ∧ (∀ j, seq j ≤ i) ∧ tendsto seq at_top (𝓝 i) ∧ (∀ j, seq j < i), from h_lub.exists_seq_monotone_tendsto (not_is_min_iff.mp hi_min), have h_Ioi_eq_Union : set.Iio i = ⋃ j, {k | k ≤ seq j}, { ext1 k, simp only [set.mem_Iio, set.mem_Union, set.mem_set_of_eq], refine ⟨λ hk_lt_i, _, λ h_exists_k_le_seq, _⟩, { rw tendsto_at_top' at h_tendsto, have h_nhds : set.Ici k ∈ 𝓝 i, from mem_nhds_iff.mpr ⟨set.Ioi k, set.Ioi_subset_Ici le_rfl, is_open_Ioi, hk_lt_i⟩, obtain ⟨a, ha⟩ : ∃ (a : ℕ), ∀ (b : ℕ), b ≥ a → k ≤ seq b := h_tendsto (set.Ici k) h_nhds, exact ⟨a, ha a le_rfl⟩, }, { obtain ⟨j, hk_seq_j⟩ := h_exists_k_le_seq, exact hk_seq_j.trans_lt (h_bound j), }, }, have h_lt_eq_preimage : {ω | τ ω < i} = τ ⁻¹' (set.Iio i), { ext1 ω, simp only [set.mem_set_of_eq, set.mem_preimage, set.mem_Iio], }, rw [h_lt_eq_preimage, h_Ioi_eq_Union], simp only [set.preimage_Union, set.preimage_set_of_eq], exact measurable_set.Union (λ n, f.mono (h_bound n).le _ (hτ.measurable_set_le (seq n))), end lemma is_stopping_time.measurable_set_lt (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω < i} := begin obtain ⟨i', hi'_lub⟩ : ∃ i', is_lub (set.Iio i) i', from exists_lub_Iio i, cases lub_Iio_eq_self_or_Iio_eq_Iic i hi'_lub with hi'_eq_i h_Iio_eq_Iic, { rw ← hi'_eq_i at hi'_lub ⊢, exact hτ.measurable_set_lt_of_is_lub i' hi'_lub, }, { have h_lt_eq_preimage : {ω : Ω | τ ω < i} = τ ⁻¹' (set.Iio i) := rfl, rw [h_lt_eq_preimage, h_Iio_eq_Iic], exact f.mono (lub_Iio_le i hi'_lub) _ (hτ.measurable_set_le i'), }, end lemma is_stopping_time.measurable_set_ge (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | i ≤ τ ω} := begin have : {ω | i ≤ τ ω} = {ω | τ ω < i}ᶜ, { ext1 ω, simp only [set.mem_set_of_eq, set.mem_compl_eq, not_lt], }, rw this, exact (hτ.measurable_set_lt i).compl, end lemma is_stopping_time.measurable_set_eq (hτ : is_stopping_time f τ) (i : ι) : measurable_set[f i] {ω | τ ω = i} := begin have : {ω | τ ω = i} = {ω | τ ω ≤ i} ∩ {ω | τ ω ≥ i}, { ext1 ω, simp only [set.mem_set_of_eq, ge_iff_le, set.mem_inter_eq, le_antisymm_iff], }, rw this, exact (hτ.measurable_set_le i).inter (hτ.measurable_set_ge i), end lemma is_stopping_time.measurable_set_eq_le (hτ : is_stopping_time f τ) {i j : ι} (hle : i ≤ j) : measurable_set[f j] {ω | τ ω = i} := f.mono hle _ $ hτ.measurable_set_eq i lemma is_stopping_time.measurable_set_lt_le (hτ : is_stopping_time f τ) {i j : ι} (hle : i ≤ j) : measurable_set[f j] {ω | τ ω < i} := f.mono hle _ $ hτ.measurable_set_lt i end topological_space end linear_order section countable lemma is_stopping_time_of_measurable_set_eq [preorder ι] [countable ι] {f : filtration ι m} {τ : Ω → ι} (hτ : ∀ i, measurable_set[f i] {ω | τ ω = i}) : is_stopping_time f τ := begin intro i, rw show {ω | τ ω ≤ i} = ⋃ k ≤ i, {ω | τ ω = k}, by { ext, simp }, refine measurable_set.bUnion (set.to_countable _) (λ k hk, _), exact f.mono hk _ (hτ k), end end countable end measurable_set namespace is_stopping_time protected lemma max [linear_order ι] {f : filtration ι m} {τ π : Ω → ι} (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : is_stopping_time f (λ ω, max (τ ω) (π ω)) := begin intro i, simp_rw [max_le_iff, set.set_of_and], exact (hτ i).inter (hπ i), end protected lemma max_const [linear_order ι] {f : filtration ι m} {τ : Ω → ι} (hτ : is_stopping_time f τ) (i : ι) : is_stopping_time f (λ ω, max (τ ω) i) := hτ.max (is_stopping_time_const f i) protected lemma min [linear_order ι] {f : filtration ι m} {τ π : Ω → ι} (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : is_stopping_time f (λ ω, min (τ ω) (π ω)) := begin intro i, simp_rw [min_le_iff, set.set_of_or], exact (hτ i).union (hπ i), end protected lemma min_const [linear_order ι] {f : filtration ι m} {τ : Ω → ι} (hτ : is_stopping_time f τ) (i : ι) : is_stopping_time f (λ ω, min (τ ω) i) := hτ.min (is_stopping_time_const f i) lemma add_const [add_group ι] [preorder ι] [covariant_class ι ι (function.swap (+)) (≤)] [covariant_class ι ι (+) (≤)] {f : filtration ι m} {τ : Ω → ι} (hτ : is_stopping_time f τ) {i : ι} (hi : 0 ≤ i) : is_stopping_time f (λ ω, τ ω + i) := begin intro j, simp_rw [← le_sub_iff_add_le], exact f.mono (sub_le_self j hi) _ (hτ (j - i)), end lemma add_const_nat {f : filtration ℕ m} {τ : Ω → ℕ} (hτ : is_stopping_time f τ) {i : ℕ} : is_stopping_time f (λ ω, τ ω + i) := begin refine is_stopping_time_of_measurable_set_eq (λ j, _), by_cases hij : i ≤ j, { simp_rw [eq_comm, ← nat.sub_eq_iff_eq_add hij, eq_comm], exact f.mono (j.sub_le i) _ (hτ.measurable_set_eq (j - i)) }, { rw not_le at hij, convert measurable_set.empty, ext ω, simp only [set.mem_empty_eq, iff_false], rintro (hx : τ ω + i = j), linarith }, end -- generalize to certain countable type? lemma add {f : filtration ℕ m} {τ π : Ω → ℕ} (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : is_stopping_time f (τ + π) := begin intro i, rw (_ : {ω | (τ + π) ω ≤ i} = ⋃ k ≤ i, {ω | π ω = k} ∩ {ω | τ ω + k ≤ i}), { exact measurable_set.Union (λ k, measurable_set.Union (λ hk, (hπ.measurable_set_eq_le hk).inter (hτ.add_const_nat i))) }, ext ω, simp only [pi.add_apply, set.mem_set_of_eq, set.mem_Union, set.mem_inter_eq, exists_prop], refine ⟨λ h, ⟨π ω, by linarith, rfl, h⟩, _⟩, rintro ⟨j, hj, rfl, h⟩, assumption end section preorder variables [preorder ι] {f : filtration ι m} {τ π : Ω → ι} /-- The associated σ-algebra with a stopping time. -/ protected def measurable_space (hτ : is_stopping_time f τ) : measurable_space Ω := { measurable_set' := λ s, ∀ i : ι, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}), measurable_set_empty := λ i, (set.empty_inter {ω | τ ω ≤ i}).symm ▸ @measurable_set.empty _ (f i), measurable_set_compl := λ s hs i, begin rw (_ : sᶜ ∩ {ω | τ ω ≤ i} = (sᶜ ∪ {ω | τ ω ≤ i}ᶜ) ∩ {ω | τ ω ≤ i}), { refine measurable_set.inter _ _, { rw ← set.compl_inter, exact (hs i).compl }, { exact hτ i} }, { rw set.union_inter_distrib_right, simp only [set.compl_inter_self, set.union_empty] } end, measurable_set_Union := λ s hs i, begin rw forall_swap at hs, rw set.Union_inter, exact measurable_set.Union (hs i), end } protected lemma measurable_set (hτ : is_stopping_time f τ) (s : set Ω) : measurable_set[hτ.measurable_space] s ↔ ∀ i : ι, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}) := iff.rfl lemma measurable_space_mono (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (hle : τ ≤ π) : hτ.measurable_space ≤ hπ.measurable_space := begin intros s hs i, rw (_ : s ∩ {ω | π ω ≤ i} = s ∩ {ω | τ ω ≤ i} ∩ {ω | π ω ≤ i}), { exact (hs i).inter (hπ i) }, { ext, simp only [set.mem_inter_eq, iff_self_and, and.congr_left_iff, set.mem_set_of_eq], intros hle' _, exact le_trans (hle _) hle' }, end lemma measurable_space_le_of_countable [countable ι] (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := begin intros s hs, change ∀ i, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}) at hs, rw (_ : s = ⋃ i, s ∩ {ω | τ ω ≤ i}), { exact measurable_set.Union (λ i, f.le i _ (hs i)) }, { ext ω, split; rw set.mem_Union, { exact λ hx, ⟨τ ω, hx, le_rfl⟩ }, { rintro ⟨_, hx, _⟩, exact hx } } end lemma measurable_space_le' [is_countably_generated (at_top : filter ι)] [(at_top : filter ι).ne_bot] (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := begin intros s hs, change ∀ i, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}) at hs, obtain ⟨seq : ℕ → ι, h_seq_tendsto⟩ := at_top.exists_seq_tendsto, rw (_ : s = ⋃ n, s ∩ {ω | τ ω ≤ seq n}), { exact measurable_set.Union (λ i, f.le (seq i) _ (hs (seq i))), }, { ext ω, split; rw set.mem_Union, { intros hx, suffices : ∃ i, τ ω ≤ seq i, from ⟨this.some, hx, this.some_spec⟩, rw tendsto_at_top at h_seq_tendsto, exact (h_seq_tendsto (τ ω)).exists, }, { rintro ⟨_, hx, _⟩, exact hx }, }, all_goals { apply_instance, }, end lemma measurable_space_le {ι} [semilattice_sup ι] {f : filtration ι m} {τ : Ω → ι} [is_countably_generated (at_top : filter ι)] (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := begin casesI is_empty_or_nonempty ι, { haveI : is_empty Ω := ⟨λ ω, is_empty.false (τ ω)⟩, intros s hsτ, suffices hs : s = ∅, by { rw hs, exact measurable_set.empty, }, haveI : unique (set Ω) := set.unique_empty, rw [unique.eq_default s, unique.eq_default ∅], }, exact measurable_space_le' hτ, end example {f : filtration ℕ m} {τ : Ω → ℕ} (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := hτ.measurable_space_le example {f : filtration ℝ m} {τ : Ω → ℝ} (hτ : is_stopping_time f τ) : hτ.measurable_space ≤ m := hτ.measurable_space_le @[simp] lemma measurable_space_const (f : filtration ι m) (i : ι) : (is_stopping_time_const f i).measurable_space = f i := begin ext1 s, change measurable_set[(is_stopping_time_const f i).measurable_space] s ↔ measurable_set[f i] s, rw is_stopping_time.measurable_set, split; intro h, { specialize h i, simpa only [le_refl, set.set_of_true, set.inter_univ] using h, }, { intro j, by_cases hij : i ≤ j, { simp only [hij, set.set_of_true, set.inter_univ], exact f.mono hij _ h, }, { simp only [hij, set.set_of_false, set.inter_empty, measurable_set.empty], }, }, end lemma measurable_set_inter_eq_iff (hτ : is_stopping_time f τ) (s : set Ω) (i : ι) : measurable_set[hτ.measurable_space] (s ∩ {ω | τ ω = i}) ↔ measurable_set[f i] (s ∩ {ω | τ ω = i}) := begin have : ∀ j, ({ω : Ω | τ ω = i} ∩ {ω : Ω | τ ω ≤ j}) = {ω : Ω | τ ω = i} ∩ {ω | i ≤ j}, { intro j, ext1 ω, simp only [set.mem_inter_eq, set.mem_set_of_eq, and.congr_right_iff], intro hxi, rw hxi, }, split; intro h, { specialize h i, simpa only [set.inter_assoc, this, le_refl, set.set_of_true, set.inter_univ] using h, }, { intro j, rw [set.inter_assoc, this], by_cases hij : i ≤ j, { simp only [hij, set.set_of_true, set.inter_univ], exact f.mono hij _ h, }, { simp [hij], }, }, end lemma measurable_space_le_of_le_const (hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ ω, τ ω ≤ i) : hτ.measurable_space ≤ f i := (measurable_space_mono hτ _ hτ_le).trans (measurable_space_const _ _).le lemma measurable_space_le_of_le (hτ : is_stopping_time f τ) {n : ι} (hτ_le : ∀ ω, τ ω ≤ n) : hτ.measurable_space ≤ m := (hτ.measurable_space_le_of_le_const hτ_le).trans (f.le n) lemma le_measurable_space_of_const_le (hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ ω, i ≤ τ ω) : f i ≤ hτ.measurable_space := (measurable_space_const _ _).symm.le.trans (measurable_space_mono _ hτ hτ_le) end preorder instance sigma_finite_stopping_time {ι} [semilattice_sup ι] [order_bot ι] [(filter.at_top : filter ι).is_countably_generated] {μ : measure Ω} {f : filtration ι m} {τ : Ω → ι} [sigma_finite_filtration μ f] (hτ : is_stopping_time f τ) : sigma_finite (μ.trim hτ.measurable_space_le) := begin refine sigma_finite_trim_mono hτ.measurable_space_le _, { exact f ⊥, }, { exact hτ.le_measurable_space_of_const_le (λ _, bot_le), }, { apply_instance, }, end instance sigma_finite_stopping_time_of_le {ι} [semilattice_sup ι] [order_bot ι] {μ : measure Ω} {f : filtration ι m} {τ : Ω → ι} [sigma_finite_filtration μ f] (hτ : is_stopping_time f τ) {n : ι} (hτ_le : ∀ ω, τ ω ≤ n) : sigma_finite (μ.trim (hτ.measurable_space_le_of_le hτ_le)) := begin refine sigma_finite_trim_mono (hτ.measurable_space_le_of_le hτ_le) _, { exact f ⊥, }, { exact hτ.le_measurable_space_of_const_le (λ _, bot_le), }, { apply_instance, }, end section linear_order variables [linear_order ι] {f : filtration ι m} {τ π : Ω → ι} protected lemma measurable_set_le' (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω ≤ i} := begin intro j, have : {ω : Ω | τ ω ≤ i} ∩ {ω : Ω | τ ω ≤ j} = {ω : Ω | τ ω ≤ min i j}, { ext1 ω, simp only [set.mem_inter_eq, set.mem_set_of_eq, le_min_iff], }, rw this, exact f.mono (min_le_right i j) _ (hτ _), end protected lemma measurable_set_gt' (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | i < τ ω} := begin have : {ω : Ω | i < τ ω} = {ω : Ω | τ ω ≤ i}ᶜ, by { ext1 ω, simp, }, rw this, exact (hτ.measurable_set_le' i).compl, end protected lemma measurable_set_eq' [topological_space ι] [order_topology ι] [first_countable_topology ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω = i} := begin rw [← set.univ_inter {ω | τ ω = i}, measurable_set_inter_eq_iff, set.univ_inter], exact hτ.measurable_set_eq i, end protected lemma measurable_set_ge' [topological_space ι] [order_topology ι] [first_countable_topology ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | i ≤ τ ω} := begin have : {ω | i ≤ τ ω} = {ω | τ ω = i} ∪ {ω | i < τ ω}, { ext1 ω, simp only [le_iff_lt_or_eq, set.mem_set_of_eq, set.mem_union_eq], rw [@eq_comm _ i, or_comm], }, rw this, exact (hτ.measurable_set_eq' i).union (hτ.measurable_set_gt' i), end protected lemma measurable_set_lt' [topological_space ι] [order_topology ι] [first_countable_topology ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω < i} := begin have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i}, { ext1 ω, simp only [lt_iff_le_and_ne, set.mem_set_of_eq, set.mem_diff], }, rw this, exact (hτ.measurable_set_le' i).diff (hτ.measurable_set_eq' i), end section countable protected lemma measurable_set_eq_of_countable_range' (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω = i} := begin rw [← set.univ_inter {ω | τ ω = i}, measurable_set_inter_eq_iff, set.univ_inter], exact hτ.measurable_set_eq_of_countable_range h_countable i, end protected lemma measurable_set_eq_of_countable' [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω = i} := hτ.measurable_set_eq_of_countable_range' (set.to_countable _) i protected lemma measurable_set_ge_of_countable_range' (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[hτ.measurable_space] {ω | i ≤ τ ω} := begin have : {ω | i ≤ τ ω} = {ω | τ ω = i} ∪ {ω | i < τ ω}, { ext1 ω, simp only [le_iff_lt_or_eq, set.mem_set_of_eq, set.mem_union_eq], rw [@eq_comm _ i, or_comm], }, rw this, exact (hτ.measurable_set_eq_of_countable_range' h_countable i).union (hτ.measurable_set_gt' i), end protected lemma measurable_set_ge_of_countable' [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | i ≤ τ ω} := hτ.measurable_set_ge_of_countable_range' (set.to_countable _) i protected lemma measurable_set_lt_of_countable_range' (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω < i} := begin have : {ω | τ ω < i} = {ω | τ ω ≤ i} \ {ω | τ ω = i}, { ext1 ω, simp only [lt_iff_le_and_ne, set.mem_set_of_eq, set.mem_diff], }, rw this, exact (hτ.measurable_set_le' i).diff (hτ.measurable_set_eq_of_countable_range' h_countable i), end protected lemma measurable_set_lt_of_countable' [countable ι] (hτ : is_stopping_time f τ) (i : ι) : measurable_set[hτ.measurable_space] {ω | τ ω < i} := hτ.measurable_set_lt_of_countable_range' (set.to_countable _) i protected lemma measurable_space_le_of_countable_range (hτ : is_stopping_time f τ) (h_countable : (set.range τ).countable) : hτ.measurable_space ≤ m := begin intros s hs, change ∀ i, measurable_set[f i] (s ∩ {ω | τ ω ≤ i}) at hs, rw (_ : s = ⋃ (i ∈ set.range τ), s ∩ {ω | τ ω ≤ i}), { exact measurable_set.bUnion h_countable (λ i _, f.le i _ (hs i)), }, { ext ω, split; rw set.mem_Union, { exact λ hx, ⟨τ ω, by simpa using hx⟩,}, { rintro ⟨i, hx⟩, simp only [set.mem_range, set.Union_exists, set.mem_Union, set.mem_inter_eq, set.mem_set_of_eq, exists_prop, exists_and_distrib_right] at hx, exact hx.1.2, } } end end countable protected lemma measurable [topological_space ι] [measurable_space ι] [borel_space ι] [order_topology ι] [second_countable_topology ι] (hτ : is_stopping_time f τ) : measurable[hτ.measurable_space] τ := @measurable_of_Iic ι Ω _ _ _ hτ.measurable_space _ _ _ _ (λ i, hτ.measurable_set_le' i) protected lemma measurable_of_le [topological_space ι] [measurable_space ι] [borel_space ι] [order_topology ι] [second_countable_topology ι] (hτ : is_stopping_time f τ) {i : ι} (hτ_le : ∀ ω, τ ω ≤ i) : measurable[f i] τ := hτ.measurable.mono (measurable_space_le_of_le_const _ hτ_le) le_rfl lemma measurable_space_min (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : (hτ.min hπ).measurable_space = hτ.measurable_space ⊓ hπ.measurable_space := begin refine le_antisymm _ _, { exact le_inf (measurable_space_mono _ hτ (λ _, min_le_left _ _)) (measurable_space_mono _ hπ (λ _, min_le_right _ _)), }, { intro s, change measurable_set[hτ.measurable_space] s ∧ measurable_set[hπ.measurable_space] s → measurable_set[(hτ.min hπ).measurable_space] s, simp_rw is_stopping_time.measurable_set, have : ∀ i, {ω | min (τ ω) (π ω) ≤ i} = {ω | τ ω ≤ i} ∪ {ω | π ω ≤ i}, { intro i, ext1 ω, simp, }, simp_rw [this, set.inter_union_distrib_left], exact λ h i, (h.left i).union (h.right i), }, end lemma measurable_set_min_iff (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (s : set Ω) : measurable_set[(hτ.min hπ).measurable_space] s ↔ measurable_set[hτ.measurable_space] s ∧ measurable_set[hπ.measurable_space] s := by { rw measurable_space_min, refl, } lemma measurable_space_min_const (hτ : is_stopping_time f τ) {i : ι} : (hτ.min_const i).measurable_space = hτ.measurable_space ⊓ f i := by rw [hτ.measurable_space_min (is_stopping_time_const _ i), measurable_space_const] lemma measurable_set_min_const_iff (hτ : is_stopping_time f τ) (s : set Ω) {i : ι} : measurable_set[(hτ.min_const i).measurable_space] s ↔ measurable_set[hτ.measurable_space] s ∧ measurable_set[f i] s := by rw [measurable_space_min_const, measurable_space.measurable_set_inf] lemma measurable_set_inter_le [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (s : set Ω) (hs : measurable_set[hτ.measurable_space] s) : measurable_set[(hτ.min hπ).measurable_space] (s ∩ {ω | τ ω ≤ π ω}) := begin simp_rw is_stopping_time.measurable_set at ⊢ hs, intro i, have : (s ∩ {ω | τ ω ≤ π ω} ∩ {ω | min (τ ω) (π ω) ≤ i}) = (s ∩ {ω | τ ω ≤ i}) ∩ {ω | min (τ ω) (π ω) ≤ i} ∩ {ω | min (τ ω) i ≤ min (min (τ ω) (π ω)) i}, { ext1 ω, simp only [min_le_iff, set.mem_inter_eq, set.mem_set_of_eq, le_min_iff, le_refl, true_and, and_true, true_or, or_true], by_cases hτi : τ ω ≤ i, { simp only [hτi, true_or, and_true, and.congr_right_iff], intro hx, split; intro h, { exact or.inl h, }, { cases h, { exact h, }, { exact hτi.trans h, }, }, }, simp only [hτi, false_or, and_false, false_and, iff_false, not_and, not_le, and_imp], refine λ hx hτ_le_π, lt_of_lt_of_le _ hτ_le_π, rw ← not_le, exact hτi, }, rw this, refine ((hs i).inter ((hτ.min hπ) i)).inter _, apply measurable_set_le, { exact (hτ.min_const i).measurable_of_le (λ _, min_le_right _ _), }, { exact ((hτ.min hπ).min_const i).measurable_of_le (λ _, min_le_right _ _), }, end lemma measurable_set_inter_le_iff [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) (s : set Ω) : measurable_set[hτ.measurable_space] (s ∩ {ω | τ ω ≤ π ω}) ↔ measurable_set[(hτ.min hπ).measurable_space] (s ∩ {ω | τ ω ≤ π ω}) := begin split; intro h, { have : s ∩ {ω | τ ω ≤ π ω} = s ∩ {ω | τ ω ≤ π ω} ∩ {ω | τ ω ≤ π ω}, by rw [set.inter_assoc, set.inter_self], rw this, exact measurable_set_inter_le _ _ _ h, }, { rw measurable_set_min_iff at h, exact h.1, }, end lemma measurable_set_inter_le_const_iff (hτ : is_stopping_time f τ) (s : set Ω) (i : ι) : measurable_set[hτ.measurable_space] (s ∩ {ω | τ ω ≤ i}) ↔ measurable_set[(hτ.min_const i).measurable_space] (s ∩ {ω | τ ω ≤ i}) := begin rw [is_stopping_time.measurable_set_min_iff hτ (is_stopping_time_const _ i), is_stopping_time.measurable_space_const, is_stopping_time.measurable_set], refine ⟨λ h, ⟨h, _⟩, λ h j, h.1 j⟩, specialize h i, rwa [set.inter_assoc, set.inter_self] at h, end lemma measurable_set_le_stopping_time [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : measurable_set[hτ.measurable_space] {ω | τ ω ≤ π ω} := begin rw hτ.measurable_set, intro j, have : {ω | τ ω ≤ π ω} ∩ {ω | τ ω ≤ j} = {ω | min (τ ω) j ≤ min (π ω) j} ∩ {ω | τ ω ≤ j}, { ext1 ω, simp only [set.mem_inter_eq, set.mem_set_of_eq, min_le_iff, le_min_iff, le_refl, and_true, and.congr_left_iff], intro h, simp only [h, or_self, and_true], by_cases hj : j ≤ π ω, { simp only [hj, h.trans hj, or_self], }, { simp only [hj, or_false], }, }, rw this, refine measurable_set.inter _ (hτ.measurable_set_le j), apply measurable_set_le, { exact (hτ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, { exact (hπ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, end lemma measurable_set_stopping_time_le [topological_space ι] [second_countable_topology ι] [order_topology ι] [measurable_space ι] [borel_space ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : measurable_set[hπ.measurable_space] {ω | τ ω ≤ π ω} := begin suffices : measurable_set[(hτ.min hπ).measurable_space] {ω : Ω | τ ω ≤ π ω}, by { rw measurable_set_min_iff hτ hπ at this, exact this.2, }, rw [← set.univ_inter {ω : Ω | τ ω ≤ π ω}, ← hτ.measurable_set_inter_le_iff hπ, set.univ_inter], exact measurable_set_le_stopping_time hτ hπ, end lemma measurable_set_eq_stopping_time [add_group ι] [topological_space ι] [measurable_space ι] [borel_space ι] [order_topology ι] [measurable_singleton_class ι] [second_countable_topology ι] [has_measurable_sub₂ ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : measurable_set[hτ.measurable_space] {ω | τ ω = π ω} := begin rw hτ.measurable_set, intro j, have : {ω | τ ω = π ω} ∩ {ω | τ ω ≤ j} = {ω | min (τ ω) j = min (π ω) j} ∩ {ω | τ ω ≤ j} ∩ {ω | π ω ≤ j}, { ext1 ω, simp only [set.mem_inter_eq, set.mem_set_of_eq], refine ⟨λ h, ⟨⟨_, h.2⟩, _⟩, λ h, ⟨_, h.1.2⟩⟩, { rw h.1, }, { rw ← h.1, exact h.2, }, { cases h with h' hσ_le, cases h' with h_eq hτ_le, rwa [min_eq_left hτ_le, min_eq_left hσ_le] at h_eq, }, }, rw this, refine measurable_set.inter (measurable_set.inter _ (hτ.measurable_set_le j)) (hπ.measurable_set_le j), apply measurable_set_eq_fun, { exact (hτ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, { exact (hπ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, end lemma measurable_set_eq_stopping_time_of_countable [countable ι] [topological_space ι] [measurable_space ι] [borel_space ι] [order_topology ι] [measurable_singleton_class ι] [second_countable_topology ι] (hτ : is_stopping_time f τ) (hπ : is_stopping_time f π) : measurable_set[hτ.measurable_space] {ω | τ ω = π ω} := begin rw hτ.measurable_set, intro j, have : {ω | τ ω = π ω} ∩ {ω | τ ω ≤ j} = {ω | min (τ ω) j = min (π ω) j} ∩ {ω | τ ω ≤ j} ∩ {ω | π ω ≤ j}, { ext1 ω, simp only [set.mem_inter_eq, set.mem_set_of_eq], refine ⟨λ h, ⟨⟨_, h.2⟩, _⟩, λ h, ⟨_, h.1.2⟩⟩, { rw h.1, }, { rw ← h.1, exact h.2, }, { cases h with h' hπ_le, cases h' with h_eq hτ_le, rwa [min_eq_left hτ_le, min_eq_left hπ_le] at h_eq, }, }, rw this, refine measurable_set.inter (measurable_set.inter _ (hτ.measurable_set_le j)) (hπ.measurable_set_le j), apply measurable_set_eq_fun_of_countable, { exact (hτ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, { exact (hπ.min_const j).measurable_of_le (λ _, min_le_right _ _), }, end end linear_order end is_stopping_time section linear_order /-! ## Stopped value and stopped process -/ /-- Given a map `u : ι → Ω → E`, its stopped value with respect to the stopping time `τ` is the map `x ↦ u (τ ω) ω`. -/ def stopped_value (u : ι → Ω → β) (τ : Ω → ι) : Ω → β := λ ω, u (τ ω) ω lemma stopped_value_const (u : ι → Ω → β) (i : ι) : stopped_value u (λ ω, i) = u i := rfl variable [linear_order ι] /-- Given a map `u : ι → Ω → E`, the stopped process with respect to `τ` is `u i ω` if `i ≤ τ ω`, and `u (τ ω) ω` otherwise. Intuitively, the stopped process stops evolving once the stopping time has occured. -/ def stopped_process (u : ι → Ω → β) (τ : Ω → ι) : ι → Ω → β := λ i ω, u (min i (τ ω)) ω lemma stopped_process_eq_stopped_value {u : ι → Ω → β} {τ : Ω → ι} : stopped_process u τ = λ i, stopped_value u (λ ω, min i (τ ω)) := rfl lemma stopped_value_stopped_process {u : ι → Ω → β} {τ σ : Ω → ι} : stopped_value (stopped_process u τ) σ = stopped_value u (λ ω, min (σ ω) (τ ω)) := rfl lemma stopped_process_eq_of_le {u : ι → Ω → β} {τ : Ω → ι} {i : ι} {ω : Ω} (h : i ≤ τ ω) : stopped_process u τ i ω = u i ω := by simp [stopped_process, min_eq_left h] lemma stopped_process_eq_of_ge {u : ι → Ω → β} {τ : Ω → ι} {i : ι} {ω : Ω} (h : τ ω ≤ i) : stopped_process u τ i ω = u (τ ω) ω := by simp [stopped_process, min_eq_right h] section prog_measurable variables [measurable_space ι] [topological_space ι] [order_topology ι] [second_countable_topology ι] [borel_space ι] [topological_space β] {u : ι → Ω → β} {τ : Ω → ι} {f : filtration ι m} lemma prog_measurable_min_stopping_time [metrizable_space ι] (hτ : is_stopping_time f τ) : prog_measurable f (λ i ω, min i (τ ω)) := begin intro i, let m_prod : measurable_space (set.Iic i × Ω) := measurable_space.prod _ (f i), let m_set : ∀ t : set (set.Iic i × Ω), measurable_space t := λ _, @subtype.measurable_space (set.Iic i × Ω) _ m_prod, let s := {p : set.Iic i × Ω | τ p.2 ≤ i}, have hs : measurable_set[m_prod] s, from @measurable_snd (set.Iic i) Ω _ (f i) _ (hτ i), have h_meas_fst : ∀ t : set (set.Iic i × Ω), measurable[m_set t] (λ x : t, ((x : set.Iic i × Ω).fst : ι)), from λ t, (@measurable_subtype_coe (set.Iic i × Ω) m_prod _).fst.subtype_coe, apply measurable.strongly_measurable, refine measurable_of_restrict_of_restrict_compl hs _ _, { refine @measurable.min _ _ _ _ _ (m_set s) _ _ _ _ _ (h_meas_fst s) _, refine @measurable_of_Iic ι s _ _ _ (m_set s) _ _ _ _ (λ j, _), have h_set_eq : (λ x : s, τ (x : set.Iic i × Ω).snd) ⁻¹' set.Iic j = (λ x : s, (x : set.Iic i × Ω).snd) ⁻¹' {ω | τ ω ≤ min i j}, { ext1 ω, simp only [set.mem_preimage, set.mem_Iic, iff_and_self, le_min_iff, set.mem_set_of_eq], exact λ _, ω.prop, }, rw h_set_eq, suffices h_meas : @measurable _ _ (m_set s) (f i) (λ x : s, (x : set.Iic i × Ω).snd), from h_meas (f.mono (min_le_left _ _) _ (hτ.measurable_set_le (min i j))), exact measurable_snd.comp (@measurable_subtype_coe _ m_prod _), }, { suffices h_min_eq_left : (λ x : sᶜ, min ↑((x : set.Iic i × Ω).fst) (τ (x : set.Iic i × Ω).snd)) = λ x : sᶜ, ↑((x : set.Iic i × Ω).fst), { rw [set.restrict, h_min_eq_left], exact h_meas_fst _, }, ext1 ω, rw min_eq_left, have hx_fst_le : ↑(ω : set.Iic i × Ω).fst ≤ i, from (ω : set.Iic i × Ω).fst.prop, refine hx_fst_le.trans (le_of_lt _), convert ω.prop, simp only [not_le, set.mem_compl_eq, set.mem_set_of_eq], }, end lemma prog_measurable.stopped_process [metrizable_space ι] (h : prog_measurable f u) (hτ : is_stopping_time f τ) : prog_measurable f (stopped_process u τ) := h.comp (prog_measurable_min_stopping_time hτ) (λ i x, min_le_left _ _) lemma prog_measurable.adapted_stopped_process [metrizable_space ι] (h : prog_measurable f u) (hτ : is_stopping_time f τ) : adapted f (stopped_process u τ) := (h.stopped_process hτ).adapted lemma prog_measurable.strongly_measurable_stopped_process [metrizable_space ι] (hu : prog_measurable f u) (hτ : is_stopping_time f τ) (i : ι) : strongly_measurable (stopped_process u τ i) := (hu.adapted_stopped_process hτ i).mono (f.le _) lemma strongly_measurable_stopped_value_of_le (h : prog_measurable f u) (hτ : is_stopping_time f τ) {n : ι} (hτ_le : ∀ ω, τ ω ≤ n) : strongly_measurable[f n] (stopped_value u τ) := begin have : stopped_value u τ = (λ (p : set.Iic n × Ω), u ↑(p.fst) p.snd) ∘ (λ ω, (⟨τ ω, hτ_le ω⟩, ω)), { ext1 ω, simp only [stopped_value, function.comp_app, subtype.coe_mk], }, rw this, refine strongly_measurable.comp_measurable (h n) _, exact (hτ.measurable_of_le hτ_le).subtype_mk.prod_mk measurable_id, end lemma measurable_stopped_value [metrizable_space β] [measurable_space β] [borel_space β] (hf_prog : prog_measurable f u) (hτ : is_stopping_time f τ) : measurable[hτ.measurable_space] (stopped_value u τ) := begin have h_str_meas : ∀ i, strongly_measurable[f i] (stopped_value u (λ ω, min (τ ω) i)), from λ i, strongly_measurable_stopped_value_of_le hf_prog (hτ.min_const i) (λ _, min_le_right _ _), intros t ht i, suffices : stopped_value u τ ⁻¹' t ∩ {ω : Ω | τ ω ≤ i} = stopped_value u (λ ω, min (τ ω) i) ⁻¹' t ∩ {ω : Ω | τ ω ≤ i}, by { rw this, exact ((h_str_meas i).measurable ht).inter (hτ.measurable_set_le i), }, ext1 ω, simp only [stopped_value, set.mem_inter_eq, set.mem_preimage, set.mem_set_of_eq, and.congr_left_iff], intro h, rw min_eq_left h, end end prog_measurable end linear_order section stopped_value_of_mem_finset variables {μ : measure Ω} {τ σ : Ω → ι} {E : Type*} {p : ℝ≥0∞} {u : ι → Ω → E} lemma stopped_value_eq_of_mem_finset [add_comm_monoid E] {s : finset ι} (hbdd : ∀ ω, τ ω ∈ s) : stopped_value u τ = ∑ i in s, set.indicator {ω | τ ω = i} (u i) := begin ext y, rw [stopped_value, finset.sum_apply, finset.sum_indicator_eq_sum_filter], suffices : finset.filter (λ i, y ∈ {ω : Ω | τ ω = i}) s = ({τ y} : finset ι), by rw [this, finset.sum_singleton], ext1 ω, simp only [set.mem_set_of_eq, finset.mem_filter, finset.mem_singleton], split; intro h, { exact h.2.symm, }, { refine ⟨_, h.symm⟩, rw h, exact hbdd y, }, end lemma stopped_value_eq' [preorder ι] [locally_finite_order_bot ι] [add_comm_monoid E] {N : ι} (hbdd : ∀ ω, τ ω ≤ N) : stopped_value u τ = ∑ i in finset.Iic N, set.indicator {ω | τ ω = i} (u i) := stopped_value_eq_of_mem_finset (λ ω, finset.mem_Iic.mpr (hbdd ω)) variables [partial_order ι] {ℱ : filtration ι m} [normed_add_comm_group E] lemma mem_ℒp_stopped_value_of_mem_finset (hτ : is_stopping_time ℱ τ) (hu : ∀ n, mem_ℒp (u n) p μ) {s : finset ι} (hbdd : ∀ ω, τ ω ∈ s) : mem_ℒp (stopped_value u τ) p μ := begin rw stopped_value_eq_of_mem_finset hbdd, swap, apply_instance, refine mem_ℒp_finset_sum' _ (λ i hi, mem_ℒp.indicator _ (hu i)), refine ℱ.le i {a : Ω | τ a = i} (hτ.measurable_set_eq_of_countable_range _ i), refine ((finset.finite_to_set s).subset (λ ω hω, _)).countable, obtain ⟨y, rfl⟩ := hω, exact hbdd y, end lemma mem_ℒp_stopped_value [locally_finite_order_bot ι] (hτ : is_stopping_time ℱ τ) (hu : ∀ n, mem_ℒp (u n) p μ) {N : ι} (hbdd : ∀ ω, τ ω ≤ N) : mem_ℒp (stopped_value u τ) p μ := mem_ℒp_stopped_value_of_mem_finset hτ hu (λ ω, finset.mem_Iic.mpr (hbdd ω)) lemma integrable_stopped_value_of_mem_finset (hτ : is_stopping_time ℱ τ) (hu : ∀ n, integrable (u n) μ) {s : finset ι} (hbdd : ∀ ω, τ ω ∈ s) : integrable (stopped_value u τ) μ := begin simp_rw ← mem_ℒp_one_iff_integrable at hu ⊢, exact mem_ℒp_stopped_value_of_mem_finset hτ hu hbdd, end variables (ι) lemma integrable_stopped_value [locally_finite_order_bot ι] (hτ : is_stopping_time ℱ τ) (hu : ∀ n, integrable (u n) μ) {N : ι} (hbdd : ∀ ω, τ ω ≤ N) : integrable (stopped_value u τ) μ := integrable_stopped_value_of_mem_finset hτ hu (λ ω, finset.mem_Iic.mpr (hbdd ω)) end stopped_value_of_mem_finset section nat /-! ### Filtrations indexed by `ℕ` -/ open filtration variables {f : filtration ℕ m} {u : ℕ → Ω → β} {τ π : Ω → ℕ} lemma stopped_value_sub_eq_sum [add_comm_group β] (hle : τ ≤ π) : stopped_value u π - stopped_value u τ = λ ω, (∑ i in finset.Ico (τ ω) (π ω), (u (i + 1) - u i)) ω := begin ext ω, rw [finset.sum_Ico_eq_sub _ (hle ω), finset.sum_range_sub, finset.sum_range_sub], simp [stopped_value], end lemma stopped_value_sub_eq_sum' [add_comm_group β] (hle : τ ≤ π) {N : ℕ} (hbdd : ∀ ω, π ω ≤ N) : stopped_value u π - stopped_value u τ = λ ω, (∑ i in finset.range (N + 1), set.indicator {ω | τ ω ≤ i ∧ i < π ω} (u (i + 1) - u i)) ω := begin rw stopped_value_sub_eq_sum hle, ext ω, simp only [finset.sum_apply, finset.sum_indicator_eq_sum_filter], refine finset.sum_congr _ (λ _ _, rfl), ext i, simp only [finset.mem_filter, set.mem_set_of_eq, finset.mem_range, finset.mem_Ico], exact ⟨λ h, ⟨lt_trans h.2 (nat.lt_succ_iff.2 $ hbdd _), h⟩, λ h, h.2⟩ end section add_comm_monoid variables [add_comm_monoid β] /-- For filtrations indexed by `ℕ`, `adapted` and `prog_measurable` are equivalent. This lemma provides `adapted f u → prog_measurable f u`. See `prog_measurable.adapted` for the reverse direction, which is true more generally. -/ lemma adapted.prog_measurable_of_nat [topological_space β] [has_continuous_add β] (h : adapted f u) : prog_measurable f u := begin intro i, have : (λ p : ↥(set.Iic i) × Ω, u ↑(p.fst) p.snd) = λ p : ↥(set.Iic i) × Ω, ∑ j in finset.range (i + 1), if ↑p.fst = j then u j p.snd else 0, { ext1 p, rw finset.sum_ite_eq, have hp_mem : (p.fst : ℕ) ∈ finset.range (i + 1) := finset.mem_range_succ_iff.mpr p.fst.prop, simp only [hp_mem, if_true], }, rw this, refine finset.strongly_measurable_sum _ (λ j hj, strongly_measurable.ite _ _ _), { suffices h_meas : measurable[measurable_space.prod _ (f i)] (λ a : ↥(set.Iic i) × Ω, (a.fst : ℕ)), from h_meas (measurable_set_singleton j), exact measurable_fst.subtype_coe, }, { have h_le : j ≤ i, from finset.mem_range_succ_iff.mp hj, exact (strongly_measurable.mono (h j) (f.mono h_le)).comp_measurable measurable_snd, }, { exact strongly_measurable_const, }, end /-- For filtrations indexed by `ℕ`, the stopped process obtained from an adapted process is adapted. -/ lemma adapted.stopped_process_of_nat [topological_space β] [has_continuous_add β] (hu : adapted f u) (hτ : is_stopping_time f τ) : adapted f (stopped_process u τ) := (hu.prog_measurable_of_nat.stopped_process hτ).adapted lemma adapted.strongly_measurable_stopped_process_of_nat [topological_space β] [has_continuous_add β] (hτ : is_stopping_time f τ) (hu : adapted f u) (n : ℕ) : strongly_measurable (stopped_process u τ n) := hu.prog_measurable_of_nat.strongly_measurable_stopped_process hτ n lemma stopped_value_eq {N : ℕ} (hbdd : ∀ ω, τ ω ≤ N) : stopped_value u τ = λ x, (∑ i in finset.range (N + 1), set.indicator {ω | τ ω = i} (u i)) x := stopped_value_eq_of_mem_finset (λ ω, finset.mem_range_succ_iff.mpr (hbdd ω)) lemma stopped_process_eq (n : ℕ) : stopped_process u τ n = set.indicator {a | n ≤ τ a} (u n) + ∑ i in finset.range n, set.indicator {ω | τ ω = i} (u i) := begin ext ω, rw [pi.add_apply, finset.sum_apply], cases le_or_lt n (τ ω), { rw [stopped_process_eq_of_le h, set.indicator_of_mem, finset.sum_eq_zero, add_zero], { intros m hm, rw finset.mem_range at hm, exact set.indicator_of_not_mem ((lt_of_lt_of_le hm h).ne.symm) _ }, { exact h } }, { rw [stopped_process_eq_of_ge (le_of_lt h), finset.sum_eq_single_of_mem (τ ω)], { rw [set.indicator_of_not_mem, zero_add, set.indicator_of_mem], { exact rfl }, -- refl does not work { exact not_le.2 h } }, { rwa [finset.mem_range] }, { intros b hb hneq, rw set.indicator_of_not_mem, exact hneq.symm } }, end lemma stopped_process_eq' (n : ℕ) : stopped_process u τ n = set.indicator {a | n + 1 ≤ τ a} (u n) + ∑ i in finset.range (n + 1), set.indicator {a | τ a = i} (u i) := begin have : {a | n ≤ τ a}.indicator (u n) = {a | n + 1 ≤ τ a}.indicator (u n) + {a | τ a = n}.indicator (u n), { ext x, rw [add_comm, pi.add_apply, ← set.indicator_union_of_not_mem_inter], { simp_rw [@eq_comm _ _ n, @le_iff_eq_or_lt _ _ n, nat.succ_le_iff], refl }, { rintro ⟨h₁, h₂⟩, exact (nat.succ_le_iff.1 h₂).ne h₁.symm } }, rw [stopped_process_eq, this, finset.sum_range_succ_comm, ← add_assoc], end end add_comm_monoid section normed_add_comm_group variables [normed_add_comm_group β] {p : ℝ≥0∞} {μ : measure Ω} lemma mem_ℒp_stopped_process (hτ : is_stopping_time f τ) (hu : ∀ n, mem_ℒp (u n) p μ) (n : ℕ) : mem_ℒp (stopped_process u τ n) p μ := begin rw stopped_process_eq, refine mem_ℒp.add _ _, { exact mem_ℒp.indicator (f.le n {a : Ω | n ≤ τ a} (hτ.measurable_set_ge n)) (hu n) }, { suffices : mem_ℒp (λ ω, ∑ (i : ℕ) in finset.range n, {a : Ω | τ a = i}.indicator (u i) ω) p μ, { convert this, ext1 ω, simp only [finset.sum_apply] }, refine mem_ℒp_finset_sum _ (λ i hi, mem_ℒp.indicator _ (hu i)), exact f.le i {a : Ω | τ a = i} (hτ.measurable_set_eq i) }, end lemma integrable_stopped_process (hτ : is_stopping_time f τ) (hu : ∀ n, integrable (u n) μ) (n : ℕ) : integrable (stopped_process u τ n) μ := by { simp_rw ← mem_ℒp_one_iff_integrable at hu ⊢, exact mem_ℒp_stopped_process hτ hu n, } end normed_add_comm_group end nat section piecewise_const variables [preorder ι] {𝒢 : filtration ι m} {τ η : Ω → ι} {i j : ι} {s : set Ω} [decidable_pred (∈ s)] /-- Given stopping times `τ` and `η` which are bounded below, `set.piecewise s τ η` is also a stopping time with respect to the same filtration. -/ lemma is_stopping_time.piecewise_of_le (hτ_st : is_stopping_time 𝒢 τ) (hη_st : is_stopping_time 𝒢 η) (hτ : ∀ ω, i ≤ τ ω) (hη : ∀ ω, i ≤ η ω) (hs : measurable_set[𝒢 i] s) : is_stopping_time 𝒢 (s.piecewise τ η) := begin intro n, have : {ω | s.piecewise τ η ω ≤ n} = (s ∩ {ω | τ ω ≤ n}) ∪ (sᶜ ∩ {ω | η ω ≤ n}), { ext1 ω, simp only [set.piecewise, set.mem_inter_eq, set.mem_set_of_eq, and.congr_right_iff], by_cases hx : ω ∈ s; simp [hx], }, rw this, by_cases hin : i ≤ n, { have hs_n : measurable_set[𝒢 n] s, from 𝒢.mono hin _ hs, exact (hs_n.inter (hτ_st n)).union (hs_n.compl.inter (hη_st n)), }, { have hτn : ∀ ω, ¬ τ ω ≤ n := λ ω hτn, hin ((hτ ω).trans hτn), have hηn : ∀ ω, ¬ η ω ≤ n := λ ω hηn, hin ((hη ω).trans hηn), simp [hτn, hηn], }, end lemma is_stopping_time_piecewise_const (hij : i ≤ j) (hs : measurable_set[𝒢 i] s) : is_stopping_time 𝒢 (s.piecewise (λ _, i) (λ _, j)) := (is_stopping_time_const 𝒢 i).piecewise_of_le (is_stopping_time_const 𝒢 j) (λ x, le_rfl) (λ _, hij) hs lemma stopped_value_piecewise_const {ι' : Type*} {i j : ι'} {f : ι' → Ω → ℝ} : stopped_value f (s.piecewise (λ _, i) (λ _, j)) = s.piecewise (f i) (f j) := by { ext ω, rw stopped_value, by_cases hx : ω ∈ s; simp [hx] } lemma stopped_value_piecewise_const' {ι' : Type*} {i j : ι'} {f : ι' → Ω → ℝ} : stopped_value f (s.piecewise (λ _, i) (λ _, j)) = s.indicator (f i) + sᶜ.indicator (f j) := by { ext ω, rw stopped_value, by_cases hx : ω ∈ s; simp [hx] } end piecewise_const end measure_theory
79d59db36e49f6bde892c0cf39e8790d5adbfdd3
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab/Open.lean
b473180cfc254b4f67002a49d2277762a125a7e3
[ "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
3,306
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Log namespace Lean.Elab namespace OpenDecl variable [Monad m] [STWorld IO.RealWorld m] [MonadEnv m] variable [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m] variable [AddMessageContext m] [MonadLiftT (ST IO.RealWorld) m] [MonadLog m] structure State where openDecls : List OpenDecl currNamespace : Name abbrev M := StateRefT State m instance : MonadResolveName (M (m := m)) where getCurrNamespace := return (← get).currNamespace getOpenDecls := return (← get).openDecls def resolveId (ns : Name) (idStx : Syntax) : M (m := m) Name := do let declName := ns ++ idStx.getId if (← getEnv).contains declName then return declName else withRef idStx <| resolveGlobalConstNoOverloadCore declName private def addOpenDecl (decl : OpenDecl) : M (m:=m) Unit := modify fun s => { s with openDecls := decl :: s.openDecls } private def elabOpenSimple (n : Syntax) : M (m:=m) Unit := -- `open` id+ for ns in n[0].getArgs do let ns ← resolveNamespace ns.getId addOpenDecl (OpenDecl.simple ns []) activateScoped ns private def elabOpenScoped (n : Syntax) : M (m:=m) Unit := -- `open` `scoped` id+ for ns in n[1].getArgs do activateScoped (← resolveNamespace ns.getId) -- `open` id `(` id+ `)` private def elabOpenOnly (n : Syntax) : M (m:=m) Unit := do let ns ← resolveNamespace n[0].getId for idStx in n[2].getArgs do let declName ← resolveId ns idStx addOpenDecl (OpenDecl.explicit idStx.getId declName) -- `open` id `hiding` id+ private def elabOpenHiding (n : Syntax) : M (m:=m) Unit := do let ns ← resolveNamespace n[0].getId let mut ids : List Name := [] for idStx in n[2].getArgs do let declName ← resolveId ns idStx let id := idStx.getId ids := id::ids addOpenDecl (OpenDecl.simple ns ids) -- `open` id `renaming` sepBy (id `->` id) `,` private def elabOpenRenaming (n : Syntax) : M (m:=m) Unit := do let ns ← resolveNamespace n[0].getId for stx in n[2].getSepArgs do let fromStx := stx[0] let toId := stx[2].getId let declName ← resolveId ns fromStx addOpenDecl (OpenDecl.explicit toId declName) def elabOpenDecl [MonadResolveName m] (openDeclStx : Syntax) : m (List OpenDecl) := do StateRefT'.run' (s := { openDecls := (← getOpenDecls), currNamespace := (← getCurrNamespace) }) do if openDeclStx.getKind == ``Parser.Command.openSimple then elabOpenSimple openDeclStx else if openDeclStx.getKind == ``Parser.Command.openScoped then elabOpenScoped openDeclStx else if openDeclStx.getKind == ``Parser.Command.openOnly then elabOpenOnly openDeclStx else if openDeclStx.getKind == ``Parser.Command.openHiding then elabOpenHiding openDeclStx else elabOpenRenaming openDeclStx return (← get).openDecls def resolveOpenDeclId [MonadResolveName m] (ns : Name) (idStx : Syntax) : m Name := do StateRefT'.run' (s := { openDecls := (← getOpenDecls), currNamespace := (← getCurrNamespace) }) do OpenDecl.resolveId ns idStx end OpenDecl export OpenDecl (elabOpenDecl resolveOpenDeclId) end Lean.Elab
ae057f3db805b1a45849f431894e875808459b3c
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/rel.lean
c1d46bbef08b8617b8115921366891f0d3596468
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
6,473
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Operations on set-valued functions, aka partial multifunctions, aka relations. -/ import tactic.basic data.set.lattice order.complete_lattice logic.relator variables {α : Type*} {β : Type*} {γ : Type*} /-- A relation on `α` and `β`, aka a set-valued function, aka a partial multifunction --/ @[derive complete_lattice, derive inhabited] def rel (α : Type*) (β : Type*) := α → β → Prop namespace rel variables {δ : Type*} (r : rel α β) /-- The inverse relation : `r.inv x y ↔ r y x`. Note that this is *not* a groupoid inverse. -/ def inv : rel β α := flip r lemma inv_def (x : α) (y : β) : r.inv y x ↔ r x y := iff.rfl lemma inv_inv : inv (inv r) = r := by { ext x y, reflexivity } /-- Domain of a relation -/ def dom := {x | ∃ y, r x y} /-- Codomain aka range of a relation-/ def codom := {y | ∃ x, r x y} lemma codom_inv : r.inv.codom = r.dom := by { ext x y, reflexivity } lemma dom_inv : r.inv.dom = r.codom := by { ext x y, reflexivity} /-- Composition of relation; note that it follows the `category_theory/` order of arguments. -/ def comp (r : rel α β) (s : rel β γ) : rel α γ := λ x z, ∃ y, r x y ∧ s y z local infixr ` ∘ ` :=rel.comp lemma comp_assoc (r : rel α β) (s : rel β γ) (t : rel γ δ) : (r ∘ s) ∘ t = r ∘ s ∘ t := begin unfold comp, ext x w, split, { rintros ⟨z, ⟨y, rxy, syz⟩, tzw⟩, exact ⟨y, rxy, z, syz, tzw⟩ }, rintros ⟨y, rxy, z, syz, tzw⟩, exact ⟨z, ⟨y, rxy, syz⟩, tzw⟩ end @[simp] lemma comp_right_id (r : rel α β) : r ∘ @eq β = r := by { unfold comp, ext y, simp } @[simp] lemma comp_left_id (r : rel α β) : @eq α ∘ r = r := by { unfold comp, ext x, simp } lemma inv_id : inv (@eq α) = @eq α := by { ext x y, split; apply eq.symm } lemma inv_comp (r : rel α β) (s : rel β γ) : inv (r ∘ s) = inv s ∘ inv r := by { ext x z, simp [comp, inv, flip, and.comm] } /-- Image of a set under a relation -/ def image (s : set α) : set β := {y | ∃ x ∈ s, r x y} lemma mem_image (y : β) (s : set α) : y ∈ image r s ↔ ∃ x ∈ s, r x y := iff.rfl lemma image_subset : ((⊆) ⇒ (⊆)) r.image r.image := assume s t h y ⟨x, xs, rxy⟩, ⟨x, h xs, rxy⟩ lemma image_mono : monotone r.image := r.image_subset lemma image_inter (s t : set α) : r.image (s ∩ t) ⊆ r.image s ∩ r.image t := r.image_mono.map_inf_le s t lemma image_union (s t : set α) : r.image (s ∪ t) = r.image s ∪ r.image t := le_antisymm (λ y ⟨x, xst, rxy⟩, xst.elim (λ xs, or.inl ⟨x, ⟨xs, rxy⟩⟩) (λ xt, or.inr ⟨x, ⟨xt, rxy⟩⟩)) (r.image_mono.le_map_sup s t) @[simp] lemma image_id (s : set α) : image (@eq α) s = s := by { ext x, simp [mem_image] } lemma image_comp (s : rel β γ) (t : set α) : image (r ∘ s) t = image s (image r t) := begin ext z, simp only [mem_image, comp], split, { rintros ⟨x, xt, y, rxy, syz⟩, exact ⟨y, ⟨x, xt, rxy⟩, syz⟩ }, rintros ⟨y, ⟨x, xt, rxy⟩, syz⟩, exact ⟨x, xt, y, rxy, syz⟩ end lemma image_univ : r.image set.univ = r.codom := by { ext y, simp [mem_image, codom] } /-- Preimage of a set under a relation `r`. Same as the image of `s` under `r.inv` -/ def preimage (s : set β) : set α := image (inv r) s lemma mem_preimage (x : α) (s : set β) : x ∈ preimage r s ↔ ∃ y ∈ s, r x y := iff.rfl lemma preimage_def (s : set β) : preimage r s = {x | ∃ y ∈ s, r x y} := set.ext $ λ x, mem_preimage _ _ _ lemma preimage_mono {s t : set β} (h : s ⊆ t) : r.preimage s ⊆ r.preimage t := image_mono _ h lemma preimage_inter (s t : set β) : r.preimage (s ∩ t) ⊆ r.preimage s ∩ r.preimage t := image_inter _ s t lemma preimage_union (s t : set β) : r.preimage (s ∪ t) = r.preimage s ∪ r.preimage t := image_union _ s t lemma preimage_id (s : set α) : preimage (@eq α) s = s := by simp only [preimage, inv_id, image_id] lemma preimage_comp (s : rel β γ) (t : set γ) : preimage (r ∘ s) t = preimage r (preimage s t) := by simp only [preimage, inv_comp, image_comp] lemma preimage_univ : r.preimage set.univ = r.dom := by { rw [preimage, image_univ, codom_inv] } /-- Core of a set `s : set β` w.r.t `r : rel α β` is the set of `x : α` that are related *only* to elements of `s`. -/ def core (s : set β) := {x | ∀ y, r x y → y ∈ s} lemma mem_core (x : α) (s : set β) : x ∈ core r s ↔ ∀ y, r x y → y ∈ s := iff.rfl lemma core_subset : ((⊆) ⇒ (⊆)) r.core r.core := assume s t h x h' y rxy, h (h' y rxy) lemma core_mono : monotone r.core := r.core_subset lemma core_inter (s t : set β) : r.core (s ∩ t) = r.core s ∩ r.core t := set.ext (by simp [mem_core, imp_and_distrib, forall_and_distrib]) lemma core_union (s t : set β) : r.core s ∪ r.core t ⊆ r.core (s ∪ t) := r.core_mono.le_map_sup s t lemma core_univ : r.core set.univ = set.univ := set.ext (by simp [mem_core]) lemma core_id (s : set α) : core (@eq α) s = s := by simp [core] lemma core_comp (s : rel β γ) (t : set γ) : core (r ∘ s) t = core r (core s t) := begin ext x, simp [core, comp], split, { intros h y rxy z syz, exact h z y rxy syz }, intros h z y rzy syz, exact h y rzy z syz end /-- Restrict the domain of a relation to a subtype. -/ def restrict_domain (s : set α) : rel {x // x ∈ s} β := λ x y, r x.val y theorem image_subset_iff (s : set α) (t : set β) : image r s ⊆ t ↔ s ⊆ core r t := iff.intro (λ h x xs y rxy, h ⟨x, xs, rxy⟩) (λ h y ⟨x, xs, rxy⟩, h xs y rxy) theorem core_preimage_gc : galois_connection (image r) (core r) := image_subset_iff _ end rel namespace function /-- The graph of a function as a relation. -/ def graph (f : α → β) : rel α β := λ x y, f x = y end function namespace set -- TODO: if image were defined with bounded quantification in corelib, the next two would -- be definitional lemma image_eq (f : α → β) (s : set α) : f '' s = (function.graph f).image s := by simp [set.image, function.graph, rel.image] lemma preimage_eq (f : α → β) (s : set β) : f ⁻¹' s = (function.graph f).preimage s := by simp [set.preimage, function.graph, rel.preimage, rel.inv, flip, rel.image] lemma preimage_eq_core (f : α → β) (s : set β) : f ⁻¹' s = (function.graph f).core s := by simp [set.preimage, function.graph, rel.core] end set
3d9e129884d469237e19e08b52a9b6f60ea29848
94e33a31faa76775069b071adea97e86e218a8ee
/src/linear_algebra/general_linear_group.lean
db3e8fb08c1731ea52c3b5aaa8b7717099c7d52f
[ "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
8,490
lean
/- Copyright (c) 2021 Chris Birkbeck. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Birkbeck -/ import linear_algebra.matrix.nonsingular_inverse import linear_algebra.special_linear_group /-! # The General Linear group $GL(n, R)$ This file defines the elements of the General Linear group `general_linear_group n R`, consisting of all invertible `n` by `n` `R`-matrices. ## Main definitions * `matrix.general_linear_group` is the type of matrices over R which are units in the matrix ring. * `matrix.GL_pos` gives the subgroup of matrices with positive determinant (over a linear ordered ring). ## Tags matrix group, group, matrix inverse -/ namespace matrix universes u v open_locale matrix open linear_map -- disable this instance so we do not accidentally use it in lemmas. local attribute [-instance] special_linear_group.has_coe_to_fun /-- `GL n R` is the group of `n` by `n` `R`-matrices with unit determinant. Defined as a subtype of matrices-/ abbreviation general_linear_group (n : Type u) (R : Type v) [decidable_eq n] [fintype n] [comm_ring R] : Type* := (matrix n n R)ˣ notation `GL` := general_linear_group namespace general_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] /-- The determinant of a unit matrix is itself a unit. -/ @[simps] def det : GL n R →* Rˣ := { to_fun := λ A, { val := (↑A : matrix n n R).det, inv := (↑(A⁻¹) : matrix n n R).det, val_inv := by rw [←det_mul, ←mul_eq_mul, A.mul_inv, det_one], inv_val := by rw [←det_mul, ←mul_eq_mul, A.inv_mul, det_one]}, map_one' := units.ext det_one, map_mul' := λ A B, units.ext $ det_mul _ _ } /--The `GL n R` and `general_linear_group R n` groups are multiplicatively equivalent-/ def to_lin : (GL n R) ≃* (linear_map.general_linear_group R (n → R)) := units.map_equiv to_lin_alg_equiv'.to_mul_equiv /--Given a matrix with invertible determinant we get an element of `GL n R`-/ def mk' (A : matrix n n R) (h : invertible (matrix.det A)) : GL n R := unit_of_det_invertible A /--Given a matrix with unit determinant we get an element of `GL n R`-/ noncomputable def mk'' (A : matrix n n R) (h : is_unit (matrix.det A)) : GL n R := nonsing_inv_unit A h /--Given a matrix with non-zero determinant over a field, we get an element of `GL n K`-/ def mk_of_det_ne_zero {K : Type*} [field K] (A : matrix n n K) (h : matrix.det A ≠ 0) : GL n K := mk' A (invertible_of_nonzero h) lemma ext_iff (A B : GL n R) : A = B ↔ (∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) := units.ext_iff.trans matrix.ext_iff.symm /-- Not marked `@[ext]` as the `ext` tactic already solves this. -/ lemma ext ⦃A B : GL n R⦄ (h : ∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) : A = B := units.ext $ matrix.ext h section coe_lemmas variables (A B : GL n R) @[simp] lemma coe_mul : ↑(A * B) = (↑A : matrix n n R) ⬝ (↑B : matrix n n R) := rfl @[simp] lemma coe_one : ↑(1 : GL n R) = (1 : matrix n n R) := rfl lemma coe_inv : ↑(A⁻¹) = (↑A : matrix n n R)⁻¹ := begin letI := A.invertible, exact inv_of_eq_nonsing_inv (↑A : matrix n n R), end /-- An element of the matrix general linear group on `(n) [fintype n]` can be considered as an element of the endomorphism general linear group on `n → R`. -/ def to_linear : general_linear_group n R ≃* linear_map.general_linear_group R (n → R) := units.map_equiv matrix.to_lin_alg_equiv'.to_ring_equiv.to_mul_equiv -- Note that without the `@` and `‹_›`, lean infers `λ a b, _inst_1 a b` instead of `_inst_1` as the -- decidability argument, which prevents `simp` from obtaining the instance by unification. -- These `λ a b, _inst a b` terms also appear in the type of `A`, but simp doesn't get confused by -- them so for now we do not care. @[simp] lemma coe_to_linear : (@to_linear n ‹_› ‹_› _ _ A : (n → R) →ₗ[R] (n → R)) = matrix.mul_vec_lin A := rfl @[simp] lemma to_linear_apply (v : n → R) : (@to_linear n ‹_› ‹_› _ _ A) v = matrix.mul_vec_lin ↑A v := rfl end coe_lemmas end general_linear_group namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] instance has_coe_to_general_linear_group : has_coe (special_linear_group n R) (GL n R) := ⟨λ A, ⟨↑A, ↑(A⁻¹), congr_arg coe (mul_right_inv A), congr_arg coe (mul_left_inv A)⟩⟩ @[simp] lemma coe_to_GL_det (g : special_linear_group n R) : (g : GL n R).det = 1 := units.ext g.prop end special_linear_group section variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ] section variables (n R) /-- This is the subgroup of `nxn` matrices with entries over a linear ordered ring and positive determinant. -/ def GL_pos : subgroup (GL n R) := (units.pos_subgroup R).comap general_linear_group.det end @[simp] lemma mem_GL_pos (A : GL n R) : A ∈ GL_pos n R ↔ 0 < (A.det : R) := iff.rfl end section has_neg variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ] [fact (even (fintype.card n))] /-- Formal operation of negation on general linear group on even cardinality `n` given by negating each element. -/ instance : has_neg (GL_pos n R) := ⟨λ g, ⟨-g, begin rw [mem_GL_pos, general_linear_group.coe_det_apply, units.coe_neg, det_neg, (fact.out $ even $ fintype.card n).neg_one_pow, one_mul], exact g.prop, end⟩⟩ @[simp] lemma GL_pos.coe_neg_GL (g : GL_pos n R) : ↑(-g) = -(g : GL n R) := rfl @[simp] lemma GL_pos.coe_neg (g : GL_pos n R) : ↑(-g) = -(g : matrix n n R) := rfl @[simp] lemma GL_pos.coe_neg_apply (g : GL_pos n R) (i j : n) : (↑(-g) : matrix n n R) i j = -((↑g : matrix n n R) i j) := rfl instance : has_distrib_neg (GL_pos n R) := subtype.coe_injective.has_distrib_neg _ GL_pos.coe_neg_GL (GL_pos n R).coe_mul end has_neg namespace special_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [linear_ordered_comm_ring R] /-- `special_linear_group n R` embeds into `GL_pos n R` -/ def to_GL_pos : special_linear_group n R →* GL_pos n R := { to_fun := λ A, ⟨(A : GL n R), show 0 < (↑A : matrix n n R).det, from A.prop.symm ▸ zero_lt_one⟩, map_one' := subtype.ext $ units.ext $ rfl, map_mul' := λ A₁ A₂, subtype.ext $ units.ext $ rfl } instance : has_coe (special_linear_group n R) (GL_pos n R) := ⟨to_GL_pos⟩ lemma coe_eq_to_GL_pos : (coe : special_linear_group n R → GL_pos n R) = to_GL_pos := rfl lemma to_GL_pos_injective : function.injective (to_GL_pos : special_linear_group n R → GL_pos n R) := (show function.injective ((coe : GL_pos n R → matrix n n R) ∘ to_GL_pos), from subtype.coe_injective).of_comp /-- Coercing a `special_linear_group` via `GL_pos` and `GL` is the same as coercing striaght to a matrix. -/ @[simp] lemma coe_GL_pos_coe_GL_coe_matrix (g : special_linear_group n R) : (↑(↑(↑g : GL_pos n R) : GL n R) : matrix n n R) = ↑g := rfl @[simp] lemma coe_to_GL_pos_to_GL_det (g : special_linear_group n R) : ((g : GL_pos n R) : GL n R).det = 1 := units.ext g.prop variable [fact (even (fintype.card n))] @[norm_cast] lemma coe_GL_pos_neg (g : special_linear_group n R) : ↑(-g) = -(↑g : GL_pos n R) := subtype.ext $ units.ext rfl end special_linear_group section examples /-- The matrix [a, -b; b, a] (inspired by multiplication by a complex number); it is an element of $GL_2(R)$ if `a ^ 2 + b ^ 2` is nonzero. -/ @[simps coe {fully_applied := ff}] def plane_conformal_matrix {R} [field R] (a b : R) (hab : a ^ 2 + b ^ 2 ≠ 0) : matrix.general_linear_group (fin 2) R := general_linear_group.mk_of_det_ne_zero ![![a, -b], ![b, a]] (by simpa [det_fin_two, sq] using hab) /- TODO: Add Iwasawa matrices `n_x=![![1,x],![0,1]]`, `a_t=![![exp(t/2),0],![0,exp(-t/2)]]` and `k_θ==![![cos θ, sin θ],![-sin θ, cos θ]]` -/ end examples namespace general_linear_group variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R] -- this section should be last to ensure we do not use it in lemmas section coe_fn_instance /-- This instance is here for convenience, but is not the simp-normal form. -/ instance : has_coe_to_fun (GL n R) (λ _, n → n → R) := { coe := λ A, A.val } @[simp] lemma coe_fn_eq_coe (A : GL n R) : ⇑A = (↑A : matrix n n R) := rfl end coe_fn_instance end general_linear_group end matrix
cdf953b9c40f441085e1352e5cb0cf6d75e63d35
4727251e0cd73359b15b664c3170e5d754078599
/src/data/set/constructions.lean
541baeea6c11a5bea20f88d2c36a53df25f65f8c
[ "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
3,020
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 data.finset.basic /-! # Constructions involving sets of sets. ## Finite Intersections We define a structure `has_finite_inter` which asserts that a set `S` of subsets of `α` is closed under finite intersections. We define `finite_inter_closure` which, given a set `S` of subsets of `α`, is the smallest set of subsets of `α` which is closed under finite intersections. `finite_inter_closure S` is endowed with a term of type `has_finite_inter` using `finite_inter_closure_has_finite_inter`. -/ variables {α : Type*} (S : set (set α)) /-- A structure encapsulating the fact that a set of sets is closed under finite intersection. -/ structure has_finite_inter := (univ_mem : set.univ ∈ S) (inter_mem : ∀ ⦃s⦄, s ∈ S → ∀ ⦃t⦄, t ∈ S → s ∩ t ∈ S) namespace has_finite_inter -- Satisfying the inhabited linter... instance : inhabited (has_finite_inter ({set.univ} : set (set α))) := ⟨⟨by tauto, λ _ h1 _ h2, by simp [set.mem_singleton_iff.1 h1, set.mem_singleton_iff.1 h2]⟩⟩ /-- The smallest set of sets containing `S` which is closed under finite intersections. -/ inductive finite_inter_closure : set (set α) | basic {s} : s ∈ S → finite_inter_closure s | univ : finite_inter_closure set.univ | inter {s t} : finite_inter_closure s → finite_inter_closure t → finite_inter_closure (s ∩ t) /-- Defines `has_finite_inter` for `finite_inter_closure S`. -/ def finite_inter_closure_has_finite_inter : has_finite_inter (finite_inter_closure S) := { univ_mem := finite_inter_closure.univ, inter_mem := λ _ h _, finite_inter_closure.inter h } variable {S} lemma finite_inter_mem (cond : has_finite_inter S) (F : finset (set α)) : ↑F ⊆ S → ⋂₀ (↑F : set (set α)) ∈ S := begin classical, refine finset.induction_on F (λ _, _) _, { simp [cond.univ_mem] }, { intros a s h1 h2 h3, suffices : a ∩ ⋂₀ ↑s ∈ S, by simpa, exact cond.inter_mem (h3 (finset.mem_insert_self a s)) (h2 $ λ x hx, h3 $ finset.mem_insert_of_mem hx) } end lemma finite_inter_closure_insert {A : set α} (cond : has_finite_inter S) (P ∈ finite_inter_closure (insert A S)) : P ∈ S ∨ ∃ Q ∈ S, P = A ∩ Q := begin induction H with S h T1 T2 _ _ h1 h2, { cases h, { exact or.inr ⟨set.univ, cond.univ_mem, by simpa⟩ }, { exact or.inl h } }, { exact or.inl cond.univ_mem }, { rcases h1 with (h | ⟨Q, hQ, rfl⟩); rcases h2 with (i | ⟨R, hR, rfl⟩), { exact or.inl (cond.inter_mem h i) }, { exact or.inr ⟨T1 ∩ R, cond.inter_mem h hR, by simp only [ ←set.inter_assoc, set.inter_comm _ A]⟩ }, { exact or.inr ⟨Q ∩ T2, cond.inter_mem hQ i, by simp only [set.inter_assoc]⟩ }, { exact or.inr ⟨Q ∩ R, cond.inter_mem hQ hR, by { ext x, split; simp { contextual := tt} }⟩ } } end end has_finite_inter
30020b09857de68028d3581efbf1bb1001a8a567
649957717d58c43b5d8d200da34bf374293fe739
/src/category_theory/isomorphism.lean
ae939f134be55963699fe5e14931655e33c84f21
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
9,383
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn import category_theory.functor universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory open category structure iso {C : Type u} [category.{v} C] (X Y : C) := (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id' : hom ≫ inv = 𝟙 X . obviously) (inv_hom_id' : inv ≫ hom = 𝟙 Y . obviously) restate_axiom iso.hom_inv_id' restate_axiom iso.inv_hom_id' attribute [simp] iso.hom_inv_id iso.inv_hom_id infixr ` ≅ `:10 := iso -- type as \cong or \iso variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 variables {X Y Z : C} namespace iso @[simp] lemma hom_inv_id_assoc (α : X ≅ Y) (f : X ⟶ Z) : α.hom ≫ α.inv ≫ f = f := by rw [←category.assoc, α.hom_inv_id, category.id_comp] @[simp] lemma inv_hom_id_assoc (α : X ≅ Y) (f : Y ⟶ Z) : α.inv ≫ α.hom ≫ f = f := by rw [←category.assoc, α.inv_hom_id, category.id_comp] @[extensionality] lemma ext (α β : X ≅ Y) (w : α.hom = β.hom) : α = β := suffices α.inv = β.inv, by cases α; cases β; cc, calc α.inv = α.inv ≫ (β.hom ≫ β.inv) : by rw [iso.hom_inv_id, category.comp_id] ... = (α.inv ≫ α.hom) ≫ β.inv : by rw [category.assoc, ←w] ... = β.inv : by rw [iso.inv_hom_id, category.id_comp] @[symm] def symm (I : X ≅ Y) : Y ≅ X := { hom := I.inv, inv := I.hom, hom_inv_id' := I.inv_hom_id', inv_hom_id' := I.hom_inv_id' } @[simp] lemma symm_hom (α : X ≅ Y) : α.symm.hom = α.inv := rfl @[simp] lemma symm_inv (α : X ≅ Y) : α.symm.inv = α.hom := rfl @[simp] lemma symm_mk {X Y : C} (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id) : iso.symm {hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id} = {hom := inv, inv := hom, hom_inv_id' := inv_hom_id, inv_hom_id' := hom_inv_id} := rfl @[refl] def refl (X : C) : X ≅ X := { hom := 𝟙 X, inv := 𝟙 X } @[simp] lemma refl_hom (X : C) : (iso.refl X).hom = 𝟙 X := rfl @[simp] lemma refl_inv (X : C) : (iso.refl X).inv = 𝟙 X := rfl @[trans] def trans (α : X ≅ Y) (β : Y ≅ Z) : X ≅ Z := { hom := α.hom ≫ β.hom, inv := β.inv ≫ α.inv } infixr ` ≪≫ `:80 := iso.trans -- type as `\ll \gg`. @[simp] lemma trans_hom (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).hom = α.hom ≫ β.hom := rfl @[simp] lemma trans_inv (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).inv = β.inv ≫ α.inv := rfl @[simp] lemma trans_mk {X Y Z : C} (hom : X ⟶ Y) (inv : Y ⟶ X) (hom_inv_id) (inv_hom_id) (hom' : Y ⟶ Z) (inv' : Z ⟶ Y) (hom_inv_id') (inv_hom_id') (hom_inv_id'') (inv_hom_id'') : iso.trans {hom := hom, inv := inv, hom_inv_id' := hom_inv_id, inv_hom_id' := inv_hom_id} {hom := hom', inv := inv', hom_inv_id' := hom_inv_id', inv_hom_id' := inv_hom_id'} = {hom := hom ≫ hom', inv := inv' ≫ inv, hom_inv_id' := hom_inv_id'', inv_hom_id' := inv_hom_id''} := rfl @[simp] lemma refl_symm (X : C) : (iso.refl X).hom = 𝟙 X := rfl @[simp] lemma trans_symm (α : X ≅ Y) (β : Y ≅ Z) : (α ≪≫ β).inv = β.inv ≫ α.inv := rfl lemma inv_comp_eq (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : α.inv ≫ f = g ↔ f = α.hom ≫ g := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_inv_comp (α : X ≅ Y) {f : X ⟶ Z} {g : Y ⟶ Z} : g = α.inv ≫ f ↔ α.hom ≫ g = f := (inv_comp_eq α.symm).symm lemma comp_inv_eq (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : f ≫ α.inv = g ↔ f = g ≫ α.hom := ⟨λ H, by simp [H.symm], λ H, by simp [H]⟩ lemma eq_comp_inv (α : X ≅ Y) {f : Z ⟶ Y} {g : Z ⟶ X} : g = f ≫ α.inv ↔ g ≫ α.hom = f := (comp_inv_eq α.symm).symm lemma inv_eq_inv (f g : X ≅ Y) : f.inv = g.inv ↔ f.hom = g.hom := have ∀{X Y : C} (f g : X ≅ Y), f.hom = g.hom → f.inv = g.inv, from λ X Y f g h, by rw [ext _ _ h], ⟨this f.symm g.symm, this f g⟩ lemma hom_comp_eq_id (α : X ≅ Y) {f : Y ⟶ X} : α.hom ≫ f = 𝟙 X ↔ f = α.inv := by rw [←eq_inv_comp, comp_id] lemma comp_hom_eq_id (α : X ≅ Y) {f : Y ⟶ X} : f ≫ α.hom = 𝟙 Y ↔ f = α.inv := by rw [←eq_comp_inv, id_comp] lemma hom_eq_inv (α : X ≅ Y) (β : Y ≅ X) : α.hom = β.inv ↔ β.hom = α.inv := by { erw [inv_eq_inv α.symm β, eq_comm], refl } end iso /-- `is_iso` typeclass expressing that a morphism is invertible. This contains the data of the inverse, but is a subsingleton type. -/ class is_iso (f : X ⟶ Y) := (inv : Y ⟶ X) (hom_inv_id' : f ≫ inv = 𝟙 X . obviously) (inv_hom_id' : inv ≫ f = 𝟙 Y . obviously) def inv (f : X ⟶ Y) [is_iso f] := is_iso.inv f namespace is_iso @[simp] lemma hom_inv_id (f : X ⟶ Y) [is_iso f] : f ≫ category_theory.inv f = 𝟙 X := is_iso.hom_inv_id' f @[simp] lemma inv_hom_id (f : X ⟶ Y) [is_iso f] : category_theory.inv f ≫ f = 𝟙 Y := is_iso.inv_hom_id' f @[simp] lemma hom_inv_id_assoc {Z} (f : X ⟶ Y) [is_iso f] (g : X ⟶ Z) : f ≫ category_theory.inv f ≫ g = g := by rw [←category.assoc, hom_inv_id, category.id_comp] @[simp] lemma inv_hom_id_assoc {Z} (f : X ⟶ Y) [is_iso f] (g : Y ⟶ Z) : category_theory.inv f ≫ f ≫ g = g := by rw [←category.assoc, inv_hom_id, category.id_comp] instance (X : C) : is_iso (𝟙 X) := { inv := 𝟙 X } instance of_iso (f : X ≅ Y) : is_iso f.hom := { inv := f.inv } instance of_iso_inverse (f : X ≅ Y) : is_iso f.inv := { inv := f.hom } variables {f g : X ⟶ Y} {h : Y ⟶ Z} instance inv_is_iso [is_iso f] : is_iso (category_theory.inv f) := { inv := f, hom_inv_id' := inv_hom_id f, inv_hom_id' := hom_inv_id f } instance comp_is_iso [is_iso f] [is_iso h] : is_iso (f ≫ h) := { inv := category_theory.inv h ≫ category_theory.inv f, hom_inv_id' := begin erw [category.assoc, hom_inv_id_assoc], exact hom_inv_id f, end, inv_hom_id' := begin erw [category.assoc, inv_hom_id_assoc], exact inv_hom_id h, end } @[simp] lemma inv_id : category_theory.inv (𝟙 X) = 𝟙 X := rfl @[simp] lemma inv_comp [is_iso f] [is_iso h] : category_theory.inv (f ≫ h) = category_theory.inv h ≫ category_theory.inv f := rfl @[simp] lemma is_iso.inv_inv [is_iso f] : category_theory.inv (category_theory.inv f) = f := rfl @[simp] lemma iso.inv_inv (f : X ≅ Y) : category_theory.inv (f.inv) = f.hom := rfl @[simp] lemma iso.inv_hom (f : X ≅ Y) : category_theory.inv (f.hom) = f.inv := rfl instance epi_of_iso (f : X ⟶ Y) [is_iso f] : epi f := { left_cancellation := λ Z g h w, -- This is an interesting test case for better rewrite automation. by rw [←category.id_comp C g, ←category.id_comp C h, ←is_iso.inv_hom_id f, category.assoc, w, category.assoc] } instance mono_of_iso (f : X ⟶ Y) [is_iso f] : mono f := { right_cancellation := λ Z g h w, by rw [←category.comp_id C g, ←category.comp_id C h, ←is_iso.hom_inv_id f, ←category.assoc, w, ←category.assoc] } end is_iso open is_iso lemma eq_of_inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] (p : inv f = inv g) : f = g := begin apply (cancel_epi (inv f)).1, erw [inv_hom_id, p, inv_hom_id], end def as_iso (f : X ⟶ Y) [is_iso f] : X ≅ Y := { hom := f, inv := inv f } @[simp] lemma as_iso_hom (f : X ⟶ Y) [is_iso f] : (as_iso f).hom = f := rfl @[simp] lemma as_iso_inv (f : X ⟶ Y) [is_iso f] : (as_iso f).inv = inv f := rfl instance (f : X ⟶ Y) : subsingleton (is_iso f) := ⟨λ a b, suffices a.inv = b.inv, by cases a; cases b; congr; exact this, show (@as_iso C _ _ _ f a).inv = (@as_iso C _ _ _ f b).inv, by congr' 1; ext; refl⟩ lemma is_iso.inv_eq_inv {f g : X ⟶ Y} [is_iso f] [is_iso g] : inv f = inv g ↔ f = g := iso.inv_eq_inv (as_iso f) (as_iso g) instance is_iso_comp (f : X ⟶ Y) (g : Y ⟶ Z) [is_iso f] [is_iso g] : is_iso (f ≫ g) := { inv := inv g ≫ inv f } instance is_iso_id : is_iso (𝟙 X) := { inv := 𝟙 X } namespace functor universes u₁ v₁ u₂ v₂ variables {D : Type u₂} variables [𝒟 : category.{v₂} D] include 𝒟 def map_iso (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : F.obj X ≅ F.obj Y := { hom := F.map i.hom, inv := F.map i.inv, hom_inv_id' := by rw [←map_comp, iso.hom_inv_id, ←map_id], inv_hom_id' := by rw [←map_comp, iso.inv_hom_id, ←map_id] } @[simp] lemma map_iso_hom (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : (F.map_iso i).hom = F.map i.hom := rfl @[simp] lemma map_iso_inv (F : C ⥤ D) {X Y : C} (i : X ≅ Y) : (F.map_iso i).inv = F.map i.inv := rfl @[simp] lemma map_iso_trans (F : C ⥤ D) {X Y Z : C} (i : X ≅ Y) (j : Y ≅ Z) : F.map_iso (i ≪≫ j) = (F.map_iso i) ≪≫ (F.map_iso j) := by ext; apply functor.map_comp instance (F : C ⥤ D) (f : X ⟶ Y) [is_iso f] : is_iso (F.map f) := { ..(F.map_iso (as_iso f)) } @[simp] lemma map_hom_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map f ≫ F.map (inv f) = 𝟙 (F.obj X) := by rw [←map_comp, is_iso.hom_inv_id, map_id] @[simp] lemma map_inv_hom (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map (inv f) ≫ F.map f = 𝟙 (F.obj Y) := by rw [←map_comp, is_iso.inv_hom_id, map_id] @[simp] lemma map_inv (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) [is_iso f] : F.map (inv f) = inv (F.map f) := rfl end functor end category_theory
da79f10a8cc15ad982cc8613bac946abbca9bbe2
5883d9218e6f144e20eee6ca1dab8529fa1a97c0
/src/exp/subst/default.lean
f76bf32c00104750f279f46d2570f0c23c1d8bab
[]
no_license
spl/alpha-conversion-is-easy
0d035bc570e52a6345d4890e4d0c9e3f9b8126c1
ed937fe85d8495daffd9412a5524c77b9fcda094
refs/heads/master
1,607,649,280,020
1,517,380,240,000
1,517,380,240,000
52,174,747
4
0
null
1,456,052,226,000
1,456,001,163,000
Lean
UTF-8
Lean
false
false
42
lean
import .type import .update import .apply
5d5b19da96a69092b75accb937d6d56093da0bc2
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/stage0/src/Lean/Parser/Basic.lean
26eb6471af08fbaadc6f19d9cd7e5a033bc373d1
[ "Apache-2.0" ]
permissive
gebner/lean4-old
a4129a041af2d4d12afb3a8d4deedabde727719b
ee51cdfaf63ee313c914d83264f91f414a0e3b6e
refs/heads/master
1,683,628,606,745
1,622,651,300,000
1,622,654,405,000
142,608,821
1
0
null
null
null
null
UTF-8
Lean
false
false
76,165
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ /-! # Basic Lean parser infrastructure The Lean parser was developed with the following primary goals in mind: * flexibility: Lean's grammar is complex and includes indentation and other whitespace sensitivity. It should be possible to introduce such custom "tweaks" locally without having to adjust the fundamental parsing approach. * extensibility: Lean's grammar can be extended on the fly within a Lean file, and with Lean 4 we want to extend this to cover embedding domain-specific languages that may look nothing like Lean, down to using a separate set of tokens. * losslessness: The parser should produce a concrete syntax tree that preserves all whitespace and other "sub-token" information for the use in tooling. * performance: The overhead of the parser building blocks, and the overall parser performance on average-complexity input, should be comparable with that of the previous parser hand-written in C++. No fancy optimizations should be necessary for this. Given these constraints, we decided to implement a combinatoric, non-monadic, lexer-less, memoizing recursive-descent parser. Using combinators instead of some more formal and introspectible grammar representation ensures ultimate flexibility as well as efficient extensibility: there is (almost) no pre-processing necessary when extending the grammar with a new parser. However, because the all results the combinators produce are of the homogeneous `Syntax` type, the basic parser type is not actually a monad but a monomorphic linear function `ParserState → ParserState`, avoiding constructing and deconstructing countless monadic return values. Instead of explicitly returning syntax objects, parsers push (zero or more of) them onto a syntax stack inside the linear state. Chaining parsers via `>>` accumulates their output on the stack. Combinators such as `node` then pop off all syntax objects produced during their invocation and wrap them in a single `Syntax.node` object that is again pushed on this stack. Instead of calling `node` directly, we usually use the macro `leading_parser p`, which unfolds to `node k p` where the new syntax node kind `k` is the name of the declaration being defined. The lack of a dedicated lexer ensures we can modify and replace the lexical grammar at any point, and simplifies detecting and propagating whitespace. The parser still has a concept of "tokens", however, and caches the most recent one for performance: when `tokenFn` is called twice at the same position in the input, it will reuse the result of the first call. `tokenFn` recognizes some built-in variable-length tokens such as identifiers as well as any fixed token in the `ParserContext`'s `TokenTable` (a trie); however, the same cache field and strategy could be reused by custom token parsers. Tokens also play a central role in the `prattParser` combinator, which selects a *leading* parser followed by zero or more *trailing* parsers based on the current token (via `peekToken`); see the documentation of `prattParser` for more details. Tokens are specified via the `symbol` parser, or with `symbolNoWs` for tokens that should not be preceded by whitespace. The `Parser` type is extended with additional metadata over the mere parsing function to propagate token information: `collectTokens` collects all tokens within a parser for registering. `firstTokens` holds information about the "FIRST" token set used to speed up parser selection in `prattParser`. This approach of combining static and dynamic information in the parser type is inspired by the paper "Deterministic, Error-Correcting Combinator Parsers" by Swierstra and Duponcheel. If multiple parsers accept the same current token, `prattParser` tries all of them using the backtracking `longestMatchFn` combinator. This is the only case where standard parsers might execute arbitrary backtracking. At the moment there is no memoization shared by these parallel parsers apart from the first token, though we might change this in the future if the need arises. Finally, error reporting follows the standard combinatoric approach of collecting a single unexpected token/... and zero or more expected tokens (see `Error` below). Expected tokens are e.g. set by `symbol` and merged by `<|>`. Combinators running multiple parsers should check if an error message is set in the parser state (`hasError`) and act accordingly. Error recovery is left to the designer of the specific language; for example, Lean's top-level `parseCommand` loop skips tokens until the next command keyword on error. -/ import Lean.Data.Trie import Lean.Data.Position import Lean.Syntax import Lean.ToExpr import Lean.Environment import Lean.Attributes import Lean.Message import Lean.Compiler.InitAttr import Lean.ResolveName namespace Lean namespace Parser def isLitKind (k : SyntaxNodeKind) : Bool := k == strLitKind || k == numLitKind || k == charLitKind || k == nameLitKind || k == scientificLitKind abbrev mkAtom (info : SourceInfo) (val : String) : Syntax := Syntax.atom info val abbrev mkIdent (info : SourceInfo) (rawVal : Substring) (val : Name) : Syntax := Syntax.ident info rawVal val [] /- Return character after position `pos` -/ def getNext (input : String) (pos : Nat) : Char := input.get (input.next pos) /- Maximal (and function application) precedence. In the standard lean language, no parser has precedence higher than `maxPrec`. Note that nothing prevents users from using a higher precedence, but we strongly discourage them from doing it. -/ def maxPrec : Nat := eval_prec max def argPrec : Nat := eval_prec arg def leadPrec : Nat := eval_prec lead def minPrec : Nat := eval_prec min abbrev Token := String structure TokenCacheEntry where startPos : String.Pos := 0 stopPos : String.Pos := 0 token : Syntax := Syntax.missing structure ParserCache where tokenCache : TokenCacheEntry def initCacheForInput (input : String) : ParserCache := { tokenCache := { startPos := input.bsize + 1 /- make sure it is not a valid position -/} } abbrev TokenTable := Trie Token abbrev SyntaxNodeKindSet := Std.PersistentHashMap SyntaxNodeKind Unit def SyntaxNodeKindSet.insert (s : SyntaxNodeKindSet) (k : SyntaxNodeKind) : SyntaxNodeKindSet := Std.PersistentHashMap.insert s k () /- Input string and related data. Recall that the `FileMap` is a helper structure for mapping `String.Pos` in the input string to line/column information. -/ structure InputContext where input : String fileName : String fileMap : FileMap deriving Inhabited /-- Input context derived from elaboration of previous commands. -/ structure ParserModuleContext where env : Environment options : Options -- for name lookup currNamespace : Name := Name.anonymous openDecls : List OpenDecl := [] structure ParserContext extends InputContext, ParserModuleContext where prec : Nat tokens : TokenTable quotDepth : Nat := 0 suppressInsideQuot : Bool := false savedPos? : Option String.Pos := none forbiddenTk? : Option Token := none def ParserContext.resolveName (ctx : ParserContext) (id : Name) : List (Name × List String) := ResolveName.resolveGlobalName ctx.env ctx.currNamespace ctx.openDecls id structure Error where unexpected : String := "" expected : List String := [] deriving Inhabited, BEq namespace Error private def expectedToString : List String → String | [] => "" | [e] => e | [e1, e2] => e1 ++ " or " ++ e2 | e::es => e ++ ", " ++ expectedToString es protected def toString (e : Error) : String := let unexpected := if e.unexpected == "" then [] else [e.unexpected] let expected := if e.expected == [] then [] else let expected := e.expected.toArray.qsort (fun e e' => e < e') let expected := expected.toList.eraseReps ["expected " ++ expectedToString expected] "; ".intercalate $ unexpected ++ expected instance : ToString Error := ⟨Error.toString⟩ def merge (e₁ e₂ : Error) : Error := match e₂ with | { unexpected := u, .. } => { unexpected := if u == "" then e₁.unexpected else u, expected := e₁.expected ++ e₂.expected } end Error structure ParserState where stxStack : Array Syntax := #[] /-- Set to the precedence of the preceding (not surrounding) parser by `runLongestMatchParser` for the use of `checkLhsPrec` in trailing parsers. Note that with chaining, the preceding parser can be another trailing parser: in `1 * 2 + 3`, the preceding parser is '*' when '+' is executed. -/ lhsPrec : Nat := 0 pos : String.Pos := 0 cache : ParserCache errorMsg : Option Error := none namespace ParserState @[inline] def hasError (s : ParserState) : Bool := s.errorMsg != none @[inline] def stackSize (s : ParserState) : Nat := s.stxStack.size def restore (s : ParserState) (iniStackSz : Nat) (iniPos : Nat) : ParserState := { s with stxStack := s.stxStack.shrink iniStackSz, errorMsg := none, pos := iniPos } def setPos (s : ParserState) (pos : Nat) : ParserState := { s with pos := pos } def setCache (s : ParserState) (cache : ParserCache) : ParserState := { s with cache := cache } def pushSyntax (s : ParserState) (n : Syntax) : ParserState := { s with stxStack := s.stxStack.push n } def popSyntax (s : ParserState) : ParserState := { s with stxStack := s.stxStack.pop } def shrinkStack (s : ParserState) (iniStackSz : Nat) : ParserState := { s with stxStack := s.stxStack.shrink iniStackSz } def next (s : ParserState) (input : String) (pos : Nat) : ParserState := { s with pos := input.next pos } def toErrorMsg (ctx : ParserContext) (s : ParserState) : String := match s.errorMsg with | none => "" | some msg => let pos := ctx.fileMap.toPosition s.pos mkErrorStringWithPos ctx.fileName pos (toString msg) def mkNode (s : ParserState) (k : SyntaxNodeKind) (iniStackSz : Nat) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, err⟩ => if err != none && stack.size == iniStackSz then -- If there is an error but there are no new nodes on the stack, use `missing` instead. -- Thus we ensure the property that an syntax tree contains (at least) one `missing` node -- if (and only if) there was a parse error. -- We should not create an actual node of kind `k` in this case because it would mean we -- choose an "arbitrary" node (in practice the last one) in an alternative of the form -- `node k1 p1 <|> ... <|> node kn pn` when all parsers fail. With the code below we -- instead return a less misleading single `missing` node without randomly selecting any `ki`. let stack := stack.push Syntax.missing ⟨stack, lhsPrec, pos, cache, err⟩ else let newNode := Syntax.node k (stack.extract iniStackSz stack.size) let stack := stack.shrink iniStackSz let stack := stack.push newNode ⟨stack, lhsPrec, pos, cache, err⟩ def mkTrailingNode (s : ParserState) (k : SyntaxNodeKind) (iniStackSz : Nat) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, err⟩ => let newNode := Syntax.node k (stack.extract (iniStackSz - 1) stack.size) let stack := stack.shrink (iniStackSz - 1) let stack := stack.push newNode ⟨stack, lhsPrec, pos, cache, err⟩ def setError (s : ParserState) (msg : String) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, _⟩ => ⟨stack, lhsPrec, pos, cache, some { expected := [ msg ] }⟩ def mkError (s : ParserState) (msg : String) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, _⟩ => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { expected := [ msg ] }⟩ def mkUnexpectedError (s : ParserState) (msg : String) (expected : List String := []) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, _⟩ => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { unexpected := msg, expected := expected }⟩ def mkEOIError (s : ParserState) (expected : List String := []) : ParserState := s.mkUnexpectedError "unexpected end of input" expected def mkErrorAt (s : ParserState) (msg : String) (pos : String.Pos) (initStackSz? : Option Nat := none) : ParserState := match s, initStackSz? with | ⟨stack, lhsPrec, _, cache, _⟩, none => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { expected := [ msg ] }⟩ | ⟨stack, lhsPrec, _, cache, _⟩, some sz => ⟨stack.shrink sz |>.push Syntax.missing, lhsPrec, pos, cache, some { expected := [ msg ] }⟩ def mkErrorsAt (s : ParserState) (ex : List String) (pos : String.Pos) (initStackSz? : Option Nat := none) : ParserState := match s, initStackSz? with | ⟨stack, lhsPrec, _, cache, _⟩, none => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { expected := ex }⟩ | ⟨stack, lhsPrec, _, cache, _⟩, some sz => ⟨stack.shrink sz |>.push Syntax.missing, lhsPrec, pos, cache, some { expected := ex }⟩ def mkUnexpectedErrorAt (s : ParserState) (msg : String) (pos : String.Pos) : ParserState := match s with | ⟨stack, lhsPrec, _, cache, _⟩ => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { unexpected := msg }⟩ end ParserState def ParserFn := ParserContext → ParserState → ParserState instance : Inhabited ParserFn where default := fun ctx s => s inductive FirstTokens where | epsilon : FirstTokens | unknown : FirstTokens | tokens : List Token → FirstTokens | optTokens : List Token → FirstTokens deriving Inhabited namespace FirstTokens def seq : FirstTokens → FirstTokens → FirstTokens | epsilon, tks => tks | optTokens s₁, optTokens s₂ => optTokens (s₁ ++ s₂) | optTokens s₁, tokens s₂ => tokens (s₁ ++ s₂) | tks, _ => tks def toOptional : FirstTokens → FirstTokens | tokens tks => optTokens tks | tks => tks def merge : FirstTokens → FirstTokens → FirstTokens | epsilon, tks => toOptional tks | tks, epsilon => toOptional tks | tokens s₁, tokens s₂ => tokens (s₁ ++ s₂) | optTokens s₁, optTokens s₂ => optTokens (s₁ ++ s₂) | tokens s₁, optTokens s₂ => optTokens (s₁ ++ s₂) | optTokens s₁, tokens s₂ => optTokens (s₁ ++ s₂) | _, _ => unknown def toStr : FirstTokens → String | epsilon => "epsilon" | unknown => "unknown" | tokens tks => toString tks | optTokens tks => "?" ++ toString tks instance : ToString FirstTokens := ⟨toStr⟩ end FirstTokens structure ParserInfo where collectTokens : List Token → List Token := id collectKinds : SyntaxNodeKindSet → SyntaxNodeKindSet := id firstTokens : FirstTokens := FirstTokens.unknown deriving Inhabited structure Parser where info : ParserInfo := {} fn : ParserFn deriving Inhabited abbrev TrailingParser := Parser def dbgTraceStateFn (label : String) (p : ParserFn) : ParserFn := fun c s => let sz := s.stxStack.size let s' := p c s dbg_trace "{label} pos: {s'.pos} err: {s'.errorMsg} out: {s'.stxStack.extract sz s'.stxStack.size}" s' def dbgTraceState (label : String) (p : Parser) : Parser where fn := dbgTraceStateFn label p.fn info := p.info @[noinline] def epsilonInfo : ParserInfo := { firstTokens := FirstTokens.epsilon } @[inline] def checkStackTopFn (p : Syntax → Bool) (msg : String) : ParserFn := fun c s => if p s.stxStack.back then s else s.mkUnexpectedError msg @[inline] def checkStackTop (p : Syntax → Bool) (msg : String) : Parser := { info := epsilonInfo, fn := checkStackTopFn p msg } @[inline] def andthenFn (p q : ParserFn) : ParserFn := fun c s => let s := p c s if s.hasError then s else q c s @[noinline] def andthenInfo (p q : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens ∘ q.collectTokens, collectKinds := p.collectKinds ∘ q.collectKinds, firstTokens := p.firstTokens.seq q.firstTokens } @[inline] def andthen (p q : Parser) : Parser := { info := andthenInfo p.info q.info, fn := andthenFn p.fn q.fn } instance : AndThen Parser := ⟨andthen⟩ @[inline] def nodeFn (n : SyntaxNodeKind) (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let s := p c s s.mkNode n iniSz @[inline] def trailingNodeFn (n : SyntaxNodeKind) (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let s := p c s s.mkTrailingNode n iniSz @[noinline] def nodeInfo (n : SyntaxNodeKind) (p : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens, collectKinds := fun s => (p.collectKinds s).insert n, firstTokens := p.firstTokens } @[inline] def node (n : SyntaxNodeKind) (p : Parser) : Parser := { info := nodeInfo n p.info, fn := nodeFn n p.fn } def errorFn (msg : String) : ParserFn := fun _ s => s.mkUnexpectedError msg @[inline] def error (msg : String) : Parser := { info := epsilonInfo, fn := errorFn msg } def errorAtSavedPosFn (msg : String) (delta : Bool) : ParserFn := fun c s => match c.savedPos? with | none => s | some pos => let pos := if delta then c.input.next pos else pos match s with | ⟨stack, lhsPrec, _, cache, _⟩ => ⟨stack.push Syntax.missing, lhsPrec, pos, cache, some { unexpected := msg }⟩ /- Generate an error at the position saved with the `withPosition` combinator. If `delta == true`, then it reports at saved position+1. This useful to make sure a parser consumed at least one character. -/ @[inline] def errorAtSavedPos (msg : String) (delta : Bool) : Parser := { fn := errorAtSavedPosFn msg delta } /- Succeeds if `c.prec <= prec` -/ def checkPrecFn (prec : Nat) : ParserFn := fun c s => if c.prec <= prec then s else s.mkUnexpectedError "unexpected token at this precedence level; consider parenthesizing the term" @[inline] def checkPrec (prec : Nat) : Parser := { info := epsilonInfo, fn := checkPrecFn prec } /- Succeeds if `c.lhsPrec >= prec` -/ def checkLhsPrecFn (prec : Nat) : ParserFn := fun c s => if s.lhsPrec >= prec then s else s.mkUnexpectedError "unexpected token at this precedence level; consider parenthesizing the term" @[inline] def checkLhsPrec (prec : Nat) : Parser := { info := epsilonInfo, fn := checkLhsPrecFn prec } def setLhsPrecFn (prec : Nat) : ParserFn := fun c s => if s.hasError then s else { s with lhsPrec := prec } @[inline] def setLhsPrec (prec : Nat) : Parser := { info := epsilonInfo, fn := setLhsPrecFn prec } def checkInsideQuotFn : ParserFn := fun c s => if c.quotDepth > 0 && !c.suppressInsideQuot then s else s.mkUnexpectedError "unexpected syntax outside syntax quotation" @[inline] def checkInsideQuot : Parser := { info := epsilonInfo, fn := checkInsideQuotFn } def checkOutsideQuotFn : ParserFn := fun c s => if !c.quotDepth == 0 || c.suppressInsideQuot then s else s.mkUnexpectedError "unexpected syntax inside syntax quotation" @[inline] def checkOutsideQuot : Parser := { info := epsilonInfo, fn := checkOutsideQuotFn } def addQuotDepthFn (i : Int) (p : ParserFn) : ParserFn := fun c s => p { c with quotDepth := c.quotDepth + i |>.toNat } s @[inline] def incQuotDepth (p : Parser) : Parser := { info := p.info, fn := addQuotDepthFn 1 p.fn } @[inline] def decQuotDepth (p : Parser) : Parser := { info := p.info, fn := addQuotDepthFn (-1) p.fn } def suppressInsideQuotFn (p : ParserFn) : ParserFn := fun c s => p { c with suppressInsideQuot := true } s @[inline] def suppressInsideQuot (p : Parser) : Parser := { info := p.info, fn := suppressInsideQuotFn p.fn } @[inline] def leadingNode (n : SyntaxNodeKind) (prec : Nat) (p : Parser) : Parser := checkPrec prec >> node n p >> setLhsPrec prec @[inline] def trailingNodeAux (n : SyntaxNodeKind) (p : Parser) : TrailingParser := { info := nodeInfo n p.info, fn := trailingNodeFn n p.fn } @[inline] def trailingNode (n : SyntaxNodeKind) (prec lhsPrec : Nat) (p : Parser) : TrailingParser := checkPrec prec >> checkLhsPrec lhsPrec >> trailingNodeAux n p >> setLhsPrec prec def mergeOrElseErrors (s : ParserState) (error1 : Error) (iniPos : Nat) (mergeErrors : Bool) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, some error2⟩ => if pos == iniPos then ⟨stack, lhsPrec, pos, cache, some (if mergeErrors then error1.merge error2 else error2)⟩ else s | other => other def orelseFnCore (p q : ParserFn) (mergeErrors : Bool) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := p c s match s.errorMsg with | some errorMsg => if s.pos == iniPos then mergeOrElseErrors (q c (s.restore iniSz iniPos)) errorMsg iniPos mergeErrors else s | none => s @[inline] def orelseFn (p q : ParserFn) : ParserFn := orelseFnCore p q true @[noinline] def orelseInfo (p q : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens ∘ q.collectTokens, collectKinds := p.collectKinds ∘ q.collectKinds, firstTokens := p.firstTokens.merge q.firstTokens } /-- Run `p`, falling back to `q` if `p` failed without consuming any input. NOTE: In order for the pretty printer to retrace an `orelse`, `p` must be a call to `node` or some other parser producing a single node kind. Nested `orelse` calls are flattened for this, i.e. `(node k1 p1 <|> node k2 p2) <|> ...` is fine as well. -/ @[inline] def orelse (p q : Parser) : Parser := { info := orelseInfo p.info q.info, fn := orelseFn p.fn q.fn } instance : OrElse Parser := ⟨orelse⟩ @[noinline] def noFirstTokenInfo (info : ParserInfo) : ParserInfo := { collectTokens := info.collectTokens, collectKinds := info.collectKinds } def atomicFn (p : ParserFn) : ParserFn := fun c s => let iniPos := s.pos match p c s with | ⟨stack, lhsPrec, _, cache, some msg⟩ => ⟨stack, lhsPrec, iniPos, cache, some msg⟩ | other => other @[inline] def atomic (p : Parser) : Parser := { info := p.info, fn := atomicFn p.fn } def optionalFn (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := p c s let s := if s.hasError && s.pos == iniPos then s.restore iniSz iniPos else s s.mkNode nullKind iniSz @[noinline] def optionaInfo (p : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens, collectKinds := p.collectKinds, firstTokens := p.firstTokens.toOptional } @[inline] def optionalNoAntiquot (p : Parser) : Parser := { info := optionaInfo p.info, fn := optionalFn p.fn } def lookaheadFn (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := p c s if s.hasError then s else s.restore iniSz iniPos @[inline] def lookahead (p : Parser) : Parser := { info := p.info, fn := lookaheadFn p.fn } def notFollowedByFn (p : ParserFn) (msg : String) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := p c s if s.hasError then s.restore iniSz iniPos else let s := s.restore iniSz iniPos s.mkUnexpectedError s!"unexpected {msg}" @[inline] def notFollowedBy (p : Parser) (msg : String) : Parser := { fn := notFollowedByFn p.fn msg } partial def manyAux (p : ParserFn) : ParserFn := fun c s => do let iniSz := s.stackSize let iniPos := s.pos let mut s := p c s if s.hasError then return if iniPos == s.pos then s.restore iniSz iniPos else s if iniPos == s.pos then return s.mkUnexpectedError "invalid 'many' parser combinator application, parser did not consume anything" if s.stackSize > iniSz + 1 then s := s.mkNode nullKind iniSz manyAux p c s @[inline] def manyFn (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let s := manyAux p c s s.mkNode nullKind iniSz @[inline] def manyNoAntiquot (p : Parser) : Parser := { info := noFirstTokenInfo p.info, fn := manyFn p.fn } @[inline] def many1Fn (p : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let s := andthenFn p (manyAux p) c s s.mkNode nullKind iniSz @[inline] def many1NoAntiquot (p : Parser) : Parser := { info := p.info, fn := many1Fn p.fn } private partial def sepByFnAux (p : ParserFn) (sep : ParserFn) (allowTrailingSep : Bool) (iniSz : Nat) (pOpt : Bool) : ParserFn := let rec parse (pOpt : Bool) (c s) := do let sz := s.stackSize let pos := s.pos let mut s := p c s if s.hasError then if s.pos > pos then return s.mkNode nullKind iniSz else if pOpt then s := s.restore sz pos return s.mkNode nullKind iniSz else -- append `Syntax.missing` to make clear that List is incomplete s := s.pushSyntax Syntax.missing return s.mkNode nullKind iniSz if s.stackSize > sz + 1 then s := s.mkNode nullKind sz let sz := s.stackSize let pos := s.pos s := sep c s if s.hasError then s := s.restore sz pos return s.mkNode nullKind iniSz if s.stackSize > sz + 1 then s := s.mkNode nullKind sz parse allowTrailingSep c s parse pOpt def sepByFn (allowTrailingSep : Bool) (p : ParserFn) (sep : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize sepByFnAux p sep allowTrailingSep iniSz true c s def sepBy1Fn (allowTrailingSep : Bool) (p : ParserFn) (sep : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize sepByFnAux p sep allowTrailingSep iniSz false c s @[noinline] def sepByInfo (p sep : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens ∘ sep.collectTokens, collectKinds := p.collectKinds ∘ sep.collectKinds } @[noinline] def sepBy1Info (p sep : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens ∘ sep.collectTokens, collectKinds := p.collectKinds ∘ sep.collectKinds, firstTokens := p.firstTokens } @[inline] def sepByNoAntiquot (p sep : Parser) (allowTrailingSep : Bool := false) : Parser := { info := sepByInfo p.info sep.info, fn := sepByFn allowTrailingSep p.fn sep.fn } @[inline] def sepBy1NoAntiquot (p sep : Parser) (allowTrailingSep : Bool := false) : Parser := { info := sepBy1Info p.info sep.info, fn := sepBy1Fn allowTrailingSep p.fn sep.fn } /- Apply `f` to the syntax object produced by `p` -/ def withResultOfFn (p : ParserFn) (f : Syntax → Syntax) : ParserFn := fun c s => let s := p c s if s.hasError then s else let stx := s.stxStack.back s.popSyntax.pushSyntax (f stx) @[noinline] def withResultOfInfo (p : ParserInfo) : ParserInfo := { collectTokens := p.collectTokens, collectKinds := p.collectKinds } @[inline] def withResultOf (p : Parser) (f : Syntax → Syntax) : Parser := { info := withResultOfInfo p.info, fn := withResultOfFn p.fn f } @[inline] def many1Unbox (p : Parser) : Parser := withResultOf (many1NoAntiquot p) fun stx => if stx.getNumArgs == 1 then stx.getArg 0 else stx partial def satisfyFn (p : Char → Bool) (errorMsg : String := "unexpected character") : ParserFn := fun c s => let i := s.pos if c.input.atEnd i then s.mkEOIError else if p (c.input.get i) then s.next c.input i else s.mkUnexpectedError errorMsg partial def takeUntilFn (p : Char → Bool) : ParserFn := fun c s => let i := s.pos if c.input.atEnd i then s else if p (c.input.get i) then s else takeUntilFn p c (s.next c.input i) def takeWhileFn (p : Char → Bool) : ParserFn := takeUntilFn (fun c => !p c) @[inline] def takeWhile1Fn (p : Char → Bool) (errorMsg : String) : ParserFn := andthenFn (satisfyFn p errorMsg) (takeWhileFn p) variable (startPos : String.Pos) in partial def finishCommentBlock (nesting : Nat) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then eoi s else let curr := input.get i let i := input.next i if curr == '-' then if input.atEnd i then eoi s else let curr := input.get i if curr == '/' then -- "-/" end of comment if nesting == 1 then s.next input i else finishCommentBlock (nesting-1) c (s.next input i) else finishCommentBlock nesting c (s.next input i) else if curr == '/' then if input.atEnd i then eoi s else let curr := input.get i if curr == '-' then finishCommentBlock (nesting+1) c (s.next input i) else finishCommentBlock nesting c (s.setPos i) else finishCommentBlock nesting c (s.setPos i) where eoi s := s.mkUnexpectedErrorAt "unterminated comment" startPos /- Consume whitespace and comments -/ partial def whitespace : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s else let curr := input.get i if curr == '\t' then s.mkUnexpectedError "tabs are not allowed; please configure your editor to expand them" else if curr.isWhitespace then whitespace c (s.next input i) else if curr == '-' then let i := input.next i let curr := input.get i if curr == '-' then andthenFn (takeUntilFn (fun c => c = '\n')) whitespace c (s.next input i) else s else if curr == '/' then let startPos := i let i := input.next i let curr := input.get i if curr == '-' then let i := input.next i let curr := input.get i if curr == '-' then s -- "/--" doc comment is an actual token else andthenFn (finishCommentBlock startPos 1) whitespace c (s.next input i) else s else s def mkEmptySubstringAt (s : String) (p : Nat) : Substring := { str := s, startPos := p, stopPos := p } private def rawAux (startPos : Nat) (trailingWs : Bool) : ParserFn := fun c s => let input := c.input let stopPos := s.pos let leading := mkEmptySubstringAt input startPos let val := input.extract startPos stopPos if trailingWs then let s := whitespace c s let stopPos' := s.pos let trailing := { str := input, startPos := stopPos, stopPos := stopPos' : Substring } let atom := mkAtom (SourceInfo.original leading startPos trailing (startPos + val.bsize)) val s.pushSyntax atom else let trailing := mkEmptySubstringAt input stopPos let atom := mkAtom (SourceInfo.original leading startPos trailing (startPos + val.bsize)) val s.pushSyntax atom /-- Match an arbitrary Parser and return the consumed String in a `Syntax.atom`. -/ @[inline] def rawFn (p : ParserFn) (trailingWs := false) : ParserFn := fun c s => let startPos := s.pos let s := p c s if s.hasError then s else rawAux startPos trailingWs c s @[inline] def chFn (c : Char) (trailingWs := false) : ParserFn := rawFn (satisfyFn (fun d => c == d) ("'" ++ toString c ++ "'")) trailingWs def rawCh (c : Char) (trailingWs := false) : Parser := { fn := chFn c trailingWs } def hexDigitFn : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError else let curr := input.get i let i := input.next i if curr.isDigit || ('a' <= curr && curr <= 'f') || ('A' <= curr && curr <= 'F') then s.setPos i else s.mkUnexpectedError "invalid hexadecimal numeral" def quotedCharCoreFn (isQuotable : Char → Bool) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError else let curr := input.get i if isQuotable curr then s.next input i else if curr == 'x' then andthenFn hexDigitFn hexDigitFn c (s.next input i) else if curr == 'u' then andthenFn hexDigitFn (andthenFn hexDigitFn (andthenFn hexDigitFn hexDigitFn)) c (s.next input i) else s.mkUnexpectedError "invalid escape sequence" def isQuotableCharDefault (c : Char) : Bool := c == '\\' || c == '\"' || c == '\'' || c == 'r' || c == 'n' || c == 't' def quotedCharFn : ParserFn := quotedCharCoreFn isQuotableCharDefault /-- Push `(Syntax.node tk <new-atom>)` into syntax stack -/ def mkNodeToken (n : SyntaxNodeKind) (startPos : Nat) : ParserFn := fun c s => let input := c.input let stopPos := s.pos let leading := mkEmptySubstringAt input startPos let val := input.extract startPos stopPos let s := whitespace c s let wsStopPos := s.pos let trailing := { str := input, startPos := stopPos, stopPos := wsStopPos : Substring } let info := SourceInfo.original leading startPos trailing stopPos s.pushSyntax (Syntax.mkLit n val info) def charLitFnAux (startPos : Nat) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError else let curr := input.get i let s := s.setPos (input.next i) let s := if curr == '\\' then quotedCharFn c s else s if s.hasError then s else let i := s.pos let curr := input.get i let s := s.setPos (input.next i) if curr == '\'' then mkNodeToken charLitKind startPos c s else s.mkUnexpectedError "missing end of character literal" partial def strLitFnAux (startPos : Nat) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkUnexpectedErrorAt "unterminated string literal" startPos else let curr := input.get i let s := s.setPos (input.next i) if curr == '\"' then mkNodeToken strLitKind startPos c s else if curr == '\\' then andthenFn quotedCharFn (strLitFnAux startPos) c s else strLitFnAux startPos c s def decimalNumberFn (startPos : Nat) (c : ParserContext) : ParserState → ParserState := fun s => let s := takeWhileFn (fun c => c.isDigit) c s let input := c.input let i := s.pos let curr := input.get i if curr == '.' || curr == 'e' || curr == 'E' then let s := parseOptDot s let s := parseOptExp s mkNodeToken scientificLitKind startPos c s else mkNodeToken numLitKind startPos c s where parseOptDot s := let input := c.input let i := s.pos let curr := input.get i if curr == '.' then let i := input.next i let curr := input.get i if curr.isDigit then takeWhileFn (fun c => c.isDigit) c (s.setPos i) else s.setPos i else s parseOptExp s := let input := c.input let i := s.pos let curr := input.get i if curr == 'e' || curr == 'E' then let i := input.next i let i := if input.get i == '-' then input.next i else i let curr := input.get i if curr.isDigit then takeWhileFn (fun c => c.isDigit) c (s.setPos i) else s.setPos i else s def binNumberFn (startPos : Nat) : ParserFn := fun c s => let s := takeWhile1Fn (fun c => c == '0' || c == '1') "binary number" c s mkNodeToken numLitKind startPos c s def octalNumberFn (startPos : Nat) : ParserFn := fun c s => let s := takeWhile1Fn (fun c => '0' ≤ c && c ≤ '7') "octal number" c s mkNodeToken numLitKind startPos c s def hexNumberFn (startPos : Nat) : ParserFn := fun c s => let s := takeWhile1Fn (fun c => ('0' ≤ c && c ≤ '9') || ('a' ≤ c && c ≤ 'f') || ('A' ≤ c && c ≤ 'F')) "hexadecimal number" c s mkNodeToken numLitKind startPos c s def numberFnAux : ParserFn := fun c s => let input := c.input let startPos := s.pos if input.atEnd startPos then s.mkEOIError else let curr := input.get startPos if curr == '0' then let i := input.next startPos let curr := input.get i if curr == 'b' || curr == 'B' then binNumberFn startPos c (s.next input i) else if curr == 'o' || curr == 'O' then octalNumberFn startPos c (s.next input i) else if curr == 'x' || curr == 'X' then hexNumberFn startPos c (s.next input i) else decimalNumberFn startPos c (s.setPos i) else if curr.isDigit then decimalNumberFn startPos c (s.next input startPos) else s.mkError "numeral" def isIdCont : String → ParserState → Bool := fun input s => let i := s.pos let curr := input.get i if curr == '.' then let i := input.next i if input.atEnd i then false else let curr := input.get i isIdFirst curr || isIdBeginEscape curr else false private def isToken (idStartPos idStopPos : Nat) (tk : Option Token) : Bool := match tk with | none => false | some tk => -- if a token is both a symbol and a valid identifier (i.e. a keyword), -- we want it to be recognized as a symbol tk.bsize ≥ idStopPos - idStartPos def mkTokenAndFixPos (startPos : Nat) (tk : Option Token) : ParserFn := fun c s => match tk with | none => s.mkErrorAt "token" startPos | some tk => if c.forbiddenTk? == some tk then s.mkErrorAt "forbidden token" startPos else let input := c.input let leading := mkEmptySubstringAt input startPos let stopPos := startPos + tk.bsize let s := s.setPos stopPos let s := whitespace c s let wsStopPos := s.pos let trailing := { str := input, startPos := stopPos, stopPos := wsStopPos : Substring } let atom := mkAtom (SourceInfo.original leading startPos trailing stopPos) tk s.pushSyntax atom def mkIdResult (startPos : Nat) (tk : Option Token) (val : Name) : ParserFn := fun c s => let stopPos := s.pos if isToken startPos stopPos tk then mkTokenAndFixPos startPos tk c s else let input := c.input let rawVal := { str := input, startPos := startPos, stopPos := stopPos : Substring } let s := whitespace c s let trailingStopPos := s.pos let leading := mkEmptySubstringAt input startPos let trailing := { str := input, startPos := stopPos, stopPos := trailingStopPos : Substring } let info := SourceInfo.original leading startPos trailing stopPos let atom := mkIdent info rawVal val s.pushSyntax atom partial def identFnAux (startPos : Nat) (tk : Option Token) (r : Name) : ParserFn := let rec parse (r : Name) (c s) := do let input := c.input let i := s.pos if input.atEnd i then return s.mkEOIError let curr := input.get i if isIdBeginEscape curr then let startPart := input.next i let s := takeUntilFn isIdEndEscape c (s.setPos startPart) if input.atEnd s.pos then return s.mkUnexpectedErrorAt "unterminated identifier escape" startPart let stopPart := s.pos let s := s.next c.input s.pos let r := Name.mkStr r (input.extract startPart stopPart) if isIdCont input s then let s := s.next input s.pos parse r c s else mkIdResult startPos tk r c s else if isIdFirst curr then let startPart := i let s := takeWhileFn isIdRest c (s.next input i) let stopPart := s.pos let r := Name.mkStr r (input.extract startPart stopPart) if isIdCont input s then let s := s.next input s.pos parse r c s else mkIdResult startPos tk r c s else mkTokenAndFixPos startPos tk c s parse r private def isIdFirstOrBeginEscape (c : Char) : Bool := isIdFirst c || isIdBeginEscape c private def nameLitAux (startPos : Nat) : ParserFn := fun c s => let input := c.input let s := identFnAux startPos none Name.anonymous c (s.next input startPos) if s.hasError then s else let stx := s.stxStack.back match stx with | Syntax.ident info rawStr _ _ => let s := s.popSyntax s.pushSyntax (Syntax.mkNameLit rawStr.toString info) | _ => s.mkError "invalid Name literal" private def tokenFnAux : ParserFn := fun c s => let input := c.input let i := s.pos let curr := input.get i if curr == '\"' then strLitFnAux i c (s.next input i) else if curr == '\'' then charLitFnAux i c (s.next input i) else if curr.isDigit then numberFnAux c s else if curr == '`' && isIdFirstOrBeginEscape (getNext input i) then nameLitAux i c s else let (_, tk) := c.tokens.matchPrefix input i identFnAux i tk Name.anonymous c s private def updateCache (startPos : Nat) (s : ParserState) : ParserState := -- do not cache token parsing errors, which are rare and usually fatal and thus not worth an extra field in `TokenCache` match s with | ⟨stack, lhsPrec, pos, cache, none⟩ => if stack.size == 0 then s else let tk := stack.back ⟨stack, lhsPrec, pos, { tokenCache := { startPos := startPos, stopPos := pos, token := tk } }, none⟩ | other => other def tokenFn (expected : List String := []) : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError expected else let tkc := s.cache.tokenCache if tkc.startPos == i then let s := s.pushSyntax tkc.token s.setPos tkc.stopPos else let s := tokenFnAux c s updateCache i s def peekTokenAux (c : ParserContext) (s : ParserState) : ParserState × Except ParserState Syntax := let iniSz := s.stackSize let iniPos := s.pos let s := tokenFn [] c s if let some e := s.errorMsg then (s.restore iniSz iniPos, Except.error s) else let stx := s.stxStack.back (s.restore iniSz iniPos, Except.ok stx) def peekToken (c : ParserContext) (s : ParserState) : ParserState × Except ParserState Syntax := let tkc := s.cache.tokenCache if tkc.startPos == s.pos then (s, Except.ok tkc.token) else peekTokenAux c s /- Treat keywords as identifiers. -/ def rawIdentFn : ParserFn := fun c s => let input := c.input let i := s.pos if input.atEnd i then s.mkEOIError else identFnAux i none Name.anonymous c s @[inline] def satisfySymbolFn (p : String → Bool) (expected : List String) : ParserFn := fun c s => let initStackSz := s.stackSize let startPos := s.pos let s := tokenFn expected c s if s.hasError then s else match s.stxStack.back with | Syntax.atom _ sym => if p sym then s else s.mkErrorsAt expected startPos initStackSz | _ => s.mkErrorsAt expected startPos initStackSz def symbolFnAux (sym : String) (errorMsg : String) : ParserFn := satisfySymbolFn (fun s => s == sym) [errorMsg] def symbolInfo (sym : String) : ParserInfo := { collectTokens := fun tks => sym :: tks, firstTokens := FirstTokens.tokens [ sym ] } @[inline] def symbolFn (sym : String) : ParserFn := symbolFnAux sym ("'" ++ sym ++ "'") @[inline] def symbolNoAntiquot (sym : String) : Parser := let sym := sym.trim { info := symbolInfo sym, fn := symbolFn sym } def checkTailNoWs (prev : Syntax) : Bool := match prev.getTailInfo with | SourceInfo.original _ _ trailing _ => trailing.stopPos == trailing.startPos | _ => false /-- Check if the following token is the symbol _or_ identifier `sym`. Useful for parsing local tokens that have not been added to the token table (but may have been so by some unrelated code). For example, the universe `max` Function is parsed using this combinator so that it can still be used as an identifier outside of universes (but registering it as a token in a Term Syntax would not break the universe Parser). -/ def nonReservedSymbolFnAux (sym : String) (errorMsg : String) : ParserFn := fun c s => let initStackSz := s.stackSize let startPos := s.pos let s := tokenFn [errorMsg] c s if s.hasError then s else match s.stxStack.back with | Syntax.atom _ sym' => if sym == sym' then s else s.mkErrorAt errorMsg startPos initStackSz | Syntax.ident info rawVal _ _ => if sym == rawVal.toString then let s := s.popSyntax s.pushSyntax (Syntax.atom info sym) else s.mkErrorAt errorMsg startPos initStackSz | _ => s.mkErrorAt errorMsg startPos initStackSz @[inline] def nonReservedSymbolFn (sym : String) : ParserFn := nonReservedSymbolFnAux sym ("'" ++ sym ++ "'") def nonReservedSymbolInfo (sym : String) (includeIdent : Bool) : ParserInfo := { firstTokens := if includeIdent then FirstTokens.tokens [ sym, "ident" ] else FirstTokens.tokens [ sym ] } @[inline] def nonReservedSymbolNoAntiquot (sym : String) (includeIdent := false) : Parser := let sym := sym.trim { info := nonReservedSymbolInfo sym includeIdent, fn := nonReservedSymbolFn sym } partial def strAux (sym : String) (errorMsg : String) (j : Nat) :ParserFn := let rec parse (j c s) := if sym.atEnd j then s else let i := s.pos let input := c.input if input.atEnd i || sym.get j != input.get i then s.mkError errorMsg else parse (sym.next j) c (s.next input i) parse j def checkTailWs (prev : Syntax) : Bool := match prev.getTailInfo with | SourceInfo.original _ _ trailing _ => trailing.stopPos > trailing.startPos | _ => false def checkWsBeforeFn (errorMsg : String) : ParserFn := fun c s => let prev := s.stxStack.back if checkTailWs prev then s else s.mkError errorMsg def checkWsBefore (errorMsg : String := "space before") : Parser := { info := epsilonInfo, fn := checkWsBeforeFn errorMsg } def checkTailLinebreak (prev : Syntax) : Bool := match prev.getTailInfo with | SourceInfo.original _ _ trailing _ => trailing.contains '\n' | _ => false def checkLinebreakBeforeFn (errorMsg : String) : ParserFn := fun c s => let prev := s.stxStack.back if checkTailLinebreak prev then s else s.mkError errorMsg def checkLinebreakBefore (errorMsg : String := "line break") : Parser := { info := epsilonInfo fn := checkLinebreakBeforeFn errorMsg } private def pickNonNone (stack : Array Syntax) : Syntax := match stack.findRev? $ fun stx => !stx.isNone with | none => Syntax.missing | some stx => stx def checkNoWsBeforeFn (errorMsg : String) : ParserFn := fun c s => let prev := pickNonNone s.stxStack if checkTailNoWs prev then s else s.mkError errorMsg def checkNoWsBefore (errorMsg : String := "no space before") : Parser := { info := epsilonInfo, fn := checkNoWsBeforeFn errorMsg } def unicodeSymbolFnAux (sym asciiSym : String) (expected : List String) : ParserFn := satisfySymbolFn (fun s => s == sym || s == asciiSym) expected def unicodeSymbolInfo (sym asciiSym : String) : ParserInfo := { collectTokens := fun tks => sym :: asciiSym :: tks, firstTokens := FirstTokens.tokens [ sym, asciiSym ] } @[inline] def unicodeSymbolFn (sym asciiSym : String) : ParserFn := unicodeSymbolFnAux sym asciiSym ["'" ++ sym ++ "', '" ++ asciiSym ++ "'"] @[inline] def unicodeSymbolNoAntiquot (sym asciiSym : String) : Parser := let sym := sym.trim let asciiSym := asciiSym.trim { info := unicodeSymbolInfo sym asciiSym, fn := unicodeSymbolFn sym asciiSym } def mkAtomicInfo (k : String) : ParserInfo := { firstTokens := FirstTokens.tokens [ k ] } def numLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["numeral"] c s if !s.hasError && !(s.stxStack.back.isOfKind numLitKind) then s.mkErrorAt "numeral" iniPos initStackSz else s @[inline] def numLitNoAntiquot : Parser := { fn := numLitFn, info := mkAtomicInfo "numLit" } def scientificLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["scientific number"] c s if !s.hasError && !(s.stxStack.back.isOfKind scientificLitKind) then s.mkErrorAt "scientific number" iniPos initStackSz else s @[inline] def scientificLitNoAntiquot : Parser := { fn := scientificLitFn, info := mkAtomicInfo "scientificLit" } def strLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["string literal"] c s if !s.hasError && !(s.stxStack.back.isOfKind strLitKind) then s.mkErrorAt "string literal" iniPos initStackSz else s @[inline] def strLitNoAntiquot : Parser := { fn := strLitFn, info := mkAtomicInfo "strLit" } def charLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["char literal"] c s if !s.hasError && !(s.stxStack.back.isOfKind charLitKind) then s.mkErrorAt "character literal" iniPos initStackSz else s @[inline] def charLitNoAntiquot : Parser := { fn := charLitFn, info := mkAtomicInfo "charLit" } def nameLitFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["Name literal"] c s if !s.hasError && !(s.stxStack.back.isOfKind nameLitKind) then s.mkErrorAt "Name literal" iniPos initStackSz else s @[inline] def nameLitNoAntiquot : Parser := { fn := nameLitFn, info := mkAtomicInfo "nameLit" } def identFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["identifier"] c s if !s.hasError && !(s.stxStack.back.isIdent) then s.mkErrorAt "identifier" iniPos initStackSz else s @[inline] def identNoAntiquot : Parser := { fn := identFn, info := mkAtomicInfo "ident" } @[inline] def rawIdentNoAntiquot : Parser := { fn := rawIdentFn } def identEqFn (id : Name) : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let s := tokenFn ["identifier"] c s if s.hasError then s else match s.stxStack.back with | Syntax.ident _ _ val _ => if val != id then s.mkErrorAt ("expected identifier '" ++ toString id ++ "'") iniPos initStackSz else s | _ => s.mkErrorAt "identifier" iniPos initStackSz @[inline] def identEq (id : Name) : Parser := { fn := identEqFn id, info := mkAtomicInfo "ident" } namespace ParserState def keepTop (s : Array Syntax) (startStackSize : Nat) : Array Syntax := let node := s.back s.shrink startStackSize |>.push node def keepNewError (s : ParserState) (oldStackSize : Nat) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, err⟩ => ⟨keepTop stack oldStackSize, lhsPrec, pos, cache, err⟩ def keepPrevError (s : ParserState) (oldStackSize : Nat) (oldStopPos : String.Pos) (oldError : Option Error) : ParserState := match s with | ⟨stack, lhsPrec, _, cache, _⟩ => ⟨stack.shrink oldStackSize, lhsPrec, oldStopPos, cache, oldError⟩ def mergeErrors (s : ParserState) (oldStackSize : Nat) (oldError : Error) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, some err⟩ => if oldError == err then s else ⟨stack.shrink oldStackSize, lhsPrec, pos, cache, some (oldError.merge err)⟩ | other => other def keepLatest (s : ParserState) (startStackSize : Nat) : ParserState := match s with | ⟨stack, lhsPrec, pos, cache, _⟩ => ⟨keepTop stack startStackSize, lhsPrec, pos, cache, none⟩ def replaceLongest (s : ParserState) (startStackSize : Nat) : ParserState := s.keepLatest startStackSize end ParserState def invalidLongestMatchParser (s : ParserState) : ParserState := s.mkError "longestMatch parsers must generate exactly one Syntax node" /-- Auxiliary function used to execute parsers provided to `longestMatchFn`. Push `left?` into the stack if it is not `none`, and execute `p`. Remark: `p` must produce exactly one syntax node. Remark: the `left?` is not none when we are processing trailing parsers. -/ def runLongestMatchParser (left? : Option Syntax) (startLhsPrec : Nat) (p : ParserFn) : ParserFn := fun c s => do /- We assume any registered parser `p` has one of two forms: * a direct call to `leadingParser` or `trailingParser` * a direct call to a (leading) token parser In the first case, we can extract the precedence of the parser by having `leadingParser/trailingParser` set `ParserState.lhsPrec` to it in the very end so that no nested parser can interfere. In the second case, the precedence is effectively `max` (there is a `checkPrec` merely for the convenience of the pretty printer) and there are no nested `leadingParser/trailingParser` calls, so the value of `lhsPrec` will not be changed by the parser (nor will it be read by any leading parser). Thus we initialize the field to `maxPrec` in the leading case. -/ let mut s := { s with lhsPrec := if left?.isSome then startLhsPrec else maxPrec } let startSize := s.stackSize if let some left := left? then s := s.pushSyntax left s := p c s -- stack contains `[..., result ]` if s.stackSize == startSize + 1 then s -- success or error with the expected number of nodes else if s.hasError then -- error with an unexpected number of nodes. s.shrinkStack startSize |>.pushSyntax Syntax.missing else -- parser succeded with incorrect number of nodes invalidLongestMatchParser s def longestMatchStep (left? : Option Syntax) (startSize startLhsPrec : Nat) (startPos : String.Pos) (prevPrio : Nat) (prio : Nat) (p : ParserFn) : ParserContext → ParserState → ParserState × Nat := fun c s => let prevErrorMsg := s.errorMsg let prevStopPos := s.pos let prevSize := s.stackSize let prevLhsPrec := s.lhsPrec let s := s.restore prevSize startPos let s := runLongestMatchParser left? startLhsPrec p c s match prevErrorMsg, s.errorMsg with | none, none => -- both succeeded if s.pos > prevStopPos || (s.pos == prevStopPos && prio > prevPrio) then (s.replaceLongest startSize, prio) else if s.pos < prevStopPos || (s.pos == prevStopPos && prio < prevPrio) then ({ s.restore prevSize prevStopPos with lhsPrec := prevLhsPrec }, prevPrio) -- keep prev -- it is not clear what the precedence of a choice node should be, so we conservatively take the minimum else ({s with lhsPrec := s.lhsPrec.min prevLhsPrec }, prio) | none, some _ => -- prev succeeded, current failed ({ s.restore prevSize prevStopPos with lhsPrec := prevLhsPrec }, prevPrio) | some oldError, some _ => -- both failed if s.pos > prevStopPos || (s.pos == prevStopPos && prio > prevPrio) then (s.keepNewError startSize, prio) else if s.pos < prevStopPos || (s.pos == prevStopPos && prio < prevPrio) then (s.keepPrevError prevSize prevStopPos prevErrorMsg, prevPrio) else (s.mergeErrors prevSize oldError, prio) | some _, none => -- prev failed, current succeeded let successNode := s.stxStack.back let s := s.shrinkStack startSize -- restore stack to initial size to make sure (failure) nodes are removed from the stack (s.pushSyntax successNode, prio) -- put successNode back on the stack def longestMatchMkResult (startSize : Nat) (s : ParserState) : ParserState := if !s.hasError && s.stackSize > startSize + 1 then s.mkNode choiceKind startSize else s def longestMatchFnAux (left? : Option Syntax) (startSize startLhsPrec : Nat) (startPos : String.Pos) (prevPrio : Nat) (ps : List (Parser × Nat)) : ParserFn := let rec parse (prevPrio : Nat) (ps : List (Parser × Nat)) := match ps with | [] => fun _ s => longestMatchMkResult startSize s | p::ps => fun c s => let (s, prevPrio) := longestMatchStep left? startSize startLhsPrec startPos prevPrio p.2 p.1.fn c s parse prevPrio ps c s parse prevPrio ps def longestMatchFn (left? : Option Syntax) : List (Parser × Nat) → ParserFn | [] => fun _ s => s.mkError "longestMatch: empty list" | [p] => fun c s => runLongestMatchParser left? s.lhsPrec p.1.fn c s | p::ps => fun c s => let startSize := s.stackSize let startLhsPrec := s.lhsPrec let startPos := s.pos let s := runLongestMatchParser left? s.lhsPrec p.1.fn c s longestMatchFnAux left? startSize startLhsPrec startPos p.2 ps c s def anyOfFn : List Parser → ParserFn | [], _, s => s.mkError "anyOf: empty list" | [p], c, s => p.fn c s | p::ps, c, s => orelseFn p.fn (anyOfFn ps) c s @[inline] def checkColGeFn (errorMsg : String) : ParserFn := fun c s => match c.savedPos? with | none => s | some savedPos => let savedPos := c.fileMap.toPosition savedPos let pos := c.fileMap.toPosition s.pos if pos.column ≥ savedPos.column then s else s.mkError errorMsg @[inline] def checkColGe (errorMsg : String := "checkColGe") : Parser := { fn := checkColGeFn errorMsg } @[inline] def checkColGtFn (errorMsg : String) : ParserFn := fun c s => match c.savedPos? with | none => s | some savedPos => let savedPos := c.fileMap.toPosition savedPos let pos := c.fileMap.toPosition s.pos if pos.column > savedPos.column then s else s.mkError errorMsg @[inline] def checkColGt (errorMsg : String := "checkColGt") : Parser := { fn := checkColGtFn errorMsg } @[inline] def checkLineEqFn (errorMsg : String) : ParserFn := fun c s => match c.savedPos? with | none => s | some savedPos => let savedPos := c.fileMap.toPosition savedPos let pos := c.fileMap.toPosition s.pos if pos.line == savedPos.line then s else s.mkError errorMsg @[inline] def checkLineEq (errorMsg : String := "checkLineEq") : Parser := { fn := checkLineEqFn errorMsg } @[inline] def withPosition (p : Parser) : Parser := { info := p.info, fn := fun c s => p.fn { c with savedPos? := s.pos } s } @[inline] def withoutPosition (p : Parser) : Parser := { info := p.info, fn := fun c s => let pos := c.fileMap.toPosition s.pos p.fn { c with savedPos? := none } s } @[inline] def withForbidden (tk : Token) (p : Parser) : Parser := { info := p.info, fn := fun c s => p.fn { c with forbiddenTk? := tk } s } @[inline] def withoutForbidden (p : Parser) : Parser := { info := p.info, fn := fun c s => p.fn { c with forbiddenTk? := none } s } def eoiFn : ParserFn := fun c s => let i := s.pos if c.input.atEnd i then s else s.mkError "expected end of file" @[inline] def eoi : Parser := { fn := eoiFn } open Std (RBMap RBMap.empty) /-- A multimap indexed by tokens. Used for indexing parsers by their leading token. -/ def TokenMap (α : Type) := RBMap Name (List α) Name.quickCmp namespace TokenMap def insert (map : TokenMap α) (k : Name) (v : α) : TokenMap α := match map.find? k with | none => Std.RBMap.insert map k [v] | some vs => Std.RBMap.insert map k (v::vs) instance : Inhabited (TokenMap α) := ⟨RBMap.empty⟩ instance : EmptyCollection (TokenMap α) := ⟨RBMap.empty⟩ end TokenMap structure PrattParsingTables where leadingTable : TokenMap (Parser × Nat) := {} leadingParsers : List (Parser × Nat) := [] -- for supporting parsers we cannot obtain first token trailingTable : TokenMap (Parser × Nat) := {} trailingParsers : List (Parser × Nat) := [] -- for supporting parsers such as function application instance : Inhabited PrattParsingTables := ⟨{}⟩ /- The type `leadingIdentBehavior` specifies how the parsing table lookup function behaves for identifiers. The function `prattParser` uses two tables `leadingTable` and `trailingTable`. They map tokens to parsers. - `LeadingIdentBehavior.default`: if the leading token is an identifier, then `prattParser` just executes the parsers associated with the auxiliary token "ident". - `LeadingIdentBehavior.symbol`: if the leading token is an identifier `<foo>`, and there are parsers `P` associated with the toek `<foo>`, then it executes `P`. Otherwise, it executes only the parsers associated with the auxiliary token "ident". - `LeadingIdentBehavior.both`: if the leading token an identifier `<foo>`, the it executes the parsers associated with token `<foo>` and parsers associated with the auxiliary token "ident". We use `LeadingIdentBehavior.symbol` and `LeadingIdentBehavior.both` and `nonReservedSymbol` parser to implement the `tactic` parsers. The idea is to avoid creating a reserved symbol for each builtin tactic (e.g., `apply`, `assumption`, etc.). That is, users may still use these symbols as identifiers (e.g., naming a function). -/ inductive LeadingIdentBehavior where | default | symbol | both deriving Inhabited, BEq /-- Each parser category is implemented using a Pratt's parser. The system comes equipped with the following categories: `level`, `term`, `tactic`, and `command`. Users and plugins may define extra categories. The method ``` categoryParser `term prec ``` executes the Pratt's parser for category `term` with precedence `prec`. That is, only parsers with precedence at least `prec` are considered. The method `termParser prec` is equivalent to the method above. -/ structure ParserCategory where tables : PrattParsingTables behavior : LeadingIdentBehavior deriving Inhabited abbrev ParserCategories := Std.PersistentHashMap Name ParserCategory def indexed {α : Type} (map : TokenMap α) (c : ParserContext) (s : ParserState) (behavior : LeadingIdentBehavior) : ParserState × List α := let (s, stx) := peekToken c s let find (n : Name) : ParserState × List α := match map.find? n with | some as => (s, as) | _ => (s, []) match stx with | Except.ok (Syntax.atom _ sym) => find (Name.mkSimple sym) | Except.ok (Syntax.ident _ _ val _) => match behavior with | LeadingIdentBehavior.default => find identKind | LeadingIdentBehavior.symbol => match map.find? val with | some as => (s, as) | none => find identKind | LeadingIdentBehavior.both => match map.find? val with | some as => match map.find? identKind with | some as' => (s, as ++ as') | _ => (s, as) | none => find identKind | Except.ok (Syntax.node k _) => find k | Except.ok _ => (s, []) | Except.error s' => (s', []) abbrev CategoryParserFn := Name → ParserFn builtin_initialize categoryParserFnRef : IO.Ref CategoryParserFn ← IO.mkRef fun _ => whitespace builtin_initialize categoryParserFnExtension : EnvExtension CategoryParserFn ← registerEnvExtension $ categoryParserFnRef.get def categoryParserFn (catName : Name) : ParserFn := fun ctx s => categoryParserFnExtension.getState ctx.env catName ctx s def categoryParser (catName : Name) (prec : Nat) : Parser := { fn := fun c s => categoryParserFn catName { c with prec := prec } s } -- Define `termParser` here because we need it for antiquotations @[inline] def termParser (prec : Nat := 0) : Parser := categoryParser `term prec /- ============== -/ /- Antiquotations -/ /- ============== -/ /-- Fail if previous token is immediately followed by ':'. -/ def checkNoImmediateColon : Parser := { fn := fun c s => let prev := s.stxStack.back if checkTailNoWs prev then let input := c.input let i := s.pos if input.atEnd i then s else let curr := input.get i if curr == ':' then s.mkUnexpectedError "unexpected ':'" else s else s } def setExpectedFn (expected : List String) (p : ParserFn) : ParserFn := fun c s => match p c s with | s'@{ errorMsg := some msg, .. } => { s' with errorMsg := some { msg with expected := [] } } | s' => s' def setExpected (expected : List String) (p : Parser) : Parser := { fn := setExpectedFn expected p.fn, info := p.info } def pushNone : Parser := { fn := fun c s => s.pushSyntax mkNullNode } -- We support two kinds of antiquotations: `$id` and `$(t)`, where `id` is a term identifier and `t` is a term. def antiquotNestedExpr : Parser := node `antiquotNestedExpr (symbolNoAntiquot "(" >> decQuotDepth termParser >> symbolNoAntiquot ")") def antiquotExpr : Parser := identNoAntiquot <|> antiquotNestedExpr @[inline] def tokenWithAntiquotFn (p : ParserFn) : ParserFn := fun c s => do let s := p c s if s.hasError || c.quotDepth == 0 then return s let iniSz := s.stackSize let iniPos := s.pos let s := (checkNoWsBefore >> symbolNoAntiquot "%" >> symbolNoAntiquot "$" >> checkNoWsBefore >> antiquotExpr).fn c s if s.hasError then return s.restore iniSz iniPos s.mkNode (`token_antiquot) (iniSz - 1) @[inline] def tokenWithAntiquot (p : Parser) : Parser where fn := tokenWithAntiquotFn p.fn info := p.info @[inline] def symbol (sym : String) : Parser := tokenWithAntiquot (symbolNoAntiquot sym) instance : Coe String Parser := ⟨fun s => symbol s ⟩ @[inline] def nonReservedSymbol (sym : String) (includeIdent := false) : Parser := tokenWithAntiquot (nonReservedSymbolNoAntiquot sym includeIdent) @[inline] def unicodeSymbol (sym asciiSym : String) : Parser := tokenWithAntiquot (unicodeSymbolNoAntiquot sym asciiSym) /-- Define parser for `$e` (if anonymous == true) and `$e:name`. Both forms can also be used with an appended `*` to turn them into an antiquotation "splice". If `kind` is given, it will additionally be checked when evaluating `match_syntax`. Antiquotations can be escaped as in `$$e`, which produces the syntax tree for `$e`. -/ def mkAntiquot (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Parser := let kind := (kind.getD Name.anonymous) ++ `antiquot let nameP := node `antiquotName $ checkNoWsBefore ("no space before ':" ++ name ++ "'") >> symbol ":" >> nonReservedSymbol name -- if parsing the kind fails and `anonymous` is true, check that we're not ignoring a different -- antiquotation kind via `noImmediateColon` let nameP := if anonymous then nameP <|> checkNoImmediateColon >> pushNone else nameP -- antiquotations are not part of the "standard" syntax, so hide "expected '$'" on error leadingNode kind maxPrec $ atomic $ setExpected [] "$" >> manyNoAntiquot (checkNoWsBefore "" >> "$") >> checkNoWsBefore "no space before spliced term" >> antiquotExpr >> nameP def tryAnti (c : ParserContext) (s : ParserState) : Bool := do if c.quotDepth == 0 then return false let (s, stx) := peekToken c s match stx with | Except.ok stx@(Syntax.atom _ sym) => sym == "$" | _ => false @[inline] def withAntiquotFn (antiquotP p : ParserFn) : ParserFn := fun c s => if tryAnti c s then orelseFn antiquotP p c s else p c s /-- Optimized version of `mkAntiquot ... <|> p`. -/ @[inline] def withAntiquot (antiquotP p : Parser) : Parser := { fn := withAntiquotFn antiquotP.fn p.fn, info := orelseInfo antiquotP.info p.info } def withoutInfo (p : Parser) : Parser := { fn := p.fn } /-- Parse `$[p]suffix`, e.g. `$[p],*`. -/ def mkAntiquotSplice (kind : SyntaxNodeKind) (p suffix : Parser) : Parser := let kind := kind ++ `antiquot_scope leadingNode kind maxPrec $ atomic $ setExpected [] "$" >> manyNoAntiquot (checkNoWsBefore "" >> "$") >> checkNoWsBefore "no space before spliced term" >> symbol "[" >> node nullKind p >> symbol "]" >> suffix @[inline] def withAntiquotSuffixSpliceFn (kind : SyntaxNodeKind) (p suffix : ParserFn) : ParserFn := fun c s => do let s := p c s if s.hasError || c.quotDepth == 0 || !s.stxStack.back.isAntiquot then return s let iniSz := s.stackSize let iniPos := s.pos let s := suffix c s if s.hasError then return s.restore iniSz iniPos s.mkNode (kind ++ `antiquot_suffix_splice) (s.stxStack.size - 2) /-- Parse `suffix` after an antiquotation, e.g. `$x,*`, and put both into a new node. -/ @[inline] def withAntiquotSuffixSplice (kind : SyntaxNodeKind) (p suffix : Parser) : Parser := { info := andthenInfo p.info suffix.info, fn := withAntiquotSuffixSpliceFn kind p.fn suffix.fn } def withAntiquotSpliceAndSuffix (kind : SyntaxNodeKind) (p suffix : Parser) := -- prevent `p`'s info from being collected twice withAntiquot (mkAntiquotSplice kind (withoutInfo p) suffix) (withAntiquotSuffixSplice kind p suffix) def nodeWithAntiquot (name : String) (kind : SyntaxNodeKind) (p : Parser) (anonymous := false) : Parser := withAntiquot (mkAntiquot name kind anonymous) $ node kind p /- ===================== -/ /- End of Antiquotations -/ /- ===================== -/ def sepByElemParser (p : Parser) (sep : String) : Parser := withAntiquotSpliceAndSuffix `sepBy p (symbol (sep.trim ++ "*")) def sepBy (p : Parser) (sep : String) (psep : Parser := symbol sep) (allowTrailingSep : Bool := false) : Parser := sepByNoAntiquot (sepByElemParser p sep) psep allowTrailingSep def sepBy1 (p : Parser) (sep : String) (psep : Parser := symbol sep) (allowTrailingSep : Bool := false) : Parser := sepBy1NoAntiquot (sepByElemParser p sep) psep allowTrailingSep def categoryParserOfStackFn (offset : Nat) : ParserFn := fun ctx s => let stack := s.stxStack if stack.size < offset + 1 then s.mkUnexpectedError ("failed to determine parser category using syntax stack, stack is too small") else match stack.get! (stack.size - offset - 1) with | Syntax.ident _ _ catName _ => categoryParserFn catName ctx s | _ => s.mkUnexpectedError ("failed to determine parser category using syntax stack, the specified element on the stack is not an identifier") def categoryParserOfStack (offset : Nat) (prec : Nat := 0) : Parser := { fn := fun c s => categoryParserOfStackFn offset { c with prec := prec } s } unsafe def evalParserConstUnsafe (declName : Name) : ParserFn := fun ctx s => match ctx.env.evalConstCheck Parser ctx.options `Lean.Parser.Parser declName <|> ctx.env.evalConstCheck Parser ctx.options `Lean.Parser.TrailingParser declName with | Except.ok p => p.fn ctx s | Except.error e => s.mkUnexpectedError s!"error running parser {declName}: {e}" @[implementedBy evalParserConstUnsafe] constant evalParserConst (declName : Name) : ParserFn unsafe def parserOfStackFnUnsafe (offset : Nat) : ParserFn := fun ctx s => let stack := s.stxStack if stack.size < offset + 1 then s.mkUnexpectedError ("failed to determine parser using syntax stack, stack is too small") else match stack.get! (stack.size - offset - 1) with | Syntax.ident (val := parserName) .. => match ctx.resolveName parserName with | [(parserName, [])] => let iniSz := s.stackSize let s := evalParserConst parserName ctx s if !s.hasError && s.stackSize != iniSz + 1 then s.mkUnexpectedError "expected parser to return exactly one syntax object" else s | _::_::_ => s.mkUnexpectedError s!"ambiguous parser name {parserName}" | _ => s.mkUnexpectedError s!"unknown parser {parserName}" | _ => s.mkUnexpectedError ("failed to determine parser using syntax stack, the specified element on the stack is not an identifier") @[implementedBy parserOfStackFnUnsafe] constant parserOfStackFn (offset : Nat) : ParserFn def parserOfStack (offset : Nat) (prec : Nat := 0) : Parser := { fn := fun c s => parserOfStackFn offset { c with prec := prec } s } /-- Run `declName` if possible and inside a quotation, or else `p`. The `ParserInfo` will always be taken from `p`. -/ def evalInsideQuot (declName : Name) (p : Parser) : Parser := { p with fn := fun c s => if c.quotDepth > 0 && !c.suppressInsideQuot && c.env.contains declName then evalParserConst declName c s else p.fn c s } private def mkResult (s : ParserState) (iniSz : Nat) : ParserState := if s.stackSize == iniSz + 1 then s else s.mkNode nullKind iniSz -- throw error instead? def leadingParserAux (kind : Name) (tables : PrattParsingTables) (behavior : LeadingIdentBehavior) : ParserFn := fun c s => do let iniSz := s.stackSize let (s, ps) := indexed tables.leadingTable c s behavior if s.hasError then return s let ps := tables.leadingParsers ++ ps if ps.isEmpty then return s.mkError (toString kind) let s := longestMatchFn none ps c s mkResult s iniSz @[inline] def leadingParser (kind : Name) (tables : PrattParsingTables) (behavior : LeadingIdentBehavior) (antiquotParser : ParserFn) : ParserFn := withAntiquotFn antiquotParser (leadingParserAux kind tables behavior) def trailingLoopStep (tables : PrattParsingTables) (left : Syntax) (ps : List (Parser × Nat)) : ParserFn := fun c s => longestMatchFn left (ps ++ tables.trailingParsers) c s partial def trailingLoop (tables : PrattParsingTables) (c : ParserContext) (s : ParserState) : ParserState := do let iniSz := s.stackSize let iniPos := s.pos let (s, ps) := indexed tables.trailingTable c s LeadingIdentBehavior.default if s.hasError then -- Discard token parse errors and break the trailing loop instead. -- The error will be flagged when the next leading position is parsed, unless the token -- is in fact valid there (e.g. EOI at command level, no-longer forbidden token) return s.restore iniSz iniPos if ps.isEmpty && tables.trailingParsers.isEmpty then return s -- no available trailing parser let left := s.stxStack.back let s := s.popSyntax let s := trailingLoopStep tables left ps c s if s.hasError then -- Discard non-consuming parse errors and break the trailing loop instead, restoring `left`. -- This is necessary for fallback parsers like `app` that pretend to be always applicable. return if s.pos == iniPos then s.restore (iniSz - 1) iniPos |>.pushSyntax left else s trailingLoop tables c s /-- Implements a variant of Pratt's algorithm. In Pratt's algorithms tokens have a right and left binding power. In our implementation, parsers have precedence instead. This method selects a parser (or more, via `longestMatchFn`) from `leadingTable` based on the current token. Note that the unindexed `leadingParsers` parsers are also tried. We have the unidexed `leadingParsers` because some parsers do not have a "first token". Example: ``` syntax term:51 "≤" ident "<" term "|" term : index ``` Example, in principle, the set of first tokens for this parser is any token that can start a term, but this set is always changing. Thus, this parsing rule is stored as an unindexed leading parser at `leadingParsers`. After processing the leading parser, we chain with parsers from `trailingTable`/`trailingParsers` that have precedence at least `c.prec` where `c` is the `ParsingContext`. Recall that `c.prec` is set by `categoryParser`. Note that in the original Pratt's algorith, precedences are only checked before calling trailing parsers. In our implementation, leading *and* trailing parsers check the precendece. We claim our algorithm is more flexible, modular and easier to understand. `antiquotParser` should be a `mkAntiquot` parser (or always fail) and is tried before all other parsers. It should not be added to the regular leading parsers because it would heavily overlap with antiquotation parsers nested inside them. -/ @[inline] def prattParser (kind : Name) (tables : PrattParsingTables) (behavior : LeadingIdentBehavior) (antiquotParser : ParserFn) : ParserFn := fun c s => let iniSz := s.stackSize let iniPos := s.pos let s := leadingParser kind tables behavior antiquotParser c s if s.hasError then s else trailingLoop tables c s def fieldIdxFn : ParserFn := fun c s => let initStackSz := s.stackSize let iniPos := s.pos let curr := c.input.get iniPos if curr.isDigit && curr != '0' then let s := takeWhileFn (fun c => c.isDigit) c s mkNodeToken fieldIdxKind iniPos c s else s.mkErrorAt "field index" iniPos initStackSz @[inline] def fieldIdx : Parser := withAntiquot (mkAntiquot "fieldIdx" `fieldIdx) { fn := fieldIdxFn, info := mkAtomicInfo "fieldIdx" } @[inline] def skip : Parser := { fn := fun c s => s, info := epsilonInfo } end Parser namespace Syntax section variable {β : Type} {m : Type → Type} [Monad m] @[inline] def foldArgsM (s : Syntax) (f : Syntax → β → m β) (b : β) : m β := s.getArgs.foldlM (flip f) b @[inline] def foldArgs (s : Syntax) (f : Syntax → β → β) (b : β) : β := Id.run (s.foldArgsM f b) @[inline] def forArgsM (s : Syntax) (f : Syntax → m Unit) : m Unit := s.foldArgsM (fun s _ => f s) () end end Syntax end Lean
c42ccba89ba8ef531dfb7dafd2b471d086778d2a
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/sheaves/abelian.lean
26296209a65de694912647cecbc5603dc70b123e
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
2,103
lean
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Jujian Zhang -/ import category_theory.abelian.functor_category import category_theory.preadditive.additive_functor import category_theory.preadditive.functor_category import category_theory.abelian.transfer import category_theory.sites.left_exact /-! # Category of sheaves is abelian Let `C, D` be categories and `J` be a grothendieck topology on `C`, when `D` is abelian and sheafification is possible in `C`, `Sheaf J D` is abelian as well (`Sheaf_is_abelian`). Hence, `presheaf_to_Sheaf` is an additive functor (`presheaf_to_Sheaf_additive`). -/ noncomputable theory namespace category_theory open category_theory.limits section abelian universes w v u variables {C : Type (max v u)} [category.{v} C] variables {D : Type w} [category.{max v u} D] [abelian D] variables {J : grothendieck_topology C} -- This needs to be specified manually because of universe level. instance : abelian (Cᵒᵖ ⥤ D) := @abelian.functor_category_abelian.{v} Cᵒᵖ _ D _ _ -- This also needs to be specified manually, but I don't know why. instance : has_finite_products (Sheaf J D) := { out := λ j, { has_limit := λ F, by apply_instance } } -- sheafification assumptions variables [∀ (P : Cᵒᵖ ⥤ D) (X : C) (S : J.cover X), has_multiequalizer (S.index P)] variables [∀ (X : C), has_colimits_of_shape (J.cover X)ᵒᵖ D] variables [concrete_category.{max v u} D] [preserves_limits (forget D)] variables [∀ (X : C), preserves_colimits_of_shape (J.cover X)ᵒᵖ (forget D)] variables [reflects_isomorphisms (forget D)] instance Sheaf_is_abelian [has_finite_limits D] : abelian (Sheaf J D) := let adj := sheafification_adjunction J D in abelian_of_adjunction _ _ (as_iso adj.counit) adj local attribute [instance] preserves_binary_biproducts_of_preserves_binary_products instance presheaf_to_Sheaf_additive : (presheaf_to_Sheaf J D).additive := (presheaf_to_Sheaf J D).additive_of_preserves_binary_biproducts end abelian end category_theory
59e9e2242f7d9241f3bf230b33b94173ea80f25b
8cb37a089cdb4af3af9d8bf1002b417e407a8e9e
/library/init/meta/smt/interactive.lean
79b1f56827bc58eee7cb449047b18f48e283ffa0
[ "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
10,090
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.smt.smt_tactic init.meta.interactive_base import init.meta.smt.rsimp namespace smt_tactic meta def save_info (p : pos) : smt_tactic unit := do (ss, ts) ← smt_tactic.read, tactic.save_info_thunk p (λ _, smt_state.to_format ss ts) meta def skip : smt_tactic unit := return () meta def solve_goals : smt_tactic unit := iterate close meta def step {α : Type} (tac : smt_tactic α) : smt_tactic unit := tac >> solve_goals meta def istep {α : Type} (line0 col0 line col : nat) (tac : smt_tactic α) : smt_tactic unit := ⟨λ ss ts, (@scope_trace _ line col (λ _, (tac >> solve_goals).run ss ts)).clamp_pos line0 line col⟩ meta def execute (tac : smt_tactic unit) : tactic unit := using_smt tac meta def execute_with (cfg : smt_config) (tac : smt_tactic unit) : tactic unit := using_smt tac cfg meta instance : interactive.executor smt_tactic := { config_type := smt_config, inhabited := ⟨{}⟩, execute_with := λ cfg tac, using_smt tac cfg, } namespace interactive open lean.parser open interactive open interactive.types local postfix `?`:9001 := optional local postfix *:9001 := many meta def itactic : Type := smt_tactic unit meta def intros : parse ident* → smt_tactic unit | [] := smt_tactic.intros | hs := smt_tactic.intro_lst hs /-- Try to close main goal by using equalities implied by the congruence closure module. -/ meta def close : smt_tactic unit := smt_tactic.close /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from lemmas in the main goal with terms in the main goal. The set of lemmas is populated with theorems tagged with the attribute specified at smt_config.em_attr, and lemmas added using tactics such as `smt_tactic.add_lemmas`. The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`. -/ meta def ematch : smt_tactic unit := smt_tactic.ematch meta def apply (q : parse texpr) : smt_tactic unit := tactic.interactive.apply q meta def fapply (q : parse texpr) : smt_tactic unit := tactic.interactive.fapply q meta def apply_instance : smt_tactic unit := tactic.apply_instance meta def change (q : parse texpr) : smt_tactic unit := tactic.interactive.change q none (loc.ns [none]) meta def exact (q : parse texpr) : smt_tactic unit := tactic.interactive.exact q meta def «from» := exact meta def «assume» := tactic.interactive.assume meta def «have» (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : smt_tactic unit := let h := h.get_or_else `this in match q₁, q₂ with | some e, some p := do t ← tactic.to_expr e, v ← tactic.to_expr ``(%%p : %%t), smt_tactic.assertv h t v | none, some p := do p ← tactic.to_expr p, smt_tactic.note h none p | some e, none := tactic.to_expr e >>= smt_tactic.assert h | none, none := do u ← tactic.mk_meta_univ, e ← tactic.mk_meta_var (expr.sort u), smt_tactic.assert h e end >> return () meta def «let» (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : smt_tactic unit := let h := h.get_or_else `this in match q₁, q₂ with | some e, some p := do t ← tactic.to_expr e, v ← tactic.to_expr ``(%%p : %%t), smt_tactic.definev h t v | none, some p := do p ← tactic.to_expr p, smt_tactic.pose h none p | some e, none := tactic.to_expr e >>= smt_tactic.define h | none, none := do u ← tactic.mk_meta_univ, e ← tactic.mk_meta_var (expr.sort u), smt_tactic.define h e end >> return () meta def add_fact (q : parse texpr) : smt_tactic unit := do h ← tactic.get_unused_name `h none, p ← tactic.to_expr_strict q, smt_tactic.note h none p meta def trace_state : smt_tactic unit := smt_tactic.trace_state meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit := tactic.trace a meta def destruct (q : parse texpr) : smt_tactic unit := do p ← tactic.to_expr_strict q, smt_tactic.destruct p meta def by_cases (q : parse texpr) : smt_tactic unit := do p ← tactic.to_expr_strict q, smt_tactic.by_cases p meta def by_contradiction : smt_tactic unit := smt_tactic.by_contradiction meta def by_contra : smt_tactic unit := smt_tactic.by_contradiction open tactic (resolve_name transparency to_expr) private meta def report_invalid_em_lemma {α : Type} (n : name) : smt_tactic α := fail format!"invalid ematch lemma '{n}'" private meta def add_lemma_name (md : transparency) (lhs_lemma : bool) (n : name) (ref : pexpr) : smt_tactic unit := do p ← resolve_name n, match p with | expr.const n _ := (add_ematch_lemma_from_decl_core md lhs_lemma n >> tactic.save_const_type_info n ref) <|> report_invalid_em_lemma n | _ := (do e ← to_expr p, add_ematch_lemma_core md lhs_lemma e >> try (tactic.save_type_info e ref)) <|> report_invalid_em_lemma n end private meta def add_lemma_pexpr (md : transparency) (lhs_lemma : bool) (p : pexpr) : smt_tactic unit := match p with | (expr.const c []) := add_lemma_name md lhs_lemma c p | (expr.local_const c _ _ _) := add_lemma_name md lhs_lemma c p | _ := do new_e ← to_expr p, add_ematch_lemma_core md lhs_lemma new_e end private meta def add_lemma_pexprs (md : transparency) (lhs_lemma : bool) : list pexpr → smt_tactic unit | [] := return () | (p::ps) := add_lemma_pexpr md lhs_lemma p >> add_lemma_pexprs ps meta def add_lemma (l : parse pexpr_list_or_texpr) : smt_tactic unit := add_lemma_pexprs reducible ff l meta def add_lhs_lemma (l : parse pexpr_list_or_texpr) : smt_tactic unit := add_lemma_pexprs reducible tt l private meta def add_eqn_lemmas_for_core (md : transparency) : list name → smt_tactic unit | [] := return () | (c::cs) := do p ← resolve_name c, match p with | expr.const n _ := add_ematch_eqn_lemmas_for_core md n >> add_eqn_lemmas_for_core cs | _ := fail format!"'{c}' is not a constant" end meta def add_eqn_lemmas_for (ids : parse ident*) : smt_tactic unit := add_eqn_lemmas_for_core reducible ids meta def add_eqn_lemmas (ids : parse ident*) : smt_tactic unit := add_eqn_lemmas_for ids private meta def add_hinst_lemma_from_name (md : transparency) (lhs_lemma : bool) (n : name) (hs : hinst_lemmas) (ref : pexpr) : smt_tactic hinst_lemmas := do p ← resolve_name n, match p with | expr.const n _ := (do h ← hinst_lemma.mk_from_decl_core md n lhs_lemma, tactic.save_const_type_info n ref, return $ hs.add h) <|> (do hs₁ ← mk_ematch_eqn_lemmas_for_core md n, tactic.save_const_type_info n ref, return $ hs.merge hs₁) <|> report_invalid_em_lemma n | _ := (do e ← to_expr p, h ← hinst_lemma.mk_core md e lhs_lemma, try (tactic.save_type_info e ref), return $ hs.add h) <|> report_invalid_em_lemma n end private meta def add_hinst_lemma_from_pexpr (md : transparency) (lhs_lemma : bool) (p : pexpr) (hs : hinst_lemmas) : smt_tactic hinst_lemmas := match p with | (expr.const c []) := add_hinst_lemma_from_name md lhs_lemma c hs p | (expr.local_const c _ _ _) := add_hinst_lemma_from_name md lhs_lemma c hs p | _ := do new_e ← to_expr p, h ← hinst_lemma.mk_core md new_e lhs_lemma, return $ hs.add h end private meta def add_hinst_lemmas_from_pexprs (md : transparency) (lhs_lemma : bool) : list pexpr → hinst_lemmas → smt_tactic hinst_lemmas | [] hs := return hs | (p::ps) hs := do hs₁ ← add_hinst_lemma_from_pexpr md lhs_lemma p hs, add_hinst_lemmas_from_pexprs ps hs₁ meta def ematch_using (l : parse pexpr_list_or_texpr) : smt_tactic unit := do hs ← add_hinst_lemmas_from_pexprs reducible ff l hinst_lemmas.mk, smt_tactic.ematch_using hs /-- Try the given tactic, and do nothing if it fails. -/ meta def try (t : itactic) : smt_tactic unit := smt_tactic.try t /-- Keep applying the given tactic until it fails. -/ meta def iterate (t : itactic) : smt_tactic unit := smt_tactic.iterate t /-- Apply the given tactic to all remaining goals. -/ meta def all_goals (t : itactic) : smt_tactic unit := smt_tactic.all_goals t meta def induction (p : parse tactic.interactive.cases_arg_p) (rec_name : parse using_ident) (ids : parse with_ident_list) (revert : parse $ (tk "generalizing" *> ident*)?) : smt_tactic unit := slift (tactic.interactive.induction p rec_name ids revert) open tactic /-- Simplify the target type of the main goal. -/ meta def simp (use_iota_eqn : parse $ (tk "!")?) (no_dflt : parse only_flag) (hs : parse simp_arg_list) (attr_names : parse with_ident_list) (cfg : simp_config_ext := {}) : smt_tactic unit := tactic.interactive.simp use_iota_eqn none no_dflt hs attr_names (loc.ns [none]) cfg meta def dsimp (no_dflt : parse only_flag) (es : parse simp_arg_list) (attr_names : parse with_ident_list) : smt_tactic unit := tactic.interactive.dsimp no_dflt es attr_names (loc.ns [none]) meta def rsimp : smt_tactic unit := do ccs ← to_cc_state, _root_.rsimp.rsimplify_goal ccs meta def add_simp_lemmas : smt_tactic unit := get_hinst_lemmas_for_attr `rsimp_attr >>= add_lemmas /-- Keep applying heuristic instantiation until the current goal is solved, or it fails. -/ meta def eblast : smt_tactic unit := smt_tactic.eblast /-- Keep applying heuristic instantiation using the given lemmas until the current goal is solved, or it fails. -/ meta def eblast_using (l : parse pexpr_list_or_texpr) : smt_tactic unit := do hs ← add_hinst_lemmas_from_pexprs reducible ff l hinst_lemmas.mk, smt_tactic.iterate (smt_tactic.ematch_using hs >> smt_tactic.try smt_tactic.close) meta def guard_expr_eq (t : expr) (p : parse $ tk ":=" *> texpr) : smt_tactic unit := do e ← to_expr p, guard (expr.alpha_eqv t e) meta def guard_target (p : parse texpr) : smt_tactic unit := do t ← target, guard_expr_eq t p end interactive end smt_tactic
7a3d1be0428c13e73e5a952ce8253fcf17ffdec7
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/group_theory/nilpotent.lean
94ea5a7f39b88ddcaa1bc1db12a1222ea6ab7888
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,560
lean
/- Copyright (c) 2021 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard -/ import group_theory.general_commutator import group_theory.quotient_group /-! # Nilpotent groups An API for nilpotent groups, that is, groups for which the upper central series reaches `⊤`. ## Main definitions Recall that if `H K : subgroup G` then `⁅H, K⁆ : subgroup G` is the subgroup of `G` generated by the commutators `hkh⁻¹k⁻¹`. Recall also Lean's conventions that `⊤` denotes the subgroup `G` of `G`, and `⊥` denotes the trivial subgroup `{1}`. * `upper_central_series G : ℕ → subgroup G` : the upper central series of a group `G`. This is an increasing sequence of normal subgroups `H n` of `G` with `H 0 = ⊥` and `H (n + 1) / H n` is the centre of `G / H n`. * `lower_central_series G : ℕ → subgroup G` : the lower central series of a group `G`. This is a decreasing sequence of normal subgroups `H n` of `G` with `H 0 = ⊤` and `H (n + 1) = ⁅H n, G⁆`. * `is_nilpotent` : A group G is nilpotent if its upper central series reaches `⊤`, or equivalently if its lower central series reaches `⊥`. * `nilpotency_class` : the length of the upper central series of a nilpotent group. * `is_ascending_central_series (H : ℕ → subgroup G) : Prop` and * `is_descending_central_series (H : ℕ → subgroup G) : Prop` : Note that in the literature a "central series" for a group is usually defined to be a *finite* sequence of normal subgroups `H 0`, `H 1`, ..., starting at `⊤`, finishing at `⊥`, and with each `H n / H (n + 1)` central in `G / H (n + 1)`. In this formalisation it is convenient to have two weaker predicates on an infinite sequence of subgroups `H n` of `G`: we say a sequence is a *descending central series* if it starts at `G` and `⁅H n, ⊤⁆ ⊆ H (n + 1)` for all `n`. Note that this series may not terminate at `⊥`, and the `H i` need not be normal. Similarly a sequence is an *ascending central series* if `H 0 = ⊥` and `⁅H (n + 1), ⊤⁆ ⊆ H n` for all `n`, again with no requirement that the series reaches `⊤` or that the `H i` are normal. ## Main theorems `G` is *defined* to be nilpotent if the upper central series reaches `⊤`. * `nilpotent_iff_finite_ascending_central_series` : `G` is nilpotent iff some ascending central series reaches `⊤`. * `nilpotent_iff_finite_descending_central_series` : `G` is nilpotent iff some descending central series reaches `⊥`. * `nilpotent_iff_lower` : `G` is nilpotent iff the lower central series reaches `⊥`. ## Warning A "central series" is usually defined to be a finite sequence of normal subgroups going from `⊥` to `⊤` with the property that each subquotient is contained within the centre of the associated quotient of `G`. This means that if `G` is not nilpotent, then none of what we have called `upper_central_series G`, `lower_central_series G` or the sequences satisfying `is_ascending_central_series` or `is_descending_central_series` are actually central series. Note that the fact that the upper and lower central series are not central series if `G` is not nilpotent is a standard abuse of notation. -/ open subgroup variables {G : Type*} [group G] (H : subgroup G) [normal H] /-- If `H` is a normal subgroup of `G`, then the set `{x : G | ∀ y : G, x*y*x⁻¹*y⁻¹ ∈ H}` is a subgroup of `G` (because it is the preimage in `G` of the centre of the quotient group `G/H`.) -/ def upper_central_series_step : subgroup G := { carrier := {x : G | ∀ y : G, x * y * x⁻¹ * y⁻¹ ∈ H}, one_mem' := λ y, by simp [subgroup.one_mem], mul_mem' := λ a b ha hb y, begin convert subgroup.mul_mem _ (ha (b * y * b⁻¹)) (hb y) using 1, group, end, inv_mem' := λ x hx y, begin specialize hx y⁻¹, rw [mul_assoc, inv_inv] at ⊢ hx, exact subgroup.normal.mem_comm infer_instance hx, end } lemma mem_upper_central_series_step (x : G) : x ∈ upper_central_series_step H ↔ ∀ y, x * y * x⁻¹ * y⁻¹ ∈ H := iff.rfl open quotient_group /-- The proof that `upper_central_series_step H` is the preimage of the centre of `G/H` under the canonical surjection. -/ lemma upper_central_series_step_eq_comap_center : upper_central_series_step H = subgroup.comap (mk' H) (center (quotient H)) := begin ext, rw [mem_comap, mem_center_iff, forall_coe], apply forall_congr, intro y, change x * y * x⁻¹ * y⁻¹ ∈ H ↔ ((y * x : G) : quotient H) = (x * y : G), rw [eq_comm, eq_iff_div_mem, div_eq_mul_inv], congr' 2, group, end instance : normal (upper_central_series_step H) := begin rw upper_central_series_step_eq_comap_center, apply_instance, end variable (G) /-- An auxiliary type-theoretic definition defining both the upper central series of a group, and a proof that it is normal, all in one go. -/ def upper_central_series_aux : ℕ → Σ' (H : subgroup G), normal H | 0 := ⟨⊥, infer_instance⟩ | (n + 1) := let un := upper_central_series_aux n, un_normal := un.2 in by exactI ⟨upper_central_series_step un.1, infer_instance⟩ /-- `upper_central_series G n` is the `n`th term in the upper central series of `G`. -/ def upper_central_series (n : ℕ) : subgroup G := (upper_central_series_aux G n).1 instance (n : ℕ) : normal (upper_central_series G n) := (upper_central_series_aux G n).2 lemma upper_central_series_zero_def : upper_central_series G 0 = ⊥ := rfl /-- The `n+1`st term of the upper central series `H i` has underlying set equal to the `x` such that `⁅x,G⁆ ⊆ H n`-/ lemma mem_upper_central_series_succ_iff {G : Type*} [group G] (n : ℕ) (x : G) : x ∈ upper_central_series G (n + 1) ↔ ∀ y : G, x * y * x⁻¹ * y⁻¹ ∈ upper_central_series G n := iff.rfl -- is_nilpotent is already defined in the root namespace (for elements of rings). /-- A group `G` is nilpotent if its upper central series is eventually `G`. -/ class group.is_nilpotent (G : Type*) [group G] : Prop := (nilpotent [] : ∃ n : ℕ, upper_central_series G n = ⊤) open group section classical open_locale classical /-- The nilpotency class of a nilpotent group is the small natural `n` such that the `n`'th term of the upper central series is `G`. -/ noncomputable def group.nilpotency_class (G : Type*) [group G] [is_nilpotent G] : ℕ := nat.find (is_nilpotent.nilpotent G) end classical variable {G} /-- A sequence of subgroups of `G` is an ascending central series if `H 0` is trivial and `⁅H (n + 1), G⁆ ⊆ H n` for all `n`. Note that we do not require that `H n = G` for some `n`. -/ def is_ascending_central_series (H : ℕ → subgroup G) : Prop := H 0 = ⊥ ∧ ∀ (x : G) (n : ℕ), x ∈ H (n + 1) → ∀ g, x * g * x⁻¹ * g⁻¹ ∈ H n /-- A sequence of subgroups of `G` is a descending central series if `H 0` is `G` and `⁅H n, G⁆ ⊆ H (n + 1)` for all `n`. Note that we do not requre that `H n = {1}` for some `n`. -/ def is_descending_central_series (H : ℕ → subgroup G) := H 0 = ⊤ ∧ ∀ (x : G) (n : ℕ), x ∈ H n → ∀ g, x * g * x⁻¹ * g⁻¹ ∈ H (n + 1) /-- Any ascending central series for a group is bounded above by the upper central series. -/ lemma ascending_central_series_le_upper (H : ℕ → subgroup G) (hH : is_ascending_central_series H) : ∀ n : ℕ, H n ≤ upper_central_series G n | 0 := hH.1.symm ▸ le_refl ⊥ | (n + 1) := begin specialize ascending_central_series_le_upper n, intros x hx, have := hH.2 x n hx, rw mem_upper_central_series_succ_iff, intro y, apply ascending_central_series_le_upper, apply this, end variable (G) /-- The upper central series of a group is an ascending central series. -/ lemma upper_central_series_is_ascending_central_series : is_ascending_central_series (upper_central_series G) := ⟨rfl, λ x n h, h⟩ /-- A group `G` is nilpotent iff there exists an ascending central series which reaches `G` in finitely many steps. -/ theorem nilpotent_iff_finite_ascending_central_series : is_nilpotent G ↔ ∃ H : ℕ → subgroup G, is_ascending_central_series H ∧ ∃ n : ℕ, H n = ⊤ := begin split, { intro h, use upper_central_series G, refine ⟨upper_central_series_is_ascending_central_series G, h.1⟩ }, { rintro ⟨H, hH, n, hn⟩, use n, have := ascending_central_series_le_upper H hH n, rw hn at this, exact eq_top_iff.mpr this } end /-- A group `G` is nilpotent iff there exists a descending central series which reaches the trivial group in a finite time. -/ theorem nilpotent_iff_finite_descending_central_series : is_nilpotent G ↔ ∃ H : ℕ → subgroup G, is_descending_central_series H ∧ ∃ n : ℕ, H n = ⊥ := begin rw nilpotent_iff_finite_ascending_central_series, split, { rintro ⟨H, ⟨h0, hH⟩, n, hn⟩, use (λ m, H (n - m)), split, { refine ⟨hn, λ x m hx g, _⟩, dsimp at hx, by_cases hm : n ≤ m, { have hnm : n - m = 0 := nat.sub_eq_zero_of_le hm, rw [hnm, h0, subgroup.mem_bot] at hx, subst hx, convert subgroup.one_mem _, group }, { push_neg at hm, apply hH, convert hx, rw nat.sub_succ, exact nat.succ_pred_eq_of_pos (nat.sub_pos_of_lt hm) } }, { use n, rwa nat.sub_self } }, { rintro ⟨H, ⟨h0, hH⟩, n, hn⟩, use (λ m, H (n - m)), split, { refine ⟨hn, λ x m hx g, _⟩, dsimp only at hx, by_cases hm : n ≤ m, { have hnm : n - m = 0 := nat.sub_eq_zero_of_le hm, dsimp only, rw [hnm, h0], exact mem_top _ }, { push_neg at hm, dsimp only, convert hH x _ hx g, rw nat.sub_succ, exact (nat.succ_pred_eq_of_pos (nat.sub_pos_of_lt hm)).symm } }, { use n, rwa nat.sub_self } }, end /-- The lower central series of a group `G` is a sequence `H n` of subgroups of `G`, defined by `H 0` is all of `G` and for `n≥1`, `H (n + 1) = ⁅H n, G⁆` -/ def lower_central_series (G : Type*) [group G] : ℕ → subgroup G | 0 := ⊤ | (n+1) := ⁅lower_central_series n, ⊤⁆ variable {G} /-- The lower central series of a group is a descending central series. -/ theorem lower_central_series_is_descending_central_series : is_descending_central_series (lower_central_series G) := begin split, refl, intros x n hxn g, exact general_commutator_containment _ _ hxn (subgroup.mem_top g), end /-- Any descending central series for a group is bounded below by the lower central series. -/ lemma descending_central_series_ge_lower (H : ℕ → subgroup G) (hH : is_descending_central_series H) : ∀ n : ℕ, lower_central_series G n ≤ H n | 0 := hH.1.symm ▸ le_refl ⊤ | (n + 1) := begin specialize descending_central_series_ge_lower n, apply (general_commutator_le _ _ _).2, intros x hx q _, exact hH.2 x n (descending_central_series_ge_lower hx) q, end /-- A group is nilpotent if and only if its lower central series eventually reaches the trivial subgroup. -/ theorem nilpotent_iff_lower_central_series : is_nilpotent G ↔ ∃ n, lower_central_series G n = ⊥ := begin rw nilpotent_iff_finite_descending_central_series, split, { rintro ⟨H, ⟨h0, hs⟩, n, hn⟩, use n, have := descending_central_series_ge_lower H ⟨h0, hs⟩ n, rw hn at this, exact eq_bot_iff.mpr this }, { intro h, use [lower_central_series G, lower_central_series_is_descending_central_series, h] }, end
f0d665a8c83a70858d136e34f93d13466b70aee6
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/measure_theory/covering/vitali_family.lean
00b8050e1f15b28747963a0bbb9675d0a653ac47
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,968
lean
/- Copyright (c) 2021 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 measure_theory.measure.measure_space /-! # Vitali families On a metric space `X` with a measure `μ`, consider for each `x : X` a family of measurable sets with nonempty interiors, called `sets_at x`. This family is a Vitali family if it satisfies the following property: consider a (possibly non-measurable) set `s`, and for any `x` in `s` a subfamily `f x` of `sets_at x` containing sets of arbitrarily small diameter. Then one can extract a disjoint subfamily covering almost all `s`. Vitali families are provided by covering theorems such as the Besicovitch covering theorem or the Vitali covering theorem. They make it possible to formulate general versions of theorems on differentiations of measure that apply in both contexts. This file gives the basic definition of Vitali families. More interesting developments of this notion are deferred to other files: * constructions of specific Vitali families are provided by the Besicovitch covering theorem, in `besicovitch.vitali_family`, and by the Vitali covering theorem, in `vitali.vitali_family`. * TODO: prove the main theorem on differentiation of measures along a Vitali family. ## Main definitions * `vitali_family μ` is a structure made, for each `x : X`, of a family of sets around `x`, such that one can extract an almost everywhere disjoint covering from any subfamily containing sets of arbitrarily small diameters. Let `v` be such a Vitali family. * `v.fine_subfamily_on` describes the subfamilies of `v` from which one can extract almost everywhere disjoint coverings. This property, called `v.fine_subfamily_on.exists_disjoint_covering_ae`, is essentially a restatement of the definition of a Vitali family. We also provide an API to use efficiently such a disjoint covering. * `v.filter_at x` is a filter on sets of `X`, such that convergence with respect to this filter means convergence when sets in the Vitali family shrink towards `x`. ## References * [Herbert Federer, Geometric Measure Theory, Chapter 2.8][Federer1996] (Vitali families are called Vitali relations there) -/ open measure_theory metric set filter topological_space measure_theory.measure open_locale filter measure_theory topological_space variables {α : Type*} [metric_space α] /-- On a metric space `X` with a measure `μ`, consider for each `x : X` a family of measurable sets with nonempty interiors, called `sets_at x`. This family is a Vitali family if it satisfies the following property: consider a (possibly non-measurable) set `s`, and for any `x` in `s` a subfamily `f x` of `sets_at x` containing sets of arbitrarily small diameter. Then one can extract a disjoint subfamily covering almost all `s`. Vitali families are provided by covering theorems such as the Besicovitch covering theorem or the Vitali covering theorem. They make it possible to formulate general versions of theorems on differentiations of measure that apply in both contexts. -/ @[nolint has_inhabited_instance] structure vitali_family {m : measurable_space α} (μ : measure α) := (sets_at : Π (x : α), set (set α)) (measurable_set' : ∀ (x : α), ∀ (a : set α), a ∈ sets_at x → measurable_set a) (nonempty_interior : ∀ (x : α), ∀ (y : set α), y ∈ sets_at x → (interior y).nonempty) (nontrivial : ∀ (x : α) (ε > (0 : ℝ)), ∃ y ∈ sets_at x, y ⊆ closed_ball x ε) (covering : ∀ (s : set α) (f : Π (x : α), set (set α)), (∀ x ∈ s, f x ⊆ sets_at x) → (∀ (x ∈ s) (ε > (0 : ℝ)), ∃ a ∈ f x, a ⊆ closed_ball x ε) → ∃ (t : set α) (u : α → set α), t ⊆ s ∧ t.pairwise_disjoint u ∧ (∀ x ∈ t, u x ∈ f x) ∧ μ (s \ ⋃ x ∈ t, u x) = 0) namespace vitali_family variables {m0 : measurable_space α} {μ : measure α} include μ /-- A Vitali family for a measure `μ` is also a Vitali family for any measure absolutely continuous with respect to `μ`. -/ def mono (v : vitali_family μ) (ν : measure α) (hν : ν ≪ μ) : vitali_family ν := { sets_at := v.sets_at, measurable_set' := v.measurable_set', nonempty_interior := v.nonempty_interior, nontrivial := v.nontrivial, covering := λ s f h h', begin rcases v.covering s f h h' with ⟨t, u, ts, u_disj, uf, μu⟩, exact ⟨t, u, ts, u_disj, uf, hν μu⟩ end } /-- Given a Vitali family `v` for a measure `μ`, a family `f` is a fine subfamily on a set `s` if every point `x` in `s` belongs to arbitrarily small sets in `v.sets_at x ∩ f x`. This is precisely the subfamilies for which the Vitali family definition ensures that one can extract a disjoint covering of almost all `s`. -/ def fine_subfamily_on (v : vitali_family μ) (f : α → set (set α)) (s : set α) : Prop := ∀ x ∈ s, ∀ (ε > 0), ∃ a ∈ v.sets_at x ∩ f x, a ⊆ closed_ball x ε namespace fine_subfamily_on variables {v : vitali_family μ} {f : α → set (set α)} {s : set α} (h : v.fine_subfamily_on f s) include h theorem exists_disjoint_covering_ae : ∃ (t : set α) (u : α → set α), t ⊆ s ∧ t.pairwise_disjoint u ∧ (∀ x ∈ t, u x ∈ v.sets_at x ∩ f x) ∧ μ (s \ ⋃ x ∈ t, u x) = 0 := v.covering s (λ x, v.sets_at x ∩ f x) (λ x hx, inter_subset_left _ _) h /-- Given `h : v.fine_subfamily_on f s`, then `h.index` is a subset of `s` parametrizing a disjoint covering of almost every `s`. -/ protected def index : set α := h.exists_disjoint_covering_ae.some /-- Given `h : v.fine_subfamily_on f s`, then `h.covering x` is a set in the family, for `x ∈ h.index`, such that these sets form a disjoint covering of almost every `s`. -/ protected def covering : α → set α := h.exists_disjoint_covering_ae.some_spec.some lemma index_subset : h.index ⊆ s := h.exists_disjoint_covering_ae.some_spec.some_spec.1 lemma covering_disjoint : h.index.pairwise_disjoint h.covering := h.exists_disjoint_covering_ae.some_spec.some_spec.2.1 lemma covering_disjoint_subtype : pairwise (disjoint on (λ x : h.index, h.covering x)) := (pairwise_subtype_iff_pairwise_set _ _).2 h.covering_disjoint lemma covering_mem {x : α} (hx : x ∈ h.index) : h.covering x ∈ f x := (h.exists_disjoint_covering_ae.some_spec.some_spec.2.2.1 x hx).2 lemma covering_mem_family {x : α} (hx : x ∈ h.index) : h.covering x ∈ v.sets_at x := (h.exists_disjoint_covering_ae.some_spec.some_spec.2.2.1 x hx).1 lemma measure_diff_bUnion : μ (s \ ⋃ x ∈ h.index, h.covering x) = 0 := h.exists_disjoint_covering_ae.some_spec.some_spec.2.2.2 lemma index_countable [second_countable_topology α] : countable h.index := h.covering_disjoint.countable_of_nonempty_interior (λ x hx, v.nonempty_interior _ _ (h.covering_mem_family hx)) protected lemma measurable_set_u {x : α} (hx : x ∈ h.index) : measurable_set (h.covering x) := v.measurable_set' x _ (h.covering_mem_family hx) lemma measure_le_tsum_of_absolutely_continuous [second_countable_topology α] {ρ : measure α} (hρ : ρ ≪ μ) : ρ s ≤ ∑' (x : h.index), ρ (h.covering x) := calc ρ s ≤ ρ ((s \ ⋃ (x ∈ h.index), h.covering x) ∪ (⋃ (x ∈ h.index), h.covering x)) : measure_mono (by simp only [subset_union_left, diff_union_self]) ... ≤ ρ (s \ ⋃ (x ∈ h.index), h.covering x) + ρ (⋃ (x ∈ h.index), h.covering x) : measure_union_le _ _ ... = ∑' (x : h.index), ρ (h.covering x) : by rw [hρ h.measure_diff_bUnion, measure_bUnion h.index_countable h.covering_disjoint (λ x hx, h.measurable_set_u hx), zero_add] lemma measure_le_tsum [second_countable_topology α] : μ s ≤ ∑' (x : h.index), μ (h.covering x) := h.measure_le_tsum_of_absolutely_continuous measure.absolutely_continuous.rfl end fine_subfamily_on variable (v : vitali_family μ) include v /-- Given a vitali family `v`, then `v.filter_at x` is the filter on `set α` made of those families that contain all sets of `v.sets_at x` of a sufficiently small diameter. This filter makes it possible to express limiting behavior when sets in `v.sets_at x` shrink to `x`. -/ def filter_at (x : α) : filter (set α) := ⨅ (ε ∈ Ioi (0 : ℝ)), 𝓟 {a ∈ v.sets_at x | a ⊆ closed_ball x ε} lemma mem_filter_at_iff {x : α} {s : set (set α)} : (s ∈ v.filter_at x) ↔ ∃ (ε > (0 : ℝ)), ∀ a ∈ v.sets_at x, a ⊆ closed_ball x ε → a ∈ s := begin simp only [filter_at, exists_prop, gt_iff_lt], rw mem_binfi_of_directed, { simp only [subset_def, and_imp, exists_prop, mem_sep_eq, mem_Ioi, mem_principal] }, { simp only [directed_on, exists_prop, ge_iff_le, le_principal_iff, mem_Ioi, order.preimage, mem_principal], assume x hx y hy, refine ⟨min x y, lt_min hx hy, λ a ha, ⟨ha.1, ha.2.trans (closed_ball_subset_closed_ball (min_le_left _ _))⟩, λ a ha, ⟨ha.1, ha.2.trans (closed_ball_subset_closed_ball (min_le_right _ _))⟩⟩ }, { exact ⟨(1 : ℝ), mem_Ioi.2 zero_lt_one⟩ } end instance filter_at_ne_bot (x : α) : (v.filter_at x).ne_bot := begin simp only [ne_bot_iff, ←empty_mem_iff_bot, mem_filter_at_iff, not_exists, exists_prop, mem_empty_eq, and_true, gt_iff_lt, not_and, ne.def, not_false_iff, not_forall], assume ε εpos, obtain ⟨w, w_sets, hw⟩ : ∃ (w ∈ v.sets_at x), w ⊆ closed_ball x ε := v.nontrivial x ε εpos, exact ⟨w, w_sets, hw⟩ end lemma eventually_filter_at_iff {x : α} {P : set α → Prop} : (∀ᶠ a in v.filter_at x, P a) ↔ ∃ (ε > (0 : ℝ)), ∀ a ∈ v.sets_at x, a ⊆ closed_ball x ε → P a := v.mem_filter_at_iff lemma eventually_filter_at_mem_sets (x : α) : ∀ᶠ a in v.filter_at x, a ∈ v.sets_at x := begin simp only [eventually_filter_at_iff, exists_prop, and_true, gt_iff_lt, implies_true_iff] {contextual := tt}, exact ⟨1, zero_lt_one⟩ end lemma frequently_filter_at_iff {x : α} {P : set α → Prop} : (∃ᶠ a in v.filter_at x, P a) ↔ ∀ (ε > (0 : ℝ)), ∃ a ∈ v.sets_at x, a ⊆ closed_ball x ε ∧ P a := by simp only [filter.frequently, eventually_filter_at_iff, not_exists, exists_prop, not_and, not_not, not_forall] lemma eventually_filter_at_subset_of_nhds {x : α} {o : set α} (hx : o ∈ 𝓝 x) : ∀ᶠ a in v.filter_at x, a ⊆ o := begin rw eventually_filter_at_iff, rcases metric.mem_nhds_iff.1 hx with ⟨ε, εpos, hε⟩, exact ⟨ε/2, half_pos εpos, λ a av ha, ha.trans ((closed_ball_subset_ball (half_lt_self εpos)).trans hε)⟩ end lemma fine_subfamily_on_of_frequently (v : vitali_family μ) (f : α → set (set α)) (s : set α) (h : ∀ x ∈ s, ∃ᶠ a in v.filter_at x, a ∈ f x) : v.fine_subfamily_on f s := begin assume x hx ε εpos, obtain ⟨a, av, ha, af⟩ : ∃ (a : set α) (H : a ∈ v.sets_at x), a ⊆ closed_ball x ε ∧ a ∈ f x := v.frequently_filter_at_iff.1 (h x hx) ε εpos, exact ⟨a, ⟨av, af⟩, ha⟩, end end vitali_family
b1f8eea1fec5a0b48b4e054a203dc1b9100be7c9
9028d228ac200bbefe3a711342514dd4e4458bff
/src/data/support.lean
6d6c113e9fc7cd0b37714bbf5a364540efff3f42
[ "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
5,471
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 order.conditionally_complete_lattice import algebra.big_operators.basic import algebra.group.prod import algebra.group.pi /-! # Support of a function In this file we define `function.support f = {x | f x ≠ 0}` and prove its basic properties. -/ universes u v w x y open set open_locale big_operators namespace function variables {α : Type u} {β : Type v} {ι : Sort w} {A : Type x} {B : Type y} /-- `support` of a function is the set of points `x` such that `f x ≠ 0`. -/ def support [has_zero A] (f : α → A) : set α := {x | f x ≠ 0} lemma nmem_support [has_zero A] {f : α → A} {x : α} : x ∉ support f ↔ f x = 0 := not_not lemma mem_support [has_zero A] {f : α → A} {x : α} : x ∈ support f ↔ f x ≠ 0 := iff.rfl lemma support_subset_iff [has_zero A] {f : α → A} {s : set α} : support f ⊆ s ↔ ∀ x, f x ≠ 0 → x ∈ s := iff.rfl lemma support_subset_iff' [has_zero A] {f : α → A} {s : set α} : support f ⊆ s ↔ ∀ x ∉ s, f x = 0 := forall_congr $ λ x, by classical; exact not_imp_comm @[simp] lemma support_eq_empty_iff [has_zero A] {f : α → A} : support f = ∅ ↔ f = 0 := by { simp_rw [← subset_empty_iff, support_subset_iff', funext_iff], simp } lemma support_binop_subset [has_zero A] (op : A → A → A) (op0 : op 0 0 = 0) (f g : α → A) : support (λ x, op (f x) (g x)) ⊆ support f ∪ support g := λ x hx, classical.by_cases (λ hf : f x = 0, or.inr $ λ hg, hx $ by simp only [hf, hg, op0]) or.inl lemma support_add [add_monoid A] (f g : α → A) : support (λ x, f x + g x) ⊆ support f ∪ support g := support_binop_subset (+) (zero_add _) f g @[simp] lemma support_neg [add_group A] (f : α → A) : support (λ x, -f x) = support f := set.ext $ λ x, not_congr neg_eq_zero lemma support_sub [add_group A] (f g : α → A) : support (λ x, f x - g x) ⊆ support f ∪ support g := support_binop_subset (has_sub.sub) (sub_self _) f g @[simp] lemma support_mul [mul_zero_class A] [no_zero_divisors A] (f g : α → A) : support (λ x, f x * g x) = support f ∩ support g := set.ext $ λ x, by simp only [support, ne.def, mul_eq_zero, mem_set_of_eq, mem_inter_iff, not_or_distrib] @[simp] lemma support_inv [division_ring A] (f : α → A) : support (λ x, (f x)⁻¹) = support f := set.ext $ λ x, not_congr inv_eq_zero @[simp] lemma support_div [division_ring A] (f g : α → A) : support (λ x, f x / g x) = support f ∩ support g := by simp [div_eq_mul_inv] lemma support_sup [has_zero A] [semilattice_sup A] (f g : α → A) : support (λ x, (f x) ⊔ (g x)) ⊆ support f ∪ support g := support_binop_subset (⊔) sup_idem f g lemma support_inf [has_zero A] [semilattice_inf A] (f g : α → A) : support (λ x, (f x) ⊓ (g x)) ⊆ support f ∪ support g := support_binop_subset (⊓) inf_idem f g lemma support_max [has_zero A] [decidable_linear_order A] (f g : α → A) : support (λ x, max (f x) (g x)) ⊆ support f ∪ support g := support_sup f g lemma support_min [has_zero A] [decidable_linear_order A] (f g : α → A) : support (λ x, min (f x) (g x)) ⊆ support f ∪ support g := support_inf f g lemma support_supr [has_zero A] [conditionally_complete_lattice A] [nonempty ι] (f : ι → α → A) : support (λ x, ⨆ i, f i x) ⊆ ⋃ i, support (f i) := begin intros x hx, classical, contrapose hx, simp only [mem_Union, not_exists, nmem_support] at hx ⊢, simp only [hx, csupr_const] end lemma support_infi [has_zero A] [conditionally_complete_lattice A] [nonempty ι] (f : ι → α → A) : support (λ x, ⨅ i, f i x) ⊆ ⋃ i, support (f i) := @support_supr _ _ (order_dual A) ⟨(0:A)⟩ _ _ f lemma support_sum [add_comm_monoid A] (s : finset α) (f : α → β → A) : support (λ x, ∑ i in s, f i x) ⊆ ⋃ i ∈ s, support (f i) := begin intros x hx, classical, contrapose hx, simp only [mem_Union, not_exists, nmem_support] at hx ⊢, exact finset.sum_eq_zero hx end lemma support_prod_subset [comm_monoid_with_zero A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) ⊆ ⋂ i ∈ s, support (f i) := λ x hx, mem_bInter_iff.2 $ λ i hi H, hx $ finset.prod_eq_zero hi H lemma support_prod [comm_monoid_with_zero A] [no_zero_divisors A] [nontrivial A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) = ⋂ i ∈ s, support (f i) := set.ext $ λ x, by simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists] lemma support_comp_subset [has_zero A] [has_zero B] {g : A → B} (hg : g 0 = 0) (f : α → A) : support (g ∘ f) ⊆ support f := λ x, mt $ λ h, by simp [(∘), *] lemma support_subset_comp [has_zero A] [has_zero B] {g : A → B} (hg : ∀ {x}, g x = 0 → x = 0) (f : α → A) : support f ⊆ support (g ∘ f) := λ x, mt hg lemma support_comp_eq [has_zero A] [has_zero B] (g : A → B) (hg : ∀ {x}, g x = 0 ↔ x = 0) (f : α → A) : support (g ∘ f) = support f := set.ext $ λ x, not_congr hg lemma support_prod_mk [has_zero A] [has_zero B] (f : α → A) (g : α → B) : support (λ x, (f x, g x)) = support f ∪ support g := set.ext $ λ x, by simp only [support, not_and_distrib, mem_union_eq, mem_set_of_eq, prod.mk_eq_zero, ne.def] end function
d2199c7de359b2c5825b920f3be6fff22be57057
9dc8cecdf3c4634764a18254e94d43da07142918
/src/set_theory/game/nim.lean
ef5a43a5db66f2afd8d76674b4a023314401b712
[ "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
15,087
lean
/- Copyright (c) 2020 Fox Thomson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Fox Thomson, Markus Himmel -/ import data.nat.bitwise import set_theory.game.birthday import set_theory.game.impartial /-! # Nim and the Sprague-Grundy theorem This file contains the definition for nim for any ordinal `o`. In the game of `nim o₁` both players may move to `nim o₂` for any `o₂ < o₁`. We also define a Grundy value for an impartial game `G` and prove the Sprague-Grundy theorem, that `G` is equivalent to `nim (grundy_value G)`. Finally, we compute the sum of finite Grundy numbers: if `G` and `H` have Grundy values `n` and `m`, where `n` and `m` are natural numbers, then `G + H` has the Grundy value `n xor m`. ## Implementation details The pen-and-paper definition of nim defines the possible moves of `nim o` to be `set.Iio o`. However, this definition does not work for us because it would make the type of nim `ordinal.{u} → pgame.{u + 1}`, which would make it impossible for us to state the Sprague-Grundy theorem, since that requires the type of `nim` to be `ordinal.{u} → pgame.{u}`. For this reason, we instead use `o.out.α` for the possible moves. You can use `to_left_moves_nim` and `to_right_moves_nim` to convert an ordinal less than `o` into a left or right move of `nim o`, and vice versa. -/ noncomputable theory universe u open_locale pgame namespace pgame /-- The definition of single-heap nim, which can be viewed as a pile of stones where each player can take a positive number of stones from it on their turn. -/ -- Uses `noncomputable!` to avoid `rec_fn_macro only allowed in meta definitions` VM error noncomputable! def nim : ordinal.{u} → pgame.{u} | o₁ := let f := λ o₂, have ordinal.typein o₁.out.r o₂ < o₁ := ordinal.typein_lt_self o₂, nim (ordinal.typein o₁.out.r o₂) in ⟨o₁.out.α, o₁.out.α, f, f⟩ using_well_founded { dec_tac := tactic.assumption } open ordinal lemma nim_def (o : ordinal) : nim o = pgame.mk o.out.α o.out.α (λ o₂, nim (ordinal.typein (<) o₂)) (λ o₂, nim (ordinal.typein (<) o₂)) := by { rw nim, refl } lemma left_moves_nim (o : ordinal) : (nim o).left_moves = o.out.α := by { rw nim_def, refl } lemma right_moves_nim (o : ordinal) : (nim o).right_moves = o.out.α := by { rw nim_def, refl } lemma move_left_nim_heq (o : ordinal) : (nim o).move_left == λ i : o.out.α, nim (typein (<) i) := by { rw nim_def, refl } lemma move_right_nim_heq (o : ordinal) : (nim o).move_right == λ i : o.out.α, nim (typein (<) i) := by { rw nim_def, refl } /-- Turns an ordinal less than `o` into a left move for `nim o` and viceversa. -/ noncomputable def to_left_moves_nim {o : ordinal} : set.Iio o ≃ (nim o).left_moves := (enum_iso_out o).to_equiv.trans (equiv.cast (left_moves_nim o).symm) /-- Turns an ordinal less than `o` into a right move for `nim o` and viceversa. -/ noncomputable def to_right_moves_nim {o : ordinal} : set.Iio o ≃ (nim o).right_moves := (enum_iso_out o).to_equiv.trans (equiv.cast (right_moves_nim o).symm) @[simp] theorem to_left_moves_nim_symm_lt {o : ordinal} (i : (nim o).left_moves) : ↑(to_left_moves_nim.symm i) < o := (to_left_moves_nim.symm i).prop @[simp] theorem to_right_moves_nim_symm_lt {o : ordinal} (i : (nim o).right_moves) : ↑(to_right_moves_nim.symm i) < o := (to_right_moves_nim.symm i).prop @[simp] lemma move_left_nim' {o : ordinal.{u}} (i) : (nim o).move_left i = nim (to_left_moves_nim.symm i).val := (congr_heq (move_left_nim_heq o).symm (cast_heq _ i)).symm lemma move_left_nim {o : ordinal} (i) : (nim o).move_left (to_left_moves_nim i) = nim i := by simp @[simp] lemma move_right_nim' {o : ordinal} (i) : (nim o).move_right i = nim (to_right_moves_nim.symm i).val := (congr_heq (move_right_nim_heq o).symm (cast_heq _ i)).symm lemma move_right_nim {o : ordinal} (i) : (nim o).move_right (to_right_moves_nim i) = nim i := by simp instance is_empty_nim_zero_left_moves : is_empty (nim 0).left_moves := by { rw nim_def, exact ordinal.is_empty_out_zero } instance is_empty_nim_zero_right_moves : is_empty (nim 0).right_moves := by { rw nim_def, exact ordinal.is_empty_out_zero } /-- `nim 0` has exactly the same moves as `0`. -/ def nim_zero_relabelling : nim 0 ≡r 0 := relabelling.is_empty _ theorem nim_zero_equiv : nim 0 ≈ 0 := equiv.is_empty _ noncomputable instance unique_nim_one_left_moves : unique (nim 1).left_moves := (equiv.cast $ left_moves_nim 1).unique noncomputable instance unique_nim_one_right_moves : unique (nim 1).right_moves := (equiv.cast $ right_moves_nim 1).unique @[simp] theorem default_nim_one_left_moves_eq : (default : (nim 1).left_moves) = @to_left_moves_nim 1 ⟨0, ordinal.zero_lt_one⟩ := rfl @[simp] theorem default_nim_one_right_moves_eq : (default : (nim 1).right_moves) = @to_right_moves_nim 1 ⟨0, ordinal.zero_lt_one⟩ := rfl @[simp] theorem to_left_moves_nim_one_symm (i) : (@to_left_moves_nim 1).symm i = ⟨0, ordinal.zero_lt_one⟩ := by simp @[simp] theorem to_right_moves_nim_one_symm (i) : (@to_right_moves_nim 1).symm i = ⟨0, ordinal.zero_lt_one⟩ := by simp theorem nim_one_move_left (x) : (nim 1).move_left x = nim 0 := by simp theorem nim_one_move_right (x) : (nim 1).move_right x = nim 0 := by simp /-- `nim 1` has exactly the same moves as `star`. -/ def nim_one_relabelling : nim 1 ≡r star := begin rw nim_def, refine ⟨_, _, λ i, _, λ j, _⟩, any_goals { dsimp, apply equiv.equiv_of_unique }, all_goals { simp, exact nim_zero_relabelling } end theorem nim_one_equiv : nim 1 ≈ star := nim_one_relabelling.equiv @[simp] lemma nim_birthday (o : ordinal) : (nim o).birthday = o := begin induction o using ordinal.induction with o IH, rw [nim_def, birthday_def], dsimp, rw max_eq_right le_rfl, convert lsub_typein o, exact funext (λ i, IH _ (typein_lt_self i)) end @[simp] lemma neg_nim (o : ordinal) : -nim o = nim o := begin induction o using ordinal.induction with o IH, rw nim_def, dsimp; congr; funext i; exact IH _ (ordinal.typein_lt_self i) end instance nim_impartial (o : ordinal) : impartial (nim o) := begin induction o using ordinal.induction with o IH, rw [impartial_def, neg_nim], refine ⟨equiv_rfl, λ i, _, λ i, _⟩; simpa using IH _ (typein_lt_self _) end lemma exists_ordinal_move_left_eq {o : ordinal} (i) : ∃ o' < o, (nim o).move_left i = nim o' := ⟨_, typein_lt_self _, move_left_nim' i⟩ lemma exists_move_left_eq {o o' : ordinal} (h : o' < o) : ∃ i, (nim o).move_left i = nim o' := ⟨to_left_moves_nim ⟨o', h⟩, by simp⟩ lemma nim_fuzzy_zero_of_ne_zero {o : ordinal} (ho : o ≠ 0) : nim o ∥ 0 := begin rw [impartial.fuzzy_zero_iff_lf, nim_def, lf_zero_le], rw ←ordinal.pos_iff_ne_zero at ho, exact ⟨(ordinal.principal_seg_out ho).top, by simp⟩ end @[simp] lemma nim_add_equiv_zero_iff (o₁ o₂ : ordinal) : nim o₁ + nim o₂ ≈ 0 ↔ o₁ = o₂ := begin split, { contrapose, intro h, rw [impartial.not_equiv_zero_iff], wlog h' : o₁ ≤ o₂ using [o₁ o₂, o₂ o₁], { exact le_total o₁ o₂ }, { have h : o₁ < o₂ := lt_of_le_of_ne h' h, rw [impartial.fuzzy_zero_iff_gf, zero_lf_le, nim_def o₂], refine ⟨to_left_moves_add (sum.inr _), _⟩, { exact (ordinal.principal_seg_out h).top }, { simpa using (impartial.add_self (nim o₁)).2 } }, { exact (fuzzy_congr_left add_comm_equiv).1 (this (ne.symm h)) } }, { rintro rfl, exact impartial.add_self (nim o₁) } end @[simp] lemma nim_add_fuzzy_zero_iff {o₁ o₂ : ordinal} : nim o₁ + nim o₂ ∥ 0 ↔ o₁ ≠ o₂ := by rw [iff_not_comm, impartial.not_fuzzy_zero_iff, nim_add_equiv_zero_iff] @[simp] lemma nim_equiv_iff_eq {o₁ o₂ : ordinal} : nim o₁ ≈ nim o₂ ↔ o₁ = o₂ := by rw [impartial.equiv_iff_add_equiv_zero, nim_add_equiv_zero_iff] /-- The Grundy value of an impartial game, the ordinal which corresponds to the game of nim that the game is equivalent to -/ noncomputable def grundy_value : Π (G : pgame.{u}), ordinal.{u} | G := ordinal.mex.{u u} (λ i, grundy_value (G.move_left i)) using_well_founded { dec_tac := pgame_wf_tac } lemma grundy_value_eq_mex_left (G : pgame) : grundy_value G = ordinal.mex.{u u} (λ i, grundy_value (G.move_left i)) := by rw grundy_value /-- The Sprague-Grundy theorem which states that every impartial game is equivalent to a game of nim, namely the game of nim corresponding to the games Grundy value -/ theorem equiv_nim_grundy_value : ∀ (G : pgame.{u}) [G.impartial], G ≈ nim (grundy_value G) | G := begin introI hG, rw [impartial.equiv_iff_add_equiv_zero, ←impartial.forall_left_moves_fuzzy_iff_equiv_zero], intro i, apply left_moves_add_cases i, { intro i₁, rw add_move_left_inl, apply (fuzzy_congr_left (add_congr_left (equiv_nim_grundy_value (G.move_left i₁)).symm)).1, rw nim_add_fuzzy_zero_iff, intro heq, rw [eq_comm, grundy_value_eq_mex_left G] at heq, have h := ordinal.ne_mex _, rw heq at h, exact (h i₁).irrefl }, { intro i₂, rw [add_move_left_inr, ←impartial.exists_left_move_equiv_iff_fuzzy_zero], revert i₂, rw nim_def, intro i₂, have h' : ∃ i : G.left_moves, (grundy_value (G.move_left i)) = ordinal.typein (quotient.out (grundy_value G)).r i₂, { revert i₂, rw grundy_value_eq_mex_left, intros i₂, have hnotin : _ ∉ _ := λ hin, (le_not_le_of_lt (ordinal.typein_lt_self i₂)).2 (cInf_le' hin), simpa using hnotin}, cases h' with i hi, use to_left_moves_add (sum.inl i), rw [add_move_left_inl, move_left_mk], apply (add_congr_left (equiv_nim_grundy_value (G.move_left i))).trans, simpa only [hi] using impartial.add_self (nim (grundy_value (G.move_left i))) } end using_well_founded { dec_tac := pgame_wf_tac } lemma grundy_value_eq_iff_equiv_nim {G : pgame} [G.impartial] {o : ordinal} : grundy_value G = o ↔ G ≈ nim o := ⟨by { rintro rfl, exact equiv_nim_grundy_value G }, by { intro h, rw ←nim_equiv_iff_eq, exact (equiv_nim_grundy_value G).symm.trans h }⟩ @[simp] lemma nim_grundy_value (o : ordinal.{u}) : grundy_value (nim o) = o := grundy_value_eq_iff_equiv_nim.2 pgame.equiv_rfl lemma grundy_value_eq_iff_equiv (G H : pgame) [G.impartial] [H.impartial] : grundy_value G = grundy_value H ↔ G ≈ H := grundy_value_eq_iff_equiv_nim.trans (equiv_congr_left.1 (equiv_nim_grundy_value H) _).symm @[simp] lemma grundy_value_zero : grundy_value 0 = 0 := grundy_value_eq_iff_equiv_nim.2 nim_zero_equiv.symm lemma grundy_value_iff_equiv_zero (G : pgame) [G.impartial] : grundy_value G = 0 ↔ G ≈ 0 := by rw [←grundy_value_eq_iff_equiv, grundy_value_zero] @[simp] lemma grundy_value_star : grundy_value star = 1 := grundy_value_eq_iff_equiv_nim.2 nim_one_equiv.symm @[simp] lemma grundy_value_neg (G : pgame) [G.impartial] : grundy_value (-G) = grundy_value G := by rw [grundy_value_eq_iff_equiv_nim, neg_equiv_iff, neg_nim, ←grundy_value_eq_iff_equiv_nim] lemma grundy_value_eq_mex_right : ∀ (G : pgame) [G.impartial], grundy_value G = ordinal.mex.{u u} (λ i, grundy_value (G.move_right i)) | ⟨l, r, L, R⟩ := begin introI H, rw [←grundy_value_neg, grundy_value_eq_mex_left], congr, ext i, haveI : (R i).impartial := @impartial.move_right_impartial ⟨l, r, L, R⟩ _ i, apply grundy_value_neg end @[simp] lemma grundy_value_nim_add_nim (n m : ℕ) : grundy_value (nim.{u} n + nim.{u} m) = nat.lxor n m := begin induction n using nat.strong_induction_on with n hn generalizing m, induction m using nat.strong_induction_on with m hm, rw [grundy_value_eq_mex_left], -- We want to show that `n xor m` is the smallest unreachable Grundy value. We will do this in two -- steps: -- h₀: `n xor m` is not a reachable grundy number. -- h₁: every Grundy number strictly smaller than `n xor m` is reachable. have h₀ : ∀ i, grundy_value ((nim n + nim m).move_left i) ≠ (nat.lxor n m : ordinal), { -- To show that `n xor m` is unreachable, we show that every move produces a Grundy number -- different from `n xor m`. intro i, -- The move operates either on the left pile or on the right pile. apply left_moves_add_cases i, all_goals { -- One of the piles is reduced to `k` stones, with `k < n` or `k < m`. intro a, obtain ⟨ok, hk, hk'⟩ := exists_ordinal_move_left_eq a, obtain ⟨k, rfl⟩ := ordinal.lt_omega.1 (lt_trans hk (ordinal.nat_lt_omega _)), replace hk := ordinal.nat_cast_lt.1 hk, -- Thus, the problem is reduced to computing the Grundy value of `nim n + nim k` or -- `nim k + nim m`, both of which can be dealt with using an inductive hypothesis. simp only [hk', add_move_left_inl, add_move_left_inr, id], rw hn _ hk <|> rw hm _ hk, -- But of course xor is injective, so if we change one of the arguments, we will not get the -- same value again. intro h, rw ordinal.nat_cast_inj at h, try { rw [nat.lxor_comm n k, nat.lxor_comm n m] at h }, exact hk.ne (nat.lxor_left_injective h) } }, have h₁ : ∀ (u : ordinal), u < nat.lxor n m → u ∈ set.range (λ i, grundy_value ((nim n + nim m).move_left i)), { -- Take any natural number `u` less than `n xor m`. intros ou hu, obtain ⟨u, rfl⟩ := ordinal.lt_omega.1 (lt_trans hu (ordinal.nat_lt_omega _)), replace hu := ordinal.nat_cast_lt.1 hu, -- Our goal is to produce a move that gives the Grundy value `u`. rw set.mem_range, -- By a lemma about xor, either `u xor m < n` or `u xor n < m`. cases nat.lt_lxor_cases hu with h h, -- Therefore, we can play the corresponding move, and by the inductive hypothesis the new state -- is `(u xor m) xor m = u` or `n xor (u xor n) = u` as required. { obtain ⟨i, hi⟩ := exists_move_left_eq (ordinal.nat_cast_lt.2 h), refine ⟨to_left_moves_add (sum.inl i), _⟩, simp only [hi, add_move_left_inl], rw [hn _ h, nat.lxor_assoc, nat.lxor_self, nat.lxor_zero] }, { obtain ⟨i, hi⟩ := exists_move_left_eq (ordinal.nat_cast_lt.2 h), refine ⟨to_left_moves_add (sum.inr i), _⟩, simp only [hi, add_move_left_inr], rw [hm _ h, nat.lxor_comm, nat.lxor_assoc, nat.lxor_self, nat.lxor_zero] } }, -- We are done! apply (ordinal.mex_le_of_ne.{u u} h₀).antisymm, contrapose! h₁, exact ⟨_, ⟨h₁, ordinal.mex_not_mem_range _⟩⟩, end lemma nim_add_nim_equiv {n m : ℕ} : nim n + nim m ≈ nim (nat.lxor n m) := by rw [←grundy_value_eq_iff_equiv_nim, grundy_value_nim_add_nim] lemma grundy_value_add (G H : pgame) [G.impartial] [H.impartial] {n m : ℕ} (hG : grundy_value G = n) (hH : grundy_value H = m) : grundy_value (G + H) = nat.lxor n m := begin rw [←nim_grundy_value (nat.lxor n m), grundy_value_eq_iff_equiv], refine equiv.trans _ nim_add_nim_equiv, convert add_congr (equiv_nim_grundy_value G) (equiv_nim_grundy_value H); simp only [hG, hH] end end pgame
b894777544651fa01b2ef9a761159f1f79e83fd6
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/hott/delta_issue2.hlean
e4f55d105955f020a3ccdd543f7824c0532ba7fa
[ "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
720
hlean
open nat eq infixr + := sum theorem add_assoc₁ : Π (a b c : ℕ), (a + b) + c = a + (b + c) | a b 0 := eq.refl (nat.rec a (λ x, succ) b) | a b (succ n) := calc (a + b) + (succ n) = succ ((a + b) + n) : rfl ... = succ (a + (b + n)) : ap succ (add_assoc₁ a b n) ... = a + (succ (b + n)) : rfl ... = a + (b + (succ n)) : rfl theorem add_assoc₂ : Π (a b c : ℕ), (a + b) + c = a + (b + c) | a b 0 := eq.refl (nat.rec a (λ x, succ) b) | a b (succ n) := ap succ (add_assoc₂ a b n) theorem add_assoc₃ : Π (a b c : ℕ), (a + b) + c = a + (b + c) | a b nat.zero := eq.refl (nat.add a b) | a b (succ n) := ap succ (add_assoc₃ a b n)
197350e91a50ddc12b21858cad4da8c43efa9214
8c02fed42525b65813b55c064afe2484758d6d09
/src/irsem_smt.lean
e61fb3a5f00183563e1e1d1af01af452aeaca993
[ "LicenseRef-scancode-generic-cla", "MIT" ]
permissive
microsoft/AliveInLean
3eac351a34154efedd3ffc4fe2fa4ec01b219e0d
4b739dd6e4266b26a045613849df221374119871
refs/heads/master
1,691,419,737,939
1,689,365,567,000
1,689,365,568,000
131,156,103
23
18
NOASSERTION
1,660,342,040,000
1,524,747,538,000
Lean
UTF-8
Lean
false
false
2,164
lean
-- Copyright (c) Microsoft Corporation. All rights reserved. -- Licensed under the MIT license. import system.io import smt2.syntax import smt2.builder import .irsem import .lang import .common import .bitvector import .smtexpr import .smtcompile open io def irsem_smt : irsem := { intty := sbitvec, poisonty := sbool, boolty := sbool } namespace irsem_smt open irsem local attribute [reducible] irsem_smt @[reducible] instance iul: uint_like irsem_smt.intty := sbitvec_is_uint_like @[reducible] instance ihc: has_comp irsem_smt.intty irsem_smt.boolty := sbitvec_has_comp @[reducible] instance ioc: has_overflow_check irsem_smt.intty irsem_smt.boolty := sbitvec_has_overflow_check @[reducible] instance pbl: bool_like irsem_smt.poisonty := sbool_is_bool_like @[reducible] instance p2b: has_coe irsem_smt.poisonty irsem_smt.boolty := ⟨@id sbool⟩ @[reducible] instance bbl: bool_like irsem_smt.boolty := sbool_is_bool_like @[reducible] instance b2i: has_coe irsem_smt.boolty (irsem_smt.intty size.one) := sbool_sbitvec_has_coe @[reducible] instance bhi (sz:size): has_ite irsem_smt.boolty (irsem_smt.intty sz) := sbitvec_has_ite @[reducible] instance bhi2 : has_ite irsem_smt.boolty irsem_smt.poisonty := sbool_has_ite meta instance poisonty_tostr: has_to_string irsem_smt.poisonty := ⟨λ b:sbool, to_string b⟩ meta instance valty_tostr: has_to_string irsem_smt.valty := ⟨λ b, match b with | (irsem.valty.ival sz b p) := has_to_string.to_string b ++ ", IS_NOT_POISON:" ++ to_string p end⟩ meta instance boolty_tostr: has_to_string irsem_smt.boolty := ⟨λ b:sbool, to_string b⟩ meta instance tostr: has_to_string irsem_smt.irstate := ⟨λ st, irstate.to_string' irsem_smt st⟩ meta def int_to_smt (sz:size) : irsem_smt.intty sz → smt2.term | x := smt.compile_sbitvec x meta def val_to_smt : irsem_smt.valty → smt2.term | (irsem.valty.ival sz t _) := int_to_smt sz t meta def poison_to_smt : irsem_smt.valty → smt2.term | (irsem.valty.ival _ _ p) := smt.compile_sbool p meta def bool_to_smt : irsem_smt.boolty → smt2.term | u := smt.compile_sbool u end irsem_smt
73013e282408e9d8e91bc95869b28d27852a784d
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/algebra/lie_algebra.lean
771fce260fe489fac229c5ed00c91d70839a0a4b
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
26,487
lean
/- Copyright (c) 2019 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import ring_theory.algebra import linear_algebra.linear_action /-! # Lie algebras This file defines Lie rings, and Lie algebras over a commutative ring. It shows how these arise from associative rings and algebras via the ring commutator. In particular it defines the Lie algebra of endomorphisms of a module as well as of the algebra of square matrices over a commutative ring. It also includes definitions of morphisms of Lie algebras, Lie subalgebras, Lie modules, Lie submodules, and the quotient of a Lie algebra by an ideal. ## Notations We introduce the notation ⁅x, y⁆ for the Lie bracket. Note that these are the Unicode "square with quill" brackets rather than the usual square brackets. We also introduce the notations L →ₗ⁅R⁆ L' for a morphism of Lie algebras over a commutative ring R, and L →ₗ⁅⁆ L' for the same, when the ring is implicit. ## Implementation notes Lie algebras are defined as modules with a compatible Lie ring structure, and thus are partially unbundled. Since they extend Lie rings, these are also partially unbundled. ## References * [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 1--3*][bourbaki1975] ## Tags lie bracket, ring commutator, jacobi identity, lie ring, lie algebra -/ universes u v w w₁ /-- A binary operation, intended use in Lie algebras and similar structures. -/ class has_bracket (L : Type v) := (bracket : L → L → L) notation `⁅`x`,` y`⁆` := has_bracket.bracket x y /-- An Abelian Lie algebra is one in which all brackets vanish. Arguably this class belongs in the `has_bracket` namespace but it seems much more user-friendly to compromise slightly and put it in the `lie_algebra` namespace. -/ class lie_algebra.is_abelian (L : Type v) [has_bracket L] [has_zero L] : Prop := (abelian : ∀ (x y : L), ⁅x, y⁆ = 0) namespace ring_commutator variables {A : Type v} [ring A] /-- The ring commutator captures the extent to which a ring is commutative. It is identically zero exactly when the ring is commutative. -/ def commutator (x y : A) := x*y - y*x local notation `⁅`x`,` y`⁆` := commutator x y @[simp] lemma add_left (x y z : A) : ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆ := by simp [commutator, right_distrib, left_distrib, sub_eq_add_neg, add_comm, add_left_comm] @[simp] lemma add_right (x y z : A) : ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆ := by simp [commutator, right_distrib, left_distrib, sub_eq_add_neg, add_comm, add_left_comm] @[simp] lemma alternate (x : A) : ⁅x, x⁆ = 0 := by simp [commutator] lemma jacobi (x y z : A) : ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0 := begin unfold commutator, repeat { rw mul_sub_left_distrib }, repeat { rw mul_sub_right_distrib }, repeat { rw add_sub }, repeat { rw ←sub_add }, repeat { rw ←mul_assoc }, have h : ∀ (x y z : A), x - y + z + y = x+z, {simp [sub_eq_add_neg, add_left_comm, add_assoc]}, repeat { rw h }, simp [sub_eq_add_neg, add_left_comm, add_assoc], end end ring_commutator section prio set_option default_priority 100 -- see Note [default priority] /-- A Lie ring is an additive group with compatible product, known as the bracket, satisfying the Jacobi identity. The bracket is not associative unless it is identically zero. -/ class lie_ring (L : Type v) extends add_comm_group L, has_bracket L := (add_lie : ∀ (x y z : L), ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆) (lie_add : ∀ (x y z : L), ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆) (lie_self : ∀ (x : L), ⁅x, x⁆ = 0) (jacobi : ∀ (x y z : L), ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0) end prio section lie_ring variables {L : Type v} [lie_ring L] @[simp] lemma add_lie (x y z : L) : ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆ := lie_ring.add_lie x y z @[simp] lemma lie_add (x y z : L) : ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆ := lie_ring.lie_add x y z @[simp] lemma lie_self (x : L) : ⁅x, x⁆ = 0 := lie_ring.lie_self x @[simp] lemma lie_skew (x y : L) : -⁅y, x⁆ = ⁅x, y⁆ := begin symmetry, rw [←sub_eq_zero_iff_eq, sub_neg_eq_add], have H : ⁅x + y, x + y⁆ = 0, from lie_self _, rw add_lie at H, simpa using H, end @[simp] lemma lie_zero (x : L) : ⁅x, 0⁆ = 0 := begin have H : ⁅x, 0⁆ + ⁅x, 0⁆ = ⁅x, 0⁆ + 0 := by { rw ←lie_add, simp, }, exact add_left_cancel H, end @[simp] lemma zero_lie (x : L) : ⁅0, x⁆ = 0 := by { rw [←lie_skew, lie_zero], simp, } @[simp] lemma neg_lie (x y : L) : ⁅-x, y⁆ = -⁅x, y⁆ := by { rw [←sub_eq_zero_iff_eq, sub_neg_eq_add, ←add_lie], simp, } @[simp] lemma lie_neg (x y : L) : ⁅x, -y⁆ = -⁅x, y⁆ := by { rw [←lie_skew, ←lie_skew], simp, } @[simp] lemma gsmul_lie (x y : L) (n : ℤ) : ⁅n • x, y⁆ = n • ⁅x, y⁆ := add_monoid_hom.map_gsmul ⟨λ x, ⁅x, y⁆, zero_lie y, λ _ _, add_lie _ _ _⟩ _ _ @[simp] lemma lie_gsmul (x y : L) (n : ℤ) : ⁅x, n • y⁆ = n • ⁅x, y⁆ := begin rw [←lie_skew, ←lie_skew x, gsmul_lie], unfold has_scalar.smul, rw gsmul_neg, end /-- An associative ring gives rise to a Lie ring by taking the bracket to be the ring commutator. -/ def lie_ring.of_associative_ring (A : Type v) [ring A] : lie_ring A := { bracket := ring_commutator.commutator, add_lie := ring_commutator.add_left, lie_add := ring_commutator.add_right, lie_self := ring_commutator.alternate, jacobi := ring_commutator.jacobi } local attribute [instance] lie_ring.of_associative_ring lemma lie_ring.of_associative_ring_bracket (A : Type v) [ring A] (x y : A) : ⁅x, y⁆ = x*y - y*x := rfl lemma commutative_ring_iff_abelian_lie_ring (A : Type v) [ring A] : is_commutative A (*) ↔ lie_algebra.is_abelian A := begin have h₁ : is_commutative A (*) ↔ ∀ (a b : A), a * b = b * a := ⟨λ h, h.1, λ h, ⟨h⟩⟩, have h₂ : lie_algebra.is_abelian A ↔ ∀ (a b : A), ⁅a, b⁆ = 0 := ⟨λ h, h.1, λ h, ⟨h⟩⟩, simp only [h₁, h₂, lie_ring.of_associative_ring_bracket, sub_eq_zero], end end lie_ring section prio set_option default_priority 100 -- see Note [default priority] /-- A Lie algebra is a module with compatible product, known as the bracket, satisfying the Jacobi identity. Forgetting the scalar multiplication, every Lie algebra is a Lie ring. -/ class lie_algebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] extends module R L := (lie_smul : ∀ (t : R) (x y : L), ⁅x, t • y⁆ = t • ⁅x, y⁆) end prio @[simp] lemma lie_smul (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (t : R) (x y : L) : ⁅x, t • y⁆ = t • ⁅x, y⁆ := lie_algebra.lie_smul t x y @[simp] lemma smul_lie (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] (t : R) (x y : L) : ⁅t • x, y⁆ = t • ⁅x, y⁆ := by { rw [←lie_skew, ←lie_skew x y], simp [-lie_skew], } namespace lie_algebra set_option old_structure_cmd true /-- A morphism of Lie algebras is a linear map respecting the bracket operations. -/ structure morphism (R : Type u) (L : Type v) (L' : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] extends linear_map R L L' := (map_lie : ∀ {x y : L}, to_fun ⁅x, y⁆ = ⁅to_fun x, to_fun y⁆) attribute [nolint doc_blame] lie_algebra.morphism.to_linear_map infixr ` →ₗ⁅⁆ `:25 := morphism _ notation L ` →ₗ⁅`:25 R:25 `⁆ `:0 L':0 := morphism R L L' section morphism_properties variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] instance : has_coe (L₁ →ₗ⁅R⁆ L₂) (L₁ →ₗ[R] L₂) := ⟨morphism.to_linear_map⟩ /-- see Note [function coercion] -/ instance : has_coe_to_fun (L₁ →ₗ⁅R⁆ L₂) := ⟨_, morphism.to_fun⟩ @[simp] lemma map_lie (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f ⁅x, y⁆ = ⁅f x, f y⁆ := morphism.map_lie f /-- The constant 0 map is a Lie algebra morphism. -/ instance : has_zero (L₁ →ₗ⁅R⁆ L₂) := ⟨{ map_lie := by simp, ..(0 : L₁ →ₗ[R] L₂)}⟩ /-- The identity map is a Lie algebra morphism. -/ instance : has_one (L₁ →ₗ⁅R⁆ L₁) := ⟨{ map_lie := by simp, ..(1 : L₁ →ₗ[R] L₁)}⟩ instance : inhabited (L₁ →ₗ⁅R⁆ L₂) := ⟨0⟩ /-- The composition of morphisms is a morphism. -/ def morphism.comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) : L₁ →ₗ⁅R⁆ L₃ := { map_lie := λ x y, by { change f (g ⁅x, y⁆) = ⁅f (g x), f (g y)⁆, rw [map_lie, map_lie], }, ..linear_map.comp f.to_linear_map g.to_linear_map } lemma morphism.comp_apply (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) (x : L₁) : f.comp g x = f (g x) := rfl /-- The inverse of a bijective morphism is a morphism. -/ def morphism.inverse (f : L₁ →ₗ⁅R⁆ L₂) (g : L₂ → L₁) (h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : L₂ →ₗ⁅R⁆ L₁ := { map_lie := λ x y, by { calc g ⁅x, y⁆ = g ⁅f (g x), f (g y)⁆ : by { conv_lhs { rw [←h₂ x, ←h₂ y], }, } ... = g (f ⁅g x, g y⁆) : by rw map_lie ... = ⁅g x, g y⁆ : (h₁ _), }, ..linear_map.inverse f.to_linear_map g h₁ h₂ } end morphism_properties /-- An equivalence of Lie algebras is a morphism which is also a linear equivalence. We could instead define an equivalence to be a morphism which is also a (plain) equivalence. However it is more convenient to define via linear equivalence to get `.to_linear_equiv` for free. -/ structure equiv (R : Type u) (L : Type v) (L' : Type w) [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] extends L →ₗ⁅R⁆ L', L ≃ₗ[R] L' attribute [nolint doc_blame] lie_algebra.equiv.to_morphism attribute [nolint doc_blame] lie_algebra.equiv.to_linear_equiv notation L ` ≃ₗ⁅`:50 R `⁆ ` L' := equiv R L L' namespace equiv variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁} variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃] variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃] instance : has_one (L₁ ≃ₗ⁅R⁆ L₁) := ⟨{ map_lie := λ x y, by { change ((1 : L₁→ₗ[R] L₁) ⁅x, y⁆) = ⁅(1 : L₁→ₗ[R] L₁) x, (1 : L₁→ₗ[R] L₁) y⁆, simp, }, ..(1 : L₁ ≃ₗ[R] L₁)}⟩ instance : inhabited (L₁ ≃ₗ⁅R⁆ L₁) := ⟨1⟩ /-- Lie algebra equivalences are reflexive. -/ @[refl] def refl : L₁ ≃ₗ⁅R⁆ L₁ := 1 /-- Lie algebra equivalences are symmetric. -/ @[symm] def symm (e : L₁ ≃ₗ⁅R⁆ L₂) : L₂ ≃ₗ⁅R⁆ L₁ := { ..morphism.inverse e.to_morphism e.inv_fun e.left_inv e.right_inv, ..e.to_linear_equiv.symm } /-- Lie algebra equivalences are transitive. -/ @[trans] def trans (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) : L₁ ≃ₗ⁅R⁆ L₃ := { ..morphism.comp e₂.to_morphism e₁.to_morphism, ..linear_equiv.trans e₁.to_linear_equiv e₂.to_linear_equiv } end equiv namespace direct_sum open dfinsupp variables {R : Type u} [comm_ring R] variables {ι : Type v} [decidable_eq ι] {L : ι → Type w} variables [Π i, lie_ring (L i)] [Π i, lie_algebra R (L i)] /-- The direct sum of Lie rings carries a natural Lie ring structure. -/ instance : lie_ring (direct_sum ι L) := { bracket := zip_with (λ i, λ x y, ⁅x, y⁆) (λ i, lie_zero 0), add_lie := λ x y z, by { ext, simp only [zip_with_apply, add_apply, add_lie], }, lie_add := λ x y z, by { ext, simp only [zip_with_apply, add_apply, lie_add], }, lie_self := λ x, by { ext, simp only [zip_with_apply, add_apply, lie_self, zero_apply], }, jacobi := λ x y z, by { ext, simp only [zip_with_apply, add_apply, lie_ring.jacobi, zero_apply], }, ..(infer_instance : add_comm_group _) } @[simp] lemma bracket_apply {x y : direct_sum ι L} {i : ι} : ⁅x, y⁆ i = ⁅x i, y i⁆ := zip_with_apply /-- The direct sum of Lie algebras carries a natural Lie algebra structure. -/ instance : lie_algebra R (direct_sum ι L) := { lie_smul := λ c x y, by { ext, simp only [zip_with_apply, smul_apply, bracket_apply, lie_smul], }, ..(infer_instance : module R _) } end direct_sum variables {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] /-- An associative algebra gives rise to a Lie algebra by taking the bracket to be the ring commutator. -/ def of_associative_algebra (A : Type v) [ring A] [algebra R A] : @lie_algebra R A _ (lie_ring.of_associative_ring _) := { lie_smul := λ t x y, by rw [lie_ring.of_associative_ring_bracket, lie_ring.of_associative_ring_bracket, algebra.mul_smul_comm, algebra.smul_mul_assoc, smul_sub], } instance (M : Type v) [add_comm_group M] [module R M] : lie_ring (module.End R M) := lie_ring.of_associative_ring _ local attribute [instance] lie_ring.of_associative_ring local attribute [instance] lie_algebra.of_associative_algebra /-- The map `of_associative_algebra` associating a Lie algebra to an associative algebra is functorial. -/ def of_associative_algebra_hom {R : Type u} {A : Type v} {B : Type w} [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] (f : A →ₐ[R] B) : A →ₗ⁅R⁆ B := { map_lie := λ x y, show f ⁅x,y⁆ = ⁅f x,f y⁆, by simp only [lie_ring.of_associative_ring_bracket, alg_hom.map_sub, alg_hom.map_mul], ..f.to_linear_map, } @[simp] lemma of_associative_algebra_hom_id {R : Type u} {A : Type v} [comm_ring R] [ring A] [algebra R A] : of_associative_algebra_hom (alg_hom.id R A) = 1 := rfl @[simp] lemma of_associative_algebra_hom_comp {R : Type u} {A : Type v} {B : Type w} {C : Type w₁} [comm_ring R] [ring A] [ring B] [ring C] [algebra R A] [algebra R B] [algebra R C] (f : A →ₐ[R] B) (g : B →ₐ[R] C) : of_associative_algebra_hom (g.comp f) = (of_associative_algebra_hom g).comp (of_associative_algebra_hom f) := rfl /-- An important class of Lie algebras are those arising from the associative algebra structure on module endomorphisms. -/ instance of_endomorphism_algebra (M : Type v) [add_comm_group M] [module R M] : lie_algebra R (module.End R M) := of_associative_algebra (module.End R M) lemma endo_algebra_bracket (M : Type v) [add_comm_group M] [module R M] (f g : module.End R M) : ⁅f, g⁆ = f.comp g - g.comp f := rfl /-- The adjoint action of a Lie algebra on itself. -/ def Ad : L →ₗ⁅R⁆ module.End R L := { to_fun := λ x, { to_fun := has_bracket.bracket x, add := by { intros, apply lie_add, }, smul := by { intros, apply lie_smul, } }, add := by { intros, ext, simp, }, smul := by { intros, ext, simp, }, map_lie := by { intros x y, ext z, rw endo_algebra_bracket, suffices : ⁅⁅x, y⁆, z⁆ = ⁅x, ⁅y, z⁆⁆ + ⁅⁅x, z⁆, y⁆, by simpa [sub_eq_add_neg], rw [eq_comm, ←lie_skew ⁅x, y⁆ z, ←lie_skew ⁅x, z⁆ y, ←lie_skew x z, lie_neg, neg_neg, ←sub_eq_zero_iff_eq, sub_neg_eq_add, lie_ring.jacobi], } } end lie_algebra section lie_subalgebra variables (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] /-- A Lie subalgebra of a Lie algebra is submodule that is closed under the Lie bracket. This is a sufficient condition for the subset itself to form a Lie algebra. -/ structure lie_subalgebra extends submodule R L := (lie_mem : ∀ {x y}, x ∈ carrier → y ∈ carrier → ⁅x, y⁆ ∈ carrier) /-- The zero algebra is a subalgebra of any Lie algebra. -/ instance : has_zero (lie_subalgebra R L) := ⟨{ lie_mem := λ x y hx hy, by { rw [((submodule.mem_bot R).1 hx), zero_lie], exact submodule.zero_mem (0 : submodule R L), }, ..(0 : submodule R L) }⟩ instance : inhabited (lie_subalgebra R L) := ⟨0⟩ instance lie_subalgebra_coe_submodule : has_coe (lie_subalgebra R L) (submodule R L) := ⟨lie_subalgebra.to_submodule⟩ /-- A Lie subalgebra forms a new Lie ring. -/ instance lie_subalgebra_lie_ring (L' : lie_subalgebra R L) : lie_ring L' := { bracket := λ x y, ⟨⁅x.val, y.val⁆, L'.lie_mem x.property y.property⟩, lie_add := by { intros, apply set_coe.ext, apply lie_add, }, add_lie := by { intros, apply set_coe.ext, apply add_lie, }, lie_self := by { intros, apply set_coe.ext, apply lie_self, }, jacobi := by { intros, apply set_coe.ext, apply lie_ring.jacobi, } } /-- A Lie subalgebra forms a new Lie algebra. -/ instance lie_subalgebra_lie_algebra (L' : lie_subalgebra R L) : @lie_algebra R L' _ (lie_subalgebra_lie_ring _ _ _) := { lie_smul := by { intros, apply set_coe.ext, apply lie_smul } } local attribute [instance] lie_ring.of_associative_ring local attribute [instance] lie_algebra.of_associative_algebra /-- A subalgebra of an associative algebra is a Lie subalgebra of the associated Lie algebra. -/ def lie_subalgebra_of_subalgebra (A : Type v) [ring A] [algebra R A] (A' : subalgebra R A) : lie_subalgebra R A := { lie_mem := λ x y hx hy, by { change ⁅x, y⁆ ∈ A', change x ∈ A' at hx, change y ∈ A' at hy, rw lie_ring.of_associative_ring_bracket, have hxy := subalgebra.mul_mem A' x y hx hy, have hyx := subalgebra.mul_mem A' y x hy hx, exact submodule.sub_mem A'.to_submodule hxy hyx, }, ..A'.to_submodule } end lie_subalgebra section lie_module variables (R : Type u) (L : Type v) [comm_ring R] [lie_ring L] [lie_algebra R L] variables (M : Type v) [add_comm_group M] [module R M] section prio set_option default_priority 100 -- see Note [default priority] /-- A Lie module is a module over a commutative ring, together with a linear action of a Lie algebra on this module, such that the Lie bracket acts as the commutator of endomorphisms. -/ class lie_module extends linear_action R L M := (lie_act : ∀ (l l' : L) (m : M), act ⁅l, l'⁆ m = act l (act l' m) - act l' (act l m)) end prio @[simp] lemma lie_act [lie_module R L M] (l l' : L) (m : M) : linear_action.act R ⁅l, l'⁆ m = linear_action.act R l (linear_action.act R l' m) - linear_action.act R l' (linear_action.act R l m) := lie_module.lie_act l l' m protected lemma of_endo_map_action (α : L →ₗ⁅R⁆ module.End R M) (x : L) (m : M) : @linear_action.act R _ _ _ _ _ _ _ (linear_action.of_endo_map R L M α) x m = α x m := rfl /-- A Lie morphism from a Lie algebra to the endomorphism algebra of a module yields a Lie module structure. -/ def lie_module.of_endo_morphism (α : L →ₗ⁅R⁆ module.End R M) : lie_module R L M := { lie_act := by { intros x y m, rw [of_endo_map_action, lie_algebra.map_lie, lie_algebra.endo_algebra_bracket], refl, }, ..(linear_action.of_endo_map R L M α) } /-- Every Lie algebra is a module over itself. -/ instance lie_algebra_self_module : lie_module R L L := lie_module.of_endo_morphism R L L lie_algebra.Ad /-- A Lie submodule of a Lie module is a submodule that is closed under the Lie bracket. This is a sufficient condition for the subset itself to form a Lie module. -/ structure lie_submodule [lie_module R L M] extends submodule R M := (lie_mem : ∀ {x : L} {m : M}, m ∈ carrier → linear_action.act R x m ∈ carrier) /-- The zero module is a Lie submodule of any Lie module. -/ instance [lie_module R L M] : has_zero (lie_submodule R L M) := ⟨{ lie_mem := λ x m h, by { rw [((submodule.mem_bot R).1 h), linear_action_zero], exact submodule.zero_mem (0 : submodule R M), }, ..(0 : submodule R M)}⟩ instance [lie_module R L M] : inhabited (lie_submodule R L M) := ⟨0⟩ instance lie_submodule_coe_submodule [lie_module R L M] : has_coe (lie_submodule R L M) (submodule R M) := ⟨lie_submodule.to_submodule⟩ instance lie_submodule_has_mem [lie_module R L M] : has_mem M (lie_submodule R L M) := ⟨λ x N, x ∈ (N : set M)⟩ instance lie_submodule_lie_module [lie_module R L M] (N : lie_submodule R L M) : lie_module R L N := { act := λ x m, ⟨linear_action.act R x m.val, N.lie_mem m.property⟩, add_act := by { intros x y m, apply set_coe.ext, apply linear_action.add_act, }, act_add := by { intros x m n, apply set_coe.ext, apply linear_action.act_add, }, act_smul := by { intros r x y, apply set_coe.ext, apply linear_action.act_smul, }, smul_act := by { intros r x y, apply set_coe.ext, apply linear_action.smul_act, }, lie_act := by { intros x y m, apply set_coe.ext, apply lie_module.lie_act, } } /-- An ideal of a Lie algebra is a Lie submodule of the Lie algebra as a Lie module over itself. -/ abbreviation lie_ideal := lie_submodule R L L lemma lie_mem_right (I : lie_ideal R L) (x y : L) (h : y ∈ I) : ⁅x, y⁆ ∈ I := I.lie_mem h lemma lie_mem_left (I : lie_ideal R L) (x y : L) (h : x ∈ I) : ⁅x, y⁆ ∈ I := by { rw [←lie_skew, ←neg_lie], apply lie_mem_right, assumption, } /-- An ideal of a Lie algebra is a Lie subalgebra. -/ def lie_ideal_subalgebra (I : lie_ideal R L) : lie_subalgebra R L := { lie_mem := by { intros x y hx hy, apply lie_mem_right, exact hy, }, ..I.to_submodule, } /-- A Lie module is irreducible if its only non-trivial Lie submodule is itself. -/ class lie_module.is_irreducible [lie_module R L M] : Prop := (irreducible : ∀ (M' : lie_submodule R L M), (∃ (m : M'), m ≠ 0) → (∀ (m : M), m ∈ M')) /-- A Lie algebra is simple if it is irreducible as a Lie module over itself via the adjoint action, and it is non-Abelian. -/ class lie_algebra.is_simple : Prop := (simple : lie_module.is_irreducible R L L ∧ ¬lie_algebra.is_abelian L) end lie_module namespace lie_submodule variables {R : Type u} {L : Type v} [comm_ring R] [lie_ring L] [lie_algebra R L] variables {M : Type v} [add_comm_group M] [module R M] [α : lie_module R L M] variables (N : lie_submodule R L M) (I : lie_ideal R L) /-- The quotient of a Lie module by a Lie submodule. It is a Lie module. -/ abbreviation quotient := N.to_submodule.quotient namespace quotient variables {N I} /-- Map sending an element of `M` to the corresponding element of `M/N`, when `N` is a lie_submodule of the lie_module `N`. -/ abbreviation mk : M → N.quotient := submodule.quotient.mk lemma is_quotient_mk (m : M) : quotient.mk' m = (mk m : N.quotient) := rfl /-- Given a Lie module `M` over a Lie algebra `L`, together with a Lie submodule `N ⊆ M`, there is a natural linear map from `L` to the endomorphisms of `M` leaving `N` invariant. -/ def lie_submodule_invariant : L →ₗ[R] submodule.compatible_maps N.to_submodule N.to_submodule := linear_map.cod_restrict _ (α.to_linear_action.to_endo_map _ _ _) N.lie_mem instance lie_quotient_action : linear_action R L N.quotient := linear_action.of_endo_map _ _ _ (linear_map.comp (submodule.mapq_linear N N) lie_submodule_invariant) lemma lie_quotient_action_apply (z : L) (m : M) : linear_action.act R z (mk m : N.quotient) = mk (linear_action.act R z m) := rfl /-- The quotient of a Lie module by a Lie submodule, is a Lie module. -/ instance lie_quotient_lie_module : lie_module R L N.quotient := { lie_act := λ x y m', by { apply quotient.induction_on' m', intros m, rw is_quotient_mk, repeat { rw lie_quotient_action_apply, }, rw lie_act, refl, }, ..quotient.lie_quotient_action, } instance lie_quotient_has_bracket : has_bracket (quotient I) := ⟨by { intros x y, apply quotient.lift_on₂' x y (λ x' y', mk ⁅x', y'⁆), intros x₁ x₂ y₁ y₂ h₁ h₂, apply (submodule.quotient.eq I.to_submodule).2, have h : ⁅x₁, x₂⁆ - ⁅y₁, y₂⁆ = ⁅x₁, x₂ - y₂⁆ + ⁅x₁ - y₁, y₂⁆ := by simp [-lie_skew, sub_eq_add_neg, add_assoc], rw h, apply submodule.add_mem, { apply lie_mem_right R L I x₁ (x₂ - y₂) h₂, }, { apply lie_mem_left R L I (x₁ - y₁) y₂ h₁, }, }⟩ @[simp] lemma mk_bracket (x y : L) : (mk ⁅x, y⁆ : quotient I) = ⁅mk x, mk y⁆ := rfl instance lie_quotient_lie_ring : lie_ring (quotient I) := { add_lie := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply add_lie, }, lie_add := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply lie_add, }, lie_self := by { intros x', apply quotient.induction_on' x', intros x, rw [is_quotient_mk, ←mk_bracket], apply congr_arg, apply lie_self, }, jacobi := by { intros x' y' z', apply quotient.induction_on₃' x' y' z', intros x y z, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_add, }, apply congr_arg, apply lie_ring.jacobi, } } instance lie_quotient_lie_algebra : lie_algebra R (quotient I) := { lie_smul := by { intros t x' y', apply quotient.induction_on₂' x' y', intros x y, repeat { rw is_quotient_mk <|> rw ←mk_bracket <|> rw ←submodule.quotient.mk_smul, }, apply congr_arg, apply lie_smul, } } end quotient end lie_submodule /-- An important class of Lie rings are those arising from the associative algebra structure on square matrices over a commutative ring. -/ def matrix.lie_ring (n : Type u) (R : Type v) [fintype n] [decidable_eq n] [comm_ring R] : lie_ring (matrix n n R) := lie_ring.of_associative_ring (matrix n n R) local attribute [instance] matrix.lie_ring /-- An important class of Lie algebras are those arising from the associative algebra structure on square matrices over a commutative ring. -/ def matrix.lie_algebra (n : Type u) (R : Type v) [fintype n] [decidable_eq n] [comm_ring R] : lie_algebra R (matrix n n R) := lie_algebra.of_associative_algebra (matrix n n R)
e1fd4e98ed229fc0408d0fea2302921c85645f6e
798dd332c1ad790518589a09bc82459fb12e5156
/data/set/basic.lean
9517e939785fd008273dd32a2d7be19420c43013
[ "Apache-2.0" ]
permissive
tobiasgrosser/mathlib
b040b7eb42d5942206149371cf92c61404de3c31
120635628368ec261e031cefc6d30e0304088b03
refs/heads/master
1,644,803,442,937
1,536,663,752,000
1,536,663,907,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
41,281
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Leonardo de Moura -/ import tactic.ext tactic.finish data.subtype tactic.interactive open function /- set coercion to a type -/ namespace set instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩ end set section set_coe universe u variables {α : Type u} @[simp] 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 @[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 lemma subtype.mem {α : Type*} {s : set α} (p : s) : (p : α) ∈ s := p.property namespace set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a : α} {s t : set α} instance : inhabited (set α) := ⟨∅⟩ @[extensionality] 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 := ⟨begin intros h x, rw h end, set.ext⟩ @[trans] theorem mem_of_mem_of_subset {α : Type u} {x : α} {s t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx /- mem and set_of -/ @[simp] theorem mem_set_of_eq {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl @[simp] 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 mem_def {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl instance decidable_mem (s : set α) [H : decidable_pred s] : ∀ a, decidable (a ∈ s) := H 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 /- subset -/ -- 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 @[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 {α : Type u} {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 := ext (λ x, iff.intro (λ ina, h₁ ina) (λ inb, h₂ inb)) theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := ⟨λ e, e ▸ ⟨subset.refl _, subset.refl _⟩, λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩ -- an alterantive name theorem eq_of_subset_of_subset {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := subset.antisymm h₁ h₂ theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := assume h₁ h₂, h₁ h₂ theorem not_subset : (¬ s ⊆ t) ↔ ∃a, a ∈ s ∧ a ∉ t := by simp [subset_def, classical.not_forall] /- strict subset -/ /-- `s ⊂ t` means that `s` is a strict subset of `t`, that is, `s ⊆ t` but `s ≠ t`. -/ def strict_subset (s t : set α) := s ⊆ t ∧ s ≠ t instance : has_ssubset (set α) := ⟨strict_subset⟩ theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ s ≠ t) := rfl lemma exists_of_ssubset {α : Type u} {s t : set α} (h : s ⊂ t) : (∃x∈t, x ∉ s) := classical.by_contradiction $ assume hn, have t ⊆ s, from assume a hat, classical.by_contradiction $ assume has, hn ⟨a, hat, has⟩, h.2 $ subset.antisymm h.1 this lemma ssubset_iff_subset_not_subset {s t : set α} : s ⊂ t ↔ s ⊆ t ∧ ¬ t ⊆ s := by split; simp [set.ssubset_def, ne.def, set.subset.antisymm_iff] {contextual := tt} theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) := assume h : x ∈ ∅, h @[simp] theorem not_not_mem [decidable (a ∈ s)] : ¬ (a ∉ s) ↔ a ∈ s := not_not /- 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 theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s := by simp [ext_iff] theorem ne_empty_of_mem {s : set α} {x : α} (h : x ∈ s) : s ≠ ∅ := by { intro hs, rw hs at h, apply not_mem_empty _ h } @[simp] theorem empty_subset (s : set α) : ∅ ⊆ s := assume x, assume h, false.elim h theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ := by simp [subset.antisymm_iff] theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ := subset_empty_iff.1 theorem ne_empty_iff_exists_mem {s : set α} : s ≠ ∅ ↔ ∃ x, x ∈ s := by haveI := classical.prop_decidable; simp [eq_empty_iff_forall_not_mem] theorem exists_mem_of_ne_empty {s : set α} : s ≠ ∅ → ∃ x, x ∈ s := ne_empty_iff_exists_mem.1 -- TODO: remove when simplifier stops rewriting `a ≠ b` to `¬ a = b` theorem not_eq_empty_iff_exists {s : set α} : ¬ (s = ∅) ↔ ∃ x, x ∈ s := ne_empty_iff_exists_mem theorem subset_eq_empty {s t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ := subset_empty_iff.1 $ e ▸ h theorem subset_ne_empty {s t : set α} (h : t ⊆ s) : t ≠ ∅ → s ≠ ∅ := mt (subset_eq_empty h) theorem ball_empty_iff {p : α → Prop} : (∀ x ∈ (∅ : set α), p x) ↔ true := by simp [iff_def] /- universal set -/ theorem univ_def : @univ α = {x | true} := rfl @[simp] theorem mem_univ (x : α) : x ∈ @univ α := trivial theorem empty_ne_univ [h : inhabited α] : (∅ : set α) ≠ univ := by simp [ext_iff] @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ := by simp [subset.antisymm_iff] 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 := by simp [ext_iff] theorem eq_univ_of_forall {s : set α} : (∀ x, x ∈ s) → s = univ := eq_univ_iff_forall.2 lemma nonempty_iff_univ_ne_empty {α : Type*} : nonempty α ↔ (univ : set α) ≠ ∅ := begin split, { rintro ⟨a⟩ H2, show a ∈ (∅ : set α), by rw ←H2 ; trivial }, { intro H, cases exists_mem_of_ne_empty H with a _, exact ⟨a⟩ } end /- 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 (assume x, or_self _) @[simp] theorem union_empty (a : set α) : a ∪ ∅ = a := ext (assume x, or_false _) @[simp] theorem empty_union (a : set α) : ∅ ∪ a = a := ext (assume x, false_or _) theorem union_comm (a b : set α) : a ∪ b = b ∪ a := ext (assume x, or.comm) theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) := ext (assume 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₃) := by finish theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by finish theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := by finish [subset_def, ext_iff, iff_def] @[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 := by finish [subset_def, union_def] @[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := by finish [iff_def, subset_def] theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := by finish [subset_def] theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h (by refl) theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union (by refl) h @[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ := ⟨by finish [ext_iff], by finish [ext_iff]⟩ /- 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 (assume x, and_self _) @[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ := ext (assume x, and_false _) @[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ := ext (assume x, false_and _) theorem inter_comm (a b : set α) : a ∩ b = b ∩ a := ext (assume x, and.comm) theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext (assume 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₃) := by finish theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := by finish @[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H @[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := by finish [subset_def, inter_def] @[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := ⟨λ h, ⟨subset.trans h (inter_subset_left _ _), subset.trans h (inter_subset_right _ _)⟩, λ ⟨h₁, h₂⟩, subset_inter h₁ h₂⟩ @[simp] theorem inter_univ (a : set α) : a ∩ univ = a := ext (assume x, and_true _) @[simp] theorem univ_inter (a : set α) : univ ∩ a = a := ext (assume x, true_and _) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := by finish [subset_def] theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := by finish [subset_def] theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := by finish [subset_def] theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s := by finish [subset_def, ext_iff, iff_def] theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_inter_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) ∩ s = s := by finish [ext_iff, iff_def] theorem union_inter_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) ∩ t = t := by finish [ext_iff, iff_def] -- TODO(Mario): remove? theorem nonempty_of_inter_nonempty_right {s t : set α} (h : s ∩ t ≠ ∅) : t ≠ ∅ := by finish [ext_iff, iff_def] theorem nonempty_of_inter_nonempty_left {s t : set α} (h : s ∩ t ≠ ∅) : s ≠ ∅ := by finish [ext_iff, iff_def] /- distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (assume x, and_or_distrib_left) theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (assume x, or_and_distrib_right) theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (assume x, or_and_distrib_left) theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (assume x, and_or_distrib_right) /- insert -/ theorem insert_def (x : α) (s : set α) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem insert_of_has_insert (x : α) (s : set α) : has_insert.insert x s = insert x s := rfl @[simp] theorem subset_insert (x : α) (s : set α) : s ⊆ insert x s := assume y ys, or.inr ys 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 α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := by finish [insert_def] @[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 := by finish [ext_iff, iff_def] theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) := by simp [subset_def, or_imp_distrib, forall_and_distrib] theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t := assume a', or.imp_right (@h a') theorem ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s := by finish [ssubset_def, ext_iff] theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := ext $ by simp [or.left_comm] theorem insert_union : insert a s ∪ t = insert a (s ∪ t) := set.ext $ assume a, by simp [or.comm, or.left_comm] @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := set.ext $ assume a, by simp [or.comm, or.left_comm] -- TODO(Jeremy): make this automatic theorem insert_ne_empty (a : α) (s : set α) : insert a s ≠ ∅ := by safe [ext_iff, iff_def]; have h' := a_1 a; finish -- useful in proofs by induction theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ insert a s → P x) : ∀ x, x ∈ s → P x := by finish theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) : ∀ x, x ∈ insert a s → P x := by finish theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := by finish [iff_def] /- singletons -/ theorem singleton_def (a : α) : ({a} : set α) = insert a ∅ := rfl @[simp] theorem mem_singleton_iff {a b : α} : a ∈ ({b} : set α) ↔ a = b := by finish [singleton_def] -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := by finish theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := by finish @[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y := by finish [ext_iff, iff_def] theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := by finish theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := by finish [ext_iff, or_comm] @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := by finish @[simp] theorem singleton_ne_empty (a : α) : ({a} : set α) ≠ ∅ := insert_ne_empty _ _ @[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s := ⟨λh, h (by simp), λh b e, by simp at e; simp [*]⟩ theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} := set.ext $ by simp @[simp] theorem union_singleton : s ∪ {a} = insert a s := by simp [singleton_def] @[simp] theorem singleton_union : {a} ∪ s = insert a s := by rw [union_comm, union_singleton] theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s := by simp [eq_empty_iff_forall_not_mem] theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s := by rw [inter_comm, singleton_inter_eq_empty] /- separation -/ theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[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 α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := by finish [ext_iff, iff_def, subset_def] theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := assume x, and.left theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) : ∀ x ∈ s, ¬ p x := by finish [ext_iff] /- complement -/ theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ -s := h 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 = ∅ := by finish [ext_iff] @[simp] theorem compl_inter_self (s : set α) : -s ∩ s = ∅ := by finish [ext_iff] @[simp] theorem compl_empty : -(∅ : set α) = univ := by finish [ext_iff] @[simp] theorem compl_union (s t : set α) : -(s ∪ t) = -s ∩ -t := by finish [ext_iff] @[simp] theorem compl_compl (s : set α) : -(-s) = s := by finish [ext_iff] -- ditto theorem compl_inter (s t : set α) : -(s ∩ t) = -s ∪ -t := by finish [ext_iff] @[simp] theorem compl_univ : -(univ : set α) = ∅ := by finish [ext_iff] theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = -(-s ∩ -t) := by simp [compl_inter, compl_compl] theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = -(-s ∪ -t) := by simp [compl_compl] @[simp] theorem union_compl_self (s : set α) : s ∪ -s = univ := by finish [ext_iff] @[simp] theorem compl_union_self (s : set α) : -s ∪ s = univ := by finish [ext_iff] theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext compl_compl theorem compl_subset_comm {s t : set α} : -s ⊆ t ↔ -t ⊆ s := by haveI := classical.prop_decidable; exact forall_congr (λ a, not_imp_comm) lemma compl_subset_compl {s t : set α} : -s ⊆ -t ↔ t ⊆ s := by rw [compl_subset_comm, compl_compl] theorem compl_subset_iff_union {s t : set α} : -s ⊆ t ↔ s ∪ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a, by haveI := classical.prop_decidable; exact 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 /- 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 union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := by finish [ext_iff, iff_def, subset_def] theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s := by finish [ext_iff, iff_def] theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t := by finish [ext_iff, iff_def] theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := inter_distrib_right _ _ _ theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) := inter_assoc _ _ _ theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ := by finish [ext_iff] theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s := by finish [ext_iff, iff_def] theorem diff_subset (s t : set α) : s \ t ⊆ s := by finish [subset_def] theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := by finish [subset_def] theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := diff_subset_diff h (by refl) theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := diff_subset_diff (subset.refl s) h theorem compl_eq_univ_diff (s : set α) : -s = univ \ s := by finish [ext_iff] theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t := ⟨assume h x hx, classical.by_contradiction $ assume : x ∉ t, show x ∈ (∅ : set α), from h ▸ ⟨hx, this⟩, assume h, eq_empty_of_subset_empty $ assume x ⟨hx, hnx⟩, hnx $ h hx⟩ @[simp] theorem diff_empty {s : set α} : s \ ∅ = s := set.ext $ assume x, ⟨assume ⟨hx, _⟩, hx, assume h, ⟨h, not_false⟩⟩ theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) := set.ext $ by simp [not_or_distrib, and.comm, and.left_comm] lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := ⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)), assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩ lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t := by rw [diff_subset_iff, diff_subset_iff, union_comm] @[simp] theorem insert_diff (h : a ∈ t) : insert a s \ t = s \ t := set.ext $ by intro; constructor; simp [or_imp_distrib, h] {contextual := tt} theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t := by finish [ext_iff, iff_def] theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_diff_self, union_comm] theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ := set.ext $ by simp [iff_def] {contextual:=tt} theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := by finish [ext_iff, iff_def, subset_def] @[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] /- 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 theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl /- 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_eq {s : set β} {a : α} : (a ∈ f ⁻¹' s) = (f a ∈ s) := rfl 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 @[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_set_of_eq {p : α → Prop} {f : β → α} : f ⁻¹' {a | p a} = {a | p (f a)} := rfl theorem preimage_id {s : set α} : id ⁻¹' s = s := rfl theorem preimage_comp {s : set γ} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl 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, set.ext $ assume ⟨x, hx⟩, by simp [h]⟩ end preimage /- function image -/ section image infix ` '' `:80 := image /-- Two functions `f₁ f₂ : α → β` are equal on `s` if `f₁ x = f₂ x` for all `x ∈ a`. -/ @[reducible] def eq_on (f1 f2 : α → β) (a : set α) : Prop := ∀ x ∈ a, f1 x = f2 x -- TODO(Jeremy): use bounded exists in 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 theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a := ⟨_, h, rfl⟩ theorem mem_image_of_injective {f : α → β} {a : α} {s : set α} (hf : injective f) : f a ∈ f '' s ↔ a ∈ s := iff.intro (assume ⟨b, hb, eq⟩, (hf eq) ▸ hb) (assume h, mem_image_of_mem _ h) theorem ball_image_of_ball {f : α → β} {s : set α} {p : β → Prop} (h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f '' s, p y := by finish [mem_image_eq] @[simp] theorem ball_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∀ y ∈ f '' s, p y) ↔ (∀ x ∈ s, p (f x)) := iff.intro (assume h a ha, h _ $ mem_image_of_mem _ ha) (assume h b ⟨a, ha, eq⟩, eq ▸ h a ha) theorem mono_image {f : α → β} {s t : set α} (h : s ⊆ t) : f '' s ⊆ f '' t := assume x ⟨y, hy, y_eq⟩, y_eq ▸ mem_image_of_mem _ $ h hy 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] theorem image_eq_image_of_eq_on {f₁ f₂ : α → β} {s : set α} (heq : eq_on f₁ f₂ s) : f₁ '' s = f₂ '' s := image_congr heq 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) /- Proof is removed as it uses generated names TODO(Jeremy): make automatic, begin safe [ext_iff, iff_def, mem_image, (∘)], have h' := h_2 (g a_2), finish end -/ 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 := by finish [ext_iff, iff_def, mem_image_eq] @[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := ext $ by simp 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₁⟩) (subset_inter (mono_image $ inter_subset_left _ _) (mono_image $ inter_subset_right _ _)) 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 simp [image]; exact H @[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} := set.ext $ λ x, by simp [image]; rw eq_comm lemma inter_singleton_ne_empty {α : Type*} {s : set α} {a : α} : s ∩ {a} ≠ ∅ ↔ a ∈ s := by finish [set.inter_singleton_eq_empty] theorem fix_set_compl (t : set α) : compl t = - t := rfl -- 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 [fix_set_compl, this]}, intro x, split; { intro e, subst e, simp } end @[simp] theorem image_id (s : set α) : id '' s = s := ext $ 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) := ext $ by simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] 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) /- image and preimage are a Galois connection -/ 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 = preimage f t ↔ s = t := iff.intro (assume eq, by rw [← @image_preimage_eq β α f s hf, ← @image_preimage_eq β α f t hf, eq]) (assume eq, eq ▸ rfl) lemma surjective_preimage {f : β → α} (hf : surjective f) : injective (preimage f) := assume s t, (preimage_eq_preimage hf).1 theorem compl_image : image (@compl α) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {α : Type u} {p : set α → Prop} : compl '' {x | p x} = {x | p (- x)} := 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 subtype_val_image {p : α → Prop} {s : set (subtype p)} : subtype.val '' 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⟩⟩ 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 injective_image {f : α → β} (hf : injective f) : injective (('') f) := assume s t, (image_eq_image hf).1 end image theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) 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 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)) := ⟨assume h i, h (f i) (mem_range_self _), assume h a ⟨i, (hi : f i = a)⟩, hi ▸ h i⟩ theorem range_iff_surjective : range f = univ ↔ surjective f := eq_univ_iff_forall @[simp] theorem range_id : range (@id α) = univ := range_iff_surjective.2 surjective_id @[simp] theorem image_univ {ι : Type*} {f : ι → β} : f '' univ = range f := set.ext $ by simp [image, range] theorem range_comp {g : α → β} : 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 {ι : Type*} {f : ι → β} {s : set β} : range f ⊆ s ↔ ∀ y, f y ∈ s := forall_range_iff lemma nonempty_of_nonempty_range {α : Type*} {β : Type*} {f : α → β} (H : ¬range f = ∅) : nonempty α := begin cases exists_mem_of_ne_empty H with x h, cases mem_range.1 h with y _, exact ⟨y⟩ end theorem image_preimage_eq_inter_range {f : α → β} {t : set β} : f '' preimage f t = t ∩ range f := set.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 ∈ preimage f t, by simp [preimage, h_eq, hx]⟩ @[simp] theorem quot_mk_range_eq [setoid α] : range (λx : α, ⟦x⟧) = univ := range_iff_surjective.2 quot.exists_rep end range lemma subtype_val_range {p : α → Prop} : range (@subtype.val _ p) = {x | p x} := by rw ← image_univ; simp [-image_univ, subtype_val_image] /-- 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 end set namespace set 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} theorem mem_prod_eq {p : α × β} : p ∈ set.prod s t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] theorem mem_prod {p : α × β} : p ∈ set.prod s t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl lemma mk_mem_prod {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ set.prod s t := ⟨a_in, b_in⟩ @[simp] theorem prod_empty {s : set α} : set.prod s ∅ = (∅ : set (α × β)) := set.ext $ by simp [set.prod] @[simp] theorem empty_prod {t : set β} : set.prod ∅ t = (∅ : set (α × β)) := set.ext $ by simp [set.prod] theorem insert_prod {a : α} {s : set α} {t : set β} : set.prod (insert a s) t = (prod.mk a '' t) ∪ set.prod s t := set.ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end theorem prod_insert {b : β} {s : set α} {t : set β} : set.prod s (insert b t) = ((λa, (a, b)) '' s) ∪ set.prod s t := set.ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end theorem prod_preimage_eq {f : γ → α} {g : δ → β} : set.prod (preimage f s) (preimage g t) = preimage (λp, (f p.1, g p.2)) (set.prod s t) := rfl theorem prod_mono {s₁ s₂ : set α} {t₁ t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : set.prod s₁ t₁ ⊆ set.prod s₂ t₂ := assume x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩ theorem prod_inter_prod : set.prod s₁ t₁ ∩ set.prod s₂ t₂ = set.prod (s₁ ∩ s₂) (t₁ ∩ t₂) := subset.antisymm (assume ⟨a, b⟩ ⟨⟨ha₁, hb₁⟩, ⟨ha₂, hb₂⟩⟩, ⟨⟨ha₁, ha₂⟩, ⟨hb₁, hb₂⟩⟩) (subset_inter (prod_mono (inter_subset_left _ _) (inter_subset_left _ _)) (prod_mono (inter_subset_right _ _) (inter_subset_right _ _))) theorem image_swap_prod : (λp:β×α, (p.2, p.1)) '' set.prod t s = set.prod s t := set.ext $ assume ⟨a, b⟩, by simp [mem_image_eq, set.prod, and_comm]; exact ⟨ assume ⟨b', a', ⟨h_a, h_b⟩, h⟩, by subst a'; subst b'; assumption, assume h, ⟨b, a, ⟨rfl, rfl⟩, h⟩⟩ 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 prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} : set.prod (image m₁ s) (image m₂ t) = image (λp:α×β, (m₁ p.1, m₂ p.2)) (set.prod s t) := set.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₂ : β → δ} : set.prod (range m₁) (range m₂) = range (λp:α×β, (m₁ p.1, m₂ p.2)) := set.ext $ by simp [range] @[simp] theorem prod_singleton_singleton {a : α} {b : β} : set.prod {a} {b} = ({(a, b)} : set (α×β)) := set.ext $ by simp [set.prod] theorem prod_neq_empty_iff {s : set α} {t : set β} : set.prod s t ≠ ∅ ↔ (s ≠ ∅ ∧ t ≠ ∅) := by simp [not_eq_empty_iff_exists] @[simp] theorem prod_mk_mem_set_prod_eq {a : α} {b : β} {s : set α} {t : set β} : (a, b) ∈ set.prod s t = (a ∈ s ∧ b ∈ t) := rfl @[simp] theorem univ_prod_univ : set.prod (@univ α) (@univ β) = univ := set.ext $ assume ⟨a, b⟩, by simp lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} : set.prod s t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W := by simp [subset_def] end prod end set
8026a7d69152a2c632079929dec16e2f02d601ea
9d2e3d5a2e2342a283affd97eead310c3b528a24
/src/exercises_sources/thursday/afternoon/category_theory/exercise1.lean
9e2ea1e4b54557940bdb7193172e02f05b15490f
[]
permissive
Vtec234/lftcm2020
ad2610ab614beefe44acc5622bb4a7fff9a5ea46
bbbd4c8162f8c2ef602300ab8fdeca231886375d
refs/heads/master
1,668,808,098,623
1,594,989,081,000
1,594,990,079,000
280,423,039
0
0
MIT
1,594,990,209,000
1,594,990,209,000
null
UTF-8
Lean
false
false
741
lean
import category_theory.isomorphism import category_theory.yoneda open category_theory open opposite variables {C : Type*} [category C] /-! Hint 1: `yoneda` is set up so that `(yoneda.obj X).obj (op Y) = (Y ⟶ X)` (we need to write `op Y` to explicitly move `Y` to the opposite category). -/ /-! Hint 2: If you have a natural isomorphism `α : F ≅ G`, you can access * the forward natural transformation as `α.hom` * the backwards natural transformation as `α.inv` * the component at `X`, as an isomorphism `F.obj X ≅ G.obj X` as `α.app X`. -/ def iso_of_hom_iso (X Y : C) (h : yoneda.obj X ≅ yoneda.obj Y) : X ≅ Y := sorry /-! There are some further hints in `src/hints/thursday/afternoon/category_theory/exercise1/` -/
738781ea5d48318efd27d827a9182e4665611f45
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/polynomial/big_operators.lean
8c2b41efd310a2c7253eb6caf50e402bcfdfa9af
[]
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
4,095
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jalex Stark. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.polynomial.monic import Mathlib.tactic.linarith.default import Mathlib.PostPort universes u w namespace Mathlib /-! # Polynomials Lemmas for the interaction between polynomials and ∑ and ∏. ## Main results - `nat_degree_prod_of_monic` : the degree of a product of monic polynomials is the product of degrees. We prove this only for [comm_semiring R], but it ought to be true for [semiring R] and list.prod. - `nat_degree_prod` : for polynomials over an integral domain, the degree of the product is the sum of degrees - `leading_coeff_prod` : for polynomials over an integral domain, the leading coefficient is the product of leading coefficients - `prod_X_sub_C_coeff_card_pred` carries most of the content for computing the second coefficient of the characteristic polynomial. -/ namespace polynomial theorem nat_degree_prod_le {R : Type u} {ι : Type w} (s : finset ι) [comm_semiring R] (f : ι → polynomial R) : nat_degree (finset.prod s fun (i : ι) => f i) ≤ finset.sum s fun (i : ι) => nat_degree (f i) := sorry /-- The leading coefficient of a product of polynomials is equal to the product of the leading coefficients, provided that this product is nonzero. See `leading_coeff_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ theorem leading_coeff_prod' {R : Type u} {ι : Type w} (s : finset ι) [comm_semiring R] (f : ι → polynomial R) (h : (finset.prod s fun (i : ι) => leading_coeff (f i)) ≠ 0) : leading_coeff (finset.prod s fun (i : ι) => f i) = finset.prod s fun (i : ι) => leading_coeff (f i) := sorry /-- The degree of a product of polynomials is equal to the product of the degrees, provided that the product of leading coefficients is nonzero. See `nat_degree_prod` (without the `'`) for a version for integral domains, where this condition is automatically satisfied. -/ theorem nat_degree_prod' {R : Type u} {ι : Type w} (s : finset ι) [comm_semiring R] (f : ι → polynomial R) (h : (finset.prod s fun (i : ι) => leading_coeff (f i)) ≠ 0) : nat_degree (finset.prod s fun (i : ι) => f i) = finset.sum s fun (i : ι) => nat_degree (f i) := sorry theorem nat_degree_prod_of_monic {R : Type u} {ι : Type w} (s : finset ι) [comm_semiring R] (f : ι → polynomial R) [nontrivial R] (h : ∀ (i : ι), i ∈ s → monic (f i)) : nat_degree (finset.prod s fun (i : ι) => f i) = finset.sum s fun (i : ι) => nat_degree (f i) := sorry theorem coeff_zero_prod {R : Type u} {ι : Type w} (s : finset ι) [comm_semiring R] (f : ι → polynomial R) : coeff (finset.prod s fun (i : ι) => f i) 0 = finset.prod s fun (i : ι) => coeff (f i) 0 := sorry -- Eventually this can be generalized with Vieta's formulas -- plus the connection between roots and factorization. theorem prod_X_sub_C_next_coeff {R : Type u} {ι : Type w} [comm_ring R] [nontrivial R] {s : finset ι} (f : ι → R) : next_coeff (finset.prod s fun (i : ι) => X - coe_fn C (f i)) = -finset.sum s fun (i : ι) => f i := sorry theorem prod_X_sub_C_coeff_card_pred {R : Type u} {ι : Type w} [comm_ring R] [nontrivial R] (s : finset ι) (f : ι → R) (hs : 0 < finset.card s) : coeff (finset.prod s fun (i : ι) => X - coe_fn C (f i)) (finset.card s - 1) = -finset.sum s fun (i : ι) => f i := sorry theorem nat_degree_prod {R : Type u} {ι : Type w} (s : finset ι) [comm_ring R] [no_zero_divisors R] (f : ι → polynomial R) [nontrivial R] (h : ∀ (i : ι), i ∈ s → f i ≠ 0) : nat_degree (finset.prod s fun (i : ι) => f i) = finset.sum s fun (i : ι) => nat_degree (f i) := sorry theorem leading_coeff_prod {R : Type u} {ι : Type w} (s : finset ι) [comm_ring R] [no_zero_divisors R] (f : ι → polynomial R) : leading_coeff (finset.prod s fun (i : ι) => f i) = finset.prod s fun (i : ι) => leading_coeff (f i) := sorry
794a2a2365260784fde6379707c9964550237a50
5ec8f5218a7c8e87dd0d70dc6b715b36d61a8d61
/linking.lean
cd4b2b8fca61db2fb134e9ace561d8c03bce4336
[]
no_license
mbrodersen/kremlin
f9f2f9dd77b9744fe0ffd5f70d9fa0f1f8bd8cec
d4665929ce9012e93a0b05fc7063b96256bab86f
refs/heads/master
1,624,057,268,130
1,496,957,084,000
1,496,957,084,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,054
lean
/- Separate compilation and syntactic linking -/ import .ast .maps .lib /- This file follows "approach A" from the paper "Lightweight Verification of Separate Compilation" by Kang, Kim, Hur, Dreyer and Vafeiadis, POPL 2016. -/ namespace linking open ast maps /- * Syntactic linking -/ /- A syntactic element [A] supports syntactic linking if it is equipped with the following: - a partial binary operator [link] that produces the result of linking two elements, or fails if they cannot be linked (e.g. two definitions that are incompatible); - a preorder [linkorder] with the meaning that [linkorder a1 a2] holds if [a2] can be obtained by linking [a1] with some other syntactic element. -/ class linker (A : Type) := (link : A → A → option A) (linkorder : A → A → Prop) (linkorder_refl : ∀ x, linkorder x x) (linkorder_trans : ∀ {x y z}, linkorder x y → linkorder y z → linkorder x z) (link_linkorder : ∀ {x y z}, link x y = some z → linkorder x z ∧ linkorder y z) export linker (link linkorder) /- Linking variable initializers. We adopt the following conventions: - an "extern" variable has an empty initialization list; - a "common" variable has an initialization list of the form [Init_space sz]; - all other initialization lists correspond to fully defined variables, neither "common" nor "extern". -/ inductive init_class : list init_data → Type | extern : init_class [] | common (sz) : init_class [init_data.space sz] | definitive (il) : init_class il def classify_init : Π (i : list init_data), init_class i | [] := init_class.extern | (init_data.space sz :: []) := init_class.common sz | i := init_class.definitive i def link_varinit (i1 i2 : list init_data) := match i1, i2, classify_init i1, classify_init i2 with | ._, i2, init_class.extern, _ := some i2 | i1, ._, _, init_class.extern := some i1 | ._, i2, init_class.common sz1, _ := if sz1 = init_data.list_size i2 then some i2 else none | i1, ._, _, init_class.common sz2 := if sz2 = init_data.list_size i1 then some i1 else none | i1, i2, _, _ := none end. inductive linkorder_varinit : list init_data → list init_data → Prop | linkorder_varinit_refl (il) : linkorder_varinit il il | linkorder_varinit_extern (il) : linkorder_varinit [] il | linkorder_varinit_common {sz il} : il ≠ list.nil → init_data.list_size il = sz → linkorder_varinit [init_data.space sz] il. instance Linker_varinit : linker (list init_data) := { link := link_varinit, linkorder := linkorder_varinit, linkorder_refl := sorry', linkorder_trans := sorry', link_linkorder := sorry' } /- Linking variable definitions. -/ def link_vardef {V} [linker V] (v1 v2 : globvar V) : option (globvar V) := match link v1.info v2.info with | none := none | some info := match link v1.init v2.init with | none := none | some init := if v1.readonly = v2.readonly ∧ v1.volatile = v2.volatile then some { info := info, init := init, readonly := v1.readonly, volatile := v1.volatile } else none end end. inductive linkorder_vardef {V} [linker V] : globvar V → globvar V → Prop | intro {info1 info2 i1 i2} {ro vo} : linkorder info1 info2 → linkorder i1 i2 → linkorder_vardef ⟨info1, i1, ro, vo⟩ ⟨info2, i2, ro, vo⟩ instance Linker_vardef {V} [linker V] : linker (globvar V) := { link := link_vardef, linkorder := linkorder_vardef, linkorder_refl := sorry', linkorder_trans := sorry', link_linkorder := sorry' } /- Linking global definitions -/ def link_def {F V} [linker F] [linker V] : globdef F V → globdef F V → option (globdef F V) | (Gfun f1) (Gfun f2) := Gfun <$> link f1 f2 | (Gvar v1) (Gvar v2) := Gvar <$> link v1 v2 | _ _ := none inductive linkorder_def {F V} [linker F] [linker V] : globdef F V → globdef F V → Prop | linkorder_def_fun (fd1 fd2) : linkorder fd1 fd2 → linkorder_def (Gfun fd1) (Gfun fd2) | linkorder_def_var (v1 v2) : linkorder v1 v2 → linkorder_def (Gvar v1) (Gvar v2). instance Linker_def {F V} [linker F] [linker V] : linker (globdef F V) := { link := link_def, linkorder := linkorder_def, linkorder_refl := sorry', linkorder_trans := sorry', link_linkorder := sorry' } /- Linking two compilation units. Compilation units are represented like whole programs using the type [program F V]. If a name has a global definition in one unit but not in the other, this definition is left unchanged in the result of the link. If a name has global definitions in both units, and is public (not static) in both, the two definitions are linked as per [Linker_def] above. If one or both definitions are static (not public), we should ideally rename it so that it can be kept unchanged in the result of the link. This would require a general notion of renaming of global identifiers in programs that we do not have yet. Hence, as a first step, linking is undefined if static definitions with the same name appear in both compilation units. -/ section linker_prog parameters {F V : Type} [linker F] [linker V] section parameters (p1 p2 : program F V) def dm1 := prog_defmap p1 def dm2 := prog_defmap p2 def link_prog_check (x : ident) (gd1 : globdef F V) := match dm2^!x with | none := tt | some gd2 := (x ∈ p1.public) && (x ∈ p2.public) && (link gd1 gd2).is_some end def link_prog_merge : option (globdef F V) → option (globdef F V) → option (globdef F V) | none o2 := o2 | o1 none := o1 | (some gd1) (some gd2) := link gd1 gd2 def link_prog : option (program F V) := if p1.main = p2.main ∧ PTree.for_all dm1 link_prog_check then some { main := p1.main, public := p1.public ++ p2.public, defs := PTree.elements $ PTree.combine link_prog_merge dm1 dm2 } else none lemma link_prog_inv (p) (h : link_prog = some p) : p1.main = p2.main ∧ (∀ (id : ident) gd1 gd2, (dm1^!id) = some gd1 → (dm2^!id) = some gd2 → id ∈ p1.public ∧ id ∈ p2.public ∧ ∃ gd, link gd1 gd2 = some gd) ∧ p = { main := p1.main, public := p1.public ++ p2.public, defs := PTree.elements (PTree.combine link_prog_merge dm1 dm2) } := sorry' lemma link_prog_succeeds (hp : p1.main = p2.main) (h : ∀ (id : ident) gd1 gd2, (dm1^!id) = some gd1 → (dm2^!id) = some gd2 → id ∈ p1.public ∧ id ∈ p2.public ∧ (link gd1 gd2).is_some) : link_prog = some { main := p1.main, public := p1.public ++ p2.public, defs := PTree.elements (PTree.combine link_prog_merge dm1 dm2) } := sorry' lemma prog_defmap_elements (m: PTree (globdef F V)) (pub mn x) : (prog_defmap ⟨PTree.elements m, pub, mn⟩ ^! x) = (m^!x) := sorry' end instance linker_prog : linker (program F V) := { link := link_prog, linkorder := λp1 p2, p1.main = p2.main ∧ p1.public ⊆ p2.public ∧ ∀ (id : ident) gd1, (prog_defmap p1^!id) = some gd1 → ∃ gd2, (prog_defmap p2^!id) = some gd2 ∧ linkorder gd1 gd2 ∧ (id ∉ p2.public → gd2 = gd1), linkorder_refl := sorry', linkorder_trans := sorry', link_linkorder := sorry' } lemma prog_defmap_linkorder {p1 p2 : program F V} {id gd1} : linkorder p1 p2 → (prog_defmap p1^!id) = some gd1 → ∃ gd2, (prog_defmap p2^!id) = some gd2 ∧ linkorder gd1 gd2 := sorry' end linker_prog /- * Matching between two programs -/ /- The following is a relational presentation of program transformations, e.g. [transf_partial_program] from module [AST]. -/ /- To capture the possibility of separate compilation, we parameterize the [match_fundef] relation between function definitions with a context, e.g. the compilation unit from which the function definition comes. This unit is characterized as any program that is in the [linkorder] relation with the final, whole program. -/ section match_program_generic parameters {C F1 V1 F2 V2 : Type} -- [linker F1] [linker V1] parameter match_fundef : C → F1 → F2 → Prop parameter match_varinfo : V1 → V2 → Prop inductive match_globvar : globvar V1 → globvar V2 → Prop | mk (i1 i2 init ro vo) : match_varinfo i1 i2 → match_globvar ⟨i1, init, ro, vo⟩ ⟨i2, init, ro, vo⟩ variable [linker C] inductive match_globdef (ctx : C) : globdef F1 V1 → globdef F2 V2 → Prop | func (ctx' f1 f2) : linkorder ctx' ctx → match_fundef ctx' f1 f2 → match_globdef (Gfun f1) (Gfun f2) | var {v1 v2} : match_globvar v1 v2 → match_globdef (Gvar v1) (Gvar v2). def match_ident_globdef (ctx : C) : ident × globdef F1 V1 → ident × globdef F2 V2 → Prop | (i1, g1) (i2, g2) := i1 = i2 ∧ match_globdef ctx g1 g2 def match_program_gen (ctx : C) (p1 : program F1 V1) (p2 : program F2 V2) : Prop := list.forall2 (match_ident_globdef ctx) p1.defs p2.defs ∧ p2.main = p1.main ∧ p2.public = p1.public theorem match_program_defmap {ctx p1 p2} (hm : match_program_gen ctx p1 p2) (id) : option.rel (match_globdef ctx) (prog_defmap p1^!id) (prog_defmap p2^!id) := sorry' lemma match_program_gen_main {ctx p1 p2} (hm : match_program_gen ctx p1 p2) : p2.main = p1.main := sorry' lemma match_program_public {ctx p1 p2} (hm : match_program_gen ctx p1 p2) : p2.public = p1.public := sorry' end match_program_generic /- In many cases, the context for [match_program_gen] is the source program or source compilation unit itself. We provide a specialized definition for this case. -/ def match_program {F1 V1 F2 V2} [linker F1] [linker V1] (match_fundef : program F1 V1 → F1 → F2 → Prop) (match_varinfo : V1 → V2 → Prop) (p1 : program F1 V1) (p2 : program F2 V2) : Prop := match_program_gen match_fundef match_varinfo p1 p1 p2 lemma match_program_main {F1 V1 F2 V2} [linker F1] [linker V1] {match_fundef : program F1 V1 → F1 → F2 → Prop} {match_varinfo : V1 → V2 → Prop} {p1 : program F1 V1} {p2 : program F2 V2} (hm : match_program match_fundef match_varinfo p1 p2) : p2.main = p1.main := match_program_gen_main _ _ hm end linking
90d2ba1adeb2f8420996f6cd293bf902f581493d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/field_theory/separable_degree.lean
c88eb9996e2c9af4025520e5f5c63df47b12ffe3
[ "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,351
lean
/- Copyright (c) 2021 Jakob Scholbach. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob Scholbach -/ import algebra.algebra.basic import algebra.char_p.exp_char import field_theory.separable /-! # Separable degree This file contains basics about the separable degree of a polynomial. ## Main results - `is_separable_contraction`: is the condition that, for `g` a separable polynomial, we have that `g(x^(q^m)) = f(x)` for some `m : ℕ`. - `has_separable_contraction`: the condition of having a separable contraction - `has_separable_contraction.degree`: the separable degree, defined as the degree of some separable contraction - `irreducible.has_separable_contraction`: any irreducible polynomial can be contracted to a separable polynomial - `has_separable_contraction.dvd_degree'`: the degree of a separable contraction divides the degree, in function of the exponential characteristic of the field - `has_separable_contraction.dvd_degree` and `has_separable_contraction.eq_degree` specialize the statement of `separable_degree_dvd_degree` - `is_separable_contraction.degree_eq`: the separable degree is well-defined, implemented as the statement that the degree of any separable contraction equals `has_separable_contraction.degree` ## Tags separable degree, degree, polynomial -/ namespace polynomial noncomputable theory open_locale classical polynomial section comm_semiring variables {F : Type} [comm_semiring F] (q : ℕ) /-- A separable contraction of a polynomial `f` is a separable polynomial `g` such that `g(x^(q^m)) = f(x)` for some `m : ℕ`.-/ def is_separable_contraction (f : F[X]) (g : F[X]) : Prop := g.separable ∧ ∃ m : ℕ, expand F (q^m) g = f /-- The condition of having a separable contration. -/ def has_separable_contraction (f : F[X]) : Prop := ∃ g : F[X], is_separable_contraction q f g variables {q} {f : F[X]} (hf : has_separable_contraction q f) /-- A choice of a separable contraction. -/ def has_separable_contraction.contraction : F[X] := classical.some hf /-- The separable degree of a polynomial is the degree of a given separable contraction. -/ def has_separable_contraction.degree : ℕ := hf.contraction.nat_degree /-- The separable degree divides the degree, in function of the exponential characteristic of F. -/ lemma is_separable_contraction.dvd_degree' {g} (hf : is_separable_contraction q f g) : ∃ m : ℕ, g.nat_degree * (q ^ m) = f.nat_degree := begin obtain ⟨m, rfl⟩ := hf.2, use m, rw nat_degree_expand, end lemma has_separable_contraction.dvd_degree' : ∃ m : ℕ, hf.degree * (q ^ m) = f.nat_degree := (classical.some_spec hf).dvd_degree' /-- The separable degree divides the degree. -/ lemma has_separable_contraction.dvd_degree : hf.degree ∣ f.nat_degree := let ⟨a, ha⟩ := hf.dvd_degree' in dvd.intro (q ^ a) ha /-- In exponential characteristic one, the separable degree equals the degree. -/ lemma has_separable_contraction.eq_degree {f : F[X]} (hf : has_separable_contraction 1 f) : hf.degree = f.nat_degree := let ⟨a, ha⟩ := hf.dvd_degree' in by rw [←ha, one_pow a, mul_one] end comm_semiring section field variables {F : Type} [field F] variables (q : ℕ) {f : F[X]} (hf : has_separable_contraction q f) /-- Every irreducible polynomial can be contracted to a separable polynomial. https://stacks.math.columbia.edu/tag/09H0 -/ lemma _root_.irreducible.has_separable_contraction (q : ℕ) [hF : exp_char F q] (f : F[X]) (irred : irreducible f) : has_separable_contraction q f := begin casesI hF, { exact ⟨f, irred.separable, ⟨0, by rw [pow_zero, expand_one]⟩⟩ }, { rcases exists_separable_of_irreducible q irred ‹q.prime›.ne_zero with ⟨n, g, hgs, hge⟩, exact ⟨g, hgs, n, hge⟩, } end /-- A helper lemma: if two expansions (along the positive characteristic) of two polynomials `g` and `g'` agree, and the one with the larger degree is separable, then their degrees are the same. -/ lemma contraction_degree_eq_aux [hq : fact q.prime] [hF : char_p F q] (g g' : F[X]) (m m' : ℕ) (h_expand : expand F (q^m) g = expand F (q^m') g') (h : m < m') (hg : g.separable): g.nat_degree = g'.nat_degree := begin obtain ⟨s, rfl⟩ := nat.exists_eq_add_of_lt h, rw [add_assoc, pow_add, expand_mul] at h_expand, let aux := expand_injective (pow_pos hq.1.pos m) h_expand, rw aux at hg, have := (is_unit_or_eq_zero_of_separable_expand q (s + 1) hq.out.pos hg).resolve_right s.succ_ne_zero, rw [aux, nat_degree_expand, nat_degree_eq_of_degree_eq_some (degree_eq_zero_of_is_unit this), zero_mul] end /-- If two expansions (along the positive characteristic) of two separable polynomials `g` and `g'` agree, then they have the same degree. -/ theorem contraction_degree_eq_or_insep [hq : fact q.prime] [char_p F q] (g g' : F[X]) (m m' : ℕ) (h_expand : expand F (q^m) g = expand F (q^m') g') (hg : g.separable) (hg' : g'.separable) : g.nat_degree = g'.nat_degree := begin by_cases h : m = m', { -- if `m = m'` then we show `g.nat_degree = g'.nat_degree` by unfolding the definitions rw h at h_expand, have expand_deg : ((expand F (q ^ m')) g).nat_degree = (expand F (q ^ m') g').nat_degree, by rw h_expand, rw [nat_degree_expand (q^m') g, nat_degree_expand (q^m') g'] at expand_deg, apply nat.eq_of_mul_eq_mul_left (pow_pos hq.1.pos m'), rw [mul_comm] at expand_deg, rw expand_deg, rw [mul_comm] }, { cases ne.lt_or_lt h, { exact contraction_degree_eq_aux q g g' m m' h_expand h_1 hg }, { exact (contraction_degree_eq_aux q g' g m' m h_expand.symm h_1 hg').symm, } } end /-- The separable degree equals the degree of any separable contraction, i.e., it is unique. -/ theorem is_separable_contraction.degree_eq [hF : exp_char F q] (g : F[X]) (hg : is_separable_contraction q f g) : g.nat_degree = hf.degree := begin casesI hF, { rcases hg with ⟨g, m, hm⟩, rw [one_pow, expand_one] at hm, rw hf.eq_degree, rw hm, }, { rcases hg with ⟨hg, m, hm⟩, let g' := classical.some hf, cases (classical.some_spec hf).2 with m' hm', haveI : fact q.prime := fact_iff.2 hF_hprime, apply contraction_degree_eq_or_insep q g g' m m', rw [hm, hm'], exact hg, exact (classical.some_spec hf).1 } end end field end polynomial
149ec1d987c4fe2298be0ee356c7ded72789faa9
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/linear_algebra/dual.lean
5a6bcbac9e853433de2d89beca62ad99f6b0b275
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
6,471
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Fabian Glöckle Dual vector spaces, the spaces of linear functionals to the base field, including the dual basis isomorphism and evaluation isomorphism in the finite-dimensional case. -/ import linear_algebra.dimension import linear_algebra.tensor_product noncomputable theory local attribute [instance, priority 0] classical.prop_decidable namespace module variables (R : Type*) (M : Type*) variables [comm_ring R] [add_comm_group M] [module R M] def dual := M →ₗ[R] R namespace dual instance : has_coe_to_fun (dual R M) := ⟨_, linear_map.to_fun⟩ instance : add_comm_group (dual R M) := by delta dual; apply_instance instance : module R (dual R M) := by delta dual; apply_instance def eval : M →ₗ[R] (dual R (dual R M)) := linear_map.id.flip lemma eval_apply (v : M) (a : dual R M) : (eval R M v) a = a v := begin dunfold eval, rw [linear_map.flip_apply, linear_map.id_apply] end end dual end module namespace is_basis universes u v variables {K : Type u} {V : Type v} variables [discrete_field K] [add_comm_group V] [vector_space K V] variables {B : set V} (h : is_basis K B) open vector_space module module.dual submodule linear_map cardinal function instance dual.vector_space : vector_space K (dual K V) := {..dual.module K V} include h def to_dual : V →ₗ[K] module.dual K V := h.constr $ λ v, h.constr $ λ w, if w = v then 1 else 0 lemma to_dual_apply (v ∈ B) (w ∈ B) : (h.to_dual v) w = if w = v then 1 else 0 := by erw [constr_basis h ‹v ∈ B›, constr_basis h ‹w ∈ B›] def to_dual_flip (v : V) : (V →ₗ[K] K) := (linear_map.flip h.to_dual).to_fun v omit h def eval_lc_at (v : V) : (lc K V) →ₗ[K] K := { to_fun := λ f, f v, add := by intros; rw finsupp.add_apply, smul := by intros; rw finsupp.smul_apply } include h def coord_fun (v : V) : (V →ₗ[K] K) := (eval_lc_at v).comp h.repr lemma coord_fun_eq_repr (v w : V) : h.coord_fun v w = h.repr w v := rfl lemma to_dual_swap_eq_to_dual (v w : V) : h.to_dual_flip v w = h.to_dual w v := rfl lemma to_dual_eq_repr (v : V) (b ∈ B) : (h.to_dual v) b = h.repr v b := begin rw [←coord_fun_eq_repr, ←to_dual_swap_eq_to_dual], apply congr_fun, dsimp, congr', apply h.ext, intros, rw [to_dual_swap_eq_to_dual, to_dual_apply], { split_ifs with hx, { rwa [hx, coord_fun_eq_repr, repr_eq_single, finsupp.single_apply, if_pos rfl] }, { rwa [coord_fun_eq_repr, repr_eq_single, finsupp.single_apply, if_neg (ne.symm hx)] } }, assumption' end lemma to_dual_inj (v : V) (a : h.to_dual v = 0) : v = 0 := begin rw [← mem_bot K, ← h.repr_ker, mem_ker], apply finsupp.ext, intro b, by_cases hb : b ∈ B, { rw [←to_dual_eq_repr _ _ _ hb, a], refl }, { dsimp, rw ←finsupp.not_mem_support_iff, by_contradiction hb, have nhb : b ∈ B := set.mem_of_subset_of_mem (h.repr_supported v) hb, contradiction } end theorem to_dual_ker : h.to_dual.ker = ⊥ := ker_eq_bot'.mpr h.to_dual_inj theorem to_dual_range [fin : fintype B] : h.to_dual.range = ⊤ := begin rw eq_top_iff', intro f, rw linear_map.mem_range, let lin_comb : B →₀ K := finsupp.on_finset fin.elems (λ b, f.to_fun b) _, let emb := embedding.subtype B, { use lc.total K V (finsupp.emb_domain emb lin_comb), apply h.ext, intros x hx, rw [h.to_dual_eq_repr _ x hx, repr_total _], have emb_x : x = emb ⟨x, hx⟩, from rfl, { rw [emb_x, finsupp.emb_domain_apply emb lin_comb _, ← emb_x], simpa }, { rw [lc.mem_supported, finsupp.support_emb_domain, finset.map_eq_image, finset.coe_image], apply subtype.val_image_subset } }, { intros a _, apply fin.complete } end def dual_basis : set (dual K V) := h.to_dual '' B theorem dual_lin_independent : linear_independent K h.dual_basis := begin apply linear_independent.image h.1, rw to_dual_ker, exact disjoint_bot_right end def to_dual_equiv [fintype B] : V ≃ₗ[K] (dual K V) := linear_equiv.of_bijective h.to_dual h.to_dual_ker h.to_dual_range theorem dual_basis_is_basis [fintype B] : is_basis K h.dual_basis := h.to_dual_equiv.is_basis h @[simp] lemma to_dual_to_dual [fintype B] : (h.dual_basis_is_basis.to_dual).comp h.to_dual = eval K V := begin apply h.ext, intros b hb, apply h.dual_basis_is_basis.ext, intros c hc, dunfold eval, rw [linear_map.flip_apply, linear_map.id_apply, linear_map.comp_apply, to_dual_apply h.dual_basis_is_basis (h.to_dual b) _ c hc], { dsimp [dual_basis] at hc, rw set.mem_image at hc, rcases hc with ⟨b', hb', rfl⟩, rw to_dual_apply, split_ifs with h₁ h₂; try {refl}, { have hh : b = b', from (ker_eq_bot.1 h.to_dual_ker h₁).symm, contradiction }, { subst h_1, contradiction }, assumption' }, { dunfold dual_basis, exact set.mem_image_of_mem h.to_dual hb } end theorem dual_dim_eq [fintype B] : cardinal.lift.{v (max u v)} (dim K V) = dim K (dual K V) := by { rw linear_equiv.dim_eq_lift h.to_dual_equiv, apply cardinal.lift_id } end is_basis namespace vector_space universes u v variables {K : Type u} {V : Type v} variables [discrete_field K] [add_comm_group V] [vector_space K V] open module module.dual submodule linear_map cardinal is_basis theorem eval_ker : (eval K V).ker = ⊥ := begin rw ker_eq_bot', intros v h, rw linear_map.ext_iff at h, by_contradiction H, rcases exists_subset_is_basis (linear_independent_singleton H) with ⟨b, hv, hb⟩, swap 4, assumption, have hv' : v ∈ b := hv (set.mem_singleton v), let hx := h (hb.to_dual v), erw [eval_apply, to_dual_apply _ _ hv' _ hv', if_pos rfl, zero_apply _] at hx, exact one_ne_zero hx end theorem dual_dim_eq (h : dim K V < omega) : cardinal.lift.{v (max u v)} (dim K V) = dim K (dual K V) := begin rcases exists_is_basis_fintype h with ⟨b, hb, ⟨hf⟩⟩, resetI, exact hb.dual_dim_eq end set_option class.instance_max_depth 70 lemma eval_range (h : dim K V < omega) : (eval K V).range = ⊤ := begin rcases exists_is_basis_fintype h with ⟨b, hb, ⟨hf⟩⟩, resetI, rw [← hb.to_dual_to_dual, range_comp, hb.to_dual_range, map_top, to_dual_range _], delta dual_basis, apply_instance end def eval_equiv (h : dim K V < omega) : V ≃ₗ[K] dual K (dual K V) := linear_equiv.of_bijective (eval K V) eval_ker (eval_range h) end vector_space
9bc9bcc8c4a9cbe3af90a34d81d1f933c9978acf
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/sec_param_pp.lean
6b2e078238426e6e44b313a07f409b19f1da16f3
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
281
lean
section parameters {A : Type*} (a : A) variable f : A → A → A definition id2 : A := a #check id2 definition pr (b : A) : A := f a b #check pr f id2 set_option pp.universes true #check pr f id2 definition pr2 (B : Type*) (b : B) : A := a #check pr2 nat 10 end
16008509960a0d11e87366bd4e865b685f758258
0c1546a496eccfb56620165cad015f88d56190c5
/tests/lean/run/simplifier_custom_relations.lean
c1d3cf23dbf0b00ea318c60851946595dc390d2f
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,001
lean
open tactic universe l constants (A : Type.{l}) (rel : A → A → Prop) (rel.refl : ∀ a, rel a a) (rel.symm : ∀ a b, rel a b → rel b a) (rel.trans : ∀ a b c, rel a b → rel b c → rel a c) attribute [refl] rel.refl attribute [symm] rel.symm attribute [trans] rel.trans constants (x y z : A) (f g h : A → A) (H₁ : rel (f x) (g y)) (H₂ : rel (h (g y)) z) (Hf : ∀ (a b : A), rel a b → rel (f a) (f b)) (Hg : ∀ (a b : A), rel a b → rel (g a) (g b)) (Hh : ∀ (a b : A), rel a b → rel (h a) (h b)) attribute [simp] H₁ H₂ attribute [congr] Hf Hg Hh print [simp] default print [congr] default meta definition relsimp_core (e : expr) : tactic (expr × expr) := do S ← simp_lemmas.mk_default, e_type ← infer_type e >>= whnf, simplify_core {} S `rel e example : rel (h (f x)) z := by do e₁ ← to_expr `(h (f x)), (e₁', pf) ← relsimp_core e₁, exact pf
2f96f7143942f66d6d853db0485566f5af5be90d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/wfOverapplicationIssue.lean
fcc2cd14d3226d4b7bceeb5108bcd894ce90c083
[ "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
591
lean
theorem Array.sizeOf_lt_of_mem' [DecidableEq α] [SizeOf α] {as : Array α} (h : a ∈ as) : sizeOf a < sizeOf as := by simp [Membership.mem, contains, any, Id.run, BEq.beq, anyM] at h let rec aux (j : Nat) : anyM.loop (m := Id) (fun b => decide (a = b)) as as.size (Nat.le_refl ..) j = true → sizeOf a < sizeOf as := by unfold anyM.loop intro h split at h · simp [Bind.bind, pure] at h; split at h next he => subst a; apply sizeOf_get_lt next => have ih := aux (j+1) h; assumption · contradiction apply aux 0 h termination_by aux j => as.size - j
e4a1908ec0dbac784cb99b93881ece01e0034cb4
9dc8cecdf3c4634764a18254e94d43da07142918
/src/tactic/interactive.lean
ea73a5b041564802296e74ed9fbb901a2f2c0693
[ "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
40,486
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Simon Hudon, Sébastien Gouëzel, Scott Morrison -/ import logic.nonempty import tactic.lint import tactic.dependencies setup_tactic_parser namespace tactic namespace interactive open interactive interactive.types expr /-- Similar to `constructor`, but does not reorder goals. -/ meta def fconstructor : tactic unit := concat_tags tactic.fconstructor add_tactic_doc { name := "fconstructor", category := doc_category.tactic, decl_names := [`tactic.interactive.fconstructor], tags := ["logic", "goal management"] } /-- `try_for n { tac }` executes `tac` for `n` ticks, otherwise uses `sorry` to close the goal. Never fails. Useful for debugging. -/ meta def try_for (max : parse parser.pexpr) (tac : itactic) : tactic unit := do max ← i_to_expr_strict max >>= tactic.eval_expr nat, λ s, match _root_.try_for max (tac s) with | some r := r | none := (tactic.trace "try_for timeout, using sorry" >> tactic.admit) s end /-- Multiple `subst`. `substs x y z` is the same as `subst x, subst y, subst z`. -/ meta def substs (l : parse ident*) : tactic unit := propagate_tags $ l.mmap' (λ h, get_local h >>= tactic.subst) >> try (tactic.reflexivity reducible) add_tactic_doc { name := "substs", category := doc_category.tactic, decl_names := [`tactic.interactive.substs], tags := ["rewriting"] } /-- Unfold coercion-related definitions -/ meta def unfold_coes (loc : parse location) : tactic unit := unfold [ ``coe, ``coe_t, ``has_coe_t.coe, ``coe_b,``has_coe.coe, ``lift, ``has_lift.lift, ``lift_t, ``has_lift_t.lift, ``coe_fn, ``has_coe_to_fun.coe, ``coe_sort, ``has_coe_to_sort.coe] loc add_tactic_doc { name := "unfold_coes", category := doc_category.tactic, decl_names := [`tactic.interactive.unfold_coes], tags := ["simplification"] } /-- Unfold `has_well_founded.r`, `sizeof` and other such definitions. -/ meta def unfold_wf := propagate_tags (well_founded_tactics.unfold_wf_rel; well_founded_tactics.unfold_sizeof) /-- Unfold auxiliary definitions associated with the current declaration. -/ meta def unfold_aux : tactic unit := do tgt ← target, name ← decl_name, let to_unfold := (tgt.list_names_with_prefix name), guard (¬ to_unfold.empty), -- should we be using simp_lemmas.mk_default? simp_lemmas.mk.dsimplify to_unfold.to_list tgt >>= tactic.change /-- For debugging only. This tactic checks the current state for any missing dropped goals and restores them. Useful when there are no goals to solve but "result contains meta-variables". -/ meta def recover : tactic unit := metavariables >>= tactic.set_goals /-- Like `try { tac }`, but in the case of failure it continues from the failure state instead of reverting to the original state. -/ meta def continue (tac : itactic) : tactic unit := λ s, result.cases_on (tac s) (λ a, result.success ()) (λ e ref, result.success ()) /-- `id { tac }` is the same as `tac`, but it is useful for creating a block scope without requiring the goal to be solved at the end like `{ tac }`. It can also be used to enclose a non-interactive tactic for patterns like `tac1; id {tac2}` where `tac2` is non-interactive. -/ @[inline] protected meta def id (tac : itactic) : tactic unit := tac /-- `work_on_goal n { tac }` creates a block scope for the `n`-goal, and does not require that the goal be solved at the end (any remaining subgoals are inserted back into the list of goals). Typically usage might look like: ```` intros, simp, apply lemma_1, work_on_goal 3 { dsimp, simp }, refl ```` See also `id { tac }`, which is equivalent to `work_on_goal 1 { tac }`. -/ meta def work_on_goal : parse small_nat → itactic → tactic unit | 0 t := fail "work_on_goal failed: goals are 1-indexed" | (n+1) t := do goals ← get_goals, let earlier_goals := goals.take n, let later_goals := goals.drop (n+1), set_goals (goals.nth n).to_list, t, new_goals ← get_goals, set_goals (earlier_goals ++ new_goals ++ later_goals) /-- `swap n` will move the `n`th goal to the front. `swap` defaults to `swap 2`, and so interchanges the first and second goals. See also `tactic.interactive.rotate`, which moves the first `n` goals to the back. -/ meta def swap (n := 2) : tactic unit := do gs ← get_goals, match gs.nth (n-1) with | (some g) := set_goals (g :: gs.remove_nth (n-1)) | _ := skip end add_tactic_doc { name := "swap", category := doc_category.tactic, decl_names := [`tactic.interactive.swap], tags := ["goal management"] } /-- `rotate` moves the first goal to the back. `rotate n` will do this `n` times. See also `tactic.interactive.swap`, which moves the `n`th goal to the front. -/ meta def rotate (n := 1) : tactic unit := tactic.rotate n add_tactic_doc { name := "rotate", category := doc_category.tactic, decl_names := [`tactic.interactive.rotate], tags := ["goal management"] } /-- Clear all hypotheses starting with `_`, like `_match` and `_let_match`. -/ meta def clear_ : tactic unit := tactic.repeat $ do l ← local_context, l.reverse.mfirst $ λ h, do name.mk_string s p ← return $ local_pp_name h, guard (s.front = '_'), cl ← infer_type h >>= is_class, guard (¬ cl), tactic.clear h add_tactic_doc { name := "clear_", category := doc_category.tactic, decl_names := [`tactic.interactive.clear_], tags := ["context management"] } /-- Acts like `have`, but removes a hypothesis with the same name as this one. For example if the state is `h : p ⊢ goal` and `f : p → q`, then after `replace h := f h` the goal will be `h : q ⊢ goal`, where `have h := f h` would result in the state `h : p, h : q ⊢ goal`. This can be used to simulate the `specialize` and `apply at` tactics of Coq. -/ meta def replace (h : parse ident?) (q₁ : parse (tk ":" *> texpr)?) (q₂ : parse $ (tk ":=" *> texpr)?) : tactic unit := do let h := h.get_or_else `this, old ← try_core (get_local h), «have» h q₁ q₂, match old, q₂ with | none, _ := skip | some o, some _ := tactic.clear o | some o, none := swap >> tactic.clear o >> swap end add_tactic_doc { name := "replace", category := doc_category.tactic, decl_names := [`tactic.interactive.replace], tags := ["context management"] } /-- Make every proposition in the context decidable. `classical!` does this more aggressively, such that even if a decidable instance is already available for a specific proposition, the noncomputable one will be used instead. -/ meta def classical (bang : parse $ (tk "!")?) := tactic.classical bang.is_some add_tactic_doc { name := "classical", category := doc_category.tactic, decl_names := [`tactic.interactive.classical], tags := ["classical logic", "type class"] } private meta def generalize_arg_p_aux : pexpr → parser (pexpr × name) | (app (app (macro _ [const `eq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" private meta def generalize_arg_p : parser (pexpr × name) := with_desc "expr = id" $ parser.pexpr 0 >>= generalize_arg_p_aux @[nolint def_lemma] noncomputable lemma {u} generalize_a_aux {α : Sort u} (h : ∀ x : Sort u, (α → x) → x) : α := h α id /-- Like `generalize` but also considers assumptions specified by the user. The user can also specify to omit the goal. -/ meta def generalize_hyp (h : parse ident?) (_ : parse $ tk ":") (p : parse generalize_arg_p) (l : parse location) : tactic unit := do h' ← get_unused_name `h, x' ← get_unused_name `x, g ← if ¬ l.include_goal then do refine ``(generalize_a_aux _), some <$> (prod.mk <$> tactic.intro x' <*> tactic.intro h') else pure none, n ← l.get_locals >>= tactic.revert_lst, generalize h () p, intron n, match g with | some (x',h') := do tactic.apply h', tactic.clear h', tactic.clear x' | none := return () end add_tactic_doc { name := "generalize_hyp", category := doc_category.tactic, decl_names := [`tactic.interactive.generalize_hyp], tags := ["context management"] } meta def compact_decl_aux : list name → binder_info → expr → list expr → tactic (list (list name × binder_info × expr)) | ns bi t [] := pure [(ns.reverse, bi, t)] | ns bi t (v'@(local_const n pp bi' t') :: xs) := do t' ← infer_type v', if bi = bi' ∧ t = t' then compact_decl_aux (pp :: ns) bi t xs else do vs ← compact_decl_aux [pp] bi' t' xs, pure $ (ns.reverse, bi, t) :: vs | ns bi t (_ :: xs) := compact_decl_aux ns bi t xs /-- go from (x₀ : t₀) (x₁ : t₀) (x₂ : t₀) to (x₀ x₁ x₂ : t₀) -/ meta def compact_decl : list expr → tactic (list (list name × binder_info × expr)) | [] := pure [] | (v@(local_const n pp bi t) :: xs) := do t ← infer_type v, compact_decl_aux [pp] bi t xs | (_ :: xs) := compact_decl xs /-- Remove identity functions from a term. These are normally automatically generated with terms like `show t, from p` or `(p : t)` which translate to some variant on `@id t p` in order to retain the type. -/ meta def clean (q : parse texpr) : tactic unit := do tgt : expr ← target, e ← i_to_expr_strict ``(%%q : %%tgt), tactic.exact $ e.clean meta def source_fields (missing : list name) (e : pexpr) : tactic (list (name × pexpr)) := do e ← to_expr e, t ← infer_type e, let struct_n : name := t.get_app_fn.const_name, fields ← expanded_field_list struct_n, let exp_fields := fields.filter (λ x, x.2 ∈ missing), exp_fields.mmap $ λ ⟨p,n⟩, (prod.mk n ∘ to_pexpr) <$> mk_mapp (n.update_prefix p) [none,some e] meta def collect_struct' : pexpr → state_t (list $ expr×structure_instance_info) tactic pexpr | e := do some str ← pure (e.get_structure_instance_info) | e.traverse collect_struct', v ← monad_lift mk_mvar, modify (list.cons (v,str)), pure $ to_pexpr v meta def collect_struct (e : pexpr) : tactic $ pexpr × list (expr×structure_instance_info) := prod.map id list.reverse <$> (collect_struct' e).run [] meta def refine_one (str : structure_instance_info) : tactic $ list (expr×structure_instance_info) := do tgt ← target >>= whnf, let struct_n : name := tgt.get_app_fn.const_name, exp_fields ← expanded_field_list struct_n, let missing_f := exp_fields.filter (λ f, (f.2 : name) ∉ str.field_names), (src_field_names,src_field_vals) ← (@list.unzip name _ ∘ list.join) <$> str.sources.mmap (source_fields $ missing_f.map prod.snd), let provided := exp_fields.filter (λ f, (f.2 : name) ∈ str.field_names), let missing_f' := missing_f.filter (λ x, x.2 ∉ src_field_names), vs ← mk_mvar_list missing_f'.length, (field_values,new_goals) ← list.unzip <$> (str.field_values.mmap collect_struct : tactic _), e' ← to_expr $ pexpr.mk_structure_instance { struct := some struct_n , field_names := str.field_names ++ missing_f'.map prod.snd ++ src_field_names , field_values := field_values ++ vs.map to_pexpr ++ src_field_vals }, tactic.exact e', gs ← with_enable_tags ( mzip_with (λ (n : name × name) v, do set_goals [v], try (dsimp_target simp_lemmas.mk), apply_auto_param <|> apply_opt_param <|> (set_main_tag [`_field,n.2,n.1]), get_goals) missing_f' vs), set_goals gs.join, return new_goals.join meta def refine_recursively : expr × structure_instance_info → tactic (list expr) | (e,str) := do set_goals [e], rs ← refine_one str, gs ← get_goals, gs' ← rs.mmap refine_recursively, return $ gs'.join ++ gs /-- `refine_struct { .. }` acts like `refine` but works only with structure instance literals. It creates a goal for each missing field and tags it with the name of the field so that `have_field` can be used to generically refer to the field currently being refined. As an example, we can use `refine_struct` to automate the construction of semigroup instances: ```lean refine_struct ( { .. } : semigroup α ), -- case semigroup, mul -- α : Type u, -- ⊢ α → α → α -- case semigroup, mul_assoc -- α : Type u, -- ⊢ ∀ (a b c : α), a * b * c = a * (b * c) ``` `have_field`, used after `refine_struct _`, poses `field` as a local constant with the type of the field of the current goal: ```lean refine_struct ({ .. } : semigroup α), { have_field, ... }, { have_field, ... }, ``` behaves like ```lean refine_struct ({ .. } : semigroup α), { have field := @semigroup.mul, ... }, { have field := @semigroup.mul_assoc, ... }, ``` -/ meta def refine_struct : parse texpr → tactic unit | e := do (x,xs) ← collect_struct e, refine x, gs ← get_goals, xs' ← xs.mmap refine_recursively, set_goals (xs'.join ++ gs) /-- `guard_hyp' h : t` fails if the hypothesis `h` does not have type `t`. We use this tactic for writing tests. Fixes `guard_hyp` by instantiating meta variables -/ meta def guard_hyp' (n : parse ident) (p : parse $ tk ":" *> texpr) : tactic unit := do h ← get_local n >>= infer_type >>= instantiate_mvars, guard_expr_eq h p /-- `match_hyp h : t` fails if the hypothesis `h` does not match the type `t` (which may be a pattern). We use this tactic for writing tests. -/ meta def match_hyp (n : parse ident) (p : parse $ tk ":" *> texpr) (m := reducible) : tactic (list expr) := do h ← get_local n >>= infer_type >>= instantiate_mvars, match_expr p h m /-- `guard_expr_strict t := e` fails if the expr `t` is not equal to `e`. By contrast to `guard_expr`, this tests strict (syntactic) equality. We use this tactic for writing tests. -/ meta def guard_expr_strict (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, guard (t = e) /-- `guard_target_strict t` fails if the target of the main goal is not syntactically `t`. We use this tactic for writing tests. -/ meta def guard_target_strict (p : parse texpr) : tactic unit := do t ← target, guard_expr_strict t p /-- `guard_hyp_strict h : t` fails if the hypothesis `h` does not have type syntactically equal to `t`. We use this tactic for writing tests. -/ meta def guard_hyp_strict (n : parse ident) (p : parse $ tk ":" *> texpr) : tactic unit := do h ← get_local n >>= infer_type >>= instantiate_mvars, guard_expr_strict h p /-- Tests that there are `n` hypotheses in the current context. -/ meta def guard_hyp_nums (n : ℕ) : tactic unit := do k ← local_context, guard (n = k.length) <|> fail format!"{k.length} hypotheses found" /-- `guard_hyp_mod_implicit h : t` fails if the type of the hypothesis `h` is not definitionally equal to `t` modulo none transparency (i.e., unifying the implicit arguments modulo semireducible transparency). We use this tactic for writing tests. -/ meta def guard_hyp_mod_implicit (n : parse ident) (p : parse $ tk ":" *> texpr) : tactic unit := do h ← get_local n >>= infer_type >>= instantiate_mvars, e ← to_expr p, is_def_eq h e transparency.none /-- `guard_target_mod_implicit t` fails if the target of the main goal is not definitionally equal to `t` modulo none transparency (i.e., unifying the implicit arguments modulo semireducible transparency). We use this tactic for writing tests. -/ meta def guard_target_mod_implicit (p : parse texpr) : tactic unit := do tgt ← target, e ← to_expr p, is_def_eq tgt e transparency.none /-- Test that `t` is the tag of the main goal. -/ meta def guard_tags (tags : parse ident*) : tactic unit := do (t : list name) ← get_main_tag, guard (t = tags) /-- `guard_proof_term { t } e` applies tactic `t` and tests whether the resulting proof term unifies with `p`. -/ meta def guard_proof_term (t : itactic) (p : parse texpr) : itactic := do g :: _ ← get_goals, e ← to_expr p, t, g ← instantiate_mvars g, unify e g /-- `success_if_fail_with_msg { tac } msg` succeeds if the interactive tactic `tac` fails with error message `msg` (for test writing purposes). -/ meta def success_if_fail_with_msg (tac : tactic.interactive.itactic) := tactic.success_if_fail_with_msg tac /-- Get the field of the current goal. -/ meta def get_current_field : tactic name := do [_,field,str] ← get_main_tag, expr.const_name <$> resolve_name (field.update_prefix str) meta def field (n : parse ident) (tac : itactic) : tactic unit := do gs ← get_goals, ts ← gs.mmap get_tag, ([g],gs') ← pure $ (list.zip gs ts).partition (λ x, x.snd.nth 1 = some n), set_goals [g.1], tac, done, set_goals $ gs'.map prod.fst /-- `have_field`, used after `refine_struct _` poses `field` as a local constant with the type of the field of the current goal: ```lean refine_struct ({ .. } : semigroup α), { have_field, ... }, { have_field, ... }, ``` behaves like ```lean refine_struct ({ .. } : semigroup α), { have field := @semigroup.mul, ... }, { have field := @semigroup.mul_assoc, ... }, ``` -/ meta def have_field : tactic unit := propagate_tags $ get_current_field >>= mk_const >>= note `field none >> return () /-- `apply_field` functions as `have_field, apply field, clear field` -/ meta def apply_field : tactic unit := propagate_tags $ get_current_field >>= applyc add_tactic_doc { name := "refine_struct", category := doc_category.tactic, decl_names := [`tactic.interactive.refine_struct, `tactic.interactive.apply_field, `tactic.interactive.have_field], tags := ["structures"], inherit_description_from := `tactic.interactive.refine_struct } /-- `apply_rules hs with attrs n` applies the list of lemmas `hs` and all lemmas tagged with an attribute from the list `attrs`, as well as the `assumption` tactic on the first goal and the resulting subgoals, iteratively, at most `n` times. `n` is optional, equal to 50 by default. You can pass an `apply_cfg` option argument as `apply_rules hs n opt`. (A typical usage would be with `apply_rules hs n { md := reducible })`, which asks `apply_rules` to not unfold `semireducible` definitions (i.e. most) when checking if a lemma matches the goal.) For instance: ```lean @[user_attribute] meta def mono_rules : user_attribute := { name := `mono_rules, descr := "lemmas usable to prove monotonicity" } attribute [mono_rules] add_le_add mul_le_mul_of_nonneg_right lemma my_test {a b c d e : real} (h1 : a ≤ b) (h2 : c ≤ d) (h3 : 0 ≤ e) : a + c * e + a + c + 0 ≤ b + d * e + b + d + e := -- any of the following lines solve the goal: add_le_add (add_le_add (add_le_add (add_le_add h1 (mul_le_mul_of_nonneg_right h2 h3)) h1 ) h2) h3 by apply_rules [add_le_add, mul_le_mul_of_nonneg_right] by apply_rules with mono_rules by apply_rules [add_le_add] with mono_rules ``` -/ meta def apply_rules (args : parse opt_pexpr_list) (attrs : parse with_ident_list) (n : nat := 50) (opt : apply_cfg := {}) : tactic unit := tactic.apply_rules args attrs n opt add_tactic_doc { name := "apply_rules", category := doc_category.tactic, decl_names := [`tactic.interactive.apply_rules], tags := ["lemma application"] } meta def return_cast (f : option expr) (t : option (expr × expr)) (es : list (expr × expr × expr)) (e x x' eq_h : expr) : tactic (option (expr × expr) × list (expr × expr × expr)) := (do guard (¬ e.has_var), unify x x', u ← mk_meta_univ, f ← f <|> mk_mapp ``_root_.id [(expr.sort u : expr)], t' ← infer_type e, some (f',t) ← pure t | return (some (f,t'), (e,x',eq_h) :: es), infer_type e >>= is_def_eq t, unify f f', return (some (f,t), (e,x',eq_h) :: es)) <|> return (t, es) meta def list_cast_of_aux (x : expr) (t : option (expr × expr)) (es : list (expr × expr × expr)) : expr → tactic (option (expr × expr) × list (expr × expr × expr)) | e@`(cast %%eq_h %%x') := return_cast none t es e x x' eq_h | e@`(eq.mp %%eq_h %%x') := return_cast none t es e x x' eq_h | e@`(eq.mpr %%eq_h %%x') := mk_eq_symm eq_h >>= return_cast none t es e x x' | e@`(@eq.subst %%α %%p %%a %%b %%eq_h %%x') := return_cast p t es e x x' eq_h | e@`(@eq.substr %%α %%p %%a %%b %%eq_h %%x') := mk_eq_symm eq_h >>= return_cast p t es e x x' | e@`(@eq.rec %%α %%a %%f %%x' _ %%eq_h) := return_cast f t es e x x' eq_h | e@`(@eq.rec_on %%α %%a %%f %%b %%eq_h %%x') := return_cast f t es e x x' eq_h | e := return (t,es) meta def list_cast_of (x tgt : expr) : tactic (list (expr × expr × expr)) := (list.reverse ∘ prod.snd) <$> tgt.mfold (none, []) (λ e i es, list_cast_of_aux x es.1 es.2 e) private meta def h_generalize_arg_p_aux : pexpr → parser (pexpr × name) | (app (app (macro _ [const `heq _ ]) h) (local_const x _ _ _)) := pure (h, x) | _ := fail "parse error" private meta def h_generalize_arg_p : parser (pexpr × name) := with_desc "expr == id" $ parser.pexpr 0 >>= h_generalize_arg_p_aux /-- `h_generalize Hx : e == x` matches on `cast _ e` in the goal and replaces it with `x`. It also adds `Hx : e == x` as an assumption. If `cast _ e` appears multiple times (not necessarily with the same proof), they are all replaced by `x`. `cast` `eq.mp`, `eq.mpr`, `eq.subst`, `eq.substr`, `eq.rec` and `eq.rec_on` are all treated as casts. - `h_generalize Hx : e == x with h` adds hypothesis `α = β` with `e : α, x : β`; - `h_generalize Hx : e == x with _` chooses automatically chooses the name of assumption `α = β`; - `h_generalize! Hx : e == x` reverts `Hx`; - when `Hx` is omitted, assumption `Hx : e == x` is not added. -/ meta def h_generalize (rev : parse (tk "!")?) (h : parse ident_?) (_ : parse (tk ":")) (arg : parse h_generalize_arg_p) (eqs_h : parse ( (tk "with" *> pure <$> ident_) <|> pure [])) : tactic unit := do let (e,n) := arg, let h' := if h = `_ then none else h, h' ← (h' : tactic name) <|> get_unused_name ("h" ++ n.to_string : string), e ← to_expr e, tgt ← target, ((e,x,eq_h)::es) ← list_cast_of e tgt | fail "no cast found", interactive.generalize h' () (to_pexpr e, n), asm ← get_local h', v ← get_local n, hs ← es.mmap (λ ⟨e,_⟩, mk_app `eq [e,v]), (eqs_h.zip [e]).mmap' (λ ⟨h,e⟩, do h ← if h ≠ `_ then pure h else get_unused_name `h, () <$ note h none eq_h ), hs.mmap' (λ h, do h' ← assert `h h, tactic.exact asm, try (rewrite_target h'), tactic.clear h' ), when h.is_some (do (to_expr ``(heq_of_eq_rec_left %%eq_h %%asm) <|> to_expr ``(heq_of_cast_eq %%eq_h %%asm)) >>= note h' none >> pure ()), tactic.clear asm, when rev.is_some (interactive.revert [n]) add_tactic_doc { name := "h_generalize", category := doc_category.tactic, decl_names := [`tactic.interactive.h_generalize], tags := ["context management"] } /-- Tests whether `t` is definitionally equal to `p`. The difference with `guard_expr_eq` is that this uses definitional equality instead of alpha-equivalence. -/ meta def guard_expr_eq' (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, is_def_eq t e /-- `guard_target' t` fails if the target of the main goal is not definitionally equal to `t`. We use this tactic for writing tests. The difference with `guard_target` is that this uses definitional equality instead of alpha-equivalence. -/ meta def guard_target' (p : parse texpr) : tactic unit := do t ← target, guard_expr_eq' t p add_tactic_doc { name := "guard_target'", category := doc_category.tactic, decl_names := [`tactic.interactive.guard_target'], tags := ["testing"] } /-- Tries to solve the goal using a canonical proof of `true` or the `reflexivity` tactic. Unlike `trivial` or `trivial'`, does not the `contradiction` tactic. -/ meta def triv : tactic unit := tactic.triv <|> tactic.reflexivity <|> fail "triv tactic failed" add_tactic_doc { name := "triv", category := doc_category.tactic, decl_names := [`tactic.interactive.triv], tags := ["finishing"] } /-- A weaker version of `trivial` that tries to solve the goal using a canonical proof of `true` or the `reflexivity` tactic (unfolding only `reducible` constants, so can fail faster than `trivial`), and otherwise tries the `contradiction` tactic. -/ meta def trivial' : tactic unit := tactic.triv' <|> tactic.reflexivity reducible <|> tactic.contradiction <|> fail "trivial' tactic failed" add_tactic_doc { name := "trivial'", category := doc_category.tactic, decl_names := [`tactic.interactive.trivial'], tags := ["finishing"] } /-- Similar to `existsi`. `use x` will instantiate the first term of an `∃` or `Σ` goal with `x`. It will then try to close the new goal using `trivial'`, or try to simplify it by applying `exists_prop`. Unlike `existsi`, `x` is elaborated with respect to the expected type. `use` will alternatively take a list of terms `[x0, ..., xn]`. `use` will work with constructors of arbitrary inductive types. Examples: ```lean example (α : Type) : ∃ S : set α, S = S := by use ∅ example : ∃ x : ℤ, x = x := by use 42 example : ∃ n > 0, n = n := begin use 1, -- goal is now 1 > 0 ∧ 1 = 1, whereas it would be ∃ (H : 1 > 0), 1 = 1 after existsi 1. exact ⟨zero_lt_one, rfl⟩, end example : ∃ a b c : ℤ, a + b + c = 6 := by use [1, 2, 3] example : ∃ p : ℤ × ℤ, p.1 = 1 := by use ⟨1, 42⟩ example : Σ x y : ℤ, (ℤ × ℤ) × ℤ := by use [1, 2, 3, 4, 5] inductive foo | mk : ℕ → bool × ℕ → ℕ → foo example : foo := by use [100, tt, 4, 3] ``` -/ meta def use (l : parse pexpr_list_or_texpr) : tactic unit := focus1 $ tactic.use l; try (trivial' <|> (do `(Exists %%p) ← target, to_expr ``(exists_prop.mpr) >>= tactic.apply >> skip)) add_tactic_doc { name := "use", category := doc_category.tactic, decl_names := [`tactic.interactive.use, `tactic.interactive.existsi], tags := ["logic"], inherit_description_from := `tactic.interactive.use } /-- `clear_aux_decl` clears every `aux_decl` in the local context for the current goal. This includes the induction hypothesis when using the equation compiler and `_let_match` and `_fun_match`. It is useful when using a tactic such as `finish`, `simp *` or `subst` that may use these auxiliary declarations, and produce an error saying the recursion is not well founded. ```lean example (n m : ℕ) (h₁ : n = m) (h₂ : ∃ a : ℕ, a = n ∧ a = m) : 2 * m = 2 * n := let ⟨a, ha⟩ := h₂ in begin clear_aux_decl, -- subst will fail without this line subst h₁ end example (x y : ℕ) (h₁ : ∃ n : ℕ, n * 1 = 2) (h₂ : 1 + 1 = 2 → x * 1 = y) : x = y := let ⟨n, hn⟩ := h₁ in begin clear_aux_decl, -- finish produces an error without this line finish end ``` -/ meta def clear_aux_decl : tactic unit := tactic.clear_aux_decl add_tactic_doc { name := "clear_aux_decl", category := doc_category.tactic, decl_names := [`tactic.interactive.clear_aux_decl, `tactic.clear_aux_decl], tags := ["context management"], inherit_description_from := `tactic.interactive.clear_aux_decl } meta def loc.get_local_pp_names : loc → tactic (list name) | loc.wildcard := list.map expr.local_pp_name <$> local_context | (loc.ns l) := return l.reduce_option meta def loc.get_local_uniq_names (l : loc) : tactic (list name) := list.map expr.local_uniq_name <$> l.get_locals /-- The logic of `change x with y at l` fails when there are dependencies. `change'` mimics the behavior of `change`, except in the case of `change x with y at l`. In this case, it will correctly replace occurences of `x` with `y` at all possible hypotheses in `l`. As long as `x` and `y` are defeq, it should never fail. -/ meta def change' (q : parse texpr) : parse (tk "with" *> texpr)? → parse location → tactic unit | none (loc.ns [none]) := do e ← i_to_expr q, change_core e none | none (loc.ns [some h]) := do eq ← i_to_expr q, eh ← get_local h, change_core eq (some eh) | none _ := fail "change-at does not support multiple locations" | (some w) l := do l' ← loc.get_local_pp_names l, l'.mmap' (λ e, try (change_with_at q w e)), when l.include_goal $ change q w (loc.ns [none]) add_tactic_doc { name := "change'", category := doc_category.tactic, decl_names := [`tactic.interactive.change', `tactic.interactive.change], tags := ["renaming"], inherit_description_from := `tactic.interactive.change' } private meta def opt_dir_with : parser (option (bool × name)) := (tk "with" *> ((λ arrow h, (option.is_some arrow, h)) <$> (tk "<-")? <*> ident))? /-- `set a := t with h` is a variant of `let a := t`. It adds the hypothesis `h : a = t` to the local context and replaces `t` with `a` everywhere it can. `set a := t with ←h` will add `h : t = a` instead. `set! a := t with h` does not do any replacing. ```lean example (x : ℕ) (h : x = 3) : x + x + x = 9 := begin set y := x with ←h_xy, /- x : ℕ, y : ℕ := x, h_xy : x = y, h : y = 3 ⊢ y + y + y = 9 -/ end ``` -/ meta def set (h_simp : parse (tk "!")?) (a : parse ident) (tp : parse ((tk ":") *> texpr)?) (_ : parse (tk ":=")) (pv : parse texpr) (rev_name : parse opt_dir_with) := do tp ← i_to_expr $ let t := tp.get_or_else pexpr.mk_placeholder in ``(%%t : Sort*), pv ← to_expr ``(%%pv : %%tp), tp ← instantiate_mvars tp, definev a tp pv, when h_simp.is_none $ change' ``(%%pv) (some (expr.const a [])) $ interactive.loc.wildcard, match rev_name with | some (flip, id) := do nv ← get_local a, mk_app `eq (cond flip [pv, nv] [nv, pv]) >>= assert id, reflexivity | none := skip end add_tactic_doc { name := "set", category := doc_category.tactic, decl_names := [`tactic.interactive.set], tags := ["context management"] } /-- `clear_except h₀ h₁` deletes all the assumptions it can except for `h₀` and `h₁`. -/ meta def clear_except (xs : parse ident *) : tactic unit := do n ← xs.mmap (try_core ∘ get_local) >>= revert_lst ∘ list.filter_map id, ls ← local_context, ls.reverse.mmap' $ try ∘ tactic.clear, intron_no_renames n add_tactic_doc { name := "clear_except", category := doc_category.tactic, decl_names := [`tactic.interactive.clear_except], tags := ["context management"] } meta def format_names (ns : list name) : format := format.join $ list.intersperse " " (ns.map to_fmt) private meta def indent_bindents (l r : string) : option (list name) → expr → tactic format | none e := do e ← pp e, pformat!"{l}{format.nest l.length e}{r}" | (some ns) e := do e ← pp e, let ns := format_names ns, let margin := l.length + ns.to_string.length + " : ".length, pformat!"{l}{ns} : {format.nest margin e}{r}" private meta def format_binders : list name × binder_info × expr → tactic format | (ns, binder_info.default, t) := indent_bindents "(" ")" ns t | (ns, binder_info.implicit, t) := indent_bindents "{" "}" ns t | (ns, binder_info.strict_implicit, t) := indent_bindents "⦃" "⦄" ns t | ([n], binder_info.inst_implicit, t) := if "_".is_prefix_of n.to_string then indent_bindents "[" "]" none t else indent_bindents "[" "]" [n] t | (ns, binder_info.inst_implicit, t) := indent_bindents "[" "]" ns t | (ns, binder_info.aux_decl, t) := indent_bindents "(" ")" ns t private meta def partition_vars' (s : name_set) : list expr → list expr → list expr → tactic (list expr × list expr) | [] as bs := pure (as.reverse, bs.reverse) | (x :: xs) as bs := do t ← infer_type x, if t.has_local_in s then partition_vars' xs as (x :: bs) else partition_vars' xs (x :: as) bs private meta def partition_vars : tactic (list expr × list expr) := do ls ← local_context, partition_vars' (name_set.of_list $ ls.map expr.local_uniq_name) ls [] [] /-- Format the current goal as a stand-alone example. Useful for testing tactics or creating [minimal working examples](https://leanprover-community.github.io/mwe.html). * `extract_goal`: formats the statement as an `example` declaration * `extract_goal my_decl`: formats the statement as a `lemma` or `def` declaration called `my_decl` * `extract_goal with i j k:` only use local constants `i`, `j`, `k` in the declaration Examples: ```lean example (i j k : ℕ) (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k := begin extract_goal, -- prints: -- example (i j k : ℕ) (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k := -- begin -- admit, -- end extract_goal my_lemma -- prints: -- lemma my_lemma (i j k : ℕ) (h₀ : i ≤ j) (h₁ : j ≤ k) : i ≤ k := -- begin -- admit, -- end end example {i j k x y z w p q r m n : ℕ} (h₀ : i ≤ j) (h₁ : j ≤ k) (h₁ : k ≤ p) (h₁ : p ≤ q) : i ≤ k := begin extract_goal my_lemma, -- prints: -- lemma my_lemma {i j k x y z w p q r m n : ℕ} -- (h₀ : i ≤ j) -- (h₁ : j ≤ k) -- (h₁ : k ≤ p) -- (h₁ : p ≤ q) : -- i ≤ k := -- begin -- admit, -- end extract_goal my_lemma with i j k -- prints: -- lemma my_lemma {p i j k : ℕ} -- (h₀ : i ≤ j) -- (h₁ : j ≤ k) -- (h₁ : k ≤ p) : -- i ≤ k := -- begin -- admit, -- end end example : true := begin let n := 0, have m : ℕ, admit, have k : fin n, admit, have : n + m + k.1 = 0, extract_goal, -- prints: -- example (m : ℕ) : let n : ℕ := 0 in ∀ (k : fin n), n + m + k.val = 0 := -- begin -- intros n k, -- admit, -- end end ``` -/ meta def extract_goal (print_use : parse $ (tk "!" *> pure tt) <|> pure ff) (n : parse ident?) (vs : parse (tk "with" *> ident*)?) : tactic unit := do tgt ← target, solve_aux tgt $ do { ((cxt₀,cxt₁,ls,tgt),_) ← solve_aux tgt $ do { vs.mmap clear_except, ls ← local_context, ls ← ls.mfilter $ succeeds ∘ is_local_def, n ← revert_lst ls, (c₀,c₁) ← partition_vars, tgt ← target, ls ← intron' n, pure (c₀,c₁,ls,tgt) }, is_prop ← is_prop tgt, let title := match n, is_prop with | none, _ := to_fmt "example" | (some n), tt := format!"lemma {n}" | (some n), ff := format!"def {n}" end, cxt₀ ← compact_decl cxt₀ >>= list.mmap format_binders, cxt₁ ← compact_decl cxt₁ >>= list.mmap format_binders, stmt ← pformat!"{tgt} :=", let fmt := format.group $ format.nest 2 $ title ++ cxt₀.foldl (λ acc x, acc ++ format.group (format.line ++ x)) "" ++ format.join (list.map (λ x, format.line ++ x) cxt₁) ++ " :" ++ format.line ++ stmt, trace $ fmt.to_string $ options.mk.set_nat `pp.width 80, let var_names := format.intercalate " " $ ls.map (to_fmt ∘ local_pp_name), let call_intron := if ls.empty then to_fmt "" else format!"\n intros {var_names},", trace!"begin{call_intron}\n admit,\nend\n" }, skip add_tactic_doc { name := "extract_goal", category := doc_category.tactic, decl_names := [`tactic.interactive.extract_goal], tags := ["goal management", "proof extraction", "debugging"] } /-- `inhabit α` tries to derive a `nonempty α` instance and then upgrades this to an `inhabited α` instance. If the target is a `Prop`, this is done constructively; otherwise, it uses `classical.choice`. ```lean example (α) [nonempty α] : ∃ a : α, true := begin inhabit α, existsi default, trivial end ``` -/ meta def inhabit (t : parse parser.pexpr) (inst_name : parse ident?) : tactic unit := do ty ← i_to_expr t, nm ← returnopt inst_name <|> get_unused_name `inst, tgt ← target, tgt_is_prop ← is_prop tgt, if tgt_is_prop then do decorate_error "could not infer nonempty instance:" $ mk_mapp ``nonempty.elim_to_inhabited [ty, none, tgt] >>= tactic.apply, introI nm else do decorate_error "could not infer nonempty instance:" $ mk_mapp ``classical.inhabited_of_nonempty' [ty, none] >>= note nm none, resetI add_tactic_doc { name := "inhabit", category := doc_category.tactic, decl_names := [`tactic.interactive.inhabit], tags := ["context management", "type class"] } /-- `revert_deps n₁ n₂ ...` reverts all the hypotheses that depend on one of `n₁, n₂, ...` It does not revert `n₁, n₂, ...` themselves (unless they depend on another `nᵢ`). -/ meta def revert_deps (ns : parse ident*) : tactic unit := propagate_tags $ ns.mmap get_local >>= revert_reverse_dependencies_of_hyps >> skip add_tactic_doc { name := "revert_deps", category := doc_category.tactic, decl_names := [`tactic.interactive.revert_deps], tags := ["context management", "goal management"] } /-- `revert_after n` reverts all the hypotheses after `n`. -/ meta def revert_after (n : parse ident) : tactic unit := propagate_tags $ get_local n >>= tactic.revert_after >> skip add_tactic_doc { name := "revert_after", category := doc_category.tactic, decl_names := [`tactic.interactive.revert_after], tags := ["context management", "goal management"] } /-- Reverts all local constants on which the target depends (recursively). -/ meta def revert_target_deps : tactic unit := propagate_tags $ tactic.revert_target_deps >> skip add_tactic_doc { name := "revert_target_deps", category := doc_category.tactic, decl_names := [`tactic.interactive.revert_target_deps], tags := ["context management", "goal management"] } /-- `clear_value n₁ n₂ ...` clears the bodies of the local definitions `n₁, n₂ ...`, changing them into regular hypotheses. A hypothesis `n : α := t` is changed to `n : α`. -/ meta def clear_value (ns : parse ident*) : tactic unit := propagate_tags $ ns.reverse.mmap get_local >>= tactic.clear_value add_tactic_doc { name := "clear_value", category := doc_category.tactic, decl_names := [`tactic.interactive.clear_value], tags := ["context management"] } /-- `generalize' : e = x` replaces all occurrences of `e` in the target with a new hypothesis `x` of the same type. `generalize' h : e = x` in addition registers the hypothesis `h : e = x`. `generalize'` is similar to `generalize`. The difference is that `generalize' : e = x` also succeeds when `e` does not occur in the goal. It is similar to `set`, but the resulting hypothesis `x` is not a local definition. -/ meta def generalize' (h : parse ident?) (_ : parse $ tk ":") (p : parse generalize_arg_p) : tactic unit := propagate_tags $ do let (p, x) := p, e ← i_to_expr p, some h ← pure h | tactic.generalize' e x >> skip, -- `h` is given, the regular implementation of `generalize` works. tgt ← target, tgt' ← do { ⟨tgt', _⟩ ← solve_aux tgt (tactic.generalize e x >> target), to_expr ``(Π x, %%e = x → %%(tgt'.binding_body.lift_vars 0 1)) } <|> to_expr ``(Π x, %%e = x → %%tgt), t ← assert h tgt', swap, exact ``(%%t %%e rfl), intro x, intro h add_tactic_doc { name := "generalize'", category := doc_category.tactic, decl_names := [`tactic.interactive.generalize'], tags := ["context management"] } /-- If the expression `q` is a local variable with type `x = t` or `t = x`, where `x` is a local constant, `tactic.interactive.subst' q` substitutes `x` by `t` everywhere in the main goal and then clears `q`. If `q` is another local variable, then we find a local constant with type `q = t` or `t = q` and substitute `t` for `q`. Like `tactic.interactive.subst`, but fails with a nicer error message if the substituted variable is a local definition. It is trickier to fix this in core, since `tactic.is_local_def` is in mathlib. -/ meta def subst' (q : parse texpr) : tactic unit := do i_to_expr q >>= tactic.subst' >> try (tactic.reflexivity reducible) add_tactic_doc { name := "subst'", category := doc_category.tactic, decl_names := [`tactic.interactive.subst'], tags := ["context management"] } end interactive end tactic
36671467ef4e94146043dfc4c5213838e92cb8de
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/char/lemmas.lean
b636655ab0391c9b9876645d39025a4155848321
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,088
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.default import Mathlib.Lean3Lib.init.logic import Mathlib.Lean3Lib.init.data.nat.lemmas import Mathlib.Lean3Lib.init.data.char.basic namespace Mathlib namespace char theorem val_of_nat_eq_of_is_valid {n : ℕ} : is_valid_char n → val (of_nat n) = n := sorry theorem val_of_nat_eq_of_not_is_valid {n : ℕ} : ¬is_valid_char n → val (of_nat n) = 0 := sorry theorem of_nat_eq_of_not_is_valid {n : ℕ} : ¬is_valid_char n → of_nat n = of_nat 0 := fun (h : ¬is_valid_char n) => eq_of_veq (eq.mpr (id (Eq._oldrec (Eq.refl (val (of_nat n) = val (of_nat 0))) (val_of_nat_eq_of_not_is_valid h))) (Eq.refl 0)) theorem of_nat_ne_of_ne {n₁ : ℕ} {n₂ : ℕ} (h₁ : n₁ ≠ n₂) (h₂ : is_valid_char n₁) (h₃ : is_valid_char n₂) : of_nat n₁ ≠ of_nat n₂ := ne_of_vne (eq.mpr (id (Eq._oldrec (Eq.refl (val (of_nat n₁) ≠ val (of_nat n₂))) (val_of_nat_eq_of_is_valid h₂))) (eq.mpr (id (Eq._oldrec (Eq.refl (n₁ ≠ val (of_nat n₂))) (val_of_nat_eq_of_is_valid h₃))) h₁))
be0b56a24039dc2a491222f591521e94567d6387
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/polynomial/erase_lead_auto.lean
57e04149f78f36bdbbfd070a235a9436e5c88a82
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
6,561
lean
/- Copyright (c) 2020 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.polynomial.degree.default import Mathlib.data.polynomial.degree.trailing_degree import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Erase the leading term of a univariate polynomial ## Definition * `erase_lead f`: the polynomial `f - leading term of f` `erase_lead` serves as reduction step in an induction, shaving off one monomial from a polynomial. The definition is set up so that it does not mention subtraction in the definition, and thus works for polynomials over semirings as well as rings. -/ namespace polynomial /-- `erase_lead f` for a polynomial `f` is the polynomial obtained by subtracting from `f` the leading term of `f`. -/ def erase_lead {R : Type u_1} [semiring R] (f : polynomial R) : polynomial R := finsupp.erase (nat_degree f) f theorem erase_lead_support {R : Type u_1} [semiring R] (f : polynomial R) : finsupp.support (erase_lead f) = finset.erase (finsupp.support f) (nat_degree f) := sorry -- `rfl` fails because LHS uses `nat.decidable_eq` but RHS is classical. theorem erase_lead_coeff {R : Type u_1} [semiring R] {f : polynomial R} (i : ℕ) : coeff (erase_lead f) i = ite (i = nat_degree f) 0 (coeff f i) := sorry -- `rfl` fails because LHS uses `nat.decidable_eq` but RHS is classical. @[simp] theorem erase_lead_coeff_nat_degree {R : Type u_1} [semiring R] {f : polynomial R} : coeff (erase_lead f) (nat_degree f) = 0 := finsupp.erase_same theorem erase_lead_coeff_of_ne {R : Type u_1} [semiring R] {f : polynomial R} (i : ℕ) (hi : i ≠ nat_degree f) : coeff (erase_lead f) i = coeff f i := finsupp.erase_ne hi @[simp] theorem erase_lead_zero {R : Type u_1} [semiring R] : erase_lead 0 = 0 := finsupp.erase_zero (nat_degree 0) @[simp] theorem erase_lead_add_monomial_nat_degree_leading_coeff {R : Type u_1} [semiring R] (f : polynomial R) : erase_lead f + coe_fn (monomial (nat_degree f)) (leading_coeff f) = f := sorry @[simp] theorem erase_lead_add_C_mul_X_pow {R : Type u_1} [semiring R] (f : polynomial R) : erase_lead f + coe_fn C (leading_coeff f) * X ^ nat_degree f = f := sorry @[simp] theorem self_sub_monomial_nat_degree_leading_coeff {R : Type u_1} [ring R] (f : polynomial R) : f - coe_fn (monomial (nat_degree f)) (leading_coeff f) = erase_lead f := Eq.symm (iff.mpr eq_sub_iff_add_eq (erase_lead_add_monomial_nat_degree_leading_coeff f)) @[simp] theorem self_sub_C_mul_X_pow {R : Type u_1} [ring R] (f : polynomial R) : f - coe_fn C (leading_coeff f) * X ^ nat_degree f = erase_lead f := sorry theorem erase_lead_ne_zero {R : Type u_1} [semiring R] {f : polynomial R} (f0 : bit0 1 ≤ finset.card (finsupp.support f)) : erase_lead f ≠ 0 := sorry @[simp] theorem nat_degree_not_mem_erase_lead_support {R : Type u_1} [semiring R] {f : polynomial R} : ¬nat_degree f ∈ finsupp.support (erase_lead f) := sorry theorem ne_nat_degree_of_mem_erase_lead_support {R : Type u_1} [semiring R] {f : polynomial R} {a : ℕ} (h : a ∈ finsupp.support (erase_lead f)) : a ≠ nat_degree f := sorry theorem erase_lead_support_card_lt {R : Type u_1} [semiring R] {f : polynomial R} (h : f ≠ 0) : finset.card (finsupp.support (erase_lead f)) < finset.card (finsupp.support f) := sorry theorem erase_lead_card_support {R : Type u_1} [semiring R] {f : polynomial R} {c : ℕ} (fc : finset.card (finsupp.support f) = c) : finset.card (finsupp.support (erase_lead f)) = c - 1 := sorry theorem erase_lead_card_support' {R : Type u_1} [semiring R] {f : polynomial R} {c : ℕ} (fc : finset.card (finsupp.support f) = c + 1) : finset.card (finsupp.support (erase_lead f)) = c := erase_lead_card_support fc @[simp] theorem erase_lead_monomial {R : Type u_1} [semiring R] (i : ℕ) (r : R) : erase_lead (coe_fn (monomial i) r) = 0 := sorry @[simp] theorem erase_lead_C {R : Type u_1} [semiring R] (r : R) : erase_lead (coe_fn C r) = 0 := erase_lead_monomial 0 r @[simp] theorem erase_lead_X {R : Type u_1} [semiring R] : erase_lead X = 0 := erase_lead_monomial 1 1 @[simp] theorem erase_lead_X_pow {R : Type u_1} [semiring R] (n : ℕ) : erase_lead (X ^ n) = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (erase_lead (X ^ n) = 0)) (X_pow_eq_monomial n))) (eq.mpr (id (Eq._oldrec (Eq.refl (erase_lead (coe_fn (monomial n) 1) = 0)) (erase_lead_monomial n 1))) (Eq.refl 0)) @[simp] theorem erase_lead_C_mul_X_pow {R : Type u_1} [semiring R] (r : R) (n : ℕ) : erase_lead (coe_fn C r * X ^ n) = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (erase_lead (coe_fn C r * X ^ n) = 0)) (C_mul_X_pow_eq_monomial r n))) (eq.mpr (id (Eq._oldrec (Eq.refl (erase_lead (coe_fn (monomial n) r) = 0)) (erase_lead_monomial n r))) (Eq.refl 0)) theorem erase_lead_degree_le {R : Type u_1} [semiring R] {f : polynomial R} : degree (erase_lead f) ≤ degree f := sorry theorem erase_lead_nat_degree_le {R : Type u_1} [semiring R] {f : polynomial R} : nat_degree (erase_lead f) ≤ nat_degree f := nat_degree_le_nat_degree erase_lead_degree_le theorem erase_lead_nat_degree_lt {R : Type u_1} [semiring R] {f : polynomial R} (f0 : bit0 1 ≤ finset.card (finsupp.support f)) : nat_degree (erase_lead f) < nat_degree f := lt_of_le_of_ne erase_lead_nat_degree_le (ne_nat_degree_of_mem_erase_lead_support (nat_degree_mem_support_of_nonzero (erase_lead_ne_zero f0))) theorem erase_lead_nat_degree_lt_or_erase_lead_eq_zero {R : Type u_1} [semiring R] (f : polynomial R) : nat_degree (erase_lead f) < nat_degree f ∨ erase_lead f = 0 := sorry /-- An induction lemma for polynomials. It takes a natural number `N` as a parameter, that is required to be at least as big as the `nat_degree` of the polynomial. This is useful to prove results where you want to change each term in a polynomial to something else depending on the `nat_degree` of the polynomial itself and not on the specific `nat_degree` of each term. -/ theorem induction_with_nat_degree_le {R : Type u_1} [semiring R] {P : polynomial R → Prop} (N : ℕ) (P_0 : P 0) (P_C_mul_pow : ∀ (n : ℕ) (r : R), r ≠ 0 → n ≤ N → P (coe_fn C r * X ^ n)) (P_C_add : ∀ (f g : polynomial R), nat_degree f ≤ N → nat_degree g ≤ N → P f → P g → P (f + g)) (f : polynomial R) : nat_degree f ≤ N → P f := sorry end Mathlib
61d9e8bdb438ebe724baa3f108e2235b4951f1c8
7b66d83f3b69dae0a3dfb684d7ebe5e9e3f3c913
/src/exercises_sources/thursday/afternoon/category_theory/exercise5.lean
9cd1fa5349b222a76d8231425a05b33490e81c61
[]
permissive
dpochekutov/lftcm2020
58a09e10f0e638075b97884d3c2612eb90296adb
cdfbf1ac089f21058e523db73f2476c9c98ed16a
refs/heads/master
1,669,226,265,076
1,594,629,725,000
1,594,629,725,000
279,213,346
1
0
MIT
1,594,622,757,000
1,594,615,843,000
null
UTF-8
Lean
false
false
1,334
lean
import category_theory.limits.shapes.biproducts /-! Let's show that every preadditive category embeds into a preadditive category with biproducts, and identify a good universal property. -/ universes v u variables (C : Type u) structure family := (ι : Type v) [fintype : fintype ι] [decidable_eq : decidable_eq ι] (val : ι → C) variables {C} def dmatrix {X Y : family C} (Z : X.ι → Y.ι → Type*) := Π (i : X.ι) (j : Y.ι), Z i j open category_theory variables [category.{v} C] [preadditive C] namespace family def hom (X Y : family C) := dmatrix (λ i j, X.val i ⟶ Y.val j) instance : category.{v (max u (v+1))} (family.{v} C) := { hom := hom, id := sorry, comp := sorry, id_comp' := sorry, comp_id' := sorry, assoc' := sorry, } variables (C) def embedding : C ⥤ family.{v} C := sorry lemma embedding.faithful : faithful (embedding C) := sorry instance : preadditive (family.{v} C) := sorry open category_theory.limits instance : has_finite_biproducts (family.{v} C) := sorry variables {C} def factor {D : Type u} [category.{v} D] [preadditive D] [has_finite_biproducts D] (F : C ⥤ D) : family.{v} C ⥤ D := sorry def factor_factorisation {D : Type u} [category.{v} D] [preadditive D] [has_finite_biproducts D] (F : C ⥤ D) : F ≅ embedding C ⋙ factor F := sorry end family
bf95a3d38e7fc5f0bb044ffc374ca440a9531a80
67190c9aacc0cac64fb4463d93e84c696a5be896
/Lists of exercises/List 7/cap16-LucasDomingues.lean
5be2f3b3cb4eabfeab1df02304755a835f203537
[]
no_license
lucasresck/Discrete-Mathematics
ffbaf55943e7ce2c7bc50cef7e3ef66a0212f738
0a08081c5f393e5765259d3f1253c3a6dd043dac
refs/heads/master
1,596,627,857,734
1,573,411,500,000
1,573,411,500,000
212,489,764
0
0
null
null
null
null
UTF-8
Lean
false
false
4,951
lean
-- Estudante: Lucas Emanuel Resck Domingues import data.set -- Exercise 1 section open function int algebra def f (x : ℤ) : ℤ := x + 3 def g (x : ℤ) : ℤ := -x def h (x : ℤ) : ℤ := 2 * x + 3 example : injective f := assume x1 x2, assume h1 : x1 + 3 = x2 + 3, -- Lean knows this is the same as f x1 = f x2 show x1 = x2, from eq_of_add_eq_add_right h1 example : surjective f := assume y, have h1 : f (y - 3) = y, from calc f (y - 3) = (y - 3) + 3 : rfl ... = y : by rw sub_add_cancel, show ∃ x, f x = y, from exists.intro (y - 3) h1 example (x y : ℤ) (h : 2 * x = 2 * y) : x = y := have h1 : 2 ≠ (0 : ℤ), from dec_trivial, -- this tells Lean to figure it out itself show x = y, from eq_of_mul_eq_mul_left h1 h example (x : ℤ) : -(-x) = x := neg_neg x example (A B : Type) (u : A → B) (v : B → A) (h : left_inverse u v) : ∀ x, u (v x) = x := h example (A B : Type) (u : A → B) (v : B → A) (h : left_inverse u v) : right_inverse v u := h -- fill in the sorry's in the following proofs example : injective h := begin intros a b h1, have h2, from eq_of_add_eq_add_right h1, have h3 : 2 ≠ (0 : ℤ), from dec_trivial, have h4, from eq_of_mul_eq_mul_left h3 h2, assumption end example : surjective g := show ∀ y, ∃ x, g x = y, from assume y, have h1 : g (-y) = y, from calc g (-y) = -(-y) : rfl ... = y : neg_neg y, show ∃ x, g x = y, from exists.intro (-y) h1 example (A B : Type) (u : A → B) (v1 : B → A) (v2 : B → A) (h1 : left_inverse v1 u) (h2 : right_inverse v2 u) : v1 = v2 := funext (assume x, calc v1 x = v1 (u (v2 x)) : by rw h2 ... = v2 x : by rw h1) end -- Exercise 2 section open function set variables {X Y : Type} variable f : X → Y variables A B : set X example : f '' (A ∪ B) = f '' A ∪ f '' B := eq_of_subset_of_subset (assume y, assume h1 : y ∈ f '' (A ∪ B), exists.elim h1 $ assume x h, have h2 : x ∈ A ∪ B, from h.left, have h3 : f x = y, from h.right, or.elim h2 (assume h4 : x ∈ A, have h5 : y ∈ f '' A, from ⟨x, h4, h3⟩, show y ∈ f '' A ∪ f '' B, from or.inl h5) (assume h4 : x ∈ B, have h5 : y ∈ f '' B, from ⟨x, h4, h3⟩, show y ∈ f '' A ∪ f '' B, from or.inr h5)) (assume y, assume h2 : y ∈ f '' A ∪ f '' B, or.elim h2 (assume h3 : y ∈ f '' A, exists.elim h3 $ assume x h, have h4 : x ∈ A, from h.left, have h5 : f x = y, from h.right, have h6 : x ∈ A ∪ B, from or.inl h4, show y ∈ f '' (A ∪ B), from ⟨x, h6, h5⟩) (assume h3 : y ∈ f '' B, exists.elim h3 $ assume x h, have h4 : x ∈ B, from h.left, have h5 : f x = y, from h.right, have h6 : x ∈ A ∪ B, from or.inr h4, show y ∈ f '' (A ∪ B), from ⟨x, h6, h5⟩)) -- remember, x ∈ A ∩ B is the same as x ∈ A ∧ x ∈ B example (x : X) (h1 : x ∈ A) (h2 : x ∈ B) : x ∈ A ∩ B := and.intro h1 h2 example (x : X) (h1 : x ∈ A ∩ B) : x ∈ A := and.left h1 -- Fill in the proof below. -- (It should take about 8 lines.) example : f '' (A ∩ B) ⊆ f '' A ∩ f '' B := assume y, assume h1 : y ∈ f '' (A ∩ B), show y ∈ f '' A ∩ f '' B, from begin cases h1 with x h2, cases h2 with h3 h4, cases h3 with h5 h6, apply and.intro, have h7, from exists.intro x (and.intro h5 h4), exact h7, have h7, from exists.intro x (and.intro h6 h4), exact h7 end example : f '' (A ∩ B) ⊆ f '' A ∩ f '' B := assume y, assume h1 : y ∈ f '' (A ∩ B), have h3 : ∃ x, x ∈ A ∧ f x = y, from exists.elim h1 (assume x (h5 : x ∈ A ∩ B ∧ f x = y), exists.intro x (and.intro h5.left.left h5.right)), have h4 : ∃ x, x ∈ B ∧ f x = y, from exists.elim h1 (assume x (h5 : x ∈ A ∩ B ∧ f x = y), exists.intro x (and.intro h5.left.right h5.right)), show y ∈ f '' A ∩ f '' B, from and.intro h3 h4 end
50a1b191dced795efde6c82fe5cac7a87d5c6e17
83d56f27a2d4619301ce53e12f816410643fe9fd
/lean/Cyclone_Formal_Syntax.lean
808e84e46c8346a36cbfc2d4774fc2be693ec7cc
[]
no_license
briangmilnes/CycloneCoqSemantics
cad5d09328fd70876d11f79b81d30954c7db6a60
190c0fc57d5aebfde244efb06a119f108de7a150
refs/heads/master
1,621,135,368,013
1,609,910,991,000
1,609,910,991,000
26,649,090
0
0
null
null
null
null
UTF-8
Lean
false
false
4,283
lean
/- Rewriting the Coq Cyclone Formal Syntax in Lean. -/ import .LeanLib /- TODO Lean 4 has enumerations, should I be using it? -/ inductive Kappa : Type | B /- 'boxed' kind. -/ | A /- 'any' kind. -/ export Kappa inductive Phi : Type | witnesschanges /- Allowing witness changes. \delta -/ | aliases /- Allowing aliases as the opened type. \amp -/ export Phi /- Path elements -/ @[derive decidable_eq] inductive IPE: Type | zero_pe | one_pe export IPE @[derive decidable_eq] inductive PE : Type | i_pe : IPE → PE | u_pe : PE export PE notation `Var` := string inductive V : Type | bevar : nat → V /- A bound expression variable, a de Bruijn index. -/ | fevar : Var → V /- A free expression variable. -/ export V inductive Tau : Type | btvar : nat → Tau /- A bound type variable, a de Bruijn index. -/ | ftvar : Var → Tau /- A free type variable. -/ | cint : Tau /- Cyclone's Integers. -/ | cross : Tau → Tau → Tau /- Pairs. -/ | arrow : Tau → Tau → Tau /- Function type. -/ | ptype : Tau → Tau /- Pointer type. -/ /- | utype : Kappa → Tau → Tau /- Universal type. -/ | etype : Phi → Kappa → Tau → Tau /- Existential type. -/ -/ export Tau mutual inductive St, E, F with St : Type | e_s : E → St /- An expression in a statement. -/ | retn : E → St /- Returns are required in this syntax. -/ | seqx : St → St → St /- Statement sequencing. -/ | if_s : E → St → St → St /- if expression in a statement. -/ | while : E → St → St /- A while statement. -/ | letx : E → St → St /- A let expression. -/ | openx : E → St → St /- open an existential package (elimination) to a value. -/ | openstar : E → St → St /- open an existential package (elimination) with a pointer to the value. -/ with E : Type /- Terms for expressions. -/ | v_e : V → E /- Put a variable into an expression, either bound or free. -/ | i_e : ℤ → E /- An integer value in an expression. -/ | p_e : V → list PE → E /- This is a term that derefences a path into the value of the variable. -/ | f_e : F → E /- A function identifier in an expression or statement. -/ | amp : E → E /- Take the address of an expression. -/ | star : E → E /- Derefence an expression of a pointer type. -/ | cpair : E → E → E /- A pair in an expression. -/ | dot : E → IPE → E /- A deference of a pair. -/ | assign : E → E → E /- Assignment. -/ | appl : E → E → E /- Application expression. app is append. -/ | call : St → E /- A call expression for the semantics use. -/ | inst : E → Tau → E /- Type instantiation, e[\tau]. -/ | pack : Tau → E → Tau → E /- Existential type introduction. -/ with F : Type | dfun : Tau → Tau → St → F /- Function definition. -/ | ufun : Kappa → F → F /- Univerally quantified polymorphic function definition. -/ export St export E export F inductive Term : Type | term_st : St → Term | term_e : E → Term | term_f : F → Term export Term inductive Value : E → Prop | IIsAValue : forall (i : ℤ), Value (E.i_e i) | AmpIsAValue : forall (v : Var) (p : list PE), Value (amp (E.p_e (V.fevar v) p)) | DfunIsAValue : forall (t1 t2 : Tau) (s : St), Value (E.f_e (F.dfun t1 t2 s)) | UfunIsAValue : forall (k : Kappa) (f : F), Value (E.f_e (F.ufun k f)) | PairIsAValue : forall (v0 v1 : E), Value v0 → Value v1 → Value (E.cpair v0 v1) | PackIsAValue : forall (tau tau': Tau) (v : E), Value v → Value (E.pack tau v tau') notation `Path` := list PE notation `Binding` := Var × E notation `Heap` := list Binding notation `State` := Heap × St notation `Delta` := list (Var × Kappa) notation `Gamma` := list (Var × Tau) notation `Upsilon` := list ((Var × Path) × Tau)
785ce6dcf45f677f6fc6b6a2f4537fbb634e1a6d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/convex/segment.lean
ea56f0fa3b164700c20d848d54d10b0bbba366bb
[ "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
20,240
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov, Yaël Dillies -/ import algebra.order.invertible import algebra.order.smul import linear_algebra.affine_space.midpoint import linear_algebra.ray import tactic.positivity /-! # Segments in vector spaces In a 𝕜-vector space, we define the following objects and properties. * `segment 𝕜 x y`: Closed segment joining `x` and `y`. * `open_segment 𝕜 x y`: Open segment joining `x` and `y`. ## Notations We provide the following notation: * `[x -[𝕜] y] = segment 𝕜 x y` in locale `convex` ## TODO Generalize all this file to affine spaces. Should we rename `segment` and `open_segment` to `convex.Icc` and `convex.Ioo`? Should we also define `clopen_segment`/`convex.Ico`/`convex.Ioc`? -/ variables {𝕜 E F : Type*} open set section ordered_semiring variables [ordered_semiring 𝕜] [add_comm_monoid E] section has_smul variables (𝕜) [has_smul 𝕜 E] {s : set E} {x y : E} /-- Segments in a vector space. -/ def segment (x y : E) : set E := {z : E | ∃ (a b : 𝕜) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), a • x + b • y = z} /-- Open segment in a vector space. Note that `open_segment 𝕜 x x = {x}` instead of being `∅` when the base semiring has some element between `0` and `1`. -/ def open_segment (x y : E) : set E := {z : E | ∃ (a b : 𝕜) (ha : 0 < a) (hb : 0 < b) (hab : a + b = 1), a • x + b • y = z} localized "notation (name := segment) `[` x ` -[` 𝕜 `] ` y `]` := segment 𝕜 x y" in convex lemma segment_eq_image₂ (x y : E) : [x -[𝕜] y] = (λ p : 𝕜 × 𝕜, p.1 • x + p.2 • y) '' {p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma open_segment_eq_image₂ (x y : E) : open_segment 𝕜 x y = (λ p : 𝕜 × 𝕜, p.1 • x + p.2 • y) '' {p | 0 < p.1 ∧ 0 < p.2 ∧ p.1 + p.2 = 1} := by simp only [open_segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_symm (x y : E) : [x -[𝕜] y] = [y -[𝕜] x] := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma open_segment_symm (x y : E) : open_segment 𝕜 x y = open_segment 𝕜 y x := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma open_segment_subset_segment (x y : E) : open_segment 𝕜 x y ⊆ [x -[𝕜] y] := λ z ⟨a, b, ha, hb, hab, hz⟩, ⟨a, b, ha.le, hb.le, hab, hz⟩ lemma segment_subset_iff : [x -[𝕜] y] ⊆ s ↔ ∀ a b : 𝕜, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s := ⟨λ H a b ha hb hab, H ⟨a, b, ha, hb, hab, rfl⟩, λ H z ⟨a, b, ha, hb, hab, hz⟩, hz ▸ H a b ha hb hab⟩ lemma open_segment_subset_iff : open_segment 𝕜 x y ⊆ s ↔ ∀ a b : 𝕜, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := ⟨λ H a b ha hb hab, H ⟨a, b, ha, hb, hab, rfl⟩, λ H z ⟨a, b, ha, hb, hab, hz⟩, hz ▸ H a b ha hb hab⟩ end has_smul open_locale convex section mul_action_with_zero variables (𝕜) [mul_action_with_zero 𝕜 E] lemma left_mem_segment (x y : E) : x ∈ [x -[𝕜] y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x -[𝕜] y] := segment_symm 𝕜 y x ▸ left_mem_segment 𝕜 y x end mul_action_with_zero section module variables (𝕜) [module 𝕜 E] {s : set E} {x y z : E} @[simp] lemma segment_same (x : E) : [x -[𝕜] x] = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ h, mem_singleton_iff.1 h ▸ left_mem_segment 𝕜 z z⟩ lemma insert_endpoints_open_segment (x y : E) : insert x (insert y (open_segment 𝕜 x y)) = [x -[𝕜] y] := begin simp only [subset_antisymm_iff, insert_subset, left_mem_segment, right_mem_segment, open_segment_subset_segment, true_and], rintro z ⟨a, b, ha, hb, hab, rfl⟩, refine hb.eq_or_gt.imp _ (λ hb', ha.eq_or_gt.imp _ $ λ ha', _), { rintro rfl, rw [← add_zero a, hab, one_smul, zero_smul, add_zero] }, { rintro rfl, rw [← zero_add b, hab, one_smul, zero_smul, zero_add] }, { exact ⟨a, b, ha', hb', hab, rfl⟩ } end variables {𝕜} lemma mem_open_segment_of_ne_left_right (hx : x ≠ z) (hy : y ≠ z) (hz : z ∈ [x -[𝕜] y]) : z ∈ open_segment 𝕜 x y := begin rw [←insert_endpoints_open_segment] at hz, exact ((hz.resolve_left hx.symm).resolve_left hy.symm) end lemma open_segment_subset_iff_segment_subset (hx : x ∈ s) (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s ↔ [x -[𝕜] y] ⊆ s := by simp only [←insert_endpoints_open_segment, insert_subset, *, true_and] end module end ordered_semiring open_locale convex section ordered_ring variables (𝕜) [ordered_ring 𝕜] [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] section densely_ordered variables [nontrivial 𝕜] [densely_ordered 𝕜] @[simp] lemma open_segment_same (x : E) : open_segment 𝕜 x x = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [←add_smul, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ (h : z = x), begin obtain ⟨a, ha₀, ha₁⟩ := densely_ordered.dense (0 : 𝕜) 1 zero_lt_one, refine ⟨a, 1 - a, ha₀, sub_pos_of_lt ha₁, add_sub_cancel'_right _ _, _⟩, rw [←add_smul, add_sub_cancel'_right, one_smul, h], end⟩ end densely_ordered lemma segment_eq_image (x y : E) : [x -[𝕜] y] = (λ θ : 𝕜, (1 - θ) • x + θ • y) '' Icc (0 : 𝕜) 1 := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ le_add_of_nonneg_left ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1-θ, θ, sub_nonneg.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma open_segment_eq_image (x y : E) : open_segment 𝕜 x y = (λ (θ : 𝕜), (1 - θ) • x + θ • y) '' Ioo (0 : 𝕜) 1 := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ lt_add_of_pos_left _ ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1 - θ, θ, sub_pos.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_image' (x y : E) : [x -[𝕜] y] = (λ (θ : 𝕜), x + θ • (y - x)) '' Icc (0 : 𝕜) 1 := by { convert segment_eq_image 𝕜 x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma open_segment_eq_image' (x y : E) : open_segment 𝕜 x y = (λ (θ : 𝕜), x + θ • (y - x)) '' Ioo (0 : 𝕜) 1 := by { convert open_segment_eq_image 𝕜 x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_eq_image_line_map (x y : E) : [x -[𝕜] y] = affine_map.line_map x y '' Icc (0 : 𝕜) 1 := by { convert segment_eq_image 𝕜 x y, ext, exact affine_map.line_map_apply_module _ _ _ } lemma open_segment_eq_image_line_map (x y : E) : open_segment 𝕜 x y = affine_map.line_map x y '' Ioo (0 : 𝕜) 1 := by { convert open_segment_eq_image 𝕜 x y, ext, exact affine_map.line_map_apply_module _ _ _ } lemma segment_image (f : E →ₗ[𝕜] F) (a b : E) : f '' [a -[𝕜] b] = [f a -[𝕜] f b] := set.ext (λ x, by simp_rw [segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul]) @[simp] lemma open_segment_image (f : E →ₗ[𝕜] F) (a b : E) : f '' open_segment 𝕜 a b = open_segment 𝕜 (f a) (f b) := set.ext (λ x, by simp_rw [open_segment_eq_image, mem_image, exists_exists_and_eq_and, map_add, map_smul]) lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b -[𝕜] a + c] ↔ x ∈ [b -[𝕜] c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj], end @[simp] lemma mem_open_segment_translate (a : E) {x b c : E} : a + x ∈ open_segment 𝕜 (a + b) (a + c) ↔ x ∈ open_segment 𝕜 b c := begin rw [open_segment_eq_image', open_segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj], end lemma segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' [a + b -[𝕜] a + c] = [b -[𝕜] c] := set.ext $ λ x, mem_segment_translate 𝕜 a lemma open_segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' open_segment 𝕜 (a + b) (a + c) = open_segment 𝕜 b c := set.ext $ λ x, mem_open_segment_translate 𝕜 a lemma segment_translate_image (a b c : E) : (λ x, a + x) '' [b -[𝕜] c] = [a + b -[𝕜] a + c] := segment_translate_preimage 𝕜 a b c ▸ image_preimage_eq _ $ add_left_surjective a lemma open_segment_translate_image (a b c : E) : (λ x, a + x) '' open_segment 𝕜 b c = open_segment 𝕜 (a + b) (a + c) := open_segment_translate_preimage 𝕜 a b c ▸ image_preimage_eq _ $ add_left_surjective a end ordered_ring lemma same_ray_of_mem_segment [strict_ordered_comm_ring 𝕜] [add_comm_group E] [module 𝕜 E] {x y z : E} (h : x ∈ [y -[𝕜] z]) : same_ray 𝕜 (x - y) (z - x) := begin rw segment_eq_image' at h, rcases h with ⟨θ, ⟨hθ₀, hθ₁⟩, rfl⟩, simpa only [add_sub_cancel', ←sub_sub, sub_smul, one_smul] using (same_ray_nonneg_smul_left (z - y) hθ₀).nonneg_smul_right (sub_nonneg.2 hθ₁) end section linear_ordered_ring variables [linear_ordered_ring 𝕜] [add_comm_group E] [module 𝕜 E] {x y : E} lemma midpoint_mem_segment [invertible (2 : 𝕜)] (x y : E) : midpoint 𝕜 x y ∈ [x -[𝕜] y] := begin rw segment_eq_image_line_map, exact ⟨⅟2, ⟨inv_of_nonneg.mpr zero_le_two, inv_of_le_one one_le_two⟩, rfl⟩, end lemma mem_segment_sub_add [invertible (2 : 𝕜)] (x y : E) : x ∈ [x - y -[𝕜] x + y] := by { convert @midpoint_mem_segment 𝕜 _ _ _ _ _ _ _, rw midpoint_sub_add } lemma mem_segment_add_sub [invertible (2 : 𝕜)] (x y : E) : x ∈ [x + y -[𝕜] x - y] := by { convert @midpoint_mem_segment 𝕜 _ _ _ _ _ _ _, rw midpoint_add_sub } @[simp] lemma left_mem_open_segment_iff [densely_ordered 𝕜] [no_zero_smul_divisors 𝕜 E] : x ∈ open_segment 𝕜 x y ↔ x = y := begin split, { rintro ⟨a, b, ha, hb, hab, hx⟩, refine smul_right_injective _ hb.ne' ((add_right_inj (a • x)).1 _), rw [hx, ←add_smul, hab, one_smul] }, { rintro rfl, rw open_segment_same, exact mem_singleton _ } end @[simp] lemma right_mem_open_segment_iff [densely_ordered 𝕜] [no_zero_smul_divisors 𝕜 E] : y ∈ open_segment 𝕜 x y ↔ x = y := by rw [open_segment_symm, left_mem_open_segment_iff, eq_comm] end linear_ordered_ring section linear_ordered_semifield variables [linear_ordered_semifield 𝕜] [add_comm_group E] [module 𝕜 E] {x y z : E} lemma mem_segment_iff_div : x ∈ [y -[𝕜] z] ↔ ∃ a b : 𝕜, 0 ≤ a ∧ 0 ≤ b ∧ 0 < a + b ∧ (a / (a + b)) • y + (b / (a + b)) • z = x := begin split, { rintro ⟨a, b, ha, hb, hab, rfl⟩, use [a, b, ha, hb], simp * }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, refine ⟨a / (a + b), b / (a + b), div_nonneg ha hab.le, div_nonneg hb hab.le, _, rfl⟩, rw [←add_div, div_self hab.ne'] } end lemma mem_open_segment_iff_div : x ∈ open_segment 𝕜 y z ↔ ∃ a b : 𝕜, 0 < a ∧ 0 < b ∧ (a / (a + b)) • y + (b / (a + b)) • z = x := begin split, { rintro ⟨a, b, ha, hb, hab, rfl⟩, use [a, b, ha, hb], rw [hab, div_one, div_one] }, { rintro ⟨a, b, ha, hb, rfl⟩, have hab : 0 < a + b := by positivity, refine ⟨a / (a + b), b / (a + b), by positivity, by positivity, _, rfl⟩, rw [←add_div, div_self hab.ne'] } end end linear_ordered_semifield section linear_ordered_field variables [linear_ordered_field 𝕜] [add_comm_group E] [module 𝕜 E] {x y z : E} lemma mem_segment_iff_same_ray : x ∈ [y -[𝕜] z] ↔ same_ray 𝕜 (x - y) (z - x) := begin refine ⟨same_ray_of_mem_segment, λ h, _⟩, rcases h.exists_eq_smul_add with ⟨a, b, ha, hb, hab, hxy, hzx⟩, rw [add_comm, sub_add_sub_cancel] at hxy hzx, rw [←mem_segment_translate _ (-x), neg_add_self], refine ⟨b, a, hb, ha, add_comm a b ▸ hab, _⟩, rw [←sub_eq_neg_add, ←neg_sub, hxy, ←sub_eq_neg_add, hzx, smul_neg, smul_comm, neg_add_self] end open affine_map /-- If `z = line_map x y c` is a point on the line passing through `x` and `y`, then the open segment `open_segment 𝕜 x y` is included in the union of the open segments `open_segment 𝕜 x z`, `open_segment 𝕜 z y`, and the point `z`. Informally, `(x, y) ⊆ {z} ∪ (x, z) ∪ (z, y)`. -/ lemma open_segment_subset_union (x y : E) {z : E} (hz : z ∈ range (line_map x y : 𝕜 → E)) : open_segment 𝕜 x y ⊆ insert z (open_segment 𝕜 x z ∪ open_segment 𝕜 z y) := begin rcases hz with ⟨c, rfl⟩, simp only [open_segment_eq_image_line_map, ← maps_to'], rintro a ⟨h₀, h₁⟩, rcases lt_trichotomy a c with hac|rfl|hca, { right, left, have hc : 0 < c, from h₀.trans hac, refine ⟨a / c, ⟨div_pos h₀ hc, (div_lt_one hc).2 hac⟩, _⟩, simp only [← homothety_eq_line_map, ← homothety_mul_apply, div_mul_cancel _ hc.ne'] }, { left, refl }, { right, right, have hc : 0 < 1 - c, from sub_pos.2 (hca.trans h₁), simp only [← line_map_apply_one_sub y], refine ⟨(a - c) / (1 - c), ⟨div_pos (sub_pos.2 hca) hc, (div_lt_one hc).2 $ sub_lt_sub_right h₁ _⟩, _⟩, simp only [← homothety_eq_line_map, ← homothety_mul_apply, sub_mul, one_mul, div_mul_cancel _ hc.ne', sub_sub_sub_cancel_right] } end end linear_ordered_field /-! #### Segments in an ordered space Relates `segment`, `open_segment` and `set.Icc`, `set.Ico`, `set.Ioc`, `set.Ioo` -/ section ordered_semiring variables [ordered_semiring 𝕜] section ordered_add_comm_monoid variables [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {x y : E} lemma segment_subset_Icc (h : x ≤ y) : [x -[𝕜] y] ⊆ Icc x y := begin rintro z ⟨a, b, ha, hb, hab, rfl⟩, split, calc x = a • x + b • x :(convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_left (smul_le_smul_of_nonneg h hb) _, calc a • x + b • y ≤ a • y + b • y : add_le_add_right (smul_le_smul_of_nonneg h ha) _ ... = y : convex.combo_self hab _, end end ordered_add_comm_monoid section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {x y : E} lemma open_segment_subset_Ioo (h : x < y) : open_segment 𝕜 x y ⊆ Ioo x y := begin rintro z ⟨a, b, ha, hb, hab, rfl⟩, split, calc x = a • x + b • x : (convex.combo_self hab _).symm ... < a • x + b • y : add_lt_add_left (smul_lt_smul_of_pos h hb) _, calc a • x + b • y < a • y + b • y : add_lt_add_right (smul_lt_smul_of_pos h ha) _ ... = y : convex.combo_self hab _, end end ordered_cancel_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {𝕜} {a b : 𝕜} lemma segment_subset_interval (x y : E) : [x -[𝕜] y] ⊆ interval x y := begin cases le_total x y, { rw interval_of_le h, exact segment_subset_Icc h }, { rw [interval_of_ge h, segment_symm], exact segment_subset_Icc h } end lemma convex.min_le_combo (x y : E) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min x y ≤ a • x + b • y := (segment_subset_interval x y ⟨_, _, ha, hb, hab, rfl⟩).1 lemma convex.combo_le_max (x y : E) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : a • x + b • y ≤ max x y := (segment_subset_interval x y ⟨_, _, ha, hb, hab, rfl⟩).2 end linear_ordered_add_comm_monoid end ordered_semiring section linear_ordered_field variables [linear_ordered_field 𝕜] {x y z : 𝕜} lemma Icc_subset_segment : Icc x y ⊆ [x -[𝕜] y] := begin rintro z ⟨hxz, hyz⟩, obtain rfl | h := (hxz.trans hyz).eq_or_lt, { rw segment_same, exact hyz.antisymm hxz }, rw ←sub_nonneg at hxz hyz, rw ←sub_pos at h, refine ⟨(y - z) / (y - x), (z - x) / (y - x), div_nonneg hyz h.le, div_nonneg hxz h.le, _, _⟩, { rw [←add_div, sub_add_sub_cancel, div_self h.ne'] }, { rw [smul_eq_mul, smul_eq_mul, ←mul_div_right_comm, ←mul_div_right_comm, ←add_div, div_eq_iff h.ne', add_comm, sub_mul, sub_mul, mul_comm x, sub_add_sub_cancel, mul_sub] } end @[simp] lemma segment_eq_Icc (h : x ≤ y) : [x -[𝕜] y] = Icc x y := (segment_subset_Icc h).antisymm Icc_subset_segment lemma Ioo_subset_open_segment : Ioo x y ⊆ open_segment 𝕜 x y := λ z hz, mem_open_segment_of_ne_left_right hz.1.ne hz.2.ne' $ Icc_subset_segment $ Ioo_subset_Icc_self hz @[simp] lemma open_segment_eq_Ioo (h : x < y) : open_segment 𝕜 x y = Ioo x y := (open_segment_subset_Ioo h).antisymm Ioo_subset_open_segment lemma segment_eq_Icc' (x y : 𝕜) : [x -[𝕜] y] = Icc (min x y) (max x y) := begin cases le_total x y, { rw [segment_eq_Icc h, max_eq_right h, min_eq_left h] }, { rw [segment_symm, segment_eq_Icc h, max_eq_left h, min_eq_right h] } end lemma open_segment_eq_Ioo' (hxy : x ≠ y) : open_segment 𝕜 x y = Ioo (min x y) (max x y) := begin cases hxy.lt_or_lt, { rw [open_segment_eq_Ioo h, max_eq_right h.le, min_eq_left h.le] }, { rw [open_segment_symm, open_segment_eq_Ioo h, max_eq_left h.le, min_eq_right h.le] } end lemma segment_eq_interval (x y : 𝕜) : [x -[𝕜] y] = interval x y := segment_eq_Icc' _ _ /-- A point is in an `Icc` iff it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Icc (h : x ≤ y) : z ∈ Icc x y ↔ ∃ a b, 0 ≤ a ∧ 0 ≤ b ∧ a + b = 1 ∧ a * x + b * y = z := by { rw ←segment_eq_Icc h, simp_rw [←exists_prop], refl } /-- A point is in an `Ioo` iff it can be expressed as a strict convex combination of the endpoints. -/ lemma convex.mem_Ioo (h : x < y) : z ∈ Ioo x y ↔ ∃ a b, 0 < a ∧ 0 < b ∧ a + b = 1 ∧ a * x + b * y = z := by { rw ←open_segment_eq_Ioo h, simp_rw [←exists_prop], refl } /-- A point is in an `Ioc` iff it can be expressed as a semistrict convex combination of the endpoints. -/ lemma convex.mem_Ioc (h : x < y) : z ∈ Ioc x y ↔ ∃ a b, 0 ≤ a ∧ 0 < b ∧ a + b = 1 ∧ a * x + b * y = z := begin refine ⟨λ hz, _, _⟩, { obtain ⟨a, b, ha, hb, hab, rfl⟩ := (convex.mem_Icc h.le).1 (Ioc_subset_Icc_self hz), obtain rfl | hb' := hb.eq_or_lt, { rw add_zero at hab, rw [hab, one_mul, zero_mul, add_zero] at hz, exact (hz.1.ne rfl).elim }, { exact ⟨a, b, ha, hb', hab, rfl⟩ } }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rwa [hab, one_mul, zero_mul, zero_add, right_mem_Ioc] }, { exact Ioo_subset_Ioc_self ((convex.mem_Ioo h).2 ⟨a, b, ha', hb, hab, rfl⟩) } } end /-- A point is in an `Ico` iff it can be expressed as a semistrict convex combination of the endpoints. -/ lemma convex.mem_Ico (h : x < y) : z ∈ Ico x y ↔ ∃ a b, 0 < a ∧ 0 ≤ b ∧ a + b = 1 ∧ a * x + b * y = z := begin refine ⟨λ hz, _, _⟩, { obtain ⟨a, b, ha, hb, hab, rfl⟩ := (convex.mem_Icc h.le).1 (Ico_subset_Icc_self hz), obtain rfl | ha' := ha.eq_or_lt, { rw zero_add at hab, rw [hab, one_mul, zero_mul, zero_add] at hz, exact (hz.2.ne rfl).elim }, { exact ⟨a, b, ha', hb, hab, rfl⟩ } }, { rintro ⟨a, b, ha, hb, hab, rfl⟩, obtain rfl | hb' := hb.eq_or_lt, { rw add_zero at hab, rwa [hab, one_mul, zero_mul, add_zero, left_mem_Ico] }, { exact Ioo_subset_Ico_self ((convex.mem_Ioo h).2 ⟨a, b, ha, hb', hab, rfl⟩) } } end end linear_ordered_field
be014d09a947c657d0ea30df0d78121f5cae6cd6
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/data/polynomial/field_division.lean
78088b0a4af6c3a012fe3f6757a4800ffbaae9bb
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,884
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import algebra.gcd_monoid.basic import data.polynomial.derivative import data.polynomial.ring_division import data.set.pairwise import ring_theory.coprime.lemmas import ring_theory.euclidean_domain /-! # Theory of univariate polynomials This file starts looking like the ring theory of $ R[X] $ -/ noncomputable theory open_locale classical big_operators namespace polynomial universes u v w y z variables {R : Type u} {S : Type v} {k : Type y} {A : Type z} {a b : R} {n : ℕ} section is_domain variables [comm_ring R] [is_domain R] [normalization_monoid R] instance : normalization_monoid (polynomial R) := { norm_unit := λ p, ⟨C ↑(norm_unit (p.leading_coeff)), C ↑(norm_unit (p.leading_coeff))⁻¹, by rw [← ring_hom.map_mul, units.mul_inv, C_1], by rw [← ring_hom.map_mul, units.inv_mul, C_1]⟩, norm_unit_zero := units.ext (by simp), norm_unit_mul := λ p q hp0 hq0, units.ext (begin dsimp, rw [ne.def, ← leading_coeff_eq_zero] at *, rw [leading_coeff_mul, norm_unit_mul hp0 hq0, units.coe_mul, C_mul], end), norm_unit_coe_units := λ u, units.ext begin rw [← mul_one u⁻¹, units.coe_mul, units.eq_inv_mul_iff_mul_eq], dsimp, rcases polynomial.is_unit_iff.1 ⟨u, rfl⟩ with ⟨_, ⟨w, rfl⟩, h2⟩, rw [← h2, leading_coeff_C, norm_unit_coe_units, ← C_mul, units.mul_inv, C_1], end } @[simp] lemma coe_norm_unit {p : polynomial R} : (norm_unit p : polynomial R) = C ↑(norm_unit p.leading_coeff) := by simp [norm_unit] lemma leading_coeff_normalize (p : polynomial R) : leading_coeff (normalize p) = normalize (leading_coeff p) := by simp lemma monic.normalize_eq_self {p : polynomial R} (hp : p.monic) : normalize p = p := by simp only [polynomial.coe_norm_unit, normalize_apply, hp.leading_coeff, norm_unit_one, units.coe_one, polynomial.C.map_one, mul_one] end is_domain section field variables [field R] {p q : polynomial R} lemma is_unit_iff_degree_eq_zero : is_unit p ↔ degree p = 0 := ⟨degree_eq_zero_of_is_unit, λ h, have degree p ≤ 0, by simp [*, le_refl], have hc : coeff p 0 ≠ 0, from λ hc, by rw [eq_C_of_degree_le_zero this, hc] at h; simpa using h, is_unit_iff_dvd_one.2 ⟨C (coeff p 0)⁻¹, begin conv in p { rw eq_C_of_degree_le_zero this }, rw [← C_mul, _root_.mul_inv_cancel hc, C_1] end⟩⟩ lemma degree_pos_of_ne_zero_of_nonunit (hp0 : p ≠ 0) (hp : ¬is_unit p) : 0 < degree p := lt_of_not_ge (λ h, begin rw [eq_C_of_degree_le_zero h] at hp0 hp, exact hp (is_unit.map (C.to_monoid_hom : R →* _) (is_unit.mk0 (coeff p 0) (mt C_inj.2 (by simpa using hp0)))), end) lemma monic_mul_leading_coeff_inv (h : p ≠ 0) : monic (p * C (leading_coeff p)⁻¹) := by rw [monic, leading_coeff_mul, leading_coeff_C, mul_inv_cancel (show leading_coeff p ≠ 0, from mt leading_coeff_eq_zero.1 h)] lemma degree_mul_leading_coeff_inv (p : polynomial R) (h : q ≠ 0) : degree (p * C (leading_coeff q)⁻¹) = degree p := have h₁ : (leading_coeff q)⁻¹ ≠ 0 := inv_ne_zero (mt leading_coeff_eq_zero.1 h), by rw [degree_mul, degree_C h₁, add_zero] theorem irreducible_of_monic {p : polynomial R} (hp1 : p.monic) (hp2 : p ≠ 1) : irreducible p ↔ (∀ f g : polynomial R, f.monic → g.monic → f * g = p → f = 1 ∨ g = 1) := ⟨λ hp3 f g hf hg hfg, or.cases_on (hp3.is_unit_or_is_unit hfg.symm) (assume huf : is_unit f, or.inl $ eq_one_of_is_unit_of_monic hf huf) (assume hug : is_unit g, or.inr $ eq_one_of_is_unit_of_monic hg hug), λ hp3, ⟨mt (eq_one_of_is_unit_of_monic hp1) hp2, λ f g hp, have hf : f ≠ 0, from λ hf, by { rw [hp, hf, zero_mul] at hp1, exact not_monic_zero hp1 }, have hg : g ≠ 0, from λ hg, by { rw [hp, hg, mul_zero] at hp1, exact not_monic_zero hp1 }, or.imp (λ hf, is_unit_of_mul_eq_one _ _ hf) (λ hg, is_unit_of_mul_eq_one _ _ hg) $ hp3 (f * C f.leading_coeff⁻¹) (g * C g.leading_coeff⁻¹) (monic_mul_leading_coeff_inv hf) (monic_mul_leading_coeff_inv hg) $ by rw [mul_assoc, mul_left_comm _ g, ← mul_assoc, ← C_mul, ← mul_inv₀, ← leading_coeff_mul, ← hp, monic.def.1 hp1, inv_one, C_1, mul_one]⟩⟩ /-- Division of polynomials. See polynomial.div_by_monic for more details.-/ def div (p q : polynomial R) := C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)) /-- Remainder of polynomial division, see the lemma `quotient_mul_add_remainder_eq_aux`. See polynomial.mod_by_monic for more details. -/ def mod (p q : polynomial R) := p %ₘ (q * C (leading_coeff q)⁻¹) private lemma quotient_mul_add_remainder_eq_aux (p q : polynomial R) : q * div p q + mod p q = p := if h : q = 0 then by simp only [h, zero_mul, mod, mod_by_monic_zero, zero_add] else begin conv {to_rhs, rw ← mod_by_monic_add_div p (monic_mul_leading_coeff_inv h)}, rw [div, mod, add_comm, mul_assoc] end private lemma remainder_lt_aux (p : polynomial R) (hq : q ≠ 0) : degree (mod p q) < degree q := by rw ← degree_mul_leading_coeff_inv q hq; exact degree_mod_by_monic_lt p (monic_mul_leading_coeff_inv hq) instance : has_div (polynomial R) := ⟨div⟩ instance : has_mod (polynomial R) := ⟨mod⟩ lemma div_def : p / q = C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)) := rfl lemma mod_def : p % q = p %ₘ (q * C (leading_coeff q)⁻¹) := rfl lemma mod_by_monic_eq_mod (p : polynomial R) (hq : monic q) : p %ₘ q = p % q := show p %ₘ q = p %ₘ (q * C (leading_coeff q)⁻¹), by simp only [monic.def.1 hq, inv_one, mul_one, C_1] lemma div_by_monic_eq_div (p : polynomial R) (hq : monic q) : p /ₘ q = p / q := show p /ₘ q = C (leading_coeff q)⁻¹ * (p /ₘ (q * C (leading_coeff q)⁻¹)), by simp only [monic.def.1 hq, inv_one, C_1, one_mul, mul_one] lemma mod_X_sub_C_eq_C_eval (p : polynomial R) (a : R) : p % (X - C a) = C (p.eval a) := mod_by_monic_eq_mod p (monic_X_sub_C a) ▸ mod_by_monic_X_sub_C_eq_C_eval _ _ lemma mul_div_eq_iff_is_root : (X - C a) * (p / (X - C a)) = p ↔ is_root p a := div_by_monic_eq_div p (monic_X_sub_C a) ▸ mul_div_by_monic_eq_iff_is_root instance : euclidean_domain (polynomial R) := { quotient := (/), quotient_zero := by simp [div_def], remainder := (%), r := _, r_well_founded := degree_lt_wf, quotient_mul_add_remainder_eq := quotient_mul_add_remainder_eq_aux, remainder_lt := λ p q hq, remainder_lt_aux _ hq, mul_left_not_lt := λ p q hq, not_lt_of_ge (degree_le_mul_left _ hq), .. polynomial.comm_ring, .. polynomial.nontrivial } lemma mod_eq_self_iff (hq0 : q ≠ 0) : p % q = p ↔ degree p < degree q := ⟨λ h, h ▸ euclidean_domain.mod_lt _ hq0, λ h, have ¬degree (q * C (leading_coeff q)⁻¹) ≤ degree p := not_le_of_gt $ by rwa degree_mul_leading_coeff_inv q hq0, begin rw [mod_def, mod_by_monic, dif_pos (monic_mul_leading_coeff_inv hq0)], unfold div_mod_by_monic_aux, simp only [this, false_and, if_false] end⟩ lemma div_eq_zero_iff (hq0 : q ≠ 0) : p / q = 0 ↔ degree p < degree q := ⟨λ h, by have := euclidean_domain.div_add_mod p q; rwa [h, mul_zero, zero_add, mod_eq_self_iff hq0] at this, λ h, have hlt : degree p < degree (q * C (leading_coeff q)⁻¹), by rwa degree_mul_leading_coeff_inv q hq0, have hm : monic (q * C (leading_coeff q)⁻¹) := monic_mul_leading_coeff_inv hq0, by rw [div_def, (div_by_monic_eq_zero_iff hm).2 hlt, mul_zero]⟩ lemma degree_add_div (hq0 : q ≠ 0) (hpq : degree q ≤ degree p) : degree q + degree (p / q) = degree p := have degree (p % q) < degree (q * (p / q)) := calc degree (p % q) < degree q : euclidean_domain.mod_lt _ hq0 ... ≤ _ : degree_le_mul_left _ (mt (div_eq_zero_iff hq0).1 (not_lt_of_ge hpq)), by conv_rhs { rw [← euclidean_domain.div_add_mod p q, degree_add_eq_left_of_degree_lt this, degree_mul] } lemma degree_div_le (p q : polynomial R) : degree (p / q) ≤ degree p := if hq : q = 0 then by simp [hq] else by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq]; exact degree_div_by_monic_le _ _ lemma degree_div_lt (hp : p ≠ 0) (hq : 0 < degree q) : degree (p / q) < degree p := have hq0 : q ≠ 0, from λ hq0, by simpa [hq0] using hq, by rw [div_def, mul_comm, degree_mul_leading_coeff_inv _ hq0]; exact degree_div_by_monic_lt _ (monic_mul_leading_coeff_inv hq0) hp (by rw degree_mul_leading_coeff_inv _ hq0; exact hq) @[simp] lemma degree_map [field k] (p : polynomial R) (f : R →+* k) : degree (p.map f) = degree p := p.degree_map_eq_of_injective f.injective @[simp] lemma nat_degree_map [field k] (f : R →+* k) : nat_degree (p.map f) = nat_degree p := nat_degree_eq_of_degree_eq (degree_map _ f) @[simp] lemma leading_coeff_map [field k] (f : R →+* k) : leading_coeff (p.map f) = f (leading_coeff p) := by simp only [← coeff_nat_degree, coeff_map f, nat_degree_map] theorem monic_map_iff [field k] {f : R →+* k} {p : polynomial R} : (p.map f).monic ↔ p.monic := by rw [monic, leading_coeff_map, ← f.map_one, function.injective.eq_iff f.injective, monic] theorem is_unit_map [field k] (f : R →+* k) : is_unit (p.map f) ↔ is_unit p := by simp_rw [is_unit_iff_degree_eq_zero, degree_map] lemma map_div [field k] (f : R →+* k) : (p / q).map f = p.map f / q.map f := if hq0 : q = 0 then by simp [hq0] else by rw [div_def, div_def, map_mul, map_div_by_monic f (monic_mul_leading_coeff_inv hq0)]; simp [f.map_inv, coeff_map f] lemma map_mod [field k] (f : R →+* k) : (p % q).map f = p.map f % q.map f := if hq0 : q = 0 then by simp [hq0] else by rw [mod_def, mod_def, leading_coeff_map f, ← f.map_inv, ← map_C f, ← map_mul f, map_mod_by_monic f (monic_mul_leading_coeff_inv hq0)] section open euclidean_domain theorem gcd_map [field k] (f : R →+* k) : gcd (p.map f) (q.map f) = (gcd p q).map f := gcd.induction p q (λ x, by simp_rw [map_zero, euclidean_domain.gcd_zero_left]) $ λ x y hx ih, by rw [gcd_val, ← map_mod, ih, ← gcd_val] end lemma eval₂_gcd_eq_zero [comm_semiring k] {ϕ : R →+* k} {f g : polynomial R} {α : k} (hf : f.eval₂ ϕ α = 0) (hg : g.eval₂ ϕ α = 0) : (euclidean_domain.gcd f g).eval₂ ϕ α = 0 := by rw [euclidean_domain.gcd_eq_gcd_ab f g, polynomial.eval₂_add, polynomial.eval₂_mul, polynomial.eval₂_mul, hf, hg, zero_mul, zero_mul, zero_add] lemma eval_gcd_eq_zero {f g : polynomial R} {α : R} (hf : f.eval α = 0) (hg : g.eval α = 0) : (euclidean_domain.gcd f g).eval α = 0 := eval₂_gcd_eq_zero hf hg lemma root_left_of_root_gcd [comm_semiring k] {ϕ : R →+* k} {f g : polynomial R} {α : k} (hα : (euclidean_domain.gcd f g).eval₂ ϕ α = 0) : f.eval₂ ϕ α = 0 := by { cases euclidean_domain.gcd_dvd_left f g with p hp, rw [hp, polynomial.eval₂_mul, hα, zero_mul] } lemma root_right_of_root_gcd [comm_semiring k] {ϕ : R →+* k} {f g : polynomial R} {α : k} (hα : (euclidean_domain.gcd f g).eval₂ ϕ α = 0) : g.eval₂ ϕ α = 0 := by { cases euclidean_domain.gcd_dvd_right f g with p hp, rw [hp, polynomial.eval₂_mul, hα, zero_mul] } lemma root_gcd_iff_root_left_right [comm_semiring k] {ϕ : R →+* k} {f g : polynomial R} {α : k} : (euclidean_domain.gcd f g).eval₂ ϕ α = 0 ↔ (f.eval₂ ϕ α = 0) ∧ (g.eval₂ ϕ α = 0) := ⟨λ h, ⟨root_left_of_root_gcd h, root_right_of_root_gcd h⟩, λ h, eval₂_gcd_eq_zero h.1 h.2⟩ lemma is_root_gcd_iff_is_root_left_right {f g : polynomial R} {α : R} : (euclidean_domain.gcd f g).is_root α ↔ f.is_root α ∧ g.is_root α := root_gcd_iff_root_left_right theorem is_coprime_map [field k] (f : R →+* k) : is_coprime (p.map f) (q.map f) ↔ is_coprime p q := by rw [← gcd_is_unit_iff, ← gcd_is_unit_iff, gcd_map, is_unit_map] @[simp] lemma map_eq_zero [semiring S] [nontrivial S] (f : R →+* S) : p.map f = 0 ↔ p = 0 := by simp only [polynomial.ext_iff, f.map_eq_zero, coeff_map, coeff_zero] lemma map_ne_zero [semiring S] [nontrivial S] {f : R →+* S} (hp : p ≠ 0) : p.map f ≠ 0 := mt (map_eq_zero f).1 hp lemma mem_roots_map [field k] {f : R →+* k} {x : k} (hp : p ≠ 0) : x ∈ (p.map f).roots ↔ p.eval₂ f x = 0 := begin rw mem_roots (show p.map f ≠ 0, by exact map_ne_zero hp), dsimp only [is_root], rw polynomial.eval_map, end lemma mem_root_set [field k] [algebra R k] {x : k} (hp : p ≠ 0) : x ∈ p.root_set k ↔ aeval x p = 0 := iff.trans multiset.mem_to_finset (mem_roots_map hp) lemma root_set_C_mul_X_pow {R S : Type*} [field R] [field S] [algebra R S] {n : ℕ} (hn : n ≠ 0) {a : R} (ha : a ≠ 0) : (C a * X ^ n).root_set S = {0} := begin ext x, rw [set.mem_singleton_iff, mem_root_set, aeval_mul, aeval_C, aeval_X_pow, mul_eq_zero], { simp_rw [ring_hom.map_eq_zero, pow_eq_zero_iff (nat.pos_of_ne_zero hn), or_iff_right_iff_imp], exact λ ha', (ha ha').elim }, { exact mul_ne_zero (mt C_eq_zero.mp ha) (pow_ne_zero n X_ne_zero) }, end lemma root_set_monomial {R S : Type*} [field R] [field S] [algebra R S] {n : ℕ} (hn : n ≠ 0) {a : R} (ha : a ≠ 0) : (monomial n a).root_set S = {0} := by rw [←C_mul_X_pow_eq_monomial, root_set_C_mul_X_pow hn ha] lemma root_set_X_pow {R S : Type*} [field R] [field S] [algebra R S] {n : ℕ} (hn : n ≠ 0) : (X ^ n : polynomial R).root_set S = {0} := by { rw [←one_mul (X ^ n : polynomial R), ←C_1, root_set_C_mul_X_pow hn], exact one_ne_zero } lemma exists_root_of_degree_eq_one (h : degree p = 1) : ∃ x, is_root p x := ⟨-(p.coeff 0 / p.coeff 1), have p.coeff 1 ≠ 0, by rw ← nat_degree_eq_of_degree_eq_some h; exact mt leading_coeff_eq_zero.1 (λ h0, by simpa [h0] using h), by conv in p { rw [eq_X_add_C_of_degree_le_one (show degree p ≤ 1, by rw h; exact le_refl _)] }; simp [is_root, mul_div_cancel' _ this]⟩ lemma coeff_inv_units (u : units (polynomial R)) (n : ℕ) : ((↑u : polynomial R).coeff n)⁻¹ = ((↑u⁻¹ : polynomial R).coeff n) := begin rw [eq_C_of_degree_eq_zero (degree_coe_units u), eq_C_of_degree_eq_zero (degree_coe_units u⁻¹), coeff_C, coeff_C, inv_eq_one_div], split_ifs, { rw [div_eq_iff_mul_eq (coeff_coe_units_zero_ne_zero u), coeff_zero_eq_eval_zero, coeff_zero_eq_eval_zero, ← eval_mul, ← units.coe_mul, inv_mul_self]; simp }, { simp } end lemma monic_normalize (hp0 : p ≠ 0) : monic (normalize p) := begin rw [ne.def, ← leading_coeff_eq_zero, ← ne.def, ← is_unit_iff_ne_zero] at hp0, rw [monic, leading_coeff_normalize, normalize_eq_one], apply hp0, end lemma coe_norm_unit_of_ne_zero (hp : p ≠ 0) : (norm_unit p : polynomial R) = C p.leading_coeff⁻¹ := by simp [hp] theorem map_dvd_map' [field k] (f : R →+* k) {x y : polynomial R} : x.map f ∣ y.map f ↔ x ∣ y := if H : x = 0 then by rw [H, map_zero, zero_dvd_iff, zero_dvd_iff, map_eq_zero] else by rw [← normalize_dvd_iff, ← @normalize_dvd_iff (polynomial R), normalize_apply, normalize_apply, coe_norm_unit_of_ne_zero H, coe_norm_unit_of_ne_zero (mt (map_eq_zero f).1 H), leading_coeff_map, ← f.map_inv, ← map_C, ← map_mul, map_dvd_map _ f.injective (monic_mul_leading_coeff_inv H)] lemma degree_normalize : degree (normalize p) = degree p := by simp lemma prime_of_degree_eq_one (hp1 : degree p = 1) : prime p := have prime (normalize p), from monic.prime_of_degree_eq_one (hp1 ▸ degree_normalize) (monic_normalize (λ hp0, absurd hp1 (hp0.symm ▸ by simp; exact dec_trivial))), (normalize_associated _).prime this lemma irreducible_of_degree_eq_one (hp1 : degree p = 1) : irreducible p := (prime_of_degree_eq_one hp1).irreducible theorem not_irreducible_C (x : R) : ¬irreducible (C x) := if H : x = 0 then by { rw [H, C_0], exact not_irreducible_zero } else λ hx, irreducible.not_unit hx $ is_unit_C.2 $ is_unit_iff_ne_zero.2 H theorem degree_pos_of_irreducible (hp : irreducible p) : 0 < p.degree := lt_of_not_ge $ λ hp0, have _ := eq_C_of_degree_le_zero hp0, not_irreducible_C (p.coeff 0) $ this ▸ hp theorem pairwise_coprime_X_sub {α : Type u} [field α] {I : Type v} {s : I → α} (H : function.injective s) : pairwise (is_coprime on (λ i : I, polynomial.X - polynomial.C (s i))) := λ i j hij, have h : s j - s i ≠ 0, from sub_ne_zero_of_ne $ function.injective.ne H hij.symm, ⟨polynomial.C (s j - s i)⁻¹, -polynomial.C (s j - s i)⁻¹, by rw [neg_mul_eq_neg_mul_symm, ← sub_eq_add_neg, ← mul_sub, sub_sub_sub_cancel_left, ← polynomial.C_sub, ← polynomial.C_mul, inv_mul_cancel h, polynomial.C_1]⟩ /-- If `f` is a polynomial over a field, and `a : K` satisfies `f' a ≠ 0`, then `f / (X - a)` is coprime with `X - a`. Note that we do not assume `f a = 0`, because `f / (X - a) = (f - f a) / (X - a)`. -/ lemma is_coprime_of_is_root_of_eval_derivative_ne_zero {K : Type*} [field K] (f : polynomial K) (a : K) (hf' : f.derivative.eval a ≠ 0) : is_coprime (X - C a : polynomial K) (f /ₘ (X - C a)) := begin refine or.resolve_left (dvd_or_coprime (X - C a) (f /ₘ (X - C a)) (irreducible_of_degree_eq_one (polynomial.degree_X_sub_C a))) _, contrapose! hf' with h, have key : (X - C a) * (f /ₘ (X - C a)) = f - (f %ₘ (X - C a)), { rw [eq_sub_iff_add_eq, ← eq_sub_iff_add_eq', mod_by_monic_eq_sub_mul_div], exact monic_X_sub_C a }, replace key := congr_arg derivative key, simp only [derivative_X, derivative_mul, one_mul, sub_zero, derivative_sub, mod_by_monic_X_sub_C_eq_C_eval, derivative_C] at key, have : (X - C a) ∣ derivative f := key ▸ (dvd_add h (dvd_mul_right _ _)), rw [← dvd_iff_mod_by_monic_eq_zero (monic_X_sub_C _), mod_by_monic_X_sub_C_eq_C_eval] at this, rw [← C_inj, this, C_0], end lemma prod_multiset_root_eq_finset_root {p : polynomial R} (hzero : p ≠ 0) : (multiset.map (λ (a : R), X - C a) p.roots).prod = ∏ a in (multiset.to_finset p.roots), (λ (a : R), (X - C a) ^ (root_multiplicity a p)) a := by simp only [count_roots hzero, finset.prod_multiset_map_count] /-- The product `∏ (X - a)` for `a` inside the multiset `p.roots` divides `p`. -/ lemma prod_multiset_X_sub_C_dvd (p : polynomial R) : (multiset.map (λ (a : R), X - C a) p.roots).prod ∣ p := begin by_cases hp0 : p = 0, { simp only [hp0, roots_zero, is_unit_one, multiset.prod_zero, multiset.map_zero, is_unit.dvd] }, rw prod_multiset_root_eq_finset_root hp0, have hcoprime : pairwise (is_coprime on λ (a : R), polynomial.X - C (id a)) := pairwise_coprime_X_sub function.injective_id, have H : pairwise (is_coprime on λ (a : R), (polynomial.X - C (id a)) ^ (root_multiplicity a p)), { intros a b hdiff, exact (hcoprime a b hdiff).pow }, apply finset.prod_dvd_of_coprime (H.set_pairwise (↑(multiset.to_finset p.roots) : set R)), intros a h, rw multiset.mem_to_finset at h, exact pow_root_multiplicity_dvd p a end lemma roots_C_mul (p : polynomial R) {a : R} (hzero : a ≠ 0) : (C a * p).roots = p.roots := begin by_cases hpzero : p = 0, { simp only [hpzero, mul_zero] }, rw multiset.ext, intro b, have prodzero : C a * p ≠ 0, { simp only [hpzero, or_false, ne.def, mul_eq_zero, C_eq_zero, hzero, not_false_iff] }, rw [count_roots hpzero, count_roots prodzero, root_multiplicity_mul prodzero], have mulzero : root_multiplicity b (C a) = 0, { simp only [hzero, root_multiplicity_eq_zero, eval_C, is_root.def, not_false_iff] }, simp only [mulzero, zero_add] end lemma roots_normalize : (normalize p).roots = p.roots := begin by_cases hzero : p = 0, { rw [hzero, normalize_zero], }, { have hcoeff : p.leading_coeff ≠ 0, { intro h, exact hzero (leading_coeff_eq_zero.1 h) }, rw [normalize_apply, mul_comm, coe_norm_unit_of_ne_zero hzero, roots_C_mul _ (inv_ne_zero hcoeff)], }, end end field end polynomial
9679bdc8faf2fef84652c1dc55c6e1d128c44431
367134ba5a65885e863bdc4507601606690974c1
/src/data/polynomial/identities.lean
fc318fd4f966a7ff2657b813a12b5318ccf420f3
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
3,325
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.derivative /-! # Theory of univariate polynomials The main def is `binom_expansion`. -/ noncomputable theory namespace polynomial universes u v w x y z variables {R : Type u} {S : Type v} {T : Type w} {ι : Type x} {k : Type y} {A : Type z} {a b : R} {m n : ℕ} section identities /- @TODO: pow_add_expansion and pow_sub_pow_factor are not specific to polynomials. These belong somewhere else. But not in group_power because they depend on tactic.ring_exp Maybe use data.nat.choose to prove it. -/ def pow_add_expansion {R : Type*} [comm_semiring R] (x y : R) : ∀ (n : ℕ), {k // (x + y)^n = x^n + n*x^(n-1)*y + k * y^2} | 0 := ⟨0, by simp⟩ | 1 := ⟨0, by simp⟩ | (n+2) := begin cases pow_add_expansion (n+1) with z hz, existsi x*z + (n+1)*x^n+z*y, calc (x + y) ^ (n + 2) = (x + y) * (x + y) ^ (n + 1) : by ring_exp ... = (x + y) * (x ^ (n + 1) + ↑(n + 1) * x ^ (n + 1 - 1) * y + z * y ^ 2) : by rw hz ... = x ^ (n + 2) + ↑(n + 2) * x ^ (n + 1) * y + (x*z + (n+1)*x^n+z*y) * y ^ 2 : by { push_cast, ring_exp! } end variables [comm_ring R] private def poly_binom_aux1 (x y : R) (e : ℕ) (a : R) : {k : R // a * (x + y)^e = a * (x^e + e*x^(e-1)*y + k*y^2)} := begin existsi (pow_add_expansion x y e).val, congr, apply (pow_add_expansion _ _ _).property end private lemma poly_binom_aux2 (f : polynomial R) (x y : R) : f.eval (x + y) = f.sum (λ e a, a * (x^e + e*x^(e-1)*y + (poly_binom_aux1 x y e a).val*y^2)) := begin unfold eval eval₂, congr' with n z, apply (poly_binom_aux1 x y _ _).property end private lemma poly_binom_aux3 (f : polynomial R) (x y : R) : f.eval (x + y) = f.sum (λ e a, a * x^e) + f.sum (λ e a, (a * e * x^(e-1)) * y) + f.sum (λ e a, (a *(poly_binom_aux1 x y e a).val)*y^2) := by rw poly_binom_aux2; simp [left_distrib, finsupp.sum_add, mul_assoc] def binom_expansion (f : polynomial R) (x y : R) : {k : R // f.eval (x + y) = f.eval x + (f.derivative.eval x) * y + k * y^2} := begin existsi f.sum (λ e a, a *((poly_binom_aux1 x y e a).val)), rw poly_binom_aux3, congr, { rw [←eval_eq_sum], }, { rw derivative_eval, symmetry, apply finsupp.sum_mul }, { symmetry, apply finsupp.sum_mul } end def pow_sub_pow_factor (x y : R) : Π (i : ℕ), {z : R // x^i - y^i = z * (x - y)} | 0 := ⟨0, by simp⟩ | 1 := ⟨1, by simp⟩ | (k+2) := begin cases @pow_sub_pow_factor (k+1) with z hz, existsi z*x + y^(k+1), calc x ^ (k + 2) - y ^ (k + 2) = x * (x ^ (k + 1) - y ^ (k + 1)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by ring_exp ... = x * (z * (x - y)) + (x * y ^ (k + 1) - y ^ (k + 2)) : by rw hz ... = (z * x + y ^ (k + 1)) * (x - y) : by ring_exp end def eval_sub_factor (f : polynomial R) (x y : R) : {z : R // f.eval x - f.eval y = z * (x - y)} := begin refine ⟨f.sum (λ i r, r * (pow_sub_pow_factor x y i).val), _⟩, delta eval eval₂, rw ← finsupp.sum_sub, rw finsupp.sum_mul, delta finsupp.sum, congr' with i r, dsimp, rw [mul_assoc, ←(pow_sub_pow_factor x y _).prop, mul_sub], end end identities end polynomial
9a9064e2017b9ad6d558d213dc415bf148c48cd2
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/real/hyperreal.lean
e5a65c714e5e482905b257ac710fae9ba9c56cd1
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
36,728
lean
/- Copyright (c) 2019 Abhimanyu Pallavi Sudhir. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Abhimanyu Pallavi Sudhir Construction of the hyperreal numbers as an ultraproduct of real sequences. -/ import data.real.basic algebra.field order.filter.filter_product analysis.specific_limits open filter filter.filter_product open_locale topological_space classical /-- Hyperreal numbers on the ultrafilter extending the cofinite filter -/ @[reducible] def hyperreal := filter.filterprod ℝ (@hyperfilter ℕ) namespace hyperreal notation `ℝ*` := hyperreal private def U : is_ultrafilter (@hyperfilter ℕ) := is_ultrafilter_hyperfilter noncomputable instance : discrete_linear_ordered_field ℝ* := filter_product.discrete_linear_ordered_field U @[simp] lemma hyperfilter_ne_bot {α} [infinite α] : ¬ @hyperfilter α = ⊥ := is_ultrafilter_hyperfilter.1 @[simp] lemma hyperfilter_ne_bot' {α} [infinite α] : ¬ ⊥ = @hyperfilter α := hyperfilter_ne_bot ∘ eq.symm @[simp, elim_cast] lemma coe_eq_coe (x y : ℝ) : (x : ℝ*) = y ↔ x = y := filter_product.coe_injective _ _ (by simp) @[simp, move_cast] lemma cast_div (x y : ℝ) : ((x / y : ℝ) : ℝ*) = x / y := filter_product.of_div is_ultrafilter_hyperfilter _ _ @[simp, elim_cast] lemma coe_lt_coe (x y : ℝ) : (x : ℝ*) < y ↔ x < y := (filter_product.of_lt is_ultrafilter_hyperfilter).symm @[simp, elim_cast] lemma coe_le_coe (x y : ℝ) : (x : ℝ*) ≤ y ↔ x ≤ y := (filter_product.of_le hyperfilter_ne_bot).symm @[simp, move_cast] lemma coe_abs (x : ℝ) : ((abs x : ℝ) : ℝ*) = abs x := filter_product.of_abs _ _ @[simp, move_cast] lemma coe_max (x y : ℝ) : ((max x y : ℝ) : ℝ*) = max x y := filter_product.of_max _ _ _ @[simp, move_cast] lemma coe_min (x y : ℝ) : ((min x y : ℝ) : ℝ*) = min x y := filter_product.of_min _ _ _ /-- A sample infinitesimal hyperreal-/ noncomputable def epsilon : ℝ* := of_seq (λ n, n⁻¹) /-- A sample infinite hyperreal-/ noncomputable def omega : ℝ* := of_seq (λ n, n) localized "notation `ε` := hyperreal.epsilon" in hyperreal localized "notation `ω` := hyperreal.omega" in hyperreal lemma epsilon_eq_inv_omega : ε = ω⁻¹ := rfl lemma inv_epsilon_eq_omega : ε⁻¹ = ω := @inv_inv' _ _ ω lemma epsilon_pos : 0 < ε := suffices ∀ᶠ i in hyperfilter, (0 : ℝ) < (i : ℕ)⁻¹, by rwa lt_def U, have h0' : {n : ℕ | ¬ n > 0} = {0} := by simp only [not_lt, (set.set_of_eq_eq_singleton).symm]; ext; exact nat.le_zero_iff, begin simp only [inv_pos, nat.cast_pos], exact mem_hyperfilter_of_finite_compl (by convert set.finite_singleton _), end lemma epsilon_ne_zero : ε ≠ 0 := ne_of_gt epsilon_pos lemma omega_pos : 0 < ω := by rw ←inv_epsilon_eq_omega; exact inv_pos.2 epsilon_pos lemma omega_ne_zero : ω ≠ 0 := ne_of_gt omega_pos theorem epsilon_mul_omega : ε * ω = 1 := @inv_mul_cancel _ _ ω omega_ne_zero lemma lt_of_tendsto_zero_of_pos {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, r > 0 → of_seq f < (r : ℝ*) := begin simp only [metric.tendsto_at_top, dist_zero_right, norm, lt_def U] at hf ⊢, intros r hr, cases hf r hr with N hf', have hs : -{i : ℕ | f i < r} ⊆ {i : ℕ | i ≤ N} := λ i hi1, le_of_lt (by simp only [lt_iff_not_ge]; exact λ hi2, hi1 (lt_of_le_of_lt (le_abs_self _) (hf' i hi2)) : i < N), exact mem_hyperfilter_of_finite_compl (set.finite_subset (set.finite_le_nat N) hs) end lemma neg_lt_of_tendsto_zero_of_pos {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, r > 0 → (-r : ℝ*) < of_seq f := λ r hr, have hg : _ := hf.neg, neg_lt_of_neg_lt (by rw [neg_zero] at hg; exact lt_of_tendsto_zero_of_pos hg hr) lemma gt_of_tendsto_zero_of_neg {f : ℕ → ℝ} (hf : tendsto f at_top (𝓝 0)) : ∀ {r : ℝ}, r < 0 → (r : ℝ*) < of_seq f := λ r hr, by rw [←neg_neg r, of_neg]; exact neg_lt_of_tendsto_zero_of_pos hf (neg_pos.mpr hr) lemma epsilon_lt_pos (x : ℝ) : x > 0 → ε < of x := lt_of_tendsto_zero_of_pos tendsto_inverse_at_top_nhds_0_nat /-- Standard part predicate -/ def is_st (x : ℝ*) (r : ℝ) := ∀ δ : ℝ, δ > 0 → (r - δ : ℝ*) < x ∧ x < r + δ /-- Standard part function: like a "round" to ℝ instead of ℤ -/ noncomputable def st : ℝ* → ℝ := λ x, if h : ∃ r, is_st x r then classical.some h else 0 /-- A hyperreal number is infinitesimal if its standard part is 0 -/ def infinitesimal (x : ℝ*) := is_st x 0 /-- A hyperreal number is positive infinite if it is larger than all real numbers -/ def infinite_pos (x : ℝ*) := ∀ r : ℝ, x > r /-- A hyperreal number is negative infinite if it is smaller than all real numbers -/ def infinite_neg (x : ℝ*) := ∀ r : ℝ, x < r /-- A hyperreal number is infinite if it is infinite positive or infinite negative -/ def infinite (x : ℝ*) := infinite_pos x ∨ infinite_neg x -- SOME FACTS ABOUT ST private lemma is_st_unique' (x : ℝ*) (r s : ℝ) (hr : is_st x r) (hs : is_st x s) (hrs : r < s) : false := have hrs' : _ := half_pos $ sub_pos_of_lt hrs, have hr' : _ := (hr _ hrs').2, have hs' : _ := (hs _ hrs').1, have h : s - ((s - r) / 2) = r + (s - r) / 2 := by linarith, begin norm_cast at *, rw h at hs', exact not_lt_of_lt hs' hr' end theorem is_st_unique {x : ℝ*} {r s : ℝ} (hr : is_st x r) (hs : is_st x s) : r = s := begin rcases lt_trichotomy r s with h | h | h, { exact false.elim (is_st_unique' x r s hr hs h) }, { exact h }, { exact false.elim (is_st_unique' x s r hs hr h) } end theorem not_infinite_of_exists_st {x : ℝ*} : (∃ r : ℝ, is_st x r) → ¬ infinite x := λ he hi, Exists.dcases_on he $ λ r hr, hi.elim (λ hip, not_lt_of_lt (hr 2 two_pos).2 (hip $ r + 2)) (λ hin, not_lt_of_lt (hr 2 two_pos).1 (hin $ r - 2)) theorem is_st_Sup {x : ℝ*} (hni : ¬ infinite x) : is_st x (Sup {y : ℝ | of y < x}) := let S : set ℝ := {y : ℝ | of y < x} in let R : _ := Sup S in have hnile : _ := not_forall.mp (not_or_distrib.mp hni).1, have hnige : _ := not_forall.mp (not_or_distrib.mp hni).2, Exists.dcases_on hnile $ Exists.dcases_on hnige $ λ r₁ hr₁ r₂ hr₂, have HR₁ : ∃ y : ℝ, y ∈ S := ⟨ r₁ - 1, lt_of_lt_of_le (of_lt_of_lt U (sub_one_lt _)) (not_lt.mp hr₁) ⟩, have HR₂ : ∃ z : ℝ, ∀ y ∈ S, y ≤ z := ⟨ r₂, λ y hy, le_of_lt ((of_lt U).mpr (lt_of_lt_of_le hy (not_lt.mp hr₂))) ⟩, λ δ hδ, ⟨ lt_of_not_ge' $ λ c, have hc : ∀ y ∈ S, y ≤ R - δ := λ y hy, (of_le U.1).mpr $ le_of_lt $ lt_of_lt_of_le hy c, not_lt_of_le ((real.Sup_le _ HR₁ HR₂).mpr hc) $ sub_lt_self R hδ, lt_of_not_ge' $ λ c, have hc : of (R + δ / 2) < x := lt_of_lt_of_le (add_lt_add_left (of_lt_of_lt U (half_lt_self hδ)) (of R)) c, not_lt_of_le (real.le_Sup _ HR₂ hc) $ (lt_add_iff_pos_right _).mpr $ half_pos hδ⟩ theorem exists_st_of_not_infinite {x : ℝ*} (hni : ¬ infinite x) : ∃ r : ℝ, is_st x r := ⟨ Sup {y : ℝ | of y < x}, is_st_Sup hni ⟩ theorem st_eq_Sup {x : ℝ*} : st x = Sup {y : ℝ | of y < x} := begin unfold st, split_ifs, { exact is_st_unique (classical.some_spec h) (is_st_Sup (not_infinite_of_exists_st h)) }, { cases not_imp_comm.mp exists_st_of_not_infinite h with H H, { rw (set.ext (λ i, ⟨λ hi, set.mem_univ i, λ hi, H i⟩) : {y : ℝ | of y < x} = set.univ), exact (real.Sup_univ).symm }, { rw (set.ext (λ i, ⟨λ hi, false.elim (not_lt_of_lt (H i) hi), λ hi, false.elim (set.not_mem_empty i hi)⟩) : {y : ℝ | of y < x} = ∅), exact (real.Sup_empty).symm } } end theorem exists_st_iff_not_infinite {x : ℝ*} : (∃ r : ℝ, is_st x r) ↔ ¬ infinite x := ⟨ not_infinite_of_exists_st, exists_st_of_not_infinite ⟩ theorem infinite_iff_not_exists_st {x : ℝ*} : infinite x ↔ ¬ ∃ r : ℝ, is_st x r := iff_not_comm.mp exists_st_iff_not_infinite theorem st_infinite {x : ℝ*} (hi : infinite x) : st x = 0 := begin unfold st, split_ifs, { exact false.elim ((infinite_iff_not_exists_st.mp hi) h) }, { refl } end lemma st_of_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : st x = r := begin unfold st, split_ifs, { exact is_st_unique (classical.some_spec h) hxr }, { exact false.elim (h ⟨r, hxr⟩) } end lemma is_st_st_of_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : is_st x (st x) := by rwa [st_of_is_st hxr] lemma is_st_st_of_exists_st {x : ℝ*} (hx : ∃ r : ℝ, is_st x r) : is_st x (st x) := Exists.dcases_on hx (λ r, is_st_st_of_is_st) lemma is_st_st {x : ℝ*} (hx : st x ≠ 0) : is_st x (st x) := begin unfold st, split_ifs, { exact classical.some_spec h }, { exact false.elim (hx (by unfold st; split_ifs; refl)) } end lemma is_st_st' {x : ℝ*} (hx : ¬ infinite x) : is_st x (st x) := is_st_st_of_exists_st $ exists_st_of_not_infinite hx lemma is_st_refl_real (r : ℝ) : is_st r r := λ δ hδ, ⟨ sub_lt_self _ (of_lt_of_lt U hδ), (lt_add_of_pos_right _ (of_lt_of_lt U hδ)) ⟩ lemma st_id_real (r : ℝ) : st r = r := st_of_is_st (is_st_refl_real r) lemma eq_of_is_st_real {r s : ℝ} : is_st r s → r = s := is_st_unique (is_st_refl_real r) lemma is_st_real_iff_eq {r s : ℝ} : is_st r s ↔ r = s := ⟨eq_of_is_st_real, λ hrs, by rw [hrs]; exact is_st_refl_real s⟩ lemma is_st_symm_real {r s : ℝ} : is_st r s ↔ is_st s r := by rw [is_st_real_iff_eq, is_st_real_iff_eq, eq_comm] lemma is_st_trans_real {r s t : ℝ} : is_st r s → is_st s t → is_st r t := by rw [is_st_real_iff_eq, is_st_real_iff_eq, is_st_real_iff_eq]; exact eq.trans lemma is_st_inj_real {r₁ r₂ s : ℝ} (h1 : is_st r₁ s) (h2 : is_st r₂ s) : r₁ = r₂ := eq.trans (eq_of_is_st_real h1) (eq_of_is_st_real h2).symm lemma is_st_iff_abs_sub_lt_delta {x : ℝ*} {r : ℝ} : is_st x r ↔ ∀ (δ : ℝ), δ > 0 → abs (x - r) < δ := by simp only [abs_sub_lt_iff, @sub_lt _ _ (r : ℝ*) x _, @sub_lt_iff_lt_add' _ _ x (r : ℝ*) _, and_comm]; refl lemma is_st_add {x y : ℝ*} {r s : ℝ} : is_st x r → is_st y s → is_st (x + y) (r + s) := λ hxr hys d hd, have hxr' : _ := hxr (d / 2) (half_pos hd), have hys' : _ := hys (d / 2) (half_pos hd), ⟨by convert add_lt_add hxr'.1 hys'.1 using 1; norm_cast; linarith, by convert add_lt_add hxr'.2 hys'.2 using 1; norm_cast; linarith⟩ lemma is_st_neg {x : ℝ*} {r : ℝ} (hxr : is_st x r) : is_st (-x) (-r) := λ d hd, by show -(r : ℝ*) - d < -x ∧ -x < -r + d; cases (hxr d hd); split; linarith lemma is_st_sub {x y : ℝ*} {r s : ℝ} : is_st x r → is_st y s → is_st (x - y) (r - s) := λ hxr hys, by rw [sub_eq_add_neg, sub_eq_add_neg]; exact is_st_add hxr (is_st_neg hys) /- (st x < st y) → (x < y) → (x ≤ y) → (st x ≤ st y) -/ lemma lt_of_is_st_lt {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) : r < s → x < y := λ hrs, have hrs' : 0 < (s - r) / 2 := half_pos (sub_pos.mpr hrs), have hxr' : _ := (hxr _ hrs').2, have hys' : _ := (hys _ hrs').1, have H1 : r + ((s - r) / 2) = (r + s) / 2 := by linarith, have H2 : s - ((s - r) / 2) = (r + s) / 2 := by linarith, begin norm_cast at *, rw H1 at hxr', rw H2 at hys', exact lt_trans hxr' hys' end lemma is_st_le_of_le {x y : ℝ*} {r s : ℝ} (hrx : is_st x r) (hsy : is_st y s) : x ≤ y → r ≤ s := by rw [←not_lt, ←not_lt, not_imp_not]; exact lt_of_is_st_lt hsy hrx lemma st_le_of_le {x y : ℝ*} (hix : ¬ infinite x) (hiy : ¬ infinite y) : x ≤ y → st x ≤ st y := have hx' : _ := is_st_st' hix, have hy' : _ := is_st_st' hiy, is_st_le_of_le hx' hy' lemma lt_of_st_lt {x y : ℝ*} (hix : ¬ infinite x) (hiy : ¬ infinite y) : st x < st y → x < y := have hx' : _ := is_st_st' hix, have hy' : _ := is_st_st' hiy, lt_of_is_st_lt hx' hy' -- BASIC LEMMAS ABOUT INFINITE lemma infinite_pos_def {x : ℝ*} : infinite_pos x ↔ ∀ r : ℝ, x > r := by rw iff_eq_eq; refl lemma infinite_neg_def {x : ℝ*} : infinite_neg x ↔ ∀ r : ℝ, x < r := by rw iff_eq_eq; refl lemma ne_zero_of_infinite {x : ℝ*} : infinite x → x ≠ 0 := λ hI h0, or.cases_on hI (λ hip, lt_irrefl (0 : ℝ*) ((by rwa ←h0 : infinite_pos 0) 0)) (λ hin, lt_irrefl (0 : ℝ*) ((by rwa ←h0 : infinite_neg 0) 0)) lemma not_infinite_zero : ¬ infinite 0 := λ hI, ne_zero_of_infinite hI rfl lemma pos_of_infinite_pos {x : ℝ*} : infinite_pos x → x > 0 := λ hip, hip 0 lemma neg_of_infinite_neg {x : ℝ*} : infinite_neg x → x < 0 := λ hin, hin 0 lemma not_infinite_pos_of_infinite_neg {x : ℝ*} : infinite_neg x → ¬ infinite_pos x := λ hn hp, not_lt_of_lt (hn 1) (hp 1) lemma not_infinite_neg_of_infinite_pos {x : ℝ*} : infinite_pos x → ¬ infinite_neg x := imp_not_comm.mp not_infinite_pos_of_infinite_neg lemma infinite_neg_neg_of_infinite_pos {x : ℝ*} : infinite_pos x → infinite_neg (-x) := λ hp r, neg_lt.mp (hp (-r)) lemma infinite_pos_neg_of_infinite_neg {x : ℝ*} : infinite_neg x → infinite_pos (-x) := λ hp r, lt_neg.mp (hp (-r)) lemma infinite_pos_iff_infinite_neg_neg {x : ℝ*} : infinite_pos x ↔ infinite_neg (-x) := ⟨ infinite_neg_neg_of_infinite_pos, λ hin, neg_neg x ▸ infinite_pos_neg_of_infinite_neg hin ⟩ lemma infinite_neg_iff_infinite_pos_neg {x : ℝ*} : infinite_neg x ↔ infinite_pos (-x) := ⟨ infinite_pos_neg_of_infinite_neg, λ hin, neg_neg x ▸ infinite_neg_neg_of_infinite_pos hin ⟩ lemma infinite_iff_infinite_neg {x : ℝ*} : infinite x ↔ infinite (-x) := ⟨ λ hi, or.cases_on hi (λ hip, or.inr (infinite_neg_neg_of_infinite_pos hip)) (λ hin, or.inl (infinite_pos_neg_of_infinite_neg hin)), λ hi, or.cases_on hi (λ hipn, or.inr (infinite_neg_iff_infinite_pos_neg.mpr hipn)) (λ hinp, or.inl (infinite_pos_iff_infinite_neg_neg.mpr hinp))⟩ lemma not_infinite_of_infinitesimal {x : ℝ*} : infinitesimal x → ¬ infinite x := λ hi hI, have hi' : _ := (hi 2 two_pos), or.dcases_on hI (λ hip, have hip' : _ := hip 2, not_lt_of_lt hip' (by convert hi'.2; exact (zero_add 2).symm)) (λ hin, have hin' : _ := hin (-2), not_lt_of_lt hin' (by convert hi'.1; exact (zero_sub 2).symm)) lemma not_infinitesimal_of_infinite {x : ℝ*} : infinite x → ¬ infinitesimal x := imp_not_comm.mp not_infinite_of_infinitesimal lemma not_infinitesimal_of_infinite_pos {x : ℝ*} : infinite_pos x → ¬ infinitesimal x := λ hp, not_infinitesimal_of_infinite (or.inl hp) lemma not_infinitesimal_of_infinite_neg {x : ℝ*} : infinite_neg x → ¬ infinitesimal x := λ hn, not_infinitesimal_of_infinite (or.inr hn) lemma infinite_pos_iff_infinite_and_pos {x : ℝ*} : infinite_pos x ↔ (infinite x ∧ x > 0) := ⟨ λ hip, ⟨or.inl hip, hip 0⟩, λ ⟨hi, hp⟩, hi.cases_on (λ hip, hip) (λ hin, false.elim (not_lt_of_lt hp (hin 0))) ⟩ lemma infinite_neg_iff_infinite_and_neg {x : ℝ*} : infinite_neg x ↔ (infinite x ∧ x < 0) := ⟨ λ hip, ⟨or.inr hip, hip 0⟩, λ ⟨hi, hp⟩, hi.cases_on (λ hin, false.elim (not_lt_of_lt hp (hin 0))) (λ hip, hip) ⟩ lemma infinite_pos_iff_infinite_of_pos {x : ℝ*} (hp : x > 0) : infinite_pos x ↔ infinite x := by rw [infinite_pos_iff_infinite_and_pos]; exact ⟨λ hI, hI.1, λ hI, ⟨hI, hp⟩⟩ lemma infinite_pos_iff_infinite_of_nonneg {x : ℝ*} (hp : x ≥ 0) : infinite_pos x ↔ infinite x := or.cases_on (lt_or_eq_of_le hp) (infinite_pos_iff_infinite_of_pos) (λ h, by rw h.symm; exact ⟨λ hIP, false.elim (not_infinite_zero (or.inl hIP)), λ hI, false.elim (not_infinite_zero hI)⟩) lemma infinite_neg_iff_infinite_of_neg {x : ℝ*} (hn : x < 0) : infinite_neg x ↔ infinite x := by rw [infinite_neg_iff_infinite_and_neg]; exact ⟨λ hI, hI.1, λ hI, ⟨hI, hn⟩⟩ lemma infinite_pos_abs_iff_infinite_abs {x : ℝ*} : infinite_pos (abs x) ↔ infinite (abs x) := infinite_pos_iff_infinite_of_nonneg (abs_nonneg _) lemma infinite_iff_infinite_pos_abs {x : ℝ*} : infinite x ↔ infinite_pos (abs x) := ⟨ λ hi d, or.cases_on hi (λ hip, by rw [abs_of_pos (hip 0)]; exact hip d) (λ hin, by rw [abs_of_neg (hin 0)]; exact lt_neg.mp (hin (-d))), λ hipa, by { rcases (lt_trichotomy x 0) with h | h | h, { exact or.inr (infinite_neg_iff_infinite_pos_neg.mpr (by rwa abs_of_neg h at hipa)) }, { exact false.elim (ne_zero_of_infinite (or.inl (by rw [h]; rwa [h, abs_zero] at hipa)) h) }, { exact or.inl (by rwa abs_of_pos h at hipa) } } ⟩ lemma infinite_iff_infinite_abs {x : ℝ*} : infinite x ↔ infinite (abs x) := by rw [←infinite_pos_iff_infinite_of_nonneg (abs_nonneg _), infinite_iff_infinite_pos_abs] lemma infinite_iff_abs_lt_abs {x : ℝ*} : infinite x ↔ ∀ r : ℝ, (abs r : ℝ*) < abs x := ⟨ λ hI r, (of_abs U r) ▸ infinite_iff_infinite_pos_abs.mp hI (abs r), λ hR, or.cases_on (max_choice x (-x)) (λ h, or.inl $ λ r, lt_of_le_of_lt (le_abs_self _) (h ▸ (hR r))) (λ h, or.inr $ λ r, neg_lt_neg_iff.mp $ lt_of_le_of_lt (neg_le_abs_self _) (h ▸ (hR r)))⟩ lemma infinite_pos_add_not_infinite_neg {x y : ℝ*} : infinite_pos x → ¬ infinite_neg y → infinite_pos (x + y) := begin intros hip hnin r, cases not_forall.mp hnin with r₂ hr₂, convert add_lt_add_of_lt_of_le (hip (r + -r₂)) (not_lt.mp hr₂) using 1, simp end lemma not_infinite_neg_add_infinite_pos {x y : ℝ*} : ¬ infinite_neg x → infinite_pos y → infinite_pos (x + y) := λ hx hy, by rw [add_comm]; exact infinite_pos_add_not_infinite_neg hy hx lemma infinite_neg_add_not_infinite_pos {x y : ℝ*} : infinite_neg x → ¬ infinite_pos y → infinite_neg (x + y) := by rw [@infinite_neg_iff_infinite_pos_neg x, @infinite_pos_iff_infinite_neg_neg y, @infinite_neg_iff_infinite_pos_neg (x + y), neg_add]; exact infinite_pos_add_not_infinite_neg lemma not_infinite_pos_add_infinite_neg {x y : ℝ*} : ¬ infinite_pos x → infinite_neg y → infinite_neg (x + y) := λ hx hy, by rw [add_comm]; exact infinite_neg_add_not_infinite_pos hy hx lemma infinite_pos_add_infinite_pos {x y : ℝ*} : infinite_pos x → infinite_pos y → infinite_pos (x + y) := λ hx hy, infinite_pos_add_not_infinite_neg hx (not_infinite_neg_of_infinite_pos hy) lemma infinite_neg_add_infinite_neg {x y : ℝ*} : infinite_neg x → infinite_neg y → infinite_neg (x + y) := λ hx hy, infinite_neg_add_not_infinite_pos hx (not_infinite_pos_of_infinite_neg hy) lemma infinite_pos_add_not_infinite {x y : ℝ*} : infinite_pos x → ¬ infinite y → infinite_pos (x + y) := λ hx hy, infinite_pos_add_not_infinite_neg hx (not_or_distrib.mp hy).2 lemma infinite_neg_add_not_infinite {x y : ℝ*} : infinite_neg x → ¬ infinite y → infinite_neg (x + y) := λ hx hy, infinite_neg_add_not_infinite_pos hx (not_or_distrib.mp hy).1 theorem infinite_pos_of_tendsto_top {f : ℕ → ℝ} (hf : tendsto f at_top at_top) : infinite_pos (of_seq f) := λ r, have hf' : _ := (tendsto_at_top_at_top _).mp hf, Exists.cases_on (hf' (r + 1)) $ λ i hi, have hi' : ∀ (a : ℕ), f a < (r + 1) → a < i := λ a, by rw [←not_le, ←not_le]; exact not_imp_not.mpr (hi a), have hS : - {a : ℕ | r < f a} ⊆ {a : ℕ | a ≤ i} := by simp only [set.compl_set_of, not_lt]; exact λ a har, le_of_lt (hi' a (lt_of_le_of_lt har (lt_add_one _))), (lt_def U).mpr $ mem_hyperfilter_of_finite_compl $ set.finite_subset (set.finite_le_nat _) hS theorem infinite_neg_of_tendsto_bot {f : ℕ → ℝ} (hf : tendsto f at_top at_bot) : infinite_neg (of_seq f) := λ r, have hf' : _ := (tendsto_at_top_at_bot _).mp hf, Exists.cases_on (hf' (r - 1)) $ λ i hi, have hi' : ∀ (a : ℕ), r - 1 < f a → a < i := λ a, by rw [←not_le, ←not_le]; exact not_imp_not.mpr (hi a), have hS : - {a : ℕ | f a < r} ⊆ {a : ℕ | a ≤ i} := by simp only [set.compl_set_of, not_lt]; exact λ a har, le_of_lt (hi' a (lt_of_lt_of_le (sub_one_lt _) har)), (lt_def U).mpr $ mem_hyperfilter_of_finite_compl $ set.finite_subset (set.finite_le_nat _) hS lemma not_infinite_neg {x : ℝ*} : ¬ infinite x → ¬ infinite (-x) := not_imp_not.mpr infinite_iff_infinite_neg.mpr lemma not_infinite_add {x y : ℝ*} (hx : ¬ infinite x) (hy : ¬ infinite y) : ¬ infinite (x + y) := have hx' : _ := exists_st_of_not_infinite hx, have hy' : _ := exists_st_of_not_infinite hy, Exists.cases_on hx' $ Exists.cases_on hy' $ λ r hr s hs, not_infinite_of_exists_st $ ⟨s + r, is_st_add hs hr⟩ theorem not_infinite_iff_exist_lt_gt {x : ℝ*} : ¬ infinite x ↔ ∃ r s : ℝ, (r : ℝ*) < x ∧ x < s := ⟨ λ hni, Exists.dcases_on (not_forall.mp (not_or_distrib.mp hni).1) $ Exists.dcases_on (not_forall.mp (not_or_distrib.mp hni).2) $ λ r hr s hs, by rw [not_lt] at hr hs; exact ⟨r - 1, s + 1, ⟨ lt_of_lt_of_le (by rw sub_eq_add_neg; norm_num) hr, lt_of_le_of_lt hs (by norm_num)⟩ ⟩, λ hrs, Exists.dcases_on hrs $ λ r hr, Exists.dcases_on hr $ λ s hs, not_or_distrib.mpr ⟨not_forall.mpr ⟨s, lt_asymm (hs.2)⟩, not_forall.mpr ⟨r, lt_asymm (hs.1) ⟩⟩⟩ theorem not_infinite_real (r : ℝ) : ¬ infinite r := by rw not_infinite_iff_exist_lt_gt; exact ⟨ r - 1, r + 1, by rw [←of_eq_coe, ←of_eq_coe, ←of_lt U]; exact sub_one_lt _, by rw [←of_eq_coe, ←of_eq_coe, ←of_lt U]; exact lt_add_one _⟩ theorem not_real_of_infinite {x : ℝ*} : infinite x → ∀ r : ℝ, x ≠ of r := λ hi r hr, not_infinite_real r $ @eq.subst _ infinite _ _ hr hi -- FACTS ABOUT ST THAT REQUIRE SOME INFINITE MACHINERY private lemma is_st_mul' {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) (hs : s ≠ 0) : is_st (x * y) (r * s) := have hxr' : _ := is_st_iff_abs_sub_lt_delta.mp hxr, have hys' : _ := is_st_iff_abs_sub_lt_delta.mp hys, have h : _ := not_infinite_iff_exist_lt_gt.mp $ not_imp_not.mpr infinite_iff_infinite_abs.mpr $ not_infinite_of_exists_st ⟨r, hxr⟩, Exists.cases_on h $ λ u h', Exists.cases_on h' $ λ t ⟨hu, ht⟩, is_st_iff_abs_sub_lt_delta.mpr $ λ d hd, calc abs (x * y - of (r * s)) = abs (x * y - (of r) * (of s)) : by simp ... = abs (x * (y - of s) + (x - of r) * (of s)) : by rw [mul_sub, sub_mul, add_sub, sub_add_cancel] ... ≤ abs (x * (y - of s)) + abs ((x - of r) * (of s)) : abs_add _ _ ... ≤ abs x * abs (y - of s) + abs (x - of r) * abs (of s) : by simp only [abs_mul] ... ≤ abs x * ((d / t) / 2 : ℝ) + ((d / abs s) / 2 : ℝ) * abs s : add_le_add (mul_le_mul_of_nonneg_left (le_of_lt $ hys' _ $ half_pos $ div_pos hd $ (of_lt U).mpr $ lt_of_le_of_lt (abs_nonneg _) ht) $ abs_nonneg _ ) (mul_le_mul_of_nonneg_right (le_of_lt $ hxr' _ $ half_pos $ div_pos hd $ abs_pos_of_ne_zero hs) $ abs_nonneg _) ... = (d / 2 * (abs x / t) + d / 2 : ℝ*) : by { rw [div_div_eq_div_mul, mul_comm t 2, ←div_div_eq_div_mul, of_div U, div_div_eq_div_mul, mul_comm (abs s) 2, ←div_div_eq_div_mul, mul_div_comm, of_div U, of_div U, of_div U, of_abs U, div_mul_cancel _ (ne_of_gt (abs_pos_of_ne_zero ((of_ne_zero U.1 _).mp hs)))], refl } ... < (d / 2 * 1 + d / 2 : ℝ*) : add_lt_add_right (mul_lt_mul_of_pos_left ((div_lt_one_iff_lt $ lt_of_le_of_lt (abs_nonneg x) ht).mpr ht) $ half_pos $ of_lt_of_lt U hd) _ ... = (d : ℝ*) : by rw [mul_one, add_halves] lemma is_st_mul {x y : ℝ*} {r s : ℝ} (hxr : is_st x r) (hys : is_st y s) : is_st (x * y) (r * s) := have h : _ := not_infinite_iff_exist_lt_gt.mp $ not_imp_not.mpr infinite_iff_infinite_abs.mpr $ not_infinite_of_exists_st ⟨r, hxr⟩, Exists.cases_on h $ λ u h', Exists.cases_on h' $ λ t ⟨hu, ht⟩, begin by_cases hs : s = 0, { apply is_st_iff_abs_sub_lt_delta.mpr, intros d hd, have hys' : _ := is_st_iff_abs_sub_lt_delta.mp hys (d / t) (div_pos hd ((of_lt U).mpr (lt_of_le_of_lt (abs_nonneg x) ht))), rw [hs, of_zero, sub_zero] at hys', rw [hs, mul_zero, of_zero, sub_zero, abs_mul, mul_comm, ←div_mul_cancel (d : ℝ*) (ne_of_gt (lt_of_le_of_lt (abs_nonneg x) ht)), ←of_div U], exact mul_lt_mul'' hys' ht (abs_nonneg _) (abs_nonneg _) }, exact is_st_mul' hxr hys hs, end --AN INFINITE LEMMA THAT REQUIRES SOME MORE ST MACHINERY lemma not_infinite_mul {x y : ℝ*} (hx : ¬ infinite x) (hy : ¬ infinite y) : ¬ infinite (x * y) := have hx' : _ := exists_st_of_not_infinite hx, have hy' : _ := exists_st_of_not_infinite hy, Exists.cases_on hx' $ Exists.cases_on hy' $ λ r hr s hs, not_infinite_of_exists_st $ ⟨s * r, is_st_mul hs hr⟩ --- lemma st_add {x y : ℝ*} (hx : ¬infinite x) (hy : ¬infinite y) : st (x + y) = st x + st y := have hx' : _ := is_st_st' hx, have hy' : _ := is_st_st' hy, have hxy : _ := is_st_st' (not_infinite_add hx hy), have hxy' : _ := is_st_add hx' hy', is_st_unique hxy hxy' lemma st_neg (x : ℝ*) : st (-x) = - st x := if h : infinite x then by rw [st_infinite h, st_infinite (infinite_iff_infinite_neg.mp h), neg_zero] else is_st_unique (is_st_st' (not_infinite_neg h)) (is_st_neg (is_st_st' h)) lemma st_mul {x y : ℝ*} (hx : ¬infinite x) (hy : ¬infinite y) : st (x * y) = (st x) * (st y) := have hx' : _ := is_st_st' hx, have hy' : _ := is_st_st' hy, have hxy : _ := is_st_st' (not_infinite_mul hx hy), have hxy' : _ := is_st_mul hx' hy', is_st_unique hxy hxy' -- BASIC LEMMAS ABOUT INFINITESIMAL theorem infinitesimal_def {x : ℝ*} : infinitesimal x ↔ (∀ r : ℝ, r > 0 → -(r : ℝ*) < x ∧ x < r) := ⟨ λ hi r hr, by { convert (hi r hr), exact (zero_sub (of r)).symm, exact (zero_add (of r)).symm }, λ hi d hd, by { convert (hi d hd), exact zero_sub (of d), exact zero_add (of d) } ⟩ theorem lt_of_pos_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, r > 0 → x < r := λ hi r hr, ((infinitesimal_def.mp hi) r hr).2 theorem lt_neg_of_pos_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, r > 0 → x > -r := λ hi r hr, ((infinitesimal_def.mp hi) r hr).1 theorem gt_of_neg_of_infinitesimal {x : ℝ*} : infinitesimal x → ∀ r : ℝ, r < 0 → x > r := λ hi r hr, by convert ((infinitesimal_def.mp hi) (-r) (neg_pos.mpr hr)).1; exact (neg_neg (of r)).symm theorem abs_lt_real_iff_infinitesimal {x : ℝ*} : infinitesimal x ↔ ∀ r : ℝ, r ≠ 0 → abs x < abs r := ⟨ λ hi r hr, abs_lt.mpr (by rw ←of_abs U; exact infinitesimal_def.mp hi (abs r) (abs_pos_of_ne_zero hr)), λ hR, infinitesimal_def.mpr $ λ r hr, abs_lt.mp $ (abs_of_pos $ of_lt_of_lt U hr : abs (r : ℝ*) = r) ▸ hR r $ ne_of_gt hr ⟩ lemma infinitesimal_zero : infinitesimal 0 := is_st_refl_real 0 lemma zero_of_infinitesimal_real {r : ℝ} : infinitesimal r → r = 0 := eq_of_is_st_real lemma zero_iff_infinitesimal_real {r : ℝ} : infinitesimal r ↔ r = 0 := ⟨zero_of_infinitesimal_real, λ hr, by rw hr; exact infinitesimal_zero⟩ lemma infinitesimal_add {x y : ℝ*} : infinitesimal x → infinitesimal y → infinitesimal (x + y) := zero_add 0 ▸ is_st_add lemma infinitesimal_neg {x : ℝ*} : infinitesimal x → infinitesimal (-x) := (neg_zero : -(0 : ℝ) = 0) ▸ is_st_neg lemma infinitesimal_neg_iff {x : ℝ*} : infinitesimal x ↔ infinitesimal (-x) := ⟨infinitesimal_neg, λ h, (neg_neg x) ▸ @infinitesimal_neg (-x) h⟩ lemma infinitesimal_mul {x y : ℝ*} : infinitesimal x → infinitesimal y → infinitesimal (x * y) := zero_mul 0 ▸ is_st_mul theorem infinitesimal_of_tendsto_zero {f : ℕ → ℝ} : tendsto f at_top (𝓝 0) → infinitesimal (of_seq f) := λ hf d hd, by rw [sub_eq_add_neg, ←of_neg, ←of_add, ←of_add, zero_add, zero_add]; exact ⟨neg_lt_of_tendsto_zero_of_pos hf hd, lt_of_tendsto_zero_of_pos hf hd⟩ theorem infinitesimal_epsilon : infinitesimal ε := infinitesimal_of_tendsto_zero tendsto_inverse_at_top_nhds_0_nat lemma not_real_of_infinitesimal_ne_zero (x : ℝ*) : infinitesimal x → x ≠ 0 → ∀ r : ℝ, x ≠ of r := λ hi hx r hr, hx (is_st_unique (hr.symm ▸ is_st_refl_real r : is_st x r) hi ▸ hr : x = of 0) theorem infinitesimal_sub_is_st {x : ℝ*} {r : ℝ} (hxr : is_st x r) : infinitesimal (x - r) := show is_st (x + -r) 0, by rw ←add_neg_self r; exact is_st_add hxr (is_st_refl_real (-r)) theorem infinitesimal_sub_st {x : ℝ*} (hx : ¬infinite x) : infinitesimal (x - st x) := infinitesimal_sub_is_st $ is_st_st' hx lemma infinite_pos_iff_infinitesimal_inv_pos {x : ℝ*} : infinite_pos x ↔ (infinitesimal x⁻¹ ∧ x⁻¹ > 0) := ⟨ λ hip, ⟨ infinitesimal_def.mpr $ λ r hr, ⟨ lt_trans (of_lt_of_lt U (neg_neg_of_pos hr)) (inv_pos.2 (hip 0)), (inv_lt (of_lt_of_lt U hr) (hip 0)).mp (by convert hip r⁻¹) ⟩, inv_pos.2 $ hip 0 ⟩, λ ⟨hi, hp⟩ r, @classical.by_cases (r = 0) (x > (r : ℝ*)) (λ h, eq.substr h (inv_pos.mp hp)) $ λ h, lt_of_le_of_lt (of_le_of_le (le_abs_self r)) ((inv_lt_inv (inv_pos.mp hp) (of_lt_of_lt U (abs_pos_of_ne_zero h))).mp ((infinitesimal_def.mp hi) ((abs r)⁻¹) (inv_pos.2 (abs_pos_of_ne_zero h))).2) ⟩ lemma infinite_neg_iff_infinitesimal_inv_neg {x : ℝ*} : infinite_neg x ↔ (infinitesimal x⁻¹ ∧ x⁻¹ < 0) := ⟨ λ hin, have hin' : _ := infinite_pos_iff_infinitesimal_inv_pos.mp (infinite_pos_neg_of_infinite_neg hin), by rwa [infinitesimal_neg_iff, ←neg_pos, neg_inv], λ hin, have h0 : x ≠ 0 := λ h0, (lt_irrefl (0 : ℝ*) (by convert hin.2; rw [h0, inv_zero])), by rwa [←neg_pos, infinitesimal_neg_iff, neg_inv, ←infinite_pos_iff_infinitesimal_inv_pos, ←infinite_neg_iff_infinite_pos_neg] at hin ⟩ theorem infinitesimal_inv_of_infinite {x : ℝ*} : infinite x → infinitesimal x⁻¹ := λ hi, or.cases_on hi (λ hip, (infinite_pos_iff_infinitesimal_inv_pos.mp hip).1) (λ hin, (infinite_neg_iff_infinitesimal_inv_neg.mp hin).1) theorem infinite_of_infinitesimal_inv {x : ℝ*} (h0 : x ≠ 0) (hi : infinitesimal x⁻¹ ) : infinite x := begin cases (lt_or_gt_of_ne h0) with hn hp, { exact or.inr (infinite_neg_iff_infinitesimal_inv_neg.mpr ⟨hi, inv_lt_zero.mpr hn⟩) }, { exact or.inl (infinite_pos_iff_infinitesimal_inv_pos.mpr ⟨hi, inv_pos.mpr hp⟩) } end theorem infinite_iff_infinitesimal_inv {x : ℝ*} (h0 : x ≠ 0) : infinite x ↔ infinitesimal x⁻¹ := ⟨ infinitesimal_inv_of_infinite, infinite_of_infinitesimal_inv h0 ⟩ lemma infinitesimal_pos_iff_infinite_pos_inv {x : ℝ*} : infinite_pos x⁻¹ ↔ (infinitesimal x ∧ x > 0) := by convert infinite_pos_iff_infinitesimal_inv_pos; simp only [inv_inv'] lemma infinitesimal_neg_iff_infinite_neg_inv {x : ℝ*} : infinite_neg x⁻¹ ↔ (infinitesimal x ∧ x < 0) := by convert infinite_neg_iff_infinitesimal_inv_neg; simp only [inv_inv'] theorem infinitesimal_iff_infinite_inv {x : ℝ*} (h : x ≠ 0) : infinitesimal x ↔ infinite x⁻¹ := by convert (infinite_iff_infinitesimal_inv (inv_ne_zero h)).symm; simp only [inv_inv'] -- ST STUFF THAT REQUIRES INFINITESIMAL MACHINERY theorem is_st_of_tendsto {f : ℕ → ℝ} {r : ℝ} (hf : tendsto f at_top (𝓝 r)) : is_st (of_seq f) r := have hg : tendsto (λ n, f n - r) at_top (𝓝 0) := (sub_self r) ▸ (hf.sub tendsto_const_nhds), by rw [←(zero_add r), ←(sub_add_cancel f (λ n, r))]; exact is_st_add (infinitesimal_of_tendsto_zero hg) (is_st_refl_real r) lemma is_st_inv {x : ℝ*} {r : ℝ} (hi : ¬ infinitesimal x) : is_st x r → is_st x⁻¹ r⁻¹ := λ hxr, have h : x ≠ 0 := (λ h, hi (h.symm ▸ infinitesimal_zero)), have H : _ := exists_st_of_not_infinite $ not_imp_not.mpr (infinitesimal_iff_infinite_inv h).mpr hi, Exists.cases_on H $ λ s hs, have H' : is_st 1 (r * s) := mul_inv_cancel h ▸ is_st_mul hxr hs, have H'' : s = r⁻¹ := one_div_eq_inv r ▸ eq_one_div_of_mul_eq_one (eq_of_is_st_real H').symm, H'' ▸ hs lemma st_inv (x : ℝ*) : st x⁻¹ = (st x)⁻¹ := begin by_cases h0 : x = 0, rw [h0, inv_zero, ←of_zero, st_id_real, inv_zero], by_cases h1 : infinitesimal x, rw [st_infinite ((infinitesimal_iff_infinite_inv h0).mp h1), st_of_is_st h1, inv_zero], by_cases h2 : infinite x, rw [st_of_is_st (infinitesimal_inv_of_infinite h2), st_infinite h2, inv_zero], exact st_of_is_st (is_st_inv h1 (is_st_st' h2)), end -- INFINITE STUFF THAT REQUIRES INFINITESIMAL MACHINERY lemma infinite_pos_omega : infinite_pos ω := infinite_pos_iff_infinitesimal_inv_pos.mpr ⟨infinitesimal_epsilon, epsilon_pos⟩ lemma infinite_omega : infinite ω := (infinite_iff_infinitesimal_inv omega_ne_zero).mpr infinitesimal_epsilon lemma infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos {x y : ℝ*} : infinite_pos x → ¬ infinitesimal y → y > 0 → infinite_pos (x * y) := λ hx hy₁ hy₂ r, have hy₁' : _ := not_forall.mp (by rw infinitesimal_def at hy₁; exact hy₁), Exists.dcases_on hy₁' $ λ r₁ hy₁'', have hyr : _ := by rw [not_imp, ←abs_lt, not_lt, abs_of_pos hy₂] at hy₁''; exact hy₁'', by rw [←div_mul_cancel r (ne_of_gt hyr.1), of_mul]; exact mul_lt_mul (hx (r / r₁)) hyr.2 (of_lt_of_lt U hyr.1) (le_of_lt (hx 0)) lemma infinite_pos_mul_of_not_infinitesimal_pos_infinite_pos {x y : ℝ*} : ¬ infinitesimal x → 0 < x → infinite_pos y → infinite_pos (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos hy hx hp lemma infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg {x y : ℝ*} : infinite_neg x → ¬ infinitesimal y → y < 0 → infinite_pos (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, ←neg_pos, ←neg_mul_neg, infinitesimal_neg_iff]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_pos_mul_of_not_infinitesimal_neg_infinite_neg {x y : ℝ*} : ¬ infinitesimal x → x < 0 → infinite_neg y → infinite_pos (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg hy hx hp lemma infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg {x y : ℝ*} : infinite_pos x → ¬ infinitesimal y → y < 0 → infinite_neg (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, ←neg_pos, neg_mul_eq_mul_neg, infinitesimal_neg_iff]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_neg_mul_of_not_infinitesimal_neg_infinite_pos {x y : ℝ*} : ¬ infinitesimal x → x < 0 → infinite_pos y → infinite_neg (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg hy hx hp lemma infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos {x y : ℝ*} : infinite_neg x → ¬ infinitesimal y → 0 < y → infinite_neg (x * y) := by rw [infinite_neg_iff_infinite_pos_neg, infinite_neg_iff_infinite_pos_neg, neg_mul_eq_neg_mul]; exact infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos lemma infinite_neg_mul_of_not_infinitesimal_pos_infinite_neg {x y : ℝ*} : ¬ infinitesimal x → x > 0 → infinite_neg y → infinite_neg (x * y) := λ hx hp hy, by rw mul_comm; exact infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos hy hx hp lemma infinite_pos_mul_infinite_pos {x y : ℝ*} : infinite_pos x → infinite_pos y → infinite_pos (x * y) := λ hx hy, infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos hx (not_infinitesimal_of_infinite_pos hy) (hy 0) lemma infinite_neg_mul_infinite_neg {x y : ℝ*} : infinite_neg x → infinite_neg y → infinite_pos (x * y) := λ hx hy, infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg hx (not_infinitesimal_of_infinite_neg hy) (hy 0) lemma infinite_pos_mul_infinite_neg {x y : ℝ*} : infinite_pos x → infinite_neg y → infinite_neg (x * y) := λ hx hy, infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg hx (not_infinitesimal_of_infinite_neg hy) (hy 0) lemma infinite_neg_mul_infinite_pos {x y : ℝ*} : infinite_neg x → infinite_pos y → infinite_neg (x * y) := λ hx hy, infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos hx (not_infinitesimal_of_infinite_pos hy) (hy 0) lemma infinite_mul_of_infinite_not_infinitesimal {x y : ℝ*} : infinite x → ¬ infinitesimal y → infinite (x * y) := λ hx hy, have h0 : y < 0 ∨ y > 0 := lt_or_gt_of_ne (λ H0, hy (eq.substr H0 (is_st_refl_real 0))), or.dcases_on hx (or.dcases_on h0 (λ H0 Hx, or.inr (infinite_neg_mul_of_infinite_pos_not_infinitesimal_neg Hx hy H0)) (λ H0 Hx, or.inl (infinite_pos_mul_of_infinite_pos_not_infinitesimal_pos Hx hy H0))) (or.dcases_on h0 (λ H0 Hx, or.inl (infinite_pos_mul_of_infinite_neg_not_infinitesimal_neg Hx hy H0)) (λ H0 Hx, or.inr (infinite_neg_mul_of_infinite_neg_not_infinitesimal_pos Hx hy H0))) lemma infinite_mul_of_not_infinitesimal_infinite {x y : ℝ*} : ¬ infinitesimal x → infinite y → infinite (x * y) := λ hx hy, by rw [mul_comm]; exact infinite_mul_of_infinite_not_infinitesimal hy hx lemma infinite_mul_infinite {x y : ℝ*} : infinite x → infinite y → infinite (x * y) := λ hx hy, infinite_mul_of_infinite_not_infinitesimal hx (not_infinitesimal_of_infinite hy) end hyperreal
26ad01dc3e28f1aaf0ca498be434ba3686bb1562
4f643cce24b2d005aeeb5004c2316a8d6cc7f3b1
/omin/def_coords.lean
a57717556f2c250eb1a1c69a5f3275291b2dbda2
[]
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
2,835
lean
import o_minimal.coordinates import o_minimal.structure -- Stripped-down version of `o_minimal.definable` -- without implicit definability hypotheses on the types. universe u namespace o_minimal open_locale finvec variables {R : Type u} (S : struc R) variables {X : Type*} [has_coordinates R X] variables {Y : Type*} [has_coordinates R Y] def struc.def_coords (s : set X) : Prop := S.definable (coords R '' s) namespace struc.def_coords variables {S} lemma diag (dX : S.def_coords (set.univ : set X)) : S.def_coords {p : X × X | p.1 = p.2} := begin unfold struc.def_coords, convert S.definable_inter (S.definable_prod_rn dX) S.definable_diag_rn, refine set.ext (finvec.rec (λ v w, _)), have : (∃ (x : X), coords R x = v ∧ coords R x = w) ↔ v ∈ coordinate_image R X ∧ v = w, { split, { rintros ⟨x, rfl, rfl⟩, exact ⟨⟨x, rfl⟩, rfl⟩ }, { rintros ⟨⟨x, rfl⟩, rfl⟩, exact ⟨x, rfl, rfl⟩ } }, simp, dsimp, simpa [finvec.append.inj_iff] end lemma reindex (dX : S.def_coords (set.univ : set X)) {f : X → Y} (hf : is_reindexing R f) {s : set Y} (ds : S.def_coords s) : S.def_coords (f ⁻¹' s) := begin cases hf with fσ hf, unfold struc.def_coords, -- The preimage f ⁻¹' s, as a subset of the Rⁿ in which X lives, -- is the intersection of X with the preimage of s under the reindexing. convert S.definable_inter dX (S.definable_reindex fσ ds), ext z, suffices : (∃ (x : X), f x ∈ s ∧ coords R x = z) ↔ z ∈ set.range (@coords R X _) ∧ ∃ (y : Y), y ∈ s ∧ coords R y = z ∘ fσ, { simpa }, -- TODO: funext'd version of `is_reindexing.hf` replace hf : ∀ (x : X), coords R x ∘ fσ = coords R (f x) := λ x, funext (λ i, (hf x i)), split, { rintro ⟨x, hfx, rfl⟩, refine ⟨set.mem_range_self _, f x, hfx, (hf x).symm⟩ }, { rintro ⟨⟨x, rfl⟩, y, hy, H⟩, rw hf x at H, replace hf := injective_coords _ H, subst y, exact ⟨x, hy, rfl⟩ } end lemma «exists» {s : X → Y → Prop} (ds : S.def_coords {p : X × Y | s p.1 p.2}) : S.def_coords {x | ∃ y, s x y} := begin unfold struc.def_coords at ⊢ ds, convert S.definable_proj ds using 1, ext x, rw [set.image_image], simp end lemma inter {s t : set X} (ds : S.def_coords s) (dt : S.def_coords t) : S.def_coords (s ∩ t) := begin unfold struc.def_coords, convert S.definable_inter ds dt, simp [set.image_inter (injective_coords X)] end lemma prod_univ (dX : S.def_coords (set.univ : set X)) (dY : S.def_coords (set.univ : set Y)) : S.def_coords (set.univ : set (X × Y)) := begin unfold struc.def_coords, convert S.definable_external_prod dX dY, refine set.ext (finvec.rec (λ v w, _)), simp [finvec.append.inj_iff, finvec.append_mem_prod_iff] end end struc.def_coords end o_minimal
f703f4eca2c47340baba2bba57954524b83a3a18
aac33c518959cd0633fdc254edbbf27b2f581c31
/src/analysis/convex.lean
04a5d98da96ceed07822111368d0f1c068b902c0
[ "Apache-2.0" ]
permissive
digama0/mathlib-ITP2019
992c4f9ac02260fca4a14860813c3ecbd5ca1ae6
5cbd0362e04e671ef5db1284870592af6950197c
refs/heads/master
1,588,517,123,478
1,554,081,078,000
1,554,081,078,000
178,686,466
2
1
null
null
null
null
UTF-8
Lean
false
false
28,101
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp Convex sets and functions on real vector spaces -/ import analysis.normed_space.basic import data.complex.basic import data.set.intervals import tactic.interactive import tactic.linarith import linear_algebra.basic import ring_theory.algebra local attribute [instance] classical.prop_decidable open set variables {α : Type*} {β : Type*} {ι : Sort _} [add_comm_group α] [vector_space ℝ α] [add_comm_group β] [vector_space ℝ β] (A : set α) (B : set α) (x : α) /-- Convexity of sets -/ def convex (A : set α) := ∀ (x y : α) (a b : ℝ), x ∈ A → y ∈ A → 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ A /-- Alternative definition of set convexity -/ lemma convex_iff: convex A ↔ ∀ {x y : α} {θ : ℝ}, x ∈ A → y ∈ A → 0 ≤ θ → θ ≤ 1 → θ • x + (1 - θ) • y ∈ A := ⟨begin assume h x y θ hx hy hθ₁ hθ₂, have hθ₂ : 0 ≤ 1 - θ, by linarith, exact (h _ _ _ _ hx hy hθ₁ hθ₂ (by linarith)) end, begin assume h x y a b hx hy ha hb hab, have ha' : a ≤ 1, by linarith, have hb' : b = 1 - a, by linarith, rw hb', exact h hx hy ha ha' end⟩ /-- Another alternative definition of set convexity -/ lemma convex_iff_div: convex A ↔ ∀ {x y : α} {a : ℝ} {b : ℝ}, x ∈ A → y ∈ A → 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ A := ⟨begin assume h x y a b hx hy ha hb hab, apply h _ _ _ _ hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin assume h x y a b hx hy ha hb hab, have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ local notation `I` := (Icc 0 1 : set ℝ) /-- Segments in a vector space -/ def segment (x y : α) := {z : α | ∃ l : ℝ, l ∈ I ∧ z - x = l•(y-x)} local notation `[`x `, ` y `]` := segment x y lemma left_mem_segment (x y : α) : x ∈ [x, y] := ⟨0, ⟨⟨le_refl _, zero_le_one⟩, by simp⟩⟩ lemma right_mem_segment (x y : α) : y ∈ [x, y] := ⟨1, ⟨⟨zero_le_one, le_refl _⟩, by simp⟩⟩ lemma mem_segment_iff {x y z : α} : z ∈ [x, y] ↔ ∃ l ∈ I, z = x + l•(y - x) := by split; rintro ⟨l, l_in, H⟩; use [l, l_in]; try { rw sub_eq_iff_eq_add at H }; rw H; abel lemma mem_segment_iff' {x y z : α} : z ∈ [x, y] ↔ ∃ l ∈ I, z = ((1:ℝ)-l)•x + l•y := begin split; rintro ⟨l, l_in, H⟩; use [l, l_in]; try { rw sub_eq_iff_eq_add at H }; rw H; simp only [smul_sub, sub_smul, one_smul]; abel, end lemma segment_symm (x y : α) : [x, y] = [y, x] := begin ext z, rw [mem_segment_iff', mem_segment_iff'], split, all_goals { rintro ⟨l, ⟨hl₀, hl₁⟩, h⟩, use (1-l), split, split; linarith, rw [h]; simp }, end lemma segment_eq_Icc {a b : ℝ} (h : a ≤ b) : [a, b] = Icc a b := begin ext z, rw mem_segment_iff, split, { rintro ⟨l, ⟨hl₀, hl₁⟩, H⟩, rw smul_eq_mul at H, have hba : 0 ≤ b - a, by linarith, split ; rw H, { have := mul_le_mul (le_refl l) hba (le_refl _) hl₀, simpa using this, }, { have := mul_le_mul hl₁ (le_refl (b-a)) hba zero_le_one, rw one_mul at this, apply le_trans (add_le_add (le_refl a) this), convert le_refl _, show b = a + (b-a), by ring } }, { rintro ⟨hza, hzb⟩, by_cases hba : b-a = 0, { use [(0:ℝ), ⟨le_refl 0, zero_le_one⟩], rw zero_smul, linarith }, { have : (z-a)/(b-a) ∈ I, { change b -a ≠ 0 at hba, have : 0 < b - a, from lt_of_le_of_ne (by linarith) hba.symm, split, apply div_nonneg ; linarith, apply (div_le_iff this).2, simp, convert hzb, ring}, use [(z-a)/(b-a), this], rw [smul_eq_mul, div_mul_cancel], ring, exact hba } } end lemma segment_translate (a b c x : α) (hx : x ∈ [b, c]) : a + x ∈ [a + b, a + c] := begin refine exists.elim hx (λθ hθ, ⟨θ, ⟨hθ.1, _⟩⟩), simp only [smul_sub, smul_add] at *, simp [smul_add, (add_eq_of_eq_sub hθ.2.symm).symm] end lemma segment_translate_image (a b c: α) : (λx, a + x) '' [b, c] = [a + b, a + c] := begin apply subset.antisymm, { intros z hz, apply exists.elim hz, intros x hx, convert segment_translate a b c x _, { exact hx.2.symm }, { exact hx.1 } }, { intros z hz, apply exists.elim hz, intros θ hθ, use z - a, apply and.intro, { convert segment_translate (-a) (a + b) (a + c) z hz; simp }, { simp only [add_sub_cancel'_right] } } end /-- Alternative defintion of set convexity using segments -/ lemma convex_segment_iff : convex A ↔ ∀ x y ∈ A, [x, y] ⊆ A := begin apply iff.intro, { intros hA x y hx hy z hseg, apply exists.elim hseg, intros l hl, have hz : z = l • y + (1-l) • x, { rw sub_eq_iff_eq_add.1 hl.2, rw [smul_sub, sub_smul, one_smul], simp }, rw hz, apply (convex_iff A).1 hA hy hx hl.1.1 hl.1.2 }, { intros hA, rw convex_iff, intros x y θ hx hy hθ₀ hθ₁, apply hA y x hy hx, use θ, apply and.intro, { exact and.intro hθ₀ hθ₁ }, { simp only [smul_sub, sub_smul, one_smul], simp } } end /- Examples of convex sets -/ lemma convex_empty : convex (∅ : set α) := by finish lemma convex_singleton (a : α) : convex ({a} : set α) := begin intros x y a b hx hy ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab], simp end lemma convex_univ : convex (set.univ : set α) := by finish lemma convex_inter (hA: convex A) (hB: convex B) : convex (A ∩ B) := λ x y a b (hx : x ∈ A ∩ B) (hy : y ∈ A ∩ B) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hA _ _ _ _ hx.left hy.left ha hb hab, hB _ _ _ _ hx.right hy.right ha hb hab⟩ lemma convex_Inter {s: ι → set α} (h: ∀ i : ι, convex (s i)) : convex (Inter s) := begin intros x y a b hx hy ha hb hab, apply mem_Inter.2, exact λi, h i _ _ _ _ (mem_Inter.1 hx i) (mem_Inter.1 hy i) ha hb hab end lemma convex_prod {A : set α} {B : set β} (hA : convex A) (hB : convex B) : convex (set.prod A B) := begin intros x y a b hx hy ha hb hab, apply mem_prod.2, exact ⟨hA _ _ _ _ (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, hB _ _ _ _ (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma convex_linear_image (f : α → β) (hf : is_linear_map ℝ f) (hA : convex A) : convex (image f A) := begin intros x y a b hx hy ha hb hab, apply exists.elim hx, intros x' hx', apply exists.elim hy, intros y' hy', use a • x' + b • y', split, { apply hA _ _ _ _ hx'.1 hy'.1 ha hb hab }, { simp [hx',hy',hf.add,hf.smul] } end lemma convex_linear_image' (f : α →ₗ[ℝ] β) (hA : convex A) : convex (image f A) := convex_linear_image A f.to_fun (linear_map.is_linear f) hA lemma convex_linear_preimage (A : set β) (f : α → β) (hf : is_linear_map ℝ f) (hA : convex A) : convex (preimage f A) := begin intros x y a b hx hy ha hb hab, simp [hf.add, hf.smul], exact hA (f x) (f y) a b hx hy ha hb hab end lemma convex_linear_preimage' (A : set β) (f : α →ₗ[ℝ] β) (hA : convex A) : convex (preimage f A) := convex_linear_preimage A f.to_fun (linear_map.is_linear f) hA lemma convex_neg : convex A → convex ((λ z, -z) '' A) := convex_linear_image _ _ is_linear_map.is_linear_map_neg lemma convex_neg_preimage : convex A → convex ((λ z, -z) ⁻¹' A) := convex_linear_preimage _ _ is_linear_map.is_linear_map_neg lemma convex_smul (c : ℝ) : convex A → convex ((λ z, c • z) '' A) := convex_linear_image _ _ (is_linear_map.is_linear_map_smul c) lemma convex_smul_preimage (c : ℝ) : convex A → convex ((λ z, c • z) ⁻¹' A) := convex_linear_preimage _ _ (is_linear_map.is_linear_map_smul _) lemma convex_add (hA : convex A) (hB : convex B) : convex ((λx : α × α, x.1 + x.2) '' (set.prod A B)) := begin apply convex_linear_image (set.prod A B) (λx : α × α, x.1 + x.2) is_linear_map.is_linear_map_add, exact convex_prod hA hB end lemma convex_sub (hA : convex A) (hB : convex B) : convex ((λx : α × α, x.1 - x.2) '' (set.prod A B)) := begin apply convex_linear_image (set.prod A B) (λx : α × α, x.1 - x.2) is_linear_map.is_linear_map_sub, exact convex_prod hA hB end lemma convex_translation (z : α) (hA : convex A) : convex ((λx, z + x) '' A) := begin have h : convex ((λ (x : α × α), x.fst + x.snd) '' set.prod (insert z ∅) A), from convex_add {z} A (convex_singleton z) hA, show convex ((λx, z + x) '' A), { rw [@insert_prod _ _ z ∅ A, set.empty_prod, set.union_empty, ←image_comp] at h, simp at h, exact h } end lemma convex_affinity (z : α) (c : ℝ) (hA : convex A) : convex ((λx, z + c • x) '' A) := begin have h : convex ((λ (x : α), z + x) '' ((λ (z : α), c • z) '' A)), from convex_translation _ z (convex_smul A c hA), show convex ((λx, z + c • x) '' A), { rw [←image_comp] at h, simp at h, exact h } end lemma convex_Iio (r : ℝ) : convex (Iio r) := begin intros x y a b hx hy ha hb hab, wlog h : x ≤ y using [x y a b, y x b a], exact le_total _ _, calc a * x + b * y ≤ a * y + b * y : add_le_add_right (mul_le_mul_of_nonneg_left h ha) _ ... = y : by rw [←add_mul a b y, hab, one_mul] ... < r : hy end lemma convex_Iic (r : ℝ) : convex (Iic r) := begin intros x y a b hx hy ha hb hab, wlog h : x ≤ y using [x y a b, y x b a], exact le_total _ _, calc a * x + b * y ≤ a * y + b * y : add_le_add_right (mul_le_mul_of_nonneg_left h ha) _ ... = y : by rw [←add_mul a b y, hab, one_mul] ... ≤ r : hy end lemma convex_Ioi (r : ℝ) : convex (Ioi r) := begin rw [← neg_neg r], rw (image_neg_Iio (-r)).symm, unfold convex, intros x y a b hx hy ha hb hab, exact convex_linear_image _ _ is_linear_map.is_linear_map_neg (convex_Iio (-r)) _ _ _ _ hx hy ha hb hab end lemma convex_Ici (r : ℝ) : convex (Ici r) := begin rw [← neg_neg r], rw (image_neg_Iic (-r)).symm, unfold convex, intros x y a b hx hy ha hb hab, exact convex_linear_image _ _ is_linear_map.is_linear_map_neg (convex_Iic (-r)) _ _ _ _ hx hy ha hb hab end lemma convex_Ioo (r : ℝ) (s : ℝ) : convex (Ioo r s) := convex_inter _ _ (convex_Ioi _) (convex_Iio _) lemma convex_Ico (r : ℝ) (s : ℝ) : convex (Ico r s) := convex_inter _ _ (convex_Ici _) (convex_Iio _) lemma convex_Ioc (r : ℝ) (s : ℝ) : convex (Ioc r s) := convex_inter _ _ (convex_Ioi _) (convex_Iic _) lemma convex_Icc (r : ℝ) (s : ℝ) : convex (Icc r s) := convex_inter _ _ (convex_Ici _) (convex_Iic _) private lemma convex_segment0 (b : α) : convex [0, b] := begin let f := (λ x : ℝ, x • b), have h_image : f '' (Icc 0 1) = [0, b], { apply subset.antisymm, { intros z hz, apply exists.elim hz, intros x hx, use x, simp [hx.2.symm, hx.1] }, { intros z hz, apply exists.elim hz, intros x hx, use x, simp at hx, exact and.intro hx.1 hx.2.symm } }, have h_lin : is_linear_map ℝ f, from is_linear_map.is_linear_map_smul' _, show convex [0, b], { rw [←h_image], exact convex_linear_image _ f h_lin (convex_Icc _ _) } end lemma convex_segment (a b : α) : convex [a, b] := begin have h: (λx, a + x) '' [0, b-a] = [a, b], { convert segment_translate_image _ _ _, { simp }, { simp only [add_sub_cancel'_right] } }, show convex [a, b], { rw [← h], apply convex_translation, apply convex_segment0 } end lemma convex_halfspace_lt (f : α → ℝ) (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w < r} := begin assume x y a b hx hy ha hb hab, simp, rw [is_linear_map.add ℝ f, is_linear_map.smul f a, is_linear_map.smul f b], apply convex_Iio _ _ _ _ _ hx hy ha hb hab end lemma convex_halfspace_le (f : α → ℝ) (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w ≤ r} := begin assume x y a b hx hy ha hb hab, simp, rw [is_linear_map.add ℝ f, is_linear_map.smul f a, is_linear_map.smul f b], apply convex_Iic _ _ _ _ _ hx hy ha hb hab end lemma convex_halfspace_gt (f : α → ℝ) (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r < f w} := begin assume x y a b hx hy ha hb hab, simp, rw [is_linear_map.add ℝ f, is_linear_map.smul f a, is_linear_map.smul f b], apply convex_Ioi _ _ _ _ _ hx hy ha hb hab end lemma convex_halfspace_ge (f : α → ℝ) (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r ≤ f w} := begin assume x y a b hx hy ha hb hab, simp, rw [is_linear_map.add ℝ f, is_linear_map.smul f a, is_linear_map.smul f b], apply convex_Ici _ _ _ _ _ hx hy ha hb hab end lemma convex_halfplane (f : α → ℝ) (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w = r} := begin assume x y a b hx hy ha hb hab, simp at *, rw [is_linear_map.add ℝ f, is_linear_map.smul f a, is_linear_map.smul f b], rw [hx, hy, (add_smul a b r).symm, hab, one_smul] end lemma convex_halfspace_re_lt (r : ℝ) : convex {c : ℂ | c.re < r} := convex_halfspace_lt _ (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_le (r : ℝ) : convex {c : ℂ | c.re ≤ r} := convex_halfspace_le _ (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_gt (r : ℝ) : convex {c : ℂ | r < c.re } := convex_halfspace_gt _ (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_lge (r : ℝ) : convex {c : ℂ | r ≤ c.re} := convex_halfspace_ge _ (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_im_lt (r : ℝ) : convex {c : ℂ | c.im < r} := convex_halfspace_lt _ (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_le (r : ℝ) : convex {c : ℂ | c.im ≤ r} := convex_halfspace_le _ (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_gt (r : ℝ) : convex {c : ℂ | r < c.im } := convex_halfspace_gt _ (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_lge (r : ℝ) : convex {c : ℂ | r ≤ c.im} := convex_halfspace_ge _ (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_sum {γ : Type*} (hA : convex A) (z : γ → α) (s : finset γ) : ∀ a : γ → ℝ, s.sum a = 1 → (∀ i ∈ s, 0 ≤ a i) → (∀ i ∈ s, z i ∈ A) → s.sum (λi, a i • z i) ∈ A := begin refine finset.induction _ _ s, { intros _ h_sum, simp at h_sum, exact false.elim h_sum }, { intros k s hks ih a h_sum ha hz, by_cases h_cases : s.sum a = 0, { have hak : a k = 1, by rwa [finset.sum_insert hks, h_cases, add_zero] at h_sum, have ha': ∀ i ∈ s, 0 ≤ a i, from λ i hi, ha i (finset.mem_insert_of_mem hi), have h_a0: ∀ i ∈ s, a i = 0, from (finset.sum_eq_zero_iff_of_nonneg ha').1 h_cases, have h_az0: ∀ i ∈ s, a i • z i = 0, { intros i hi, rw h_a0 i hi, exact zero_smul _ (z i) }, show finset.sum (insert k s) (λ (i : γ), a i • z i) ∈ A, { rw [finset.sum_insert hks, hak, finset.sum_eq_zero h_az0], simp, exact hz k (finset.mem_insert_self k s) } }, { have h_sum_nonneg : 0 ≤ s.sum a, { apply finset.zero_le_sum', intros i hi, apply ha _ (finset.mem_insert_of_mem hi) }, have h_div_in_A: s.sum (λ (i : γ), ((s.sum a)⁻¹ * a i) • z i) ∈ A, { apply ih, { rw finset.mul_sum.symm, exact division_ring.inv_mul_cancel h_cases }, { intros i hi, exact zero_le_mul (inv_nonneg.2 h_sum_nonneg) (ha i (finset.mem_insert_of_mem hi))}, { intros i hi, exact hz i (finset.mem_insert_of_mem hi) } }, have h_sum_in_A: a k • z k + finset.sum s a • finset.sum s (λ (i : γ), ((finset.sum s a)⁻¹ * a i) • z i) ∈ A, { apply hA, exact hz k (finset.mem_insert_self k s), exact h_div_in_A, exact ha k (finset.mem_insert_self k s), exact h_sum_nonneg, rw (finset.sum_insert hks).symm, exact h_sum }, show finset.sum (insert k s) (λ (i : γ), a i • z i) ∈ A, { rw finset.sum_insert hks, rw finset.smul_sum at h_sum_in_A, simp [smul_smul, (mul_assoc (s.sum a) _ _).symm] at h_sum_in_A, conv begin congr, congr, skip, congr, skip, funext, rw (one_mul (a _)).symm, rw (field.mul_inv_cancel h_cases).symm, end, exact h_sum_in_A } } } end lemma convex_sum_iff : convex A ↔ (∀ (s : finset α) (as : α → ℝ), s.sum as = 1 → (∀ i ∈ s, 0 ≤ as i) → (∀ x ∈ s, x ∈ A) → s.sum (λx, as x • x) ∈ A ) := begin apply iff.intro, { intros hA s as h_sum has hs, exact convex_sum A hA id s _ h_sum has hs }, { intros h, intros x y a b hx hy ha hb hab, by_cases h_cases: x = y, { rw [h_cases, ←add_smul, hab, one_smul], exact hy }, { let s := insert x (finset.singleton y), have h_sum_eq_add : finset.sum s (λ z, ite (x = z) a b • z) = a • x + b • y, { rw [finset.sum_insert (finset.not_mem_singleton.2 h_cases), finset.sum_singleton], simp [h_cases] }, rw h_sum_eq_add.symm, apply h s, { rw [finset.sum_insert (finset.not_mem_singleton.2 h_cases), finset.sum_singleton], simp [h_cases], exact hab }, { intros k hk, by_cases h_cases : x = k, { simp [h_cases], exact ha }, { simp [h_cases], exact hb } }, { intros z hz, apply or.elim (finset.mem_insert.1 hz), { intros h_eq, rw h_eq, exact hx }, { intros h_eq, rw finset.mem_singleton at h_eq, rw h_eq, exact hy } } } } end variables (D: set α) (D': set α) (f : α → ℝ) (g : α → ℝ) /-- Convexity of functions -/ def convex_on (f : α → ℝ) : Prop := convex D ∧ ∀ (x y : α) (a b : ℝ), x ∈ D → y ∈ D → 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a * f x + b * f y lemma convex_on_iff : convex_on D f ↔ convex D ∧ ∀ {x y : α} {θ : ℝ}, x ∈ D → y ∈ D → 0 ≤ θ → θ ≤ 1 → f (θ • x + (1 - θ) • y) ≤ θ * f x + (1 - θ) * f y := ⟨begin intro h, apply and.intro h.1, intros x y θ hx hy hθ₁ hθ₂, have hθ₂: 0 ≤ 1 - θ, by linarith, exact (h.2 _ _ _ _ hx hy hθ₁ hθ₂ (by linarith)) end, begin intro h, apply and.intro h.1, assume x y a b hx hy ha hb hab, have ha': a ≤ 1, by linarith, have hb': b = 1 - a, by linarith, rw hb', exact (h.2 hx hy ha ha') end⟩ lemma convex_on_iff_div: convex_on D f ↔ convex D ∧ ∀ {x y : α} {a : ℝ} {b : ℝ}, x ∈ D → y ∈ D → 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a/(a+b)) • x + (b/(a+b)) • y) ≤ (a/(a+b)) * f x + (b/(a+b)) * f y := ⟨begin intro h, apply and.intro h.1, intros x y a b hx hy ha hb hab, apply h.2 _ _ _ _ hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin intro h, apply and.intro h.1, intros x y a b hx hy ha hb hab, have h', from h.2 hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ lemma convex_on_sum {γ : Type} (s : finset γ) (z : γ → α) (hs : s ≠ ∅) : ∀ (a : γ → ℝ), convex_on D f → (∀ i ∈ s, 0 ≤ a i) → (∀ i ∈ s, z i ∈ D) → s.sum a = 1 → f (s.sum (λi, a i • z i)) ≤ s.sum (λi, a i • f (z i)) := begin refine finset.induction (by simp) _ s, intros k s hks ih a hf ha hz h_sum, by_cases h_cases : s.sum a = 0, { have hak : a k = 1, by rwa [finset.sum_insert hks, h_cases, add_zero] at h_sum, have ha': ∀ i ∈ s, 0 ≤ a i, from λ i hi, ha i (finset.mem_insert_of_mem hi), have h_a0: ∀ i ∈ s, a i = 0, from (finset.sum_eq_zero_iff_of_nonneg ha').1 h_cases, have h_az0: ∀ i ∈ s, a i • z i = 0, { intros i hi, rw h_a0 i hi, exact zero_smul _ _ }, have h_afz0: ∀ i ∈ s, a i • f (z i) = 0, { intros i hi, rw h_a0 i hi, exact zero_smul _ _ }, show f (finset.sum (insert k s) (λi, a i • z i)) ≤ finset.sum (insert k s) (λi, a i • f (z i)), { rw [finset.sum_insert hks, hak, finset.sum_eq_zero h_az0], rw [finset.sum_insert hks, hak, finset.sum_eq_zero h_afz0], simp } }, { have h_sum_nonneg : 0 ≤ s.sum a , { apply finset.zero_le_sum', intros i hi, apply ha _ (finset.mem_insert_of_mem hi) }, have ih_div: f (s.sum (λ (i : γ), ((s.sum a)⁻¹ * a i) • z i)) ≤ s.sum (λ (i : γ), ((s.sum a)⁻¹ * a i) • f (z i)), { apply ih _ hf, { intros i hi, exact zero_le_mul (inv_nonneg.2 h_sum_nonneg) (ha i (finset.mem_insert_of_mem hi))}, { intros i hi, exact hz i (finset.mem_insert_of_mem hi) }, { rw finset.mul_sum.symm, exact division_ring.inv_mul_cancel h_cases } }, have h_div_in_D: s.sum (λ (i : γ), ((s.sum a)⁻¹ * a i) • z i) ∈ D, { apply convex_sum _ hf.1, { rw finset.mul_sum.symm, exact division_ring.inv_mul_cancel h_cases }, { intros i hi, exact zero_le_mul (inv_nonneg.2 h_sum_nonneg) (ha i (finset.mem_insert_of_mem hi))}, { intros i hi, exact hz i (finset.mem_insert_of_mem hi) } }, have hf': f (a k • z k + s.sum a • s.sum (λ (i : γ), ((finset.sum s a)⁻¹ * a i) • z i)) ≤ a k • f (z k) + s.sum a • f (s.sum (λ (i : γ), ((finset.sum s a)⁻¹ * a i) • z i)), { apply hf.2, exact hz k (finset.mem_insert_self k s), exact h_div_in_D, exact ha k (finset.mem_insert_self k s), exact h_sum_nonneg, rw (finset.sum_insert hks).symm, exact h_sum }, have ih_div': f (a k • z k + s.sum a • s.sum (λ (i : γ), ((finset.sum s a)⁻¹ * a i) • z i)) ≤ a k • f (z k) + s.sum a • s.sum (λ (i : γ), ((finset.sum s a)⁻¹ * a i) • f (z i)), from trans hf' (add_le_add_left (mul_le_mul_of_nonneg_left ih_div h_sum_nonneg) _), show f (finset.sum (insert k s) (λ (i : γ), a i • z i)) ≤ finset.sum (insert k s) (λ (i : γ), a i • f (z i)), { simp [finset.sum_insert hks], simp [finset.smul_sum] at ih_div', simp [smul_smul, (mul_assoc (s.sum a) _ _).symm] at ih_div', convert ih_div', repeat { apply funext, intro i, rw [field.mul_inv_cancel, one_mul], exact h_cases } } } end lemma convex_on_linorder [hα : linear_order α] (f : α → ℝ) : convex_on D f ↔ convex D ∧ ∀ (x y : α) (a b : ℝ), x ∈ D → y ∈ D → x < y → a ≥ 0 → b ≥ 0 → a + b = 1 → f (a • x + b • y) ≤ a * f x + b * f y := begin apply iff.intro, { intro h, apply and.intro h.1, intros x y a b hx hy hxy ha hb hab, exact h.2 x y a b hx hy ha hb hab }, { intro h, apply and.intro h.1, intros x y a b hx hy ha hb hab, wlog hxy : x<=y using [x y a b, y x b a], exact le_total _ _, apply or.elim (lt_or_eq_of_le hxy), { intros hxy, exact h.2 x y a b hx hy hxy ha hb hab }, { intros hxy, rw [hxy,←add_smul, hab, one_smul,←add_mul,hab,one_mul] } } end lemma convex_on_subset (h_convex_on : convex_on D f) (h_subset : A ⊆ D) (h_convex : convex A) : convex_on A f := begin apply and.intro h_convex, intros x y a b hx hy, exact h_convex_on.2 x y a b (h_subset hx) (h_subset hy), end lemma convex_on_add (hf : convex_on D f) (hg : convex_on D g) : convex_on D (λx, f x + g x) := begin apply and.intro hf.1, intros x y a b hx hy ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) ≤ (a * f x + b * f y) + (a * g x + b * g y) : add_le_add (hf.2 x y a b hx hy ha hb hab) (hg.2 x y a b hx hy ha hb hab) ... = a * f x + a * g x + b * f y + b * g y : by linarith ... = a * (f x + g x) + b * (f y + g y) : by simp [mul_add] end lemma convex_on_smul (c : ℝ) (hc : 0 ≤ c) (hf : convex_on D f) : convex_on D (λx, c * f x) := begin apply and.intro hf.1, intros x y a b hx hy ha hb hab, calc c * f (a • x + b • y) ≤ c * (a * f x + b * f y) : mul_le_mul_of_nonneg_left (hf.2 x y a b hx hy ha hb hab) hc ... = a * (c * f x) + b * (c * f y) : by rw mul_add; ac_refl end lemma convex_le_of_convex_on (hf : convex_on D f) (r : ℝ) : convex {x ∈ D | f x ≤ r} := begin intros x y a b hx hy ha hb hab, simp at *, apply and.intro, { exact hf.1 x y a b hx.1 hy.1 ha hb hab }, { apply le_trans (hf.2 x y a b hx.1 hy.1 ha hb hab), wlog h_wlog : f x ≤ f y using [x y a b, y x b a], apply le_total, calc a * f x + b * f y ≤ a * f y + b * f y : add_le_add (mul_le_mul_of_nonneg_left h_wlog ha) (le_refl _) ... = (a + b) * f y : (add_mul _ _ _).symm ... ≤ r : by rw [hab, one_mul]; exact hy.2 } end lemma convex_lt_of_convex_on (hf : convex_on D f) (r : ℝ) : convex {x ∈ D | f x < r} := begin intros x y a b hx hy ha hb hab, simp at *, apply and.intro, { exact hf.1 x y a b hx.1 hy.1 ha hb hab }, { apply lt_of_le_of_lt (hf.2 x y a b hx.1 hy.1 ha hb hab), wlog h_wlog : f x ≤ f y using [x y a b, y x b a], apply le_total, calc a * f x + b * f y ≤ a * f y + b * f y : add_le_add (mul_le_mul_of_nonneg_left h_wlog ha) (le_refl _) ... = (a + b) * f y : (add_mul _ _ _).symm ... < r : by rw [hab, one_mul]; exact hy.2 } end lemma le_on_interval_of_convex_on (x y : α) (a b : ℝ) (hf : convex_on D f) (hx : x ∈ D) (hy : y ∈ D) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := calc f (a • x + b • y) ≤ a * f x + b * f y : hf.2 x y a b hx hy ha hb hab ... ≤ a * max (f x) (f y) + b * max (f x) (f y) : add_le_add (mul_le_mul_of_nonneg_left (le_max_left _ _) ha) (mul_le_mul_of_nonneg_left (le_max_right _ _) hb) ... ≤ max (f x) (f y) : by rw [←add_mul, hab, one_mul] /- This instance is necessary to guide class instance search in the lemma below. -/ noncomputable instance real_normed_space.to_has_scalar (α : Type) [normed_space ℝ α] : has_scalar ℝ α := mul_action.to_has_scalar ℝ α lemma convex_on_dist {α : Type} [normed_space ℝ α] (z : α) (D : set α) (hD : convex D) : convex_on D (λz', dist z' z) := begin apply and.intro hD, intros x y a b hx hy ha hb hab, calc dist (a • x + b • y) z = ∥ (a • x + b • y) - (a + b) • z ∥ : by rw [hab, one_smul, normed_group.dist_eq] ... = ∥a • (x - z) + b • (y - z)∥ : by rw [add_smul, smul_sub, smul_sub]; simp ... ≤ ∥a • (x - z)∥ + ∥b • (y - z)∥ : norm_triangle (a • (x - z)) (b • (y - z)) ... = a * dist x z + b * dist y z : by simp [norm_smul, normed_group.dist_eq, real.norm_eq_abs, abs_of_nonneg ha, abs_of_nonneg hb] end lemma convex_ball {α : Type} [normed_space ℝ α] (a : α) (r : ℝ) : convex (metric.ball a r) := by simpa using convex_lt_of_convex_on univ (λb, dist b a) (convex_on_dist _ _ convex_univ) r lemma convex_closed_ball {α : Type} [normed_space ℝ α] (a : α) (r : ℝ) : convex (metric.closed_ball a r) := by simpa using convex_le_of_convex_on univ (λb, dist b a) (convex_on_dist _ _ convex_univ) r
203efcebffa9fe32b5b9cf38e1837b39cb2372f9
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/complex/conformal.lean
5b3084712aaba142c2e719a7d487a97c12d223eb
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,889
lean
/- Copyright (c) 2021 Yourong Zang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yourong Zang -/ import analysis.complex.isometry import analysis.normed_space.conformal_linear_map /-! # Conformal maps between complex vector spaces We prove the sufficient and necessary conditions for a real-linear map between complex vector spaces to be conformal. ## Main results * `is_conformal_map_complex_linear`: a nonzero complex linear map into an arbitrary complex normed space is conformal. * `is_conformal_map_complex_linear_conj`: the composition of a nonzero complex linear map with `conj` is complex linear. * `is_conformal_map_iff_is_complex_or_conj_linear`: a real linear map between the complex plane is conformal iff it's complex linear or the composition of some complex linear map and `conj`. ## Warning Antiholomorphic functions such as the complex conjugate are considered as conformal functions in this file. -/ noncomputable theory open complex continuous_linear_map open_locale complex_conjugate lemma is_conformal_map_conj : is_conformal_map (conj_lie : ℂ →L[ℝ] ℂ) := conj_lie.to_linear_isometry.is_conformal_map section conformal_into_complex_normed variables {E : Type*} [normed_group E] [normed_space ℝ E] [normed_space ℂ E] [is_scalar_tower ℝ ℂ E] {z : ℂ} {g : ℂ →L[ℝ] E} {f : ℂ → E} lemma is_conformal_map_complex_linear {map : ℂ →L[ℂ] E} (nonzero : map ≠ 0) : is_conformal_map (map.restrict_scalars ℝ) := begin have minor₁ : ∥map 1∥ ≠ 0, { simpa [ext_ring_iff] using nonzero }, refine ⟨∥map 1∥, minor₁, ⟨∥map 1∥⁻¹ • map, _⟩, _⟩, { intros x, simp only [linear_map.smul_apply], have : x = x • 1 := by rw [smul_eq_mul, mul_one], nth_rewrite 0 [this], rw [_root_.coe_coe map, linear_map.coe_coe_is_scalar_tower], simp only [map.coe_coe, map.map_smul, norm_smul, normed_field.norm_inv, norm_norm], field_simp [minor₁], }, { ext1, rw [← linear_isometry.coe_to_linear_map], simp [minor₁], }, end lemma is_conformal_map_complex_linear_conj {map : ℂ →L[ℂ] E} (nonzero : map ≠ 0) : is_conformal_map ((map.restrict_scalars ℝ).comp (conj_cle : ℂ →L[ℝ] ℂ)) := (is_conformal_map_complex_linear nonzero).comp is_conformal_map_conj end conformal_into_complex_normed section conformal_into_complex_plane open continuous_linear_map variables {f : ℂ → ℂ} {z : ℂ} {g : ℂ →L[ℝ] ℂ} lemma is_conformal_map.is_complex_or_conj_linear (h : is_conformal_map g) : (∃ (map : ℂ →L[ℂ] ℂ), map.restrict_scalars ℝ = g) ∨ (∃ (map : ℂ →L[ℂ] ℂ), map.restrict_scalars ℝ = g ∘L ↑conj_cle) := begin rcases h with ⟨c, hc, li, hg⟩, rcases linear_isometry_complex (li.to_linear_isometry_equiv rfl) with ⟨a, ha⟩, let rot := c • (a : ℂ) • continuous_linear_map.id ℂ ℂ, cases ha, { refine or.intro_left _ ⟨rot, _⟩, ext1, simp only [coe_restrict_scalars', hg, ← li.coe_to_linear_isometry_equiv, ha, pi.smul_apply, continuous_linear_map.smul_apply, rotation_apply, continuous_linear_map.id_apply, smul_eq_mul], }, { refine or.intro_right _ ⟨rot, _⟩, ext1, rw [continuous_linear_map.coe_comp', hg, ← li.coe_to_linear_isometry_equiv, ha], simp only [coe_restrict_scalars', function.comp_app, pi.smul_apply, linear_isometry_equiv.coe_trans, conj_lie_apply, rotation_apply, continuous_linear_equiv.coe_apply, conj_cle_apply], simp only [continuous_linear_map.smul_apply, continuous_linear_map.id_apply, smul_eq_mul, conj_conj], }, end /-- A real continuous linear map on the complex plane is conformal if and only if the map or its conjugate is complex linear, and the map is nonvanishing. -/ lemma is_conformal_map_iff_is_complex_or_conj_linear: is_conformal_map g ↔ ((∃ (map : ℂ →L[ℂ] ℂ), map.restrict_scalars ℝ = g) ∨ (∃ (map : ℂ →L[ℂ] ℂ), map.restrict_scalars ℝ = g ∘L ↑conj_cle)) ∧ g ≠ 0 := begin split, { exact λ h, ⟨h.is_complex_or_conj_linear, h.ne_zero⟩, }, { rintros ⟨⟨map, rfl⟩ | ⟨map, hmap⟩, h₂⟩, { refine is_conformal_map_complex_linear _, contrapose! h₂ with w, simp [w] }, { have minor₁ : g = (map.restrict_scalars ℝ) ∘L ↑conj_cle, { ext1, simp [hmap] }, rw minor₁ at ⊢ h₂, refine is_conformal_map_complex_linear_conj _, contrapose! h₂ with w, simp [w] } } end end conformal_into_complex_plane
267bfe10067f1ae9c03736fefb9e2060446f6666
4727251e0cd73359b15b664c3170e5d754078599
/src/data/multiset/sections.lean
cad402b544edc47806754adfa6bb30ffffc24c5b
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
2,229
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 data.multiset.bind /-! # Sections of a multiset -/ namespace multiset variables {α : Type*} section sections /-- The sections of a multiset of multisets `s` consists of all those multisets which can be put in bijection with `s`, so each element is an member of the corresponding multiset. -/ def sections (s : multiset (multiset α)) : multiset (multiset α) := multiset.rec_on s {0} (λs _ c, s.bind $ λa, c.map (multiset.cons a)) (assume a₀ a₁ s pi, by simp [map_bind, bind_bind a₀ a₁, cons_swap]) @[simp] lemma sections_zero : sections (0 : multiset (multiset α)) = {0} := rfl @[simp] lemma sections_cons (s : multiset (multiset α)) (m : multiset α) : sections (m ::ₘ s) = m.bind (λa, (sections s).map (multiset.cons a)) := rec_on_cons m s lemma coe_sections : ∀(l : list (list α)), sections ((l.map (λl:list α, (l : multiset α))) : multiset (multiset α)) = ((l.sections.map (λl:list α, (l : multiset α))) : multiset (multiset α)) | [] := rfl | (a :: l) := begin simp, rw [← cons_coe, sections_cons, bind_map_comm, coe_sections l], simp [list.sections, (∘), list.bind] end @[simp] lemma sections_add (s t : multiset (multiset α)) : sections (s + t) = (sections s).bind (λm, (sections t).map ((+) m)) := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, bind_assoc, map_bind, bind_map, -add_comm]) lemma mem_sections {s : multiset (multiset α)} : ∀{a}, a ∈ sections s ↔ s.rel (λs a, a ∈ s) a := multiset.induction_on s (by simp) (assume a s ih a', by simp [ih, rel_cons_left, -exists_and_distrib_left, exists_and_distrib_left.symm, eq_comm]) lemma card_sections {s : multiset (multiset α)} : card (sections s) = prod (s.map card) := multiset.induction_on s (by simp) (by simp {contextual := tt}) lemma prod_map_sum [comm_semiring α] {s : multiset (multiset α)} : prod (s.map sum) = sum ((sections s).map prod) := multiset.induction_on s (by simp) (assume a s ih, by simp [ih, map_bind, sum_map_mul_left, sum_map_mul_right]) end sections end multiset
441f8766811d3a88ecebb7d4c299e243e7495b91
80746c6dba6a866de5431094bf9f8f841b043d77
/src/algebra/group.lean
52678766decf28e2e7181cddae6251d2fa242132
[ "Apache-2.0" ]
permissive
leanprover-fork/mathlib-backup
8b5c95c535b148fca858f7e8db75a76252e32987
0eb9db6a1a8a605f0cf9e33873d0450f9f0ae9b0
refs/heads/master
1,585,156,056,139
1,548,864,430,000
1,548,864,438,000
143,964,213
0
0
Apache-2.0
1,550,795,966,000
1,533,705,322,000
Lean
UTF-8
Lean
false
false
30,183
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 Various multiplicative and additive structures. -/ import tactic.interactive data.option.defs section pending_1857 /- Transport multiplicative to additive -/ section transport open tactic @[user_attribute] meta def to_additive_attr : user_attribute (name_map name) name := { name := `to_additive, descr := "Transport multiplicative to additive", cache_cfg := ⟨λ ns, ns.mfoldl (λ dict n, do val ← to_additive_attr.get_param n, pure $ dict.insert n val) mk_name_map, []⟩, parser := lean.parser.ident, after_set := some $ λ src _ _, do env ← get_env, dict ← to_additive_attr.get_cache, tgt ← to_additive_attr.get_param src, (get_decl tgt >> skip) <|> transport_with_dict dict src tgt } end transport /- map operations -/ attribute [to_additive has_add.add] has_mul.mul attribute [to_additive has_zero.zero] has_one.one attribute [to_additive has_neg.neg] has_inv.inv attribute [to_additive has_add] has_mul attribute [to_additive has_zero] has_one attribute [to_additive has_neg] has_inv /- map constructors -/ attribute [to_additive has_add.mk] has_mul.mk attribute [to_additive has_zero.mk] has_one.mk attribute [to_additive has_neg.mk] has_inv.mk /- map structures -/ attribute [to_additive add_semigroup] semigroup attribute [to_additive add_semigroup.mk] semigroup.mk attribute [to_additive add_semigroup.to_has_add] semigroup.to_has_mul attribute [to_additive add_semigroup.add_assoc] semigroup.mul_assoc attribute [to_additive add_semigroup.add] semigroup.mul attribute [to_additive add_comm_semigroup] comm_semigroup attribute [to_additive add_comm_semigroup.mk] comm_semigroup.mk attribute [to_additive add_comm_semigroup.to_add_semigroup] comm_semigroup.to_semigroup attribute [to_additive add_comm_semigroup.add_comm] comm_semigroup.mul_comm attribute [to_additive add_left_cancel_semigroup] left_cancel_semigroup attribute [to_additive add_left_cancel_semigroup.mk] left_cancel_semigroup.mk attribute [to_additive add_left_cancel_semigroup.to_add_semigroup] left_cancel_semigroup.to_semigroup attribute [to_additive add_left_cancel_semigroup.add_left_cancel] left_cancel_semigroup.mul_left_cancel attribute [to_additive add_right_cancel_semigroup] right_cancel_semigroup attribute [to_additive add_right_cancel_semigroup.mk] right_cancel_semigroup.mk attribute [to_additive add_right_cancel_semigroup.to_add_semigroup] right_cancel_semigroup.to_semigroup attribute [to_additive add_right_cancel_semigroup.add_right_cancel] right_cancel_semigroup.mul_right_cancel attribute [to_additive add_monoid] monoid attribute [to_additive add_monoid.mk] monoid.mk attribute [to_additive add_monoid.to_has_zero] monoid.to_has_one attribute [to_additive add_monoid.to_add_semigroup] monoid.to_semigroup attribute [to_additive add_monoid.add] monoid.mul attribute [to_additive add_monoid.add_assoc] monoid.mul_assoc attribute [to_additive add_monoid.zero] monoid.one attribute [to_additive add_monoid.zero_add] monoid.one_mul attribute [to_additive add_monoid.add_zero] monoid.mul_one attribute [to_additive add_comm_monoid] comm_monoid attribute [to_additive add_comm_monoid.mk] comm_monoid.mk attribute [to_additive add_comm_monoid.to_add_monoid] comm_monoid.to_monoid attribute [to_additive add_comm_monoid.to_add_comm_semigroup] comm_monoid.to_comm_semigroup attribute [to_additive add_group] group attribute [to_additive add_group.mk] group.mk attribute [to_additive add_group.to_has_neg] group.to_has_inv attribute [to_additive add_group.to_add_monoid] group.to_monoid attribute [to_additive add_group.add_left_neg] group.mul_left_inv attribute [to_additive add_group.add] group.mul attribute [to_additive add_group.add_assoc] group.mul_assoc attribute [to_additive add_group.zero] group.one attribute [to_additive add_group.zero_add] group.one_mul attribute [to_additive add_group.add_zero] group.mul_one attribute [to_additive add_group.neg] group.inv attribute [to_additive add_comm_group] comm_group attribute [to_additive add_comm_group.mk] comm_group.mk attribute [to_additive add_comm_group.to_add_group] comm_group.to_group attribute [to_additive add_comm_group.to_add_comm_monoid] comm_group.to_comm_monoid /- map theorems -/ attribute [to_additive add_assoc] mul_assoc attribute [to_additive add_semigroup_to_is_associative] semigroup_to_is_associative attribute [to_additive add_comm] mul_comm attribute [to_additive add_comm_semigroup_to_is_commutative] comm_semigroup_to_is_commutative attribute [to_additive add_left_comm] mul_left_comm attribute [to_additive add_right_comm] mul_right_comm attribute [to_additive add_left_cancel] mul_left_cancel attribute [to_additive add_right_cancel] mul_right_cancel attribute [to_additive add_left_cancel_iff] mul_left_cancel_iff attribute [to_additive add_right_cancel_iff] mul_right_cancel_iff attribute [to_additive zero_add] one_mul attribute [to_additive add_zero] mul_one attribute [to_additive add_left_neg] mul_left_inv attribute [to_additive neg_add_self] inv_mul_self attribute [to_additive neg_add_cancel_left] inv_mul_cancel_left attribute [to_additive neg_add_cancel_right] inv_mul_cancel_right attribute [to_additive neg_eq_of_add_eq_zero] inv_eq_of_mul_eq_one attribute [to_additive neg_zero] one_inv attribute [to_additive neg_neg] inv_inv attribute [to_additive add_right_neg] mul_right_inv attribute [to_additive add_neg_self] mul_inv_self attribute [to_additive neg_inj] inv_inj attribute [to_additive add_group.add_left_cancel] group.mul_left_cancel attribute [to_additive add_group.add_right_cancel] group.mul_right_cancel attribute [to_additive add_group.to_left_cancel_add_semigroup] group.to_left_cancel_semigroup attribute [to_additive add_group.to_right_cancel_add_semigroup] group.to_right_cancel_semigroup attribute [to_additive add_neg_cancel_left] mul_inv_cancel_left attribute [to_additive add_neg_cancel_right] mul_inv_cancel_right attribute [to_additive neg_add_rev] mul_inv_rev attribute [to_additive eq_neg_of_eq_neg] eq_inv_of_eq_inv attribute [to_additive eq_neg_of_add_eq_zero] eq_inv_of_mul_eq_one attribute [to_additive eq_add_neg_of_add_eq] eq_mul_inv_of_mul_eq attribute [to_additive eq_neg_add_of_add_eq] eq_inv_mul_of_mul_eq attribute [to_additive neg_add_eq_of_eq_add] inv_mul_eq_of_eq_mul attribute [to_additive add_neg_eq_of_eq_add] mul_inv_eq_of_eq_mul attribute [to_additive eq_add_of_add_neg_eq] eq_mul_of_mul_inv_eq attribute [to_additive eq_add_of_neg_add_eq] eq_mul_of_inv_mul_eq attribute [to_additive add_eq_of_eq_neg_add] mul_eq_of_eq_inv_mul attribute [to_additive add_eq_of_eq_add_neg] mul_eq_of_eq_mul_inv attribute [to_additive neg_add] mul_inv end pending_1857 instance monoid_to_is_left_id {α : Type*} [monoid α] : is_left_id α (*) 1 := ⟨ monoid.one_mul ⟩ instance monoid_to_is_right_id {α : Type*} [monoid α] : is_right_id α (*) 1 := ⟨ monoid.mul_one ⟩ instance add_monoid_to_is_left_id {α : Type*} [add_monoid α] : is_left_id α (+) 0 := ⟨ add_monoid.zero_add ⟩ instance add_monoid_to_is_right_id {α : Type*} [add_monoid α] : is_right_id α (+) 0 := ⟨ add_monoid.add_zero ⟩ universes u v variables {α : Type u} {β : Type v} def additive (α : Type*) := α def multiplicative (α : Type*) := α instance [semigroup α] : add_semigroup (additive α) := { add := ((*) : α → α → α), add_assoc := @mul_assoc _ _ } instance [add_semigroup α] : semigroup (multiplicative α) := { mul := ((+) : α → α → α), mul_assoc := @add_assoc _ _ } instance [comm_semigroup α] : add_comm_semigroup (additive α) := { add_comm := @mul_comm _ _, ..additive.add_semigroup } instance [add_comm_semigroup α] : comm_semigroup (multiplicative α) := { mul_comm := @add_comm _ _, ..multiplicative.semigroup } instance [left_cancel_semigroup α] : add_left_cancel_semigroup (additive α) := { add_left_cancel := @mul_left_cancel _ _, ..additive.add_semigroup } instance [add_left_cancel_semigroup α] : left_cancel_semigroup (multiplicative α) := { mul_left_cancel := @add_left_cancel _ _, ..multiplicative.semigroup } instance [right_cancel_semigroup α] : add_right_cancel_semigroup (additive α) := { add_right_cancel := @mul_right_cancel _ _, ..additive.add_semigroup } instance [add_right_cancel_semigroup α] : right_cancel_semigroup (multiplicative α) := { mul_right_cancel := @add_right_cancel _ _, ..multiplicative.semigroup } @[simp, to_additive add_left_inj] theorem mul_left_inj [left_cancel_semigroup α] (a : α) {b c : α} : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congr_arg _⟩ @[simp, to_additive add_right_inj] theorem mul_right_inj [right_cancel_semigroup α] (a : α) {b c : α} : b * a = c * a ↔ b = c := ⟨mul_right_cancel, congr_arg _⟩ structure units (α : Type u) [monoid α] := (val : α) (inv : α) (val_inv : val * inv = 1) (inv_val : inv * val = 1) namespace units variables [monoid α] {a b c : units α} instance : has_coe (units α) α := ⟨val⟩ @[extensionality] theorem ext : ∀ {a b : units α}, (a : α) = b → a = b | ⟨v, i₁, vi₁, iv₁⟩ ⟨v', i₂, vi₂, iv₂⟩ e := by change v = v' at e; subst v'; congr; simpa only [iv₂, vi₁, one_mul, mul_one] using mul_assoc i₂ v i₁ theorem ext_iff {a b : units α} : a = b ↔ (a : α) = b := ⟨congr_arg _, ext⟩ instance [decidable_eq α] : decidable_eq (units α) | a b := decidable_of_iff' _ ext_iff protected def mul (u₁ u₂ : units α) : units α := ⟨u₁.val * u₂.val, u₂.inv * u₁.inv, have u₁.val * (u₂.val * u₂.inv) * u₁.inv = 1, by rw [u₂.val_inv]; rw [mul_one, u₁.val_inv], by simpa only [mul_assoc], have u₂.inv * (u₁.inv * u₁.val) * u₂.val = 1, by rw [u₁.inv_val]; rw [mul_one, u₂.inv_val], by simpa only [mul_assoc]⟩ protected def inv' (u : units α) : units α := ⟨u.inv, u.val, u.inv_val, u.val_inv⟩ instance : has_mul (units α) := ⟨units.mul⟩ instance : has_one (units α) := ⟨⟨1, 1, mul_one 1, one_mul 1⟩⟩ instance : has_inv (units α) := ⟨units.inv'⟩ variables (a b) @[simp] lemma coe_mul : (↑(a * b) : α) = a * b := rfl @[simp] lemma coe_one : ((1 : units α) : α) = 1 := rfl lemma val_coe : (↑a : α) = a.val := rfl lemma coe_inv : ((a⁻¹ : units α) : α) = a.inv := rfl @[simp] lemma inv_mul : (↑a⁻¹ * a : α) = 1 := inv_val _ @[simp] lemma mul_inv : (a * ↑a⁻¹ : α) = 1 := val_inv _ @[simp] lemma mul_inv_cancel_left (a : units α) (b : α) : (a:α) * (↑a⁻¹ * b) = b := by rw [← mul_assoc, mul_inv, one_mul] @[simp] lemma inv_mul_cancel_left (a : units α) (b : α) : (↑a⁻¹:α) * (a * b) = b := by rw [← mul_assoc, inv_mul, one_mul] @[simp] lemma mul_inv_cancel_right (a : α) (b : units α) : a * b * ↑b⁻¹ = a := by rw [mul_assoc, mul_inv, mul_one] @[simp] lemma inv_mul_cancel_right (a : α) (b : units α) : a * ↑b⁻¹ * b = a := by rw [mul_assoc, inv_mul, mul_one] instance : group (units α) := by refine {mul := (*), one := 1, inv := has_inv.inv, ..}; { intros, apply ext, simp only [coe_mul, coe_one, mul_assoc, one_mul, mul_one, inv_mul] } instance {α} [comm_monoid α] : comm_group (units α) := { mul_comm := λ u₁ u₂, ext $ mul_comm _ _, ..units.group } instance [has_repr α] : has_repr (units α) := ⟨repr ∘ val⟩ @[simp] theorem mul_left_inj (a : units α) {b c : α} : (a:α) * b = a * c ↔ b = c := ⟨λ h, by simpa only [inv_mul_cancel_left] using congr_arg ((*) ↑(a⁻¹ : units α)) h, congr_arg _⟩ @[simp] theorem mul_right_inj (a : units α) {b c : α} : b * a = c * a ↔ b = c := ⟨λ h, by simpa only [mul_inv_cancel_right] using congr_arg (* ↑(a⁻¹ : units α)) h, congr_arg _⟩ end units theorem nat.units_eq_one (u : units ℕ) : u = 1 := units.ext $ nat.eq_one_of_dvd_one ⟨u.inv, u.val_inv.symm⟩ def units.mk_of_mul_eq_one [comm_monoid α] (a b : α) (hab : a * b = 1) : units α := ⟨a, b, hab, (mul_comm b a).trans hab⟩ @[to_additive with_zero] def with_one (α) := option α @[to_additive with_zero.has_coe_t] instance : has_coe_t α (with_one α) := ⟨some⟩ instance [semigroup α] : monoid (with_one α) := { one := none, mul := option.lift_or_get (*), mul_assoc := (option.lift_or_get_assoc _).1, one_mul := (option.lift_or_get_is_left_id _).1, mul_one := (option.lift_or_get_is_right_id _).1 } attribute [to_additive with_zero.add_monoid._proof_1] with_one.monoid._proof_1 attribute [to_additive with_zero.add_monoid._proof_2] with_one.monoid._proof_2 attribute [to_additive with_zero.add_monoid._proof_3] with_one.monoid._proof_3 attribute [to_additive with_zero.add_monoid] with_one.monoid instance [semigroup α] : mul_zero_class (with_zero α) := { zero := none, mul := λ o₁ o₂, o₁.bind (λ a, o₂.map (λ b, a * b)), zero_mul := λ a, rfl, mul_zero := λ a, by cases a; refl, ..with_zero.add_monoid } instance [semigroup α] : semigroup (with_zero α) := { mul_assoc := λ a b c, match a, b, c with | none, _, _ := rfl | some a, none, _ := rfl | some a, some b, none := rfl | some a, some b, some c := congr_arg some (mul_assoc _ _ _) end, ..with_zero.mul_zero_class } instance [comm_semigroup α] : comm_semigroup (with_zero α) := { mul_comm := λ a b, match a, b with | none, _ := (mul_zero _).symm | some a, none := rfl | some a, some b := congr_arg some (mul_comm _ _) end, ..with_zero.semigroup } instance [monoid α] : monoid (with_zero α) := { one := some 1, one_mul := λ a, match a with | none := rfl | some a := congr_arg some $ one_mul _ end, mul_one := λ a, match a with | none := rfl | some a := congr_arg some $ mul_one _ end, ..with_zero.semigroup } instance [comm_monoid α] : comm_monoid (with_zero α) := { ..with_zero.monoid, ..with_zero.comm_semigroup } instance [monoid α] : add_monoid (additive α) := { zero := (1 : α), zero_add := @one_mul _ _, add_zero := @mul_one _ _, ..additive.add_semigroup } instance [add_monoid α] : monoid (multiplicative α) := { one := (0 : α), one_mul := @zero_add _ _, mul_one := @add_zero _ _, ..multiplicative.semigroup } section monoid variables [monoid α] {a b c : α} /-- Partial division. It is defined when the second argument is invertible, and unlike the division operator in `division_ring` it is not totalized at zero. -/ def divp (a : α) (u) : α := a * (u⁻¹ : units α) infix ` /ₚ `:70 := divp @[simp] theorem divp_self (u : units α) : (u : α) /ₚ u = 1 := units.mul_inv _ @[simp] theorem divp_one (a : α) : a /ₚ 1 = a := mul_one _ theorem divp_assoc (a b : α) (u : units α) : a * b /ₚ u = a * (b /ₚ u) := mul_assoc _ _ _ @[simp] theorem divp_mul_cancel (a : α) (u : units α) : a /ₚ u * u = a := (mul_assoc _ _ _).trans $ by rw [units.inv_mul, mul_one] @[simp] theorem mul_divp_cancel (a : α) (u : units α) : (a * u) /ₚ u = a := (mul_assoc _ _ _).trans $ by rw [units.mul_inv, mul_one] @[simp] theorem divp_right_inj (u : units α) {a b : α} : a /ₚ u = b /ₚ u ↔ a = b := units.mul_right_inj _ theorem divp_eq_one (a : α) (u : units α) : a /ₚ u = 1 ↔ a = u := (units.mul_right_inj u).symm.trans $ by rw [divp_mul_cancel, one_mul] @[simp] theorem one_divp (u : units α) : 1 /ₚ u = ↑u⁻¹ := one_mul _ end monoid instance [comm_semigroup α] : comm_monoid (with_one α) := { mul_comm := (option.lift_or_get_comm _).1, ..with_one.monoid } instance [add_comm_semigroup α] : add_comm_monoid (with_zero α) := { add_comm := (option.lift_or_get_comm _).1, ..with_zero.add_monoid } attribute [to_additive with_zero.add_comm_monoid] with_one.comm_monoid instance [comm_monoid α] : add_comm_monoid (additive α) := { add_comm := @mul_comm α _, ..additive.add_monoid } instance [add_comm_monoid α] : comm_monoid (multiplicative α) := { mul_comm := @add_comm α _, ..multiplicative.monoid } instance [group α] : add_group (additive α) := { neg := @has_inv.inv α _, add_left_neg := @mul_left_inv _ _, ..additive.add_monoid } instance [add_group α] : group (multiplicative α) := { inv := @has_neg.neg α _, mul_left_inv := @add_left_neg _ _, ..multiplicative.monoid } section group variables [group α] {a b c : α} instance : has_lift α (units α) := ⟨λ a, ⟨a, a⁻¹, mul_inv_self _, inv_mul_self _⟩⟩ @[simp, to_additive neg_inj'] theorem inv_inj' : a⁻¹ = b⁻¹ ↔ a = b := ⟨λ h, by rw [← inv_inv a, h, inv_inv], congr_arg _⟩ @[to_additive eq_of_neg_eq_neg] theorem eq_of_inv_eq_inv : a⁻¹ = b⁻¹ → a = b := inv_inj'.1 @[simp, to_additive add_self_iff_eq_zero] theorem mul_self_iff_eq_one : a * a = a ↔ a = 1 := by have := @mul_left_inj _ _ a a 1; rwa mul_one at this @[simp, to_additive neg_eq_zero] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 := by rw [← @inv_inj' _ _ a 1, one_inv] @[simp, to_additive neg_ne_zero] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := not_congr inv_eq_one @[to_additive left_inverse_neg] theorem left_inverse_inv (α) [group α] : function.left_inverse (λ a : α, a⁻¹) (λ a, a⁻¹) := assume a, inv_inv a attribute [simp] mul_inv_cancel_left add_neg_cancel_left mul_inv_cancel_right add_neg_cancel_right @[to_additive eq_neg_iff_eq_neg] theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ := ⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩ @[to_additive neg_eq_iff_neg_eq] theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a := by rw [eq_comm, @eq_comm _ _ a, eq_inv_iff_eq_inv] @[to_additive add_eq_zero_iff_eq_neg] theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ := by simpa [mul_left_inv, -mul_right_inj] using @mul_right_inj _ _ b a (b⁻¹) @[to_additive add_eq_zero_iff_neg_eq] theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b := by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm] @[to_additive eq_neg_iff_add_eq_zero] theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 := mul_eq_one_iff_eq_inv.symm @[to_additive neg_eq_iff_add_eq_zero] theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 := mul_eq_one_iff_inv_eq.symm @[to_additive eq_add_neg_iff_add_eq] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive eq_neg_add_iff_add_eq] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive neg_add_eq_iff_eq_add] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive add_neg_eq_iff_eq_add] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ @[to_additive add_neg_eq_zero] theorem mul_inv_eq_one {a b : α} : a * b⁻¹ = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inv] @[to_additive neg_comm_of_comm] theorem inv_comm_of_comm {a b : α} (H : a * b = b * a) : a⁻¹ * b = b * a⁻¹ := begin have : a⁻¹ * (b * a) * a⁻¹ = a⁻¹ * (a * b) * a⁻¹ := congr_arg (λ x:α, a⁻¹ * x * a⁻¹) H.symm, rwa [inv_mul_cancel_left, mul_assoc, mul_inv_cancel_right] at this end end group instance [comm_group α] : add_comm_group (additive α) := { add_comm := @mul_comm α _, ..additive.add_group } instance [add_comm_group α] : comm_group (multiplicative α) := { mul_comm := @add_comm α _, ..multiplicative.group } section add_monoid variables [add_monoid α] {a b c : α} @[simp] lemma bit0_zero : bit0 (0 : α) = 0 := add_zero _ @[simp] lemma bit1_zero [has_one α] : bit1 (0 : α) = 1 := show 0+0+1=(1:α), by rw [zero_add, zero_add] end add_monoid section add_group variables [add_group α] {a b c : α} local attribute [simp] sub_eq_add_neg def sub_sub_cancel := @sub_sub_self @[simp] lemma sub_left_inj : a - b = a - c ↔ b = c := (add_left_inj _).trans neg_inj' @[simp] lemma sub_right_inj : b - a = c - a ↔ b = c := add_right_inj _ lemma sub_add_sub_cancel (a b c : α) : (a - b) + (b - c) = a - c := by rw [← add_sub_assoc, sub_add_cancel] lemma sub_sub_sub_cancel_right (a b c : α) : (a - c) - (b - c) = a - b := by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel] theorem sub_eq_zero : a - b = 0 ↔ a = b := ⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩ theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b := not_congr sub_eq_zero theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b := eq_add_neg_iff_add_eq theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b := add_neg_eq_iff_eq_add theorem eq_iff_eq_of_sub_eq_sub {a b c d : α} (H : a - b = c - d) : a = b ↔ c = d := by rw [← sub_eq_zero, H, sub_eq_zero] theorem left_inverse_sub_add_left (c : α) : function.left_inverse (λ x, x - c) (λ x, x + c) := assume x, add_sub_cancel x c theorem left_inverse_add_left_sub (c : α) : function.left_inverse (λ x, x + c) (λ x, x - c) := assume x, sub_add_cancel x c theorem left_inverse_add_right_neg_add (c : α) : function.left_inverse (λ x, c + x) (λ x, - c + x) := assume x, add_neg_cancel_left c x theorem left_inverse_neg_add_add_right (c : α) : function.left_inverse (λ x, - c + x) (λ x, c + x) := assume x, neg_add_cancel_left c x end add_group section add_comm_group variables [add_comm_group α] {a b c : α} lemma sub_eq_neg_add (a b : α) : a - b = -b + a := add_comm _ _ theorem neg_add' (a b : α) : -(a + b) = -a - b := neg_add a b lemma neg_sub_neg (a b : α) : -a - -b = b - a := by simp lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b := by rw [eq_sub_iff_add_eq, add_comm] lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c := by rw [sub_eq_iff_eq_add, add_comm] lemma add_sub_cancel' (a b : α) : a + b - a = b := by rw [sub_eq_neg_add, neg_add_cancel_left] lemma add_sub_cancel'_right (a b : α) : a + (b - a) = b := by rw [← add_sub_assoc, add_sub_cancel'] lemma sub_right_comm (a b c : α) : a - b - c = a - c - b := add_right_comm _ _ _ lemma sub_add_sub_cancel' (a b c : α) : (a - b) + (c - a) = c - b := by rw add_comm; apply sub_add_sub_cancel lemma sub_sub_sub_cancel_left (a b c : α) : (c - a) - (c - b) = b - a := by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel] end add_comm_group section is_conj variables [group α] [group β] def is_conj (a b : α) := ∃ c : α, c * a * c⁻¹ = b @[refl] lemma is_conj_refl (a : α) : is_conj a a := ⟨1, by rw [one_mul, one_inv, mul_one]⟩ @[symm] lemma is_conj_symm {a b : α} : is_conj a b → is_conj b a | ⟨c, hc⟩ := ⟨c⁻¹, by rw [← hc, mul_assoc, mul_inv_cancel_right, inv_mul_cancel_left]⟩ @[trans] lemma is_conj_trans {a b c : α} : is_conj a b → is_conj b c → is_conj a c | ⟨c₁, hc₁⟩ ⟨c₂, hc₂⟩ := ⟨c₂ * c₁, by rw [← hc₂, ← hc₁, mul_inv_rev]; simp only [mul_assoc]⟩ @[simp] lemma is_conj_one_right {a : α} : is_conj 1 a ↔ a = 1 := ⟨by simp [is_conj, is_conj_refl] {contextual := tt}, by simp [is_conj_refl] {contextual := tt}⟩ @[simp] lemma is_conj_one_left {a : α} : is_conj a 1 ↔ a = 1 := calc is_conj a 1 ↔ is_conj 1 a : ⟨is_conj_symm, is_conj_symm⟩ ... ↔ a = 1 : is_conj_one_right @[simp] lemma is_conj_iff_eq {α : Type*} [comm_group α] {a b : α} : is_conj a b ↔ a = b := ⟨λ ⟨c, hc⟩, by rw [← hc, mul_right_comm, mul_inv_self, one_mul], λ h, by rw h⟩ end is_conj class is_monoid_hom [monoid α] [monoid β] (f : α → β) : Prop := (map_one : f 1 = 1) (map_mul : ∀ {x y}, f (x * y) = f x * f y) class is_add_monoid_hom [add_monoid α] [add_monoid β] (f : α → β) : Prop := (map_zero : f 0 = 0) (map_add : ∀ {x y}, f (x + y) = f x + f y) attribute [to_additive is_add_monoid_hom] is_monoid_hom attribute [to_additive is_add_monoid_hom.map_add] is_monoid_hom.map_mul attribute [to_additive is_add_monoid_hom.mk] is_monoid_hom.mk namespace is_monoid_hom variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] @[to_additive is_add_monoid_hom.id] instance id : is_monoid_hom (@id α) := by refine {..}; intros; refl @[to_additive is_add_monoid_hom.id] instance comp {γ} [monoid γ] (g : β → γ) [is_monoid_hom g] : is_monoid_hom (g ∘ f) := { map_mul := λ x y, show g _ = g _ * g _, by rw [map_mul f, map_mul g], map_one := show g _ = 1, by rw [map_one f, map_one g] } instance is_add_monoid_hom_mul_left {γ : Type*} [semiring γ] (x : γ) : is_add_monoid_hom (λ y : γ, x * y) := by refine_struct {..}; simp [mul_add] instance is_add_monoid_hom_mul_right {γ : Type*} [semiring γ] (x : γ) : is_add_monoid_hom (λ y : γ, y * x) := by refine_struct {..}; simp [add_mul] end is_monoid_hom -- TODO rename fields of is_group_hom: mul ↝ map_mul? /-- Predicate for group homomorphism. -/ class is_group_hom [group α] [group β] (f : α → β) : Prop := (mul : ∀ a b : α, f (a * b) = f a * f b) class is_add_group_hom [add_group α] [add_group β] (f : α → β) : Prop := (add : ∀ a b, f (a + b) = f a + f b) attribute [to_additive is_add_group_hom] is_group_hom attribute [to_additive is_add_group_hom.add] is_group_hom.mul attribute [to_additive is_add_group_hom.mk] is_group_hom.mk instance additive.is_add_group_hom [group α] [group β] (f : α → β) [is_group_hom f] : @is_add_group_hom (additive α) (additive β) _ _ f := ⟨@is_group_hom.mul α β _ _ f _⟩ instance multiplicative.is_group_hom [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] : @is_group_hom (multiplicative α) (multiplicative β) _ _ f := ⟨@is_add_group_hom.add α β _ _ f _⟩ namespace is_group_hom variables [group α] [group β] (f : α → β) [is_group_hom f] @[to_additive is_add_group_hom.zero] theorem one : f 1 = 1 := mul_self_iff_eq_one.1 $ by rw [← mul f, one_mul] @[to_additive is_add_group_hom.neg] theorem inv (a : α) : f a⁻¹ = (f a)⁻¹ := eq_inv_of_mul_eq_one $ by rw [← mul f, inv_mul_self, one f] @[to_additive is_add_group_hom.id] instance id : is_group_hom (@id α) := ⟨λ _ _, rfl⟩ @[to_additive is_add_group_hom.comp] instance comp {γ} [group γ] (g : β → γ) [is_group_hom g] : is_group_hom (g ∘ f) := ⟨λ x y, show g _ = g _ * g _, by rw [mul f, mul g]⟩ protected lemma is_conj (f : α → β) [is_group_hom f] {a b : α} : is_conj a b → is_conj (f a) (f b) | ⟨c, hc⟩ := ⟨f c, by rw [← is_group_hom.mul f, ← is_group_hom.inv f, ← is_group_hom.mul f, hc]⟩ @[to_additive is_add_group_hom.to_is_add_monoid_hom] lemma to_is_monoid_hom (f : α → β) [is_group_hom f] : is_monoid_hom f := ⟨is_group_hom.one f, is_group_hom.mul f⟩ @[to_additive is_add_group_hom.injective_iff] lemma injective_iff (f : α → β) [is_group_hom f] : function.injective f ↔ (∀ a, f a = 1 → a = 1) := ⟨λ h _, by rw ← is_group_hom.one f; exact @h _ _, λ h x y hxy, by rw [← inv_inv (f x), inv_eq_iff_mul_eq_one, ← is_group_hom.inv f, ← is_group_hom.mul f] at hxy; simpa using inv_eq_of_mul_eq_one (h _ hxy)⟩ attribute [instance] is_group_hom.to_is_monoid_hom is_add_group_hom.to_is_add_monoid_hom end is_group_hom @[to_additive is_add_group_hom_add] lemma is_group_hom_mul {α β} [group α] [comm_group β] (f g : α → β) [is_group_hom f] [is_group_hom g] : is_group_hom (λa, f a * g a) := ⟨assume a b, by simp only [is_group_hom.mul f, is_group_hom.mul g, mul_comm, mul_assoc, mul_left_comm]⟩ attribute [instance] is_group_hom_mul is_add_group_hom_add @[to_additive is_add_group_hom_neg] lemma is_group_hom_inv {α β} [group α] [comm_group β] (f : α → β) [is_group_hom f] : is_group_hom (λa, (f a)⁻¹) := ⟨assume a b, by rw [is_group_hom.mul f, mul_inv]⟩ attribute [instance] is_group_hom_inv is_add_group_hom_neg @[to_additive neg.is_add_group_hom] lemma inv.is_group_hom [comm_group α] : is_group_hom (has_inv.inv : α → α) := ⟨by simp [mul_inv_rev, mul_comm]⟩ attribute [instance] inv.is_group_hom neg.is_add_group_hom /-- Predicate for group anti-homomorphism, or a homomorphism into the opposite group. -/ class is_group_anti_hom {β : Type*} [group α] [group β] (f : α → β) : Prop := (mul : ∀ a b : α, f (a * b) = f b * f a) namespace is_group_anti_hom variables [group α] [group β] (f : α → β) [w : is_group_anti_hom f] include w theorem one : f 1 = 1 := mul_self_iff_eq_one.1 $ by rw [← mul f, one_mul] theorem inv (a : α) : f a⁻¹ = (f a)⁻¹ := eq_inv_of_mul_eq_one $ by rw [← mul f, mul_inv_self, one f] end is_group_anti_hom theorem inv_is_group_anti_hom [group α] : is_group_anti_hom (λ x : α, x⁻¹) := ⟨mul_inv_rev⟩ namespace is_add_group_hom variables [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] lemma sub (a b) : f (a - b) = f a - f b := calc f (a - b) = f (a + -b) : rfl ... = f a + f (-b) : add f _ _ ... = f a - f b : by simp[neg f] end is_add_group_hom lemma is_add_group_hom_sub {α β} [add_group α] [add_comm_group β] (f g : α → β) [is_add_group_hom f] [is_add_group_hom g] : is_add_group_hom (λa, f a - g a) := is_add_group_hom_add f (λa, - g a) attribute [instance] is_add_group_hom_sub namespace units variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] definition map : units α → units β := λ u, ⟨f u.val, f u.inv, by rw [← is_monoid_hom.map_mul f, u.val_inv, is_monoid_hom.map_one f], by rw [← is_monoid_hom.map_mul f, u.inv_val, is_monoid_hom.map_one f] ⟩ instance : is_group_hom (units.map f) := ⟨λ a b, by ext; exact is_monoid_hom.map_mul f ⟩ instance : is_monoid_hom (coe : units α → α) := ⟨by simp, by simp⟩ end units
470299b729908534bdfd324aced2757128faca66
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-algebra-35.lean
b10ac304547904bfcb34b13ebe45d5e614b4e86b
[ "MIT", "Apache-2.0" ]
permissive
zeta1999/miniF2F
6d66c75d1c18152e224d07d5eed57624f731d4b7
c1ba9629559c5273c92ec226894baa0c1ce27861
refs/heads/main
1,681,897,460,642
1,620,646,361,000
1,620,646,361,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
329
lean
/- Copyright (c) 2021 OpenAI. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kunhao Zheng -/ import data.real.basic example (p q : ℝ → ℝ) (h₀ : ∀ x, p x = 2 - x ^ 2) (h₁ : ∀ x≠0, q x = 6 / x) : p ( q 2) = -7 := begin rw [h₀, h₁], ring, linarith, end
f241401666fde8a41276c717ff6be7bdfc570ccb
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/compatibleTypesBugAtLCNF.lean
203c01b5696ee72d97ec5ff44ce2bf3f923e12c2
[ "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
82
lean
import Lean #eval Lean.Compiler.compile #[``Lean.Compiler.LCNF.Simp.etaPolyApp?]
a4437c84eb2bf5cd7ef2b5d8b2f7f2b980a0c58f
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/compiler/bigctor.lean
1206779290a9dbcc76662feb2aa09b9343d08c70
[ "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
1,350
lean
structure Foo where x1 : Nat := 0 x2 : Nat := 0 x3 : Nat := 0 x4 : Nat := 0 x5 : Nat := 0 x6 : Nat := 0 x7 : Nat := 0 x8 : Nat := 0 x9 : Nat := 0 x10 : Nat := 0 y1 : Nat := 0 y2 : Nat := 0 y3 : Nat := 0 y4 : Nat := 0 y5 : Nat := 0 y6 : Nat := 0 y7 : Nat := 0 y8 : Nat := 0 y9 : Nat := 0 y10 : Nat := 0 z1 : Nat := 0 z2 : Nat := 0 z3 : Nat := 0 z4 : Nat := 0 z5 : Nat := 0 z6 : Nat := 0 z7 : Nat := 0 z8 : Nat := 0 z9 : Nat := 0 z10 : Nat := 0 w1 : Nat := 0 w2 : Nat := 0 w3 : Nat := 0 w4 : Nat := 0 w5 : Nat := 0 w6 : Nat := 0 w7 : Nat := 0 w8 : Nat := 0 w9 : Nat := 0 w10 : Nat := 0 xx1 : Nat := 0 xx2 : Nat := 0 xx3 : Nat := 0 xx4 : Nat := 0 xx5 : Nat := 0 xx6 : Nat := 0 xx7 : Nat := 0 xx8 : Nat := 0 xx9 : Nat := 0 xx10 : Nat := 0 yy1 : Nat := 0 yy2 : Nat := 0 yy3 : Nat := 0 yy4 : Nat := 0 yy5 : Nat := 0 yy6 : Nat := 0 yy7 : Nat := 0 yy8 : Nat := 0 yy9 : Nat := 0 yy10 : Nat := 0 ww1 : Nat := 0 ww2 : Nat := 0 ww3 : Nat := 0 ww4 : Nat := 0 ww5 : Nat := 0 ww6 : Nat := 0 ww7 : Nat := 0 ww8 : Nat := 0 ww9 : Nat := 0 ww10 : Nat := 0 @[noinline] def mkFoo (x : Nat) : Foo := { yy10 := x } def main : IO Unit := IO.println (mkFoo 10).yy10
de8c577cb4493f7db7624aae9d998f3867901ca6
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/analytic/isolated_zeros.lean
92ff0d95fb527d70d2cf2042cfd771430458585b
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
5,756
lean
/- Copyright (c) 2022 Vincent Beffara. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Vincent Beffara -/ import analysis.analytic.basic import analysis.calculus.dslope import analysis.calculus.fderiv_analytic import analysis.calculus.formal_multilinear_series import analysis.complex.basic import topology.algebra.infinite_sum /-! # Principle of isolated zeros This file proves the fact that the zeros of a non-constant analytic function of one variable are isolated. It also introduces a little bit of API in the `has_fpower_series_at` namespace that is useful in this setup. ## Main results * `analytic_at.eventually_eq_zero_or_eventually_ne_zero` is the main statement that if a function is analytic at `z₀`, then either it is identically zero in a neighborhood of `z₀`, or it does not vanish in a punctured neighborhood of `z₀`. -/ open_locale classical open filter function nat formal_multilinear_series emetric open_locale topological_space big_operators variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] {s : E} {p q : formal_multilinear_series 𝕜 𝕜 E} {f g : 𝕜 → E} {n : ℕ} {z z₀ : 𝕜} {y : fin n → 𝕜} namespace has_sum variables {a : ℕ → E} lemma has_sum_at_zero (a : ℕ → E) : has_sum (λ n, (0:𝕜) ^ n • a n) (a 0) := by convert has_sum_single 0 (λ b h, _); simp [nat.pos_of_ne_zero h] <|> simp lemma exists_has_sum_smul_of_apply_eq_zero (hs : has_sum (λ m, z ^ m • a m) s) (ha : ∀ k < n, a k = 0) : ∃ t : E, z ^ n • t = s ∧ has_sum (λ m, z ^ m • a (m + n)) t := begin obtain rfl | hn := n.eq_zero_or_pos, { simpa }, by_cases h : z = 0, { have : s = 0 := hs.unique (by simpa [ha 0 hn, h] using has_sum_at_zero a), exact ⟨a n, by simp [h, hn, this], by simpa [h] using has_sum_at_zero (λ m, a (m + n))⟩ }, { refine ⟨(z ^ n)⁻¹ • s, by field_simp [smul_smul], _⟩, have h1 : ∑ i in finset.range n, z ^ i • a i = 0, from finset.sum_eq_zero (λ k hk, by simp [ha k (finset.mem_range.mp hk)]), have h2 : has_sum (λ m, z ^ (m + n) • a (m + n)) s, by simpa [h1] using (has_sum_nat_add_iff' n).mpr hs, convert @has_sum.const_smul E ℕ 𝕜 _ _ _ _ _ _ _ (z⁻¹ ^ n) h2, { field_simp [pow_add, smul_smul] }, { simp only [inv_pow] } } end end has_sum namespace has_fpower_series_at lemma has_fpower_series_dslope_fslope (hp : has_fpower_series_at f p z₀) : has_fpower_series_at (dslope f z₀) p.fslope z₀ := begin have hpd : deriv f z₀ = p.coeff 1 := hp.deriv, have hp0 : p.coeff 0 = f z₀ := hp.coeff_zero 1, simp only [has_fpower_series_at_iff, apply_eq_pow_smul_coeff, coeff_fslope] at hp ⊢, refine hp.mono (λ x hx, _), by_cases h : x = 0, { convert has_sum_single 0 _; intros; simp [*] }, { have hxx : ∀ (n : ℕ), x⁻¹ * x ^ (n + 1) = x ^ n := λ n, by field_simp [h, pow_succ'], suffices : has_sum (λ n, x⁻¹ • x ^ (n + 1) • p.coeff (n + 1)) (x⁻¹ • (f (z₀ + x) - f z₀)), { simpa [dslope, slope, h, smul_smul, hxx] using this }, { simpa [hp0] using ((has_sum_nat_add_iff' 1).mpr hx).const_smul } } end lemma has_fpower_series_iterate_dslope_fslope (n : ℕ) (hp : has_fpower_series_at f p z₀) : has_fpower_series_at ((swap dslope z₀)^[n] f) (fslope^[n] p) z₀ := begin induction n with n ih generalizing f p, { exact hp }, { simpa using ih (has_fpower_series_dslope_fslope hp) } end lemma iterate_dslope_fslope_ne_zero (hp : has_fpower_series_at f p z₀) (h : p ≠ 0) : (swap dslope z₀)^[p.order] f z₀ ≠ 0 := begin rw [← coeff_zero (has_fpower_series_iterate_dslope_fslope p.order hp) 1], simpa [coeff_eq_zero] using apply_order_ne_zero h end lemma eq_pow_order_mul_iterate_dslope (hp : has_fpower_series_at f p z₀) : ∀ᶠ z in 𝓝 z₀, f z = (z - z₀) ^ p.order • ((swap dslope z₀)^[p.order] f z) := begin have hq := has_fpower_series_at_iff'.mp (has_fpower_series_iterate_dslope_fslope p.order hp), filter_upwards [hq, has_fpower_series_at_iff'.mp hp] with x hx1 hx2, have : ∀ k < p.order, p.coeff k = 0, from λ k hk, by simpa [coeff_eq_zero] using apply_eq_zero_of_lt_order hk, obtain ⟨s, hs1, hs2⟩ := has_sum.exists_has_sum_smul_of_apply_eq_zero hx2 this, convert hs1.symm, simp only [coeff_iterate_fslope] at hx1, exact hx1.unique hs2 end lemma locally_ne_zero (hp : has_fpower_series_at f p z₀) (h : p ≠ 0) : ∀ᶠ z in 𝓝[≠] z₀, f z ≠ 0 := begin rw [eventually_nhds_within_iff], have h2 := (has_fpower_series_iterate_dslope_fslope p.order hp).continuous_at, have h3 := h2.eventually_ne (iterate_dslope_fslope_ne_zero hp h), filter_upwards [eq_pow_order_mul_iterate_dslope hp, h3] with z e1 e2 e3, simpa [e1, e2, e3] using pow_ne_zero p.order (sub_ne_zero.mpr e3), end lemma locally_zero_iff (hp : has_fpower_series_at f p z₀) : (∀ᶠ z in 𝓝 z₀, f z = 0) ↔ p = 0 := ⟨λ hf, hp.eq_zero_of_eventually hf, λ h, eventually_eq_zero (by rwa h at hp)⟩ end has_fpower_series_at namespace analytic_at /-- The *principle of isolated zeros* for an analytic function, local version: if a function is analytic at `z₀`, then either it is identically zero in a neighborhood of `z₀`, or it does not vanish in a punctured neighborhood of `z₀`. -/ theorem eventually_eq_zero_or_eventually_ne_zero (hf : analytic_at 𝕜 f z₀) : (∀ᶠ z in 𝓝 z₀, f z = 0) ∨ (∀ᶠ z in 𝓝[≠] z₀, f z ≠ 0) := begin rcases hf with ⟨p, hp⟩, by_cases h : p = 0, { exact or.inl (has_fpower_series_at.eventually_eq_zero (by rwa h at hp)) }, { exact or.inr (hp.locally_ne_zero h) } end end analytic_at
247b7b3206525fe11dc2cf6db1edbafe1c511119
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/topology/sheaves/sheaf.lean
05b5dfa32f9835414bc7316a797110c02302b124
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
10,807
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 topology.sheaves.presheaf import category_theory.limits.punit import category_theory.limits.shapes.products import category_theory.limits.shapes.equalizers /-! # Sheaves We define sheaves on a topological space, with values in an arbitrary category. The sheaf condition for a `F : presheaf C X` requires that the morphism `F.obj U ⟶ ∏ F.obj (U i)` (where `U` is some open set which is the union of the `U i`) is the equalizer of the two morphisms `∏ F.obj (U i) ⟶ ∏ F.obj (U i) ⊓ (U j)`. We provide the instance `category (sheaf C X)` as the full subcategory of presheaves, and the fully faithful functor `sheaf.forget : sheaf C X ⥤ presheaf C X`. -/ universes v u noncomputable theory open category_theory open category_theory.limits open topological_space open opposite open topological_space.opens namespace Top variables {C : Type u} [category.{v} C] [has_products C] variables {X : Top.{v}} (F : presheaf C X) {ι : Type v} (U : ι → opens X) namespace sheaf_condition /-- The product of the sections of a presheaf over a family of open sets. -/ def pi_opens : C := ∏ (λ i : ι, F.obj (op (U i))) /-- The product of the sections of a presheaf over the pairwise intersections of a family of open sets. -/ def pi_inters : C := ∏ (λ p : ι × ι, F.obj (op (U p.1 ⊓ U p.2))) /-- The morphism `Π F.obj (U i) ⟶ Π F.obj (U i) ⊓ (U j)` whose components are given by the restriction maps from `U i` to `U i ⊓ U j`. -/ def left_res : pi_opens F U ⟶ pi_inters F U := pi.lift (λ p : ι × ι, pi.π _ p.1 ≫ F.map (inf_le_left (U p.1) (U p.2)).op) /-- The morphism `Π F.obj (U i) ⟶ Π F.obj (U i) ⊓ (U j)` whose components are given by the restriction maps from `U j` to `U i ⊓ U j`. -/ def right_res : pi_opens F U ⟶ pi_inters F U := pi.lift (λ p : ι × ι, pi.π _ p.2 ≫ F.map (inf_le_right (U p.1) (U p.2)).op) /-- The morphism `F.obj U ⟶ Π F.obj (U i)` whose components are given by the restriction maps from `U j` to `U i ⊓ U j`. -/ def res : F.obj (op (supr U)) ⟶ pi_opens F U := pi.lift (λ i : ι, F.map (topological_space.opens.le_supr U i).op) lemma w : res F U ≫ left_res F U = res F U ≫ right_res F U := begin dsimp [res, left_res, right_res], ext, simp only [limit.lift_π, limit.lift_π_assoc, fan.mk_π_app, category.assoc], rw [←F.map_comp], rw [←F.map_comp], congr, end /-- The equalizer diagram for the sheaf condition. -/ @[reducible] def diagram : walking_parallel_pair.{v} ⥤ C := parallel_pair (left_res F U) (right_res F U) /-- The restriction map `F.obj U ⟶ Π F.obj (U i)` gives a cone over the equalizer diagram for the sheaf condition. The sheaf condition asserts this cone is a limit cone. -/ def fork : fork.{v} (left_res F U) (right_res F U) := fork.of_ι _ (w F U) @[simp] lemma fork_X : (fork F U).X = F.obj (op (supr U)) := rfl @[simp] lemma fork_ι : (fork F U).ι = res F U := rfl @[simp] lemma fork_π_app_walking_parallel_pair_zero : (fork F U).π.app walking_parallel_pair.zero = res F U := rfl @[simp] lemma fork_π_app_walking_parallel_pair_one : (fork F U).π.app walking_parallel_pair.one = res F U ≫ left_res F U := rfl variables {F} {G : presheaf C X} /-- Isomorphic presheaves have isomorphic `pi_opens` for any cover `U`. -/ @[simp] def pi_opens.iso_of_iso (α : F ≅ G) : pi_opens F U ≅ pi_opens G U := pi.map_iso (λ X, α.app _) /-- Isomorphic presheaves have isomorphic `pi_inters` for any cover `U`. -/ @[simp] def pi_inters.iso_of_iso (α : F ≅ G) : pi_inters F U ≅ pi_inters G U := pi.map_iso (λ X, α.app _) /-- Isomorphic presheaves have isomorphic sheaf condition diagrams. -/ def diagram.iso_of_iso (α : F ≅ G) : diagram F U ≅ diagram G U := nat_iso.of_components begin rintro ⟨⟩, exact pi_opens.iso_of_iso U α, exact pi_inters.iso_of_iso U α end begin rintro ⟨⟩ ⟨⟩ ⟨⟩, { simp, }, { ext, simp [left_res], }, { ext, simp [right_res], }, { simp, }, end. /-- If `F G : presheaf C X` are isomorphic presheaves, then the `fork F U`, the canonical cone of the sheaf condition diagram for `F`, is isomorphic to `fork F G` postcomposed with the corresponding isomorphic between sheaf condition diagrams. -/ def fork.iso_of_iso (α : F ≅ G) : fork F U ≅ (cones.postcompose (diagram.iso_of_iso U α).inv).obj (fork G U) := begin fapply fork.ext, { apply α.app, }, { ext, dunfold fork.ι, -- Ugh, `simp` can't unfold abbreviations. simp [res, diagram.iso_of_iso], } end section open_embedding variables {V : Top.{v}} {j : V ⟶ X} (oe : open_embedding j) variables (𝒰 : ι → opens V) /-- Push forward a cover along an open embedding. -/ @[simp] def cover.of_open_embedding : ι → opens X := (λ i, oe.is_open_map.functor.obj (𝒰 i)) /-- The isomorphism between `pi_opens` corresponding to an open embedding. -/ @[simp] def pi_opens.iso_of_open_embedding : pi_opens (oe.is_open_map.functor.op ⋙ F) 𝒰 ≅ pi_opens F (cover.of_open_embedding oe 𝒰) := pi.map_iso (λ X, F.map_iso (iso.refl _)) /-- The isomorphism between `pi_inters` corresponding to an open embedding. -/ @[simp] def pi_inters.iso_of_open_embedding : pi_inters (oe.is_open_map.functor.op ⋙ F) 𝒰 ≅ pi_inters F (cover.of_open_embedding oe 𝒰) := pi.map_iso (λ X, F.map_iso begin dsimp [is_open_map.functor], exact iso.op { hom := hom_of_le (by { simp only [oe.to_embedding.inj, set.image_inter], apply le_refl _, }), inv := hom_of_le (by { simp only [oe.to_embedding.inj, set.image_inter], apply le_refl _, }), }, end) /-- The isomorphism of sheaf condition diagrams corresponding to an open embedding. -/ def diagram.iso_of_open_embedding : diagram (oe.is_open_map.functor.op ⋙ F) 𝒰 ≅ diagram F (cover.of_open_embedding oe 𝒰) := nat_iso.of_components begin rintro ⟨⟩, exact pi_opens.iso_of_open_embedding oe 𝒰, exact pi_inters.iso_of_open_embedding oe 𝒰 end begin rintro ⟨⟩ ⟨⟩ ⟨⟩, { simp, }, { ext, dsimp [left_res, is_open_map.functor], simp only [limit.lift_π, cones.postcompose_obj_π, iso.op_hom, discrete.nat_iso_hom_app, functor.map_iso_refl, functor.map_iso_hom, limit.map_π_assoc, limit.lift_map, fan.mk_π_app, nat_trans.comp_app, category.assoc], dsimp, rw [category.id_comp, ←F.map_comp], refl, }, { ext, dsimp [right_res, is_open_map.functor], simp only [limit.lift_π, cones.postcompose_obj_π, iso.op_hom, discrete.nat_iso_hom_app, functor.map_iso_refl, functor.map_iso_hom, limit.map_π_assoc, limit.lift_map, fan.mk_π_app, nat_trans.comp_app, category.assoc], dsimp, rw [category.id_comp, ←F.map_comp], refl, }, { simp, }, end. /-- If `F : presheaf C X` is a presheaf, and `oe : U ⟶ X` is an open embedding, then the sheaf condition fork for a cover `𝒰` in `U` for the composition of `oe` and `F` is isomorphic to sheaf condition fork for `oe '' 𝒰`, precomposed with the isomorphism of indexing diagrams `diagram.iso_of_open_embedding`. We use this to show that the restriction of sheaf along an open embedding is still a sheaf. -/ def fork.iso_of_open_embedding : fork (oe.is_open_map.functor.op ⋙ F) 𝒰 ≅ (cones.postcompose (diagram.iso_of_open_embedding oe 𝒰).inv).obj (fork F (cover.of_open_embedding oe 𝒰)) := begin fapply fork.ext, { dsimp [is_open_map.functor], exact F.map_iso (iso.op { hom := hom_of_le (by simp only [supr_s, supr_mk, le_def, subtype.coe_mk, set.le_eq_subset, set.image_Union]), inv := hom_of_le (by simp only [supr_s, supr_mk, le_def, subtype.coe_mk, set.le_eq_subset, set.image_Union]), }), }, { ext, dunfold fork.ι, -- Ugh, it is unpleasant that we need this. simp only [res, diagram.iso_of_open_embedding, discrete.nat_iso_inv_app, functor.map_iso_inv, limit.lift_π, cones.postcompose_obj_π, functor.comp_map, fork_π_app_walking_parallel_pair_zero, pi_opens.iso_of_open_embedding, nat_iso.of_components.inv_app, functor.map_iso_refl, functor.op_map, limit.lift_map, fan.mk_π_app, nat_trans.comp_app, has_hom.hom.unop_op, category.assoc], dsimp, rw [category.comp_id, ←F.map_comp], refl, }, end end open_embedding end sheaf_condition /-- The sheaf condition for a `F : presheaf C X` requires that the morphism `F.obj U ⟶ ∏ F.obj (U i)` (where `U` is some open set which is the union of the `U i`) is the equalizer of the two morphisms `∏ F.obj (U i) ⟶ ∏ F.obj (U i) ⊓ (U j)`. -/ -- One might prefer to work with sets of opens, rather than indexed families, -- which would reduce the universe level here to `max u v`. -- However as it's a subsingleton the universe level doesn't matter much. @[derive subsingleton] def sheaf_condition (F : presheaf C X) : Type (max u (v+1)) := Π ⦃ι : Type v⦄ (U : ι → opens X), is_limit (sheaf_condition.fork F U) /-- The presheaf valued in `punit` over any topological space is a sheaf. -/ def sheaf_condition_punit (F : presheaf (category_theory.discrete punit) X) : sheaf_condition F := λ ι U, punit_cone_is_limit -- Let's construct a trivial example, to keep the inhabited linter happy. instance sheaf_condition_inhabited (F : presheaf (category_theory.discrete punit) X) : inhabited (sheaf_condition F) := ⟨sheaf_condition_punit F⟩ /-- Transfer the sheaf condition across an isomorphism of presheaves. -/ def sheaf_condition_equiv_of_iso {F G : presheaf C X} (α : F ≅ G) : sheaf_condition F ≃ sheaf_condition G := equiv_of_subsingleton_of_subsingleton (λ c ι U, is_limit.of_iso_limit ((is_limit.postcompose_inv_equiv _ _).symm (c U)) (sheaf_condition.fork.iso_of_iso U α.symm).symm) (λ c ι U, is_limit.of_iso_limit ((is_limit.postcompose_inv_equiv _ _).symm (c U)) (sheaf_condition.fork.iso_of_iso U α).symm) variables (C X) /-- A `sheaf C X` is a presheaf of objects from `C` over a (bundled) topological space `X`, satisfying the sheaf condition. -/ structure sheaf := (presheaf : presheaf C X) (sheaf_condition : sheaf_condition presheaf) instance : category (sheaf C X) := induced_category.category sheaf.presheaf -- Let's construct a trivial example, to keep the inhabited linter happy. instance sheaf_inhabited : inhabited (sheaf (category_theory.discrete punit) X) := ⟨{ presheaf := functor.star _, sheaf_condition := default _ }⟩ namespace sheaf /-- The forgetful functor from sheaves to presheaves. -/ @[derive [full, faithful]] def forget : Top.sheaf C X ⥤ Top.presheaf C X := induced_functor sheaf.presheaf end sheaf end Top
2d5c11887da9152e84408c1cab91b898b274af62
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/group_theory/abelianization.lean
7ebcf8ed8127623d12c844c85b8f5c03f6d83b66
[ "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
3,357
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Michael Howes -/ import group_theory.quotient_group import tactic.group /-! # The abelianization of a group This file defines the commutator and the abelianization of a group. It furthermore prepares for the result that the abelianization is left adjoint to the forgetful functor from abelian groups to groups, which can be found in `algebra/category/Group/adjunctions`. ## Main definitions * `commutator`: defines the commutator of a group `G` as a subgroup of `G`. * `abelianization`: defines the abelianization of a group `G` as the quotient of a group by its commutator subgroup. -/ universes u v -- Let G be a group. variables (G : Type u) [group G] /-- The commutator subgroup of a group G is the normal subgroup generated by the commutators [p,q]=`p*q*p⁻¹*q⁻¹`. -/ @[derive subgroup.normal] def commutator : subgroup G := subgroup.normal_closure {x | ∃ p q, p * q * p⁻¹ * q⁻¹ = x} /-- The abelianization of G is the quotient of G by its commutator subgroup. -/ def abelianization : Type u := quotient_group.quotient (commutator G) namespace abelianization local attribute [instance] quotient_group.left_rel instance : comm_group (abelianization G) := { mul_comm := λ x y, quotient.induction_on₂' x y $ λ a b, begin apply quotient.sound, apply subgroup.subset_normal_closure, use b⁻¹, use a⁻¹, group, end, .. quotient_group.quotient.group _ } instance : inhabited (abelianization G) := ⟨1⟩ variable {G} /-- `of` is the canonical projection from G to its abelianization. -/ def of : G →* abelianization G := { to_fun := quotient_group.mk, map_one' := rfl, map_mul' := λ x y, rfl } section lift -- So far we have built Gᵃᵇ and proved it's an abelian group. -- Furthremore we defined the canonical projection `of : G → Gᵃᵇ` -- Let `A` be an abelian group and let `f` be a group homomorphism from `G` to `A`. variables {A : Type v} [comm_group A] (f : G →* A) lemma commutator_subset_ker : commutator G ≤ f.ker := begin apply subgroup.normal_closure_le_normal, rintros x ⟨p, q, rfl⟩, simp [monoid_hom.mem_ker, mul_right_comm (f p) (f q)], end /-- If `f : G → A` is a group homomorphism to an abelian group, then `lift f` is the unique map from the abelianization of a `G` to `A` that factors through `f`. -/ def lift : (G →* A) ≃ (abelianization G →* A) := { to_fun := λ f, quotient_group.lift _ f (λ x h, f.mem_ker.2 $ commutator_subset_ker _ h), inv_fun := λ F, F.comp of, left_inv := λ f, monoid_hom.ext $ λ x, rfl, right_inv := λ F, monoid_hom.ext $ λ x, quotient_group.induction_on x $ λ z, rfl } @[simp] lemma lift.of (x : G) : lift f (of x) = f x := rfl theorem lift.unique (φ : abelianization G →* A) -- hφ : φ agrees with f on the image of G in Gᵃᵇ (hφ : ∀ (x : G), φ (of x) = f x) {x : abelianization G} : φ x = lift f x := quotient_group.induction_on x hφ end lift variables {A : Type v} [monoid A] /-- See note [partially-applied ext lemmas]. -/ @[ext] theorem hom_ext (φ ψ : abelianization G →* A) (h : φ.comp of = ψ.comp of) : φ = ψ := monoid_hom.ext $ λ x, quotient_group.induction_on x $ monoid_hom.congr_fun h end abelianization
0e47d2bcca38e90e4e02c73a1b70936e1a4d5832
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/ind1.lean
47d236b39ff8e09bf1cf1dfa6493236bb638a95f
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
136
lean
inductive list (A : Type) : Type := | nil : list A | cons : A → list A → list A check list.{1} check cons.{1} check list_rec.{1 1}
2e85f990b44864d75af8bda4ad2d735b07eaa75d
32317185abf7e7c963f4c67c190aec61af6b3628
/hott/init/funext.hlean
e030ceb2861d98f0975bbbeaddf5553d9d9e4e14
[ "Apache-2.0" ]
permissive
Andrew-Zipperer-unorganized/lean
198a2317f21198cd8d26e7085e484b86277f17f7
dcb35008e1474a0abebe632b1dced120e5f8c009
refs/heads/master
1,622,526,520,945
1,453,576,559,000
1,454,612,842,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,452
hlean
/- Copyright (c) 2014 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Floris van Doorn Ported from Coq HoTT -/ prelude import .trunc .equiv .ua open eq is_trunc sigma function is_equiv equiv prod unit prod.ops lift /- We now prove that funext follows from a couple of weaker-looking forms of function extensionality. This proof is originally due to Voevodsky; it has since been simplified by Peter Lumsdaine and Michael Shulman. -/ definition funext.{l k} := Π ⦃A : Type.{l}⦄ {P : A → Type.{k}} (f g : Π x, P x), is_equiv (@apd10 A P f g) -- Naive funext is the simple assertion that pointwise equal functions are equal. definition naive_funext := Π ⦃A : Type⦄ {P : A → Type} (f g : Πx, P x), (f ~ g) → f = g -- Weak funext says that a product of contractible types is contractible. definition weak_funext := Π ⦃A : Type⦄ (P : A → Type) [H: Πx, is_contr (P x)], is_contr (Πx, P x) definition weak_funext_of_naive_funext : naive_funext → weak_funext := (λ nf A P (Pc : Πx, is_contr (P x)), let c := λx, center (P x) in is_contr.mk c (λ f, have eq' : (λx, center (P x)) ~ f, from (λx, center_eq (f x)), have eq : (λx, center (P x)) = f, from nf A P (λx, center (P x)) f eq', eq ) ) /- The less obvious direction is that weak_funext implies funext (and hence all three are logically equivalent). The point is that under weak funext, the space of "pointwise homotopies" has the same universal property as the space of paths. -/ section universe variables l k parameters [wf : weak_funext.{l k}] {A : Type.{l}} {B : A → Type.{k}} (f : Π x, B x) definition is_contr_sigma_homotopy : is_contr (Σ (g : Π x, B x), f ~ g) := is_contr.mk (sigma.mk f (homotopy.refl f)) (λ dp, sigma.rec_on dp (λ (g : Π x, B x) (h : f ~ g), let r := λ (k : Π x, Σ y, f x = y), @sigma.mk _ (λg, f ~ g) (λx, pr1 (k x)) (λx, pr2 (k x)) in let s := λ g h x, @sigma.mk _ (λy, f x = y) (g x) (h x) in have t1 : Πx, is_contr (Σ y, f x = y), from (λx, !is_contr_sigma_eq), have t2 : is_contr (Πx, Σ y, f x = y), from !wf, have t3 : (λ x, @sigma.mk _ (λ y, f x = y) (f x) idp) = s g h, from @eq_of_is_contr (Π x, Σ y, f x = y) t2 _ _, have t4 : r (λ x, sigma.mk (f x) idp) = r (s g h), from ap r t3, have endt : sigma.mk f (homotopy.refl f) = sigma.mk g h, from t4, endt ) ) local attribute is_contr_sigma_homotopy [instance] parameters (Q : Π g (h : f ~ g), Type) (d : Q f (homotopy.refl f)) definition homotopy_ind (g : Πx, B x) (h : f ~ g) : Q g h := @transport _ (λ gh, Q (pr1 gh) (pr2 gh)) (sigma.mk f (homotopy.refl f)) (sigma.mk g h) (@eq_of_is_contr _ is_contr_sigma_homotopy _ _) d local attribute weak_funext [reducible] local attribute homotopy_ind [reducible] definition homotopy_ind_comp : homotopy_ind f (homotopy.refl f) = d := (@hprop_eq_of_is_contr _ _ _ _ !eq_of_is_contr idp)⁻¹ ▸ idp end /- Now the proof is fairly easy; we can just use the same induction principle on both sides. -/ section universe variables l k local attribute weak_funext [reducible] theorem funext_of_weak_funext (wf : weak_funext.{l k}) : funext.{l k} := λ A B f g, let eq_to_f := (λ g' x, f = g') in let sim2path := homotopy_ind f eq_to_f idp in assert t1 : sim2path f (homotopy.refl f) = idp, proof homotopy_ind_comp f eq_to_f idp qed, assert t2 : apd10 (sim2path f (homotopy.refl f)) = (homotopy.refl f), proof ap apd10 t1 qed, have left_inv : apd10 ∘ (sim2path g) ~ id, proof (homotopy_ind f (λ g' x, apd10 (sim2path g' x) = x) t2) g qed, have right_inv : (sim2path g) ∘ apd10 ~ id, from (λ h, eq.rec_on h (homotopy_ind_comp f _ idp)), is_equiv.adjointify apd10 (sim2path g) left_inv right_inv definition funext_from_naive_funext : naive_funext → funext := compose funext_of_weak_funext weak_funext_of_naive_funext end section universe variables l private theorem ua_isequiv_postcompose {A B : Type.{l}} {C : Type} {w : A → B} [H0 : is_equiv w] : is_equiv (@compose C A B w) := let w' := equiv.mk w H0 in let eqinv : A = B := ((@is_equiv.inv _ _ _ (univalence A B)) w') in let eq' := equiv_of_eq eqinv in is_equiv.adjointify (@compose C A B w) (@compose C B A (is_equiv.inv w)) (λ (x : C → B), have eqretr : eq' = w', from (@right_inv _ _ (@equiv_of_eq A B) (univalence A B) w'), have invs_eq : (to_fun eq')⁻¹ = (to_fun w')⁻¹, from inv_eq eq' w' eqretr, have eqfin : (to_fun eq') ∘ ((to_fun eq')⁻¹ ∘ x) = x, from (λ p, (@eq.rec_on Type.{l} A (λ B' p', Π (x' : C → B'), (to_fun (equiv_of_eq p')) ∘ ((to_fun (equiv_of_eq p'))⁻¹ ∘ x') = x') B p (λ x', idp)) ) eqinv x, have eqfin' : (to_fun w') ∘ ((to_fun eq')⁻¹ ∘ x) = x, from eqretr ▸ eqfin, have eqfin'' : (to_fun w') ∘ ((to_fun w')⁻¹ ∘ x) = x, from invs_eq ▸ eqfin', eqfin'' ) (λ (x : C → A), have eqretr : eq' = w', from (@right_inv _ _ (@equiv_of_eq A B) (univalence A B) w'), have invs_eq : (to_fun eq')⁻¹ = (to_fun w')⁻¹, from inv_eq eq' w' eqretr, have eqfin : (to_fun eq')⁻¹ ∘ ((to_fun eq') ∘ x) = x, from (λ p, eq.rec_on p idp) eqinv, have eqfin' : (to_fun eq')⁻¹ ∘ ((to_fun w') ∘ x) = x, from eqretr ▸ eqfin, have eqfin'' : (to_fun w')⁻¹ ∘ ((to_fun w') ∘ x) = x, from invs_eq ▸ eqfin', eqfin'' ) -- We are ready to prove functional extensionality, -- starting with the naive non-dependent version. private definition diagonal [reducible] (B : Type) : Type := Σ xy : B × B, pr₁ xy = pr₂ xy private definition isequiv_src_compose {A B : Type} : @is_equiv (A → diagonal B) (A → B) (compose (pr₁ ∘ pr1)) := @ua_isequiv_postcompose _ _ _ (pr₁ ∘ pr1) (is_equiv.adjointify (pr₁ ∘ pr1) (λ x, sigma.mk (x , x) idp) (λx, idp) (λ x, sigma.rec_on x (λ xy, prod.rec_on xy (λ b c p, eq.rec_on p idp)))) private definition isequiv_tgt_compose {A B : Type} : is_equiv (compose (pr₂ ∘ pr1) : (A → diagonal B) → (A → B)) := begin refine @ua_isequiv_postcompose _ _ _ (pr2 ∘ pr1) _, fapply adjointify, { intro b, exact ⟨(b, b), idp⟩}, { intro b, reflexivity}, { intro a, induction a with q p, induction q, esimp at *, induction p, reflexivity} end theorem nondep_funext_from_ua {A : Type} {B : Type} : Π {f g : A → B}, f ~ g → f = g := (λ (f g : A → B) (p : f ~ g), let d := λ (x : A), sigma.mk (f x , f x) idp in let e := λ (x : A), sigma.mk (f x , g x) (p x) in let precomp1 := compose (pr₁ ∘ pr1) in have equiv1 [visible] : is_equiv precomp1, from @isequiv_src_compose A B, have equiv2 [visible] : Π x y, is_equiv (ap precomp1), from is_equiv.is_equiv_ap precomp1, have H' : Π (x y : A → diagonal B), pr₁ ∘ pr1 ∘ x = pr₁ ∘ pr1 ∘ y → x = y, from (λ x y, is_equiv.inv (ap precomp1)), have eq2 : pr₁ ∘ pr1 ∘ d = pr₁ ∘ pr1 ∘ e, from idp, have eq0 : d = e, from H' d e eq2, have eq1 : (pr₂ ∘ pr1) ∘ d = (pr₂ ∘ pr1) ∘ e, from ap _ eq0, eq1 ) end -- Now we use this to prove weak funext, which as we know -- implies (with dependent eta) also the strong dependent funext. theorem weak_funext_of_ua : weak_funext := (λ (A : Type) (P : A → Type) allcontr, let U := (λ (x : A), lift unit) in have pequiv : Π (x : A), P x ≃ unit, from (λ x, @equiv_unit_of_is_contr (P x) (allcontr x)), have psim : Π (x : A), P x = U x, from (λ x, eq_of_equiv_lift (pequiv x)), have p : P = U, from @nondep_funext_from_ua A Type P U psim, have tU' : is_contr (A → lift unit), from is_contr.mk (λ x, up ⋆) (λ f, nondep_funext_from_ua (λa, by induction (f a) with u;induction u;reflexivity)), have tU : is_contr (Π x, U x), from tU', have tlast : is_contr (Πx, P x), from p⁻¹ ▸ tU, tlast) -- In the following we will proof function extensionality using the univalence axiom definition funext_of_ua : funext := funext_of_weak_funext (@weak_funext_of_ua) variables {A : Type} {P : A → Type} {f g : Π x, P x} namespace funext theorem is_equiv_apd [instance] (f g : Π x, P x) : is_equiv (@apd10 A P f g) := funext_of_ua f g end funext open funext definition eq_equiv_homotopy : (f = g) ≃ (f ~ g) := equiv.mk apd10 _ definition eq_of_homotopy [reducible] : f ~ g → f = g := (@apd10 A P f g)⁻¹ definition apd10_eq_of_homotopy (p : f ~ g) : apd10 (eq_of_homotopy p) = p := right_inv apd10 p definition eq_of_homotopy_apd10 (p : f = g) : eq_of_homotopy (apd10 p) = p := left_inv apd10 p definition eq_of_homotopy_idp (f : Π x, P x) : eq_of_homotopy (λx : A, idpath (f x)) = idpath f := is_equiv.left_inv apd10 idp definition naive_funext_of_ua : naive_funext := λ A P f g h, eq_of_homotopy h protected definition homotopy.rec_on [recursor] {Q : (f ~ g) → Type} (p : f ~ g) (H : Π(q : f = g), Q (apd10 q)) : Q p := right_inv apd10 p ▸ H (eq_of_homotopy p) protected definition homotopy.rec_on_idp [recursor] {Q : Π{g}, (f ~ g) → Type} {g : Π x, P x} (p : f ~ g) (H : Q (homotopy.refl f)) : Q p := homotopy.rec_on p (λq, eq.rec_on q H) definition eq_of_homotopy_inv {f g : Π x, P x} (H : f ~ g) : eq_of_homotopy (λx, (H x)⁻¹) = (eq_of_homotopy H)⁻¹ := begin apply homotopy.rec_on_idp H, rewrite [+eq_of_homotopy_idp] end definition eq_of_homotopy_con {f g h : Π x, P x} (H1 : f ~ g) (H2 : g ~ h) : eq_of_homotopy (λx, H1 x ⬝ H2 x) = eq_of_homotopy H1 ⬝ eq_of_homotopy H2 := begin apply homotopy.rec_on_idp H1, apply homotopy.rec_on_idp H2, rewrite [+eq_of_homotopy_idp] end
e08661cf17fda4180bc5105d8cda6d0c51c52552
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/pnat/factors.lean
3fc139375fef7d2b5bf83b78d8ab93debf7a9f45
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
10,687
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Neil Strickland -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.pnat.prime import Mathlib.data.multiset.sort import Mathlib.data.int.gcd import Mathlib.algebra.group.default import Mathlib.PostPort namespace Mathlib /-- The type of multisets of prime numbers. Unique factorization gives an equivalence between this set and ℕ+, as we will formalize below. -/ def prime_multiset := multiset nat.primes namespace prime_multiset protected instance inhabited : Inhabited prime_multiset := eq.mpr sorry multiset.inhabited protected instance has_repr : has_repr prime_multiset := id multiset.has_repr protected instance canonically_ordered_add_monoid : canonically_ordered_add_monoid prime_multiset := id multiset.canonically_ordered_add_monoid protected instance distrib_lattice : distrib_lattice prime_multiset := id multiset.distrib_lattice protected instance semilattice_sup_bot : semilattice_sup_bot prime_multiset := id multiset.semilattice_sup_bot protected instance has_sub : Sub prime_multiset := id multiset.has_sub theorem add_sub_of_le {u : prime_multiset} {v : prime_multiset} : u ≤ v → u + (v - u) = v := multiset.add_sub_of_le /-- The multiset consisting of a single prime -/ def of_prime (p : nat.primes) : prime_multiset := p ::ₘ 0 theorem card_of_prime (p : nat.primes) : coe_fn multiset.card (of_prime p) = 1 := rfl /-- We can forget the primality property and regard a multiset of primes as just a multiset of positive integers, or a multiset of natural numbers. In the opposite direction, if we have a multiset of positive integers or natural numbers, together with a proof that all the elements are prime, then we can regard it as a multiset of primes. The next block of results records obvious properties of these coercions. -/ def to_nat_multiset : prime_multiset → multiset ℕ := fun (v : prime_multiset) => multiset.map (fun (p : nat.primes) => ↑p) v protected instance coe_nat : has_coe prime_multiset (multiset ℕ) := has_coe.mk to_nat_multiset protected instance coe_nat_hom : is_add_monoid_hom coe := eq.mpr (id ((fun (f f_1 : prime_multiset → multiset ℕ) (e_3 : f = f_1) => congr_arg is_add_monoid_hom e_3) coe to_nat_multiset (Eq.trans (Eq.trans (Eq.trans coe.equations._eqn_1 lift_t.equations._eqn_1) coe_t.equations._eqn_1) coe_b.equations._eqn_1))) (id (multiset.map.is_add_monoid_hom coe)) theorem coe_nat_injective : function.injective coe := multiset.map_injective nat.primes.coe_nat_inj theorem coe_nat_of_prime (p : nat.primes) : ↑(of_prime p) = ↑p ::ₘ 0 := rfl theorem coe_nat_prime (v : prime_multiset) (p : ℕ) (h : p ∈ ↑v) : nat.prime p := sorry /-- Converts a `prime_multiset` to a `multiset ℕ+`. -/ def to_pnat_multiset : prime_multiset → multiset ℕ+ := fun (v : prime_multiset) => multiset.map (fun (p : nat.primes) => ↑p) v protected instance coe_pnat : has_coe prime_multiset (multiset ℕ+) := has_coe.mk to_pnat_multiset protected instance coe_pnat_hom : is_add_monoid_hom coe := eq.mpr (id ((fun (f f_1 : prime_multiset → multiset ℕ+) (e_3 : f = f_1) => congr_arg is_add_monoid_hom e_3) coe to_pnat_multiset (Eq.trans (Eq.trans (Eq.trans coe.equations._eqn_1 lift_t.equations._eqn_1) coe_t.equations._eqn_1) coe_b.equations._eqn_1))) (id (multiset.map.is_add_monoid_hom coe)) theorem coe_pnat_injective : function.injective coe := multiset.map_injective nat.primes.coe_pnat_inj theorem coe_pnat_of_prime (p : nat.primes) : ↑(of_prime p) = ↑p ::ₘ 0 := rfl theorem coe_pnat_prime (v : prime_multiset) (p : ℕ+) (h : p ∈ ↑v) : pnat.prime p := sorry protected instance coe_multiset_pnat_nat : has_coe (multiset ℕ+) (multiset ℕ) := has_coe.mk fun (v : multiset ℕ+) => multiset.map (fun (n : ℕ+) => ↑n) v theorem coe_pnat_nat (v : prime_multiset) : ↑↑v = ↑v := sorry /-- The product of a `prime_multiset`, as a `ℕ+`. -/ def prod (v : prime_multiset) : ℕ+ := multiset.prod ↑v theorem coe_prod (v : prime_multiset) : ↑(prod v) = multiset.prod ↑v := sorry theorem prod_of_prime (p : nat.primes) : prod (of_prime p) = ↑p := sorry /-- If a `multiset ℕ` consists only of primes, it can be recast as a `prime_multiset`. -/ def of_nat_multiset (v : multiset ℕ) (h : ∀ (p : ℕ), p ∈ v → nat.prime p) : prime_multiset := multiset.pmap (fun (p : ℕ) (hp : nat.prime p) => { val := p, property := hp }) v h theorem to_of_nat_multiset (v : multiset ℕ) (h : ∀ (p : ℕ), p ∈ v → nat.prime p) : ↑(of_nat_multiset v h) = v := sorry theorem prod_of_nat_multiset (v : multiset ℕ) (h : ∀ (p : ℕ), p ∈ v → nat.prime p) : ↑(prod (of_nat_multiset v h)) = multiset.prod v := eq.mpr (id (Eq._oldrec (Eq.refl (↑(prod (of_nat_multiset v h)) = multiset.prod v)) (coe_prod (of_nat_multiset v h)))) (eq.mpr (id (Eq._oldrec (Eq.refl (multiset.prod ↑(of_nat_multiset v h) = multiset.prod v)) (to_of_nat_multiset v h))) (Eq.refl (multiset.prod v))) /-- If a `multiset ℕ+` consists only of primes, it can be recast as a `prime_multiset`. -/ def of_pnat_multiset (v : multiset ℕ+) (h : ∀ (p : ℕ+), p ∈ v → pnat.prime p) : prime_multiset := multiset.pmap (fun (p : ℕ+) (hp : pnat.prime p) => { val := ↑p, property := hp }) v h theorem to_of_pnat_multiset (v : multiset ℕ+) (h : ∀ (p : ℕ+), p ∈ v → pnat.prime p) : ↑(of_pnat_multiset v h) = v := sorry theorem prod_of_pnat_multiset (v : multiset ℕ+) (h : ∀ (p : ℕ+), p ∈ v → pnat.prime p) : prod (of_pnat_multiset v h) = multiset.prod v := sorry /-- Lists can be coerced to multisets; here we have some results about how this interacts with our constructions on multisets. -/ def of_nat_list (l : List ℕ) (h : ∀ (p : ℕ), p ∈ l → nat.prime p) : prime_multiset := of_nat_multiset (↑l) h theorem prod_of_nat_list (l : List ℕ) (h : ∀ (p : ℕ), p ∈ l → nat.prime p) : ↑(prod (of_nat_list l h)) = list.prod l := eq.mp (Eq._oldrec (Eq.refl (↑(prod (of_nat_multiset (↑l) h)) = multiset.prod ↑l)) (multiset.coe_prod l)) (prod_of_nat_multiset (↑l) h) /-- If a `list ℕ+` consists only of primes, it can be recast as a `prime_multiset` with the coercion from lists to multisets. -/ def of_pnat_list (l : List ℕ+) (h : ∀ (p : ℕ+), p ∈ l → pnat.prime p) : prime_multiset := of_pnat_multiset (↑l) h theorem prod_of_pnat_list (l : List ℕ+) (h : ∀ (p : ℕ+), p ∈ l → pnat.prime p) : prod (of_pnat_list l h) = list.prod l := eq.mp (Eq._oldrec (Eq.refl (prod (of_pnat_multiset (↑l) h) = multiset.prod ↑l)) (multiset.coe_prod l)) (prod_of_pnat_multiset (↑l) h) /-- The product map gives a homomorphism from the additive monoid of multisets to the multiplicative monoid ℕ+. -/ theorem prod_zero : prod 0 = 1 := id multiset.prod_zero theorem prod_add (u : prime_multiset) (v : prime_multiset) : prod (u + v) = prod u * prod v := sorry theorem prod_smul (d : ℕ) (u : prime_multiset) : prod (d •ℕ u) = prod u ^ d := sorry end prime_multiset namespace pnat /-- The prime factors of n, regarded as a multiset -/ def factor_multiset (n : ℕ+) : prime_multiset := prime_multiset.of_nat_list (nat.factors ↑n) sorry /-- The product of the factors is the original number -/ theorem prod_factor_multiset (n : ℕ+) : prime_multiset.prod (factor_multiset n) = n := sorry theorem coe_nat_factor_multiset (n : ℕ+) : ↑(factor_multiset n) = ↑(nat.factors ↑n) := prime_multiset.to_of_nat_multiset (↑(nat.factors ↑n)) nat.mem_factors end pnat namespace prime_multiset /-- If we start with a multiset of primes, take the product and then factor it, we get back the original multiset. -/ theorem factor_multiset_prod (v : prime_multiset) : pnat.factor_multiset (prod v) = v := sorry end prime_multiset namespace pnat /-- Positive integers biject with multisets of primes. -/ def factor_multiset_equiv : ℕ+ ≃ prime_multiset := equiv.mk factor_multiset prime_multiset.prod prod_factor_multiset prime_multiset.factor_multiset_prod /-- Factoring gives a homomorphism from the multiplicative monoid ℕ+ to the additive monoid of multisets. -/ theorem factor_multiset_one : factor_multiset 1 = 0 := rfl theorem factor_multiset_mul (n : ℕ+) (m : ℕ+) : factor_multiset (n * m) = factor_multiset n + factor_multiset m := sorry theorem factor_multiset_pow (n : ℕ+) (m : ℕ) : factor_multiset (n ^ m) = m •ℕ factor_multiset n := sorry /-- Factoring a prime gives the corresponding one-element multiset. -/ theorem factor_multiset_of_prime (p : nat.primes) : factor_multiset ↑p = prime_multiset.of_prime p := sorry /-- We now have four different results that all encode the idea that inequality of multisets corresponds to divisibility of positive integers. -/ theorem factor_multiset_le_iff {m : ℕ+} {n : ℕ+} : factor_multiset m ≤ factor_multiset n ↔ m ∣ n := sorry theorem factor_multiset_le_iff' {m : ℕ+} {v : prime_multiset} : factor_multiset m ≤ v ↔ m ∣ prime_multiset.prod v := sorry end pnat namespace prime_multiset theorem prod_dvd_iff {u : prime_multiset} {v : prime_multiset} : prod u ∣ prod v ↔ u ≤ v := let h : pnat.factor_multiset (prod u) ≤ v ↔ prod u ∣ prod v := pnat.factor_multiset_le_iff'; iff.symm (eq.mp (Eq._oldrec (Eq.refl (pnat.factor_multiset (prod u) ≤ v ↔ prod u ∣ prod v)) (factor_multiset_prod u)) h) theorem prod_dvd_iff' {u : prime_multiset} {n : ℕ+} : prod u ∣ n ↔ u ≤ pnat.factor_multiset n := sorry end prime_multiset namespace pnat /-- The gcd and lcm operations on positive integers correspond to the inf and sup operations on multisets. -/ theorem factor_multiset_gcd (m : ℕ+) (n : ℕ+) : factor_multiset (gcd m n) = factor_multiset m ⊓ factor_multiset n := sorry theorem factor_multiset_lcm (m : ℕ+) (n : ℕ+) : factor_multiset (lcm m n) = factor_multiset m ⊔ factor_multiset n := sorry /-- The number of occurrences of p in the factor multiset of m is the same as the p-adic valuation of m. -/ theorem count_factor_multiset (m : ℕ+) (p : nat.primes) (k : ℕ) : ↑p ^ k ∣ m ↔ k ≤ multiset.count p (factor_multiset m) := sorry end pnat namespace prime_multiset theorem prod_inf (u : prime_multiset) (v : prime_multiset) : prod (u ⊓ v) = pnat.gcd (prod u) (prod v) := sorry theorem prod_sup (u : prime_multiset) (v : prime_multiset) : prod (u ⊔ v) = pnat.lcm (prod u) (prod v) := sorry
7dc55c19db184cca6adb95ca3be3363687889e3b
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/analysis/calculus/mean_value.lean
4798257705da2c631d8078a657ea10787b2fffd8
[ "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
7,661
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 The mean value inequality: a bound on the derivative of a function implies that this function is Lipschitz continuous for the same bound. -/ import analysis.calculus.deriv set_option class.instance_max_depth 100 variables {E : Type*} [normed_group E] [normed_space ℝ E] {F : Type*} [normed_group F] [normed_space ℝ F] open metric set lattice asymptotics continuous_linear_map /-- The mean value theorem along a segment: a bound on the derivative of a function along a segment implies a bound on the distance of the endpoints images -/ theorem norm_image_sub_le_of_norm_deriv_le_segment {f : ℝ → F} {C : ℝ} (hf : differentiable_on ℝ f (Icc 0 1)) (bound : ∀t ∈ Icc (0:ℝ) 1, ∥fderiv_within ℝ f (Icc 0 1) t∥ ≤ C) : ∥f 1 - f 0∥ ≤ C := begin /- Let D > C. We will show that, for all t ∈ [0,1], one has ∥f t - f 0∥ ≤ D * t. This is true for t=0. Let k be maximal in [0,1] for which this holds. By continuity of all functions, the maximum is realized. If k were <1, then a point x slightly to its right would satisfy ∥f x - f k∥ ≤ D * (k-x), since the differential of f at k has norm at most C < D. Therefore, the point x also satisfies ∥f x - f 0∥ ≤ D * x, contradicting the maximality of k. Hence, k = 1. -/ refine le_of_forall_le_of_dense (λD hD, _), let K := {t ∈ Icc (0 : ℝ) 1 | ∥f t - f 0∥ ≤ D * t}, let k := Sup K, have k_mem_K : k ∈ K, { refine cSup_mem_of_is_closed _ _ _, show K ≠ ∅, { have : (0 : ℝ) ∈ K, by simp [K, le_refl, zero_le_one], apply ne_empty_of_mem this }, show bdd_above K, from ⟨1, λy hy, hy.1.2⟩, have A : continuous_on (λt:ℝ, (∥f t - f 0∥, D * t)) (Icc 0 1), { apply continuous_on.prod, { refine continuous_norm.comp_continuous_on _, apply continuous_on.sub hf.continuous_on continuous_on_const }, { exact (continuous_mul continuous_const continuous_id).continuous_on } }, show is_closed K, from A.preimage_closed_of_closed is_closed_Icc (ordered_topology.is_closed_le' _) }, have : k = 1, { by_contradiction k_ne_1, have k_lt_1 : k < 1 := lt_of_le_of_ne k_mem_K.1.2 k_ne_1, have : 0 ≤ k := k_mem_K.1.1, let g := fderiv_within ℝ f (Icc 0 1) k, let h := λx, f x - f k - g (x-k), have : is_o (λ x, h x) (λ x, x - k) (nhds_within k (Icc 0 1)) := (hf k k_mem_K.1).has_fderiv_within_at, have : {x | ∥h x∥ ≤ (D-C) * ∥x-k∥} ∈ nhds_within k (Icc 0 1) := this (D-C) (sub_pos_of_lt hD), rcases (mem_nhds_within _ _ _).1 this with ⟨s, s_open, ks, hs⟩, rcases is_open_iff.1 s_open k ks with ⟨ε, εpos, hε⟩, change 0 < ε at εpos, let δ := min ε (1-k), have δpos : 0 < δ, by simp [δ, εpos, k_lt_1], let x := k + δ/2, have k_lt_x : k < x, by { simp only [x], linarith }, have x_lt_1 : x < 1 := calc x < k + δ : add_lt_add_left (half_lt_self δpos) _ ... ≤ k + (1-k) : add_le_add_left (min_le_right _ _) _ ... = 1 : by ring, have xε : x ∈ ball k ε, { simp [dist, x, abs_of_nonneg (le_of_lt (half_pos δpos))], exact lt_of_lt_of_le (half_lt_self δpos) (min_le_left _ _) }, have xI : x ∈ Icc (0:ℝ) 1 := ⟨le_of_lt (lt_of_le_of_lt (k_mem_K.1.1) k_lt_x), le_of_lt x_lt_1⟩, have Ih : ∥h x∥ ≤ (D - C) * ∥x - k∥ := hs ⟨hε xε, xI⟩, have I : ∥f x - f k∥ ≤ D * (x-k) := calc ∥f x - f k∥ = ∥g (x-k) + h x∥ : by { congr' 1, simp only [h], abel } ... ≤ ∥g (x-k)∥ + ∥h x∥ : norm_triangle _ _ ... ≤ ∥g∥ * ∥x-k∥ + (D-C) * ∥x-k∥ : add_le_add (g.le_op_norm _) Ih ... ≤ C * ∥x-k∥ + (D-C) * ∥x-k∥ : add_le_add_right (mul_le_mul_of_nonneg_right (bound k k_mem_K.1) (norm_nonneg _)) _ ... = D * ∥x-k∥ : by ring ... = D * (x-k) : by simp [norm, abs_of_nonneg (le_of_lt (half_pos δpos))], have : ∥f x - f 0∥ ≤ D * x := calc ∥f x - f 0∥ = ∥(f x - f k) + (f k - f 0)∥ : by { congr' 1, abel } ... ≤ ∥f x - f k∥ + ∥f k - f 0∥ : norm_triangle _ _ ... ≤ D * (x - k) + D * k : add_le_add I (k_mem_K.2) ... = D * x : by ring, have xK : x ∈ K := ⟨xI, this⟩, have : x ≤ k := le_cSup ⟨1, λy hy, hy.1.2⟩ xK, exact (not_le_of_lt k_lt_x) this }, rw this at k_mem_K, simpa [this] using k_mem_K.2 end /-- The mean value theorem on a convex set: if the derivative of a function is bounded by C, then the function is C-Lipschitz -/ theorem norm_image_sub_le_of_norm_deriv_le_convex {f : E → F} {C : ℝ} {s : set E} {x y : E} (hf : differentiable_on ℝ f s) (bound : ∀x∈s, ∥fderiv_within ℝ f s x∥ ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∥f y - f x∥ ≤ C * ∥y - x∥ := begin /- By composition with t ↦ x + t • (y-x), we reduce to a statement for functions defined on [0,1], for which it is proved in norm_image_sub_le_of_norm_deriv_le_segment. We just have to check the differentiability of the composition and bounds on its derivative, which is straightforward but tedious for lack of automation. -/ have C0 : 0 ≤ C := le_trans (norm_nonneg _) (bound x xs), let g := λ(t:ℝ), f (x + t • (y-x)), have D1 : differentiable ℝ (λt:ℝ, x + t • (y-x)) := differentiable.add (differentiable_const _) (differentiable.smul' differentiable_id (differentiable_const _)), have segm : (λ (t : ℝ), x + t • (y - x)) '' Icc 0 1 ⊆ s, by { rw image_Icc_zero_one_eq_segment, apply (convex_segment_iff _).1 hs x y xs ys }, have : f x = g 0, by { simp only [g], rw [zero_smul, add_zero] }, rw this, have : f y = g 1, by { simp only [g], rw one_smul, congr' 1, abel }, rw this, apply norm_image_sub_le_of_norm_deriv_le_segment (hf.comp D1.differentiable_on segm) (λt ht, _), /- It remains to check that the derivative of g is bounded by C ∥y-x∥ at any t ∈ [0,1] -/ have t_s : x + t • (y-x) ∈ s := segm (mem_image_of_mem _ ht), simp only [g], /- Expand the derivative of the composition, and bound its norm by the product of the norms -/ rw fderiv_within.comp t (hf _ t_s) ((D1 t).differentiable_within_at) segm (unique_diff_on_Icc_zero_one t ht), refine le_trans (op_norm_comp_le _ _) (mul_le_mul (bound _ t_s) _ (norm_nonneg _) C0), have : fderiv_within ℝ (λ (t : ℝ), x + t • (y - x)) (Icc 0 1) t = (id : ℝ →L[ℝ] ℝ).scalar_prod_space_iso (y - x) := calc fderiv_within ℝ (λ (t : ℝ), x + t • (y - x)) (Icc 0 1) t = fderiv ℝ (λ (t : ℝ), x + t • (y - x)) t : differentiable.fderiv_within (D1 t) (unique_diff_on_Icc_zero_one t ht) ... = fderiv ℝ (λ (t : ℝ), x) t + fderiv ℝ (λ (t : ℝ), t • (y-x)) t : fderiv_add (differentiable_at_const _) ((differentiable.smul' differentiable_id (differentiable_const _)) t) ... = fderiv ℝ (λ (t : ℝ), t • (y-x)) t : by rw [fderiv_const, zero_add] ... = t • fderiv ℝ (λ (t : ℝ), (y-x)) t + (fderiv ℝ id t).scalar_prod_space_iso (y - x) : fderiv_smul' differentiable_at_id (differentiable_at_const _) ... = (id : ℝ →L[ℝ] ℝ).scalar_prod_space_iso (y - x) : by rw [fderiv_const, smul_zero, zero_add, fderiv_id], rw [this, scalar_prod_space_iso_norm], calc ∥(id : ℝ →L[ℝ] ℝ)∥ * ∥y - x∥ ≤ 1 * ∥y-x∥ : mul_le_mul_of_nonneg_right norm_id (norm_nonneg _) ... = ∥y - x∥ : one_mul _ end
0296d5326adb7980dbc1af0a0c9d556786c2db51
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/clifford_algebra/basic.lean
5e6a00f8c1f2c6c6c40c562eca6486e6c50e7483
[ "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
12,282
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Utensil Song -/ import algebra.ring_quot import linear_algebra.tensor_algebra.basic import linear_algebra.exterior_algebra.basic import linear_algebra.quadratic_form.basic /-! # Clifford Algebras We construct the Clifford algebra of a module `M` over a commutative ring `R`, equipped with a quadratic_form `Q`. ## Notation The Clifford algebra of the `R`-module `M` equipped with a quadratic_form `Q` is denoted as `clifford_algebra Q`. Given a linear morphism `f : M → A` from a module `M` to another `R`-algebra `A`, such that `cond : ∀ m, f m * f m = algebra_map _ _ (Q m)`, there is a (unique) lift of `f` to an `R`-algebra morphism, which is denoted `clifford_algebra.lift Q f cond`. The canonical linear map `M → clifford_algebra Q` is denoted `clifford_algebra.ι Q`. ## Theorems The main theorems proved ensure that `clifford_algebra Q` satisfies the universal property of the Clifford algebra. 1. `ι_comp_lift` is the fact that the composition of `ι Q` with `lift Q f cond` agrees with `f`. 2. `lift_unique` ensures the uniqueness of `lift Q f cond` with respect to 1. Additionally, when `Q = 0` an `alg_equiv` to the `exterior_algebra` is provided as `as_exterior`. ## Implementation details The Clifford algebra of `M` is constructed as a quotient of the tensor algebra, as follows. 1. We define a relation `clifford_algebra.rel Q` on `tensor_algebra R M`. This is the smallest relation which identifies squares of elements of `M` with `Q m`. 2. The Clifford algebra is the quotient of the tensor algebra by this relation. This file is almost identical to `linear_algebra/exterior_algebra.lean`. -/ variables {R : Type*} [comm_ring R] variables {M : Type*} [add_comm_group M] [module R M] variables (Q : quadratic_form R M) variable {n : ℕ} namespace clifford_algebra open tensor_algebra /-- `rel` relates each `ι m * ι m`, for `m : M`, with `Q m`. The Clifford algebra of `M` is defined as the quotient modulo this relation. -/ inductive rel : tensor_algebra R M → tensor_algebra R M → Prop | of (m : M) : rel (ι R m * ι R m) (algebra_map R _ (Q m)) end clifford_algebra /-- The Clifford algebra of an `R`-module `M` equipped with a quadratic_form `Q`. -/ @[derive [inhabited, ring, algebra R]] def clifford_algebra := ring_quot (clifford_algebra.rel Q) namespace clifford_algebra /-- The canonical linear map `M →ₗ[R] clifford_algebra Q`. -/ def ι : M →ₗ[R] clifford_algebra Q := (ring_quot.mk_alg_hom R _).to_linear_map.comp (tensor_algebra.ι R) /-- As well as being linear, `ι Q` squares to the quadratic form -/ @[simp] theorem ι_sq_scalar (m : M) : ι Q m * ι Q m = algebra_map R _ (Q m) := begin erw [←alg_hom.map_mul, ring_quot.mk_alg_hom_rel R (rel.of m), alg_hom.commutes], refl, end variables {Q} {A : Type*} [semiring A] [algebra R A] @[simp] theorem comp_ι_sq_scalar (g : clifford_algebra Q →ₐ[R] A) (m : M) : g (ι Q m) * g (ι Q m) = algebra_map _ _ (Q m) := by rw [←alg_hom.map_mul, ι_sq_scalar, alg_hom.commutes] variables (Q) /-- Given a linear map `f : M →ₗ[R] A` into an `R`-algebra `A`, which satisfies the condition: `cond : ∀ m : M, f m * f m = Q(m)`, this is the canonical lift of `f` to a morphism of `R`-algebras from `clifford_algebra Q` to `A`. -/ @[simps symm_apply] def lift : {f : M →ₗ[R] A // ∀ m, f m * f m = algebra_map _ _ (Q m)} ≃ (clifford_algebra Q →ₐ[R] A) := { to_fun := λ f, ring_quot.lift_alg_hom R ⟨tensor_algebra.lift R (f : M →ₗ[R] A), (λ x y (h : rel Q x y), by { induction h, rw [alg_hom.commutes, alg_hom.map_mul, tensor_algebra.lift_ι_apply, f.prop], })⟩, inv_fun := λ F, ⟨F.to_linear_map.comp (ι Q), λ m, by rw [ linear_map.comp_apply, alg_hom.to_linear_map_apply, comp_ι_sq_scalar]⟩, left_inv := λ f, by { ext, simp only [ι, alg_hom.to_linear_map_apply, function.comp_app, linear_map.coe_comp, subtype.coe_mk, ring_quot.lift_alg_hom_mk_alg_hom_apply, tensor_algebra.lift_ι_apply] }, right_inv := λ F, by { ext, simp only [ι, alg_hom.comp_to_linear_map, alg_hom.to_linear_map_apply, function.comp_app, linear_map.coe_comp, subtype.coe_mk, ring_quot.lift_alg_hom_mk_alg_hom_apply, tensor_algebra.lift_ι_apply] } } variables {Q} @[simp] theorem ι_comp_lift (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = algebra_map _ _ (Q m)) : (lift Q ⟨f, cond⟩).to_linear_map.comp (ι Q) = f := (subtype.mk_eq_mk.mp $ (lift Q).symm_apply_apply ⟨f, cond⟩) @[simp] theorem lift_ι_apply (f : M →ₗ[R] A) (cond : ∀ m, f m * f m = algebra_map _ _ (Q m)) (x) : lift Q ⟨f, cond⟩ (ι Q x) = f x := (linear_map.ext_iff.mp $ ι_comp_lift f cond) x @[simp] theorem lift_unique (f : M →ₗ[R] A) (cond : ∀ m : M, f m * f m = algebra_map _ _ (Q m)) (g : clifford_algebra Q →ₐ[R] A) : g.to_linear_map.comp (ι Q) = f ↔ g = lift Q ⟨f, cond⟩ := begin convert (lift Q).symm_apply_eq, rw lift_symm_apply, simp only, end attribute [irreducible] clifford_algebra ι lift @[simp] theorem lift_comp_ι (g : clifford_algebra Q →ₐ[R] A) : lift Q ⟨g.to_linear_map.comp (ι Q), comp_ι_sq_scalar _⟩ = g := begin convert (lift Q).apply_symm_apply g, rw lift_symm_apply, refl, end /-- See note [partially-applied ext lemmas]. -/ @[ext] theorem hom_ext {A : Type*} [semiring A] [algebra R A] {f g : clifford_algebra Q →ₐ[R] A} : f.to_linear_map.comp (ι Q) = g.to_linear_map.comp (ι Q) → f = g := begin intro h, apply (lift Q).symm.injective, rw [lift_symm_apply, lift_symm_apply], simp only [h], end /-- If `C` holds for the `algebra_map` of `r : R` into `clifford_algebra Q`, the `ι` of `x : M`, and is preserved under addition and muliplication, then it holds for all of `clifford_algebra Q`. -/ -- This proof closely follows `tensor_algebra.induction` @[elab_as_eliminator] lemma induction {C : clifford_algebra Q → Prop} (h_grade0 : ∀ r, C (algebra_map R (clifford_algebra Q) r)) (h_grade1 : ∀ x, C (ι Q x)) (h_mul : ∀ a b, C a → C b → C (a * b)) (h_add : ∀ a b, C a → C b → C (a + b)) (a : clifford_algebra Q) : C a := begin -- the arguments are enough to construct a subalgebra, and a mapping into it from M let s : subalgebra R (clifford_algebra Q) := { carrier := C, mul_mem' := h_mul, add_mem' := h_add, algebra_map_mem' := h_grade0, }, let of : { f : M →ₗ[R] s // ∀ m, f m * f m = algebra_map _ _ (Q m) } := ⟨(ι Q).cod_restrict s.to_submodule h_grade1, λ m, subtype.eq $ ι_sq_scalar Q m ⟩, -- the mapping through the subalgebra is the identity have of_id : alg_hom.id R (clifford_algebra Q) = s.val.comp (lift Q of), { ext, simp [of], }, -- finding a proof is finding an element of the subalgebra convert subtype.prop (lift Q of a), exact alg_hom.congr_fun of_id a, end /-- A Clifford algebra with a zero quadratic form is isomorphic to an `exterior_algebra` -/ def as_exterior : clifford_algebra (0 : quadratic_form R M) ≃ₐ[R] exterior_algebra R M := alg_equiv.of_alg_hom (clifford_algebra.lift 0 ⟨(exterior_algebra.ι R), by simp only [forall_const, ring_hom.map_zero, exterior_algebra.ι_sq_zero, quadratic_form.zero_apply]⟩) (exterior_algebra.lift R ⟨(ι (0 : quadratic_form R M)), by simp only [forall_const, ring_hom.map_zero, quadratic_form.zero_apply, ι_sq_scalar]⟩) (exterior_algebra.hom_ext $ linear_map.ext $ by simp only [alg_hom.comp_to_linear_map, linear_map.coe_comp, function.comp_app, alg_hom.to_linear_map_apply, exterior_algebra.lift_ι_apply, clifford_algebra.lift_ι_apply, alg_hom.to_linear_map_id, linear_map.id_comp, eq_self_iff_true, forall_const]) (clifford_algebra.hom_ext $ linear_map.ext $ by simp only [alg_hom.comp_to_linear_map, linear_map.coe_comp, function.comp_app, alg_hom.to_linear_map_apply, clifford_algebra.lift_ι_apply, exterior_algebra.lift_ι_apply, alg_hom.to_linear_map_id, linear_map.id_comp, eq_self_iff_true, forall_const]) /-- The symmetric product of vectors is a scalar -/ lemma ι_mul_ι_add_swap (a b : M) : ι Q a * ι Q b + ι Q b * ι Q a = algebra_map R _ (quadratic_form.polar Q a b) := calc ι Q a * ι Q b + ι Q b * ι Q a = ι Q (a + b) * ι Q (a + b) - ι Q a * ι Q a - ι Q b * ι Q b : by { rw [(ι Q).map_add, mul_add, add_mul, add_mul], abel, } ... = algebra_map R _ (Q (a + b)) - algebra_map R _ (Q a) - algebra_map R _ (Q b) : by rw [ι_sq_scalar, ι_sq_scalar, ι_sq_scalar] ... = algebra_map R _ (Q (a + b) - Q a - Q b) : by rw [←ring_hom.map_sub, ←ring_hom.map_sub] ... = algebra_map R _ (quadratic_form.polar Q a b) : rfl section map variables {M₁ M₂ M₃ : Type*} variables [add_comm_group M₁] [add_comm_group M₂] [add_comm_group M₃] variables [module R M₁] [module R M₂] [module R M₃] variables (Q₁ : quadratic_form R M₁) (Q₂ : quadratic_form R M₂) (Q₃ : quadratic_form R M₃) /-- Any linear map that preserves the quadratic form lifts to an `alg_hom` between algebras. See `clifford_algebra.equiv_of_isometry` for the case when `f` is a `quadratic_form.isometry`. -/ def map (f : M₁ →ₗ[R] M₂) (hf : ∀ m, Q₂ (f m) = Q₁ m) : clifford_algebra Q₁ →ₐ[R] clifford_algebra Q₂ := clifford_algebra.lift Q₁ ⟨(clifford_algebra.ι Q₂).comp f, λ m, (ι_sq_scalar _ _).trans $ ring_hom.congr_arg _ $ hf m⟩ @[simp] lemma map_comp_ι (f : M₁ →ₗ[R] M₂) (hf) : (map Q₁ Q₂ f hf).to_linear_map.comp (ι Q₁) = (ι Q₂).comp f := ι_comp_lift _ _ @[simp] lemma map_apply_ι (f : M₁ →ₗ[R] M₂) (hf) (m : M₁): map Q₁ Q₂ f hf (ι Q₁ m) = ι Q₂ (f m) := lift_ι_apply _ _ m @[simp] lemma map_id : map Q₁ Q₁ (linear_map.id : M₁ →ₗ[R] M₁) (λ m, rfl) = alg_hom.id R (clifford_algebra Q₁) := by { ext m, exact map_apply_ι _ _ _ _ m } @[simp] lemma map_comp_map (f : M₂ →ₗ[R] M₃) (hf) (g : M₁ →ₗ[R] M₂) (hg) : (map Q₂ Q₃ f hf).comp (map Q₁ Q₂ g hg) = map Q₁ Q₃ (f.comp g) (λ m, (hf _).trans $ hg m) := begin ext m, dsimp only [linear_map.comp_apply, alg_hom.comp_apply, alg_hom.to_linear_map_apply, alg_hom.id_apply], rw [map_apply_ι, map_apply_ι, map_apply_ι, linear_map.comp_apply], end variables {Q₁ Q₂ Q₃} /-- Two `clifford_algebra`s are equivalent as algebras if their quadratic forms are equivalent. -/ @[simps apply] def equiv_of_isometry (e : Q₁.isometry Q₂) : clifford_algebra Q₁ ≃ₐ[R] clifford_algebra Q₂ := alg_equiv.of_alg_hom (map Q₁ Q₂ e e.map_app) (map Q₂ Q₁ e.symm e.symm.map_app) ((map_comp_map _ _ _ _ _ _ _).trans $ begin convert map_id _ using 2, ext m, exact e.to_linear_equiv.apply_symm_apply m, end) ((map_comp_map _ _ _ _ _ _ _).trans $ begin convert map_id _ using 2, ext m, exact e.to_linear_equiv.symm_apply_apply m, end) @[simp] lemma equiv_of_isometry_symm (e : Q₁.isometry Q₂) : (equiv_of_isometry e).symm = equiv_of_isometry e.symm := rfl @[simp] lemma equiv_of_isometry_trans (e₁₂ : Q₁.isometry Q₂) (e₂₃ : Q₂.isometry Q₃) : (equiv_of_isometry e₁₂).trans (equiv_of_isometry e₂₃) = equiv_of_isometry (e₁₂.trans e₂₃) := by { ext x, exact alg_hom.congr_fun (map_comp_map Q₁ Q₂ Q₃ _ _ _ _) x } @[simp] lemma equiv_of_isometry_refl : (equiv_of_isometry $ quadratic_form.isometry.refl Q₁) = alg_equiv.refl := by { ext x, exact alg_hom.congr_fun (map_id Q₁) x } end map end clifford_algebra namespace tensor_algebra variables {Q} /-- The canonical image of the `tensor_algebra` in the `clifford_algebra`, which maps `tensor_algebra.ι R x` to `clifford_algebra.ι Q x`. -/ def to_clifford : tensor_algebra R M →ₐ[R] clifford_algebra Q := tensor_algebra.lift R (clifford_algebra.ι Q) @[simp] lemma to_clifford_ι (m : M) : (tensor_algebra.ι R m).to_clifford = clifford_algebra.ι Q m := by simp [to_clifford] end tensor_algebra
44ec2fbb82befa55303208dfc7c82e8582195d25
8b209d7bd1995e5795f3538ecb50643f69fb146f
/src/globular/signature.1.lean
6bdfe977e25a569140f5115029f05042677a6a2f
[]
no_license
semorrison/lean-globular
319661251c3c6d0e593d4f81d8a18b1856ffbc20
cf44b96e2921be4efed4e00c412ae6088687e411
refs/heads/master
1,609,627,650,726
1,543,965,455,000
1,543,965,455,000
99,391,087
0
0
null
null
null
null
UTF-8
Lean
false
false
2,670
lean
inductive {u} dependent_list (A : Type u) (f : A → Type u) (g : Π a : A, f a → A) : A → A → Type (u+1) | nil : Π a: A, (dependent_list a a) | cons : Π { a b : A }, Π x : f b, (dependent_list a b) → dependent_list a (g b x) open nat definition divisor_chain : ℕ → ℕ → Type 1 := dependent_list ℕ (λ m: ℕ, { k : ℕ // k ∣ m }) (λ _ n, n) definition divisor_chain.nil : Π n : ℕ, divisor_chain n n := @dependent_list.nil ℕ (λ m: ℕ, { k : ℕ // k ∣ m }) (λ _ n, n) definition divisor_chain.cons : Π {m n : ℕ}, Π k : { k // k ∣ n }, divisor_chain m n → divisor_chain m k := λ m n k c, @dependent_list.cons _ (λ m: ℕ, { k : ℕ // k ∣ m }) _ m n k c open divisor_chain definition example_1 : divisor_chain 10 10 := nil 10 definition example_2 : divisor_chain 10 5 := cons ⟨ 5, begin exact dec_trivial end ⟩ (nil 10) notation a ## b := divisor_chain.cons ⟨ a, by exact dec_trivial ⟩ b definition example_3 := (20 ## (nil 40)) definition example_4 := 5 ## example_3 -- definition example_5 := 5 ## (20 ## (nil 40)) -- In this first section, we define -- * signature_data -- * diagram_data -- * embedding_data -- all parametrised by dummy types, which we'll later specialise recursively into the appropriate types one level down. structure {u} signature_data (Signature : Type (u+1)) (Diagram : Signature → Type (u+1)) := (g : Type u) (σ : Signature) (s t : g → Diagram σ) definition {u} embedding_chain_data (Diagram : Type u) (Rewrite : Diagram → Type u) (result : Π d : Diagram, Rewrite d → Diagram) : Diagram → Diagram → Type (u+1) := dependent_list Diagram (λ d, Rewrite d) result structure {u} diagram_data (Signature : Type (u+1)) (Diagram : Signature → Type (u+1)) (Rewrite : Π σ : Signature, Diagram σ → Type (u+1)) (result : Π σ : Signature, Π d : Diagram σ, Rewrite σ d → Diagram σ) (σ : signature_data Signature Diagram) := (s t : Diagram σ.σ) (δ : embedding_chain_data (Diagram σ.σ) (Rewrite σ.σ) (result σ.σ) s t) structure embedding_data (Diagram Diagram' : Type) (s : Diagram → Diagram') (Embedding: Diagram' → Diagram' → Type) (S T : Diagram) := (h : ℕ) (e : Embedding (s S) (s T)) definition {u} signature_diagram_pair : ℕ → (Σ T : Type (u+1), T → Type (u+1)) | 0 := ⟨ulift unit, λ _, ulift unit⟩ | (n+1) := let ⟨S, D⟩ := signature_diagram_pair n in ⟨signature_data S D, diagram_data n S D⟩ definition {u} signature (n : ℕ) : Type (u+1) := (signature_diagram_pair n).1 definition {u} diagram (n : ℕ) : signature n → Type (u+1) := (signature_diagram_pair n).2
46ae9af89de60a5f59f0fafadcca52e37eccd7bb
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch6/ex0404.lean
9091c2fa48663e74db1eec644eb7e38637f57aa4
[]
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
362
lean
variable {α : Type*} def is_prefix (l₁ : list α) (l₂ : list α) : Prop := ∃ t, l₁ ++ t = l₂ infix ` <+: `:50 := is_prefix section local attribute [simp] theorem list.is_prefix_refl (l : list α) : l <+: l := ⟨[], by simp⟩ example : [1, 2, 3] <+: [1, 2, 3] := by simp end -- error: -- example : [1, 2, 3] <+: [1, 2, 3] := by simp
33fe031f4fa7ca9a2496b861d26a815c20e6ddbe
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/subtype.lean
b730238ea6e1887530873a73dcd9b6295698f6bb
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
4,690
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import tactic.lint -- Lean complains if this section is turned into a namespace open function section subtype variables {α : Sort*} {p : α → Prop} @[simp] theorem subtype.forall {q : {a // p a} → Prop} : (∀ x, q x) ↔ (∀ a b, q ⟨a, b⟩) := ⟨assume h a b, h ⟨a, b⟩, assume h ⟨a, b⟩, h a b⟩ /-- An alternative version of `subtype.forall`. This one is useful if Lean cannot figure out `q` when using `subtype.forall` from right to left. -/ theorem subtype.forall' {q : ∀x, p x → Prop} : (∀ x h, q x h) ↔ (∀ x : {a // p a}, q x.1 x.2) := (@subtype.forall _ _ (λ x, q x.1 x.2)).symm @[simp] theorem subtype.exists {q : {a // p a} → Prop} : (∃ x, q x) ↔ (∃ a b, q ⟨a, b⟩) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ end subtype namespace subtype variables {α : Sort*} {β : Sort*} {γ : Sort*} {p : α → Prop} lemma val_eq_coe : @val _ p = coe := rfl protected lemma eq' : ∀ {a1 a2 : {x // p x}}, a1.val = a2.val → a1 = a2 | ⟨x, h1⟩ ⟨.(x), h2⟩ rfl := rfl lemma ext {a1 a2 : {x // p x}} : a1 = a2 ↔ a1.val = a2.val := ⟨congr_arg _, subtype.eq'⟩ lemma coe_ext {a1 a2 : {x // p x}} : a1 = a2 ↔ (a1 : α) = a2 := ext theorem val_injective : injective (@val _ p) := λ a b, subtype.eq' /-- Restrict a (dependent) function to a subtype -/ def restrict {α} {β : α → Type*} (f : Πx, β x) (p : α → Prop) (x : subtype p) : β x.1 := f x.1 lemma restrict_apply {α} {β : α → Type*} (f : Πx, β x) (p : α → Prop) (x : subtype p) : restrict f p x = f x.1 := by refl lemma restrict_def {α β} (f : α → β) (p : α → Prop) : restrict f p = f ∘ subtype.val := by refl lemma restrict_injective {α β} {f : α → β} (p : α → Prop) (h : injective f) : injective (restrict f p) := injective_comp h subtype.val_injective /-- Defining a map into a subtype, this can be seen as an "coinduction principle" of `subtype`-/ def coind {α β} (f : α → β) {p : β → Prop} (h : ∀a, p (f a)) : α → subtype p := λ a, ⟨f a, h a⟩ theorem coind_injective {α β} {f : α → β} {p : β → Prop} (h : ∀a, p (f a)) (hf : injective f) : injective (coind f h) := λ x y hxy, hf $ by apply congr_arg subtype.val hxy /-- Restriction of a function to a function on subtypes. -/ def map {p : α → Prop} {q : β → Prop} (f : α → β) (h : ∀a, p a → q (f a)) : subtype p → subtype q := λ x, ⟨f x.1, h x.1 x.2⟩ theorem map_comp {p : α → Prop} {q : β → Prop} {r : γ → Prop} {x : subtype p} (f : α → β) (h : ∀a, p a → q (f a)) (g : β → γ) (l : ∀a, q a → r (g a)) : map g l (map f h x) = map (g ∘ f) (assume a ha, l (f a) $ h a ha) x := rfl theorem map_id {p : α → Prop} {h : ∀a, p a → p (id a)} : map (@id α) h = id := funext $ assume ⟨v, h⟩, rfl lemma map_injective {p : α → Prop} {q : β → Prop} {f : α → β} (h : ∀a, p a → q (f a)) (hf : injective f) : injective (map f h) := coind_injective _ $ injective_comp hf val_injective instance [has_equiv α] (p : α → Prop) : has_equiv (subtype p) := ⟨λ s t, s.val ≈ t.val⟩ theorem equiv_iff [has_equiv α] {p : α → Prop} {s t : subtype p} : s ≈ t ↔ s.val ≈ t.val := iff.rfl variables [setoid α] protected theorem refl (s : subtype p) : s ≈ s := setoid.refl s.val protected theorem symm {s t : subtype p} (h : s ≈ t) : t ≈ s := setoid.symm h protected theorem trans {s t u : subtype p} (h₁ : s ≈ t) (h₂ : t ≈ u) : s ≈ u := setoid.trans h₁ h₂ theorem equivalence (p : α → Prop) : equivalence (@has_equiv.equiv (subtype p) _) := mk_equivalence _ subtype.refl (@subtype.symm _ p _) (@subtype.trans _ p _) instance (p : α → Prop) : setoid (subtype p) := setoid.mk (≈) (equivalence p) end subtype namespace subtype variables {α : Type*} {β : Type*} {γ : Type*} {p : α → Prop} @[simp] theorem coe_eta {α : Type*} {p : α → Prop} (a : {a // p a}) (h : p a) : mk ↑a h = a := eta _ _ @[simp] theorem coe_mk {α : Type*} {p : α → Prop} (a h) : (@mk α p a h : α) = a := rfl @[simp, nolint simp_nf] -- built-in reduction doesn't always work theorem mk_eq_mk {α : Type*} {p : α → Prop} {a h a' h'} : @mk α p a h = @mk α p a' h' ↔ a = a' := ⟨λ H, by injection H, λ H, by congr; assumption⟩ @[simp] lemma val_prop {S : set α} (a : {a // a ∈ S}) : a.val ∈ S := a.property @[simp] lemma val_prop' {S : set α} (a : {a // a ∈ S}) : ↑a ∈ S := a.property end subtype
d979a49ab7b81c2151f613a3bf8c9405c47b3bf5
7b89826c26634aa18c0110f1634f73027851edfe
/natural-number-game/src/world02/level06.lean
75714d8b981ecded4a7bc96e1cf863a17035b2f3
[ "MIT" ]
permissive
marcofavorito/leanings
b7642344d8c9012a1cec74a804c5884297880c4d
581b83be66ff4f8dd946fb6a1bb045d2ddf91076
refs/heads/master
1,672,310,991,244
1,603,031,766,000
1,603,031,766,000
279,163,004
1
0
null
null
null
null
UTF-8
Lean
false
false
584
lean
import mynat.definition -- Imports the natural numbers. import mynat.add -- imports addition. import world02.level01 -- hide import world02.level02 -- hide import world02.level03 -- hide import world02.level04 -- hide import world02.level05 -- hide namespace mynat -- hide lemma add_right_comm (a b c : mynat) : a + b + c = a + c + b := begin [nat_num_game] /- induction c with d hd, rw add_zero, rw add_zero, refl, rw add_succ, rw add_succ, rw succ_add, rw hd, refl, -/ rw add_assoc, rw add_comm b c, rw ← add_assoc, refl, end end mynat -- hide
7369046d79477d98f5a20b04e518cadb4dd2f223
e030b0259b777fedcdf73dd966f3f1556d392178
/tests/lean/structure_instance_bug2.lean
9d3c7b2ec757a0326bbf3ea71e64ecfe0b8df4cc
[ "Apache-2.0" ]
permissive
fgdorais/lean
17b46a095b70b21fa0790ce74876658dc5faca06
c3b7c54d7cca7aaa25328f0a5660b6b75fe26055
refs/heads/master
1,611,523,590,686
1,484,412,902,000
1,484,412,902,000
38,489,734
0
0
null
1,435,923,380,000
1,435,923,379,000
null
UTF-8
Lean
false
false
162
lean
def my_pre_config1 : smt_pre_config := { default_smt_pre_config . zeta := tt } def my_pre_config2 : smt_pre_config := { default_smt_pre_config with zeta := tt }
5ce6edab7d71ace6333fe69f78db5317ba3c5a05
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/set/basic.lean
ca7ce4a9550b298445cb6e87a63dcab938b9b25e
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
113,598
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 universes 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 u} : has_coe_to_sort (set α) (Type u) := ⟨λ 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 insert_subset_insert_iff (ha : a ∉ s) : insert a s ⊆ insert a t ↔ s ⊆ t := begin refine ⟨λ h x hx, _, insert_subset_insert⟩, rcases h (subset_insert _ _ hx) with (rfl|hxt), exacts [(ha hx).elim, hxt] end 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 = {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 = {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⟩ lemma exists_eq_singleton_iff_nonempty_unique_mem : (∃ a : α, s = {a}) ↔ (s.nonempty ∧ ∀ a b ∈ s, a = b) := begin refine ⟨_, λ h, _⟩, { rintros ⟨a, rfl⟩, refine ⟨set.singleton_nonempty a, λ b c hb hc, hb.trans hc.symm⟩ }, { obtain ⟨a, ha⟩ := h.1, refine ⟨a, set.eq_singleton_iff_unique_mem.mpr ⟨ha, λ b hb, (h.2 b a hb ha)⟩⟩ }, end -- 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 } lemma sep_inter_sep {p q : α → Prop} : {x ∈ s | p x} ∩ {x ∈ s | q x} = {x ∈ s | p x ∧ q x} := begin ext, simp_rw [mem_inter_iff, mem_sep_iff], rw [and_and_and_comm, and_self], end @[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 subset_union_compl_iff_inter_subset {s t u : set α} : s ⊆ t ∪ uᶜ ↔ s ∩ u ⊆ t := (@is_compl_compl _ u _).le_sup_right_iff_inf_left_le 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_cancel_right 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_sdiff_right ‹s₁ ≤ s₂› theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := sdiff_le_sdiff_left ‹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_univ_subsingleton (h : (univ : set α).subsingleton) : subsingleton α := ⟨λ a b, h (mem_univ a) (mem_univ b)⟩ @[simp] lemma subsingleton_univ_iff : (univ : set α).subsingleton ↔ subsingleton α := ⟨subsingleton_of_univ_subsingleton, λ h, @subsingleton_univ _ h⟩ 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 @[simp] lemma coe_comp_range_factorization (f : ι → β) : coe ∘ range_factorization f = f := 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 right_inverse_range_splitting {f : α → β} (h : injective f) : right_inverse (range_factorization f) (range_splitting f) := (left_inverse_range_splitting f).right_inverse_of_injective $ λ x y hxy, h $ subtype.ext_iff.1 hxy lemma preimage_range_splitting {f : α → β} (hf : injective f) : preimage (range_splitting f) = image (range_factorization f) := (image_eq_preimage_of_inverse (right_inverse_range_splitting hf) (left_inverse_range_splitting f)).symm 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 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 lemma option.injective_iff {α β} {f : option α → β} : injective f ↔ injective (f ∘ some) ∧ f none ∉ range (f ∘ some) := begin simp only [mem_range, not_exists, (∘)], refine ⟨λ hf, ⟨hf.comp (option.some_injective _), λ x, hf.ne $ option.some_ne_none _⟩, _⟩, rintro ⟨h_some, h_none⟩ (_|a) (_|b) hab, exacts [rfl, (h_none _ hab.symm).elim, (h_none _ hab).elim, congr_arg some (h_some hab)] end /-! ### 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
07968963d9b4fbf942872e1bbb1be302b078828e
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/linear_algebra/multilinear.lean
17cca4bf697e2ede825652204dedd582b0f11624
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
46,319
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import linear_algebra.basic import algebra.algebra.basic import tactic.omega import data.fintype.sort /-! # Multilinear maps We define multilinear maps as maps from `Π(i : ι), M₁ i` to `M₂` which are linear in each coordinate. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type (although some statements will require it to be a fintype). This space, denoted by `multilinear_map R M₁ M₂`, inherits a module structure by pointwise addition and multiplication. ## Main definitions * `multilinear_map R M₁ M₂` is the space of multilinear maps from `Π(i : ι), M₁ i` to `M₂`. * `f.map_smul` is the multiplicativity of the multilinear map `f` along each coordinate. * `f.map_add` is the additivity of the multilinear map `f` along each coordinate. * `f.map_smul_univ` expresses the multiplicativity of `f` over all coordinates at the same time, writing `f (λi, c i • m i)` as `(∏ i, c i) • f m`. * `f.map_add_univ` expresses the additivity of `f` over all coordinates at the same time, writing `f (m + m')` as the sum over all subsets `s` of `ι` of `f (s.piecewise m m')`. * `f.map_sum` expresses `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` as the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all possible functions. We also register isomorphisms corresponding to currying or uncurrying variables, transforming a multilinear function `f` on `n+1` variables into a linear function taking values in multilinear functions in `n` variables, and into a multilinear function in `n` variables taking values in linear functions. These operations are called `f.curry_left` and `f.curry_right` respectively (with inverses `f.uncurry_left` and `f.uncurry_right`). These operations induce linear equivalences between spaces of multilinear functions in `n+1` variables and spaces of linear functions into multilinear functions in `n` variables (resp. multilinear functions in `n` variables taking values in linear functions), called respectively `multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`. ## Implementation notes Expressing that a map is linear along the `i`-th coordinate when all other coordinates are fixed can be done in two (equivalent) different ways: * fixing a vector `m : Π(j : ι - i), M₁ j.val`, and then choosing separately the `i`-th coordinate * fixing a vector `m : Πj, M₁ j`, and then modifying its `i`-th coordinate The second way is more artificial as the value of `m` at `i` is not relevant, but it has the advantage of avoiding subtype inclusion issues. This is the definition we use, based on `function.update` that allows to change the value of `m` at `i`. -/ open function fin set open_locale big_operators universes u v v' v₁ v₂ v₃ w u' variables {R : Type u} {ι : Type u'} {n : ℕ} {M : fin n.succ → Type v} {M₁ : ι → Type v₁} {M₂ : Type v₂} {M₃ : Type v₃} {M' : Type v'} [decidable_eq ι] /-- Multilinear maps over the ring `R`, from `Πi, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules over `R`. -/ structure multilinear_map (R : Type u) {ι : Type u'} (M₁ : ι → Type v) (M₂ : Type w) [decidable_eq ι] [semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [∀i, semimodule R (M₁ i)] [semimodule R M₂] := (to_fun : (Πi, M₁ i) → M₂) (map_add' : ∀(m : Πi, M₁ i) (i : ι) (x y : M₁ i), to_fun (update m i (x + y)) = to_fun (update m i x) + to_fun (update m i y)) (map_smul' : ∀(m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i), to_fun (update m i (c • x)) = c • to_fun (update m i x)) namespace multilinear_map section semiring variables [semiring R] [∀i, add_comm_monoid (M i)] [∀i, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M'] [∀i, semimodule R (M i)] [∀i, semimodule R (M₁ i)] [semimodule R M₂] [semimodule R M₃] [semimodule R M'] (f f' : multilinear_map R M₁ M₂) instance : has_coe_to_fun (multilinear_map R M₁ M₂) := ⟨_, to_fun⟩ initialize_simps_projections multilinear_map (to_fun → apply) @[simp] lemma to_fun_eq_coe : f.to_fun = f := rfl @[simp] lemma coe_mk (f : (Π i, M₁ i) → M₂) (h₁ h₂ ) : ⇑(⟨f, h₁, h₂⟩ : multilinear_map R M₁ M₂) = f := rfl theorem congr_fun {f g : multilinear_map R M₁ M₂} (h : f = g) (x : Π i, M₁ i) : f x = g x := congr_arg (λ h : multilinear_map R M₁ M₂, h x) h theorem congr_arg (f : multilinear_map R M₁ M₂) {x y : Π i, M₁ i} (h : x = y) : f x = f y := congr_arg (λ x : Π i, M₁ i, f x) h theorem coe_inj ⦃f g : multilinear_map R M₁ M₂⦄ (h : ⇑f = g) : f = g := by cases f; cases g; cases h; refl @[ext] theorem ext {f f' : multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' := coe_inj (funext H) theorem ext_iff {f g : multilinear_map R M₁ M₂} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ @[simp] lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.map_add' m i x y @[simp] lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update m i (c • x)) = c • f (update m i x) := f.map_smul' m i c x lemma map_coord_zero {m : Πi, M₁ i} (i : ι) (h : m i = 0) : f m = 0 := begin have : (0 : R) • (0 : M₁ i) = 0, by simp, rw [← update_eq_self i m, h, ← this, f.map_smul, zero_smul] end @[simp] lemma map_update_zero (m : Πi, M₁ i) (i : ι) : f (update m i 0) = 0 := f.map_coord_zero i (update_same i 0 m) @[simp] lemma map_zero [nonempty ι] : f 0 = 0 := begin obtain ⟨i, _⟩ : ∃i:ι, i ∈ set.univ := set.exists_mem_of_nonempty ι, exact map_coord_zero f i rfl end instance : has_add (multilinear_map R M₁ M₂) := ⟨λf f', ⟨λx, f x + f' x, λm i x y, by simp [add_left_comm, add_assoc], λm i c x, by simp [smul_add]⟩⟩ @[simp] lemma add_apply (m : Πi, M₁ i) : (f + f') m = f m + f' m := rfl instance : has_zero (multilinear_map R M₁ M₂) := ⟨⟨λ _, 0, λm i x y, by simp, λm i c x, by simp⟩⟩ instance : inhabited (multilinear_map R M₁ M₂) := ⟨0⟩ @[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : multilinear_map R M₁ M₂) m = 0 := rfl instance : add_comm_monoid (multilinear_map R M₁ M₂) := by refine {zero := 0, add := (+), ..}; intros; ext; simp [add_comm, add_left_comm] @[simp] lemma sum_apply {α : Type*} (f : α → multilinear_map R M₁ M₂) (m : Πi, M₁ i) : ∀ {s : finset α}, (∑ a in s, f a) m = ∑ a in s, f a m := begin classical, apply finset.induction, { rw finset.sum_empty, simp }, { assume a s has H, rw finset.sum_insert has, simp [H, has] } end /-- If `f` is a multilinear map, then `f.to_linear_map m i` is the linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ def to_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →ₗ[R] M₂ := { to_fun := λx, f (update m i x), map_add' := λx y, by simp, map_smul' := λc x, by simp } /-- The cartesian product of two multilinear maps, as a multilinear map. -/ def prod (f : multilinear_map R M₁ M₂) (g : multilinear_map R M₁ M₃) : multilinear_map R M₁ (M₂ × M₃) := { to_fun := λ m, (f m, g m), map_add' := λ m i x y, by simp, map_smul' := λ m i c x, by simp } /-- Given a multilinear map `f` on `n` variables (parameterized by `fin n`) and a subset `s` of `k` of these variables, one gets a new multilinear map on `fin k` by varying these variables, and fixing the other ones equal to a given value `z`. It is denoted by `f.restr s hk z`, where `hk` is a proof that the cardinality of `s` is `k`. The implicit identification between `fin k` and `s` that we use is the canonical (increasing) bijection. -/ def restr {k n : ℕ} (f : multilinear_map R (λ i : fin n, M') M₂) (s : finset (fin n)) (hk : s.card = k) (z : M') : multilinear_map R (λ i : fin k, M') M₂ := { to_fun := λ v, f (λ j, if h : j ∈ s then v ((s.order_iso_of_fin hk).symm ⟨j, h⟩) else z), map_add' := λ v i x y, by { erw [dite_comp_equiv_update, dite_comp_equiv_update, dite_comp_equiv_update], simp }, map_smul' := λ v i c x, by { erw [dite_comp_equiv_update, dite_comp_equiv_update], simp } } variable {R} /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma cons_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (x y : M 0) : f (cons (x+y) m) = f (cons x m) + f (cons y m) := by rw [← update_cons_zero x m (x+y), f.map_add, update_cons_zero, update_cons_zero] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma cons_smul (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (c : R) (x : M 0) : f (cons (c • x) m) = c • f (cons x m) := by rw [← update_cons_zero x m (c • x), f.map_smul, update_cons_zero] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `snoc`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma snoc_add (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x y : M (last n)) : f (snoc m (x+y)) = f (snoc m x) + f (snoc m y) := by rw [← update_snoc_last x m (x+y), f.map_add, update_snoc_last, update_snoc_last] /-- In the specific case of multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma snoc_smul (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (c : R) (x : M (last n)) : f (snoc m (c • x)) = c • f (snoc m x) := by rw [← update_snoc_last x m (c • x), f.map_smul, update_snoc_last] section variables {M₁' : ι → Type*} [Π i, add_comm_monoid (M₁' i)] [Π i, semimodule R (M₁' i)] /-- If `g` is a multilinear map and `f` is a collection of linear maps, then `g (f₁ m₁, ..., fₙ mₙ)` is again a multilinear map, that we call `g.comp_linear_map f`. -/ def comp_linear_map (g : multilinear_map R M₁' M₂) (f : Π i, M₁ i →ₗ[R] M₁' i) : multilinear_map R M₁ M₂ := { to_fun := λ m, g $ λ i, f i (m i), map_add' := λ m i x y, have ∀ j z, f j (update m i z j) = update (λ k, f k (m k)) i (f i z) j := λ j z, function.apply_update (λ k, f k) _ _ _ _, by simp [this], map_smul' := λ m i c x, have ∀ j z, f j (update m i z j) = update (λ k, f k (m k)) i (f i z) j := λ j z, function.apply_update (λ k, f k) _ _ _ _, by simp [this] } @[simp] lemma comp_linear_map_apply (g : multilinear_map R M₁' M₂) (f : Π i, M₁ i →ₗ[R] M₁' i) (m : Π i, M₁ i) : g.comp_linear_map f m = g (λ i, f i (m i)) := rfl end /-- If one adds to a vector `m'` another vector `m`, but only for coordinates in a finset `t`, then the image under a multilinear map `f` is the sum of `f (s.piecewise m m')` along all subsets `s` of `t`. This is mainly an auxiliary statement to prove the result when `t = univ`, given in `map_add_univ`, although it can be useful in its own right as it does not require the index set `ι` to be finite.-/ lemma map_piecewise_add (m m' : Πi, M₁ i) (t : finset ι) : f (t.piecewise (m + m') m') = ∑ s in t.powerset, f (s.piecewise m m') := begin revert m', refine finset.induction_on t (by simp) _, assume i t hit Hrec m', have A : (insert i t).piecewise (m + m') m' = update (t.piecewise (m + m') m') i (m i + m' i) := t.piecewise_insert _ _ _, have B : update (t.piecewise (m + m') m') i (m' i) = t.piecewise (m + m') m', { ext j, by_cases h : j = i, { rw h, simp [hit] }, { simp [h] } }, let m'' := update m' i (m i), have C : update (t.piecewise (m + m') m') i (m i) = t.piecewise (m + m'') m'', { ext j, by_cases h : j = i, { rw h, simp [m'', hit] }, { by_cases h' : j ∈ t; simp [h, hit, m'', h'] } }, rw [A, f.map_add, B, C, finset.sum_powerset_insert hit, Hrec, Hrec, add_comm], congr' 1, apply finset.sum_congr rfl (λs hs, _), have : (insert i s).piecewise m m' = s.piecewise m m'', { ext j, by_cases h : j = i, { rw h, simp [m'', finset.not_mem_of_mem_powerset_of_not_mem hs hit] }, { by_cases h' : j ∈ s; simp [h, m'', h'] } }, rw this end /-- Additivity of a multilinear map along all coordinates at the same time, writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/ lemma map_add_univ [fintype ι] (m m' : Πi, M₁ i) : f (m + m') = ∑ s : finset ι, f (s.piecewise m m') := by simpa using f.map_piecewise_add m m' finset.univ section apply_sum variables {α : ι → Type*} (g : Π i, α i → M₁ i) (A : Π i, finset (α i)) open_locale classical open fintype finset /-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. Here, we give an auxiliary statement tailored for an inductive proof. Use instead `map_sum_finset`. -/ lemma map_sum_finset_aux [fintype ι] {n : ℕ} (h : ∑ i, (A i).card = n) : f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) := begin induction n using nat.strong_induction_on with n IH generalizing A, -- If one of the sets is empty, then all the sums are zero by_cases Ai_empty : ∃ i, A i = ∅, { rcases Ai_empty with ⟨i, hi⟩, have : ∑ j in A i, g i j = 0, by convert sum_empty, rw f.map_coord_zero i this, have : pi_finset A = ∅, { apply finset.eq_empty_of_forall_not_mem (λ r hr, _), have : r i ∈ A i := mem_pi_finset.mp hr i, rwa hi at this }, convert sum_empty.symm }, push_neg at Ai_empty, -- Otherwise, if all sets are at most singletons, then they are exactly singletons and the result -- is again straightforward by_cases Ai_singleton : ∀ i, (A i).card ≤ 1, { have Ai_card : ∀ i, (A i).card = 1, { assume i, have : finset.card (A i) ≠ 0, by simp [finset.card_eq_zero, Ai_empty i], have : finset.card (A i) ≤ 1 := Ai_singleton i, omega }, have : ∀ (r : Π i, α i), r ∈ pi_finset A → f (λ i, g i (r i)) = f (λ i, ∑ j in A i, g i j), { assume r hr, unfold_coes, congr' with i, have : ∀ j ∈ A i, g i j = g i (r i), { assume j hj, congr, apply finset.card_le_one_iff.1 (Ai_singleton i) hj, exact mem_pi_finset.mp hr i }, simp only [finset.sum_congr rfl this, finset.mem_univ, finset.sum_const, Ai_card i, one_nsmul] }, simp only [sum_congr rfl this, Ai_card, card_pi_finset, prod_const_one, one_nsmul, sum_const] }, -- Remains the interesting case where one of the `A i`, say `A i₀`, has cardinality at least 2. -- We will split into two parts `B i₀` and `C i₀` of smaller cardinality, let `B i = C i = A i` -- for `i ≠ i₀`, apply the inductive assumption to `B` and `C`, and add up the corresponding -- parts to get the sum for `A`. push_neg at Ai_singleton, obtain ⟨i₀, hi₀⟩ : ∃ i, 1 < (A i).card := Ai_singleton, obtain ⟨j₁, j₂, hj₁, hj₂, j₁_ne_j₂⟩ : ∃ j₁ j₂, (j₁ ∈ A i₀) ∧ (j₂ ∈ A i₀) ∧ j₁ ≠ j₂ := finset.one_lt_card_iff.1 hi₀, let B := function.update A i₀ (A i₀ \ {j₂}), let C := function.update A i₀ {j₂}, have B_subset_A : ∀ i, B i ⊆ A i, { assume i, by_cases hi : i = i₀, { rw hi, simp only [B, sdiff_subset, update_same]}, { simp only [hi, B, update_noteq, ne.def, not_false_iff, finset.subset.refl] } }, have C_subset_A : ∀ i, C i ⊆ A i, { assume i, by_cases hi : i = i₀, { rw hi, simp only [C, hj₂, finset.singleton_subset_iff, update_same] }, { simp only [hi, C, update_noteq, ne.def, not_false_iff, finset.subset.refl] } }, -- split the sum at `i₀` as the sum over `B i₀` plus the sum over `C i₀`, to use additivity. have A_eq_BC : (λ i, ∑ j in A i, g i j) = function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in B i₀, g i₀ j + ∑ j in C i₀, g i₀ j), { ext i, by_cases hi : i = i₀, { rw [hi], simp only [function.update_same], have : A i₀ = B i₀ ∪ C i₀, { simp only [B, C, function.update_same, finset.sdiff_union_self_eq_union], symmetry, simp only [hj₂, finset.singleton_subset_iff, union_eq_left_iff_subset] }, rw this, apply finset.sum_union, apply finset.disjoint_right.2 (λ j hj, _), have : j = j₂, by { dsimp [C] at hj, simpa using hj }, rw this, dsimp [B], simp only [mem_sdiff, eq_self_iff_true, not_true, not_false_iff, finset.mem_singleton, update_same, and_false] }, { simp [hi] } }, have Beq : function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in B i₀, g i₀ j) = (λ i, ∑ j in B i, g i j), { ext i, by_cases hi : i = i₀, { rw hi, simp only [update_same] }, { simp only [hi, B, update_noteq, ne.def, not_false_iff] } }, have Ceq : function.update (λ i, ∑ j in A i, g i j) i₀ (∑ j in C i₀, g i₀ j) = (λ i, ∑ j in C i, g i j), { ext i, by_cases hi : i = i₀, { rw hi, simp only [update_same] }, { simp only [hi, C, update_noteq, ne.def, not_false_iff] } }, -- Express the inductive assumption for `B` have Brec : f (λ i, ∑ j in B i, g i j) = ∑ r in pi_finset B, f (λ i, g i (r i)), { have : ∑ i, finset.card (B i) < ∑ i, finset.card (A i), { refine finset.sum_lt_sum (λ i hi, finset.card_le_of_subset (B_subset_A i)) ⟨i₀, finset.mem_univ _, _⟩, have : {j₂} ⊆ A i₀, by simp [hj₂], simp only [B, finset.card_sdiff this, function.update_same, finset.card_singleton], exact nat.pred_lt (ne_of_gt (lt_trans nat.zero_lt_one hi₀)) }, rw h at this, exact IH _ this B rfl }, -- Express the inductive assumption for `C` have Crec : f (λ i, ∑ j in C i, g i j) = ∑ r in pi_finset C, f (λ i, g i (r i)), { have : ∑ i, finset.card (C i) < ∑ i, finset.card (A i) := finset.sum_lt_sum (λ i hi, finset.card_le_of_subset (C_subset_A i)) ⟨i₀, finset.mem_univ _, by simp [C, hi₀]⟩, rw h at this, exact IH _ this C rfl }, have D : disjoint (pi_finset B) (pi_finset C), { have : disjoint (B i₀) (C i₀), by simp [B, C], exact pi_finset_disjoint_of_disjoint B C this }, have pi_BC : pi_finset A = pi_finset B ∪ pi_finset C, { apply finset.subset.antisymm, { assume r hr, by_cases hri₀ : r i₀ = j₂, { apply finset.mem_union_right, apply mem_pi_finset.2 (λ i, _), by_cases hi : i = i₀, { have : r i₀ ∈ C i₀, by simp [C, hri₀], convert this }, { simp [C, hi, mem_pi_finset.1 hr i] } }, { apply finset.mem_union_left, apply mem_pi_finset.2 (λ i, _), by_cases hi : i = i₀, { have : r i₀ ∈ B i₀, by simp [B, hri₀, mem_pi_finset.1 hr i₀], convert this }, { simp [B, hi, mem_pi_finset.1 hr i] } } }, { exact finset.union_subset (pi_finset_subset _ _ (λ i, B_subset_A i)) (pi_finset_subset _ _ (λ i, C_subset_A i)) } }, rw A_eq_BC, simp only [multilinear_map.map_add, Beq, Ceq, Brec, Crec, pi_BC], rw ← finset.sum_union D, end /-- If `f` is multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum_finset [fintype ι] : f (λ i, ∑ j in A i, g i j) = ∑ r in pi_finset A, f (λ i, g i (r i)) := f.map_sum_finset_aux _ _ rfl /-- If `f` is multilinear, then `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions `r`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum [fintype ι] [∀ i, fintype (α i)] : f (λ i, ∑ j, g i j) = ∑ r : Π i, α i, f (λ i, g i (r i)) := f.map_sum_finset g (λ i, finset.univ) lemma map_update_sum {α : Type*} (t : finset α) (i : ι) (g : α → M₁ i) (m : Π i, M₁ i): f (update m i (∑ a in t, g a)) = ∑ a in t, f (update m i (g a)) := begin induction t using finset.induction with a t has ih h, { simp }, { simp [finset.sum_insert has, ih] } end end apply_sum section restrict_scalar variables (R) {A : Type*} [semiring A] [has_scalar R A] [Π (i : ι), semimodule A (M₁ i)] [semimodule A M₂] [∀ i, is_scalar_tower R A (M₁ i)] [is_scalar_tower R A M₂] /-- Reinterpret an `A`-multilinear map as an `R`-multilinear map, if `A` is an algebra over `R` and their actions on all involved semimodules agree with the action of `R` on `A`. -/ def restrict_scalars (f : multilinear_map A M₁ M₂) : multilinear_map R M₁ M₂ := { to_fun := f, map_add' := f.map_add, map_smul' := λ m i, (f.to_linear_map m i).map_smul_of_tower } @[simp] lemma coe_restrict_scalars (f : multilinear_map A M₁ M₂) : ⇑(f.restrict_scalars R) = f := rfl end restrict_scalar section variables {ι₁ ι₂ ι₃ : Type*} [decidable_eq ι₁] [decidable_eq ι₂] [decidable_eq ι₃] /-- Transfer the arguments to a map along an equivalence between argument indices. The naming is derived from `finsupp.dom_congr`, noting that here the permutation applies to the domain of the domain. -/ @[simps apply] def dom_dom_congr (σ : ι₁ ≃ ι₂) (m : multilinear_map R (λ i : ι₁, M₂) M₃) : multilinear_map R (λ i : ι₂, M₂) M₃ := { to_fun := λ v, m (λ i, v (σ i)), map_add' := λ v i a b, by { simp_rw function.update_apply_equiv_apply v, rw m.map_add, }, map_smul' := λ v i a b, by { simp_rw function.update_apply_equiv_apply v, rw m.map_smul, }, } lemma dom_dom_congr_trans (σ₁ : ι₁ ≃ ι₂) (σ₂ : ι₂ ≃ ι₃) (m : multilinear_map R (λ i : ι₁, M₂) M₃) : m.dom_dom_congr (σ₁.trans σ₂) = (m.dom_dom_congr σ₁).dom_dom_congr σ₂ := rfl lemma dom_dom_congr_mul (σ₁ : equiv.perm ι₁) (σ₂ : equiv.perm ι₁) (m : multilinear_map R (λ i : ι₁, M₂) M₃) : m.dom_dom_congr (σ₂ * σ₁) = (m.dom_dom_congr σ₁).dom_dom_congr σ₂ := rfl /-- `multilinear_map.dom_dom_congr` as an equivalence. This is declared separately because it does not work with dot notation. -/ @[simps apply symm_apply] def dom_dom_congr_equiv (σ : ι₁ ≃ ι₂) : multilinear_map R (λ i : ι₁, M₂) M₃ ≃+ multilinear_map R (λ i : ι₂, M₂) M₃ := { to_fun := dom_dom_congr σ, inv_fun := dom_dom_congr σ.symm, left_inv := λ m, by {ext, simp}, right_inv := λ m, by {ext, simp}, map_add' := λ a b, by {ext, simp} } end end semiring end multilinear_map namespace linear_map variables [semiring R] [Πi, add_comm_monoid (M₁ i)] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M'] [∀i, semimodule R (M₁ i)] [semimodule R M₂] [semimodule R M₃] [semimodule R M'] /-- Composing a multilinear map with a linear map gives again a multilinear map. -/ def comp_multilinear_map (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) : multilinear_map R M₁ M₃ := { to_fun := g ∘ f, map_add' := λ m i x y, by simp, map_smul' := λ m i c x, by simp } @[simp] lemma coe_comp_multilinear_map (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) : ⇑(g.comp_multilinear_map f) = g ∘ f := rfl lemma comp_multilinear_map_apply (g : M₂ →ₗ[R] M₃) (f : multilinear_map R M₁ M₂) (m : Π i, M₁ i) : g.comp_multilinear_map f m = g (f m) := rfl variables {ι₁ ι₂ : Type*} [decidable_eq ι₁] [decidable_eq ι₂] @[simp] lemma comp_multilinear_map_dom_dom_congr (σ : ι₁ ≃ ι₂) (g : M₂ →ₗ[R] M₃) (f : multilinear_map R (λ i : ι₁, M') M₂) : (g.comp_multilinear_map f).dom_dom_congr σ = g.comp_multilinear_map (f.dom_dom_congr σ) := by { ext, simp } end linear_map namespace multilinear_map section comm_semiring variables [comm_semiring R] [∀i, add_comm_monoid (M₁ i)] [∀i, add_comm_monoid (M i)] [add_comm_monoid M₂] [∀i, semimodule R (M i)] [∀i, semimodule R (M₁ i)] [semimodule R M₂] (f f' : multilinear_map R M₁ M₂) /-- If one multiplies by `c i` the coordinates in a finset `s`, then the image under a multilinear map is multiplied by `∏ i in s, c i`. This is mainly an auxiliary statement to prove the result when `s = univ`, given in `map_smul_univ`, although it can be useful in its own right as it does not require the index set `ι` to be finite. -/ lemma map_piecewise_smul (c : ι → R) (m : Πi, M₁ i) (s : finset ι) : f (s.piecewise (λi, c i • m i) m) = (∏ i in s, c i) • f m := begin refine s.induction_on (by simp) _, assume j s j_not_mem_s Hrec, have A : function.update (s.piecewise (λi, c i • m i) m) j (m j) = s.piecewise (λi, c i • m i) m, { ext i, by_cases h : i = j, { rw h, simp [j_not_mem_s] }, { simp [h] } }, rw [s.piecewise_insert, f.map_smul, A, Hrec], simp [j_not_mem_s, mul_smul] end /-- Multiplicativity of a multilinear map along all coordinates at the same time, writing `f (λi, c i • m i)` as `(∏ i, c i) • f m`. -/ lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) : f (λi, c i • m i) = (∏ i, c i) • f m := by simpa using map_piecewise_smul f c m finset.univ section distrib_mul_action variables {R' A : Type*} [monoid R'] [semiring A] [Π i, semimodule A (M₁ i)] [distrib_mul_action R' M₂] [semimodule A M₂] [smul_comm_class A R' M₂] instance : has_scalar R' (multilinear_map A M₁ M₂) := ⟨λ c f, ⟨λ m, c • f m, λm i x y, by simp [smul_add], λl i x d, by simp [←smul_comm x c] ⟩⟩ @[simp] lemma smul_apply (f : multilinear_map A M₁ M₂) (c : R') (m : Πi, M₁ i) : (c • f) m = c • f m := rfl instance : distrib_mul_action R' (multilinear_map A M₁ M₂) := { one_smul := λ f, ext $ λ x, one_smul _ _, mul_smul := λ c₁ c₂ f, ext $ λ x, mul_smul _ _ _, smul_zero := λ r, ext $ λ x, smul_zero _, smul_add := λ r f₁ f₂, ext $ λ x, smul_add _ _ _ } end distrib_mul_action section semimodule variables {R' A : Type*} [semiring R'] [semiring A] [Π i, semimodule A (M₁ i)] [semimodule R' M₂] [semimodule A M₂] [smul_comm_class A R' M₂] /-- The space of multilinear maps over an algebra over `R` is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance : semimodule R' (multilinear_map A M₁ M₂) := { add_smul := λ r₁ r₂ f, ext $ λ x, add_smul _ _ _, zero_smul := λ f, ext $ λ x, zero_smul _ _ } end semimodule section dom_coprod open_locale tensor_product variables {ι₁ ι₂ ι₃ ι₄ : Type*} variables [decidable_eq ι₁] [decidable_eq ι₂][decidable_eq ι₃] [decidable_eq ι₄] variables {N₁ : Type*} [add_comm_monoid N₁] [semimodule R N₁] variables {N₂ : Type*} [add_comm_monoid N₂] [semimodule R N₂] variables {N : Type*} [add_comm_monoid N] [semimodule R N] /-- Given two multilinear maps `(ι₁ → N) → N₁` and `(ι₂ → N) → N₂`, this produces the map `(ι₁ ⊕ ι₂ → N) → N₁ ⊗ N₂` by taking the coproduct of the domain and the tensor product of the codomain. This can be thought of as combining `equiv.sum_arrow_equiv_prod_arrow.symm` with `tensor_product.map`, noting that the two operations can't be separated as the intermediate result is not a `multilinear_map`. While this can be generalized to work for dependent `Π i : ι₁, N'₁ i` instead of `ι₁ → N`, doing so introduces `sum.elim N'₁ N'₂` types in the result which are difficult to work with and not defeq to the simple case defined here. See [this zulip thread]( https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there.20code.20for.20X.3F/topic/Instances.20on.20.60sum.2Eelim.20A.20B.20i.60/near/218484619). -/ @[simps apply] def dom_coprod (a : multilinear_map R (λ _ : ι₁, N) N₁) (b : multilinear_map R (λ _ : ι₂, N) N₂) : multilinear_map R (λ _ : ι₁ ⊕ ι₂, N) (N₁ ⊗[R] N₂) := have inl_disjoint : ∀ {α β : Type*} (a : α), (sum.inl a : α ⊕ β) ∉ set.range (@sum.inr α β) := by simp, have inr_disjoint : ∀ {α β : Type*} (b : β), (sum.inr b : α ⊕ β) ∉ set.range (@sum.inl α β) := by simp, { to_fun := λ v, a (λ i, v (sum.inl i)) ⊗ₜ b (λ i, v (sum.inr i)), map_add' := λ v i p q, begin cases i, { iterate 3 { rw [function.update_comp_eq_of_injective' _ sum.injective_inl, function.update_comp_eq_of_not_mem_range' _ _ (inl_disjoint i)],}, rw [a.map_add, tensor_product.add_tmul], }, { iterate 3 { rw [function.update_comp_eq_of_injective' _ sum.injective_inr, function.update_comp_eq_of_not_mem_range' _ _ (inr_disjoint i)],}, rw [b.map_add, tensor_product.tmul_add], } end, map_smul' := λ v i c p, begin cases i, { iterate 2 { rw [function.update_comp_eq_of_injective' _ sum.injective_inl, function.update_comp_eq_of_not_mem_range' _ _ (inl_disjoint i)],}, rw [a.map_smul, tensor_product.smul_tmul'], }, { iterate 2 { rw [function.update_comp_eq_of_injective' _ sum.injective_inr, function.update_comp_eq_of_not_mem_range' _ _ (inr_disjoint i)]}, rw [b.map_smul, tensor_product.tmul_smul], }, end } /-- A more bundled version of `multilinear_map.dom_coprod` that maps `((ι₁ → N) → N₁) ⊗ ((ι₂ → N) → N₂)` to `(ι₁ ⊕ ι₂ → N) → N₁ ⊗ N₂`. -/ def dom_coprod' : multilinear_map R (λ _ : ι₁, N) N₁ ⊗[R] multilinear_map R (λ _ : ι₂, N) N₂ →ₗ[R] multilinear_map R (λ _ : ι₁ ⊕ ι₂, N) (N₁ ⊗[R] N₂) := tensor_product.lift $ linear_map.mk₂ R (dom_coprod) (λ m₁ m₂ n, by { ext, simp only [dom_coprod_apply, tensor_product.add_tmul, add_apply] }) (λ c m n, by { ext, simp only [dom_coprod_apply, tensor_product.smul_tmul', smul_apply] }) (λ m n₁ n₂, by { ext, simp only [dom_coprod_apply, tensor_product.tmul_add, add_apply] }) (λ c m n, by { ext, simp only [dom_coprod_apply, tensor_product.tmul_smul, smul_apply] }) @[simp] lemma dom_coprod'_apply (a : multilinear_map R (λ _ : ι₁, N) N₁) (b : multilinear_map R (λ _ : ι₂, N) N₂) : dom_coprod' (a ⊗ₜ[R] b) = dom_coprod a b := rfl /-- When passed an `equiv.sum_congr`, `multilinear_map.dom_dom_congr` distributes over `multilinear_map.dom_coprod`. -/ lemma dom_coprod_dom_dom_congr_sum_congr (a : multilinear_map R (λ _ : ι₁, N) N₁) (b : multilinear_map R (λ _ : ι₂, N) N₂) (σa : ι₁ ≃ ι₃) (σb : ι₂ ≃ ι₄) : (a.dom_coprod b).dom_dom_congr (σa.sum_congr σb) = (a.dom_dom_congr σa).dom_coprod (b.dom_dom_congr σb) := rfl end dom_coprod section variables (R ι) (A : Type*) [comm_semiring A] [algebra R A] [fintype ι] /-- Given an `R`-algebra `A`, `mk_pi_algebra` is the multilinear map on `A^ι` associating to `m` the product of all the `m i`. See also `multilinear_map.mk_pi_algebra_fin` for a version that works with a non-commutative algebra `A` but requires `ι = fin n`. -/ protected def mk_pi_algebra : multilinear_map R (λ i : ι, A) A := { to_fun := λ m, ∏ i, m i, map_add' := λ m i x y, by simp [finset.prod_update_of_mem, add_mul], map_smul' := λ m i c x, by simp [finset.prod_update_of_mem] } variables {R A ι} @[simp] lemma mk_pi_algebra_apply (m : ι → A) : multilinear_map.mk_pi_algebra R ι A m = ∏ i, m i := rfl end section variables (R n) (A : Type*) [semiring A] [algebra R A] /-- Given an `R`-algebra `A`, `mk_pi_algebra_fin` is the multilinear map on `A^n` associating to `m` the product of all the `m i`. See also `multilinear_map.mk_pi_algebra` for a version that assumes `[comm_semiring A]` but works for `A^ι` with any finite type `ι`. -/ protected def mk_pi_algebra_fin : multilinear_map R (λ i : fin n, A) A := { to_fun := λ m, (list.of_fn m).prod, map_add' := begin intros m i x y, have : (list.fin_range n).index_of i < n, by simpa using list.index_of_lt_length.2 (list.mem_fin_range i), simp [list.of_fn_eq_map, (list.nodup_fin_range n).map_update, list.prod_update_nth, add_mul, this, mul_add, add_mul] end, map_smul' := begin intros m i c x, have : (list.fin_range n).index_of i < n, by simpa using list.index_of_lt_length.2 (list.mem_fin_range i), simp [list.of_fn_eq_map, (list.nodup_fin_range n).map_update, list.prod_update_nth, this] end } variables {R A n} @[simp] lemma mk_pi_algebra_fin_apply (m : fin n → A) : multilinear_map.mk_pi_algebra_fin R n A m = (list.of_fn m).prod := rfl lemma mk_pi_algebra_fin_apply_const (a : A) : multilinear_map.mk_pi_algebra_fin R n A (λ _, a) = a ^ n := by simp end /-- Given an `R`-multilinear map `f` taking values in `R`, `f.smul_right z` is the map sending `m` to `f m • z`. -/ def smul_right (f : multilinear_map R M₁ R) (z : M₂) : multilinear_map R M₁ M₂ := (linear_map.smul_right linear_map.id z).comp_multilinear_map f @[simp] lemma smul_right_apply (f : multilinear_map R M₁ R) (z : M₂) (m : Π i, M₁ i) : f.smul_right z m = f m • z := rfl variables (R ι) /-- The canonical multilinear map on `R^ι` when `ι` is finite, associating to `m` the product of all the `m i` (multiplied by a fixed reference element `z` in the target module). See also `mk_pi_algebra` for a more general version. -/ protected def mk_pi_ring [fintype ι] (z : M₂) : multilinear_map R (λ(i : ι), R) M₂ := (multilinear_map.mk_pi_algebra R ι R).smul_right z variables {R ι} @[simp] lemma mk_pi_ring_apply [fintype ι] (z : M₂) (m : ι → R) : (multilinear_map.mk_pi_ring R ι z : (ι → R) → M₂) m = (∏ i, m i) • z := rfl lemma mk_pi_ring_apply_one_eq_self [fintype ι] (f : multilinear_map R (λ(i : ι), R) M₂) : multilinear_map.mk_pi_ring R ι (f (λi, 1)) = f := begin ext m, have : m = (λi, m i • 1), by { ext j, simp }, conv_rhs { rw [this, f.map_smul_univ] }, refl end end comm_semiring section range_add_comm_group variables [semiring R] [∀i, add_comm_monoid (M₁ i)] [add_comm_group M₂] [∀i, semimodule R (M₁ i)] [semimodule R M₂] (f g : multilinear_map R M₁ M₂) instance : has_neg (multilinear_map R M₁ M₂) := ⟨λ f, ⟨λ m, - f m, λm i x y, by simp [add_comm], λm i c x, by simp⟩⟩ @[simp] lemma neg_apply (m : Πi, M₁ i) : (-f) m = - (f m) := rfl instance : has_sub (multilinear_map R M₁ M₂) := ⟨λ f g, ⟨λ m, f m - g m, λ m i x y, by { simp only [map_add, sub_eq_add_neg, neg_add], cc }, λ m i c x, by { simp only [map_smul, smul_sub] }⟩⟩ @[simp] lemma sub_apply (m : Πi, M₁ i) : (f - g) m = f m - g m := rfl instance : add_comm_group (multilinear_map R M₁ M₂) := by refine { zero := 0, add := (+), neg := has_neg.neg, sub := has_sub.sub, sub_eq_add_neg := _, .. }; intros; ext; simp [add_comm, add_left_comm, sub_eq_add_neg] end range_add_comm_group section add_comm_group variables [semiring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, semimodule R (M₁ i)] [semimodule R M₂] (f : multilinear_map R M₁ M₂) @[simp] lemma map_neg (m : Πi, M₁ i) (i : ι) (x : M₁ i) : f (update m i (-x)) = -f (update m i x) := eq_neg_of_add_eq_zero $ by rw [←map_add, add_left_neg, f.map_coord_zero i (update_same i 0 m)] @[simp] lemma map_sub (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x - y)) = f (update m i x) - f (update m i y) := by rw [sub_eq_add_neg, sub_eq_add_neg, map_add, map_neg] end add_comm_group section comm_semiring variables [comm_semiring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, semimodule R (M₁ i)] [semimodule R M₂] /-- When `ι` is finite, multilinear maps on `R^ι` with values in `M₂` are in bijection with `M₂`, as such a multilinear map is completely determined by its value on the constant vector made of ones. We register this bijection as a linear equivalence in `multilinear_map.pi_ring_equiv`. -/ protected def pi_ring_equiv [fintype ι] : M₂ ≃ₗ[R] (multilinear_map R (λ(i : ι), R) M₂) := { to_fun := λ z, multilinear_map.mk_pi_ring R ι z, inv_fun := λ f, f (λi, 1), map_add' := λ z z', by { ext m, simp [smul_add] }, map_smul' := λ c z, by { ext m, simp [smul_smul, mul_comm] }, left_inv := λ z, by simp, right_inv := λ f, f.mk_pi_ring_apply_one_eq_self } end comm_semiring end multilinear_map section currying /-! ### Currying We associate to a multilinear map in `n+1` variables (i.e., based on `fin n.succ`) two curried functions, named `f.curry_left` (which is a linear map on `E 0` taking values in multilinear maps in `n` variables) and `f.curry_right` (wich is a multilinear map in `n` variables taking values in linear maps on `E 0`). In both constructions, the variable that is singled out is `0`, to take advantage of the operations `cons` and `tail` on `fin n`. The inverse operations are called `uncurry_left` and `uncurry_right`. We also register linear equiv versions of these correspondences, in `multilinear_curry_left_equiv` and `multilinear_curry_right_equiv`. -/ open multilinear_map variables {R M M₂} [comm_ring R] [∀i, add_comm_group (M i)] [add_comm_group M'] [add_comm_group M₂] [∀i, module R (M i)] [module R M'] [module R M₂] /-! #### Left currying -/ /-- Given a linear map `f` from `M 0` to multilinear maps on `n` variables, construct the corresponding multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (m 0) (tail m)`-/ def linear_map.uncurry_left (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) : multilinear_map R M M₂ := { to_fun := λm, f (m 0) (tail m), map_add' := λm i x y, begin by_cases h : i = 0, { revert x y, rw h, assume x y, rw [update_same, update_same, update_same, f.map_add, add_apply, tail_update_zero, tail_update_zero, tail_update_zero] }, { rw [update_noteq (ne.symm h), update_noteq (ne.symm h), update_noteq (ne.symm h)], revert x y, rw ← succ_pred i h, assume x y, rw [tail_update_succ, map_add, tail_update_succ, tail_update_succ] } end, map_smul' := λm i c x, begin by_cases h : i = 0, { revert x, rw h, assume x, rw [update_same, update_same, tail_update_zero, tail_update_zero, ← smul_apply, f.map_smul] }, { rw [update_noteq (ne.symm h), update_noteq (ne.symm h)], revert x, rw ← succ_pred i h, assume x, rw [tail_update_succ, tail_update_succ, map_smul] } end } @[simp] lemma linear_map.uncurry_left_apply (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) (m : Πi, M i) : f.uncurry_left m = f (m 0) (tail m) := rfl /-- Given a multilinear map `f` in `n+1` variables, split the first variable to obtain a linear map into multilinear maps in `n` variables, given by `x ↦ (m ↦ f (cons x m))`. -/ def multilinear_map.curry_left (f : multilinear_map R M M₂) : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂) := { to_fun := λx, { to_fun := λm, f (cons x m), map_add' := λm i y y', by simp, map_smul' := λm i y c, by simp }, map_add' := λx y, by { ext m, exact cons_add f m x y }, map_smul' := λc x, by { ext m, exact cons_smul f m c x } } @[simp] lemma multilinear_map.curry_left_apply (f : multilinear_map R M M₂) (x : M 0) (m : Π(i : fin n), M i.succ) : f.curry_left x m = f (cons x m) := rfl @[simp] lemma linear_map.curry_uncurry_left (f : M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) : f.uncurry_left.curry_left = f := begin ext m x, simp only [tail_cons, linear_map.uncurry_left_apply, multilinear_map.curry_left_apply], rw cons_zero end @[simp] lemma multilinear_map.uncurry_curry_left (f : multilinear_map R M M₂) : f.curry_left.uncurry_left = f := by { ext m, simp } variables (R M M₂) /-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to the space of linear maps from `M 0` to the space of multilinear maps on `Π(i : fin n), M i.succ `, by separating the first variable. We register this isomorphism as a linear isomorphism in `multilinear_curry_left_equiv R M M₂`. The direct and inverse maps are given by `f.uncurry_left` and `f.curry_left`. Use these unless you need the full framework of linear equivs. -/ def multilinear_curry_left_equiv : (M 0 →ₗ[R] (multilinear_map R (λ(i : fin n), M i.succ) M₂)) ≃ₗ[R] (multilinear_map R M M₂) := { to_fun := linear_map.uncurry_left, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, refl }, inv_fun := multilinear_map.curry_left, left_inv := linear_map.curry_uncurry_left, right_inv := multilinear_map.uncurry_curry_left } variables {R M M₂} /-! #### Right currying -/ /-- Given a multilinear map `f` in `n` variables to the space of linear maps from `M (last n)` to `M₂`, construct the corresponding multilinear map on `n+1` variables obtained by concatenating the variables, given by `m ↦ f (init m) (m (last n))`-/ def multilinear_map.uncurry_right (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) (M (last n) →ₗ[R] M₂))) : multilinear_map R M M₂ := { to_fun := λm, f (init m) (m (last n)), map_add' := λm i x y, begin by_cases h : i.val < n, { have : last n ≠ i := ne.symm (ne_of_lt h), rw [update_noteq this, update_noteq this, update_noteq this], revert x y, rw [(cast_succ_cast_lt i h).symm], assume x y, rw [init_update_cast_succ, map_add, init_update_cast_succ, init_update_cast_succ, linear_map.add_apply] }, { revert x y, rw eq_last_of_not_lt h, assume x y, rw [init_update_last, init_update_last, init_update_last, update_same, update_same, update_same, linear_map.map_add] } end, map_smul' := λm i c x, begin by_cases h : i.val < n, { have : last n ≠ i := ne.symm (ne_of_lt h), rw [update_noteq this, update_noteq this], revert x, rw [(cast_succ_cast_lt i h).symm], assume x, rw [init_update_cast_succ, init_update_cast_succ, map_smul, linear_map.smul_apply] }, { revert x, rw eq_last_of_not_lt h, assume x, rw [update_same, update_same, init_update_last, init_update_last, linear_map.map_smul] } end } @[simp] lemma multilinear_map.uncurry_right_apply (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) (m : Πi, M i) : f.uncurry_right m = f (init m) (m (last n)) := rfl /-- Given a multilinear map `f` in `n+1` variables, split the last variable to obtain a multilinear map in `n` variables taking values in linear maps from `M (last n)` to `M₂`, given by `m ↦ (x ↦ f (snoc m x))`. -/ def multilinear_map.curry_right (f : multilinear_map R M M₂) : multilinear_map R (λ(i : fin n), M (fin.cast_succ i)) ((M (last n)) →ₗ[R] M₂) := { to_fun := λm, { to_fun := λx, f (snoc m x), map_add' := λx y, by rw f.snoc_add, map_smul' := λc x, by rw f.snoc_smul }, map_add' := λm i x y, begin ext z, change f (snoc (update m i (x + y)) z) = f (snoc (update m i x) z) + f (snoc (update m i y) z), rw [snoc_update, snoc_update, snoc_update, f.map_add] end, map_smul' := λm i c x, begin ext z, change f (snoc (update m i (c • x)) z) = c • f (snoc (update m i x) z), rw [snoc_update, snoc_update, f.map_smul] end } @[simp] lemma multilinear_map.curry_right_apply (f : multilinear_map R M M₂) (m : Π(i : fin n), M i.cast_succ) (x : M (last n)) : f.curry_right m x = f (snoc m x) := rfl @[simp] lemma multilinear_map.curry_uncurry_right (f : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂))) : f.uncurry_right.curry_right = f := begin ext m x, simp only [snoc_last, multilinear_map.curry_right_apply, multilinear_map.uncurry_right_apply], rw init_snoc end @[simp] lemma multilinear_map.uncurry_curry_right (f : multilinear_map R M M₂) : f.curry_right.uncurry_right = f := by { ext m, simp } variables (R M M₂) /-- The space of multilinear maps on `Π(i : fin (n+1)), M i` is canonically isomorphic to the space of linear maps from the space of multilinear maps on `Π(i : fin n), M i.cast_succ` to the space of linear maps on `M (last n)`, by separating the last variable. We register this isomorphism as a linear isomorphism in `multilinear_curry_right_equiv R M M₂`. The direct and inverse maps are given by `f.uncurry_right` and `f.curry_right`. Use these unless you need the full framework of linear equivs. -/ def multilinear_curry_right_equiv : (multilinear_map R (λ(i : fin n), M i.cast_succ) ((M (last n)) →ₗ[R] M₂)) ≃ₗ[R] (multilinear_map R M M₂) := { to_fun := multilinear_map.uncurry_right, map_add' := λf₁ f₂, by { ext m, refl }, map_smul' := λc f, by { ext m, rw [smul_apply], refl }, inv_fun := multilinear_map.curry_right, left_inv := multilinear_map.curry_uncurry_right, right_inv := multilinear_map.uncurry_curry_right } end currying
cfdde6002e5aea33f57b9fe1a524598ff932c2e6
a4673261e60b025e2c8c825dfa4ab9108246c32e
/stage0/src/Lean/ReducibilityAttrs.lean
463986651a1387c59521a0092674dc22b181f9b1
[ "Apache-2.0" ]
permissive
jcommelin/lean4
c02dec0cc32c4bccab009285475f265f17d73228
2909313475588cc20ac0436e55548a4502050d0a
refs/heads/master
1,674,129,550,893
1,606,415,348,000
1,606,415,348,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,940
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Attributes namespace Lean inductive ReducibilityStatus := | reducible | semireducible | irreducible instance : Inhabited ReducibilityStatus := ⟨ReducibilityStatus.semireducible⟩ builtin_initialize reducibilityAttrs : EnumAttributes ReducibilityStatus ← registerEnumAttributes `reducibility [(`reducible, "reducible", ReducibilityStatus.reducible), (`semireducible, "semireducible", ReducibilityStatus.semireducible), (`irreducible, "irreducible", ReducibilityStatus.irreducible)] @[export lean_get_reducibility_status] def getReducibilityStatusImp (env : Environment) (declName : Name) : ReducibilityStatus := match reducibilityAttrs.getValue env declName with | some s => s | none => ReducibilityStatus.semireducible @[export lean_set_reducibility_status] def setReducibilityStatusImp (env : Environment) (declName : Name) (s : ReducibilityStatus) : Environment := match reducibilityAttrs.setValue env declName s with | Except.ok env => env | _ => env -- TODO(Leo): we should extend EnumAttributes.setValue in the future and ensure it never fails def getReducibilityStatus {m} [Monad m] [MonadEnv m] (declName : Name) : m ReducibilityStatus := do return getReducibilityStatusImp (← getEnv) declName def setReducibilityStatus {m} [Monad m] [MonadEnv m] (declName : Name) (s : ReducibilityStatus) : m Unit := do modifyEnv fun env => setReducibilityStatusImp env declName s def setReducibleAttribute {m} [Monad m] [MonadEnv m] (declName : Name) : m Unit := do setReducibilityStatus declName ReducibilityStatus.reducible def isReducible {m} [Monad m] [MonadEnv m] (declName : Name) : m Bool := do match ← getReducibilityStatus declName with | ReducibilityStatus.reducible => true | _ => false end Lean
a5ddbdc3b75cbd365161ecb084954e601280b04a
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/tactic/doc_commands.lean
c539d9823f661a33d075e88b2ab19fe5eb5f514e
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
15,658
lean
/- Copyright (c) 2020 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import tactic.fix_reflect_string /-! # Documentation commands We generate html documentation from mathlib. It is convenient to collect lists of tactics, commands, notes, etc. To facilitate this, we declare these documentation entries in the library using special commands. * `library_note` adds a note describing a certain feature or design decision. These can be referenced in doc strings with the text `note [name of note]`. * `add_tactic_doc` adds an entry documenting an interactive tactic, command, hole command, or attribute. Since these commands are used in files imported by `tactic.core`, this file has no imports. ## Implementation details `library_note note_id note_msg` creates a declaration `` `library_note.i `` for some `i`. This declaration is a pair of strings `note_id` and `note_msg`, and it gets tagged with the `library_note` attribute. Similarly, `add_tactic_doc` creates a declaration `` `tactic_doc.i `` that stores the provided information. -/ /-- A rudimentary hash function on strings. -/ def string.hash (s : string) : ℕ := s.fold 1 (λ h c, (33*h + c.val) % unsigned_sz) /-- `mk_hashed_name nspace id` hashes the string `id` to a value `i` and returns the name `nspace._i` -/ meta def string.mk_hashed_name (nspace : name) (id : string) : name := nspace <.> ("_" ++ to_string id.hash) /-! ### The `library_note` command -/ /-- A user attribute `library_note` for tagging decls of type `string × string` for use in note output. -/ @[user_attribute] meta def library_note_attr : user_attribute := { name := `library_note, descr := "Notes about library features to be included in documentation" } open tactic /-- `mk_reflected_definition name val` constructs a definition declaration by reflection. Example: ``mk_reflected_definition `foo 17`` constructs the definition declaration corresponding to `def foo : ℕ := 17` -/ meta def mk_reflected_definition (decl_name : name) {type} [reflected type] (body : type) [reflected body] : declaration := mk_definition decl_name (reflect type).collect_univ_params (reflect type) (reflect body) /-- If `note_name` and `note` are `pexpr`s representing strings, `add_library_note note_name note` adds a declaration of type `string × string` and tags it with the `library_note` attribute. -/ meta def tactic.add_library_note (note_name note : string) : tactic unit := do let decl_name := note_name.mk_hashed_name `library_note, add_decl $ mk_reflected_definition decl_name (note_name, note), library_note_attr.set decl_name () tt none /-- `tactic.eval_pexpr e α` evaluates the pre-expression `e` to a VM object of type `α`. -/ meta def tactic.eval_pexpr (α) [reflected α] (e : pexpr) : tactic α := to_expr ``(%%e : %%(reflect α)) ff ff >>= eval_expr α open tactic lean lean.parser interactive /-- A command to add library notes. Syntax: ``` /-- note message -/ library_note "note id" ``` --- At various places in mathlib, we leave implementation notes that are referenced from many other files. To keep track of these notes, we use the command `library_note`. This makes it easy to retrieve a list of all notes, e.g. for documentation output. These notes can be referenced in mathlib with the syntax `Note [note id]`. Often, these references will be made in code comments (`--`) that won't be displayed in docs. If such a reference is made in a doc string or module doc, it will be linked to the corresponding note in the doc display. Syntax: ``` /-- note message -/ library_note "note id" ``` An example from `meta.expr`: ``` /-- Some declarations work with open expressions, i.e. an expr that has free variables. Terms will free variables are not well-typed, and one should not use them in tactics like `infer_type` or `unify`. You can still do syntactic analysis/manipulation on them. The reason for working with open types is for performance: instantiating variables requires iterating through the expression. In one performance test `pi_binders` was more than 6x quicker than `mk_local_pis` (when applied to the type of all imported declarations 100x). -/ library_note "open expressions" ``` This note can be referenced near a usage of `pi_binders`: ``` -- See Note [open expressions] /-- behavior of f -/ def f := pi_binders ... ``` -/ @[user_command] meta def library_note (mi : interactive.decl_meta_info) (_ : parse (tk "library_note")) : parser unit := do note_name ← parser.pexpr, note_name ← eval_pexpr string note_name, some doc_string ← pure mi.doc_string | fail "library_note requires a doc string", add_library_note note_name doc_string /-- Collects all notes in the current environment. Returns a list of pairs `(note_id, note_content)` -/ meta def tactic.get_library_notes : tactic (list (string × string)) := attribute.get_instances `library_note >>= list.mmap (λ dcl, mk_const dcl >>= eval_expr (string × string)) /-! ### The `add_tactic_doc_entry` command -/ /-- The categories of tactic doc entry. -/ @[derive [decidable_eq, has_reflect]] inductive doc_category | tactic | cmd | hole_cmd | attr /-- Format a `doc_category` -/ meta def doc_category.to_string : doc_category → string | doc_category.tactic := "tactic" | doc_category.cmd := "command" | doc_category.hole_cmd := "hole_command" | doc_category.attr := "attribute" meta instance : has_to_format doc_category := ⟨↑doc_category.to_string⟩ /-- The information used to generate a tactic doc entry -/ @[derive has_reflect] structure tactic_doc_entry := (name : string) (category : doc_category) (decl_names : list _root_.name) (tags : list string := []) (description : string := "") (inherit_description_from : option _root_.name := none) /-- format a `tactic_doc_entry` -/ meta def tactic_doc_entry.to_string : tactic_doc_entry → string | ⟨name, category, decl_names, tags, description, _⟩ := let decl_names := decl_names.map (repr ∘ to_string), tags := tags.map repr in "{" ++ to_string (format!"\"name\": {repr name}, \"category\": \"{category}\", \"decl_names\":{decl_names}, \"tags\": {tags}, \"description\": {repr description}") ++ "}" meta instance : has_to_string tactic_doc_entry := ⟨tactic_doc_entry.to_string⟩ /-- `update_description_from tde inh_id` replaces the `description` field of `tde` with the doc string of the declaration named `inh_id`. -/ meta def tactic_doc_entry.update_description_from (tde : tactic_doc_entry) (inh_id : name) : tactic tactic_doc_entry := do ds ← doc_string inh_id <|> fail (to_string inh_id ++ " has no doc string"), return { description := ds .. tde } /-- `update_description tde` replaces the `description` field of `tde` with: * the doc string of `tde.inherit_description_from`, if this field has a value * the doc string of the entry in `tde.decl_names`, if this field has length 1 If neither of these conditions are met, it returns `tde`. -/ meta def tactic_doc_entry.update_description (tde : tactic_doc_entry) : tactic tactic_doc_entry := match tde.inherit_description_from, tde.decl_names with | some inh_id, _ := tde.update_description_from inh_id | none, [inh_id] := tde.update_description_from inh_id | none, _ := return tde end /-- A user attribute `tactic_doc` for tagging decls of type `tactic_doc_entry` for use in doc output -/ @[user_attribute] meta def tactic_doc_entry_attr : user_attribute := { name := `tactic_doc, descr := "Information about a tactic to be included in documentation" } /-- Collects everything in the environment tagged with the attribute `tactic_doc`. -/ meta def tactic.get_tactic_doc_entries : tactic (list tactic_doc_entry) := attribute.get_instances `tactic_doc >>= list.mmap (λ dcl, mk_const dcl >>= eval_expr tactic_doc_entry) /-- `add_tactic_doc tde` adds a declaration to the environment with `tde` as its body and tags it with the `tactic_doc` attribute. If `tde.decl_names` has exactly one entry `` `decl`` and if `tde.description` is the empty string, `add_tactic_doc` uses the doc string of `decl` as the description. -/ meta def tactic.add_tactic_doc (tde : tactic_doc_entry) : tactic unit := do when (tde.description = "" ∧ tde.inherit_description_from.is_none ∧ tde.decl_names.length ≠ 1) $ fail "A tactic doc entry must contain either a description or a declaration to inherit a description from", tde ← if tde.description = "" then tde.update_description else return tde, let decl_name := (tde.name ++ tde.category.to_string).mk_hashed_name `tactic_doc, add_decl $ mk_definition decl_name [] `(tactic_doc_entry) (reflect tde), tactic_doc_entry_attr.set decl_name () tt none /-- A command used to add documentation for a tactic, command, hole command, or attribute. Usage: after defining an interactive tactic, command, or attribute, add its documentation as follows. ```lean /-- describe what the command does here -/ add_tactic_doc { name := "display name of the tactic", category := cat, decl_names := [`dcl_1, `dcl_2], tags := ["tag_1", "tag_2"] } ``` The argument to `add_tactic_doc` is a structure of type `tactic_doc_entry`. * `name` refers to the display name of the tactic; it is used as the header of the doc entry. * `cat` refers to the category of doc entry. Options: `doc_category.tactic`, `doc_category.cmd`, `doc_category.hole_cmd`, `doc_category.attr` * `decl_names` is a list of the declarations associated with this doc. For instance, the entry for `linarith` would set ``decl_names := [`tactic.interactive.linarith]``. Some entries may cover multiple declarations. It is only necessary to list the interactive versions of tactics. * `tags` is an optional list of strings used to categorize entries. * The doc string is the body of the entry. It can be formatted with markdown. What you are reading now is the description of `add_tactic_doc`. If only one related declaration is listed in `decl_names` and if this invocation of `add_tactic_doc` does not have a doc string, the doc string of that declaration will become the body of the tactic doc entry. If there are multiple declarations, you can select the one to be used by passing a name to the `inherit_description_from` field. If you prefer a tactic to have a doc string that is different then the doc entry, then between the `/--` `-/` markers, write the desired doc string first, then `---` surrounded by new lines, and then the doc entry. Note that providing a badly formed `tactic_doc_entry` to the command can result in strange error messages. -/ @[user_command] meta def add_tactic_doc_command (mi : interactive.decl_meta_info) (_ : parse $ tk "add_tactic_doc") : parser unit := do pe ← parser.pexpr, e ← eval_pexpr tactic_doc_entry pe, let e : tactic_doc_entry := match mi.doc_string with | some desc := { description := desc, ..e } | none := e end, tactic.add_tactic_doc e . add_tactic_doc { name := "library_note", category := doc_category.cmd, decl_names := [`library_note, `tactic.add_library_note], tags := ["documentation"], inherit_description_from := `library_note } add_tactic_doc { name := "add_tactic_doc", category := doc_category.cmd, decl_names := [`add_tactic_doc_command, `tactic.add_tactic_doc], tags := ["documentation"], inherit_description_from := `add_tactic_doc_command } -- add docs to core tactics /-- The congruence closure tactic `cc` tries to solve the goal by chaining equalities from context and applying congruence (i.e. if `a = b`, then `f a = f b`). It is a finishing tactic, i.e. it is meant to close the current goal, not to make some inconclusive progress. A mostly trivial example would be: ```lean example (a b c : ℕ) (f : ℕ → ℕ) (h: a = b) (h' : b = c) : f a = f c := by cc ``` As an example requiring some thinking to do by hand, consider: ```lean example (f : ℕ → ℕ) (x : ℕ) (H1 : f (f (f x)) = x) (H2 : f (f (f (f (f x)))) = x) : f x = x := by cc ``` The tactic works by building an equality matching graph. It's a graph where the vertices are terms and they are linked by edges if they are known to be equal. Once you've added all the equalities in your context, you take the transitive closure of the graph and, for each connected component (i.e. equivalence class) you can elect a term that will represent the whole class and store proofs that the other elements are equal to it. You then take the transitive closure of these equalities under the congruence lemmas. The `cc` implementation in Lean does a few more tricks: for example it derives `a=b` from `nat.succ a = nat.succ b`, and `nat.succ a != nat.zero` for any `a`. * The starting reference point is Nelson, Oppen, [Fast decision procedures based on congruence closure](http://www.cs.colorado.edu/~bec/courses/csci5535-s09/reading/nelson-oppen-congruence.pdf), Journal of the ACM (1980) * The congruence lemmas for dependent type theory as used in Lean are described in [Congruence closure in intensional type theory](https://leanprover.github.io/papers/congr.pdf) (de Moura, Selsam IJCAR 2016). -/ add_tactic_doc { name := "cc (congruence closure)", category := doc_category.tactic, decl_names := [`tactic.interactive.cc], tags := ["core", "finishing"] } /-- `conv {...}` allows the user to perform targeted rewriting on a goal or hypothesis, by focusing on particular subexpressions. See <https://leanprover-community.github.io/mathlib_docs/conv.html> for more details. Inside `conv` blocks, mathlib currently additionally provides * `erw`, * `ring` and `ring2`, * `norm_num`, * `norm_cast`, * `apply_congr`, and * `conv` (within another `conv`). `apply_congr` applies congruence lemmas to step further inside expressions, and sometimes gives between results than the automatically generated congruence lemmas used by `congr`. Using `conv` inside a `conv` block allows the user to return to the previous state of the outer `conv` block after it is finished. Thus you can continue editing an expression without having to start a new `conv` block and re-scoping everything. For example: ```lean example (a b c d : ℕ) (h₁ : b = c) (h₂ : a + c = a + d) : a + b = a + d := by conv { to_lhs, conv { congr, skip, rw h₁, }, rw h₂, } ``` Without `conv`, the above example would need to be proved using two successive `conv` blocks, each beginning with `to_lhs`. Also, as a shorthand, `conv_lhs` and `conv_rhs` are provided, so that ```lean example : 0 + 0 = 0 := begin conv_lhs { simp } end ``` just means ```lean example : 0 + 0 = 0 := begin conv { to_lhs, simp } end ``` and likewise for `to_rhs`. -/ add_tactic_doc { name := "conv", category := doc_category.tactic, decl_names := [`tactic.interactive.conv], tags := ["core"] } add_tactic_doc { name := "simp", category := doc_category.tactic, decl_names := [`tactic.interactive.simp], tags := ["core", "simplification"] } /-- The `add_decl_doc` command is used to add a doc string to an existing declaration. ```lean def foo := 5 /-- Doc string for foo. -/ add_decl_doc foo ``` -/ @[user_command] meta def add_decl_doc_command (mi : interactive.decl_meta_info) (_ : parse $ tk "add_decl_doc") : parser unit := do n ← parser.ident, n ← resolve_constant n, some doc ← pure mi.doc_string | fail "add_decl_doc requires a doc string", add_doc_string n doc add_tactic_doc { name := "add_decl_doc", category := doc_category.cmd, decl_names := [``add_decl_doc_command], tags := ["documentation"] }
93aa347b3f23c6a78c086884c63be341d51c0437
17d3111f5ed342ec13f42572761b13d569787376
/src/carddisjointunion.lean
26102e7317140c28f6bfb708212c201d98066768
[ "Apache-2.0" ]
permissive
cfbolz/lean-carddisjointunion
6dda1c277ffdf3a5f57417bcb1df3c4a1e1e8f33
33b9013419de7eb9d62a40bf132537dfa224fd0f
refs/heads/master
1,668,609,073,581
1,594,319,171,000
1,594,319,171,000
278,421,864
0
0
null
null
null
null
UTF-8
Lean
false
false
2,260
lean
import tactic import tactic.suggest import tactic.nth_rewrite import data.fintype.basic import data.setoid.partition variables {α : Type} {β : Type} {γ : Type} open_locale classical open_locale big_operators lemma lift_disjoint_to_finset (s1 s2 : set α) [fintype α] (h : disjoint s1 s2) : disjoint s1.to_finset s2.to_finset := begin intros a hinter, have hset : a ∈ ∅, { rw ←set.bot_eq_empty, rw ←le_bot_iff.mp h, apply (set.mem_inter_iff a s1 s2).mpr , split, exact set.mem_to_finset.mp (finset.mem_of_mem_inter_left hinter), exact set.mem_to_finset.mp (finset.mem_of_mem_inter_right hinter), }, exfalso, apply set.not_mem_empty a hset, end lemma to_finset_bind_eq_univ_of_partition {c : set (set α)} [fintype α] (h : setoid.is_partition c) : (set.to_finset c).bind(λ (x : set α), x.to_finset) = finset.univ := begin ext, split, { simp, }, { intro ha, rw finset.mem_bind, cases h.2 a with s hs, simp at hs, use s, rcases hs with ⟨⟨hmemsc, hmemas⟩, _⟩, split, { exact set.mem_to_finset.mpr hmemsc, }, { exact set.mem_to_finset.mpr hmemas, } } end lemma sum_card_partition {c : set (set α)} [fintype α] (h : setoid.is_partition c): fintype.card α = ∑ x in (set.to_finset c), (set.to_finset x).card := begin /- proof idea: |α| = ∑ x in α, 1 = ∑ x in (⋃ s in c, s), 1 = ∑ s in c, (∑ x in s, 1) = ∑ s in c, |s| -/ conv begin to_rhs, congr, skip, funext, rw finset.card_eq_sum_ones, end, have hdisjoint : ∀ (x : set α), x ∈ c.to_finset → ∀ (y : set α), y ∈ c.to_finset → x ≠ y → disjoint x.to_finset y.to_finset, { intros x hx y hy hne, apply lift_disjoint_to_finset, exact setoid.is_partition.pairwise_disjoint h x (set.mem_to_finset.mp hx) y (set.mem_to_finset.mp hy) hne, }, rw ← finset.sum_bind hdisjoint, rw ←finset.card_eq_sum_ones, rw to_finset_bind_eq_univ_of_partition h, exact finset.card_univ.symm, end
0d75cf0a499d32215bb5a30c3866b94bd649ed8d
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/order/pointwise.lean
157edcb4fbafd1f293208783122e29a0602f1775
[ "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
7,324
lean
/- Copyright (c) 2021 Alex J. Best. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alex J. Best, Yaël Dillies -/ import algebra.bounds /-! # Pointwise operations on ordered algebraic objects This file contains lemmas about the effect of pointwise operations on sets with an order structure. ## TODO `Sup (s • t) = Sup s • Sup t` and `Inf (s • t) = Inf s • Inf t` hold as well but `covariant_class` is currently not polymorphic enough to state it. -/ open function set open_locale pointwise variables {α : Type*} section conditionally_complete_lattice variables [conditionally_complete_lattice α] section has_one variables [has_one α] @[simp, to_additive] lemma cSup_one : Sup (1 : set α) = 1 := cSup_singleton _ @[simp, to_additive] lemma cInf_one : Inf (1 : set α) = 1 := cInf_singleton _ end has_one section group variables [group α] [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] {s t : set α} @[to_additive] lemma cSup_inv (hs₀ : s.nonempty) (hs₁ : bdd_below s) : Sup s⁻¹ = (Inf s)⁻¹ := by { rw ←image_inv, exact ((order_iso.inv α).map_cInf' hs₀ hs₁).symm } @[to_additive] lemma cInf_inv (hs₀ : s.nonempty) (hs₁ : bdd_above s) : Inf s⁻¹ = (Sup s)⁻¹ := by { rw ←image_inv, exact ((order_iso.inv α).map_cSup' hs₀ hs₁).symm } @[to_additive] lemma cSup_mul (hs₀ : s.nonempty) (hs₁ : bdd_above s) (ht₀ : t.nonempty) (ht₁ : bdd_above t) : Sup (s * t) = Sup s * Sup t := cSup_image2_eq_cSup_cSup (λ _, (order_iso.mul_right _).to_galois_connection) (λ _, (order_iso.mul_left _).to_galois_connection) hs₀ hs₁ ht₀ ht₁ @[to_additive] lemma cInf_mul (hs₀ : s.nonempty) (hs₁ : bdd_below s) (ht₀ : t.nonempty) (ht₁ : bdd_below t) : Inf (s * t) = Inf s * Inf t := cInf_image2_eq_cInf_cInf (λ _, (order_iso.mul_right _).symm.to_galois_connection) (λ _, (order_iso.mul_left _).symm.to_galois_connection) hs₀ hs₁ ht₀ ht₁ @[to_additive] lemma cSup_div (hs₀ : s.nonempty) (hs₁ : bdd_above s) (ht₀ : t.nonempty) (ht₁ : bdd_below t) : Sup (s / t) = Sup s / Inf t := by rw [div_eq_mul_inv, cSup_mul hs₀ hs₁ ht₀.inv ht₁.inv, cSup_inv ht₀ ht₁, div_eq_mul_inv] @[to_additive] lemma cInf_div (hs₀ : s.nonempty) (hs₁ : bdd_below s) (ht₀ : t.nonempty) (ht₁ : bdd_above t) : Inf (s / t) = Inf s / Sup t := by rw [div_eq_mul_inv, cInf_mul hs₀ hs₁ ht₀.inv ht₁.inv, cInf_inv ht₀ ht₁, div_eq_mul_inv] end group end conditionally_complete_lattice section complete_lattice variables [complete_lattice α] section has_one variables [has_one α] @[simp, to_additive] lemma Sup_one : Sup (1 : set α) = 1 := Sup_singleton @[simp, to_additive] lemma Inf_one : Inf (1 : set α) = 1 := Inf_singleton end has_one section group variables [group α] [covariant_class α α (*) (≤)] [covariant_class α α (swap (*)) (≤)] (s t : set α) @[to_additive] lemma Sup_inv (s : set α) : Sup s⁻¹ = (Inf s)⁻¹ := by { rw [←image_inv, Sup_image], exact ((order_iso.inv α).map_Inf _).symm } @[to_additive] lemma Inf_inv (s : set α) : Inf s⁻¹ = (Sup s)⁻¹ := by { rw [←image_inv, Inf_image], exact ((order_iso.inv α).map_Sup _).symm } @[to_additive] lemma Sup_mul : Sup (s * t) = Sup s * Sup t := Sup_image2_eq_Sup_Sup (λ _, (order_iso.mul_right _).to_galois_connection) $ λ _, (order_iso.mul_left _).to_galois_connection @[to_additive] lemma Inf_mul : Inf (s * t) = Inf s * Inf t := Inf_image2_eq_Inf_Inf (λ _, (order_iso.mul_right _).symm.to_galois_connection) $ λ _, (order_iso.mul_left _).symm.to_galois_connection @[to_additive] lemma Sup_div : Sup (s / t) = Sup s / Inf t := by simp_rw [div_eq_mul_inv, Sup_mul, Sup_inv] @[to_additive] lemma Inf_div : Inf (s / t) = Inf s / Sup t := by simp_rw [div_eq_mul_inv, Inf_mul, Inf_inv] end group end complete_lattice namespace linear_ordered_field variables {K : Type*} [linear_ordered_field K] {a b r : K} (hr : 0 < r) open set include hr lemma smul_Ioo : r • Ioo a b = Ioo (r • a) (r • b) := begin ext x, simp only [mem_smul_set, smul_eq_mul, mem_Ioo], split, { rintro ⟨a, ⟨a_h_left_left, a_h_left_right⟩, rfl⟩, split, exact (mul_lt_mul_left hr).mpr a_h_left_left, exact (mul_lt_mul_left hr).mpr a_h_left_right, }, { rintro ⟨a_left, a_right⟩, use x / r, refine ⟨⟨(lt_div_iff' hr).mpr a_left, (div_lt_iff' hr).mpr a_right⟩, _⟩, rw mul_div_cancel' _ (ne_of_gt hr), } end lemma smul_Icc : r • Icc a b = Icc (r • a) (r • b) := begin ext x, simp only [mem_smul_set, smul_eq_mul, mem_Icc], split, { rintro ⟨a, ⟨a_h_left_left, a_h_left_right⟩, rfl⟩, split, exact (mul_le_mul_left hr).mpr a_h_left_left, exact (mul_le_mul_left hr).mpr a_h_left_right, }, { rintro ⟨a_left, a_right⟩, use x / r, refine ⟨⟨(le_div_iff' hr).mpr a_left, (div_le_iff' hr).mpr a_right⟩, _⟩, rw mul_div_cancel' _ (ne_of_gt hr), } end lemma smul_Ico : r • Ico a b = Ico (r • a) (r • b) := begin ext x, simp only [mem_smul_set, smul_eq_mul, mem_Ico], split, { rintro ⟨a, ⟨a_h_left_left, a_h_left_right⟩, rfl⟩, split, exact (mul_le_mul_left hr).mpr a_h_left_left, exact (mul_lt_mul_left hr).mpr a_h_left_right, }, { rintro ⟨a_left, a_right⟩, use x / r, refine ⟨⟨(le_div_iff' hr).mpr a_left, (div_lt_iff' hr).mpr a_right⟩, _⟩, rw mul_div_cancel' _ (ne_of_gt hr), } end lemma smul_Ioc : r • Ioc a b = Ioc (r • a) (r • b) := begin ext x, simp only [mem_smul_set, smul_eq_mul, mem_Ioc], split, { rintro ⟨a, ⟨a_h_left_left, a_h_left_right⟩, rfl⟩, split, exact (mul_lt_mul_left hr).mpr a_h_left_left, exact (mul_le_mul_left hr).mpr a_h_left_right, }, { rintro ⟨a_left, a_right⟩, use x / r, refine ⟨⟨(lt_div_iff' hr).mpr a_left, (div_le_iff' hr).mpr a_right⟩, _⟩, rw mul_div_cancel' _ (ne_of_gt hr), } end lemma smul_Ioi : r • Ioi a = Ioi (r • a) := begin ext x, simp only [mem_smul_set, smul_eq_mul, mem_Ioi], split, { rintro ⟨a_w, a_h_left, rfl⟩, exact (mul_lt_mul_left hr).mpr a_h_left, }, { rintro h, use x / r, split, exact (lt_div_iff' hr).mpr h, exact mul_div_cancel' _ (ne_of_gt hr), } end lemma smul_Iio : r • Iio a = Iio (r • a) := begin ext x, simp only [mem_smul_set, smul_eq_mul, mem_Iio], split, { rintro ⟨a_w, a_h_left, rfl⟩, exact (mul_lt_mul_left hr).mpr a_h_left, }, { rintro h, use x / r, split, exact (div_lt_iff' hr).mpr h, exact mul_div_cancel' _ (ne_of_gt hr), } end lemma smul_Ici : r • Ici a = Ici (r • a) := begin ext x, simp only [mem_smul_set, smul_eq_mul, mem_Ioi], split, { rintro ⟨a_w, a_h_left, rfl⟩, exact (mul_le_mul_left hr).mpr a_h_left, }, { rintro h, use x / r, split, exact (le_div_iff' hr).mpr h, exact mul_div_cancel' _ (ne_of_gt hr), } end lemma smul_Iic : r • Iic a = Iic (r • a) := begin ext x, simp only [mem_smul_set, smul_eq_mul, mem_Iio], split, { rintro ⟨a_w, a_h_left, rfl⟩, exact (mul_le_mul_left hr).mpr a_h_left, }, { rintro h, use x / r, split, exact (div_le_iff' hr).mpr h, exact mul_div_cancel' _ (ne_of_gt hr), } end end linear_ordered_field
13c2ae4ec5d78b339e0045150e2f91f515292291
42c01158c2730cc6ac3e058c1339c18cb90366e2
/graph_theory/graph.lean
71568fdd9511c74ec85fec42f656db45df0d48a3
[]
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
3,230
lean
structure graph: Type 1 := (vertex : Type) (edge: vertex → vertex → Prop) (undirected: ∀ a b: vertex, edge a b ↔ edge b a) (no_loops : ∀ a: vertex, ¬ edge a a) notation a - b := graph.edge a b class graphs: Type 1 := (empty: graph) (g : graph) (add_vertex : graph → graph) (add_edge : graph → graph.vertex g → graph.vertex g → graph) structure graph2 := (deg : nat) (edge : fin deg → fin deg → Prop ) (undirected: ∀ a b: fin deg, edge a b ↔ edge b a) (no_loops : ∀ a: fin deg, ¬ edge a a) definition complete_graph (n: nat) : graph2 := { deg := n, edge := λ a b, a ≠ b, undirected := λ a b, ⟨ne.symm,ne.symm⟩, no_loops := λ a, ne.irrefl } definition empty_graph (n: nat) : graph2 := { deg := n, edge := λ a b, false, undirected := λ a b, iff.refl false, no_loops := λ a, id } def add_vertex : graph2 → graph2 | ⟨d, e, u, n⟩ := { deg := d + 1, edge := λ a b, begin apply or.by_cases (decidable.em (a.val < d)), intro ha, apply or.by_cases (decidable.em (b.val < d)), intro hb, exact e ⟨a.val,ha⟩ ⟨b.val,hb⟩, exact (λ hb, false), exact (λ ha, false) end, undirected := λ a b, begin split, intros bs, unfold or.by_cases at *, apply or.by_cases (decidable.em (b.val < d)), intro hb, apply or.by_cases (decidable.em (a.val < d)), intro ha, rw [dif_pos hb, dif_pos ha], rw u, rwa [dif_pos ha, dif_pos hb] at bs, intro hna, rw [dif_pos hb, dif_neg hna, dif_pos hna], rwa [dif_neg hna, dif_pos hna] at bs, intro hnb, apply or.by_cases (decidable.em (a.val < d)), intro ha, rw [dif_neg hnb, dif_pos hnb], rwa [dif_pos ha, dif_neg hnb, dif_pos hnb] at bs, intro hna, rw [dif_neg hnb, dif_pos hnb], rwa [dif_neg hna, dif_pos hna] at bs, intros bs, unfold or.by_cases at *, apply or.by_cases (decidable.em (a.val < d)), intro hb, apply or.by_cases (decidable.em (b.val < d)), intro ha, rw [dif_pos hb, dif_pos ha], rw u, rwa [dif_pos ha, dif_pos hb] at bs, intro hna, rw [dif_pos hb, dif_neg hna, dif_pos hna], rwa [dif_neg hna, dif_pos hna] at bs, intro hnb, apply or.by_cases (decidable.em (b.val < d)), intro ha, rw [dif_neg hnb, dif_pos hnb], rwa [dif_pos ha, dif_neg hnb, dif_pos hnb] at bs, intro hna, rw [dif_neg hnb, dif_pos hnb], rwa [dif_neg hna, dif_pos hna] at bs, end, no_loops := λ a, begin intro bs, unfold or.by_cases at bs, apply or.by_cases (decidable.em (a.val < d)), intro ha, rwa [dif_pos ha, dif_pos ha] at bs, apply n ⟨a.val, ha⟩, exact bs, intro hna, rwa [dif_neg hna, dif_pos hna] at bs end }
4974db675f35ffaa1338597b8f67745a0f819b59
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/category/bifunctor.lean
b566667d8fe984a02c7d4dd7a5568c790a79b79d
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
4,528
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon Functors with two arguments -/ import data.sum category.basic category.functor tactic.basic universes u₀ u₁ u₂ v₀ v₁ v₂ class bifunctor (F : Type u₀ → Type u₁ → Type u₂) := (bimap : Π {α α' β β'}, (α → α') → (β → β') → F α β → F α' β') export bifunctor ( bimap ) class is_lawful_bifunctor (F : Type u₀ → Type u₁ → Type u₂) [bifunctor F] := (id_bimap : Π {α β} (x : F α β), bimap id id x = x) (bimap_bimap : Π {α₀ α₁ α₂ β₀ β₁ β₂} (f : α₀ → α₁) (f' : α₁ → α₂) (g : β₀ → β₁) (g' : β₁ → β₂) (x : F α₀ β₀), bimap f' g' (bimap f g x) = bimap (f' ∘ f) (g' ∘ g) x) export is_lawful_bifunctor (id_bimap bimap_bimap) attribute [higher_order bimap_id_id] id_bimap attribute [higher_order bimap_comp_bimap] bimap_bimap export is_lawful_bifunctor (bimap_id_id bimap_comp_bimap) variables {F : Type u₀ → Type u₁ → Type u₂} [bifunctor F] namespace bifunctor @[reducible] def fst {α α' β} (f : α → α') : F α β → F α' β := bimap f id @[reducible] def snd {α β β'} (f : β → β') : F α β → F α β' := bimap id f variable [is_lawful_bifunctor F] @[higher_order fst_id] lemma id_fst : Π {α β} (x : F α β), fst id x = x := @id_bimap _ _ _ @[higher_order snd_id] lemma id_snd : Π {α β} (x : F α β), snd id x = x := @id_bimap _ _ _ @[higher_order fst_comp_fst] lemma comp_fst {α₀ α₁ α₂ β} (f : α₀ → α₁) (f' : α₁ → α₂) (x : F α₀ β) : fst f' (fst f x) = fst (f' ∘ f) x := by simp [fst,bimap_bimap] @[higher_order fst_comp_snd] lemma fst_snd {α₀ α₁ β₀ β₁} (f : α₀ → α₁) (f' : β₀ → β₁) (x : F α₀ β₀) : fst f (snd f' x) = bimap f f' x := by simp [fst,bimap_bimap] @[higher_order snd_comp_fst] lemma snd_fst {α₀ α₁ β₀ β₁} (f : α₀ → α₁) (f' : β₀ → β₁) (x : F α₀ β₀) : snd f' (fst f x) = bimap f f' x := by simp [snd,bimap_bimap] @[higher_order snd_comp_snd] lemma comp_snd {α β₀ β₁ β₂} (g : β₀ → β₁) (g' : β₁ → β₂) (x : F α β₀) : snd g' (snd g x) = snd (g' ∘ g) x := by simp [snd,bimap_bimap] attribute [functor_norm] bimap_bimap comp_snd comp_fst snd_comp_snd snd_comp_fst fst_comp_snd fst_comp_fst bimap_comp_bimap bimap_id_id fst_id snd_id def bicompl (F : Type* → Type* → Type*) (G : Type* → Type*) (H : Type* → Type*) (α β) := F (G α) (H β) def bicompr (F : Type* → Type*) (G : Type* → Type* → Type*) (α β) := F (G α β) end bifunctor open functor instance : bifunctor prod := { bimap := @prod.map } instance : is_lawful_bifunctor prod := by refine { .. }; intros; cases x; refl instance bifunctor.const : bifunctor const := { bimap := (λ α α' β β f _, f) } instance is_lawful_bifunctor.const : is_lawful_bifunctor const := by refine { .. }; intros; refl instance bifunctor.flip : bifunctor (flip F) := { bimap := (λ α α' β β' f f' x, (bimap f' f x : F β' α')) } instance is_lawful_bifunctor.flip [is_lawful_bifunctor F] : is_lawful_bifunctor (flip F) := by refine { .. }; intros; simp [bimap] with functor_norm instance : bifunctor sum := { bimap := @sum.map } instance : is_lawful_bifunctor sum := by refine { .. }; intros; cases x; refl open bifunctor functor @[priority 0] instance bifunctor.functor {α} : functor (F α) := { map := λ _ _, snd } @[priority 0] instance bifunctor.is_lawful_functor [is_lawful_bifunctor F] {α} : is_lawful_functor (F α) := by refine {..}; intros; simp [functor.map] with functor_norm section bicompl variables (G : Type* → Type u₀) (H : Type* → Type u₁) [functor G] [functor H] instance : bifunctor (bicompl F G H) := { bimap := λ α α' β β' f f' x, (bimap (map f) (map f') x : F (G α') (H β')) } instance [is_lawful_functor G] [is_lawful_functor H] [is_lawful_bifunctor F] : is_lawful_bifunctor (bicompl F G H) := by constructor; intros; simp [bimap,map_id,map_comp_map] with functor_norm end bicompl section bicompr variables (G : Type u₂ → Type*) [functor G] instance : bifunctor (bicompr G F) := { bimap := λ α α' β β' f f' x, (map (bimap f f') x : G (F α' β')) } instance [is_lawful_functor G] [is_lawful_bifunctor F] : is_lawful_bifunctor (bicompr G F) := by constructor; intros; simp [bimap] with functor_norm end bicompr
5a7881a7403ea218ea6f40b23ff7ff04d7d9d320
618003631150032a5676f229d13a079ac875ff77
/src/ring_theory/algebraic.lean
94b8154b395bc34ee4411798b29cda1f60df4a6b
[ "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
3,548
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 ring_theory.integral_closure /-! # Algebraic elements and algebraic extensions An element of an R-algebra is algebraic over R if it is the root of a nonzero polynomial. An R-algebra is algebraic over R if and only if all its elements are algebraic over R. The main result in this file proves transitivity of algebraicity: a tower of algebraic field extensions is algebraic. -/ universe variables u v open_locale classical open polynomial section variables (R : Type u) {A : Type v} [comm_ring R] [comm_ring A] [algebra R A] /-- An element of an R-algebra is algebraic over R if it is the root of a nonzero polynomial. -/ def is_algebraic (x : A) : Prop := ∃ p : polynomial R, p ≠ 0 ∧ aeval R A x p = 0 variables {R} /-- A subalgebra is algebraic if all its elements are algebraic. -/ def subalgebra.is_algebraic (S : subalgebra R A) : Prop := ∀ x ∈ S, is_algebraic R x variables (R A) /-- An algebra is algebraic if all its elements are algebraic. -/ def algebra.is_algebraic : Prop := ∀ x : A, is_algebraic R x variables {R A} /-- A subalgebra is algebraic if and only if it is algebraic an algebra. -/ lemma subalgebra.is_algebraic_iff (S : subalgebra R A) : S.is_algebraic ↔ @algebra.is_algebraic R S _ _ (by convert S.algebra) := begin delta algebra.is_algebraic subalgebra.is_algebraic, rw [subtype.forall'], apply forall_congr, rintro ⟨x, hx⟩, apply exists_congr, intro p, apply and_congr iff.rfl, have h : function.injective (S.val) := subtype.val_injective, conv_rhs { rw [← h.eq_iff, alg_hom.map_zero], }, apply eq_iff_eq_cancel_right.mpr, symmetry, -- TODO: add an `aeval`-specific version of `hom_eval₂` simp only [aeval_def], convert hom_eval₂ p (algebra_map R S) ↑S.val ⟨x, hx⟩, refl end /-- An algebra is algebraic if and only if it is algebraic as a subalgebra. -/ lemma algebra.is_algebraic_iff : algebra.is_algebraic R A ↔ (⊤ : subalgebra R A).is_algebraic := begin delta algebra.is_algebraic subalgebra.is_algebraic, simp only [algebra.mem_top, forall_prop_of_true, iff_self], end end section zero_ne_one variables (R : Type u) {A : Type v} [comm_ring R] [nonzero R] [comm_ring A] [algebra R A] /-- An integral element of an algebra is algebraic.-/ lemma is_integral.is_algebraic {x : A} (h : is_integral R x) : is_algebraic R x := by { rcases h with ⟨p, hp, hpx⟩, exact ⟨p, hp.ne_zero, hpx⟩ } end zero_ne_one section field variables (K : Type u) {A : Type v} [field K] [comm_ring A] [algebra K A] /-- An element of an algebra over a field is algebraic if and only if it is integral.-/ lemma is_algebraic_iff_is_integral {x : A} : is_algebraic K x ↔ is_integral K x := begin refine ⟨_, is_integral.is_algebraic K⟩, rintro ⟨p, hp, hpx⟩, refine ⟨_, monic_mul_leading_coeff_inv hp, _⟩, rw [alg_hom.map_mul, hpx, zero_mul], end end field namespace algebra variables {K : Type*} {L : Type*} {A : Type*} variables [field K] [field L] [comm_ring A] variables [algebra K L] [algebra L A] /-- If L is an algebraic field extension of K and A is an algebraic algebra over L, then A is algebraic over K. -/ lemma is_algebraic_trans (L_alg : is_algebraic K L) (A_alg : is_algebraic L A) : is_algebraic K (comap K L A) := begin simp only [is_algebraic, is_algebraic_iff_is_integral] at L_alg A_alg ⊢, exact is_integral_trans L_alg A_alg, end end algebra
356e0eabc9f78908a05c16c2a0a48d9eeb08eef5
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/group_theory/submonoid/operations.lean
4767adc91898f12b338b4302d16f9e5f0faea1de
[ "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
36,256
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 group_theory.submonoid.basic import data.equiv.mul_add import algebra.group.prod import algebra.group.inj_surj import group_theory.group_action.defs /-! # Operations on `submonoid`s In this file we define various operations on `submonoid`s and `monoid_hom`s. ## Main definitions ### Conversion between multiplicative and additive definitions * `submonoid.to_add_submonoid`, `submonoid.to_add_submonoid'`, `add_submonoid.to_submonoid`, `add_submonoid.to_submonoid'`: convert between multiplicative and additive submonoids of `M`, `multiplicative M`, and `additive M`. These are stated as `order_iso`s. ### (Commutative) monoid structure on a submonoid * `submonoid.to_monoid`, `submonoid.to_comm_monoid`: a submonoid inherits a (commutative) monoid structure. ### Group actions by submonoids * `submonoid.mul_action`, `submonoid.distrib_mul_action`: a submonoid inherits (distributive) multiplicative actions. ### Operations on submonoids * `submonoid.comap`: preimage of a submonoid under a monoid homomorphism as a submonoid of the domain; * `submonoid.map`: image of a submonoid under a monoid homomorphism as a submonoid of the codomain; * `submonoid.prod`: product of two submonoids `s : submonoid M` and `t : submonoid N` as a submonoid of `M × N`; ### Monoid homomorphisms between submonoid * `submonoid.subtype`: embedding of a submonoid into the ambient monoid. * `submonoid.inclusion`: given two submonoids `S`, `T` such that `S ≤ T`, `S.inclusion T` is the inclusion of `S` into `T` as a monoid homomorphism; * `mul_equiv.submonoid_congr`: converts a proof of `S = T` into a monoid isomorphism between `S` and `T`. * `submonoid.prod_equiv`: monoid isomorphism between `s.prod t` and `s × t`; ### Operations on `monoid_hom`s * `monoid_hom.mrange`: range of a monoid homomorphism as a submonoid of the codomain; * `monoid_hom.mker`: kernel of a monoid homomorphism as a submonoid of the domain; * `monoid_hom.mrestrict`: restrict a monoid homomorphism to a submonoid; * `monoid_hom.cod_mrestrict`: restrict the codomain of a monoid homomorphism to a submonoid; * `monoid_hom.mrange_restrict`: restrict a monoid homomorphism to its range; ## Tags submonoid, range, product, map, comap -/ variables {M N P : Type*} [mul_one_class M] [mul_one_class N] [mul_one_class P] (S : submonoid M) /-! ### Conversion to/from `additive`/`multiplicative` -/ section /-- Submonoids of monoid `M` are isomorphic to additive submonoids of `additive M`. -/ @[simps] def submonoid.to_add_submonoid : submonoid M ≃o add_submonoid (additive M) := { to_fun := λ S, { carrier := additive.to_mul ⁻¹' S, zero_mem' := S.one_mem', add_mem' := S.mul_mem' }, inv_fun := λ S, { carrier := additive.of_mul ⁻¹' S, one_mem' := S.zero_mem', mul_mem' := S.add_mem' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Additive submonoids of an additive monoid `additive M` are isomorphic to submonoids of `M`. -/ abbreviation add_submonoid.to_submonoid' : add_submonoid (additive M) ≃o submonoid M := submonoid.to_add_submonoid.symm lemma submonoid.to_add_submonoid_closure (S : set M) : (submonoid.closure S).to_add_submonoid = add_submonoid.closure (additive.to_mul ⁻¹' S) := le_antisymm (submonoid.to_add_submonoid.le_symm_apply.1 $ submonoid.closure_le.2 add_submonoid.subset_closure) (add_submonoid.closure_le.2 submonoid.subset_closure) lemma add_submonoid.to_submonoid'_closure (S : set (additive M)) : (add_submonoid.closure S).to_submonoid' = submonoid.closure (multiplicative.of_add ⁻¹' S) := le_antisymm (add_submonoid.to_submonoid'.le_symm_apply.1 $ add_submonoid.closure_le.2 submonoid.subset_closure) (submonoid.closure_le.2 add_submonoid.subset_closure) end section variables {A : Type*} [add_zero_class A] /-- Additive submonoids of an additive monoid `A` are isomorphic to multiplicative submonoids of `multiplicative A`. -/ @[simps] def add_submonoid.to_submonoid : add_submonoid A ≃o submonoid (multiplicative A) := { to_fun := λ S, { carrier := multiplicative.to_add ⁻¹' S, one_mem' := S.zero_mem', mul_mem' := S.add_mem' }, inv_fun := λ S, { carrier := multiplicative.of_add ⁻¹' S, zero_mem' := S.one_mem', add_mem' := S.mul_mem' }, left_inv := λ x, by cases x; refl, right_inv := λ x, by cases x; refl, map_rel_iff' := λ a b, iff.rfl, } /-- Submonoids of a monoid `multiplicative A` are isomorphic to additive submonoids of `A`. -/ abbreviation submonoid.to_add_submonoid' : submonoid (multiplicative A) ≃o add_submonoid A := add_submonoid.to_submonoid.symm lemma add_submonoid.to_submonoid_closure (S : set A) : (add_submonoid.closure S).to_submonoid = submonoid.closure (multiplicative.to_add ⁻¹' S) := le_antisymm (add_submonoid.to_submonoid.to_galois_connection.l_le $ add_submonoid.closure_le.2 submonoid.subset_closure) (submonoid.closure_le.2 add_submonoid.subset_closure) lemma submonoid.to_add_submonoid'_closure (S : set (multiplicative A)) : (submonoid.closure S).to_add_submonoid' = add_submonoid.closure (additive.of_mul ⁻¹' S) := le_antisymm (submonoid.to_add_submonoid'.to_galois_connection.l_le $ submonoid.closure_le.2 add_submonoid.subset_closure) (add_submonoid.closure_le.2 submonoid.subset_closure) end namespace submonoid open set /-! ### `comap` and `map` -/ /-- 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 @[simp, to_additive] lemma comap_id (S : submonoid P) : S.comap (monoid_hom.id _) = S := ext (by simp) /-- 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 mem_map_of_mem (f : M →* N) {S : submonoid M} {x : M} (hx : x ∈ S) : f x ∈ S.map f := mem_image_of_mem f hx @[to_additive] lemma apply_coe_mem_map (f : M →* N) (S : submonoid M) (x : S) : f x ∈ S.map f := mem_map_of_mem f x.prop @[to_additive] lemma map_map (g : N →* P) (f : M →* N) : (S.map f).map g = S.map (g.comp f) := set_like.coe_injective $ image_image _ _ _ @[to_additive] lemma mem_map_iff_mem {f : M →* N} (hf : function.injective f) {S : submonoid M} {x : M} : f x ∈ S.map f ↔ x ∈ S := hf.mem_set_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_le_of_le_comap {T : submonoid N} {f : M →* N} : S ≤ T.comap f → S.map f ≤ T := (gc_map_comap f).l_le @[to_additive] lemma le_comap_of_map_le {T : submonoid N} {f : M →* N} : S.map f ≤ T → S ≤ T.comap f := (gc_map_comap f).le_u @[to_additive] lemma le_comap_map {f : M →* N} : S ≤ (S.map f).comap f := (gc_map_comap f).le_u_l _ @[to_additive] lemma map_comap_le {S : submonoid N} {f : M →* N} : (S.comap f).map f ≤ S := (gc_map_comap f).l_u_le _ @[to_additive] lemma monotone_map {f : M →* N} : monotone (map f) := (gc_map_comap f).monotone_l @[to_additive] lemma monotone_comap {f : M →* N} : monotone (comap f) := (gc_map_comap f).monotone_u @[simp, to_additive] lemma map_comap_map {f : M →* N} : ((S.map f).comap f).map f = S.map f := (gc_map_comap f).l_u_l_eq_l _ @[simp, to_additive] lemma comap_map_comap {S : submonoid N} {f : M →* N} : ((S.comap f).map f).comap f = S.comap f := (gc_map_comap f).u_l_u_eq_u _ @[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⟩⟩) section galois_coinsertion variables {ι : Type*} {f : M →* N} (hf : function.injective f) include hf /-- `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. -/ @[to_additive /-" `map f` and `comap f` form a `galois_coinsertion` when `f` is injective. "-/] def gci_map_comap : galois_coinsertion (map f) (comap f) := (gc_map_comap f).to_galois_coinsertion (λ S x, by simp [mem_comap, mem_map, hf.eq_iff]) @[to_additive] lemma comap_map_eq_of_injective (S : submonoid M) : (S.map f).comap f = S := (gci_map_comap hf).u_l_eq _ @[to_additive] lemma comap_surjective_of_injective : function.surjective (comap f) := (gci_map_comap hf).u_surjective @[to_additive] lemma map_injective_of_injective : function.injective (map f) := (gci_map_comap hf).l_injective @[to_additive] lemma comap_inf_map_of_injective (S T : submonoid M) : (S.map f ⊓ T.map f).comap f = S ⊓ T := (gci_map_comap hf).u_inf_l _ _ @[to_additive] lemma comap_infi_map_of_injective (S : ι → submonoid M) : (⨅ i, (S i).map f).comap f = infi S := (gci_map_comap hf).u_infi_l _ @[to_additive] lemma comap_sup_map_of_injective (S T : submonoid M) : (S.map f ⊔ T.map f).comap f = S ⊔ T := (gci_map_comap hf).u_sup_l _ _ @[to_additive] lemma comap_supr_map_of_injective (S : ι → submonoid M) : (⨆ i, (S i).map f).comap f = supr S := (gci_map_comap hf).u_supr_l _ @[to_additive] lemma map_le_map_iff_of_injective {S T : submonoid M} : S.map f ≤ T.map f ↔ S ≤ T := (gci_map_comap hf).l_le_l_iff @[to_additive] lemma map_strict_mono_of_injective : strict_mono (map f) := (gci_map_comap hf).strict_mono_l end galois_coinsertion section galois_insertion variables {ι : Type*} {f : M →* N} (hf : function.surjective f) include hf /-- `map f` and `comap f` form a `galois_insertion` when `f` is surjective. -/ @[to_additive /-" `map f` and `comap f` form a `galois_insertion` when `f` is surjective. "-/] def gi_map_comap : galois_insertion (map f) (comap f) := (gc_map_comap f).to_galois_insertion (λ S x h, let ⟨y, hy⟩ := hf x in mem_map.2 ⟨y, by simp [hy, h]⟩) @[to_additive] lemma map_comap_eq_of_surjective (S : submonoid N) : (S.comap f).map f = S := (gi_map_comap hf).l_u_eq _ @[to_additive] lemma map_surjective_of_surjective : function.surjective (map f) := (gi_map_comap hf).l_surjective @[to_additive] lemma comap_injective_of_surjective : function.injective (comap f) := (gi_map_comap hf).u_injective @[to_additive] lemma map_inf_comap_of_surjective (S T : submonoid N) : (S.comap f ⊓ T.comap f).map f = S ⊓ T := (gi_map_comap hf).l_inf_u _ _ @[to_additive] lemma map_infi_comap_of_surjective (S : ι → submonoid N) : (⨅ i, (S i).comap f).map f = infi S := (gi_map_comap hf).l_infi_u _ @[to_additive] lemma map_sup_comap_of_surjective (S T : submonoid N) : (S.comap f ⊔ T.comap f).map f = S ⊔ T := (gi_map_comap hf).l_sup_u _ _ @[to_additive] lemma map_supr_comap_of_surjective (S : ι → submonoid N) : (⨆ i, (S i).comap f).map f = supr S := (gi_map_comap hf).l_supr_u _ @[to_additive] lemma comap_le_comap_iff_of_surjective {S T : submonoid N} : S.comap f ≤ T.comap f ↔ S ≤ T := (gi_map_comap hf).u_le_u_iff @[to_additive] lemma comap_strict_mono_of_surjective : strict_mono (comap f) := (gi_map_comap hf).strict_mono_u end galois_insertion /-- 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, norm_cast, to_additive] lemma coe_mul (x y : S) : (↑(x * y) : M) = ↑x * ↑y := rfl @[simp, norm_cast, to_additive] lemma coe_one : ((1 : S) : M) = 1 := rfl @[simp, to_additive] lemma mk_mul_mk (x y : M) (hx : x ∈ S) (hy : y ∈ S) : (⟨x, hx⟩ : S) * ⟨y, hy⟩ = ⟨x * y, S.mul_mem hx hy⟩ := rfl @[to_additive] lemma mul_def (x y : S) : x * y = ⟨x * y, S.mul_mem x.2 y.2⟩ := rfl @[to_additive] lemma one_def : (1 : S) = ⟨1, S.one_mem⟩ := rfl /-- A submonoid of a unital magma inherits a unital magma structure. -/ @[to_additive "An `add_submonoid` of an unital additive magma inherits an unital additive magma structure."] instance to_mul_one_class {M : Type*} [mul_one_class M] (S : submonoid M) : mul_one_class S := subtype.coe_injective.mul_one_class coe rfl (λ _ _, rfl) /-- A submonoid of a monoid inherits a monoid structure. -/ @[to_additive "An `add_submonoid` of an `add_monoid` inherits an `add_monoid` structure."] instance to_monoid {M : Type*} [monoid M] (S : submonoid M) : monoid S := subtype.coe_injective.monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `comm_monoid` is a `comm_monoid`. -/ @[to_additive "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 := subtype.coe_injective.comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of an `ordered_comm_monoid` is an `ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_add_comm_monoid` is an `ordered_add_comm_monoid`."] instance to_ordered_comm_monoid {M} [ordered_comm_monoid M] (S : submonoid M) : ordered_comm_monoid S := subtype.coe_injective.ordered_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `linear_ordered_comm_monoid` is a `linear_ordered_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_add_comm_monoid` is a `linear_ordered_add_comm_monoid`."] instance to_linear_ordered_comm_monoid {M} [linear_ordered_comm_monoid M] (S : submonoid M) : linear_ordered_comm_monoid S := subtype.coe_injective.linear_ordered_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of an `ordered_cancel_comm_monoid` is an `ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of an `ordered_cancel_add_comm_monoid` is an `ordered_cancel_add_comm_monoid`."] instance to_ordered_cancel_comm_monoid {M} [ordered_cancel_comm_monoid M] (S : submonoid M) : ordered_cancel_comm_monoid S := subtype.coe_injective.ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) /-- A submonoid of a `linear_ordered_cancel_comm_monoid` is a `linear_ordered_cancel_comm_monoid`. -/ @[to_additive "An `add_submonoid` of a `linear_ordered_cancel_add_comm_monoid` is a `linear_ordered_cancel_add_comm_monoid`."] instance to_linear_ordered_cancel_comm_monoid {M} [linear_ordered_cancel_comm_monoid M] (S : submonoid M) : linear_ordered_cancel_comm_monoid S := subtype.coe_injective.linear_ordered_cancel_comm_monoid coe rfl (λ _ _, rfl) /-- 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 /-- A submonoid is isomorphic to its image under an injective function -/ @[to_additive "An additive submonoid is isomorphic to its image under an injective function"] noncomputable def equiv_map_of_injective (f : M →* N) (hf : function.injective f) : S ≃* S.map f := { map_mul' := λ _ _, subtype.ext (f.map_mul _ _), ..equiv.set.image f S hf } @[simp, to_additive] lemma coe_equiv_map_of_injective_apply (f : M →* N) (hf : function.injective f) (x : S) : (equiv_map_of_injective S f hf x : N) = f x := rfl @[simp, to_additive] lemma closure_closure_coe_preimage {s : set M} : closure ((coe : closure s → M) ⁻¹' s) = ⊤ := eq_top_iff.2 $ λ x, subtype.rec_on x $ λ x hx _, begin refine closure_induction' _ (λ g hg, _) _ (λ g₁ g₂ hg₁ hg₂, _) hx, { exact subset_closure hg }, { exact submonoid.one_mem _ }, { exact submonoid.mul_mem _ }, end /-- 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) ×ˢ (t : set N), 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) ×ˢ (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 {s₁ s₂ : submonoid M} {t₁ t₂ : submonoid N} (hs : s₁ ≤ s₂) (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht @[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) = ⊥ := set_like.coe_injective $ by simp [coe_prod, prod.one_eq_mk] /-- The product of submonoids is isomorphic to their product as monoids. -/ @[to_additive prod_equiv "The 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 } open monoid_hom @[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⟩⟩ @[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 (le_refl s) bot_le) (prod_mono bot_le (le_refl t))) $ 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⟩) @[to_additive] lemma mem_map_equiv {f : M ≃* N} {K : submonoid M} {x : N} : x ∈ K.map f.to_monoid_hom ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x @[to_additive] lemma map_equiv_eq_comap_symm (f : M ≃* N) (K : submonoid M) : K.map f.to_monoid_hom = K.comap f.symm.to_monoid_hom := set_like.coe_injective (f.to_equiv.image_eq_preimage K) @[to_additive] lemma comap_equiv_eq_map_symm (f : N ≃* M) (K : submonoid M) : K.comap f.to_monoid_hom = K.map f.symm.to_monoid_hom := (map_equiv_eq_comap_symm f.symm K).symm @[simp, to_additive] lemma map_equiv_top (f : M ≃* N) : (⊤ : submonoid M).map f.to_monoid_hom = ⊤ := set_like.coe_injective $ set.image_univ.trans f.surjective.range_eq end submonoid namespace monoid_hom open submonoid /-- For many categories (monoids, modules, rings, ...) the set-theoretic image of a morphism `f` is a subobject of the codomain. When this is the case, it is useful to define the range of a morphism in such a way that the underlying carrier set of the range subobject is definitionally `set.range f`. In particular this means that the types `↥(set.range f)` and `↥f.range` are interchangeable without proof obligations. A convenient candidate definition for range which is mathematically correct is `map ⊤ f`, just as `set.range` could have been defined as `f '' set.univ`. However, this lacks the desired definitional convenience, in that it both does not match `set.range`, and that it introduces a redudant `x ∈ ⊤` term which clutters proofs. In such a case one may resort to the `copy` pattern. A `copy` function converts the definitional problem for the carrier set of a subobject into a one-off propositional proof obligation which one discharges while writing the definition of the definitionally convenient range (the parameter `hs` in the example below). A good example is the case of a morphism of monoids. A convenient definition for `monoid_hom.mrange` would be `(⊤ : submonoid M).map f`. However since this lacks the required definitional convenience, we first define `submonoid.copy` as follows: ```lean protected 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' } ``` and then finally define: ```lean def mrange (f : M →* N) : submonoid N := ((⊤ : submonoid M).map f).copy (set.range f) set.image_univ.symm ``` -/ library_note "range copy pattern" /-- The range of a monoid homomorphism is a submonoid. See Note [range copy pattern]. -/ @[to_additive "The range of an `add_monoid_hom` is an `add_submonoid`."] def mrange (f : M →* N) : submonoid N := ((⊤ : submonoid M).map f).copy (set.range f) set.image_univ.symm @[simp, to_additive] lemma coe_mrange (f : M →* N) : (f.mrange : set N) = set.range f := rfl @[simp, to_additive] lemma mem_mrange {f : M →* N} {y : N} : y ∈ f.mrange ↔ ∃ x, f x = y := iff.rfl @[to_additive] lemma mrange_eq_map (f : M →* N) : f.mrange = (⊤ : submonoid M).map f := copy_eq _ @[to_additive] lemma map_mrange (g : N →* P) (f : M →* N) : f.mrange.map g = (g.comp f).mrange := by simpa only [mrange_eq_map] using (⊤ : submonoid M).map_map g f @[to_additive] lemma mrange_top_iff_surjective {N} [mul_one_class N] {f : M →* N} : f.mrange = (⊤ : submonoid N) ↔ function.surjective f := set_like.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} [mul_one_class N] (f : M →* N) (hf : function.surjective f) : f.mrange = (⊤ : submonoid N) := mrange_top_iff_surjective.2 hf @[to_additive] lemma mclosure_preimage_le (f : M →* N) (s : set N) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a 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) /-- 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*} [mul_one_class N] (f : M →* N) (S : submonoid M) : S →* N := f.comp S.subtype @[simp, to_additive] lemma mrestrict_apply {N : Type*} [mul_one_class 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.", simps] 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 interpreted as a submonoid. -/ @[to_additive "Restriction of an `add_monoid` hom to its range interpreted as a submonoid."] def mrange_restrict {N} [mul_one_class N] (f : M →* N) : M →* f.mrange := f.cod_mrestrict f.mrange $ λ x, ⟨x, rfl⟩ @[simp, to_additive] lemma coe_mrange_restrict {N} [mul_one_class N] (f : M →* N) (x : M) : (f.mrange_restrict x : N) = f x := rfl /-- The multiplicative kernel of a monoid homomorphism is the submonoid of elements `x : G` such that `f x = 1` -/ @[to_additive "The additive kernel of an `add_monoid` homomorphism is the `add_submonoid` of elements such that `f x = 0`"] def mker (f : M →* N) : submonoid M := (⊥ : submonoid N).comap f @[to_additive] lemma mem_mker (f : M →* N) {x : M} : x ∈ f.mker ↔ f x = 1 := iff.rfl @[to_additive] lemma coe_mker (f : M →* N) : (f.mker : set M) = (f : M → N) ⁻¹' {1} := rfl @[to_additive] instance decidable_mem_mker [decidable_eq N] (f : M →* N) : decidable_pred (∈ f.mker) := λ x, decidable_of_iff (f x = 1) f.mem_mker @[to_additive] lemma comap_mker (g : N →* P) (f : M →* N) : g.mker.comap f = (g.comp f).mker := rfl @[simp, to_additive] lemma comap_bot' (f : M →* N) : (⊥ : submonoid N).comap f = f.mker := rfl @[to_additive] lemma range_restrict_mker (f : M →* N) : mker (mrange_restrict f) = mker f := begin ext, change (⟨f x, _⟩ : mrange f) = ⟨1, _⟩ ↔ f x = 1, simp only [], end @[simp, to_additive] lemma mker_one : (1 : M →* N).mker = ⊤ := by { ext, simp [mem_mker] } @[to_additive] lemma prod_map_comap_prod' {M' : Type*} {N' : Type*} [mul_one_class M'] [mul_one_class N'] (f : M →* N) (g : M' →* N') (S : submonoid N) (S' : submonoid N') : (S.prod S').comap (prod_map f g) = (S.comap f).prod (S'.comap g) := set_like.coe_injective $ set.preimage_prod_map_prod f g _ _ @[to_additive] lemma mker_prod_map {M' : Type*} {N' : Type*} [mul_one_class M'] [mul_one_class N'] (f : M →* N) (g : M' →* N') : (prod_map f g).mker = f.mker.prod g.mker := by rw [←comap_bot', ←comap_bot', ←comap_bot', ←prod_map_comap_prod', bot_prod_bot] /-- The `monoid_hom` from the preimage of a submonoid to itself. -/ @[to_additive "the `add_monoid_hom` from the preimage of an additive submonoid to itself.", simps] def submonoid_comap (f : M →* N) (N' : submonoid N) : N'.comap f →* N' := { to_fun := λ x, ⟨f x, x.prop⟩, map_one' := subtype.eq f.map_one, map_mul' := λ x y, subtype.eq (f.map_mul x y) } /-- The `monoid_hom` from a submonoid to its image. See `mul_equiv.submonoid_map` for a variant for `mul_equiv`s. -/ @[to_additive "the `add_monoid_hom` from an additive submonoid to its image. See `add_equiv.add_submonoid_map` for a variant for `add_equiv`s.", simps] def submonoid_map (f : M →* N) (M' : submonoid M) : M' →* M'.map f := { to_fun := λ x, ⟨f x, ⟨x, x.prop, rfl⟩⟩, map_one' := subtype.eq $ f.map_one, map_mul' := λ x y, subtype.eq $ f.map_mul x y } @[to_additive] lemma submonoid_map_surjective (f : M →* N) (M' : submonoid M) : function.surjective (f.submonoid_map M') := by { rintro ⟨_, x, hx, rfl⟩, exact ⟨⟨x, hx⟩, rfl⟩ } end monoid_hom namespace submonoid open monoid_hom @[to_additive] lemma mrange_inl : (inl M N).mrange = prod ⊤ ⊥ := by simpa only [mrange_eq_map] using map_inl ⊤ @[to_additive] lemma mrange_inr : (inr M N).mrange = prod ⊥ ⊤ := by simpa only [mrange_eq_map] using map_inr ⊤ @[to_additive] lemma mrange_inl' : (inl M N).mrange = comap (snd M N) ⊥ := mrange_inl.trans (top_prod _) @[to_additive] lemma mrange_inr' : (inr M N).mrange = comap (fst M N) ⊥ := mrange_inr.trans (prod_top _) @[simp, to_additive] lemma mrange_fst : (fst M N).mrange = ⊤ := (fst M N).mrange_top_of_surjective $ @prod.fst_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_snd : (snd M N).mrange = ⊤ := (snd M N).mrange_top_of_surjective $ @prod.snd_surjective _ _ ⟨1⟩ @[simp, to_additive] lemma mrange_inl_sup_mrange_inr : (inl M N).mrange ⊔ (inr M N).mrange = ⊤ := by simp only [mrange_inl, mrange_inr, prod_bot_sup_bot_prod, top_prod_top] /-- 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 := set_like.coe_injective $ (coe_mrange _).trans $ subtype.range_coe @[to_additive] lemma eq_top_iff' : S = ⊤ ↔ ∀ x : M, x ∈ S := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ @[to_additive] lemma eq_bot_iff_forall : S = ⊥ ↔ ∀ x ∈ S, x = (1 : M) := set_like.ext_iff.trans $ by simp [iff_def, S.one_mem] { contextual := tt } @[to_additive] lemma nontrivial_iff_exists_ne_one (S : submonoid M) : nontrivial S ↔ ∃ x ∈ S, x ≠ (1:M) := calc nontrivial S ↔ ∃ x : S, x ≠ 1 : nontrivial_iff_exists_ne 1 ... ↔ ∃ x (hx : x ∈ S), (⟨x, hx⟩ : S) ≠ ⟨1, S.one_mem⟩ : subtype.exists ... ↔ ∃ x ∈ S, x ≠ (1 : M) : by simp only [ne.def] /-- A submonoid is either the trivial submonoid or nontrivial. -/ @[to_additive] lemma bot_or_nontrivial (S : submonoid M) : S = ⊥ ∨ nontrivial S := by simp only [eq_bot_iff_forall, nontrivial_iff_exists_ne_one, ← not_forall, classical.em] /-- A submonoid is either the trivial submonoid or contains a nonzero element. -/ @[to_additive] lemma bot_or_exists_ne_one (S : submonoid M) : S = ⊥ ∨ ∃ x ∈ S, x ≠ (1:M) := S.bot_or_nontrivial.imp_right S.nontrivial_iff_exists_ne_one.mp end submonoid namespace mul_equiv variables {S} {T : submonoid M} /-- Makes the identity isomorphism from a proof that two submonoids of a multiplicative monoid are equal. -/ @[to_additive "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 $ congr_arg _ h } -- this name is primed so that the version to `f.range` instead of `f.mrange` can be unprimed. /-- A monoid homomorphism `f : M →* N` with a left-inverse `g : N → M` defines a multiplicative equivalence between `M` and `f.mrange`. This is a bidirectional version of `monoid_hom.mrange_restrict`. -/ @[to_additive /-" An additive monoid homomorphism `f : M →+ N` with a left-inverse `g : N → M` defines an additive equivalence between `M` and `f.mrange`. This is a bidirectional version of `add_monoid_hom.mrange_restrict`. "-/, simps {simp_rhs := tt}] def of_left_inverse' (f : M →* N) {g : N → M} (h : function.left_inverse g f) : M ≃* f.mrange := { to_fun := f.mrange_restrict, inv_fun := g ∘ f.mrange.subtype, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := monoid_hom.mem_mrange.mp x.prop in show f (g x) = x, by rw [←hx', h x'], .. f.mrange_restrict } /-- A `mul_equiv` `φ` between two monoids `M` and `N` induces a `mul_equiv` between a submonoid `S ≤ M` and the submonoid `φ(S) ≤ N`. See `monoid_hom.submonoid_map` for a variant for `monoid_hom`s. -/ @[to_additive "An `add_equiv` `φ` between two additive monoids `M` and `N` induces an `add_equiv` between a submonoid `S ≤ M` and the submonoid `φ(S) ≤ N`. See `add_monoid_hom.add_submonoid_map` for a variant for `add_monoid_hom`s.", simps] def submonoid_map (e : M ≃* N) (S : submonoid M) : S ≃* S.map e.to_monoid_hom := { to_fun := λ x, ⟨e x, _⟩, inv_fun := λ x, ⟨e.symm x, _⟩, -- we restate this for `simps` to avoid `⇑e.symm.to_equiv x` ..e.to_monoid_hom.submonoid_map S, ..e.to_equiv.image S } end mul_equiv section actions /-! ### Actions by `submonoid`s These instances tranfer the action by an element `m : M` of a monoid `M` written as `m • a` onto the action by an element `s : S` of a submonoid `S : submonoid M` such that `s • a = (s : M) • a`. These instances work particularly well in conjunction with `monoid.to_mul_action`, enabling `s • m` as an alias for `↑s * m`. -/ namespace submonoid variables {M' : Type*} {α β : Type*} [monoid M'] /-- The action by a submonoid is the action by the underlying monoid. -/ @[to_additive /-"The additive action by an add_submonoid is the action by the underlying add_monoid. "-/] instance [mul_action M' α] (S : submonoid M') : mul_action S α := mul_action.comp_hom _ S.subtype @[to_additive] lemma smul_def [mul_action M' α] {S : submonoid M'} (g : S) (m : α) : g • m = (g : M') • m := rfl /-- The action by a submonoid is the action by the underlying monoid. -/ instance [add_monoid α] [distrib_mul_action M' α] (S : submonoid M') : distrib_mul_action S α := distrib_mul_action.comp_hom _ S.subtype /-- The action by a submonoid is the action by the underlying monoid. -/ instance [monoid α] [mul_distrib_mul_action M' α] (S : submonoid M') : mul_distrib_mul_action S α := mul_distrib_mul_action.comp_hom _ S.subtype @[to_additive] instance smul_comm_class_left [mul_action M' β] [has_scalar α β] [smul_comm_class M' α β] (S : submonoid M') : smul_comm_class S α β := ⟨λ a, (smul_comm (a : M') : _)⟩ @[to_additive] instance smul_comm_class_right [has_scalar α β] [mul_action M' β] [smul_comm_class α M' β] (S : submonoid M') : smul_comm_class α S β := ⟨λ a s, (smul_comm a (s : M') : _)⟩ /-- Note that this provides `is_scalar_tower S M' M'` which is needed by `smul_mul_assoc`. -/ instance [has_scalar α β] [mul_action M' α] [mul_action M' β] [is_scalar_tower M' α β] (S : submonoid M') : is_scalar_tower S α β := ⟨λ a, (smul_assoc (a : M') : _)⟩ example {S : submonoid M'} : is_scalar_tower S M' M' := by apply_instance instance [mul_action M' α] [has_faithful_scalar M' α] (S : submonoid M') : has_faithful_scalar S α := { eq_of_smul_eq_smul := λ x y h, subtype.ext (eq_of_smul_eq_smul h) } end submonoid end actions
7be107897e3563b0535b1b3407e8b911d83037dc
2fbe653e4bc441efde5e5d250566e65538709888
/src/algebra/group/hom.lean
6a39ef2840a74133a8d6f9949b81a3da11511cdc
[ "Apache-2.0" ]
permissive
aceg00/mathlib
5e15e79a8af87ff7eb8c17e2629c442ef24e746b
8786ea6d6d46d6969ac9a869eb818bf100802882
refs/heads/master
1,649,202,698,930
1,580,924,783,000
1,580,924,783,000
149,197,272
0
0
Apache-2.0
1,537,224,208,000
1,537,224,207,000
null
UTF-8
Lean
false
false
16,252
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Kevin Buzzard, Scott Morrison, Johan Commelin, Chris Hughes, Johannes Hölzl, Yury Kudryashov Homomorphisms of multiplicative and additive (semi)groups and monoids. -/ import algebra.group.to_additive algebra.group.basic /-! # monoid and group homomorphisms This file defines the basic structures for monoid and group homomorphisms, both unbundled (e.g. `is_monoid_hom f`) and bundled (e.g. `monoid_hom M N`, a.k.a. `M →* N`). The unbundled ones are deprecated and the plan is to slowly remove them from mathlib. ## main definitions monoid_hom, is_monoid_hom (deprecated), is_group_hom (deprecated) ## Notations →* for bundled monoid homs (also use for group homs) →+ for bundled add_monoid homs (also use for add_group homs) ## implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `group_hom` -- the idea is that `monoid_hom` is used. The constructor for `monoid_hom` needs a proof of `map_one` as well as `map_mul`; a separate constructor `monoid_hom.mk'` will construct group homs (i.e. monoid homs between groups) given only a proof that multiplication is preserved, Throughout the `monoid_hom` section implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `monoid_hom`. When they can be inferred from the type it is faster to use this method than to use type class inference. ## Tags is_group_hom, is_monoid_hom, monoid_hom -/ library_note "low priority instance on morphisms" "We have instances stating that the composition or the product of two morphisms is again a morphism. Type class inference will 'succeed' in applying these instances when they shouldn't apply (for example when the goal is just `⊢ is_mul_hom f` the instances `is_mul_hom.comp` or `is_mul_hom.mul` might still succeed). This can cause type class inference to loop. To avoid this, we make the priority of these instances very low. We should think about not making these declarations instances in the first place." universes u v variables {α : Type u} {β : Type v} /-- Predicate for maps which preserve an addition. -/ class is_add_hom {α β : Type*} [has_add α] [has_add β] (f : α → β) : Prop := (map_add : ∀ x y, f (x + y) = f x + f y) /-- Predicate for maps which preserve a multiplication. -/ @[to_additive] class is_mul_hom {α β : Type*} [has_mul α] [has_mul β] (f : α → β) : Prop := (map_mul : ∀ x y, f (x * y) = f x * f y) namespace is_mul_hom variables [has_mul α] [has_mul β] {γ : Type*} [has_mul γ] /-- The identity map preserves multiplication. -/ @[to_additive "The identity map preserves addition"] instance id : is_mul_hom (id : α → α) := {map_mul := λ _ _, rfl} /-- The composition of maps which preserve multiplication, also preserves multiplication. -/ -- see Note [low priority instance on morphisms] @[priority 10, to_additive "The composition of addition preserving maps also preserves addition"] instance comp (f : α → β) (g : β → γ) [is_mul_hom f] [hg : is_mul_hom g] : is_mul_hom (g ∘ f) := { map_mul := λ x y, by simp only [function.comp, map_mul f, map_mul g] } /-- A product of maps which preserve multiplication, preserves multiplication when the target is commutative. -/ -- see Note [low priority instance on morphisms] @[instance, priority 10, to_additive] lemma mul {α β} [semigroup α] [comm_semigroup β] (f g : α → β) [is_mul_hom f] [is_mul_hom g] : is_mul_hom (λa, f a * g a) := { map_mul := assume a b, by simp only [map_mul f, map_mul g, mul_comm, mul_assoc, mul_left_comm] } /-- The inverse of a map which preserves multiplication, preserves multiplication when the target is commutative. -/ @[instance, to_additive] lemma inv {α β} [has_mul α] [comm_group β] (f : α → β) [is_mul_hom f] : is_mul_hom (λa, (f a)⁻¹) := { map_mul := assume a b, (map_mul f a b).symm ▸ mul_inv _ _ } end is_mul_hom section prio set_option default_priority 100 -- see Note [default priority] /-- Predicate for add_monoid homomorphisms (deprecated -- use the bundled `monoid_hom` version). -/ class is_add_monoid_hom [add_monoid α] [add_monoid β] (f : α → β) extends is_add_hom f : Prop := (map_zero : f 0 = 0) /-- Predicate for monoid homomorphisms (deprecated -- use the bundled `monoid_hom` version). -/ @[to_additive is_add_monoid_hom] class is_monoid_hom [monoid α] [monoid β] (f : α → β) extends is_mul_hom f : Prop := (map_one : f 1 = 1) end prio namespace is_monoid_hom variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] /-- A monoid homomorphism preserves multiplication. -/ @[to_additive] lemma map_mul (x y) : f (x * y) = f x * f y := is_mul_hom.map_mul f x y end is_monoid_hom /-- A map to a group preserving multiplication is a monoid homomorphism. -/ @[to_additive] theorem is_monoid_hom.of_mul [monoid α] [group β] (f : α → β) [is_mul_hom f] : is_monoid_hom f := { map_one := mul_self_iff_eq_one.1 $ by rw [← is_mul_hom.map_mul f, one_mul] } namespace is_monoid_hom variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f] /-- The identity map is a monoid homomorphism. -/ @[to_additive] instance id : is_monoid_hom (@id α) := { map_one := rfl } /-- The composite of two monoid homomorphisms is a monoid homomorphism. -/ @[priority 10, to_additive] -- see Note [low priority instance on morphisms] instance comp {γ} [monoid γ] (g : β → γ) [is_monoid_hom g] : is_monoid_hom (g ∘ f) := { map_one := show g _ = 1, by rw [map_one f, map_one g] } end is_monoid_hom namespace is_add_monoid_hom /-- Left multiplication in a ring is an additive monoid morphism. -/ instance is_add_monoid_hom_mul_left {γ : Type*} [semiring γ] (x : γ) : is_add_monoid_hom (λ y : γ, x * y) := { map_zero := mul_zero x, map_add := λ y z, mul_add x y z } /-- Right multiplication in a ring is an additive monoid morphism. -/ instance is_add_monoid_hom_mul_right {γ : Type*} [semiring γ] (x : γ) : is_add_monoid_hom (λ y : γ, y * x) := { map_zero := zero_mul x, map_add := λ y z, add_mul y z x } end is_add_monoid_hom section prio set_option default_priority 100 -- see Note [default priority] /-- Predicate for additive group homomorphism (deprecated -- use bundled `monoid_hom`). -/ class is_add_group_hom [add_group α] [add_group β] (f : α → β) extends is_add_hom f : Prop /-- Predicate for group homomorphisms (deprecated -- use bundled `monoid_hom`). -/ @[to_additive is_add_group_hom] class is_group_hom [group α] [group β] (f : α → β) extends is_mul_hom f : Prop end prio /-- Construct `is_group_hom` from its only hypothesis. The default constructor tries to get `is_mul_hom` from class instances, and this makes some proofs fail. -/ @[to_additive] lemma is_group_hom.mk' [group α] [group β] {f : α → β} (hf : ∀ x y, f (x * y) = f x * f y) : is_group_hom f := { map_mul := hf } namespace is_group_hom variables [group α] [group β] (f : α → β) [is_group_hom f] open is_mul_hom (map_mul) /-- A group homomorphism is a monoid homomorphism. -/ @[priority 100, to_additive to_is_add_monoid_hom] -- see Note [lower instance priority] instance to_is_monoid_hom : is_monoid_hom f := is_monoid_hom.of_mul f /-- A group homomorphism sends 1 to 1. -/ @[to_additive] lemma map_one : f 1 = 1 := is_monoid_hom.map_one f /-- A group homomorphism sends inverses to inverses. -/ @[to_additive] theorem map_inv (a : α) : f a⁻¹ = (f a)⁻¹ := eq_inv_of_mul_eq_one $ by rw [← map_mul f, inv_mul_self, map_one f] /-- The identity is a group homomorphism. -/ @[to_additive] instance id : is_group_hom (@id α) := { } /-- The composition of two group homomomorphisms is a group homomorphism. -/ @[priority 10, to_additive] -- see Note [low priority instance on morphisms] instance comp {γ} [group γ] (g : β → γ) [is_group_hom g] : is_group_hom (g ∘ f) := { } /-- A group homomorphism is injective iff its kernel is trivial. -/ @[to_additive] lemma injective_iff (f : α → β) [is_group_hom f] : function.injective f ↔ (∀ a, f a = 1 → a = 1) := ⟨λ h _, by rw ← is_group_hom.map_one f; exact @h _ _, λ h x y hxy, by rw [← inv_inv (f x), inv_eq_iff_mul_eq_one, ← map_inv f, ← map_mul f] at hxy; simpa using inv_eq_of_mul_eq_one (h _ hxy)⟩ /-- The product of group homomorphisms is a group homomorphism if the target is commutative. -/ @[instance, priority 10, to_additive] -- see Note [low priority instance on morphisms] lemma mul {α β} [group α] [comm_group β] (f g : α → β) [is_group_hom f] [is_group_hom g] : is_group_hom (λa, f a * g a) := { } /-- The inverse of a group homomorphism is a group homomorphism if the target is commutative. -/ @[instance, to_additive] lemma inv {α β} [group α] [comm_group β] (f : α → β) [is_group_hom f] : is_group_hom (λa, (f a)⁻¹) := { } end is_group_hom /-- Inversion is a group homomorphism if the group is commutative. -/ @[instance, to_additive is_add_group_hom] lemma inv.is_group_hom [comm_group α] : is_group_hom (has_inv.inv : α → α) := { map_mul := mul_inv } namespace is_add_group_hom variables [add_group α] [add_group β] (f : α → β) [is_add_group_hom f] /-- Additive group homomorphisms commute with subtraction. -/ lemma map_sub (a b) : f (a - b) = f a - f b := calc f (a + -b) = f a + f (-b) : is_add_hom.map_add f _ _ ... = f a + -f b : by rw [map_neg f] end is_add_group_hom /-- The difference of two additive group homomorphisms is an additive group homomorphism if the target is commutative. -/ @[instance] lemma is_add_group_hom.sub {α β} [add_group α] [add_comm_group β] (f g : α → β) [is_add_group_hom f] [is_add_group_hom g] : is_add_group_hom (λa, f a - g a) := is_add_group_hom.add f (λa, - g a) /-- Bundled add_monoid homomorphisms; use this for bundled add_group homomorphisms too. -/ structure add_monoid_hom (M : Type*) (N : Type*) [add_monoid M] [add_monoid N] := (to_fun : M → N) (map_zero' : to_fun 0 = 0) (map_add' : ∀ x y, to_fun (x + y) = to_fun x + to_fun y) infixr ` →+ `:25 := add_monoid_hom /-- Bundled monoid homomorphisms; use this for bundled group homomorphisms too. -/ @[to_additive add_monoid_hom] structure monoid_hom (M : Type*) (N : Type*) [monoid M] [monoid N] := (to_fun : M → N) (map_one' : to_fun 1 = 1) (map_mul' : ∀ x y, to_fun (x * y) = to_fun x * to_fun y) infixr ` →* `:25 := monoid_hom @[to_additive] instance {M : Type*} {N : Type*} {mM : monoid M} {mN : monoid N} : has_coe_to_fun (M →* N) := ⟨_, monoid_hom.to_fun⟩ namespace monoid_hom variables {M : Type*} {N : Type*} {P : Type*} [mM : monoid M] [mN : monoid N] {mP : monoid P} variables {G : Type*} {H : Type*} [group G] [comm_group H] include mM mN /-- Interpret a map `f : M → N` as a homomorphism `M →* N`. -/ @[to_additive "Interpret a map `f : M → N` as a homomorphism `M →+ N`."] def of (f : M → N) [h : is_monoid_hom f] : M →* N := { to_fun := f, map_one' := h.2, map_mul' := h.1.1 } variables {mM mN mP} @[simp, to_additive] lemma coe_of (f : M → N) [is_monoid_hom f] : ⇑ (monoid_hom.of f) = f := rfl @[to_additive] lemma coe_inj ⦃f g : M →* N⦄ (h : (f : M → N) = g) : f = g := by cases f; cases g; cases h; refl @[ext, to_additive] lemma ext ⦃f g : M →* N⦄ (h : ∀ x, f x = g x) : f = g := coe_inj (funext h) @[to_additive] lemma ext_iff {f g : M →* N} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- If f is a monoid homomorphism then f 1 = 1. -/ @[simp, to_additive] lemma map_one (f : M →* N) : f 1 = 1 := f.map_one' /-- If f is a monoid homomorphism then f (a * b) = f a * f b. -/ @[simp, to_additive] lemma map_mul (f : M →* N) (a b : M) : f (a * b) = f a * f b := f.map_mul' a b @[to_additive is_add_monoid_hom] instance (f : M →* N) : is_monoid_hom (f : M → N) := { map_mul := f.map_mul, map_one := f.map_one } omit mN mM @[to_additive is_add_group_hom] instance (f : G →* H) : is_group_hom (f : G → H) := { map_mul := f.map_mul } /-- The identity map from a monoid to itself. -/ @[to_additive] def id (M : Type*) [monoid M] : M →* M := { to_fun := id, map_one' := rfl, map_mul' := λ _ _, rfl } include mM mN mP /-- Composition of monoid morphisms is a monoid morphism. -/ @[to_additive] def comp (hnp : N →* P) (hmn : M →* N) : M →* P := { to_fun := hnp ∘ hmn, map_one' := by simp, map_mul' := by simp } @[simp, to_additive] lemma comp_apply (g : N →* P) (f : M →* N) (x : M) : g.comp f x = g (f x) := rfl /-- Composition of monoid homomorphisms is associative. -/ @[to_additive] lemma comp_assoc {Q : Type*} [monoid Q] (f : M →* N) (g : N →* P) (h : P →* Q) : (h.comp g).comp f = h.comp (g.comp f) := rfl omit mP variables [mM] [mN] @[to_additive] protected def one : M →* N := { to_fun := λ _, 1, map_one' := rfl, map_mul' := λ _ _, (one_mul 1).symm } @[to_additive] instance : has_one (M →* N) := ⟨monoid_hom.one⟩ @[to_additive] instance : inhabited (M →* N) := ⟨1⟩ omit mM mN /-- The product of two monoid morphisms is a monoid morphism if the target is commutative. -/ @[to_additive] protected def mul {M N} {mM : monoid M} [comm_monoid N] (f g : M →* N) : M →* N := { to_fun := λ m, f m * g m, map_one' := show f 1 * g 1 = 1, by simp, map_mul' := begin intros, show f (x * y) * g (x * y) = f x * g x * (f y * g y), rw [f.map_mul, g.map_mul, ←mul_assoc, ←mul_assoc, mul_right_comm (f x)], end } @[to_additive] instance {M N} {mM : monoid M} [comm_monoid N] : has_mul (M →* N) := ⟨monoid_hom.mul⟩ /-- (M →* N) is a comm_monoid if N is commutative. -/ @[to_additive add_comm_monoid] instance {M N} [monoid M] [comm_monoid N] : comm_monoid (M →* N) := { mul := (*), mul_assoc := by intros; ext; apply mul_assoc, one := 1, one_mul := by intros; ext; apply one_mul, mul_one := by intros; ext; apply mul_one, mul_comm := by intros; ext; apply mul_comm } /-- Group homomorphisms preserve inverse. -/ @[simp, to_additive] theorem map_inv {G H} [group G] [group H] (f : G →* H) (g : G) : f g⁻¹ = (f g)⁻¹ := eq_inv_of_mul_eq_one $ by rw [←f.map_mul, inv_mul_self, f.map_one] /-- Group homomorphisms preserve division. -/ @[simp, to_additive] theorem map_mul_inv {G H} [group G] [group H] (f : G →* H) (g h : G) : f (g * h⁻¹) = (f g) * (f h)⁻¹ := by rw [f.map_mul, f.map_inv] /-- A group homomorphism is injective iff its kernel is trivial. -/ @[to_additive] lemma injective_iff {G H} [group G] [group H] (f : G →* H) : function.injective f ↔ (∀ a, f a = 1 → a = 1) := ⟨λ h _, by rw ← f.map_one; exact @h _ _, λ h x y hxy, by rw [← inv_inv (f x), inv_eq_iff_mul_eq_one, ← f.map_inv, ← f.map_mul] at hxy; simpa using inv_eq_of_mul_eq_one (h _ hxy)⟩ include mM /-- Makes a group homomomorphism from a proof that the map preserves multiplication. -/ @[to_additive] def mk' (f : M → G) (map_mul : ∀ a b : M, f (a * b) = f a * f b) : M →* G := { to_fun := f, map_mul' := map_mul, map_one' := mul_self_iff_eq_one.1 $ by rw [←map_mul, mul_one] } omit mM /-- The inverse of a monoid homomorphism is a monoid homomorphism if the target is a commutative group.-/ @[to_additive] protected def inv {M G} {mM : monoid M} [comm_group G] (f : M →* G) : M →* G := mk' (λ g, (f g)⁻¹) $ λ a b, by rw [←mul_inv, f.map_mul] @[to_additive] instance {M G} [monoid M] [comm_group G] : has_inv (M →* G) := ⟨monoid_hom.inv⟩ /-- (M →* G) is a comm_group if G is a comm_group -/ @[to_additive add_comm_group] instance {M G} [monoid M] [comm_group G] : comm_group (M →* G) := { inv := has_inv.inv, mul_left_inv := by intros; ext; apply mul_left_inv, ..monoid_hom.comm_monoid } end monoid_hom /-- Additive group homomorphisms preserve subtraction. -/ @[simp] theorem add_monoid_hom.map_sub {G H} [add_group G] [add_group H] (f : G →+ H) (g h : G) : f (g - h) = (f g) - (f h) := f.map_add_neg g h
a4dd48a1b40a43a6b07cf19c3c1703522d876e9d
ea97c777e51529c97caac532f9a6bbea417eca7a
/02_logic.lean
2559920aed6c871e3a59501164a33a0d71035753
[]
no_license
gebner/avm2017_tutorial
0fcd023fbcefd6e46384ca4919b90a0f6118368e
3954983cdc8aef0e58e1a5809c0b3e217057ac4c
refs/heads/master
1,624,860,271,990
1,505,717,496,000
1,505,717,496,000
103,569,052
2
0
null
null
null
null
UTF-8
Lean
false
false
2,874
lean
universes u #check true ∧ false ∨ (3 > 1 → 1 < 3) #check 4 = 2+2 #check ∀ n : ℕ, n ≠ 0 → ∃ m, n = m+1 /- Curry-Howard: Propositions as types (A proposition is true if there is a value of the type.) -/ -- Functions are proofs of implication lemma p1 {a b : Prop} : a → b → a := λ ha hb, ha -- Functions are proofs of forall lemma p2 : ∀ n : ℕ, n = n := λ n, rfl -- Conjunction is a product (defined as a structure!) lemma p3 {a b : Prop} (ha : a) (hb : b) : a ∧ b := { left := ha, right := hb } -- A proof of exists is a pair of witness and proof lemma p4 (n : ℕ) : ∃ m, n+1 ≤ m := ⟨n+1, le_refl _⟩ -- Disjunction is a sum type lemma p5 {a b : Prop} (ha : a) : a ∨ b := or.inl ha /- Proofs are "just" expressions; pattern-matching, recursive definitions, etc. just work. -/ lemma p6 {a b : Prop} : a ∨ b → b ∨ a | (or.inl ha) := or.inr ha | (or.inr hb) := or.inl hb lemma p7 {α : Type u} (p q : α → Prop) : (∃ x, p x) → (∃ x, q x) → (∃ x y, p x ∧ q y) | ⟨x, hpx⟩ ⟨y, hpy⟩ := ⟨x, y, hpx, hpy⟩ lemma p8 : ∀ n : ℕ, 0 ≤ n | 0 := le_refl _ | (n+1) := let h1 : n ≥ 0 := p8 n in have h2 : n+1 ≥ n, from nat.le_succ n, show 0 ≤ n+1, from le_trans h1 h2 /- Subtypes restrict types to a subset of values. -/ def positive_nat := { n : ℕ // n > 0 } example : positive_nat := ⟨10, dec_trivial⟩ /- There are two different kinds of "truth values": - Prop: types, erased at runtime - bool: inductive type with values tt/ff, computable -/ #check true ∧ false ∨ true #check tt && ff || tt -- Lean automatically converts ("coerces") bools into Props -- x is coerced into x = tt example : (tt : Prop) := rfl -- We can convert a Prop p into a bool if it is "decidable", -- i.e. there is a function that computes whether p is true or not #eval (∀ x ∈ [1,2,3,4], x^2 < x + 10 : bool) -- decidable is implemented as a typeclass, and you can add your own instances #print instances decidable -- Many definitions in Lean use decidable propositions instead of bool: #eval if ∀ x ∈ [1,2,3,4], x^2 < x + 10 then "ok" else "ko" #check guard $ ∀ x ∈ [1,2,3,4], x^2 < x + 10 #check list.filter (λ x, x^2 < x + 10) /- Since Prop is erased at runtime, we can use classical logic in Prop without any problems. -/ example {p : Prop} : p ∨ ¬ p := classical.em p -- Classical logic implies that all propositions are decidable. -- If we mark `classical.prop_decidable` as a type-class instance, -- then if-then-else works on all propositions. local attribute [instance] classical.prop_decidable -- However we cannot execute definitions that use classical.prop_decidable to -- construct data, such definitions are then marked as "noncomputable". noncomputable def find_zero (f : ℕ → ℕ) : ℕ := if h : ∃ i, f i = 0 then classical.some h else 0
ebef280c5d4aca77ff14ef13ceff81ceba9534cd
7cdf3413c097e5d36492d12cdd07030eb991d394
/world_experiments/world7/level6andseveneighths.lean
da48c0aaac1b2e450065de7b01cf78d9ab481c81
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
1,286
lean
import mynat.definition -- hide import mynat.add -- hide import game.world2.level6andthreequarters -- hide namespace mynat -- hide /- Tactic : intro The `intro h` tactic is very simple. If we're trying to prove $P\implies Q$ with $P$ and $Q$ true/false statements, then `intro h` is Lean's way of saying "Let's assume $P$ is true, and let's call its proof `h`". More formally, the `intro` tactic makes progress if the *goal* is an *implication*. For example, say our local context looks like this: ``` a b : mynat ⊢ a + a = b + b → a + b ``` Then after `intro h` it will look like this: ``` a b : mynat, h : a + a = b + b ⊢ a = b ``` -/ /- # World 2 -- Addition World ## Level 6 and seven eighths: -- `succ_eq_succ_of_eq`. -/ /- Here we will learn the `intro` tactic. We are going to prove something completely obvious: if $a=b$ then $succ(a)=succ(b)$. This is *not* `succ_inj`! This is trivial -- we can just rewrite our proof of `a=b`. But how do we get to that proof? -/ /- Theorem For all naturals $a$ and $b$, $a=b\implies succ(a)=succ(b)$. -/ theorem succ_eq_succ_of_eq {a b : mynat} : a = b → succ(a) = succ(b) := begin [less_leaky] intro h, rw h, refl, end /- Try starting with `intro h,` Now `rw` and `refl` will get you home. -/ end mynat -- hide
2194ad229cd4ab519cd5f0c5806d287af3da2d39
e030b0259b777fedcdf73dd966f3f1556d392178
/library/init/meta/smt/congruence_closure.lean
62824520910a78ec18b485c9c23f132dbf481033
[ "Apache-2.0" ]
permissive
fgdorais/lean
17b46a095b70b21fa0790ce74876658dc5faca06
c3b7c54d7cca7aaa25328f0a5660b6b75fe26055
refs/heads/master
1,611,523,590,686
1,484,412,902,000
1,484,412,902,000
38,489,734
0
0
null
1,435,923,380,000
1,435,923,379,000
null
UTF-8
Lean
false
false
4,952
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.meta.set_get_option_tactics structure cc_config := /- If tt, congruence closure will treat implicit instance arguments as constants. -/ (ignore_instances : bool) /- If tt, congruence closure modulo AC. -/ (ac : bool) /- If ho_fns is (some fns), then full (and more expensive) support for higher-order functions is *only* considered for the functions in fns and local functions. The performance overhead is described in the paper "Congruence Closure in Intensional Type Theory". If ho_fns is none, then full support is provided for *all* constants. -/ (ho_fns : option (list name)) /- If true, then use excluded middle -/ (em : bool) def default_cc_config : cc_config := {ignore_instances := tt, ac := tt, ho_fns := some [], em := tt} /- Congruence closure state -/ meta constant cc_state : Type meta constant cc_state.mk_core : cc_config → cc_state /- Create a congruence closure state object using the hypotheses in the current goal. -/ meta constant cc_state.mk_using_hs_core : cc_config → tactic cc_state meta constant cc_state.next : cc_state → expr → expr meta constant cc_state.roots_core : cc_state → bool → list expr meta constant cc_state.root : cc_state → expr → expr meta constant cc_state.mt : cc_state → expr → nat meta constant cc_state.gmt : cc_state → nat meta constant cc_state.inc_gmt : cc_state → cc_state meta constant cc_state.is_cg_root : cc_state → expr → bool meta constant cc_state.pp_eqc : cc_state → expr → tactic format meta constant cc_state.pp_core : cc_state → bool → tactic format meta constant cc_state.internalize : cc_state → expr → tactic cc_state meta constant cc_state.add : cc_state → expr → tactic cc_state meta constant cc_state.is_eqv : cc_state → expr → expr → tactic bool meta constant cc_state.is_not_eqv : cc_state → expr → expr → tactic bool meta constant cc_state.eqv_proof : cc_state → expr → expr → tactic expr meta constant cc_state.inconsistent : cc_state → bool /- (proof_for cc e) constructs a proof for e if it is equivalent to true in cc_state -/ meta constant cc_state.proof_for : cc_state → expr → tactic expr /- (refutation_for cc e) constructs a proof for (not e) if it is equivalent to false in cc_state -/ meta constant cc_state.refutation_for : cc_state → expr → tactic expr /- If the given state is inconsistent, return a proof for false. Otherwise fail. -/ meta constant cc_state.proof_for_false : cc_state → tactic expr namespace cc_state meta def mk : cc_state := cc_state.mk_core default_cc_config meta def mk_using_hs : tactic cc_state := cc_state.mk_using_hs_core default_cc_config meta def roots (s : cc_state) : list expr := cc_state.roots_core s tt meta def pp (s : cc_state) : tactic format := cc_state.pp_core s tt meta def eqc_of_core (s : cc_state) : expr → expr → list expr → list expr | e f r := let n := s^.next e in if n = f then e::r else eqc_of_core n f (e::r) meta def eqc_of (s : cc_state) (e : expr) : list expr := s^.eqc_of_core e e [] meta def in_singlenton_eqc (s : cc_state) (e : expr) : bool := to_bool (s^.next e = e) meta def eqc_size (s : cc_state) (e : expr) : nat := (s^.eqc_of e)^.length end cc_state open tactic meta def tactic.cc_core (cfg : cc_config) : tactic unit := do intros, s ← cc_state.mk_using_hs_core cfg, t ← target, s ← s^.internalize t, if s^.inconsistent then do { pr ← s^.proof_for_false, mk_app `false.elim [t, pr] >>= exact} else do { tr ← return $ expr.const `true [], b ← s^.is_eqv t tr, if b then do { pr ← s^.eqv_proof t tr, mk_app `of_eq_true [pr] >>= exact } else do { dbg ← get_bool_option `trace.cc.failure ff, if dbg then do { ccf ← s^.pp, msg ← return $ to_fmt "cc tactic failed, equivalence classes: " ++ format.line ++ ccf, fail msg } else do { fail "cc tactic failed" } } } meta def tactic.cc : tactic unit := tactic.cc_core default_cc_config meta def tactic.cc_dbg_core (cfg : cc_config) : tactic unit := save_options $ set_bool_option `trace.cc.failure tt >> tactic.cc_core cfg meta def tactic.cc_dbg : tactic unit := tactic.cc_dbg_core default_cc_config meta def tactic.ac_refl : tactic unit := do (lhs, rhs) ← target >>= match_eq, s ← return $ cc_state.mk, s ← s^.internalize lhs, s ← s^.internalize rhs, b ← s^.is_eqv lhs rhs, if b then do { s^.eqv_proof lhs rhs >>= exact } else do { fail "ac_refl failed" }
976c0bb2adf4183ac476807e7041217021cacafd
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/1026.lean
6b6ee12c782529b5d9f4fbaa9e02f151c6fa5d3d
[ "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
236
lean
def foo (n : Nat) : Nat := if n = 0 then 0 else let x := n - 1 have := match () with | _ => trivial foo x termination_by _ n => n decreasing_by sorry theorem ex : foo 0 = 0 := by unfold foo sorry #check @foo._unfold
1f37285acc093aa935edc851c86fe45e43309c14
94e33a31faa76775069b071adea97e86e218a8ee
/src/order/rel_iso.lean
60c811dfbeef126ad3ce10d025adde97802f03b3
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
25,080
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.group.defs import data.fun_like.basic import logic.embedding import logic.equiv.set import order.rel_classes /-! # Relation homomorphisms, embeddings, isomorphisms This file defines relation homomorphisms, embeddings, isomorphisms and order embeddings and isomorphisms. ## Main declarations * `rel_hom`: Relation homomorphism. A `rel_hom r s` is a function `f : α → β` such that `r a b → s (f a) (f b)`. * `rel_embedding`: Relation embedding. A `rel_embedding r s` is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. * `rel_iso`: Relation isomorphism. A `rel_iso r s` is an equivalence `f : α ≃ β` such that `r a b ↔ s (f a) (f b)`. * `sum_lex_congr`, `prod_lex_congr`: Creates a relation homomorphism between two `sum_lex` or two `prod_lex` from relation homomorphisms between their arguments. ## Notation * `→r`: `rel_hom` * `↪r`: `rel_embedding` * `≃r`: `rel_iso` -/ open function universes u v w variables {α β γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-- A relation homomorphism with respect to a given pair of relations `r` and `s` is a function `f : α → β` such that `r a b → s (f a) (f b)`. -/ @[nolint has_inhabited_instance] structure rel_hom {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) := (to_fun : α → β) (map_rel' : ∀ {a b}, r a b → s (to_fun a) (to_fun b)) infix ` →r `:25 := rel_hom /-- `rel_hom_class F r s` asserts that `F` is a type of functions such that all `f : F` satisfy `r a b → s (f a) (f b)`. The relations `r` and `s` are `out_param`s since figuring them out from a goal is a higher-order matching problem that Lean usually can't do unaided. -/ class rel_hom_class (F : Type*) {α β : out_param $ Type*} (r : out_param $ α → α → Prop) (s : out_param $ β → β → Prop) extends fun_like F α (λ _, β) := (map_rel : ∀ (f : F) {a b}, r a b → s (f a) (f b)) export rel_hom_class (map_rel) -- The free parameters `r` and `s` are `out_param`s so this is not dangerous. attribute [nolint dangerous_instance] rel_hom_class.to_fun_like namespace rel_hom_class variables {F : Type*} lemma map_inf [semilattice_inf α] [linear_order β] [rel_hom_class F ((<) : β → β → Prop) ((<) : α → α → Prop)] (a : F) (m n : β) : a (m ⊓ n) = a m ⊓ a n := (strict_mono.monotone $ λ x y, map_rel a).map_inf m n lemma map_sup [semilattice_sup α] [linear_order β] [rel_hom_class F ((>) : β → β → Prop) ((>) : α → α → Prop)] (a : F) (m n : β) : a (m ⊔ n) = a m ⊔ a n := @map_inf αᵒᵈ βᵒᵈ _ _ _ _ _ _ _ protected theorem is_irrefl [rel_hom_class F r s] (f : F) : ∀ [is_irrefl β s], is_irrefl α r | ⟨H⟩ := ⟨λ a h, H _ (map_rel f h)⟩ protected theorem is_asymm [rel_hom_class F r s] (f : F) : ∀ [is_asymm β s], is_asymm α r | ⟨H⟩ := ⟨λ a b h₁ h₂, H _ _ (map_rel f h₁) (map_rel f h₂)⟩ protected theorem acc [rel_hom_class F r s] (f : F) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (map_rel f h) _ rfl⟩ end protected theorem well_founded [rel_hom_class F r s] (f : F) : ∀ (h : well_founded s), well_founded r | ⟨H⟩ := ⟨λ a, rel_hom_class.acc f _ (H _)⟩ end rel_hom_class namespace rel_hom instance : rel_hom_class (r →r s) r s := { coe := λ o, o.to_fun, coe_injective' := λ f g h, by { cases f, cases g, congr' }, map_rel := map_rel' } /-- Auxiliary instance if `rel_hom_class.to_fun_like.to_has_coe_to_fun` isn't found -/ instance : has_coe_to_fun (r →r s) (λ _, α → β) := ⟨λ o, o.to_fun⟩ initialize_simps_projections rel_hom (to_fun → apply) protected theorem map_rel (f : r →r s) : ∀ {a b}, r a b → s (f a) (f b) := f.map_rel' @[simp] theorem coe_fn_mk (f : α → β) (o) : (@rel_hom.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_fun (f : r →r s) : (f.to_fun : α → β) = f := rfl /-- The map `coe_fn : (r →r s) → (α → β)` is injective. -/ theorem coe_fn_injective : @function.injective (r →r s) (α → β) coe_fn := fun_like.coe_injective @[ext] theorem ext ⦃f g : r →r s⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h theorem ext_iff {f g : r →r s} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff /-- Identity map is a relation homomorphism. -/ @[refl, simps] protected def id (r : α → α → Prop) : r →r r := ⟨λ x, x, λ a b x, x⟩ /-- Composition of two relation homomorphisms is a relation homomorphism. -/ @[trans, simps] protected def comp (g : s →r t) (f : r →r s) : r →r t := ⟨λ x, g (f x), λ a b h, g.2 (f.2 h)⟩ /-- A relation homomorphism is also a relation homomorphism between dual relations. -/ protected def swap (f : r →r s) : swap r →r swap s := ⟨f, λ a b, f.map_rel⟩ /-- A function is a relation homomorphism from the preimage relation of `s` to `s`. -/ def preimage (f : α → β) (s : β → β → Prop) : f ⁻¹'o s →r s := ⟨f, λ a b, id⟩ end rel_hom /-- An increasing function is injective -/ lemma injective_of_increasing (r : α → α → Prop) (s : β → β → Prop) [is_trichotomous α r] [is_irrefl β s] (f : α → β) (hf : ∀ {x y}, r x y → s (f x) (f y)) : injective f := begin intros x y hxy, rcases trichotomous_of r x y with h | h | h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this, exact h, have := hf h, rw hxy at this, exfalso, exact irrefl_of s (f y) this end /-- An increasing function is injective -/ lemma rel_hom.injective_of_increasing [is_trichotomous α r] [is_irrefl β s] (f : r →r s) : injective f := injective_of_increasing r s f (λ x y, f.map_rel) -- TODO: define a `rel_iff_class` so we don't have to do all the `convert` trickery? theorem surjective.well_founded_iff {f : α → β} (hf : surjective f) (o : ∀ {a b}, r a b ↔ s (f a) (f b)) : well_founded r ↔ well_founded s := iff.intro (begin refine rel_hom_class.well_founded (rel_hom.mk _ _ : s →r r), { exact classical.some hf.has_right_inverse }, intros a b h, apply o.2, convert h, iterate 2 { apply classical.some_spec hf.has_right_inverse }, end) (rel_hom_class.well_founded (⟨f, λ _ _, o.1⟩ : r →r s)) /-- A relation embedding with respect to a given pair of relations `r` and `s` is an embedding `f : α ↪ β` such that `r a b ↔ s (f a) (f b)`. -/ structure rel_embedding {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ↪ β := (map_rel_iff' : ∀ {a b}, s (to_embedding a) (to_embedding b) ↔ r a b) infix ` ↪r `:25 := rel_embedding /-- The induced relation on a subtype is an embedding under the natural inclusion. -/ definition subtype.rel_embedding {X : Type*} (r : X → X → Prop) (p : X → Prop) : ((subtype.val : subtype p → X) ⁻¹'o r) ↪r r := ⟨embedding.subtype p, λ x y, iff.rfl⟩ theorem preimage_equivalence {α β} (f : α → β) {s : β → β → Prop} (hs : equivalence s) : equivalence (f ⁻¹'o s) := ⟨λ a, hs.1 _, λ a b h, hs.2.1 h, λ a b c h₁ h₂, hs.2.2 h₁ h₂⟩ namespace rel_embedding /-- A relation embedding is also a relation homomorphism -/ def to_rel_hom (f : r ↪r s) : (r →r s) := { to_fun := f.to_embedding.to_fun, map_rel' := λ x y, (map_rel_iff' f).mpr } instance : has_coe (r ↪r s) (r →r s) := ⟨to_rel_hom⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ↪r s) (λ _, α → β) := ⟨λ o, o.to_embedding⟩ -- TODO: define and instantiate a `rel_embedding_class` when `embedding_like` is defined instance : rel_hom_class (r ↪r s) r s := { coe := coe_fn, coe_injective' := λ f g h, by { rcases f with ⟨⟨⟩⟩, rcases g with ⟨⟨⟩⟩, congr' }, map_rel := λ f a b, iff.mpr (map_rel_iff' f) } /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : r ↪r s) : α → β := h initialize_simps_projections rel_embedding (to_embedding_to_fun → apply, -to_embedding) @[simp] lemma to_rel_hom_eq_coe (f : r ↪r s) : f.to_rel_hom = f := rfl @[simp] lemma coe_coe_fn (f : r ↪r s) : ((f : r →r s) : α → β) = f := rfl theorem injective (f : r ↪r s) : injective f := f.inj' theorem map_rel_iff (f : r ↪r s) : ∀ {a b}, s (f a) (f b) ↔ r a b := f.map_rel_iff' @[simp] theorem coe_fn_mk (f : α ↪ β) (o) : (@rel_embedding.mk _ _ r s f o : α → β) = f := rfl @[simp] theorem coe_fn_to_embedding (f : r ↪r s) : (f.to_embedding : α → β) = f := rfl /-- The map `coe_fn : (r ↪r s) → (α → β)` is injective. -/ theorem coe_fn_injective : @function.injective (r ↪r s) (α → β) coe_fn := fun_like.coe_injective @[ext] theorem ext ⦃f g : r ↪r s⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext _ _ h theorem ext_iff {f g : r ↪r s} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff /-- Identity map is a relation embedding. -/ @[refl, simps] protected def refl (r : α → α → Prop) : r ↪r r := ⟨embedding.refl _, λ a b, iff.rfl⟩ /-- Composition of two relation embeddings is a relation embedding. -/ @[trans] protected def trans (f : r ↪r s) (g : s ↪r t) : r ↪r t := ⟨f.1.trans g.1, λ a b, by simp [f.map_rel_iff, g.map_rel_iff]⟩ instance (r : α → α → Prop) : inhabited (r ↪r r) := ⟨rel_embedding.refl _⟩ theorem trans_apply (f : r ↪r s) (g : s ↪r t) (a : α) : (f.trans g) a = g (f a) := rfl @[simp] theorem coe_trans (f : r ↪r s) (g : s ↪r t) : ⇑(f.trans g) = g ∘ f := rfl /-- A relation embedding is also a relation embedding between dual relations. -/ protected def swap (f : r ↪r s) : swap r ↪r swap s := ⟨f.to_embedding, λ a b, f.map_rel_iff⟩ /-- If `f` is injective, then it is a relation embedding from the preimage relation of `s` to `s`. -/ def preimage (f : α ↪ β) (s : β → β → Prop) : f ⁻¹'o s ↪r s := ⟨f, λ a b, iff.rfl⟩ theorem eq_preimage (f : r ↪r s) : r = f ⁻¹'o s := by { ext a b, exact f.map_rel_iff.symm } protected theorem is_irrefl (f : r ↪r s) [is_irrefl β s] : is_irrefl α r := ⟨λ a, mt f.map_rel_iff.2 (irrefl (f a))⟩ protected theorem is_refl (f : r ↪r s) [is_refl β s] : is_refl α r := ⟨λ a, f.map_rel_iff.1 $ refl _⟩ protected theorem is_symm (f : r ↪r s) [is_symm β s] : is_symm α r := ⟨λ a b, imp_imp_imp f.map_rel_iff.2 f.map_rel_iff.1 symm⟩ protected theorem is_asymm (f : r ↪r s) [is_asymm β s] : is_asymm α r := ⟨λ a b h₁ h₂, asymm (f.map_rel_iff.2 h₁) (f.map_rel_iff.2 h₂)⟩ protected theorem is_antisymm : ∀ (f : r ↪r s) [is_antisymm β s], is_antisymm α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b h₁ h₂, f.inj' (H _ _ (o.2 h₁) (o.2 h₂))⟩ protected theorem is_trans : ∀ (f : r ↪r s) [is_trans β s], is_trans α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b c h₁ h₂, o.1 (H _ _ _ (o.2 h₁) (o.2 h₂))⟩ protected theorem is_total : ∀ (f : r ↪r s) [is_total β s], is_total α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o o).1 (H _ _)⟩ protected theorem is_preorder : ∀ (f : r ↪r s) [is_preorder β s], is_preorder α r | f H := by exactI {..f.is_refl, ..f.is_trans} protected theorem is_partial_order : ∀ (f : r ↪r s) [is_partial_order β s], is_partial_order α r | f H := by exactI {..f.is_preorder, ..f.is_antisymm} protected theorem is_linear_order : ∀ (f : r ↪r s) [is_linear_order β s], is_linear_order α r | f H := by exactI {..f.is_partial_order, ..f.is_total} protected theorem is_strict_order : ∀ (f : r ↪r s) [is_strict_order β s], is_strict_order α r | f H := by exactI {..f.is_irrefl, ..f.is_trans} protected theorem is_trichotomous : ∀ (f : r ↪r s) [is_trichotomous β s], is_trichotomous α r | ⟨f, o⟩ ⟨H⟩ := ⟨λ a b, (or_congr o (or_congr f.inj'.eq_iff o)).1 (H _ _)⟩ protected theorem is_strict_total_order' : ∀ (f : r ↪r s) [is_strict_total_order' β s], is_strict_total_order' α r | f H := by exactI {..f.is_trichotomous, ..f.is_strict_order} protected theorem acc (f : r ↪r s) (a : α) : acc s (f a) → acc r a := begin generalize h : f a = b, intro ac, induction ac with _ H IH generalizing a, subst h, exact ⟨_, λ a' h, IH (f a') (f.map_rel_iff.2 h) _ rfl⟩ end protected theorem well_founded : ∀ (f : r ↪r s) (h : well_founded s), well_founded r | f ⟨H⟩ := ⟨λ a, f.acc _ (H _)⟩ protected theorem is_well_order : ∀ (f : r ↪r s) [is_well_order β s], is_well_order α r | f H := by exactI {wf := f.well_founded H.wf, ..f.is_strict_total_order'} /-- To define an relation embedding from an antisymmetric relation `r` to a reflexive relation `s` it suffices to give a function together with a proof that it satisfies `s (f a) (f b) ↔ r a b`. -/ def of_map_rel_iff (f : α → β) [is_antisymm α r] [is_refl β s] (hf : ∀ a b, s (f a) (f b) ↔ r a b) : r ↪r s := { to_fun := f, inj' := λ x y h, antisymm ((hf _ _).1 (h ▸ refl _)) ((hf _ _).1 (h ▸ refl _)), map_rel_iff' := hf } @[simp] lemma of_map_rel_iff_coe (f : α → β) [is_antisymm α r] [is_refl β s] (hf : ∀ a b, s (f a) (f b) ↔ r a b) : ⇑(of_map_rel_iff f hf : r ↪r s) = f := rfl /-- It suffices to prove `f` is monotone between strict relations to show it is a relation embedding. -/ def of_monotone [is_trichotomous α r] [is_asymm β s] (f : α → β) (H : ∀ a b, r a b → s (f a) (f b)) : r ↪r s := begin haveI := @is_asymm.is_irrefl β s _, refine ⟨⟨f, λ a b e, _⟩, λ a b, ⟨λ h, _, H _ _⟩⟩, { refine ((@trichotomous _ r _ a b).resolve_left _).resolve_right _; exact λ h, @irrefl _ s _ _ (by simpa [e] using H _ _ h) }, { refine (@trichotomous _ r _ a b).resolve_right (or.rec (λ e, _) (λ h', _)), { subst e, exact irrefl _ h }, { exact asymm (H _ _ h') h } } end @[simp] theorem of_monotone_coe [is_trichotomous α r] [is_asymm β s] (f : α → β) (H) : (@of_monotone _ _ r s _ _ f H : α → β) = f := rfl /-- A relation embedding from an empty type. -/ def of_is_empty (r : α → α → Prop) (s : β → β → Prop) [is_empty α] : r ↪r s := ⟨embedding.of_is_empty, is_empty_elim⟩ end rel_embedding /-- A relation isomorphism is an equivalence that is also a relation embedding. -/ structure rel_iso {α β : Type*} (r : α → α → Prop) (s : β → β → Prop) extends α ≃ β := (map_rel_iff' : ∀ {a b}, s (to_equiv a) (to_equiv b) ↔ r a b) infix ` ≃r `:25 := rel_iso namespace rel_iso /-- Convert an `rel_iso` to an `rel_embedding`. This function is also available as a coercion but often it is easier to write `f.to_rel_embedding` than to write explicitly `r` and `s` in the target type. -/ def to_rel_embedding (f : r ≃r s) : r ↪r s := ⟨f.to_equiv.to_embedding, f.map_rel_iff'⟩ theorem to_equiv_injective : injective (to_equiv : (r ≃r s) → α ≃ β) | ⟨e₁, o₁⟩ ⟨e₂, o₂⟩ h := by { congr, exact h } instance : has_coe (r ≃r s) (r ↪r s) := ⟨to_rel_embedding⟩ -- see Note [function coercion] instance : has_coe_to_fun (r ≃r s) (λ _, α → β) := ⟨λ f, f⟩ -- TODO: define and instantiate a `rel_iso_class` when `equiv_like` is defined instance : rel_hom_class (r ≃r s) r s := { coe := coe_fn, coe_injective' := equiv.coe_fn_injective.comp to_equiv_injective, map_rel := λ f a b, iff.mpr (map_rel_iff' f) } @[simp] lemma to_rel_embedding_eq_coe (f : r ≃r s) : f.to_rel_embedding = f := rfl @[simp] lemma coe_coe_fn (f : r ≃r s) : ((f : r ↪r s) : α → β) = f := rfl theorem map_rel_iff (f : r ≃r s) : ∀ {a b}, s (f a) (f b) ↔ r a b := f.map_rel_iff' @[simp] theorem coe_fn_mk (f : α ≃ β) (o : ∀ ⦃a b⦄, s (f a) (f b) ↔ r a b) : (rel_iso.mk f o : α → β) = f := rfl @[simp] theorem coe_fn_to_equiv (f : r ≃r s) : (f.to_equiv : α → β) = f := rfl /-- The map `coe_fn : (r ≃r s) → (α → β)` is injective. Lean fails to parse `function.injective (λ e : r ≃r s, (e : α → β))`, so we use a trick to say the same. -/ theorem coe_fn_injective : @function.injective (r ≃r s) (α → β) coe_fn := fun_like.coe_injective @[ext] theorem ext ⦃f g : r ≃r s⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h theorem ext_iff {f g : r ≃r s} : f = g ↔ ∀ x, f x = g x := fun_like.ext_iff /-- Inverse map of a relation isomorphism is a relation isomorphism. -/ @[symm] protected def symm (f : r ≃r s) : s ≃r r := ⟨f.to_equiv.symm, λ a b, by erw [← f.map_rel_iff, f.1.apply_symm_apply, f.1.apply_symm_apply]⟩ /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def simps.apply (h : r ≃r s) : α → β := h /-- See Note [custom simps projection]. -/ def simps.symm_apply (h : r ≃r s) : β → α := h.symm initialize_simps_projections rel_iso (to_equiv_to_fun → apply, to_equiv_inv_fun → symm_apply, -to_equiv) /-- Identity map is a relation isomorphism. -/ @[refl, simps apply] protected def refl (r : α → α → Prop) : r ≃r r := ⟨equiv.refl _, λ a b, iff.rfl⟩ /-- Composition of two relation isomorphisms is a relation isomorphism. -/ @[trans, simps apply] protected def trans (f₁ : r ≃r s) (f₂ : s ≃r t) : r ≃r t := ⟨f₁.to_equiv.trans f₂.to_equiv, λ a b, f₂.map_rel_iff.trans f₁.map_rel_iff⟩ instance (r : α → α → Prop) : inhabited (r ≃r r) := ⟨rel_iso.refl _⟩ @[simp] lemma default_def (r : α → α → Prop) : default = rel_iso.refl r := rfl /-- A relation isomorphism between equal relations on equal types. -/ @[simps to_equiv apply] protected def cast {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} (h₁ : α = β) (h₂ : r == s) : r ≃r s := ⟨equiv.cast h₁, λ a b, by { subst h₁, rw eq_of_heq h₂, refl }⟩ @[simp] protected theorem cast_symm {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} (h₁ : α = β) (h₂ : r == s) : (rel_iso.cast h₁ h₂).symm = rel_iso.cast h₁.symm h₂.symm := rfl @[simp] protected theorem cast_refl {α : Type u} {r : α → α → Prop} (h₁ : α = α := rfl) (h₂ : r == r := heq.rfl) : rel_iso.cast h₁ h₂ = rel_iso.refl r := rfl @[simp] protected theorem cast_trans {α β γ : Type u} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} (h₁ : α = β) (h₁' : β = γ) (h₂ : r == s) (h₂' : s == t): (rel_iso.cast h₁ h₂).trans (rel_iso.cast h₁' h₂') = rel_iso.cast (h₁.trans h₁') (h₂.trans h₂') := ext $ λ x, by { subst h₁, refl } /-- a relation isomorphism is also a relation isomorphism between dual relations. -/ protected def swap (f : r ≃r s) : (swap r) ≃r (swap s) := ⟨f.to_equiv, λ _ _, f.map_rel_iff⟩ @[simp] theorem coe_fn_symm_mk (f o) : ((@rel_iso.mk _ _ r s f o).symm : β → α) = f.symm := rfl @[simp] theorem apply_symm_apply (e : r ≃r s) (x : β) : e (e.symm x) = x := e.to_equiv.apply_symm_apply x @[simp] theorem symm_apply_apply (e : r ≃r s) (x : α) : e.symm (e x) = x := e.to_equiv.symm_apply_apply x theorem rel_symm_apply (e : r ≃r s) {x y} : r x (e.symm y) ↔ s (e x) y := by rw [← e.map_rel_iff, e.apply_symm_apply] theorem symm_apply_rel (e : r ≃r s) {x y} : r (e.symm x) y ↔ s x (e y) := by rw [← e.map_rel_iff, e.apply_symm_apply] protected lemma bijective (e : r ≃r s) : bijective e := e.to_equiv.bijective protected lemma injective (e : r ≃r s) : injective e := e.to_equiv.injective protected lemma surjective (e : r ≃r s) : surjective e := e.to_equiv.surjective @[simp] lemma range_eq (e : r ≃r s) : set.range e = set.univ := e.surjective.range_eq @[simp] lemma eq_iff_eq (f : r ≃r s) {a b} : f a = f b ↔ a = b := f.injective.eq_iff /-- Any equivalence lifts to a relation isomorphism between `s` and its preimage. -/ protected def preimage (f : α ≃ β) (s : β → β → Prop) : f ⁻¹'o s ≃r s := ⟨f, λ a b, iff.rfl⟩ instance is_well_order.preimage {α : Type u} (r : α → α → Prop) [is_well_order α r] (f : β ≃ α) : is_well_order β (f ⁻¹'o r) := @rel_embedding.is_well_order _ _ (f ⁻¹'o r) r (rel_iso.preimage f r) _ instance is_well_order.ulift {α : Type u} (r : α → α → Prop) [is_well_order α r] : is_well_order (ulift α) (ulift.down ⁻¹'o r) := is_well_order.preimage r equiv.ulift /-- A surjective relation embedding is a relation isomorphism. -/ @[simps apply] noncomputable def of_surjective (f : r ↪r s) (H : surjective f) : r ≃r s := ⟨equiv.of_bijective f ⟨f.injective, H⟩, λ a b, f.map_rel_iff⟩ /-- Given relation isomorphisms `r₁ ≃r s₁` and `r₂ ≃r s₂`, construct a relation isomorphism for the lexicographic orders on the sum. -/ def sum_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @rel_iso α₁ β₁ r₁ s₁) (e₂ : @rel_iso α₂ β₂ r₂ s₂) : sum.lex r₁ r₂ ≃r sum.lex s₁ s₂ := ⟨equiv.sum_congr e₁.to_equiv e₂.to_equiv, λ a b, by cases e₁ with f hf; cases e₂ with g hg; cases a; cases b; simp [hf, hg]⟩ /-- Given relation isomorphisms `r₁ ≃r s₁` and `r₂ ≃r s₂`, construct a relation isomorphism for the lexicographic orders on the product. -/ def prod_lex_congr {α₁ α₂ β₁ β₂ r₁ r₂ s₁ s₂} (e₁ : @rel_iso α₁ β₁ r₁ s₁) (e₂ : @rel_iso α₂ β₂ r₂ s₂) : prod.lex r₁ r₂ ≃r prod.lex s₁ s₂ := ⟨equiv.prod_congr e₁.to_equiv e₂.to_equiv, λ a b, by simp [prod.lex_def, e₁.map_rel_iff, e₂.map_rel_iff]⟩ instance : group (r ≃r r) := { one := rel_iso.refl r, mul := λ f₁ f₂, f₂.trans f₁, inv := rel_iso.symm, mul_assoc := λ f₁ f₂ f₃, rfl, one_mul := λ f, ext $ λ _, rfl, mul_one := λ f, ext $ λ _, rfl, mul_left_inv := λ f, ext f.symm_apply_apply } @[simp] lemma coe_one : ⇑(1 : r ≃r r) = id := rfl @[simp] lemma coe_mul (e₁ e₂ : r ≃r r) : ⇑(e₁ * e₂) = e₁ ∘ e₂ := rfl lemma mul_apply (e₁ e₂ : r ≃r r) (x : α) : (e₁ * e₂) x = e₁ (e₂ x) := rfl @[simp] lemma inv_apply_self (e : r ≃r r) (x) : e⁻¹ (e x) = x := e.symm_apply_apply x @[simp] lemma apply_inv_self (e : r ≃r r) (x) : e (e⁻¹ x) = x := e.apply_symm_apply x /-- Two relations on empty types are isomorphic. -/ def rel_iso_of_is_empty (r : α → α → Prop) (s : β → β → Prop) [is_empty α] [is_empty β] : r ≃r s := ⟨equiv.equiv_of_is_empty α β, is_empty_elim⟩ /-- Two irreflexive relations on a unique type are isomorphic. -/ def rel_iso_of_unique_of_irrefl (r : α → α → Prop) (s : β → β → Prop) [is_irrefl α r] [is_irrefl β s] [unique α] [unique β] : r ≃r s := ⟨equiv.equiv_of_unique α β, λ x y, by simp [not_rel_of_subsingleton r, not_rel_of_subsingleton s]⟩ /-- Two reflexive relations on a unique type are isomorphic. -/ def rel_iso_of_unique_of_refl (r : α → α → Prop) (s : β → β → Prop) [is_refl α r] [is_refl β s] [unique α] [unique β] : r ≃r s := ⟨equiv.equiv_of_unique α β, λ x y, by simp [rel_of_subsingleton r, rel_of_subsingleton s]⟩ end rel_iso /-- `subrel r p` is the inherited relation on a subset. -/ def subrel (r : α → α → Prop) (p : set α) : p → p → Prop := (coe : p → α) ⁻¹'o r @[simp] theorem subrel_val (r : α → α → Prop) (p : set α) {a b} : subrel r p a b ↔ r a.1 b.1 := iff.rfl namespace subrel /-- The relation embedding from the inherited relation on a subset. -/ protected def rel_embedding (r : α → α → Prop) (p : set α) : subrel r p ↪r r := ⟨embedding.subtype _, λ a b, iff.rfl⟩ @[simp] theorem rel_embedding_apply (r : α → α → Prop) (p a) : subrel.rel_embedding r p a = a.1 := rfl instance (r : α → α → Prop) [is_well_order α r] (p : set α) : is_well_order p (subrel r p) := rel_embedding.is_well_order (subrel.rel_embedding r p) instance (r : α → α → Prop) [is_refl α r] (p : set α) : is_refl p (subrel r p) := ⟨λ x, @is_refl.refl α r _ x⟩ instance (r : α → α → Prop) [is_symm α r] (p : set α) : is_symm p (subrel r p) := ⟨λ x y, @is_symm.symm α r _ x y⟩ instance (r : α → α → Prop) [is_trans α r] (p : set α) : is_trans p (subrel r p) := ⟨λ x y z, @is_trans.trans α r _ x y z⟩ instance (r : α → α → Prop) [is_irrefl α r] (p : set α) : is_irrefl p (subrel r p) := ⟨λ x, @is_irrefl.irrefl α r _ x⟩ end subrel /-- Restrict the codomain of a relation embedding. -/ def rel_embedding.cod_restrict (p : set β) (f : r ↪r s) (H : ∀ a, f a ∈ p) : r ↪r subrel s p := ⟨f.to_embedding.cod_restrict p H, f.map_rel_iff'⟩ @[simp] theorem rel_embedding.cod_restrict_apply (p) (f : r ↪r s) (H a) : rel_embedding.cod_restrict p f H a = ⟨f a, H a⟩ := rfl
2be9c66ef9a1a2e5f7e24dde61509907ec3664eb
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/group_theory/free_group_auto.lean
2d2983313502671bcf6de6e02f0772ec857e3c36
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
28,396
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 Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.fintype.basic import Mathlib.group_theory.subgroup import Mathlib.PostPort universes u v u_1 w u_2 namespace Mathlib namespace free_group /-- Reduction step: `w * x * x⁻¹ * v ~> w * v` -/ inductive red.step {α : Type u} : List (α × Bool) → List (α × Bool) → Prop where | bnot : ∀ {L₁ L₂ : List (α × Bool)} {x : α} {b : Bool}, red.step (L₁ ++ (x, b) :: (x, !b) :: L₂) (L₁ ++ L₂) /-- Reflexive-transitive closure of red.step -/ def red {α : Type u} : List (α × Bool) → List (α × Bool) → Prop := relation.refl_trans_gen sorry theorem red.refl {α : Type u} {L : List (α × Bool)} : red L L := relation.refl_trans_gen.refl theorem red.trans {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {L₃ : List (α × Bool)} : red L₁ L₂ → red L₂ L₃ → red L₁ L₃ := relation.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 {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} : step L₁ L₂ → list.length L₂ + bit0 1 = list.length L₁ := sorry @[simp] theorem step.bnot_rev {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {x : α} {b : Bool} : step (L₁ ++ (x, !b) :: (x, b) :: L₂) (L₁ ++ L₂) := bool.cases_on b step.bnot step.bnot @[simp] theorem step.cons_bnot {α : Type u} {L : List (α × Bool)} {x : α} {b : Bool} : step ((x, b) :: (x, !b) :: L) L := step.bnot @[simp] theorem step.cons_bnot_rev {α : Type u} {L : List (α × Bool)} {x : α} {b : Bool} : step ((x, !b) :: (x, b) :: L) L := step.bnot_rev theorem step.append_left {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {L₃ : List (α × Bool)} : step L₂ L₃ → step (L₁ ++ L₂) (L₁ ++ L₃) := sorry theorem step.cons {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {x : α × Bool} (H : step L₁ L₂) : step (x :: L₁) (x :: L₂) := step.append_left H theorem step.append_right {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {L₃ : List (α × Bool)} : step L₁ L₂ → step (L₁ ++ L₃) (L₂ ++ L₃) := sorry theorem not_step_nil {α : Type u} {L : List (α × Bool)} : ¬step [] L := sorry theorem step.cons_left_iff {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {a : α} {b : Bool} : step ((a, b) :: L₁) L₂ ↔ (∃ (L : List (α × Bool)), step L₁ L ∧ L₂ = (a, b) :: L) ∨ L₁ = (a, !b) :: L₂ := sorry theorem not_step_singleton {α : Type u} {L : List (α × Bool)} {p : α × Bool} : ¬step [p] L := sorry theorem step.cons_cons_iff {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {p : α × Bool} : step (p :: L₁) (p :: L₂) ↔ step L₁ L₂ := sorry theorem step.append_left_iff {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} (L : List (α × Bool)) : step (L ++ L₁) (L ++ L₂) ↔ step L₁ L₂ := sorry theorem step.diamond {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {L₃ : List (α × Bool)} {L₄ : List (α × Bool)} : step L₁ L₃ → step L₂ L₄ → L₁ = L₂ → L₃ = L₄ ∨ ∃ (L₅ : List (α × Bool)), step L₃ L₅ ∧ step L₄ L₅ := sorry theorem step.to_red {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} : step L₁ L₂ → red L₁ L₂ := relation.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 {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {L₃ : List (α × Bool)} : red L₁ L₂ → red L₁ L₃ → relation.join red L₂ L₃ := sorry theorem cons_cons {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {p : α × Bool} : red L₁ L₂ → red (p :: L₁) (p :: L₂) := relation.refl_trans_gen_lift (List.cons p) fun (a b : List (α × Bool)) => step.cons theorem cons_cons_iff {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} (p : α × Bool) : red (p :: L₁) (p :: L₂) ↔ red L₁ L₂ := sorry theorem append_append_left_iff {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} (L : List (α × Bool)) : red (L ++ L₁) (L ++ L₂) ↔ red L₁ L₂ := sorry theorem append_append {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {L₃ : List (α × Bool)} {L₄ : List (α × Bool)} (h₁ : red L₁ L₃) (h₂ : red L₂ L₄) : red (L₁ ++ L₂) (L₃ ++ L₄) := sorry theorem to_append_iff {α : Type u} {L : List (α × Bool)} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} : red L (L₁ ++ L₂) ↔ ∃ (L₃ : List (α × Bool)), ∃ (L₄ : List (α × Bool)), L = L₃ ++ L₄ ∧ red L₃ L₁ ∧ red L₄ L₂ := sorry /-- The empty word `[]` only reduces to itself. -/ theorem nil_iff {α : Type u} {L : List (α × Bool)} : red [] L ↔ L = [] := relation.refl_trans_gen_iff_eq fun (l : List (α × Bool)) => not_step_nil /-- A letter only reduces to itself. -/ theorem singleton_iff {α : Type u} {L₁ : List (α × Bool)} {x : α × Bool} : red [x] L₁ ↔ L₁ = [x] := relation.refl_trans_gen_iff_eq fun (l : List (α × Bool)) => 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 {α : Type u} {L : List (α × Bool)} {x : α} {b : Bool} : red ((x, b) :: L) [] ↔ red L [(x, !b)] := sorry theorem red_iff_irreducible {α : Type u} {L : List (α × Bool)} {x1 : α} {b1 : Bool} {x2 : α} {b2 : Bool} (h : (x1, b1) ≠ (x2, b2)) : red [(x1, !b1), (x2, b2)] L ↔ L = [(x1, !b1), (x2, b2)] := sorry /-- 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 {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {x1 : α} {b1 : Bool} {x2 : α} {b2 : Bool} (H1 : (x1, b1) ≠ (x2, b2)) (H2 : red ((x1, b1) :: L₁) ((x2, b2) :: L₂)) : red L₁ ((x1, !b1) :: (x2, b2) :: L₂) := sorry theorem step.sublist {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} (H : step L₁ L₂) : L₂ <+ L₁ := sorry /-- If `w₁ w₂` are words such that `w₁` reduces to `w₂`, then `w₂` is a sublist of `w₁`. -/ theorem sublist {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} : red L₁ L₂ → L₂ <+ L₁ := relation.refl_trans_gen_of_transitive_reflexive (fun (l : List (α × Bool)) => list.sublist.refl l) (fun (a b c : List (α × Bool)) (hab : b <+ a) (hbc : c <+ b) => list.sublist.trans hbc hab) fun (a b : List (α × Bool)) => step.sublist theorem sizeof_of_step {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} : step L₁ L₂ → list.sizeof L₂ < list.sizeof L₁ := sorry theorem length {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} (h : red L₁ L₂) : ∃ (n : ℕ), list.length L₁ = list.length L₂ + bit0 1 * n := sorry theorem antisymm {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} (h₁₂ : red L₁ L₂) : red L₂ L₁ → L₁ = L₂ := sorry end red theorem equivalence_join_red {α : Type u} : equivalence (relation.join red) := sorry theorem join_red_of_step {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} (h : red.step L₁ L₂) : relation.join red L₁ L₂ := relation.join_of_single relation.reflexive_refl_trans_gen (red.step.to_red h) theorem eqv_gen_step_iff_join_red {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} : eqv_gen red.step L₁ L₂ ↔ relation.join red L₁ L₂ := sorry 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) := Quot sorry namespace free_group /-- The canonical map from `list (α × bool)` to the free group on `α`. -/ def mk {α : Type u} (L : List (α × Bool)) : free_group α := Quot.mk red.step L @[simp] theorem quot_mk_eq_mk {α : Type u} {L : List (α × Bool)} : Quot.mk red.step L = mk L := rfl @[simp] theorem quot_lift_mk {α : Type u} {L : List (α × Bool)} (β : Type v) (f : List (α × Bool) → β) (H : ∀ (L₁ L₂ : List (α × Bool)), red.step L₁ L₂ → f L₁ = f L₂) : Quot.lift f H (mk L) = f L := rfl @[simp] theorem quot_lift_on_mk {α : Type u} {L : List (α × Bool)} (β : Type v) (f : List (α × Bool) → β) (H : ∀ (L₁ L₂ : List (α × Bool)), red.step L₁ L₂ → f L₁ = f L₂) : quot.lift_on (mk L) f H = f L := rfl protected instance has_one {α : Type u} : HasOne (free_group α) := { one := mk [] } theorem one_eq_mk {α : Type u} : 1 = mk [] := rfl protected instance inhabited {α : Type u} : Inhabited (free_group α) := { default := 1 } protected instance has_mul {α : Type u} : Mul (free_group α) := { mul := fun (x y : free_group α) => quot.lift_on x (fun (L₁ : List (α × Bool)) => quot.lift_on y (fun (L₂ : List (α × Bool)) => mk (L₁ ++ L₂)) sorry) sorry } @[simp] theorem mul_mk {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} : mk L₁ * mk L₂ = mk (L₁ ++ L₂) := rfl protected instance has_inv {α : Type u} : has_inv (free_group α) := has_inv.mk fun (x : free_group α) => quot.lift_on x (fun (L : List (α × Bool)) => mk (list.reverse (list.map (fun (x : α × Bool) => (prod.fst x, !prod.snd x)) L))) sorry @[simp] theorem inv_mk {α : Type u} {L : List (α × Bool)} : mk L⁻¹ = mk (list.reverse (list.map (fun (x : α × Bool) => (prod.fst x, !prod.snd x)) L)) := rfl protected instance group {α : Type u} : group (free_group α) := group.mk Mul.mul sorry 1 sorry sorry has_inv.inv (div_inv_monoid.div._default Mul.mul sorry 1 sorry sorry has_inv.inv) sorry /-- `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 {α : Type u} (x : α) : free_group α := mk [(x, tt)] theorem red.exact {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} : mk L₁ = mk L₂ ↔ relation.join red L₁ L₂ := iff.trans { mp := quot.exact red.step, mpr := quot.eqv_gen_sound } eqv_gen_step_iff_join_red /-- The canonical injection from the type to the free group is an injection. -/ theorem of_injective {α : Type u} : function.injective of := sorry /-- Given `f : α → β` with `β` a group, the canonical map `list (α × bool) → β` -/ def to_group.aux {α : Type u} {β : Type v} [group β] (f : α → β) : List (α × Bool) → β := fun (L : List (α × Bool)) => list.prod (list.map (fun (x : α × Bool) => cond (prod.snd x) (f (prod.fst x)) (f (prod.fst x)⁻¹)) L) theorem red.step.to_group {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {β : Type v} [group β] {f : α → β} (H : red.step L₁ L₂) : to_group.aux f L₁ = to_group.aux f L₂ := sorry /-- If `β` is a group, then any function from `α` to `β` extends uniquely to a group homomorphism from the free group over `α` to `β`. Note that this is the bare function; the group homomorphism is `to_group`. -/ def to_group.to_fun {α : Type u} {β : Type v} [group β] (f : α → β) : free_group α → β := Quot.lift (to_group.aux f) sorry /-- If `β` is a group, then any function from `α` to `β` extends uniquely to a group homomorphism from the free group over `α` to `β` -/ def to_group {α : Type u} {β : Type v} [group β] (f : α → β) : free_group α →* β := monoid_hom.mk' sorry sorry @[simp] theorem to_group.mk {α : Type u} {L : List (α × Bool)} {β : Type v} [group β] {f : α → β} : coe_fn (to_group f) (mk L) = list.prod (list.map (fun (x : α × Bool) => cond (prod.snd x) (f (prod.fst x)) (f (prod.fst x)⁻¹)) L) := rfl @[simp] theorem to_group.of {α : Type u} {β : Type v} [group β] {f : α → β} {x : α} : coe_fn (to_group f) (of x) = f x := one_mul ((fun (x : α × Bool) => cond (prod.snd x) (f (prod.fst x)) (f (prod.fst x)⁻¹)) (x, tt)) protected instance to_group.is_group_hom {α : Type u} {β : Type v} [group β] {f : α → β} : is_group_hom ⇑(to_group f) := is_group_hom.mk @[simp] theorem to_group.mul {α : Type u} {β : Type v} [group β] {f : α → β} {x : free_group α} {y : free_group α} : coe_fn (to_group f) (x * y) = coe_fn (to_group f) x * coe_fn (to_group f) y := is_mul_hom.map_mul (⇑(to_group f)) x y @[simp] theorem to_group.one {α : Type u} {β : Type v} [group β] {f : α → β} : coe_fn (to_group f) 1 = 1 := is_group_hom.map_one ⇑(to_group f) @[simp] theorem to_group.inv {α : Type u} {β : Type v} [group β] {f : α → β} {x : free_group α} : coe_fn (to_group f) (x⁻¹) = (coe_fn (to_group f) x⁻¹) := is_group_hom.map_inv (⇑(to_group f)) x theorem to_group.unique {α : Type u} {β : Type v} [group β] {f : α → β} (g : free_group α →* β) (hg : ∀ (x : α), coe_fn g (of x) = f x) {x : free_group α} : coe_fn g x = coe_fn (to_group f) x := sorry /-- Two homomorphisms out of a free group are equal if they are equal on generators. See note [partially-applied ext lemmas]. -/ theorem ext_hom {α : Type u} {G : Type u_1} [group G] (f : free_group α →* G) (g : free_group α →* G) (h : ∀ (a : α), coe_fn f (of a) = coe_fn g (of a)) : f = g := sorry theorem to_group.of_eq {α : Type u} (x : free_group α) : coe_fn (to_group of) x = x := Eq.symm (to_group.unique (monoid_hom.id (free_group α)) fun (x : α) => rfl) theorem to_group.range_subset {α : Type u} {β : Type v} [group β] {f : α → β} {s : subgroup β} (H : set.range f ⊆ ↑s) : set.range ⇑(to_group f) ⊆ ↑s := sorry theorem closure_subset {G : Type u_1} [group G] {s : set G} {t : subgroup G} (h : s ⊆ ↑t) : subgroup.closure s ≤ t := eq.mpr (id (Eq.trans (propext (subgroup.closure_le t)) (propext (iff_true_intro h)))) trivial theorem to_group.range_eq_closure {α : Type u} {β : Type v} [group β] {f : α → β} : set.range ⇑(to_group f) = ↑(subgroup.closure (set.range f)) := sorry /-- Given `f : α → β`, the canonical map `list (α × bool) → list (β × bool)`. -/ def map.aux {α : Type u} {β : Type v} (f : α → β) (L : List (α × Bool)) : List (β × Bool) := list.map (fun (x : α × Bool) => (f (prod.fst x), prod.snd x)) L /-- Any function from `α` to `β` extends uniquely to a group homomorphism from the free group over `α` to the free group over `β`. Note that this is the bare function; for the group homomorphism use `map`. -/ def map.to_fun {α : Type u} {β : Type v} (f : α → β) (x : free_group α) : free_group β := quot.lift_on x (fun (L : List (α × Bool)) => mk (map.aux f L)) sorry /-- Any function from `α` to `β` extends uniquely to a group homomorphism from the free group ver `α` to the free group over `β`. -/ def map {α : Type u} {β : Type v} (f : α → β) : free_group α →* free_group β := monoid_hom.mk' sorry sorry --by rintros ⟨L₁⟩ ⟨L₂⟩; simp [map, map.aux] @[simp] theorem map.mk {α : Type u} {L : List (α × Bool)} {β : Type v} {f : α → β} : coe_fn (map f) (mk L) = mk (list.map (fun (x : α × Bool) => (f (prod.fst x), prod.snd x)) L) := rfl @[simp] theorem map.id {α : Type u} {x : free_group α} : coe_fn (map id) x = x := sorry @[simp] theorem map.id' {α : Type u} {x : free_group α} : coe_fn (map fun (z : α) => z) x = x := map.id theorem map.comp {α : Type u} {β : Type v} {γ : Type w} {f : α → β} {g : β → γ} {x : free_group α} : coe_fn (map g) (coe_fn (map f) x) = coe_fn (map (g ∘ f)) x := sorry @[simp] theorem map.of {α : Type u} {β : Type v} {f : α → β} {x : α} : coe_fn (map f) (of x) = of (f x) := rfl @[simp] theorem map.mul {α : Type u} {β : Type v} {f : α → β} {x : free_group α} {y : free_group α} : coe_fn (map f) (x * y) = coe_fn (map f) x * coe_fn (map f) y := is_mul_hom.map_mul (⇑(map f)) x y @[simp] theorem map.one {α : Type u} {β : Type v} {f : α → β} : coe_fn (map f) 1 = 1 := is_group_hom.map_one ⇑(map f) @[simp] theorem map.inv {α : Type u} {β : Type v} {f : α → β} {x : free_group α} : coe_fn (map f) (x⁻¹) = (coe_fn (map f) x⁻¹) := is_group_hom.map_inv (⇑(map f)) x theorem map.unique {α : Type u} {β : Type v} {f : α → β} (g : free_group α → free_group β) [is_group_hom g] (hg : ∀ (x : α), g (of x) = of (f x)) {x : free_group α} : g x = coe_fn (map f) x := sorry /-- Equivalent types give rise to equivalent free groups. -/ def free_group_congr {α : Type u_1} {β : Type u_2} (e : α ≃ β) : free_group α ≃ free_group β := equiv.mk ⇑(map ⇑e) ⇑(map ⇑(equiv.symm e)) sorry sorry theorem map_eq_to_group {α : Type u} {β : Type v} {f : α → β} {x : free_group α} : coe_fn (map f) x = coe_fn (to_group (of ∘ f)) x := sorry /-- 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 {α : Type u} [group α] : free_group α →* α := to_group id @[simp] theorem prod_mk {α : Type u} {L : List (α × Bool)} [group α] : coe_fn prod (mk L) = list.prod (list.map (fun (x : α × Bool) => cond (prod.snd x) (prod.fst x) (prod.fst x⁻¹)) L) := rfl @[simp] theorem prod.of {α : Type u} [group α] {x : α} : coe_fn prod (of x) = x := to_group.of @[simp] theorem prod.mul {α : Type u} [group α] {x : free_group α} {y : free_group α} : coe_fn prod (x * y) = coe_fn prod x * coe_fn prod y := to_group.mul @[simp] theorem prod.one {α : Type u} [group α] : coe_fn prod 1 = 1 := to_group.one @[simp] theorem prod.inv {α : Type u} [group α] {x : free_group α} : coe_fn prod (x⁻¹) = (coe_fn prod x⁻¹) := to_group.inv theorem prod.unique {α : Type u} [group α] (g : free_group α →* α) (hg : ∀ (x : α), coe_fn g (of x) = x) {x : free_group α} : coe_fn g x = coe_fn prod x := to_group.unique g hg theorem to_group_eq_prod_map {α : Type u} {β : Type v} [group β] {f : α → β} {x : free_group α} : coe_fn (to_group f) x = coe_fn prod (coe_fn (map f) x) := sorry /-- 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 {α : Type u} [add_group α] (x : free_group α) : α := coe_fn prod x @[simp] theorem sum_mk {α : Type u} {L : List (α × Bool)} [add_group α] : sum (mk L) = list.sum (list.map (fun (x : α × Bool) => cond (prod.snd x) (prod.fst x) (-prod.fst x)) L) := rfl @[simp] theorem sum.of {α : Type u} [add_group α] {x : α} : sum (of x) = x := prod.of protected instance sum.is_group_hom {α : Type u} [add_group α] : is_group_hom sum := monoid_hom.is_group_hom prod @[simp] theorem sum.mul {α : Type u} [add_group α] {x : free_group α} {y : free_group α} : sum (x * y) = sum x + sum y := prod.mul @[simp] theorem sum.one {α : Type u} [add_group α] : sum 1 = 0 := prod.one @[simp] theorem sum.inv {α : Type u} [add_group α] {x : free_group α} : sum (x⁻¹) = -sum x := prod.inv /-- The bijection between the free group on the empty type, and a type with one element. -/ def free_group_empty_equiv_unit : free_group empty ≃ Unit := equiv.mk (fun (_x : free_group empty) => Unit.unit) (fun (_x : Unit) => 1) sorry sorry /-- The bijection between the free group on a singleton, and the integers. -/ def free_group_unit_equiv_int : free_group Unit ≃ ℤ := equiv.mk (fun (x : free_group Unit) => sum (monoid_hom.to_fun (map fun (_x : Unit) => 1) x)) (fun (x : ℤ) => of Unit.unit ^ x) sorry sorry protected instance monad : Monad free_group := sorry protected theorem induction_on {α : Type u} {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 : free_group α), C x → C y → C (x * y)) : C z := sorry @[simp] theorem map_pure {α : Type u} {β : Type u} (f : α → β) (x : α) : f <$> pure x = pure (f x) := map.of @[simp] theorem map_one {α : Type u} {β : Type u} (f : α → β) : f <$> 1 = 1 := map.one @[simp] theorem map_mul {α : Type u} {β : Type u} (f : α → β) (x : free_group α) (y : free_group α) : f <$> (x * y) = f <$> x * f <$> y := map.mul @[simp] theorem map_inv {α : Type u} {β : Type u} (f : α → β) (x : free_group α) : f <$> (x⁻¹) = (f <$> x⁻¹) := map.inv @[simp] theorem pure_bind {α : Type u} {β : Type u} (f : α → free_group β) (x : α) : pure x >>= f = f x := to_group.of @[simp] theorem one_bind {α : Type u} {β : Type u} (f : α → free_group β) : 1 >>= f = 1 := to_group.one @[simp] theorem mul_bind {α : Type u} {β : Type u} (f : α → free_group β) (x : free_group α) (y : free_group α) : x * y >>= f = (x >>= f) * (y >>= f) := to_group.mul @[simp] theorem inv_bind {α : Type u} {β : Type u} (f : α → free_group β) (x : free_group α) : x⁻¹ >>= f = (x >>= f⁻¹) := to_group.inv protected instance is_lawful_monad : is_lawful_monad free_group := sorry /-- The maximal reduction of a word. It is computable iff `α` has decidable equality. -/ def reduce {α : Type u} [DecidableEq α] (L : List (α × Bool)) : List (α × Bool) := list.rec_on L [] fun (hd1 : α × Bool) (tl1 ih : List (α × Bool)) => list.cases_on ih [hd1] fun (hd2 : α × Bool) (tl2 : List (α × Bool)) => ite (prod.fst hd1 = prod.fst hd2 ∧ prod.snd hd1 = !prod.snd hd2) tl2 (hd1 :: hd2 :: tl2) @[simp] theorem reduce.cons {α : Type u} {L : List (α × Bool)} [DecidableEq α] (x : α × Bool) : reduce (x :: L) = list.cases_on (reduce L) [x] fun (hd : α × Bool) (tl : List (α × Bool)) => ite (prod.fst x = prod.fst hd ∧ prod.snd x = !prod.snd hd) tl (x :: hd :: tl) := rfl /-- The first theorem that characterises the function `reduce`: a word reduces to its maximal reduction. -/ theorem reduce.red {α : Type u} {L : List (α × Bool)} [DecidableEq α] : red L (reduce L) := sorry theorem reduce.not {α : Type u} [DecidableEq α] {p : Prop} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {L₃ : List (α × Bool)} {x : α} {b : Bool} : reduce L₁ = L₂ ++ (x, b) :: (x, !b) :: L₃ → p := sorry /-- The second theorem that characterises the function `reduce`: the maximal reduction of a word only reduces to itself. -/ theorem reduce.min {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} [DecidableEq α] (H : red (reduce L₁) L₂) : reduce L₁ = L₂ := sorry /-- `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 {α : Type u} {L : List (α × Bool)} [DecidableEq α] : reduce (reduce L) = reduce L := Eq.symm (reduce.min reduce.red) theorem reduce.step.eq {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} [DecidableEq α] (H : red.step L₁ L₂) : reduce L₁ = reduce L₂ := sorry /-- If a word reduces to another word, then they have a common maximal reduction. -/ theorem reduce.eq_of_red {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} [DecidableEq α] (H : red L₁ L₂) : reduce L₁ = reduce L₂ := sorry /-- 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 {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} [DecidableEq α] (H : mk L₁ = mk L₂) : reduce L₁ = reduce L₂ := sorry /-- If two words have a common maximal reduction, then they correspond to the same element in the free group. -/ theorem reduce.exact {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} [DecidableEq α] (H : reduce L₁ = reduce L₂) : mk L₁ = mk L₂ := iff.mpr red.exact (Exists.intro (reduce L₂) { left := H ▸ reduce.red, right := reduce.red }) /-- A word and its maximal reduction correspond to the same element of the free group. -/ theorem reduce.self {α : Type u} {L : List (α × Bool)} [DecidableEq α] : 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 {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} [DecidableEq α] (H : red L₁ L₂) : red L₂ (reduce L₁) := Eq.symm (reduce.eq_of_red H) ▸ reduce.red /-- The function that sends an element of the free group to its maximal reduction. -/ def to_word {α : Type u} [DecidableEq α] : free_group α → List (α × Bool) := Quot.lift reduce sorry theorem to_word.mk {α : Type u} [DecidableEq α] {x : free_group α} : mk (to_word x) = x := quot.induction_on x fun (L : List (α × Bool)) => reduce.self theorem to_word.inj {α : Type u} [DecidableEq α] (x : free_group α) (y : free_group α) : to_word x = to_word y → x = y := quot.induction_on x fun (L₁ : List (α × Bool)) => quot.induction_on y fun (L₂ : List (α × Bool)) => reduce.exact /-- Constructive Church-Rosser theorem (compare `church_rosser`). -/ def reduce.church_rosser {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} {L₃ : List (α × Bool)} [DecidableEq α] (H12 : red L₁ L₂) (H13 : red L₁ L₃) : Subtype fun (L₄ : List (α × Bool)) => red L₂ L₄ ∧ red L₃ L₄ := { val := reduce L₁, property := sorry } protected instance decidable_eq {α : Type u} [DecidableEq α] : DecidableEq (free_group α) := function.injective.decidable_eq sorry protected instance red.decidable_rel {α : Type u} [DecidableEq α] : DecidableRel red := sorry /-- A list containing every word that `w₁` reduces to. -/ def red.enum {α : Type u} [DecidableEq α] (L₁ : List (α × Bool)) : List (List (α × Bool)) := list.filter (fun (L₂ : List (α × Bool)) => red L₁ L₂) (list.sublists L₁) theorem red.enum.sound {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} [DecidableEq α] (H : L₂ ∈ red.enum L₁) : red L₁ L₂ := list.of_mem_filter H theorem red.enum.complete {α : Type u} {L₁ : List (α × Bool)} {L₂ : List (α × Bool)} [DecidableEq α] (H : red L₁ L₂) : L₂ ∈ red.enum L₁ := list.mem_filter_of_mem (iff.mpr list.mem_sublists (red.sublist H)) H protected instance subtype.fintype {α : Type u} {L₁ : List (α × Bool)} [DecidableEq α] : fintype (Subtype fun (L₂ : List (α × Bool)) => red L₁ L₂) := fintype.subtype (list.to_finset (red.enum L₁)) sorry end Mathlib