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
631365abbd562b886f14eb2754f42514f0791a75
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/convex/star.lean
02e452ba5af2d2d2f8327738a4f00921fdb90e6a
[ "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,898
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import analysis.convex.segment /-! # Star-convex sets This files defines star-convex sets (aka star domains, star-shaped set, radially convex set). A set is star-convex at `x` if every segment from `x` to a point in the set is contained in the set. This is the prototypical example of a contractible set in homotopy theory (by scaling every point towards `x`), but has wider uses. Note that this has nothing to do with star rings, `has_star` and co. ## Main declarations * `star_convex 𝕜 x s`: `s` is star-convex at `x` with scalars `𝕜`. ## Implementation notes Instead of saying that a set is star-convex, we say a set is star-convex *at a point*. This has the advantage of allowing us to talk about convexity as being "everywhere star-convexity" and of making the union of star-convex sets be star-convex. Incidentally, this choice means we don't need to assume a set is nonempty for it to be star-convex. Concretely, the empty set is star-convex at every point. ## TODO Balanced sets are star-convex. The closure of a star-convex set is star-convex. Star-convex sets are contractible. A nonempty open star-convex set in `ℝ^n` is diffeomorphic to the entire space. -/ open set open_locale convex pointwise variables {𝕜 E F : Type*} section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section has_smul variables (𝕜) [has_smul 𝕜 E] [has_smul 𝕜 F] (x : E) (s : set E) /-- Star-convexity of sets. `s` is star-convex at `x` if every segment from `x` to a point in `s` is contained in `s`. -/ def star_convex : Prop := ∀ ⦃y : E⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s variables {𝕜 x s} {t : set E} lemma star_convex_iff_segment_subset : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → [x -[𝕜] y] ⊆ s := begin split, { rintro h y hy z ⟨a, b, ha, hb, hab, rfl⟩, exact h hy ha hb hab }, { rintro h y hy a b ha hb hab, exact h hy ⟨a, b, ha, hb, hab, rfl⟩ } end lemma star_convex.segment_subset (h : star_convex 𝕜 x s) {y : E} (hy : y ∈ s) : [x -[𝕜] y] ⊆ s := star_convex_iff_segment_subset.1 h hy lemma star_convex.open_segment_subset (h : star_convex 𝕜 x s) {y : E} (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s := (open_segment_subset_segment 𝕜 x y).trans (h.segment_subset hy) /-- Alternative definition of star-convexity, in terms of pointwise set operations. -/ lemma star_convex_iff_pointwise_add_subset : star_convex 𝕜 x s ↔ ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • {x} + b • s ⊆ s := begin refine ⟨_, λ h y hy a b ha hb hab, h ha hb hab (add_mem_add (smul_mem_smul_set $ mem_singleton _) ⟨_, hy, rfl⟩)⟩, rintro hA a b ha hb hab w ⟨au, bv, ⟨u, (rfl : u = x), rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hv ha hb hab, end lemma star_convex_empty (x : E) : star_convex 𝕜 x ∅ := λ y hy, hy.elim lemma star_convex_univ (x : E) : star_convex 𝕜 x univ := λ _ _ _ _ _ _ _, trivial lemma star_convex.inter (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 x t) : star_convex 𝕜 x (s ∩ t) := λ y hy a b ha hb hab, ⟨hs hy.left ha hb hab, ht hy.right ha hb hab⟩ lemma star_convex_sInter {S : set (set E)} (h : ∀ s ∈ S, star_convex 𝕜 x s) : star_convex 𝕜 x (⋂₀ S) := λ y hy a b ha hb hab s hs, h s hs (hy s hs) ha hb hab lemma star_convex_Inter {ι : Sort*} {s : ι → set E} (h : ∀ i, star_convex 𝕜 x (s i)) : star_convex 𝕜 x (⋂ i, s i) := (sInter_range s) ▸ star_convex_sInter $ forall_range_iff.2 h lemma star_convex.union (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 x t) : star_convex 𝕜 x (s ∪ t) := begin rintro y (hy | hy) a b ha hb hab, { exact or.inl (hs hy ha hb hab) }, { exact or.inr (ht hy ha hb hab) } end lemma star_convex_Union {ι : Sort*} {s : ι → set E} (hs : ∀ i, star_convex 𝕜 x (s i)) : star_convex 𝕜 x (⋃ i, s i) := begin rintro y hy a b ha hb hab, rw mem_Union at ⊢ hy, obtain ⟨i, hy⟩ := hy, exact ⟨i, hs i hy ha hb hab⟩, end lemma star_convex_sUnion {S : set (set E)} (hS : ∀ s ∈ S, star_convex 𝕜 x s) : star_convex 𝕜 x (⋃₀ S) := begin rw sUnion_eq_Union, exact star_convex_Union (λ s, hS _ s.2), end lemma star_convex.prod {y : F} {s : set E} {t : set F} (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x, y) (s ×ˢ t) := λ y hy a b ha hb hab, ⟨hs hy.1 ha hb hab, ht hy.2 ha hb hab⟩ lemma star_convex_pi {ι : Type*} {E : ι → Type*} [Π i, add_comm_monoid (E i)] [Π i, has_smul 𝕜 (E i)] {x : Π i, E i} {s : set ι} {t : Π i, set (E i)} (ht : ∀ ⦃i⦄, i ∈ s → star_convex 𝕜 (x i) (t i)) : star_convex 𝕜 x (s.pi t) := λ y hy a b ha hb hab i hi, ht hi (hy i hi) ha hb hab end has_smul section module variables [module 𝕜 E] [module 𝕜 F] {x y z : E} {s : set E} lemma star_convex.mem (hs : star_convex 𝕜 x s) (h : s.nonempty) : x ∈ s := begin obtain ⟨y, hy⟩ := h, convert hs hy zero_le_one le_rfl (add_zero 1), rw [one_smul, zero_smul, add_zero], end lemma star_convex_iff_forall_pos (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h y hy a b ha hb hab, h hy ha.le hb.le hab, _⟩, intros h y hy a b ha hb hab, obtain rfl | ha := ha.eq_or_lt, { rw zero_add at hab, rwa [hab, one_smul, zero_smul, zero_add] }, obtain rfl | hb := hb.eq_or_lt, { rw add_zero at hab, rwa [hab, one_smul, zero_smul, add_zero] }, exact h hy ha hb hab, end lemma star_convex_iff_forall_ne_pos (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h y hy _ a b ha hb hab, h hy ha.le hb.le hab, _⟩, intros h y 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 hy hxy ha' hb' hab, end lemma star_convex_iff_open_segment_subset (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → open_segment 𝕜 x y ⊆ s := star_convex_iff_segment_subset.trans $ forall₂_congr $ λ y hy, (open_segment_subset_iff_segment_subset hx hy).symm lemma star_convex_singleton (x : E) : star_convex 𝕜 x {x} := begin rintro y (rfl : y = x) a b ha hb hab, exact convex.combo_self hab _, end lemma star_convex.linear_image (hs : star_convex 𝕜 x s) (f : E →ₗ[𝕜] F) : star_convex 𝕜 (f x) (s.image f) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, exact ⟨a • x + b • y', hs hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]⟩, end lemma star_convex.is_linear_image (hs : star_convex 𝕜 x s) {f : E → F} (hf : is_linear_map 𝕜 f) : star_convex 𝕜 (f x) (f '' s) := hs.linear_image $ hf.mk' f lemma star_convex.linear_preimage {s : set F} (f : E →ₗ[𝕜] F) (hs : star_convex 𝕜 (f x) s) : star_convex 𝕜 x (s.preimage f) := begin intros y hy a b ha hb hab, rw [mem_preimage, f.map_add, f.map_smul, f.map_smul], exact hs hy ha hb hab, end lemma star_convex.is_linear_preimage {s : set F} {f : E → F} (hs : star_convex 𝕜 (f x) s) (hf : is_linear_map 𝕜 f) : star_convex 𝕜 x (preimage f s) := hs.linear_preimage $ hf.mk' f lemma star_convex.add {t : set E} (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x + y) (s + t) := by { rw ←add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma star_convex.add_left (hs : star_convex 𝕜 x s) (z : E) : star_convex 𝕜 (z + x) ((λ x, z + x) '' s) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, refine ⟨a • x + b • y', hs hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end lemma star_convex.add_right (hs : star_convex 𝕜 x s) (z : E) : star_convex 𝕜 (x + z) ((λ x, x + z) '' s) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, refine ⟨a • x + b • y', hs hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end /-- The translation of a star-convex set is also star-convex. -/ lemma star_convex.preimage_add_right (hs : star_convex 𝕜 (z + x) s) : star_convex 𝕜 x ((λ x, z + x) ⁻¹' s) := begin intros y hy a b ha hb hab, have h := hs 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 star-convex set is also star-convex. -/ lemma star_convex.preimage_add_left (hs : star_convex 𝕜 (x + z) s) : star_convex 𝕜 x ((λ x, x + z) ⁻¹' s) := begin rw add_comm at hs, simpa only [add_comm] using hs.preimage_add_right, end end module end add_comm_monoid section add_comm_group variables [add_comm_group E] [module 𝕜 E] {x y : E} lemma star_convex.sub' {s : set (E × E)} (hs : star_convex 𝕜 (x, y) s) : star_convex 𝕜 (x - y) ((λ x : E × E, x.1 - x.2) '' s) := hs.is_linear_image is_linear_map.is_linear_map_sub 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] {x : E} {s : set E} lemma star_convex.smul (hs : star_convex 𝕜 x s) (c : 𝕜) : star_convex 𝕜 (c • x) (c • s) := hs.linear_image $ linear_map.lsmul _ _ c lemma star_convex.preimage_smul {c : 𝕜} (hs : star_convex 𝕜 (c • x) s) : star_convex 𝕜 x ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma star_convex.affinity (hs : star_convex 𝕜 x s) (z : E) (c : 𝕜) : star_convex 𝕜 (z + c • x) ((λ x, z + c • x) '' s) := begin have h := (hs.smul c).add_left 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_monoid variables [add_comm_monoid E] [smul_with_zero 𝕜 E]{s : set E} lemma star_convex_zero_iff : star_convex 𝕜 0 s ↔ ∀ ⦃x : E⦄, x ∈ s → ∀ ⦃a : 𝕜⦄, 0 ≤ a → a ≤ 1 → a • x ∈ s := begin refine forall_congr (λ x, forall_congr $ λ hx, ⟨λ h a ha₀ ha₁, _, λ h a b ha hb hab, _⟩), { simpa only [sub_add_cancel, eq_self_iff_true, forall_true_left, zero_add, smul_zero'] using h (sub_nonneg_of_le ha₁) ha₀ }, { rw [smul_zero', zero_add], exact h hb (by { rw ←hab, exact le_add_of_nonneg_left ha }) } end end add_comm_monoid section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {x y : E} {s t : set E} lemma star_convex.add_smul_mem (hs : star_convex 𝕜 x s) (hy : x + y ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 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 hy (sub_nonneg_of_le ht₁) ht₀ (sub_add_cancel _ _), end lemma star_convex.smul_mem (hs : star_convex 𝕜 0 s) (hx : x ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 1) : t • x ∈ s := by simpa using hs.add_smul_mem (by simpa using hx) ht₀ ht₁ lemma star_convex.add_smul_sub_mem (hs : star_convex 𝕜 x s) (hy : y ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 1) : x + t • (y - x) ∈ s := begin apply hs.segment_subset hy, rw segment_eq_image', exact mem_image_of_mem _ ⟨ht₀, ht₁⟩, end /-- The preimage of a star-convex set under an affine map is star-convex. -/ lemma star_convex.affine_preimage (f : E →ᵃ[𝕜] F) {s : set F} (hs : star_convex 𝕜 (f x) s) : star_convex 𝕜 x (f ⁻¹' s) := begin intros y hy a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs hy ha hb hab, end /-- The image of a star-convex set under an affine map is star-convex. -/ lemma star_convex.affine_image (f : E →ᵃ[𝕜] F) {s : set E} (hs : star_convex 𝕜 x s) : star_convex 𝕜 (f x) (f '' s) := begin rintro y ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a • x + b • y', ⟨hs hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hy'f], end lemma star_convex.neg (hs : star_convex 𝕜 x s) : star_convex 𝕜 (-x) (-s) := by { rw ←image_neg, exact hs.is_linear_image is_linear_map.is_linear_map_neg } lemma star_convex.sub (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x - y) (s - t) := by { simp_rw sub_eq_add_neg, exact hs.add ht.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] [module 𝕜 E] {x : E} {s : set E} /-- Alternative definition of star-convexity, using division. -/ lemma star_convex_iff_div : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a / (a + b)) • x + (b / (a + b)) • y ∈ s := ⟨λ h y hy a b ha hb hab, begin apply h 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 y hy a b ha hb hab, begin have h', from h hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ lemma star_convex.mem_smul (hs : star_convex 𝕜 0 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 hs.smul_mem hx (inv_nonneg.2 $ zero_le_one.trans ht) (inv_le_one ht), end end add_comm_group end linear_ordered_field /-! #### Star-convex sets in an ordered space Relates `star_convex` and `set.ord_connected`. -/ section ord_connected lemma set.ord_connected.star_convex [ordered_semiring 𝕜] [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {x : E} {s : set E} (hs : s.ord_connected) (hx : x ∈ s) (h : ∀ y ∈ s, x ≤ y ∨ y ≤ x) : star_convex 𝕜 x s := begin intros y hy a b ha hb hab, obtain hxy | hyx := h _ 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 star_convex_iff_ord_connected [linear_ordered_field 𝕜] {x : 𝕜} {s : set 𝕜} (hx : x ∈ s) : star_convex 𝕜 x s ↔ s.ord_connected := by simp_rw [ord_connected_iff_interval_subset_left hx, star_convex_iff_segment_subset, segment_eq_interval] alias star_convex_iff_ord_connected ↔ star_convex.ord_connected _ end ord_connected
74b70bc0bf87f9cb4e1dcddbdc096355dab7dbbd
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast17.lean
a36d49e39b341856b516729df71a6f39492ddf94
[ "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
128
lean
set_option blast.strategy "preprocess" example (p q r : Prop) (a b : nat) : true → a = a → q → q → p → p := by blast
e430d4df9c6cecf3b83066a292cc52bc8206702a
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/Fintype.lean
6891b2a95d191abbce98703a920a6e6cc0de5ae1
[ "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,347
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Adam Topaz -/ import data.fintype.basic import data.fin.basic import category_theory.concrete_category.bundled import category_theory.concrete_category import category_theory.full_subcategory import category_theory.skeletal /-! # The category of finite types. We define the category of finite types, denoted `Fintype` as (bundled) types with a `fintype` instance. We also define `Fintype.skeleton`, the standard skeleton of `Fintype` whose objects are `fin n` for `n : ℕ`. We prove that the obvious inclusion functor `Fintype.skeleton ⥤ Fintype` is an equivalence of categories in `Fintype.skeleton.equivalence`. We prove that `Fintype.skeleton` is a skeleton of `Fintype` in `Fintype.is_skeleton`. -/ open_locale classical open category_theory /-- The category of finite types. -/ def Fintype := bundled fintype namespace Fintype instance : has_coe_to_sort Fintype Type* := bundled.has_coe_to_sort /-- Construct a bundled `Fintype` from the underlying type and typeclass. -/ def of (X : Type*) [fintype X] : Fintype := bundled.of X instance : inhabited Fintype := ⟨⟨pempty⟩⟩ instance {X : Fintype} : fintype X := X.2 instance : category Fintype := induced_category.category bundled.α /-- The fully faithful embedding of `Fintype` into the category of types. -/ @[derive [full, faithful], simps] def incl : Fintype ⥤ Type* := induced_functor _ instance : concrete_category Fintype := ⟨incl⟩ @[simp] lemma id_apply (X : Fintype) (x : X) : (𝟙 X : X → X) x = x := rfl @[simp] lemma comp_apply {X Y Z : Fintype} (f : X ⟶ Y) (g : Y ⟶ Z) (x : X) : (f ≫ g) x = g (f x) := rfl universe u /-- The "standard" skeleton for `Fintype`. This is the full subcategory of `Fintype` spanned by objects of the form `ulift (fin n)` for `n : ℕ`. We parameterize the objects of `Fintype.skeleton` directly as `ulift ℕ`, as the type `ulift (fin m) ≃ ulift (fin n)` is nonempty if and only if `n = m`. Specifying universes, `skeleton : Type u` is a small skeletal category equivalent to `Fintype.{u}`. -/ def skeleton : Type u := ulift ℕ namespace skeleton /-- Given any natural number `n`, this creates the associated object of `Fintype.skeleton`. -/ def mk : ℕ → skeleton := ulift.up instance : inhabited skeleton := ⟨mk 0⟩ /-- Given any object of `Fintype.skeleton`, this returns the associated natural number. -/ def len : skeleton → ℕ := ulift.down @[ext] lemma ext (X Y : skeleton) : X.len = Y.len → X = Y := ulift.ext _ _ instance : small_category skeleton.{u} := { hom := λ X Y, ulift.{u} (fin X.len) → ulift.{u} (fin Y.len), id := λ _, id, comp := λ _ _ _ f g, g ∘ f } lemma is_skeletal : skeletal skeleton.{u} := λ X Y ⟨h⟩, ext _ _ $ fin.equiv_iff_eq.mp $ nonempty.intro $ { to_fun := λ x, (h.hom ⟨x⟩).down, inv_fun := λ x, (h.inv ⟨x⟩).down, left_inv := begin intro a, change ulift.down _ = _, rw ulift.up_down, change ((h.hom ≫ h.inv) _).down = _, simpa, end, right_inv := begin intro a, change ulift.down _ = _, rw ulift.up_down, change ((h.inv ≫ h.hom) _).down = _, simpa, end } /-- The canonical fully faithful embedding of `Fintype.skeleton` into `Fintype`. -/ def incl : skeleton.{u} ⥤ Fintype.{u} := { obj := λ X, Fintype.of (ulift (fin X.len)), map := λ _ _ f, f } instance : full incl := { preimage := λ _ _ f, f } instance : faithful incl := {} instance : ess_surj incl := ess_surj.mk $ λ X, let F := fintype.equiv_fin X in ⟨mk (fintype.card X), nonempty.intro { hom := F.symm ∘ ulift.down, inv := ulift.up ∘ F }⟩ noncomputable instance : is_equivalence incl := equivalence.of_fully_faithfully_ess_surj _ /-- The equivalence between `Fintype.skeleton` and `Fintype`. -/ noncomputable def equivalence : skeleton ≌ Fintype := incl.as_equivalence @[simp] lemma incl_mk_nat_card (n : ℕ) : fintype.card (incl.obj (mk n)) = n := begin convert finset.card_fin n, apply fintype.of_equiv_card, end end skeleton /-- `Fintype.skeleton` is a skeleton of `Fintype`. -/ noncomputable def is_skeleton : is_skeleton_of Fintype skeleton skeleton.incl := { skel := skeleton.is_skeletal, eqv := by apply_instance } end Fintype
44ead0bf47a912d55d5c878284cf3710a6d808b9
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category_theory/monoidal/category_aux.lean
3a5ede0aec385193f0eacfd51f71efdb09050a98
[ "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
3,216
lean
/- Copyright (c) 2018 Michael Jendrusch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Michael Jendrusch, Scott Morrison -/ import category_theory.products /-! # Auxiliary definitions for the definition of a monoidal category. -/ universes v u open category_theory namespace category_theory @[reducible] def tensor_obj_type (C : Type u) [category.{v} C] := C → C → C @[reducible] def tensor_hom_type {C : Type u} [category.{v} C] (tensor_obj : tensor_obj_type C) : Sort (imax (u+1) (u+1) (u+1) (u+1) v) := Π {X₁ Y₁ X₂ Y₂ : C}, (X₁ ⟶ Y₁) → (X₂ ⟶ Y₂) → ((tensor_obj X₁ X₂) ⟶ (tensor_obj Y₁ Y₂)) def assoc_obj {C : Type u} [category.{v} C] (tensor_obj : tensor_obj_type C) : Sort (max (u+1) v) := Π X Y Z : C, (tensor_obj (tensor_obj X Y) Z) ≅ (tensor_obj X (tensor_obj Y Z)) def assoc_natural {C : Type u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_hom : tensor_hom_type tensor_obj) (assoc : assoc_obj tensor_obj) : Prop := ∀ {X₁ X₂ X₃ Y₁ Y₂ Y₃ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (f₃ : X₃ ⟶ Y₃), (tensor_hom (tensor_hom f₁ f₂) f₃) ≫ (assoc Y₁ Y₂ Y₃).hom = (assoc X₁ X₂ X₃).hom ≫ (tensor_hom f₁ (tensor_hom f₂ f₃)) def left_unitor_obj {C : Type u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_unit : C) : Sort (max (u+1) v) := Π X : C, (tensor_obj tensor_unit X) ≅ X def left_unitor_natural {C : Type u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_hom : tensor_hom_type tensor_obj) (tensor_unit : C) (left_unitor : left_unitor_obj tensor_obj tensor_unit) : Prop := ∀ {X Y : C} (f : X ⟶ Y), (tensor_hom (𝟙 tensor_unit) f) ≫ (left_unitor Y).hom = (left_unitor X).hom ≫ f def right_unitor_obj {C : Type u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_unit : C) : Sort (max (u+1) v 1) := Π (X : C), (tensor_obj X tensor_unit) ≅ X def right_unitor_natural {C : Type u} [category.{v} C] (tensor_obj : tensor_obj_type C) (tensor_hom : tensor_hom_type tensor_obj) (tensor_unit : C) (right_unitor : right_unitor_obj tensor_obj tensor_unit) : Prop := ∀ {X Y : C} (f : X ⟶ Y), (tensor_hom f (𝟙 tensor_unit)) ≫ (right_unitor Y).hom = (right_unitor X).hom ≫ f @[reducible] def pentagon {C : Type u} [category.{v} C] {tensor_obj : tensor_obj_type C} (tensor_hom : tensor_hom_type tensor_obj) (assoc : assoc_obj tensor_obj) : Prop := ∀ W X Y Z : C, (tensor_hom (assoc W X Y).hom (𝟙 Z)) ≫ (assoc W (tensor_obj X Y) Z).hom ≫ (tensor_hom (𝟙 W) (assoc X Y Z).hom) = (assoc (tensor_obj W X) Y Z).hom ≫ (assoc W X (tensor_obj Y Z)).hom @[reducible] def triangle {C : Type u} [category.{v} C] {tensor_obj : tensor_obj_type C} {tensor_unit : C} (tensor_hom : tensor_hom_type tensor_obj) (left_unitor : left_unitor_obj tensor_obj tensor_unit) (right_unitor : right_unitor_obj tensor_obj tensor_unit) (assoc : assoc_obj tensor_obj) : Prop := ∀ X Y : C, (assoc X tensor_unit Y).hom ≫ (tensor_hom (𝟙 X) (left_unitor Y).hom) = tensor_hom (right_unitor X).hom (𝟙 Y) end category_theory
90b78f9d416a2022640b39dbb3169e2d8dcae731
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/fintype/parity.lean
a5fe50a158d252c0bb9d59705a52e0192ddd503e
[ "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
759
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.fintype.card import algebra.parity /-! # The cardinality of `fin (bit0 n)` is even. -/ variables {α : Type*} namespace fintype instance is_square.decidable_pred [has_mul α] [fintype α] [decidable_eq α] : decidable_pred (is_square : α → Prop) := λ a, fintype.decidable_exists_fintype end fintype /-- The cardinality of `fin (bit0 n)` is even, `fact` version. This `fact` is needed as an instance by `matrix.special_linear_group.has_neg`. -/ lemma fintype.card_fin_even {n : ℕ} : fact (even (fintype.card (fin (bit0 n)))) := ⟨by { rw fintype.card_fin, exact even_bit0 _ }⟩
e6b217e7b5d799531ab7e6c46a38ac05e50b4761
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/hahn_series.lean
3ec8f826939dd1b4b61f90ba05462d9fd9ad88a8
[ "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
60,235
lean
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import order.well_founded_set import algebra.big_operators.finprod import ring_theory.valuation.basic import algebra.module.pi import ring_theory.power_series.basic /-! # Hahn Series If `Γ` is ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are partially well-ordered. With further structure on `R` and `Γ`, we can add further structure on `hahn_series Γ R`, with the most studied case being when `Γ` is a linearly ordered abelian group and `R` is a field, in which case `hahn_series Γ R` is a valued field, with value group `Γ`. These generalize Laurent series (with value group `ℤ`), and Laurent series are implemented that way in the file `ring_theory/laurent_series`. ## Main Definitions * If `Γ` is ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are partially well-ordered. * If `R` is a (commutative) additive monoid or group, then so is `hahn_series Γ R`. * If `R` is a (comm_)(semi)ring, then so is `hahn_series Γ R`. * `hahn_series.add_val Γ R` defines an `add_valuation` on `hahn_series Γ R` when `Γ` is linearly ordered. * A `hahn_series.summable_family` is a family of Hahn series such that the union of their supports is well-founded and only finitely many are nonzero at any given coefficient. They have a formal sum, `hahn_series.summable_family.hsum`, which can be bundled as a `linear_map` as `hahn_series.summable_family.lsum`. Note that this is different from `summable` in the valuation topology, because there are topologically summable families that do not satisfy the axioms of `hahn_series.summable_family`, and formally summable families whose sums do not converge topologically. * Laurent series over `R` are implemented as `hahn_series ℤ R` in the file `ring_theory/laurent_series`. ## TODO * Build an API for the variable `X` (defined to be `single 1 1 : hahn_series Γ R`) in analogy to `X : polynomial R` and `X : power_series R` ## References - [J. van der Hoeven, *Operators on Generalized Power Series*][van_der_hoeven] -/ open finset function open_locale big_operators classical pointwise noncomputable theory /-- If `Γ` is linearly ordered and `R` has zero, then `hahn_series Γ R` consists of formal series over `Γ` with coefficients in `R`, whose supports are well-founded. -/ @[ext] structure hahn_series (Γ : Type*) (R : Type*) [partial_order Γ] [has_zero R] := (coeff : Γ → R) (is_pwo_support' : (support coeff).is_pwo) variables {Γ : Type*} {R : Type*} namespace hahn_series section zero variables [partial_order Γ] [has_zero R] lemma coeff_injective : injective (coeff : hahn_series Γ R → (Γ → R)) := ext @[simp] lemma coeff_inj {x y : hahn_series Γ R} : x.coeff = y.coeff ↔ x = y := coeff_injective.eq_iff /-- The support of a Hahn series is just the set of indices whose coefficients are nonzero. Notably, it is well-founded. -/ def support (x : hahn_series Γ R) : set Γ := support x.coeff @[simp] lemma is_pwo_support (x : hahn_series Γ R) : x.support.is_pwo := x.is_pwo_support' @[simp] lemma is_wf_support (x : hahn_series Γ R) : x.support.is_wf := x.is_pwo_support.is_wf @[simp] lemma mem_support (x : hahn_series Γ R) (a : Γ) : a ∈ x.support ↔ x.coeff a ≠ 0 := iff.refl _ instance : has_zero (hahn_series Γ R) := ⟨{ coeff := 0, is_pwo_support' := by simp }⟩ instance : inhabited (hahn_series Γ R) := ⟨0⟩ instance [subsingleton R] : subsingleton (hahn_series Γ R) := ⟨λ a b, a.ext b (subsingleton.elim _ _)⟩ @[simp] lemma zero_coeff {a : Γ} : (0 : hahn_series Γ R).coeff a = 0 := rfl @[simp] lemma coeff_fun_eq_zero_iff {x : hahn_series Γ R} : x.coeff = 0 ↔ x = 0 := coeff_injective.eq_iff' rfl lemma ne_zero_of_coeff_ne_zero {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : x ≠ 0 := mt (λ x0, (x0.symm ▸ zero_coeff : x.coeff g = 0)) h @[simp] lemma support_zero : support (0 : hahn_series Γ R) = ∅ := function.support_zero @[simp] lemma support_nonempty_iff {x : hahn_series Γ R} : x.support.nonempty ↔ x ≠ 0 := by rw [support, support_nonempty_iff, ne.def, coeff_fun_eq_zero_iff] @[simp] lemma support_eq_empty_iff {x : hahn_series Γ R} : x.support = ∅ ↔ x = 0 := support_eq_empty_iff.trans coeff_fun_eq_zero_iff /-- `single a r` is the Hahn series which has coefficient `r` at `a` and zero otherwise. -/ def single (a : Γ) : zero_hom R (hahn_series Γ R) := { to_fun := λ r, { coeff := pi.single a r, is_pwo_support' := (set.is_pwo_singleton a).mono pi.support_single_subset }, map_zero' := ext _ _ (pi.single_zero _) } variables {a b : Γ} {r : R} @[simp] theorem single_coeff_same (a : Γ) (r : R) : (single a r).coeff a = r := pi.single_eq_same a r @[simp] theorem single_coeff_of_ne (h : b ≠ a) : (single a r).coeff b = 0 := pi.single_eq_of_ne h r theorem single_coeff : (single a r).coeff b = if (b = a) then r else 0 := by { split_ifs with h; simp [h] } @[simp] lemma support_single_of_ne (h : r ≠ 0) : support (single a r) = {a} := pi.support_single_of_ne h lemma support_single_subset : support (single a r) ⊆ {a} := pi.support_single_subset lemma eq_of_mem_support_single {b : Γ} (h : b ∈ support (single a r)) : b = a := support_single_subset h @[simp] lemma single_eq_zero : (single a (0 : R)) = 0 := (single a).map_zero lemma single_injective (a : Γ) : function.injective (single a : R → hahn_series Γ R) := λ r s rs, by rw [← single_coeff_same a r, ← single_coeff_same a s, rs] lemma single_ne_zero (h : r ≠ 0) : single a r ≠ 0 := λ con, h (single_injective a (con.trans single_eq_zero.symm)) @[simp] lemma single_eq_zero_iff {a : Γ} {r : R} : single a r = 0 ↔ r = 0 := begin split, { contrapose!, exact single_ne_zero }, { simp {contextual := tt} } end instance [nonempty Γ] [nontrivial R] : nontrivial (hahn_series Γ R) := ⟨begin obtain ⟨r, s, rs⟩ := exists_pair_ne R, inhabit Γ, refine ⟨single (arbitrary Γ) r, single (arbitrary Γ) s, λ con, rs _⟩, rw [← single_coeff_same (arbitrary Γ) r, con, single_coeff_same], end⟩ section order variable [has_zero Γ] /-- The order of a nonzero Hahn series `x` is a minimal element of `Γ` where `x` has a nonzero coefficient, the order of 0 is 0. -/ def order (x : hahn_series Γ R) : Γ := if h : x = 0 then 0 else x.is_wf_support.min (support_nonempty_iff.2 h) @[simp] lemma order_zero : order (0 : hahn_series Γ R) = 0 := dif_pos rfl lemma order_of_ne {x : hahn_series Γ R} (hx : x ≠ 0) : order x = x.is_wf_support.min (support_nonempty_iff.2 hx) := dif_neg hx lemma coeff_order_ne_zero {x : hahn_series Γ R} (hx : x ≠ 0) : x.coeff x.order ≠ 0 := begin rw order_of_ne hx, exact x.is_wf_support.min_mem (support_nonempty_iff.2 hx) end lemma order_le_of_coeff_ne_zero {Γ} [linear_ordered_cancel_add_comm_monoid Γ] {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : x.order ≤ g := le_trans (le_of_eq (order_of_ne (ne_zero_of_coeff_ne_zero h))) (set.is_wf.min_le _ _ ((mem_support _ _).2 h)) @[simp] lemma order_single (h : r ≠ 0) : (single a r).order = a := (order_of_ne (single_ne_zero h)).trans (support_single_subset ((single a r).is_wf_support.min_mem (support_nonempty_iff.2 (single_ne_zero h)))) lemma coeff_eq_zero_of_lt_order {x : hahn_series Γ R} {i : Γ} (hi : i < x.order) : x.coeff i = 0 := begin rcases eq_or_ne x 0 with rfl|hx, { simp }, contrapose! hi, rw [←ne.def, ←mem_support] at hi, rw [order_of_ne hx], exact set.is_wf.not_lt_min _ _ hi end end order section domain variables {Γ' : Type*} [partial_order Γ'] /-- Extends the domain of a `hahn_series` by an `order_embedding`. -/ def emb_domain (f : Γ ↪o Γ') : hahn_series Γ R → hahn_series Γ' R := λ x, { coeff := λ (b : Γ'), if h : b ∈ f '' x.support then x.coeff (classical.some h) else 0, is_pwo_support' := (x.is_pwo_support.image_of_monotone f.monotone).mono (λ b hb, begin contrapose! hb, rw [function.mem_support, dif_neg hb, not_not], end) } @[simp] lemma emb_domain_coeff {f : Γ ↪o Γ'} {x : hahn_series Γ R} {a : Γ} : (emb_domain f x).coeff (f a) = x.coeff a := begin rw emb_domain, dsimp only, by_cases ha : a ∈ x.support, { rw dif_pos (set.mem_image_of_mem f ha), exact congr rfl (f.injective (classical.some_spec (set.mem_image_of_mem f ha)).2) }, { rw [dif_neg, not_not.1 (λ c, ha ((mem_support _ _).2 c))], contrapose! ha, obtain ⟨b, hb1, hb2⟩ := (set.mem_image _ _ _).1 ha, rwa f.injective hb2 at hb1 } end @[simp] lemma emb_domain_mk_coeff {f : Γ → Γ'} (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') {x : hahn_series Γ R} {a : Γ} : (emb_domain ⟨⟨f, hfi⟩, hf⟩ x).coeff (f a) = x.coeff a := emb_domain_coeff lemma emb_domain_notin_image_support {f : Γ ↪o Γ'} {x : hahn_series Γ R} {b : Γ'} (hb : b ∉ f '' x.support) : (emb_domain f x).coeff b = 0 := dif_neg hb lemma support_emb_domain_subset {f : Γ ↪o Γ'} {x : hahn_series Γ R} : support (emb_domain f x) ⊆ f '' x.support := begin intros g hg, contrapose! hg, rw [mem_support, emb_domain_notin_image_support hg, not_not], end lemma emb_domain_notin_range {f : Γ ↪o Γ'} {x : hahn_series Γ R} {b : Γ'} (hb : b ∉ set.range f) : (emb_domain f x).coeff b = 0 := emb_domain_notin_image_support (λ con, hb (set.image_subset_range _ _ con)) @[simp] lemma emb_domain_zero {f : Γ ↪o Γ'} : emb_domain f (0 : hahn_series Γ R) = 0 := by { ext, simp [emb_domain_notin_image_support] } @[simp] lemma emb_domain_single {f : Γ ↪o Γ'} {g : Γ} {r : R} : emb_domain f (single g r) = single (f g) r := begin ext g', by_cases h : g' = f g, { simp [h] }, rw [emb_domain_notin_image_support, single_coeff_of_ne h], by_cases hr : r = 0, { simp [hr] }, rwa [support_single_of_ne hr, set.image_singleton, set.mem_singleton_iff], end lemma emb_domain_injective {f : Γ ↪o Γ'} : function.injective (emb_domain f : hahn_series Γ R → hahn_series Γ' R) := λ x y xy, begin ext g, rw [ext_iff, function.funext_iff] at xy, have xyg := xy (f g), rwa [emb_domain_coeff, emb_domain_coeff] at xyg, end end domain end zero section addition variable [partial_order Γ] section add_monoid variable [add_monoid R] instance : has_add (hahn_series Γ R) := { add := λ x y, { coeff := x.coeff + y.coeff, is_pwo_support' := (x.is_pwo_support.union y.is_pwo_support).mono (function.support_add _ _) } } instance : add_monoid (hahn_series Γ R) := { zero := 0, add := (+), add_assoc := λ x y z, by { ext, apply add_assoc }, zero_add := λ x, by { ext, apply zero_add }, add_zero := λ x, by { ext, apply add_zero } } @[simp] lemma add_coeff' {x y : hahn_series Γ R} : (x + y).coeff = x.coeff + y.coeff := rfl lemma add_coeff {x y : hahn_series Γ R} {a : Γ} : (x + y).coeff a = x.coeff a + y.coeff a := rfl lemma support_add_subset {x y : hahn_series Γ R} : support (x + y) ⊆ support x ∪ support y := λ a ha, begin rw [mem_support, add_coeff] at ha, rw [set.mem_union, mem_support, mem_support], contrapose! ha, rw [ha.1, ha.2, add_zero], end lemma min_order_le_order_add {Γ} [linear_ordered_cancel_add_comm_monoid Γ] {x y : hahn_series Γ R} (hxy : x + y ≠ 0) : min x.order y.order ≤ (x + y).order := begin by_cases hx : x = 0, { simp [hx], }, by_cases hy : y = 0, { simp [hy], }, rw [order_of_ne hx, order_of_ne hy, order_of_ne hxy], refine le_trans _ (set.is_wf.min_le_min_of_subset support_add_subset), { exact x.is_wf_support.union y.is_wf_support }, { exact set.nonempty.mono (set.subset_union_left _ _) (support_nonempty_iff.2 hx) }, rw set.is_wf.min_union, end /-- `single` as an additive monoid/group homomorphism -/ @[simps] def single.add_monoid_hom (a : Γ) : R →+ (hahn_series Γ R) := { map_add' := λ x y, by { ext b, by_cases h : b = a; simp [h] }, ..single a } /-- `coeff g` as an additive monoid/group homomorphism -/ @[simps] def coeff.add_monoid_hom (g : Γ) : (hahn_series Γ R) →+ R := { to_fun := λ f, f.coeff g, map_zero' := zero_coeff, map_add' := λ x y, add_coeff } section domain variables {Γ' : Type*} [partial_order Γ'] lemma emb_domain_add (f : Γ ↪o Γ') (x y : hahn_series Γ R) : emb_domain f (x + y) = emb_domain f x + emb_domain f y := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨a, rfl⟩ := hg, simp }, { simp [emb_domain_notin_range, hg] } end end domain end add_monoid instance [add_comm_monoid R] : add_comm_monoid (hahn_series Γ R) := { add_comm := λ x y, by { ext, apply add_comm } .. hahn_series.add_monoid } section add_group variable [add_group R] instance : add_group (hahn_series Γ R) := { neg := λ x, { coeff := λ a, - x.coeff a, is_pwo_support' := by { rw function.support_neg, exact x.is_pwo_support }, }, add_left_neg := λ x, by { ext, apply add_left_neg }, .. hahn_series.add_monoid } @[simp] lemma neg_coeff' {x : hahn_series Γ R} : (- x).coeff = - x.coeff := rfl lemma neg_coeff {x : hahn_series Γ R} {a : Γ} : (- x).coeff a = - x.coeff a := rfl @[simp] lemma support_neg {x : hahn_series Γ R} : (- x).support = x.support := by { ext, simp } @[simp] lemma sub_coeff' {x y : hahn_series Γ R} : (x - y).coeff = x.coeff - y.coeff := by { ext, simp [sub_eq_add_neg] } lemma sub_coeff {x y : hahn_series Γ R} {a : Γ} : (x - y).coeff a = x.coeff a - y.coeff a := by simp end add_group instance [add_comm_group R] : add_comm_group (hahn_series Γ R) := { .. hahn_series.add_comm_monoid, .. hahn_series.add_group } end addition section distrib_mul_action variables [partial_order Γ] {V : Type*} [monoid R] [add_monoid V] [distrib_mul_action R V] instance : has_scalar R (hahn_series Γ V) := ⟨λ r x, { coeff := r • x.coeff, is_pwo_support' := x.is_pwo_support.mono (function.support_smul_subset_right r x.coeff) }⟩ @[simp] lemma smul_coeff {r : R} {x : hahn_series Γ V} {a : Γ} : (r • x).coeff a = r • (x.coeff a) := rfl instance : distrib_mul_action R (hahn_series Γ V) := { smul := (•), one_smul := λ _, by { ext, simp }, smul_zero := λ _, by { ext, simp }, smul_add := λ _ _ _, by { ext, simp [smul_add] }, mul_smul := λ _ _ _, by { ext, simp [mul_smul] } } variables {S : Type*} [monoid S] [distrib_mul_action S V] instance [has_scalar R S] [is_scalar_tower R S V] : is_scalar_tower R S (hahn_series Γ V) := ⟨λ r s a, by { ext, simp }⟩ instance [smul_comm_class R S V] : smul_comm_class R S (hahn_series Γ V) := ⟨λ r s a, by { ext, simp [smul_comm] }⟩ end distrib_mul_action section module variables [partial_order Γ] [semiring R] {V : Type*} [add_comm_monoid V] [module R V] instance : module R (hahn_series Γ V) := { zero_smul := λ _, by { ext, simp }, add_smul := λ _ _ _, by { ext, simp [add_smul] }, .. hahn_series.distrib_mul_action } /-- `single` as a linear map -/ @[simps] def single.linear_map (a : Γ) : R →ₗ[R] (hahn_series Γ R) := { map_smul' := λ r s, by { ext b, by_cases h : b = a; simp [h] }, ..single.add_monoid_hom a } /-- `coeff g` as a linear map -/ @[simps] def coeff.linear_map (g : Γ) : (hahn_series Γ R) →ₗ[R] R := { map_smul' := λ r s, rfl, ..coeff.add_monoid_hom g } section domain variables {Γ' : Type*} [partial_order Γ'] lemma emb_domain_smul (f : Γ ↪o Γ') (r : R) (x : hahn_series Γ R) : emb_domain f (r • x) = r • emb_domain f x := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨a, rfl⟩ := hg, simp }, { simp [emb_domain_notin_range, hg] } end /-- Extending the domain of Hahn series is a linear map. -/ @[simps] def emb_domain_linear_map (f : Γ ↪o Γ') : hahn_series Γ R →ₗ[R] hahn_series Γ' R := { to_fun := emb_domain f, map_add' := emb_domain_add f, map_smul' := emb_domain_smul f } end domain end module section multiplication variable [ordered_cancel_add_comm_monoid Γ] instance [has_zero R] [has_one R] : has_one (hahn_series Γ R) := ⟨single 0 1⟩ @[simp] lemma one_coeff [has_zero R] [has_one R] {a : Γ} : (1 : hahn_series Γ R).coeff a = if a = 0 then 1 else 0 := single_coeff @[simp] lemma single_zero_one [has_zero R] [has_one R] : (single 0 (1 : R)) = 1 := rfl @[simp] lemma support_one [mul_zero_one_class R] [nontrivial R] : support (1 : hahn_series Γ R) = {0} := support_single_of_ne one_ne_zero @[simp] lemma order_one [mul_zero_one_class R] : order (1 : hahn_series Γ R) = 0 := begin cases subsingleton_or_nontrivial R with h h; haveI := h, { rw [subsingleton.elim (1 : hahn_series Γ R) 0, order_zero] }, { exact order_single one_ne_zero } end instance [non_unital_non_assoc_semiring R] : has_mul (hahn_series Γ R) := { mul := λ x y, { coeff := λ a, ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd, is_pwo_support' := begin have h : {a : Γ | ∑ (ij : Γ × Γ) in add_antidiagonal x.is_pwo_support y.is_pwo_support a, x.coeff ij.fst * y.coeff ij.snd ≠ 0} ⊆ {a : Γ | (add_antidiagonal x.is_pwo_support y.is_pwo_support a).nonempty}, { intros a ha, contrapose! ha, simp [not_nonempty_iff_eq_empty.1 ha] }, exact is_pwo_support_add_antidiagonal.mono h, end, }, } @[simp] lemma mul_coeff [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} : (x * y).coeff a = ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd := rfl lemma mul_coeff_right' [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} {s : set Γ} (hs : s.is_pwo) (hys : y.support ⊆ s) : (x * y).coeff a = ∑ ij in (add_antidiagonal x.is_pwo_support hs a), x.coeff ij.fst * y.coeff ij.snd := begin rw mul_coeff, apply sum_subset_zero_on_sdiff (add_antidiagonal_mono_right hys) _ (λ _ _, rfl), intros b hb, simp only [not_and, not_not, mem_sdiff, mem_add_antidiagonal, ne.def, set.mem_set_of_eq, mem_support] at hb, rw [(hb.2 hb.1.1 hb.1.2.1), mul_zero] end lemma mul_coeff_left' [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} {a : Γ} {s : set Γ} (hs : s.is_pwo) (hxs : x.support ⊆ s) : (x * y).coeff a = ∑ ij in (add_antidiagonal hs y.is_pwo_support a), x.coeff ij.fst * y.coeff ij.snd := begin rw mul_coeff, apply sum_subset_zero_on_sdiff (add_antidiagonal_mono_left hxs) _ (λ _ _, rfl), intros b hb, simp only [not_and, not_not, mem_sdiff, mem_add_antidiagonal, ne.def, set.mem_set_of_eq, mem_support] at hb, rw [not_not.1 (λ con, hb.1.2.2 (hb.2 hb.1.1 con)), zero_mul], end instance [non_unital_non_assoc_semiring R] : distrib (hahn_series Γ R) := { left_distrib := λ x y z, begin ext a, have hwf := (y.is_pwo_support.union z.is_pwo_support), rw [mul_coeff_right' hwf, add_coeff, mul_coeff_right' hwf (set.subset_union_right _ _), mul_coeff_right' hwf (set.subset_union_left _ _)], { simp only [add_coeff, mul_add, sum_add_distrib] }, { intro b, simp only [add_coeff, ne.def, set.mem_union_eq, set.mem_set_of_eq, mem_support], contrapose!, intro h, rw [h.1, h.2, add_zero], } end, right_distrib := λ x y z, begin ext a, have hwf := (x.is_pwo_support.union y.is_pwo_support), rw [mul_coeff_left' hwf, add_coeff, mul_coeff_left' hwf (set.subset_union_right _ _), mul_coeff_left' hwf (set.subset_union_left _ _)], { simp only [add_coeff, add_mul, sum_add_distrib] }, { intro b, simp only [add_coeff, ne.def, set.mem_union_eq, set.mem_set_of_eq, mem_support], contrapose!, intro h, rw [h.1, h.2, add_zero], }, end, .. hahn_series.has_mul, .. hahn_series.has_add } lemma single_mul_coeff_add [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} {b : Γ} : ((single b r) * x).coeff (a + b) = r * x.coeff a := begin by_cases hr : r = 0, { simp [hr] }, simp only [hr, smul_coeff, mul_coeff, support_single_of_ne, ne.def, not_false_iff, smul_eq_mul], by_cases hx : x.coeff a = 0, { simp only [hx, mul_zero], rw [sum_congr _ (λ _ _, rfl), sum_empty], ext ⟨a1, a2⟩, simp only [not_mem_empty, not_and, set.mem_singleton_iff, not_not, mem_add_antidiagonal, set.mem_set_of_eq, iff_false], rintro h1 rfl h2, rw add_comm at h1, rw ← add_right_cancel h1 at hx, exact h2 hx, }, transitivity ∑ (ij : Γ × Γ) in {(b, a)}, (single b r).coeff ij.fst * x.coeff ij.snd, { apply sum_congr _ (λ _ _, rfl), ext ⟨a1, a2⟩, simp only [set.mem_singleton_iff, prod.mk.inj_iff, mem_add_antidiagonal, mem_singleton, set.mem_set_of_eq], split, { rintro ⟨h1, rfl, h2⟩, rw add_comm at h1, refine ⟨rfl, add_right_cancel h1⟩ }, { rintro ⟨rfl, rfl⟩, refine ⟨add_comm _ _, _⟩, simp [hx] } }, { simp } end lemma mul_single_coeff_add [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} {b : Γ} : (x * (single b r)).coeff (a + b) = x.coeff a * r := begin by_cases hr : r = 0, { simp [hr] }, simp only [hr, smul_coeff, mul_coeff, support_single_of_ne, ne.def, not_false_iff, smul_eq_mul], by_cases hx : x.coeff a = 0, { simp only [hx, zero_mul], rw [sum_congr _ (λ _ _, rfl), sum_empty], ext ⟨a1, a2⟩, simp only [not_mem_empty, not_and, set.mem_singleton_iff, not_not, mem_add_antidiagonal, set.mem_set_of_eq, iff_false], rintro h1 h2 rfl, rw ← add_right_cancel h1 at hx, exact h2 hx, }, transitivity ∑ (ij : Γ × Γ) in {(a,b)}, x.coeff ij.fst * (single b r).coeff ij.snd, { apply sum_congr _ (λ _ _, rfl), ext ⟨a1, a2⟩, simp only [set.mem_singleton_iff, prod.mk.inj_iff, mem_add_antidiagonal, mem_singleton, set.mem_set_of_eq], split, { rintro ⟨h1, h2, rfl⟩, refine ⟨add_right_cancel h1, rfl⟩ }, { rintro ⟨rfl, rfl⟩, simp [hx] } }, { simp } end @[simp] lemma mul_single_zero_coeff [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} : (x * (single 0 r)).coeff a = x.coeff a * r := by rw [← add_zero a, mul_single_coeff_add, add_zero] lemma single_zero_mul_coeff [non_unital_non_assoc_semiring R] {r : R} {x : hahn_series Γ R} {a : Γ} : ((single 0 r) * x).coeff a = r * x.coeff a := by rw [← add_zero a, single_mul_coeff_add, add_zero] @[simp] lemma single_zero_mul_eq_smul [semiring R] {r : R} {x : hahn_series Γ R} : (single 0 r) * x = r • x := by { ext, exact single_zero_mul_coeff } theorem support_mul_subset_add_support [non_unital_non_assoc_semiring R] {x y : hahn_series Γ R} : support (x * y) ⊆ support x + support y := begin apply set.subset.trans (λ x hx, _) support_add_antidiagonal_subset_add, { exact x.is_pwo_support }, { exact y.is_pwo_support }, contrapose! hx, simp only [not_nonempty_iff_eq_empty, ne.def, set.mem_set_of_eq] at hx, simp [hx], end lemma mul_coeff_order_add_order {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R] (x y : hahn_series Γ R) : (x * y).coeff (x.order + y.order) = x.coeff x.order * y.coeff y.order := begin by_cases hx : x = 0, { simp [hx], }, by_cases hy : y = 0, { simp [hy], }, rw [order_of_ne hx, order_of_ne hy, mul_coeff, finset.add_antidiagonal_min_add_min, finset.sum_singleton], end private lemma mul_assoc' [non_unital_semiring R] (x y z : hahn_series Γ R) : x * y * z = x * (y * z) := begin ext b, rw [mul_coeff_left' (x.is_pwo_support.add y.is_pwo_support) support_mul_subset_add_support, mul_coeff_right' (y.is_pwo_support.add z.is_pwo_support) support_mul_subset_add_support], simp only [mul_coeff, add_coeff, sum_mul, mul_sum, sum_sigma'], refine sum_bij_ne_zero (λ a has ha0, ⟨⟨a.2.1, a.2.2 + a.1.2⟩, ⟨a.2.2, a.1.2⟩⟩) _ _ _ _, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp only [true_and, set.image2_add, eq_self_iff_true, mem_add_antidiagonal, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq] at H1 H2 ⊢, obtain ⟨⟨rfl, ⟨H3, nz⟩⟩, ⟨rfl, nx, ny⟩⟩ := H1, refine ⟨⟨(add_assoc _ _ _).symm, nx, set.add_mem_add ny nz⟩, ny, nz⟩ }, { rintros ⟨⟨i1,j1⟩, ⟨k1,l1⟩⟩ ⟨⟨i2,j2⟩, ⟨k2,l2⟩⟩ H1 H2 H3 H4 H5, simp only [set.image2_add, prod.mk.inj_iff, mem_add_antidiagonal, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq, heq_iff_eq] at H1 H3 H5, obtain ⟨⟨rfl, H⟩, rfl, rfl⟩ := H5, simp only [and_true, prod.mk.inj_iff, eq_self_iff_true, heq_iff_eq], exact add_right_cancel (H1.1.1.trans H3.1.1.symm) }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp only [exists_prop, set.image2_add, prod.mk.inj_iff, mem_add_antidiagonal, sigma.exists, ne.def, set.image_prod, mem_sigma, set.mem_set_of_eq, heq_iff_eq, prod.exists] at H1 H2 ⊢, obtain ⟨⟨rfl, nx, H⟩, rfl, ny, nz⟩ := H1, exact ⟨i + k, l, i, k, ⟨⟨add_assoc _ _ _, set.add_mem_add nx ny, nz⟩, rfl, nx, ny⟩, λ con, H2 ((mul_assoc _ _ _).symm.trans con), ⟨rfl, rfl⟩, rfl, rfl⟩ }, { rintros ⟨⟨i,j⟩, ⟨k,l⟩⟩ H1 H2, simp [mul_assoc], } end instance [non_unital_non_assoc_semiring R] : non_unital_non_assoc_semiring (hahn_series Γ R) := { zero := 0, add := (+), mul := (*), zero_mul := λ _, by { ext, simp }, mul_zero := λ _, by { ext, simp }, .. hahn_series.add_comm_monoid, .. hahn_series.distrib } instance [non_unital_semiring R] : non_unital_semiring (hahn_series Γ R) := { zero := 0, add := (+), mul := (*), mul_assoc := mul_assoc', .. hahn_series.non_unital_non_assoc_semiring } instance [non_assoc_semiring R] : non_assoc_semiring (hahn_series Γ R) := { zero := 0, one := 1, add := (+), mul := (*), one_mul := λ x, by { ext, exact single_zero_mul_coeff.trans (one_mul _) }, mul_one := λ x, by { ext, exact mul_single_zero_coeff.trans (mul_one _) }, .. hahn_series.non_unital_non_assoc_semiring } instance [semiring R] : semiring (hahn_series Γ R) := { zero := 0, one := 1, add := (+), mul := (*), .. hahn_series.non_assoc_semiring, .. hahn_series.non_unital_semiring } instance [comm_semiring R] : comm_semiring (hahn_series Γ R) := { mul_comm := λ x y, begin ext, simp_rw [mul_coeff, mul_comm], refine sum_bij (λ a ha, ⟨a.2, a.1⟩) _ (λ a ha, by simp) _ _, { intros a ha, simp only [mem_add_antidiagonal, ne.def, set.mem_set_of_eq] at ha ⊢, obtain ⟨h1, h2, h3⟩ := ha, refine ⟨_, h3, h2⟩, rw [add_comm, h1], }, { rintros ⟨a1, a2⟩ ⟨b1, b2⟩ ha hb hab, rw prod.ext_iff at *, refine ⟨hab.2, hab.1⟩, }, { intros a ha, refine ⟨a.swap, _, by simp⟩, simp only [prod.fst_swap, mem_add_antidiagonal, prod.snd_swap, ne.def, set.mem_set_of_eq] at ha ⊢, exact ⟨(add_comm _ _).trans ha.1, ha.2.2, ha.2.1⟩ } end, .. hahn_series.semiring } instance [ring R] : ring (hahn_series Γ R) := { .. hahn_series.semiring, .. hahn_series.add_comm_group } instance [comm_ring R] : comm_ring (hahn_series Γ R) := { .. hahn_series.comm_semiring, .. hahn_series.ring } instance {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R] [no_zero_divisors R] : no_zero_divisors (hahn_series Γ R) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y xy, begin by_cases hx : x = 0, { left, exact hx }, right, contrapose! xy, rw [hahn_series.ext_iff, function.funext_iff, not_forall], refine ⟨x.order + y.order, _⟩, rw [mul_coeff_order_add_order x y, zero_coeff, mul_eq_zero], simp [coeff_order_ne_zero, hx, xy], end } instance {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [ring R] [is_domain R] : is_domain (hahn_series Γ R) := { .. hahn_series.no_zero_divisors, .. hahn_series.nontrivial, .. hahn_series.ring } @[simp] lemma order_mul {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [non_unital_non_assoc_semiring R] [no_zero_divisors R] {x y : hahn_series Γ R} (hx : x ≠ 0) (hy : y ≠ 0) : (x * y).order = x.order + y.order := begin apply le_antisymm, { apply order_le_of_coeff_ne_zero, rw [mul_coeff_order_add_order x y], exact mul_ne_zero (coeff_order_ne_zero hx) (coeff_order_ne_zero hy) }, { rw [order_of_ne hx, order_of_ne hy, order_of_ne (mul_ne_zero hx hy), ← set.is_wf.min_add], exact set.is_wf.min_le_min_of_subset (support_mul_subset_add_support) }, end @[simp] lemma order_pow {Γ} [linear_ordered_cancel_add_comm_monoid Γ] [semiring R] [no_zero_divisors R] (x : hahn_series Γ R) (n : ℕ) : (x ^ n).order = n • x.order := begin induction n with h IH, { simp }, rcases eq_or_ne x 0 with rfl|hx, { simp }, rw [pow_succ', order_mul (pow_ne_zero _ hx) hx, succ_nsmul', IH] end section non_unital_non_assoc_semiring variables [non_unital_non_assoc_semiring R] @[simp] lemma single_mul_single {a b : Γ} {r s : R} : single a r * single b s = single (a + b) (r * s) := begin ext x, by_cases h : x = a + b, { rw [h, mul_single_coeff_add], simp }, { rw [single_coeff_of_ne h, mul_coeff, sum_eq_zero], rintros ⟨y1, y2⟩ hy, obtain ⟨rfl, hy1, hy2⟩ := mem_add_antidiagonal.1 hy, rw [eq_of_mem_support_single hy1, eq_of_mem_support_single hy2] at h, exact (h rfl).elim } end end non_unital_non_assoc_semiring section non_assoc_semiring variables [non_assoc_semiring R] /-- `C a` is the constant Hahn Series `a`. `C` is provided as a ring homomorphism. -/ @[simps] def C : R →+* (hahn_series Γ R) := { to_fun := single 0, map_zero' := single_eq_zero, map_one' := rfl, map_add' := λ x y, by { ext a, by_cases h : a = 0; simp [h] }, map_mul' := λ x y, by rw [single_mul_single, zero_add] } @[simp] lemma C_zero : C (0 : R) = (0 : hahn_series Γ R) := C.map_zero @[simp] lemma C_one : C (1 : R) = (1 : hahn_series Γ R) := C.map_one lemma C_injective : function.injective (C : R → hahn_series Γ R) := begin intros r s rs, rw [ext_iff, function.funext_iff] at rs, have h := rs 0, rwa [C_apply, single_coeff_same, C_apply, single_coeff_same] at h, end lemma C_ne_zero {r : R} (h : r ≠ 0) : (C r : hahn_series Γ R) ≠ 0 := begin contrapose! h, rw ← C_zero at h, exact C_injective h, end lemma order_C {r : R} : order (C r : hahn_series Γ R) = 0 := begin by_cases h : r = 0, { rw [h, C_zero, order_zero] }, { exact order_single h } end end non_assoc_semiring section semiring variables [semiring R] lemma C_mul_eq_smul {r : R} {x : hahn_series Γ R} : C r * x = r • x := single_zero_mul_eq_smul end semiring section domain variables {Γ' : Type*} [ordered_cancel_add_comm_monoid Γ'] lemma emb_domain_mul [non_unital_non_assoc_semiring R] (f : Γ ↪o Γ') (hf : ∀ x y, f (x + y) = f x + f y) (x y : hahn_series Γ R) : emb_domain f (x * y) = emb_domain f x * emb_domain f y := begin ext g, by_cases hg : g ∈ set.range f, { obtain ⟨g, rfl⟩ := hg, simp only [mul_coeff, emb_domain_coeff], transitivity ∑ ij in (add_antidiagonal x.is_pwo_support y.is_pwo_support g).map (function.embedding.prod_map f.to_embedding f.to_embedding), (emb_domain f x).coeff (ij.1) * (emb_domain f y).coeff (ij.2), { simp }, apply sum_subset, { rintro ⟨i, j⟩ hij, simp only [exists_prop, mem_map, prod.mk.inj_iff, mem_add_antidiagonal, ne.def, function.embedding.coe_prod_map, mem_support, prod.exists] at hij, obtain ⟨i, j, ⟨rfl, hx, hy⟩, rfl, rfl⟩ := hij, simp [hx, hy, hf], }, { rintro ⟨_, _⟩ h1 h2, contrapose! h2, obtain ⟨i, hi, rfl⟩ := support_emb_domain_subset (ne_zero_and_ne_zero_of_mul h2).1, obtain ⟨j, hj, rfl⟩ := support_emb_domain_subset (ne_zero_and_ne_zero_of_mul h2).2, simp only [exists_prop, mem_map, prod.mk.inj_iff, mem_add_antidiagonal, ne.def, function.embedding.coe_prod_map, mem_support, prod.exists], simp only [mem_add_antidiagonal, emb_domain_coeff, ne.def, mem_support, ← hf] at h1, exact ⟨i, j, ⟨f.injective h1.1, h1.2⟩, rfl⟩, } }, { rw [emb_domain_notin_range hg, eq_comm], contrapose! hg, obtain ⟨_, _, hi, hj, rfl⟩ := support_mul_subset_add_support ((mem_support _ _).2 hg), obtain ⟨i, hi, rfl⟩ := support_emb_domain_subset hi, obtain ⟨j, hj, rfl⟩ := support_emb_domain_subset hj, refine ⟨i + j, hf i j⟩, } end lemma emb_domain_one [non_assoc_semiring R] (f : Γ ↪o Γ') (hf : f 0 = 0): emb_domain f (1 : hahn_series Γ R) = (1 : hahn_series Γ' R) := emb_domain_single.trans $ hf.symm ▸ rfl /-- Extending the domain of Hahn series is a ring homomorphism. -/ @[simps] def emb_domain_ring_hom [non_assoc_semiring R] (f : Γ →+ Γ') (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') : hahn_series Γ R →+* hahn_series Γ' R := { to_fun := emb_domain ⟨⟨f, hfi⟩, hf⟩, map_one' := emb_domain_one _ f.map_zero, map_mul' := emb_domain_mul _ f.map_add, map_zero' := emb_domain_zero, map_add' := emb_domain_add _} lemma emb_domain_ring_hom_C [non_assoc_semiring R] {f : Γ →+ Γ'} {hfi : function.injective f} {hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g'} {r : R} : emb_domain_ring_hom f hfi hf (C r) = C r := emb_domain_single.trans (by simp) end domain section algebra variables [comm_semiring R] {A : Type*} [semiring A] [algebra R A] instance : algebra R (hahn_series Γ A) := { to_ring_hom := C.comp (algebra_map R A), smul_def' := λ r x, by { ext, simp }, commutes' := λ r x, by { ext, simp only [smul_coeff, single_zero_mul_eq_smul, ring_hom.coe_comp, ring_hom.to_fun_eq_coe, C_apply, function.comp_app, algebra_map_smul, mul_single_zero_coeff], rw [← algebra.commutes, algebra.smul_def], }, } theorem C_eq_algebra_map : C = (algebra_map R (hahn_series Γ R)) := rfl theorem algebra_map_apply {r : R} : algebra_map R (hahn_series Γ A) r = C (algebra_map R A r) := rfl instance [nontrivial Γ] [nontrivial R] : nontrivial (subalgebra R (hahn_series Γ R)) := ⟨⟨⊥, ⊤, begin rw [ne.def, set_like.ext_iff, not_forall], obtain ⟨a, ha⟩ := exists_ne (0 : Γ), refine ⟨single a 1, _⟩, simp only [algebra.mem_bot, not_exists, set.mem_range, iff_true, algebra.mem_top], intros x, rw [ext_iff, function.funext_iff, not_forall], refine ⟨a, _⟩, rw [single_coeff_same, algebra_map_apply, C_apply, single_coeff_of_ne ha], exact zero_ne_one end⟩⟩ section domain variables {Γ' : Type*} [ordered_cancel_add_comm_monoid Γ'] /-- Extending the domain of Hahn series is an algebra homomorphism. -/ @[simps] def emb_domain_alg_hom (f : Γ →+ Γ') (hfi : function.injective f) (hf : ∀ g g' : Γ, f g ≤ f g' ↔ g ≤ g') : hahn_series Γ A →ₐ[R] hahn_series Γ' A := { commutes' := λ r, emb_domain_ring_hom_C, .. emb_domain_ring_hom f hfi hf } end domain end algebra end multiplication section semiring variables [semiring R] /-- The ring `hahn_series ℕ R` is isomorphic to `power_series R`. -/ @[simps] def to_power_series : (hahn_series ℕ R) ≃+* power_series R := { to_fun := λ f, power_series.mk f.coeff, inv_fun := λ f, ⟨λ n, power_series.coeff R n f, (nat.lt_wf.is_wf _).is_pwo⟩, left_inv := λ f, by { ext, simp }, right_inv := λ f, by { ext, simp }, map_add' := λ f g, by { ext, simp }, map_mul' := λ f g, begin ext n, simp only [power_series.coeff_mul, power_series.coeff_mk, mul_coeff, is_pwo_support], classical, refine sum_filter_ne_zero.symm.trans ((sum_congr _ (λ _ _, rfl)).trans sum_filter_ne_zero), ext m, simp only [nat.mem_antidiagonal, and.congr_left_iff, mem_add_antidiagonal, ne.def, and_iff_left_iff_imp, mem_filter, mem_support], intros h1 h2, contrapose h1, rw ← decidable.or_iff_not_and_not at h1, cases h1; simp [h1] end } lemma coeff_to_power_series {f : hahn_series ℕ R} {n : ℕ} : power_series.coeff R n f.to_power_series = f.coeff n := power_series.coeff_mk _ _ lemma coeff_to_power_series_symm {f : power_series R} {n : ℕ} : (hahn_series.to_power_series.symm f).coeff n = power_series.coeff R n f := rfl variables (Γ) (R) [ordered_semiring Γ] [nontrivial Γ] /-- Casts a power series as a Hahn series with coefficients from an `ordered_semiring`. -/ def of_power_series : (power_series R) →+* hahn_series Γ R := (hahn_series.emb_domain_ring_hom (nat.cast_add_monoid_hom Γ) nat.strict_mono_cast.injective (λ _ _, nat.cast_le)).comp (ring_equiv.to_ring_hom to_power_series.symm) variables {Γ} {R} lemma of_power_series_injective : function.injective (of_power_series Γ R) := emb_domain_injective.comp to_power_series.symm.injective @[simp] lemma of_power_series_apply (x : power_series R) : of_power_series Γ R x = hahn_series.emb_domain ⟨⟨(coe : ℕ → Γ), nat.strict_mono_cast.injective⟩, λ a b, begin simp only [function.embedding.coe_fn_mk], exact nat.cast_le, end⟩ (to_power_series.symm x) := rfl lemma of_power_series_apply_coeff (x : power_series R) (n : ℕ) : (of_power_series Γ R x).coeff n = power_series.coeff R n x := by simp @[simp] lemma of_power_series_C (r : R) : of_power_series Γ R (power_series.C R r) = hahn_series.C r := begin ext n, simp only [C, single_coeff, of_power_series_apply, ring_hom.coe_mk], split_ifs with hn hn, { rw hn, convert @emb_domain_coeff _ _ _ _ _ _ _ _ 0, simp }, { rw emb_domain_notin_image_support, simp only [not_exists, set.mem_image, to_power_series_symm_apply_coeff, mem_support, power_series.coeff_C], intro, simp [ne.symm hn] {contextual := tt} } end @[simp] lemma of_power_series_X : of_power_series Γ R power_series.X = single 1 1 := begin ext n, simp only [single_coeff, of_power_series_apply, ring_hom.coe_mk], split_ifs with hn hn, { rw hn, convert @emb_domain_coeff _ _ _ _ _ _ _ _ 1; simp }, { rw emb_domain_notin_image_support, simp only [not_exists, set.mem_image, to_power_series_symm_apply_coeff, mem_support, power_series.coeff_X], intro, simp [ne.symm hn] {contextual := tt} } end @[simp] lemma of_power_series_X_pow {R} [comm_semiring R] (n : ℕ) : of_power_series Γ R (power_series.X ^ n) = single (n : Γ) 1 := begin rw ring_hom.map_pow, induction n with n ih, { refl }, rw [pow_succ, ih, of_power_series_X, mul_comm, single_mul_single, one_mul, nat.cast_succ] end end semiring section algebra variables (R) [comm_semiring R] {A : Type*} [semiring A] [algebra R A] /-- The `R`-algebra `hahn_series ℕ A` is isomorphic to `power_series A`. -/ @[simps] def to_power_series_alg : (hahn_series ℕ A) ≃ₐ[R] power_series A := { commutes' := λ r, begin ext n, simp only [algebra_map_apply, power_series.algebra_map_apply, ring_equiv.to_fun_eq_coe, C_apply, coeff_to_power_series], cases n, { simp only [power_series.coeff_zero_eq_constant_coeff, single_coeff_same], refl }, { simp only [n.succ_ne_zero, ne.def, not_false_iff, single_coeff_of_ne], rw [power_series.coeff_C, if_neg n.succ_ne_zero] } end, .. to_power_series } variables (Γ) (R) [ordered_semiring Γ] [nontrivial Γ] /-- Casting a power series as a Hahn series with coefficients from an `ordered_semiring` is an algebra homomorphism. -/ @[simps] def of_power_series_alg : (power_series A) →ₐ[R] hahn_series Γ A := (hahn_series.emb_domain_alg_hom (nat.cast_add_monoid_hom Γ) nat.strict_mono_cast.injective (λ _ _, nat.cast_le)).comp (alg_equiv.to_alg_hom (to_power_series_alg R).symm) instance power_series_algebra {S : Type*} [comm_semiring S] [algebra S (power_series R)] : algebra S (hahn_series Γ R) := ring_hom.to_algebra $ (of_power_series Γ R).comp (algebra_map S (power_series R)) variables {R} {S : Type*} [comm_semiring S] [algebra S (power_series R)] lemma algebra_map_apply' (x : S) : algebra_map S (hahn_series Γ R) x = of_power_series Γ R (algebra_map S (power_series R) x) := rfl @[simp] lemma _root_.polynomial.algebra_map_hahn_series_apply (f : polynomial R) : algebra_map (polynomial R) (hahn_series Γ R) f = of_power_series Γ R f := rfl lemma _root_.polynomial.algebra_map_hahn_series_injective : function.injective (algebra_map (polynomial R) (hahn_series Γ R)) := of_power_series_injective.comp (polynomial.coe_injective R) end algebra section valuation variables [linear_ordered_add_comm_group Γ] [ring R] [is_domain R] instance : linear_ordered_comm_group (multiplicative Γ) := { .. (infer_instance : linear_order (multiplicative Γ)), .. (infer_instance : ordered_comm_group (multiplicative Γ)) } instance : linear_ordered_comm_group_with_zero (with_zero (multiplicative Γ)) := { zero_le_one := with_zero.zero_le 1, .. (with_zero.ordered_comm_monoid), .. (infer_instance : linear_order (with_zero (multiplicative Γ))), .. (infer_instance : comm_group_with_zero (with_zero (multiplicative Γ))) } variables (Γ) (R) /-- The additive valuation on `hahn_series Γ R`, returning the smallest index at which a Hahn Series has a nonzero coefficient, or `⊤` for the 0 series. -/ def add_val : add_valuation (hahn_series Γ R) (with_top Γ) := add_valuation.of (λ x, if x = (0 : hahn_series Γ R) then (⊤ : with_top Γ) else x.order) (if_pos rfl) ((if_neg one_ne_zero).trans (by simp [order_of_ne])) (λ x y, begin by_cases hx : x = 0, { by_cases hy : y = 0; { simp [hx, hy] } }, { by_cases hy : y = 0, { simp [hx, hy] }, { simp only [hx, hy, support_nonempty_iff, if_neg, not_false_iff, is_wf_support], by_cases hxy : x + y = 0, { simp [hxy] }, rw [if_neg hxy, ← with_top.coe_min, with_top.coe_le_coe], exact min_order_le_order_add hxy } }, end) (λ x y, begin by_cases hx : x = 0, { simp [hx] }, by_cases hy : y = 0, { simp [hy] }, rw [if_neg hx, if_neg hy, if_neg (mul_ne_zero hx hy), ← with_top.coe_add, with_top.coe_eq_coe, order_mul hx hy], end) variables {Γ} {R} lemma add_val_apply {x : hahn_series Γ R} : add_val Γ R x = if x = (0 : hahn_series Γ R) then (⊤ : with_top Γ) else x.order := add_valuation.of_apply _ @[simp] lemma add_val_apply_of_ne {x : hahn_series Γ R} (hx : x ≠ 0) : add_val Γ R x = x.order := if_neg hx lemma add_val_le_of_coeff_ne_zero {x : hahn_series Γ R} {g : Γ} (h : x.coeff g ≠ 0) : add_val Γ R x ≤ g := begin rw [add_val_apply_of_ne (ne_zero_of_coeff_ne_zero h), with_top.coe_le_coe], exact order_le_of_coeff_ne_zero h end end valuation lemma is_pwo_Union_support_powers [linear_ordered_add_comm_group Γ] [ring R] [is_domain R] {x : hahn_series Γ R} (hx : 0 < add_val Γ R x) : (⋃ n : ℕ, (x ^ n).support).is_pwo := begin apply (x.is_wf_support.is_pwo.add_submonoid_closure (λ g hg, _)).mono _, { exact with_top.coe_le_coe.1 (le_trans (le_of_lt hx) (add_val_le_of_coeff_ne_zero hg)) }, refine set.Union_subset (λ n, _), induction n with n ih; intros g hn, { simp only [exists_prop, and_true, set.mem_singleton_iff, set.set_of_eq_eq_singleton, mem_support, ite_eq_right_iff, ne.def, not_false_iff, one_ne_zero, pow_zero, not_forall, one_coeff] at hn, rw [hn, set_like.mem_coe], exact add_submonoid.zero_mem _ }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support hn, exact set_like.mem_coe.2 (add_submonoid.add_mem _ (add_submonoid.subset_closure hi) (ih hj)) } end section variables (Γ) (R) [partial_order Γ] [add_comm_monoid R] /-- An infinite family of Hahn series which has a formal coefficient-wise sum. The requirements for this are that the union of the supports of the series is well-founded, and that only finitely many series are nonzero at any given coefficient. -/ structure summable_family (α : Type*) := (to_fun : α → hahn_series Γ R) (is_pwo_Union_support' : set.is_pwo (⋃ (a : α), (to_fun a).support)) (finite_co_support' : ∀ (g : Γ), ({a | (to_fun a).coeff g ≠ 0}).finite) end namespace summable_family section add_comm_monoid variables [partial_order Γ] [add_comm_monoid R] {α : Type*} instance : has_coe_to_fun (summable_family Γ R α) (λ _, α → hahn_series Γ R):= ⟨to_fun⟩ lemma is_pwo_Union_support (s : summable_family Γ R α) : set.is_pwo (⋃ (a : α), (s a).support) := s.is_pwo_Union_support' lemma finite_co_support (s : summable_family Γ R α) (g : Γ) : (function.support (λ a, (s a).coeff g)).finite := s.finite_co_support' g lemma coe_injective : @function.injective (summable_family Γ R α) (α → hahn_series Γ R) coe_fn | ⟨f1, hU1, hf1⟩ ⟨f2, hU2, hf2⟩ h := begin change f1 = f2 at h, subst h, end @[ext] lemma ext {s t : summable_family Γ R α} (h : ∀ (a : α), s a = t a) : s = t := coe_injective $ funext h instance : has_add (summable_family Γ R α) := ⟨λ x y, { to_fun := x + y, is_pwo_Union_support' := (x.is_pwo_Union_support.union y.is_pwo_Union_support).mono (begin rw ← set.Union_union_distrib, exact set.Union_mono (λ a, support_add_subset) end), finite_co_support' := λ g, ((x.finite_co_support g).union (y.finite_co_support g)).subset begin intros a ha, change (x a).coeff g + (y a).coeff g ≠ 0 at ha, rw [set.mem_union, function.mem_support, function.mem_support], contrapose! ha, rw [ha.1, ha.2, add_zero] end }⟩ instance : has_zero (summable_family Γ R α) := ⟨⟨0, by simp, by simp⟩⟩ instance : inhabited (summable_family Γ R α) := ⟨0⟩ @[simp] lemma coe_add {s t : summable_family Γ R α} : ⇑(s + t) = s + t := rfl lemma add_apply {s t : summable_family Γ R α} {a : α} : (s + t) a = s a + t a := rfl @[simp] lemma coe_zero : ((0 : summable_family Γ R α) : α → hahn_series Γ R) = 0 := rfl lemma zero_apply {a : α} : (0 : summable_family Γ R α) a = 0 := rfl instance : add_comm_monoid (summable_family Γ R α) := { add := (+), zero := 0, zero_add := λ s, by { ext, apply zero_add }, add_zero := λ s, by { ext, apply add_zero }, add_comm := λ s t, by { ext, apply add_comm }, add_assoc := λ r s t, by { ext, apply add_assoc } } /-- The infinite sum of a `summable_family` of Hahn series. -/ def hsum (s : summable_family Γ R α) : hahn_series Γ R := { coeff := λ g, ∑ᶠ i, (s i).coeff g, is_pwo_support' := s.is_pwo_Union_support.mono (λ g, begin contrapose, rw [set.mem_Union, not_exists, function.mem_support, not_not], simp_rw [mem_support, not_not], intro h, rw [finsum_congr h, finsum_zero], end) } @[simp] lemma hsum_coeff {s : summable_family Γ R α} {g : Γ} : s.hsum.coeff g = ∑ᶠ i, (s i).coeff g := rfl lemma support_hsum_subset {s : summable_family Γ R α} : s.hsum.support ⊆ ⋃ (a : α), (s a).support := λ g hg, begin rw [mem_support, hsum_coeff, finsum_eq_sum _ (s.finite_co_support _)] at hg, obtain ⟨a, h1, h2⟩ := exists_ne_zero_of_sum_ne_zero hg, rw [set.mem_Union], exact ⟨a, h2⟩, end @[simp] lemma hsum_add {s t : summable_family Γ R α} : (s + t).hsum = s.hsum + t.hsum := begin ext g, simp only [hsum_coeff, add_coeff, add_apply], exact finsum_add_distrib (s.finite_co_support _) (t.finite_co_support _) end end add_comm_monoid section add_comm_group variables [partial_order Γ] [add_comm_group R] {α : Type*} {s t : summable_family Γ R α} {a : α} instance : add_comm_group (summable_family Γ R α) := { neg := λ s, { to_fun := λ a, - s a, is_pwo_Union_support' := by { simp_rw [support_neg], exact s.is_pwo_Union_support' }, finite_co_support' := λ g, by { simp only [neg_coeff', pi.neg_apply, ne.def, neg_eq_zero], exact s.finite_co_support g } }, add_left_neg := λ a, by { ext, apply add_left_neg }, .. summable_family.add_comm_monoid } @[simp] lemma coe_neg : ⇑(-s) = - s := rfl lemma neg_apply : (-s) a = - (s a) := rfl @[simp] lemma coe_sub : ⇑(s - t) = s - t := rfl lemma sub_apply : (s - t) a = s a - t a := rfl end add_comm_group section semiring variables [ordered_cancel_add_comm_monoid Γ] [semiring R] {α : Type*} instance : has_scalar (hahn_series Γ R) (summable_family Γ R α) := { smul := λ x s, { to_fun := λ a, x * (s a), is_pwo_Union_support' := begin apply (x.is_pwo_support.add s.is_pwo_Union_support).mono, refine set.subset.trans (set.Union_mono (λ a, support_mul_subset_add_support)) _, intro g, simp only [set.mem_Union, exists_imp_distrib], exact λ a ha, (set.add_subset_add (set.subset.refl _) (set.subset_Union _ a)) ha, end, finite_co_support' := λ g, begin refine ((add_antidiagonal x.is_pwo_support s.is_pwo_Union_support g).finite_to_set.bUnion (λ ij hij, _)).subset (λ a ha, _), { exact λ ij hij, function.support (λ a, (s a).coeff ij.2) }, { apply s.finite_co_support }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support ha, simp only [exists_prop, set.mem_Union, mem_add_antidiagonal, mul_coeff, ne.def, mem_support, is_pwo_support, prod.exists], refine ⟨i, j, mem_coe.2 (mem_add_antidiagonal.2 ⟨rfl, hi, set.mem_Union.2 ⟨a, hj⟩⟩), hj⟩, } end } } @[simp] lemma smul_apply {x : hahn_series Γ R} {s : summable_family Γ R α} {a : α} : (x • s) a = x * (s a) := rfl instance : module (hahn_series Γ R) (summable_family Γ R α) := { smul := (•), smul_zero := λ x, ext (λ a, mul_zero _), zero_smul := λ x, ext (λ a, zero_mul _), one_smul := λ x, ext (λ a, one_mul _), add_smul := λ x y s, ext (λ a, add_mul _ _ _), smul_add := λ x s t, ext (λ a, mul_add _ _ _), mul_smul := λ x y s, ext (λ a, mul_assoc _ _ _) } @[simp] lemma hsum_smul {x : hahn_series Γ R} {s : summable_family Γ R α} : (x • s).hsum = x * s.hsum := begin ext g, simp only [mul_coeff, hsum_coeff, smul_apply], have h : ∀ i, (s i).support ⊆ ⋃ j, (s j).support := set.subset_Union _, refine (eq.trans (finsum_congr (λ a, _)) (finsum_sum_comm (add_antidiagonal x.is_pwo_support s.is_pwo_Union_support g) (λ i ij, x.coeff (prod.fst ij) * (s i).coeff ij.snd) _)).trans _, { refine sum_subset (add_antidiagonal_mono_right (set.subset_Union _ a)) _, rintro ⟨i, j⟩ hU ha, rw mem_add_antidiagonal at *, rw [not_not.1 (λ con, ha ⟨hU.1, hU.2.1, con⟩), mul_zero] }, { rintro ⟨i, j⟩ hij, refine (s.finite_co_support j).subset _, simp_rw [function.support_subset_iff', function.mem_support, not_not], intros a ha, rw [ha, mul_zero] }, { refine (sum_congr rfl _).trans (sum_subset (add_antidiagonal_mono_right _) _).symm, { rintro ⟨i, j⟩ hij, rw mul_finsum, apply s.finite_co_support, }, { intros x hx, simp only [set.mem_Union, ne.def, mem_support], contrapose! hx, simp [hx] }, { rintro ⟨i, j⟩ hU ha, rw mem_add_antidiagonal at *, rw [← hsum_coeff, not_not.1 (λ con, ha ⟨hU.1, hU.2.1, con⟩), mul_zero] } } end /-- The summation of a `summable_family` as a `linear_map`. -/ @[simps] def lsum : (summable_family Γ R α) →ₗ[hahn_series Γ R] (hahn_series Γ R) := { to_fun := hsum, map_add' := λ _ _, hsum_add, map_smul' := λ _ _, hsum_smul } @[simp] lemma hsum_sub {R : Type*} [ring R] {s t : summable_family Γ R α} : (s - t).hsum = s.hsum - t.hsum := by rw [← lsum_apply, linear_map.map_sub, lsum_apply, lsum_apply] end semiring section of_finsupp variables [partial_order Γ] [add_comm_monoid R] {α : Type*} /-- A family with only finitely many nonzero elements is summable. -/ def of_finsupp (f : α →₀ (hahn_series Γ R)) : summable_family Γ R α := { to_fun := f, is_pwo_Union_support' := begin apply (f.support.is_pwo_sup (λ a, (f a).support) (λ a ha, (f a).is_pwo_support)).mono, intros g hg, obtain ⟨a, ha⟩ := set.mem_Union.1 hg, have haf : a ∈ f.support, { rw finsupp.mem_support_iff, contrapose! ha, rw [ha, support_zero], exact set.not_mem_empty _ }, have h : (λ i, (f i).support) a ≤ _ := le_sup haf, exact h ha, end, finite_co_support' := λ g, begin refine f.support.finite_to_set.subset (λ a ha, _), simp only [coeff.add_monoid_hom_apply, mem_coe, finsupp.mem_support_iff, ne.def, function.mem_support], contrapose! ha, simp [ha] end } @[simp] lemma coe_of_finsupp {f : α →₀ (hahn_series Γ R)} : ⇑(summable_family.of_finsupp f) = f := rfl @[simp] lemma hsum_of_finsupp {f : α →₀ (hahn_series Γ R)} : (of_finsupp f).hsum = f.sum (λ a, id) := begin ext g, simp only [hsum_coeff, coe_of_finsupp, finsupp.sum, ne.def], simp_rw [← coeff.add_monoid_hom_apply, id.def], rw [add_monoid_hom.map_sum, finsum_eq_sum_of_support_subset], intros x h, simp only [coeff.add_monoid_hom_apply, mem_coe, finsupp.mem_support_iff, ne.def], contrapose! h, simp [h] end end of_finsupp section emb_domain variables [partial_order Γ] [add_comm_monoid R] {α β : Type*} /-- A summable family can be reindexed by an embedding without changing its sum. -/ def emb_domain (s : summable_family Γ R α) (f : α ↪ β) : summable_family Γ R β := { to_fun := λ b, if h : b ∈ set.range f then s (classical.some h) else 0, is_pwo_Union_support' := begin refine s.is_pwo_Union_support.mono (set.Union_subset (λ b g h, _)), by_cases hb : b ∈ set.range f, { rw dif_pos hb at h, exact set.mem_Union.2 ⟨classical.some hb, h⟩ }, { contrapose! h, simp [hb] } end, finite_co_support' := λ g, ((s.finite_co_support g).image f).subset begin intros b h, by_cases hb : b ∈ set.range f, { simp only [ne.def, set.mem_set_of_eq, dif_pos hb] at h, exact ⟨classical.some hb, h, classical.some_spec hb⟩ }, { contrapose! h, simp only [ne.def, set.mem_set_of_eq, dif_neg hb, not_not, zero_coeff] } end } variables (s : summable_family Γ R α) (f : α ↪ β) {a : α} {b : β} lemma emb_domain_apply : s.emb_domain f b = if h : b ∈ set.range f then s (classical.some h) else 0 := rfl @[simp] lemma emb_domain_image : s.emb_domain f (f a) = s a := begin rw [emb_domain_apply, dif_pos (set.mem_range_self a)], exact congr rfl (f.injective (classical.some_spec (set.mem_range_self a))) end @[simp] lemma emb_domain_notin_range (h : b ∉ set.range f) : s.emb_domain f b = 0 := by rw [emb_domain_apply, dif_neg h] @[simp] lemma hsum_emb_domain : (s.emb_domain f).hsum = s.hsum := begin ext g, simp only [hsum_coeff, emb_domain_apply, apply_dite hahn_series.coeff, dite_apply, zero_coeff], exact finsum_emb_domain f (λ a, (s a).coeff g) end end emb_domain section powers variables [linear_ordered_add_comm_group Γ] [comm_ring R] [is_domain R] /-- The powers of an element of positive valuation form a summable family. -/ def powers (x : hahn_series Γ R) (hx : 0 < add_val Γ R x) : summable_family Γ R ℕ := { to_fun := λ n, x ^ n, is_pwo_Union_support' := is_pwo_Union_support_powers hx, finite_co_support' := λ g, begin have hpwo := (is_pwo_Union_support_powers hx), by_cases hg : g ∈ ⋃ n : ℕ, {g | (x ^ n).coeff g ≠ 0 }, swap, { exact set.finite_empty.subset (λ n hn, hg (set.mem_Union.2 ⟨n, hn⟩)) }, apply hpwo.is_wf.induction hg, intros y ys hy, refine ((((add_antidiagonal x.is_pwo_support hpwo y).finite_to_set.bUnion (λ ij hij, hy ij.snd _ _)).image nat.succ).union (set.finite_singleton 0)).subset _, { exact (mem_add_antidiagonal.1 (mem_coe.1 hij)).2.2 }, { obtain ⟨rfl, hi, hj⟩ := mem_add_antidiagonal.1 (mem_coe.1 hij), rw [← zero_add ij.snd, ← add_assoc, add_zero], exact add_lt_add_right (with_top.coe_lt_coe.1 (lt_of_lt_of_le hx (add_val_le_of_coeff_ne_zero hi))) _, }, { intros n hn, cases n, { exact set.mem_union_right _ (set.mem_singleton 0) }, { obtain ⟨i, j, hi, hj, rfl⟩ := support_mul_subset_add_support hn, refine set.mem_union_left _ ⟨n, set.mem_Union.2 ⟨⟨i, j⟩, set.mem_Union.2 ⟨_, hj⟩⟩, rfl⟩, simp only [true_and, set.mem_Union, mem_add_antidiagonal, mem_coe, eq_self_iff_true, ne.def, mem_support, set.mem_set_of_eq], exact ⟨hi, ⟨n, hj⟩⟩ } } end } variables {x : hahn_series Γ R} (hx : 0 < add_val Γ R x) @[simp] lemma coe_powers : ⇑(powers x hx) = pow x := rfl lemma emb_domain_succ_smul_powers : (x • powers x hx).emb_domain ⟨nat.succ, nat.succ_injective⟩ = powers x hx - of_finsupp (finsupp.single 0 1) := begin apply summable_family.ext (λ n, _), cases n, { rw [emb_domain_notin_range, sub_apply, coe_powers, pow_zero, coe_of_finsupp, finsupp.single_eq_same, sub_self], rw [set.mem_range, not_exists], exact nat.succ_ne_zero }, { refine eq.trans (emb_domain_image _ ⟨nat.succ, nat.succ_injective⟩) _, simp only [pow_succ, coe_powers, coe_sub, smul_apply, coe_of_finsupp, pi.sub_apply], rw [finsupp.single_eq_of_ne (n.succ_ne_zero).symm, sub_zero] } end lemma one_sub_self_mul_hsum_powers : (1 - x) * (powers x hx).hsum = 1 := begin rw [← hsum_smul, sub_smul, one_smul, hsum_sub, ← hsum_emb_domain (x • powers x hx) ⟨nat.succ, nat.succ_injective⟩, emb_domain_succ_smul_powers], simp, end end powers end summable_family section inversion variables [linear_ordered_add_comm_group Γ] section is_domain variables [comm_ring R] [is_domain R] lemma unit_aux (x : hahn_series Γ R) {r : R} (hr : r * x.coeff x.order = 1) : 0 < add_val Γ R (1 - C r * (single (- x.order) 1) * x) := begin have h10 : (1 : R) ≠ 0 := one_ne_zero, have x0 : x ≠ 0 := ne_zero_of_coeff_ne_zero (right_ne_zero_of_mul_eq_one hr), refine lt_of_le_of_ne ((add_val Γ R).map_le_sub (ge_of_eq (add_val Γ R).map_one) _) _, { simp only [add_valuation.map_mul], rw [add_val_apply_of_ne x0, add_val_apply_of_ne (single_ne_zero h10), add_val_apply_of_ne _, order_C, order_single h10, with_top.coe_zero, zero_add, ← with_top.coe_add, neg_add_self, with_top.coe_zero], { exact le_refl 0 }, { exact C_ne_zero (left_ne_zero_of_mul_eq_one hr) } }, { rw [add_val_apply, ← with_top.coe_zero], split_ifs, { apply with_top.coe_ne_top }, rw [ne.def, with_top.coe_eq_coe], intro con, apply coeff_order_ne_zero h, rw [← con, mul_assoc, sub_coeff, one_coeff, if_pos rfl, C_mul_eq_smul, smul_coeff, smul_eq_mul, ← add_neg_self x.order, single_mul_coeff_add, one_mul, hr, sub_self] } end lemma is_unit_iff {x : hahn_series Γ R} : is_unit x ↔ is_unit (x.coeff x.order) := begin split, { rintro ⟨⟨u, i, ui, iu⟩, rfl⟩, refine is_unit_of_mul_eq_one (u.coeff u.order) (i.coeff i.order) ((mul_coeff_order_add_order u i).symm.trans _), rw [ui, one_coeff, if_pos], rw [← order_mul (left_ne_zero_of_mul_eq_one ui) (right_ne_zero_of_mul_eq_one ui), ui, order_one] }, { rintro ⟨⟨u, i, ui, iu⟩, h⟩, rw [units.coe_mk] at h, rw h at iu, have h := summable_family.one_sub_self_mul_hsum_powers (unit_aux x iu), rw [sub_sub_cancel] at h, exact is_unit_of_mul_is_unit_right (is_unit_of_mul_eq_one _ _ h) }, end end is_domain instance [field R] : field (hahn_series Γ R) := { inv := λ x, if x0 : x = 0 then 0 else (C (x.coeff x.order)⁻¹ * (single (-x.order)) 1 * (summable_family.powers _ (unit_aux x (inv_mul_cancel (coeff_order_ne_zero x0)))).hsum), inv_zero := dif_pos rfl, mul_inv_cancel := λ x x0, begin refine (congr rfl (dif_neg x0)).trans _, have h := summable_family.one_sub_self_mul_hsum_powers (unit_aux x (inv_mul_cancel (coeff_order_ne_zero x0))), rw [sub_sub_cancel] at h, rw [← mul_assoc, mul_comm x, h], end, .. hahn_series.is_domain, .. hahn_series.comm_ring } end inversion end hahn_series
49b98a73e0986b3a45e9269665f3228a23b882cb
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/group_theory/perm/sign.lean
a7941b0eb55b4f7ea1799133d8c34fca800322b6
[ "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
33,382
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 import algebra.big_operators universes u v open equiv function fintype finset variables {α : Type u} {β : Type v} namespace equiv.perm 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, λ _, by simp⟩ @[simp] lemma subtype_perm_one (p : α → Prop) (h : ∀ x, p x ↔ p ((1 : perm α) x)) : @subtype_perm α 1 p h = 1 := equiv.ext _ _ $ λ ⟨_, _⟩, rfl def of_subtype {p : α → Prop} [decidable_pred p] (f : perm (subtype p)) : perm α := ⟨λ 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; split_ifs at *; simp * at *, λ x, have h : ∀ h : p x, p (f⁻¹ ⟨x, h⟩), from λ h, (f⁻¹ ⟨x, h⟩).2, by simp; split_ifs at *; simp * at *⟩ instance of_subtype.is_group_hom {p : α → Prop} [decidable_pred p] : is_group_hom (@of_subtype α p _) := { map_mul := λ f g, equiv.ext _ _ $ λ x, begin rw [of_subtype, of_subtype, of_subtype], 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 [h, h₁, h₂] }, { simp [h] } end } @[simp] lemma of_subtype_one (p : α → Prop) [decidable_pred p] : @of_subtype α p _ 1 = 1 := is_group_hom.map_one of_subtype 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] 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 [disjoint, or.comm] 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 := begin induction hp, { refl }, { rw [list.prod_cons, list.prod_cons, hp_ih (list.pairwise_cons.1 hl).2] }, { simp [list.prod_cons, disjoint_mul_comm, (mul_assoc _ _ _).symm, *, list.pairwise_cons] at * }, { rw [hp_ih_a hl, hp_ih_a_1 ((list.perm_pairwise (λ x y (h : disjoint x y), disjoint.symm h) hp_a).1 hl)] } end 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 [hx] }, { haveI := classical.prop_decidable, simp [hx, not_not.1 (mt (h₂ x) hx)] } 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, 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, 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 α] 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 [support] 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 [mul_apply, swap_apply_def], split_ifs; simp [*, eq_inv_iff_eq] at * <|> cc 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 at hxy; tauto, equiv.ext _ _ $ λ z, begin rw [hxy.2, of_subtype], simp [swap_apply_def], split_ifs; cc <|> simp * at * 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 * 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.2 $ λ 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 * at *⟩, λ hy, by simp only [mem_erase, mem_support, swap_apply_def, mul_apply] at *; intro; split_ifs at *; simp * 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))⟩ 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 _)) 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 [hl.1.symm] {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.1).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 [fin_pairs_lt, fin.lt_def] def sign_aux {n : ℕ} (a : perm (fin n)) : units ℤ := (fin_pairs_lt n).prod (λ x, 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 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 [*, injective.eq_iff f.injective, sigma.mk.inj_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 [not_le_of_gt hab]; congr }, { 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 instance sign_aux.is_group_hom {n : ℕ} : is_group_hom (@sign_aux n) := { map_mul := sign_aux_mul } 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 _ = (finset.singleton (⟨one, zero⟩ : Σ a : fin n, fin n)).prod (λ x : Σ 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 [*, lt_irrefl, -not_lt, not_le.symm, -not_le, le_refl, fin.lt_def, fin.le_def, nat.zero_le, zero, one, iff.intro fin.veq_of_eq fin.eq_of_veq, nat.le_zero_iff] 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.of sign_aux).map_is_conj (is_conj_swap hxy dec_trivial) 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, from equiv.ext _ _ (λ z, by rw ← equiv.symm_trans_swap_trans; simp [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 } end 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 [list.mem_of_perm h]) (λ 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 α] (f : perm α) := sign_aux3 f mem_univ instance sign.is_group_hom [fintype α] : is_group_hom (@sign α _ _) := { map_mul := λ 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 := is_mul_hom.map_mul sign _ _ @[simp] lemma sign_one : (sign (1 : perm α)) = 1 := is_group_hom.map_one sign @[simp] lemma sign_refl : sign (equiv.refl α) = 1 := is_group_hom.map_one sign @[simp] lemma sign_inv (f : perm α) : sign f⁻¹ = sign f := by rw [is_group_hom.map_inv sign, int.units_inv_eq_self]; apply_instance 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))) 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 ℤ} [is_group_hom s] (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), funext $ λ 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 (show function.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})), from ⟨λ ⟨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) def is_cycle (f : perm β) := ∃ x, f x ≠ x ∧ ∀ y, f y ≠ y → ∃ i : ℤ, (f ^ i) x = y lemma is_cycle_swap {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 [eq_inv_iff_eq, inv_eq_iff_eq, *] at *; cc, λ y hy, let ⟨i, hi⟩ := hx.2 y (by simp [eq_inv_iff_eq, inv_eq_iff_eq, *] 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₁ : ∀ (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₂ : ∀ (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 [inv_eq_iff_eq, eq_inv_iff_eq] 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, ← 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 {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 {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 [fintype α] {x y : α} (hxy : x ≠ y) : (swap x y).support = {x, y} := finset.ext.2 $ λ a, by simp [swap_apply_def]; split_ifs; cc lemma card_support_swap [fintype α] {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]; cc lemma sign_cycle [fintype α] : ∀ {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, by conv in (f) {rw eq_swap_of_is_cycle_of_apply_apply_eq_self hf hx.1 h1 }; simp [mul_def, one_def], 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 [pow_add] using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ f, f.support.card)⟩]} end sign end equiv.perm lemma finset.prod_univ_perm [fintype α] [comm_monoid β] {f : α → β} (σ : perm α) : (univ : finset α).prod f = univ.prod (λ z, f (σ z)) := eq.symm $ prod_bij (λ z _, σ z) (λ _ _, mem_univ _) (λ _ _, rfl) (λ _ _ _ _ H, σ.injective H) (λ b _, ⟨σ⁻¹ b, mem_univ _, by simp⟩) lemma finset.sum_univ_perm [fintype α] [add_comm_monoid β] {f : α → β} (σ : perm α) : (univ : finset α).sum f = univ.sum (λ z, f (σ z)) := @finset.prod_univ_perm _ (multiplicative β) _ _ f σ attribute [to_additive] finset.prod_univ_perm
fd58b0c3f4974c8d14826b898f306badfe5a439e
570f46c4bf91dd2d8b97fc9d43ce1c435867f7dc
/src/exercises/00_first_proofs.lean
6431b343b75f4952d18bf7e6899551b58d0ccece
[ "Apache-2.0" ]
permissive
Timeroot/tutorials
ef586f20e059c2cfa2b044e5663ea98f32c86646
ff7a6c1ad58b2eedfdc7303661e19409656467ab
refs/heads/master
1,691,446,431,254
1,633,556,762,000
1,633,556,762,000
414,377,840
1
0
null
null
null
null
UTF-8
Lean
false
false
18,052
lean
/- This file is intended for Lean beginners. The goal is to demonstrate what it feels like to prove things using Lean and mathlib. Complicated definitions and theory building are not covered. Everything is covered again more slowly and with exercises in the next files. -/ -- We want real numbers and their basic properties import data.real.basic -- We want to be able to use Lean's built-in "help" functionality import tactic.suggest -- We want to be able to define functions using the law of excluded middle noncomputable theory open_locale classical /- Our first goal is to define the set of upper bounds of a set of real numbers. This is already defined in mathlib (in a more general context), but we repeat it for the sake of exposition. Right-click "upper_bounds" below to get offered to jump to mathlib's version -/ #check upper_bounds /-- The set of upper bounds of a set of real numbers ℝ -/ def up_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, a ≤ x} /-- Predicate `is_max a A` means `a` is a maximum of `A` -/ def is_max (a : ℝ) (A : set ℝ) := a ∈ A ∧ a ∈ up_bounds A /- In the above definition, the symbol `∧` means "and". We also see the most visible difference between set theoretic foundations and type theoretic ones (used by almost all proof assistants). In set theory, everything is a set, and the only relation you get from foundations are `=` and `∈`. In type theory, there is a meta-theoretic relation of "typing": `a : ℝ` reads "`a` is a real number" or, more precisely, "the type of `a` is `ℝ`". Here "meta-theoretic" means this is not a statement you can prove or disprove inside the theory, it's a fact that is true or not. Here we impose this fact, in other circumstances, it would be checked by the Lean kernel. By contrast, `a ∈ A` is a statement inside the theory. Here it's part of the definition, in other circumstances it could be something proven inside Lean. -/ /- For illustrative purposes, we now define an infix version of the above predicate. It will allow us to write `a is_a_max_of A`, which is closer to a sentence. -/ infix ` is_a_max_of `:55 := is_max /- Let's prove something now! A set of real numbers has at most one maximum. Here everything left of the final `:` is introducing the objects and assumption. The equality `x = y` right of the colon is the conclusion. -/ lemma unique_max (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin -- We first break our assumptions in their two constituent pieces. -- We are free to choose the name following `with` cases hx with x_in x_up, cases hy with y_in y_up, -- Assumption `x_up` means x isn't less than elements of A, let's apply this to y specialize x_up y, -- Assumption `x_up` now needs the information that `y` is indeed in `A`. specialize x_up y_in, -- Let's do this quicker with roles swapped specialize y_up x x_in, -- We explained to Lean the idea of this proof. -- Now we know `x ≤ y` and `y ≤ x`, and Lean shouldn't need more help. -- `linarith` proves equalities and inequalities that follow linearly from -- the assumption we have. linarith, end /- The above proof is too long, even if you remove comments. We don't really need the unpacking steps at the beginning; we can access both parts of the assumption `hx : x is_a_max_of A` using shortcuts `h.1` and `h.2`. We can also improve readability without assistance from the tactic state display, clearly announcing intermediate goals using `have`. This way we get to the following version of the same proof. -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin have : x ≤ y, from hy.2 x hx.1, have : y ≤ x, from hx.2 y hy.1, linarith, end /- Notice how mathematics based on type theory treats the assumption `∀ a ∈ A, a ≤ y` as a function turning an element `a` of `A` into the statement `a ≤ y`. More precisely, this assumption is the abbreviation of `∀ a : ℝ, a ∈ A → a ≤ y`. The expression `hy.2 x` appearing in the above proof is then the statement `x ∈ A → x ≤ y`, which itself is a function turning a statement `x ∈ A` into `x ≤ y` so that the full expression `hy.2 x hx.1` is indeed a proof of `x ≤ y`. One could argue a three-line-long proof of this lemma is still two lines too long. This is debatable, but mathlib's style is to write very short proofs for trivial lemmas. Those proofs are not easy to read but they are meant to indicate that the proof is probably not worth reading. In order to reach this stage, we need to know what `linarith` did for us. It invoked the lemma `le_antisymm` which says: `x ≤ y → y ≤ x → x = y`. This arrow, which is used both for function and implication, is right associative. So the statement is `x ≤ y → (y ≤ x → x = y)` which reads: I will send a proof `p` of `x ≤ y` to a function sending a proof `q'` of `y ≤ x` to a proof of `x = y`. Hence `le_antisymm p q'` is a proof of `x = y`. Using this we can get our one-line proof: -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := le_antisymm (hy.2 x hx.1) (hx.2 y hy.1) /- Such a proof is called a proof term (or a "term mode" proof). Notice it has no `begin` and `end`. It is directly the kind of low level proof that the Lean kernel is consuming. Commands like `cases`, `specialize` or `linarith` are called tactics, they help users constructing proof terms that could be very tedious to write directly. The most efficient proof style combines tactics with proof terms like our previous `have : x ≤ y, from hy.2 x hx.1` where `hy.2 x hx.1` is a proof term embeded inside a tactic mode proof. In the remaining of this file, we'll be characterizing infima of sets of real numbers in term of sequences. -/ /-- The set of lower bounds of a set of real numbers ℝ -/ def low_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, x ≤ a} /- We now define `a` is an infimum of `A`. Again there is already a more general version in mathlib. -/ def is_inf (x : ℝ) (A : set ℝ) := x is_a_max_of (low_bounds A) infix ` is_an_inf_of `:55 := is_inf /- We need to prove that any number which is greater than the infimum of A is greater than some element of A. -/ lemma inf_lt {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) : ∀ y, x < y → ∃ a ∈ A, a < y := begin -- Let `y` be any real number. intro y, -- Let's prove the contrapositive contrapose, -- The symbol `¬` means negation. Let's ask Lean to rewrite the goal without negation, -- pushing negation through quantifiers and inequalities push_neg, -- Let's assume the premise, calling the assumption `h` intro h, -- `h` is exactly saying `y` is a lower bound of `A` so the second part of -- the infimum assumption `hx` applied to `y` and `h` is exactly what we want. exact hx.2 y h end /- In the above proof, the sequence `contrapose, push_neg` is so common that it can be abbreviated to `contrapose!`. With these commands, we enter the gray zone between proof checking and proof finding. Practical computer proof checking crucially needs the computer to handle tedious proof steps. In the next proof, we'll start using `linarith` a bit more seriously, going one step further into automation. Our next real goal is to prove inequalities for limits of sequences. We extract the following lemma: if `y ≤ x + ε` for all positive `ε` then `y ≤ x`. -/ lemma le_of_le_add_eps {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin -- Let's prove the contrapositive, asking Lean to push negations right away. contrapose!, -- Assume `h : x < y`. intro h, -- We need to find `ε` such that `ε` is positive and `x + ε < y`. -- Let's use `(y-x)/2` use ((y-x)/2), -- we now have two properties to prove. Let's do both in turn, using `linarith` split, linarith, linarith, end /- Note how `linarith` was used for both sub-goals at the end of the above proof. We could have shortened that using the semi-colon combinator instead of comma, writing `split ; linarith`. Next we will study a compressed version of that proof: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩, end /- The angle brackets `⟨` and `⟩` introduce compound data or proofs. A proof of a `∃ z, P z` statemement is composed of a witness `z₀` and a proof `h` of `P z₀`. The compound is denoted by `⟨z₀, h⟩`. In the example above, the predicate is itself compound, it is a conjunction `P z ∧ Q z`. So the proof term should read `⟨z₀, ⟨h₁, h₂⟩⟩` where `h₁` (resp. `h₂`) is a proof of `P z₀` (resp. `Q z₀`). But these so-called "anonymous constructor" brackets are right-associative, so we can get rid of the nested brackets. The keyword `by` introduces tactic mode inside term mode, it is a shorter version of the `begin`/`end` pair, which is more convenient for single tactic blocks. In this example, `begin` enters tactic mode, `exact` leaves it, `by` re-enters it. Going all the way to a proof term would make the proof much longer, because we crucially use automation with `contrapose!` and `linarith`. We can still get a one-line proof using curly braces to gather several tactic invocations, and the `by` abbreviation instead of `begin`/`end`: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := by { contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩ } /- One could argue that the above proof is a bit too terse, and we are relying too much on linarith. Let's have more `linarith` calls for smaller steps. For the sake of (tiny) variation, we will also assume the premise and argue by contradiction instead of contraposing. -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin intro h, -- Assume the conclusion is false, and call this assumption H. by_contradiction H, push_neg at H, -- Now let's compute. have key := calc -- Each line must end with a colon followed by a proof term -- We want to specialize our assumption `h` to `ε = (y-x)/2` but this is long to -- type, so let's put a hole `_` that Lean will fill in by comparing the -- statement we want to prove and our proof term with a hole. As usual, -- positivity of `(y-x)/2` is proved by `linarith` y ≤ x + (y-x)/2 : h _ (by linarith) ... = x/2 + y/2 : by ring ... < y : by linarith, -- our key now says `y < y` (notice how the sequence `≤`, `=`, `<` was correctly -- merged into a `<`). Let `linarith` find the desired contradiction now. linarith, -- alternatively, we could have provided the proof term -- `exact lt_irrefl y key` end /- Now we are ready for some analysis. Let's set up notation for absolute value -/ local notation `|`x`|` := abs x /- And let's define convergence of sequences of real numbers (of course there is a much more general definition in mathlib). -/ /-- The sequence `u` tends to `l` -/ def limit (u : ℕ → ℝ) (l : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε /- In the above definition, `u n` denotes the n-th term of the sequence. We can add parentheses to get `u(n)` but we try to avoid parentheses because they pile up very quickly -/ -- If y ≤ u n for all n and u n goes to x then y ≤ x lemma le_lim {x y : ℝ} {u : ℕ → ℝ} (hu : limit u x) (ineq : ∀ n, y ≤ u n) : y ≤ x := begin -- Let's apply our previous lemma apply le_of_le_add_eps, -- We need to prove y ≤ x + ε for all positive ε. -- Let ε be any positive real intros ε ε_pos, -- we now specialize our limit assumption to this `ε`, and immediately -- fix a `N` as promised by the definition. cases hu ε ε_pos with N HN, -- Now we only need to compute until reaching the conclusion calc y ≤ u N : ineq N ... = x + (u N - x) : by linarith -- We'll need `add_le_add` which says `a ≤ b` and `c ≤ d` implies `a + c ≤ b + d` -- We need a lemma saying `z ≤ |z|`. Because we don't know the name of this lemma, -- let's use `library_search`. Because searching through the library is slow, -- Lean will write what it found in the Lean message window when cursor is on -- that line, so that we can replace it by the lemma. We see `le_abs_self`, which -- says `a ≤ |a|`, exactly what we're looking for. ... ≤ x + |u N - x| : add_le_add (by linarith) (by library_search) ... ≤ x + ε : add_le_add (by linarith) (HN N (by linarith)), end /- The next lemma has been extracted from the main proof in order to discuss numbers. In ordinary maths, we know that ℕ is *not* contained in `ℝ`, whatever the construction of real numbers that we use. For instance a natural number is not an equivalence class of Cauchy sequences. But it's very easy to pretend otherwise. Formal maths requires slightly more care. In the statement below, the "type ascription" `(n + 1 : ℝ)` forces Lean to convert the natural number `n+1` into a real number. The "inclusion" map will be displayed in tactic state as `↑`. There are various lemmas asserting this map is compatible with addition and monotone, but we don't want to bother writing their names. The `norm_cast` tactic is designed to wisely apply those lemmas for us. -/ lemma inv_succ_pos : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := begin -- Let `n` be any integer intro n, -- Since we don't know the name of the relevant lemma, asserting that the inverse of -- a positive number is positive, let's state that is suffices -- to prove that `n+1`, seen as a real number, is positive, and ask `library_search` suffices : (n + 1 : ℝ) > 0, { library_search }, -- Now we want to reduce to a statement about natural numbers, not real numbers -- coming from natural numbers. norm_cast, -- and then get the usual help from `linarith` linarith, end /- That was a pretty long proof for an obvious fact. And stating it as a lemma feels stupid, so let's find a way to write it on one line in case we want to include it in some other proof without stating a lemma. First the `library_search` call above displays the name of the relevant lemma: `one_div_pos`. We can also replace the `linarith` call on the last line by `library_search` to learn the name of the lemma `nat.succ_pos` asserting that the successor of a natural number is positive. There is also a variant on `norm_cast` that combines it with `exact`. The term mode analogue of `intro` is `λ`. We get down to: -/ example : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := λ n, one_div_pos.mpr (by exact_mod_cast nat.succ_pos n) /- The next proof uses mostly known things, so we will commment only new aspects. -/ lemma limit_inv_succ : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1/(n + 1 : ℝ) ≤ ε := begin intros ε ε_pos, suffices : ∃ N : ℕ, 1/ε ≤ N, { -- Because we didn't provide a name for the above statement, Lean called it `this`. -- Let's fix an `N` that works. cases this with N HN, use N, intros n Hn, -- Now we want to rewrite the goal using lemmas -- `div_le_iff' : 0 < b → (a / b ≤ c ↔ a ≤ b * c)` -- `div_le_iff : 0 < b → (a / b ≤ c ↔ a ≤ c * b)` -- the second one will be rewritten from right to left, as indicated by `←`. -- Lean will create a side goal for the required positivity assumption that -- we don't provide for `div_le_iff'`. rw [div_le_iff', ← div_le_iff ε_pos], -- We want to replace assumption `Hn` by its real counter-part so that -- linarith can find what it needs. replace Hn : (N : ℝ) ≤ n, exact_mod_cast Hn, linarith, -- we are still left with the positivity assumption, but already discussed -- how to prove it in the preceding lemma exact_mod_cast nat.succ_pos n }, -- Now we need to prove that sufficient statement. -- We want to use that `ℝ` is archimedean. So we start typing -- `exact archimedean_` and hit Ctrl-space to see what completion Lean proposes -- the lemma `archimedean_iff_nat_le` sounds promising. We select the left to -- right implication using `.1`. This a generic lemma for fields equiped with -- a linear (ie total) order. We need to provide a proof that `ℝ` is indeed -- archimedean. This is done using the `apply_instance` tactic that will be -- covered elsewhere. exact archimedean_iff_nat_le.1 (by apply_instance) (1/ε), end /- We can now put all pieces together, with almost no new things to explain. -/ lemma inf_seq (A : set ℝ) (x : ℝ) : (x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ ∃ u : ℕ → ℝ, limit u x ∧ ∀ n, u n ∈ A ) := begin split, { intro h, split, { exact h.1 }, -- On the next line, we don't need to tell Lean to treat `n+1` as a real number because -- we add `x` to it, so Lean knows there is only one way to make sense of this expression. have key : ∀ n : ℕ, ∃ a ∈ A, a < x + 1/(n+1), { intro n, -- we can use the lemma we proved above apply inf_lt h, -- and another one we proved! have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n, linarith }, -- Now we need to use axiom of (countable) choice choose u hu using key, use u, split, { intros ε ε_pos, -- again we use a lemma we proved, specializing it to our fixed `ε`, and fixing a `N` cases limit_inv_succ ε ε_pos with N H, use N, intros n hn, have : x ≤ u n, from h.1 _ (hu n).1, have := calc u n < x + 1/(n + 1) : (hu n).2 ... ≤ x + ε : add_le_add (le_refl x) (H n hn), rw abs_of_nonneg ; linarith }, { intro n, exact (hu n).1 } }, { intro h, -- Assumption `h` is made of nested compound statements. We can use the -- recursive version of `cases` to unpack it in one go. rcases h with ⟨x_min, u, lim, huA⟩, split, exact x_min, intros y y_mino, apply le_lim lim, intro n, exact y_mino (u n) (huA n) }, end
394809921d802d94607400e8f036d9be80245d5f
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/inner_product_space/orientation.lean
605f4775837aee19c7c1e4b032d78df7e571d61c
[ "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
14,653
lean
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Heather Macbeth -/ import analysis.inner_product_space.gram_schmidt_ortho import linear_algebra.orientation /-! # Orientations of real inner product spaces. This file provides definitions and proves lemmas about orientations of real inner product spaces. ## Main definitions * `orthonormal_basis.adjust_to_orientation` takes an orthonormal basis and an orientation, and returns an orthonormal basis with that orientation: either the original orthonormal basis, or one constructed by negating a single (arbitrary) basis vector. * `orientation.fin_orthonormal_basis` is an orthonormal basis, indexed by `fin n`, with the given orientation. * `orientation.volume_form` is a nonvanishing top-dimensional alternating form on an oriented real inner product space, uniquely defined by compatibility with the orientation and inner product structure. ## Main theorems * `orientation.volume_form_apply_le` states that the result of applying the volume form to a set of `n` vectors, where `n` is the dimension the inner product space, is bounded by the product of the lengths of the vectors. * `orientation.abs_volume_form_apply_of_pairwise_orthogonal` states that the result of applying the volume form to a set of `n` orthogonal vectors, where `n` is the dimension the inner product space, is equal up to sign to the product of the lengths of the vectors. -/ noncomputable theory variables {E : Type*} [inner_product_space ℝ E] open finite_dimensional open_locale big_operators real_inner_product_space namespace orthonormal_basis variables {ι : Type*} [fintype ι] [decidable_eq ι] [ne : nonempty ι] (e f : orthonormal_basis ι ℝ E) (x : orientation ℝ E ι) /-- The change-of-basis matrix between two orthonormal bases with the same orientation has determinant 1. -/ lemma det_to_matrix_orthonormal_basis_of_same_orientation (h : e.to_basis.orientation = f.to_basis.orientation) : e.to_basis.det f = 1 := begin apply (e.det_to_matrix_orthonormal_basis_real f).resolve_right, have : 0 < e.to_basis.det f, { rw e.to_basis.orientation_eq_iff_det_pos at h, simpa using h }, linarith, end /-- The change-of-basis matrix between two orthonormal bases with the opposite orientations has determinant -1. -/ lemma det_to_matrix_orthonormal_basis_of_opposite_orientation (h : e.to_basis.orientation ≠ f.to_basis.orientation) : e.to_basis.det f = -1 := begin contrapose! h, simp [e.to_basis.orientation_eq_iff_det_pos, (e.det_to_matrix_orthonormal_basis_real f).resolve_right h], end variables {e f} /-- Two orthonormal bases with the same orientation determine the same "determinant" top-dimensional form on `E`, and conversely. -/ lemma same_orientation_iff_det_eq_det : e.to_basis.det = f.to_basis.det ↔ e.to_basis.orientation = f.to_basis.orientation := begin split, { intros h, dsimp [basis.orientation], congr' }, { intros h, rw e.to_basis.det.eq_smul_basis_det f.to_basis, simp [e.det_to_matrix_orthonormal_basis_of_same_orientation f h], }, end variables (e f) /-- Two orthonormal bases with opposite orientations determine opposite "determinant" top-dimensional forms on `E`. -/ lemma det_eq_neg_det_of_opposite_orientation (h : e.to_basis.orientation ≠ f.to_basis.orientation) : e.to_basis.det = -f.to_basis.det := begin rw e.to_basis.det.eq_smul_basis_det f.to_basis, simp [e.det_to_matrix_orthonormal_basis_of_opposite_orientation f h], end section adjust_to_orientation include ne /-- `orthonormal_basis.adjust_to_orientation`, applied to an orthonormal basis, preserves the property of orthonormality. -/ lemma orthonormal_adjust_to_orientation : orthonormal ℝ (e.to_basis.adjust_to_orientation x) := begin apply e.orthonormal.orthonormal_of_forall_eq_or_eq_neg, simpa using e.to_basis.adjust_to_orientation_apply_eq_or_eq_neg x end /-- Given an orthonormal basis and an orientation, return an orthonormal basis giving that orientation: either the original basis, or one constructed by negating a single (arbitrary) basis vector. -/ def adjust_to_orientation : orthonormal_basis ι ℝ E := (e.to_basis.adjust_to_orientation x).to_orthonormal_basis (e.orthonormal_adjust_to_orientation x) lemma to_basis_adjust_to_orientation : (e.adjust_to_orientation x).to_basis = e.to_basis.adjust_to_orientation x := (e.to_basis.adjust_to_orientation x).to_basis_to_orthonormal_basis _ /-- `adjust_to_orientation` gives an orthonormal basis with the required orientation. -/ @[simp] lemma orientation_adjust_to_orientation : (e.adjust_to_orientation x).to_basis.orientation = x := begin rw e.to_basis_adjust_to_orientation, exact e.to_basis.orientation_adjust_to_orientation x, end /-- Every basis vector from `adjust_to_orientation` is either that from the original basis or its negation. -/ lemma adjust_to_orientation_apply_eq_or_eq_neg (i : ι) : e.adjust_to_orientation x i = e i ∨ e.adjust_to_orientation x i = -(e i) := by simpa [← e.to_basis_adjust_to_orientation] using e.to_basis.adjust_to_orientation_apply_eq_or_eq_neg x i lemma det_adjust_to_orientation : (e.adjust_to_orientation x).to_basis.det = e.to_basis.det ∨ (e.adjust_to_orientation x).to_basis.det = -e.to_basis.det := by simpa using e.to_basis.det_adjust_to_orientation x lemma abs_det_adjust_to_orientation (v : ι → E) : |(e.adjust_to_orientation x).to_basis.det v| = |e.to_basis.det v| := by simp [to_basis_adjust_to_orientation] end adjust_to_orientation end orthonormal_basis namespace orientation variables {n : ℕ} open orthonormal_basis /-- An orthonormal basis, indexed by `fin n`, with the given orientation. -/ protected def fin_orthonormal_basis (hn : 0 < n) (h : finrank ℝ E = n) (x : orientation ℝ E (fin n)) : orthonormal_basis (fin n) ℝ E := begin haveI := fin.pos_iff_nonempty.1 hn, haveI := finite_dimensional_of_finrank (h.symm ▸ hn : 0 < finrank ℝ E), exact ((std_orthonormal_basis _ _).reindex $ fin_congr h).adjust_to_orientation x end /-- `orientation.fin_orthonormal_basis` gives a basis with the required orientation. -/ @[simp] lemma fin_orthonormal_basis_orientation (hn : 0 < n) (h : finrank ℝ E = n) (x : orientation ℝ E (fin n)) : (x.fin_orthonormal_basis hn h).to_basis.orientation = x := begin haveI := fin.pos_iff_nonempty.1 hn, haveI := finite_dimensional_of_finrank (h.symm ▸ hn : 0 < finrank ℝ E), exact ((std_orthonormal_basis _ _).reindex $ fin_congr h).orientation_adjust_to_orientation x end section volume_form variables [_i : fact (finrank ℝ E = n)] (o : orientation ℝ E (fin n)) include _i o /-- The volume form on an oriented real inner product space, a nonvanishing top-dimensional alternating form uniquely defined by compatibility with the orientation and inner product structure. -/ @[irreducible] def volume_form : alternating_map ℝ E ℝ (fin n) := begin classical, unfreezingI { cases n }, { let opos : alternating_map ℝ E ℝ (fin 0) := alternating_map.const_of_is_empty ℝ E (1:ℝ), exact o.eq_or_eq_neg_of_is_empty.by_cases (λ _, opos) (λ _, -opos) }, { exact (o.fin_orthonormal_basis n.succ_pos _i.out).to_basis.det } end omit _i o @[simp] lemma volume_form_zero_pos [_i : fact (finrank ℝ E = 0)] : orientation.volume_form (positive_orientation : orientation ℝ E (fin 0)) = alternating_map.const_linear_equiv_of_is_empty 1 := by simp [volume_form, or.by_cases, if_pos] lemma volume_form_zero_neg [_i : fact (finrank ℝ E = 0)] : orientation.volume_form (-positive_orientation : orientation ℝ E (fin 0)) = - alternating_map.const_linear_equiv_of_is_empty 1 := begin dsimp [volume_form, or.by_cases, positive_orientation], apply if_neg, rw [ray_eq_iff, same_ray_comm], intros h, simpa using congr_arg alternating_map.const_linear_equiv_of_is_empty.symm (eq_zero_of_same_ray_self_neg h), end include _i o /-- The volume form on an oriented real inner product space can be evaluated as the determinant with respect to any orthonormal basis of the space compatible with the orientation. -/ lemma volume_form_robust (b : orthonormal_basis (fin n) ℝ E) (hb : b.to_basis.orientation = o) : o.volume_form = b.to_basis.det := begin unfreezingI { cases n }, { have : o = positive_orientation := hb.symm.trans b.to_basis.orientation_is_empty, simp [volume_form, or.by_cases, dif_pos this] }, { dsimp [volume_form], rw [same_orientation_iff_det_eq_det, hb], exact o.fin_orthonormal_basis_orientation _ _ }, end /-- The volume form on an oriented real inner product space can be evaluated as the determinant with respect to any orthonormal basis of the space compatible with the orientation. -/ lemma volume_form_robust_neg (b : orthonormal_basis (fin n) ℝ E) (hb : b.to_basis.orientation ≠ o) : o.volume_form = - b.to_basis.det := begin unfreezingI { cases n }, { have : positive_orientation ≠ o := by rwa b.to_basis.orientation_is_empty at hb, simp [volume_form, or.by_cases, dif_neg this.symm] }, let e : orthonormal_basis (fin n.succ) ℝ E := o.fin_orthonormal_basis n.succ_pos (fact.out _), dsimp [volume_form], apply e.det_eq_neg_det_of_opposite_orientation b, convert hb.symm, exact o.fin_orthonormal_basis_orientation _ _, end @[simp] lemma volume_form_neg_orientation : (-o).volume_form = - o.volume_form := begin unfreezingI { cases n }, { refine o.eq_or_eq_neg_of_is_empty.by_cases _ _; rintros rfl; simp [volume_form_zero_neg] }, let e : orthonormal_basis (fin n.succ) ℝ E := o.fin_orthonormal_basis n.succ_pos (fact.out _), have h₁ : e.to_basis.orientation = o := o.fin_orthonormal_basis_orientation _ _, have h₂ : e.to_basis.orientation ≠ -o, { symmetry, rw [e.to_basis.orientation_ne_iff_eq_neg, h₁] }, rw [o.volume_form_robust e h₁, (-o).volume_form_robust_neg e h₂], end lemma volume_form_robust' (b : orthonormal_basis (fin n) ℝ E) (v : fin n → E) : |o.volume_form v| = |b.to_basis.det v| := begin unfreezingI { cases n }, { refine o.eq_or_eq_neg_of_is_empty.by_cases _ _; rintros rfl; simp }, { rw [o.volume_form_robust (b.adjust_to_orientation o) (b.orientation_adjust_to_orientation o), b.abs_det_adjust_to_orientation] }, end /-- Let `v` be an indexed family of `n` vectors in an oriented `n`-dimensional real inner product space `E`. The output of the volume form of `E` when evaluated on `v` is bounded in absolute value by the product of the norms of the vectors `v i`. -/ lemma abs_volume_form_apply_le (v : fin n → E) : |o.volume_form v| ≤ ∏ i : fin n, ‖v i‖ := begin unfreezingI { cases n }, { refine o.eq_or_eq_neg_of_is_empty.by_cases _ _; rintros rfl; simp }, haveI : finite_dimensional ℝ E := fact_finite_dimensional_of_finrank_eq_succ n, have : finrank ℝ E = fintype.card (fin n.succ) := by simpa using _i.out, let b : orthonormal_basis (fin n.succ) ℝ E := gram_schmidt_orthonormal_basis this v, have hb : b.to_basis.det v = ∏ i, ⟪b i, v i⟫ := gram_schmidt_orthonormal_basis_det this v, rw [o.volume_form_robust' b, hb, finset.abs_prod], apply finset.prod_le_prod, { intros i hi, positivity }, intros i hi, convert abs_real_inner_le_norm (b i) (v i), simp [b.orthonormal.1 i], end lemma volume_form_apply_le (v : fin n → E) : o.volume_form v ≤ ∏ i : fin n, ‖v i‖ := (le_abs_self _).trans (o.abs_volume_form_apply_le v) /-- Let `v` be an indexed family of `n` orthogonal vectors in an oriented `n`-dimensional real inner product space `E`. The output of the volume form of `E` when evaluated on `v` is, up to sign, the product of the norms of the vectors `v i`. -/ lemma abs_volume_form_apply_of_pairwise_orthogonal {v : fin n → E} (hv : pairwise (λ i j, ⟪v i, v j⟫ = 0)) : |o.volume_form v| = ∏ i : fin n, ‖v i‖ := begin unfreezingI { cases n }, { refine o.eq_or_eq_neg_of_is_empty.by_cases _ _; rintros rfl; simp }, haveI : finite_dimensional ℝ E := fact_finite_dimensional_of_finrank_eq_succ n, have hdim : finrank ℝ E = fintype.card (fin n.succ) := by simpa using _i.out, let b : orthonormal_basis (fin n.succ) ℝ E := gram_schmidt_orthonormal_basis hdim v, have hb : b.to_basis.det v = ∏ i, ⟪b i, v i⟫ := gram_schmidt_orthonormal_basis_det hdim v, rw [o.volume_form_robust' b, hb, finset.abs_prod], by_cases h : ∃ i, v i = 0, obtain ⟨i, hi⟩ := h, { rw [finset.prod_eq_zero (finset.mem_univ i), finset.prod_eq_zero (finset.mem_univ i)]; simp [hi] }, push_neg at h, congr, ext i, have hb : b i = ‖v i‖⁻¹ • v i := gram_schmidt_orthonormal_basis_apply_of_orthogonal hdim hv (h i), simp only [hb, inner_smul_left, real_inner_self_eq_norm_mul_norm, is_R_or_C.conj_to_real], rw abs_of_nonneg, { have : ‖v i‖ ≠ 0 := by simpa using h i, field_simp }, { positivity }, end /-- The output of the volume form of an oriented real inner product space `E` when evaluated on an orthonormal basis is ±1. -/ lemma abs_volume_form_apply_of_orthonormal (v : orthonormal_basis (fin n) ℝ E) : |o.volume_form v| = 1 := by simpa [o.volume_form_robust' v v] using congr_arg abs v.to_basis.det_self lemma volume_form_map {F : Type*} [inner_product_space ℝ F] [fact (finrank ℝ F = n)] (φ : E ≃ₗᵢ[ℝ] F) (x : fin n → F) : (orientation.map (fin n) φ.to_linear_equiv o).volume_form x = o.volume_form (φ.symm ∘ x) := begin unfreezingI { cases n }, { refine o.eq_or_eq_neg_of_is_empty.by_cases _ _; rintros rfl; simp }, let e : orthonormal_basis (fin n.succ) ℝ E := o.fin_orthonormal_basis n.succ_pos (fact.out _), have he : e.to_basis.orientation = o := (o.fin_orthonormal_basis_orientation n.succ_pos (fact.out _)), have heφ : (e.map φ).to_basis.orientation = orientation.map (fin n.succ) φ.to_linear_equiv o, { rw ← he, exact (e.to_basis.orientation_map φ.to_linear_equiv) }, rw (orientation.map (fin n.succ) φ.to_linear_equiv o).volume_form_robust (e.map φ) heφ, rw o.volume_form_robust e he, simp, end /-- The volume form is invariant under pullback by a positively-oriented isometric automorphism. -/ lemma volume_form_comp_linear_isometry_equiv (φ : E ≃ₗᵢ[ℝ] E) (hφ : 0 < (φ.to_linear_equiv : E →ₗ[ℝ] E).det) (x : fin n → E) : o.volume_form (φ ∘ x) = o.volume_form x := begin convert o.volume_form_map φ (φ ∘ x), { symmetry, rwa ← o.map_eq_iff_det_pos φ.to_linear_equiv at hφ, rw [_i.out, fintype.card_fin] }, { ext, simp } end end volume_form end orientation
6c17699865ce7857bc64d5f1555535bc9aea75e2
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/tactic/tauto.lean
f0ae5123d985febbc0752c79e305cd82b89680eb
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
9,782
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.hint namespace tactic open expr open tactic.interactive ( casesm constructor_matching ) /-- find all assumptions of the shape `¬ (p ∧ q)` or `¬ (p ∨ q)` and replace them using de Morgan's law. -/ meta def distrib_not : tactic unit := do hs ← local_context, hs.for_each $ λ h, all_goals' $ iterate_at_most' 3 $ do h ← get_local h.local_pp_name, e ← infer_type h, match e with | `(¬ _ = _) := replace h.local_pp_name ``(mt iff.to_eq %%h) | `(_ ≠ _) := replace h.local_pp_name ``(mt iff.to_eq %%h) | `(_ = _) := replace h.local_pp_name ``(eq.to_iff %%h) | `(¬ (_ ∧ _)) := replace h.local_pp_name ``(decidable.not_and_distrib'.mp %%h) <|> replace h.local_pp_name ``(decidable.not_and_distrib.mp %%h) | `(¬ (_ ∨ _)) := replace h.local_pp_name ``(not_or_distrib.mp %%h) | `(¬ ¬ _) := replace h.local_pp_name ``(decidable.of_not_not %%h) | `(¬ (_ → (_ : Prop))) := replace h.local_pp_name ``(decidable.not_imp.mp %%h) | `(¬ (_ ↔ _)) := replace h.local_pp_name ``(decidable.not_iff.mp %%h) | `(_ ↔ _) := replace h.local_pp_name ``(decidable.iff_iff_and_or_not_and_not.mp %%h) <|> replace h.local_pp_name ``(decidable.iff_iff_and_or_not_and_not.mp (%%h).symm) <|> () <$ tactic.cases h | `(_ → _) := replace h.local_pp_name ``(decidable.not_or_of_imp %%h) | _ := failed end meta def tauto_state := ref $ expr_map (option (expr × expr)) meta def modify_ref {α : Type} (r : ref α) (f : α → α) := read_ref r >>= write_ref r ∘ f meta def add_refl (r : tauto_state) (e : expr) : tactic (expr × expr) := do m ← read_ref r, p ← mk_mapp `rfl [none,e], write_ref r $ m.insert e none, return (e,p) meta def add_symm_proof (r : tauto_state) (e : expr) : tactic (expr × expr) := do env ← get_env, let rel := e.get_app_fn.const_name, some symm ← pure $ environment.symm_for env rel | add_refl r e, (do e' ← mk_meta_var `(Prop), iff_t ← to_expr ``(%%e = %%e'), (_,p) ← solve_aux iff_t (applyc `iff.to_eq ; () <$ split ; applyc symm), e' ← instantiate_mvars e', m ← read_ref r, write_ref r $ (m.insert e (e',p)).insert e' none, return (e',p) ) <|> add_refl r e meta def add_edge (r : tauto_state) (x y p : expr) : tactic unit := modify_ref r $ λ m, m.insert x (y,p) meta def root (r : tauto_state) : expr → tactic (expr × expr) | e := do m ← read_ref r, let record_e : tactic (expr × expr) := match e with | v@(expr.mvar _ _ _) := (do (e,p) ← get_assignment v >>= root, add_edge r v e p, return (e,p)) <|> add_refl r e | _ := add_refl r e end, some e' ← pure $ m.find e | record_e, match e' with | (some (e',p')) := do (e'',p'') ← root e', p'' ← mk_app `eq.trans [p',p''], add_edge r e e'' p'', pure (e'',p'') | none := prod.mk e <$> mk_mapp `rfl [none,some e] end meta def symm_eq (r : tauto_state) : expr → expr → tactic expr | a b := do m ← read_ref r, (a',pa) ← root r a, (b',pb) ← root r b, (unify a' b' >> add_refl r a' *> mk_mapp `rfl [none,a]) <|> do p ← match (a', b') with | (`(¬ %%a₀), `(¬ %%b₀)) := do p ← symm_eq a₀ b₀, p' ← mk_app `congr_arg [`(not),p], add_edge r a' b' p', return p' | (`(%%a₀ ∧ %%a₁), `(%%b₀ ∧ %%b₁)) := do p₀ ← symm_eq a₀ b₀, p₁ ← symm_eq a₁ b₁, p' ← to_expr ``(congr (congr_arg and %%p₀) %%p₁), add_edge r a' b' p', return p' | (`(%%a₀ ∨ %%a₁), `(%%b₀ ∨ %%b₁)) := do p₀ ← symm_eq a₀ b₀, p₁ ← symm_eq a₁ b₁, p' ← to_expr ``(congr (congr_arg or %%p₀) %%p₁), add_edge r a' b' p', return p' | (`(%%a₀ ↔ %%a₁), `(%%b₀ ↔ %%b₁)) := (do p₀ ← symm_eq a₀ b₀, p₁ ← symm_eq a₁ b₁, p' ← to_expr ``(congr (congr_arg iff %%p₀) %%p₁), add_edge r a' b' p', return p') <|> do p₀ ← symm_eq a₀ b₁, p₁ ← symm_eq a₁ b₀, p' ← to_expr ``(eq.trans (congr (congr_arg iff %%p₀) %%p₁) (iff.to_eq iff.comm ) ), add_edge r a' b' p', return p' | (`(%%a₀ → %%a₁), `(%%b₀ → %%b₁)) := if ¬ a₁.has_var ∧ ¬ b₁.has_var then do p₀ ← symm_eq a₀ b₀, p₁ ← symm_eq a₁ b₁, p' ← mk_app `congr_arg [`(implies),p₀,p₁], add_edge r a' b' p', return p' else unify a' b' >> add_refl r a' *> mk_mapp `rfl [none,a] | (_, _) := (do guard $ a'.get_app_fn.is_constant ∧ a'.get_app_fn.const_name = b'.get_app_fn.const_name, (a'',pa') ← add_symm_proof r a', guard $ a'' =ₐ b', pure pa' ) end, p' ← mk_eq_trans pa p, add_edge r a' b' p', mk_eq_symm pb >>= mk_eq_trans p' meta def find_eq_type (r : tauto_state) : expr → list expr → tactic (expr × expr) | e [] := failed | e (H :: Hs) := do t ← infer_type H, t' ← infer_type e, (prod.mk H <$> symm_eq r e t) <|> find_eq_type e Hs private meta def contra_p_not_p (r : tauto_state) : list expr → list expr → tactic unit | [] Hs := failed | (H1 :: Rs) Hs := do t ← (extract_opt_auto_param <$> infer_type H1) >>= whnf, (do a ← match_not t, (H2,p) ← find_eq_type r a Hs, H2 ← to_expr ``( (%%p).mpr %%H2 ), tgt ← target, pr ← mk_app `absurd [tgt, H2, H1], tactic.exact pr) <|> contra_p_not_p Rs Hs meta def contradiction_with (r : tauto_state) : tactic unit := contradiction <|> do tactic.try intro1, ctx ← local_context, contra_p_not_p r ctx ctx meta def contradiction_symm := using_new_ref (native.rb_map.mk _ _) contradiction_with meta def assumption_with (r : tauto_state) : tactic unit := do { ctx ← local_context, t ← target, (H,p) ← find_eq_type r t ctx, mk_eq_mpr p H >>= tactic.exact } <|> fail "assumption tactic failed" meta def assumption_symm := using_new_ref (native.rb_map.mk _ _) assumption_with meta def tautology (c : bool := ff) : tactic unit := do when c classical, using_new_ref (expr_map.mk _) $ λ r, do try (contradiction_with r); try (assumption_with r); repeat (do gs ← get_goals, repeat (() <$ tactic.intro1); distrib_not; casesm (some ()) [``(_ ∧ _),``(_ ∨ _),``(Exists _),``(false)]; try (contradiction_with r); try (target >>= match_or >> refine ``( or_iff_not_imp_left.mpr _)); try (target >>= match_or >> refine ``( or_iff_not_imp_right.mpr _)); repeat (() <$ tactic.intro1); constructor_matching (some ()) [``(_ ∧ _),``(_ ↔ _),``(true)]; try (assumption_with r), gs' ← get_goals, guard (gs ≠ gs') ) ; repeat (reflexivity <|> solve_by_elim <|> constructor_matching none [``(_ ∧ _),``(_ ↔ _),``(Exists _),``(true)] ) ; done open interactive lean.parser namespace interactive local postfix `?`:9001 := optional /-- `tautology` breaks down assumptions of the form `_ ∧ _`, `_ ∨ _`, `_ ↔ _` and `∃ _, _` and splits a goal of the form `_ ∧ _`, `_ ↔ _` or `∃ _, _` until it can be discharged using `reflexivity` or `solve_by_elim`. This is a finishing tactic: it either closes the goal or raises an error. The variant `tautology!` uses the law of excluded middle. -/ meta def tautology (c : parse $ (tk "!")?) := tactic.tautology c.is_some -- Now define a shorter name for the tactic `tautology`. /-- `tauto` breaks down assumptions of the form `_ ∧ _`, `_ ∨ _`, `_ ↔ _` and `∃ _, _` and splits a goal of the form `_ ∧ _`, `_ ↔ _` or `∃ _, _` until it can be discharged using `reflexivity` or `solve_by_elim`. This is a finishing tactic: it either closes the goal or raises an error. The variant `tauto!` uses the law of excluded middle. -/ meta def tauto (c : parse $ (tk "!")?) := tautology c add_hint_tactic "tauto" /-- This tactic (with shorthand `tauto`) breaks down assumptions of the form `_ ∧ _`, `_ ∨ _`, `_ ↔ _` and `∃ _, _` and splits a goal of the form `_ ∧ _`, `_ ↔ _` or `∃ _, _` until it can be discharged using `reflexivity` or `solve_by_elim`. This is a finishing tactic: it either closes the goal or raises an error. The variants `tautology!` and `tauto!` use the law of excluded middle. For instance, one can write: ```lean example (p q r : Prop) [decidable p] [decidable r] : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (r ∨ p ∨ r) := by tauto ``` and the decidability assumptions can be dropped if `tauto!` is used instead of `tauto`. -/ add_tactic_doc { name := "tautology", category := doc_category.tactic, decl_names := [`tactic.interactive.tautology, `tactic.interactive.tauto], tags := ["logic", "decision procedure"] } end interactive end tactic
4810a6bf9451b494af96671ed5a3ef264f3ffc53
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/algebra/gcd_monoid.lean
57a880e56659853b4d6c386368e08d9570e4b1c3
[ "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
34,781
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, Jens Wagemaker -/ import algebra.associated import data.nat.gcd import algebra.group_power.lemmas /-! # Monoids with normalization functions, `gcd`, and `lcm` This file defines extra structures on `comm_cancel_monoid_with_zero`s, including `integral_domain`s. ## Main Definitions * `normalization_monoid` * `gcd_monoid` * `gcd_monoid_of_exists_gcd` * `gcd_monoid_of_exists_lcm` For the `gcd_monoid` instances on `ℕ` and `Z`, see `ring_theory.int.basic`. ## Implementation Notes * `normalization_monoid` is defined by assigning to each element a `norm_unit` such that multiplying by that unit normalizes the monoid, and `normalize` is an idempotent monoid homomorphism. This definition as currently implemented does casework on `0`. * `gcd_monoid` extends `normalization_monoid`, so the `gcd` and `lcm` are always normalized. This makes `gcd`s of polynomials easier to work with, but excludes Euclidean domains, and monoids without zero. * `gcd_monoid_of_gcd` noncomputably constructs a `gcd_monoid` structure just from the `gcd` and its properties. * `gcd_monoid_of_exists_gcd` noncomputably constructs a `gcd_monoid` structure just from a proof that any two elements have a (not necessarily normalized) `gcd`. * `gcd_monoid_of_lcm` noncomputably constructs a `gcd_monoid` structure just from the `lcm` and its properties. * `gcd_monoid_of_exists_lcm` noncomputably constructs a `gcd_monoid` structure just from a proof that any two elements have a (not necessarily normalized) `lcm`. ## TODO * Port GCD facts about nats, definition of coprime * Generalize normalization monoids to commutative (cancellative) monoids with or without zero * Generalize GCD monoid to not require normalization in all cases ## Tags divisibility, gcd, lcm, normalize -/ variables {α : Type*} set_option old_structure_cmd true /-- Normalization monoid: multiplying with `norm_unit` gives a normal form for associated elements. -/ @[protect_proj] class normalization_monoid (α : Type*) [nontrivial α] [comm_cancel_monoid_with_zero α] := (norm_unit : α → units α) (norm_unit_zero : norm_unit 0 = 1) (norm_unit_mul : ∀{a b}, a ≠ 0 → b ≠ 0 → norm_unit (a * b) = norm_unit a * norm_unit b) (norm_unit_coe_units : ∀(u : units α), norm_unit u = u⁻¹) export normalization_monoid (norm_unit norm_unit_zero norm_unit_mul norm_unit_coe_units) attribute [simp] norm_unit_coe_units norm_unit_zero norm_unit_mul section normalization_monoid variables [comm_cancel_monoid_with_zero α] [nontrivial α] [normalization_monoid α] @[simp] theorem norm_unit_one : norm_unit (1:α) = 1 := norm_unit_coe_units 1 /-- Chooses an element of each associate class, by multiplying by `norm_unit` -/ def normalize : monoid_with_zero_hom α α := { to_fun := λ x, x * norm_unit x, map_zero' := by simp, map_one' := by rw [norm_unit_one, units.coe_one, mul_one], map_mul' := λ x y, classical.by_cases (λ hx : x = 0, by rw [hx, zero_mul, zero_mul, zero_mul]) $ λ hx, classical.by_cases (λ hy : y = 0, by rw [hy, mul_zero, zero_mul, mul_zero]) $ λ hy, by simp only [norm_unit_mul hx hy, units.coe_mul]; simp only [mul_assoc, mul_left_comm y], } theorem associated_normalize (x : α) : associated x (normalize x) := ⟨_, rfl⟩ theorem normalize_associated (x : α) : associated (normalize x) x := (associated_normalize _).symm lemma associates.mk_normalize (x : α) : associates.mk (normalize x) = associates.mk x := associates.mk_eq_mk_iff_associated.2 (normalize_associated _) @[simp] lemma normalize_apply (x : α) : normalize x = x * norm_unit x := rfl @[simp] lemma normalize_zero : normalize (0 : α) = 0 := normalize.map_zero @[simp] lemma normalize_one : normalize (1 : α) = 1 := normalize.map_one lemma normalize_coe_units (u : units α) : normalize (u : α) = 1 := by simp lemma normalize_eq_zero {x : α} : normalize x = 0 ↔ x = 0 := ⟨λ hx, (associated_zero_iff_eq_zero x).1 $ hx ▸ associated_normalize _, by rintro rfl; exact normalize_zero⟩ lemma normalize_eq_one {x : α} : normalize x = 1 ↔ is_unit x := ⟨λ hx, is_unit_iff_exists_inv.2 ⟨_, hx⟩, λ ⟨u, hu⟩, hu ▸ normalize_coe_units u⟩ @[simp] theorem norm_unit_mul_norm_unit (a : α) : norm_unit (a * norm_unit a) = 1 := classical.by_cases (assume : a = 0, by simp only [this, norm_unit_zero, zero_mul]) $ assume h, by rw [norm_unit_mul h (units.ne_zero _), norm_unit_coe_units, mul_inv_eq_one] theorem normalize_idem (x : α) : normalize (normalize x) = normalize x := by simp theorem normalize_eq_normalize {a b : α} (hab : a ∣ b) (hba : b ∣ a) : normalize a = normalize b := begin rcases associated_of_dvd_dvd hab hba with ⟨u, rfl⟩, refine classical.by_cases (by rintro rfl; simp only [zero_mul]) (assume ha : a ≠ 0, _), suffices : a * ↑(norm_unit a) = a * ↑u * ↑(norm_unit a) * ↑u⁻¹, by simpa only [normalize_apply, mul_assoc, norm_unit_mul ha u.ne_zero, norm_unit_coe_units], calc a * ↑(norm_unit a) = a * ↑(norm_unit a) * ↑u * ↑u⁻¹: (units.mul_inv_cancel_right _ _).symm ... = a * ↑u * ↑(norm_unit a) * ↑u⁻¹ : by rw mul_right_comm a end lemma normalize_eq_normalize_iff {x y : α} : normalize x = normalize y ↔ x ∣ y ∧ y ∣ x := ⟨λ h, ⟨units.dvd_mul_right.1 ⟨_, h.symm⟩, units.dvd_mul_right.1 ⟨_, h⟩⟩, λ ⟨hxy, hyx⟩, normalize_eq_normalize hxy hyx⟩ theorem dvd_antisymm_of_normalize_eq {a b : α} (ha : normalize a = a) (hb : normalize b = b) (hab : a ∣ b) (hba : b ∣ a) : a = b := ha ▸ hb ▸ normalize_eq_normalize hab hba --can be proven by simp lemma dvd_normalize_iff {a b : α} : a ∣ normalize b ↔ a ∣ b := units.dvd_mul_right --can be proven by simp lemma normalize_dvd_iff {a b : α} : normalize a ∣ b ↔ a ∣ b := units.mul_right_dvd end normalization_monoid namespace comm_group_with_zero variables [decidable_eq α] [comm_group_with_zero α] @[priority 100] -- see Note [lower instance priority] instance : normalization_monoid α := { norm_unit := λ x, if h : x = 0 then 1 else (units.mk0 x h)⁻¹, norm_unit_zero := dif_pos rfl, norm_unit_mul := λ x y x0 y0, units.eq_iff.1 (by simp [x0, y0, mul_comm]), norm_unit_coe_units := λ u, by { rw [dif_neg (units.ne_zero _), units.mk0_coe], apply_instance } } @[simp] lemma coe_norm_unit {a : α} (h0 : a ≠ 0) : (↑(norm_unit a) : α) = a⁻¹ := by simp [norm_unit, h0] end comm_group_with_zero namespace associates variables [comm_cancel_monoid_with_zero α] [nontrivial α] [normalization_monoid α] local attribute [instance] associated.setoid /-- Maps an element of `associates` back to the normalized element of its associate class -/ protected def out : associates α → α := quotient.lift (normalize : α → α) $ λ a b ⟨u, hu⟩, hu ▸ normalize_eq_normalize ⟨_, rfl⟩ (units.mul_right_dvd.2 $ dvd_refl a) lemma out_mk (a : α) : (associates.mk a).out = normalize a := rfl @[simp] lemma out_one : (1 : associates α).out = 1 := normalize_one lemma out_mul (a b : associates α) : (a * b).out = a.out * b.out := quotient.induction_on₂ a b $ assume a b, by simp only [associates.quotient_mk_eq_mk, out_mk, mk_mul_mk, normalize.map_mul] lemma dvd_out_iff (a : α) (b : associates α) : a ∣ b.out ↔ associates.mk a ≤ b := quotient.induction_on b $ by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff] lemma out_dvd_iff (a : α) (b : associates α) : b.out ∣ a ↔ b ≤ associates.mk a := quotient.induction_on b $ by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff] @[simp] lemma out_top : (⊤ : associates α).out = 0 := normalize_zero @[simp] lemma normalize_out (a : associates α) : normalize a.out = a.out := quotient.induction_on a normalize_idem end associates /-- GCD monoid: a `comm_cancel_monoid_with_zero` with normalization and `gcd` (greatest common divisor) and `lcm` (least common multiple) operations. In this setting `gcd` and `lcm` form a bounded lattice on the associated elements where `gcd` is the infimum, `lcm` is the supremum, `1` is bottom, and `0` is top. The type class focuses on `gcd` and we derive the corresponding `lcm` facts from `gcd`. -/ @[protect_proj] class gcd_monoid (α : Type*) [comm_cancel_monoid_with_zero α] [nontrivial α] extends normalization_monoid α := (gcd : α → α → α) (lcm : α → α → α) (gcd_dvd_left : ∀a b, gcd a b ∣ a) (gcd_dvd_right : ∀a b, gcd a b ∣ b) (dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b) (normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b) (gcd_mul_lcm : ∀a b, gcd a b * lcm a b = normalize (a * b)) (lcm_zero_left : ∀a, lcm 0 a = 0) (lcm_zero_right : ∀a, lcm a 0 = 0) export gcd_monoid (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right) attribute [simp] lcm_zero_left lcm_zero_right section gcd_monoid variables [comm_cancel_monoid_with_zero α] [nontrivial α] [gcd_monoid α] @[simp] theorem normalize_gcd : ∀a b:α, normalize (gcd a b) = gcd a b := gcd_monoid.normalize_gcd @[simp] theorem gcd_mul_lcm : ∀a b:α, gcd a b * lcm a b = normalize (a * b) := gcd_monoid.gcd_mul_lcm section gcd theorem dvd_gcd_iff (a b c : α) : a ∣ gcd b c ↔ (a ∣ b ∧ a ∣ c) := iff.intro (assume h, ⟨h.trans (gcd_dvd_left _ _), h.trans (gcd_dvd_right _ _)⟩) (assume ⟨hab, hac⟩, dvd_gcd hab hac) theorem gcd_comm (a b : α) : gcd a b = gcd b a := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) theorem gcd_assoc (m n k : α) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) instance : is_commutative α gcd := ⟨gcd_comm⟩ instance : is_associative α gcd := ⟨gcd_assoc⟩ theorem gcd_eq_normalize {a b c : α} (habc : gcd a b ∣ c) (hcab : c ∣ gcd a b) : gcd a b = normalize c := normalize_gcd a b ▸ normalize_eq_normalize habc hcab @[simp] theorem gcd_zero_left (a : α) : gcd 0 a = normalize a := gcd_eq_normalize (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a)) @[simp] theorem gcd_zero_right (a : α) : gcd a 0 = normalize a := gcd_eq_normalize (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _)) @[simp] theorem gcd_eq_zero_iff (a b : α) : gcd a b = 0 ↔ a = 0 ∧ b = 0 := iff.intro (assume h, let ⟨ca, ha⟩ := gcd_dvd_left a b, ⟨cb, hb⟩ := gcd_dvd_right a b in by rw [h, zero_mul] at ha hb; exact ⟨ha, hb⟩) (assume ⟨ha, hb⟩, by rw [ha, hb, gcd_zero_left, normalize_zero]) @[simp] theorem gcd_one_left (a : α) : gcd 1 a = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_left _ _) (one_dvd _) @[simp] theorem gcd_one_right (a : α) : gcd a 1 = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_right _ _) (one_dvd _) theorem gcd_dvd_gcd {a b c d: α} (hab : a ∣ b) (hcd : c ∣ d) : gcd a c ∣ gcd b d := dvd_gcd ((gcd_dvd_left _ _).trans hab) ((gcd_dvd_right _ _).trans hcd) @[simp] theorem gcd_same (a : α) : gcd a a = normalize a := gcd_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_refl a)) @[simp] theorem gcd_mul_left (a b c : α) : gcd (a * b) (a * c) = normalize a * gcd b c := classical.by_cases (by rintro rfl; simp only [zero_mul, gcd_zero_left, normalize_zero]) $ assume ha : a ≠ 0, suffices gcd (a * b) (a * c) = normalize (a * gcd b c), by simpa only [normalize.map_mul, normalize_gcd], let ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) in gcd_eq_normalize (eq.symm ▸ mul_dvd_mul_left a $ show d ∣ gcd b c, from dvd_gcd ((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_left _ _) ((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_right _ _)) (dvd_gcd (mul_dvd_mul_left a $ gcd_dvd_left _ _) (mul_dvd_mul_left a $ gcd_dvd_right _ _)) @[simp] theorem gcd_mul_right (a b c : α) : gcd (b * a) (c * a) = gcd b c * normalize a := by simp only [mul_comm, gcd_mul_left] theorem gcd_eq_left_iff (a b : α) (h : normalize a = a) : gcd a b = a ↔ a ∣ b := iff.intro (assume eq, eq ▸ gcd_dvd_right _ _) $ assume hab, dvd_antisymm_of_normalize_eq (normalize_gcd _ _) h (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) hab) theorem gcd_eq_right_iff (a b : α) (h : normalize b = b) : gcd a b = b ↔ b ∣ a := by simpa only [gcd_comm a b] using gcd_eq_left_iff b a h theorem gcd_dvd_gcd_mul_left (m n k : α) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd (dvd_mul_left _ _) (dvd_refl _) theorem gcd_dvd_gcd_mul_right (m n k : α) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd (dvd_mul_right _ _) (dvd_refl _) theorem gcd_dvd_gcd_mul_left_right (m n k : α) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd (dvd_refl _) (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (m n k : α) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd (dvd_refl _) (dvd_mul_right _ _) theorem associated.gcd_eq_left {m n : α} (h : associated m n) (k : α) : gcd m k = gcd n k := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd h.dvd (dvd_refl _)) (gcd_dvd_gcd h.symm.dvd (dvd_refl _)) theorem associated.gcd_eq_right {m n : α} (h : associated m n) (k : α) : gcd k m = gcd k n := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd (dvd_refl _) h.dvd) (gcd_dvd_gcd (dvd_refl _) h.symm.dvd) lemma dvd_gcd_mul_of_dvd_mul {m n k : α} (H : k ∣ m * n) : k ∣ (gcd k m) * n := begin transitivity gcd k m * normalize n, { rw ← gcd_mul_right, exact dvd_gcd (dvd_mul_right _ _) H }, { apply dvd.intro ↑(norm_unit n)⁻¹, rw [normalize_apply, mul_assoc, mul_assoc, ← units.coe_mul], simp } end lemma dvd_mul_gcd_of_dvd_mul {m n k : α} (H : k ∣ m * n) : k ∣ m * gcd k n := by { rw mul_comm at H ⊢, exact dvd_gcd_mul_of_dvd_mul H } /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. Note: In general, this representation is highly non-unique. -/ lemma exists_dvd_and_dvd_of_dvd_mul {m n k : α} (H : k ∣ m * n) : ∃ d₁ (hd₁ : d₁ ∣ m) d₂ (hd₂ : d₂ ∣ n), k = d₁ * d₂ := begin by_cases h0 : gcd k m = 0, { rw gcd_eq_zero_iff at h0, rcases h0 with ⟨rfl, rfl⟩, refine ⟨0, dvd_refl 0, n, dvd_refl n, _⟩, simp }, { obtain ⟨a, ha⟩ := gcd_dvd_left k m, refine ⟨gcd k m, gcd_dvd_right _ _, a, _, ha⟩, suffices h : gcd k m * a ∣ gcd k m * n, { cases h with b hb, use b, rw mul_assoc at hb, apply mul_left_cancel' h0 hb }, rw ← ha, exact dvd_gcd_mul_of_dvd_mul H } end theorem gcd_mul_dvd_mul_gcd (k m n : α) : gcd k (m * n) ∣ gcd k m * gcd k n := begin obtain ⟨m', hm', n', hn', h⟩ := (exists_dvd_and_dvd_of_dvd_mul $ gcd_dvd_right k (m * n)), replace h : gcd k (m * n) = m' * n' := h, rw h, have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _, apply mul_dvd_mul, { have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n', exact dvd_gcd hm'k hm' }, { have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n', exact dvd_gcd hn'k hn' } end theorem gcd_pow_right_dvd_pow_gcd {a b : α} {k : ℕ} : gcd a (b ^ k) ∣ (gcd a b) ^ k := begin by_cases hg : gcd a b = 0, { rw gcd_eq_zero_iff at hg, rcases hg with ⟨rfl, rfl⟩, simp }, { induction k with k hk, simp, rw [pow_succ, pow_succ], transitivity gcd a b * gcd a (b ^ k), apply gcd_mul_dvd_mul_gcd a b (b ^ k), refine (mul_dvd_mul_iff_left hg).mpr hk } end theorem gcd_pow_left_dvd_pow_gcd {a b : α} {k : ℕ} : gcd (a ^ k) b ∣ (gcd a b) ^ k := by { rw [gcd_comm, gcd_comm a b], exact gcd_pow_right_dvd_pow_gcd } theorem pow_dvd_of_mul_eq_pow {a b c d₁ d₂ : α} (ha : a ≠ 0) (hab : gcd a b = 1) {k : ℕ} (h : a * b = c ^ k) (hc : c = d₁ * d₂) (hd₁ : d₁ ∣ a) : d₁ ^ k ≠ 0 ∧ d₁ ^ k ∣ a := begin have h1 : gcd (d₁ ^ k) b = 1, { rw ← normalize_gcd (d₁ ^ k) b, rw normalize_eq_one, apply is_unit_of_dvd_one, transitivity (gcd d₁ b) ^ k, { exact gcd_pow_left_dvd_pow_gcd }, { apply is_unit.dvd, apply is_unit.pow, apply is_unit_of_dvd_one, rw ← hab, apply gcd_dvd_gcd hd₁ (dvd_refl b) } }, have h2 : d₁ ^ k ∣ a * b, { use d₂ ^ k, rw [h, hc], exact mul_pow d₁ d₂ k }, rw mul_comm at h2, have h3 : d₁ ^ k ∣ a, { rw [← one_mul a, ← h1], apply dvd_gcd_mul_of_dvd_mul h2 }, have h4 : d₁ ^ k ≠ 0, { intro hdk, rw hdk at h3, apply absurd (zero_dvd_iff.mp h3) ha }, tauto end theorem exists_associated_pow_of_mul_eq_pow {a b c : α} (hab : gcd a b = 1) {k : ℕ} (h : a * b = c ^ k) : ∃ (d : α), associated (d ^ k) a := begin by_cases ha : a = 0, { use 0, rw ha, by_cases hk : k = 0, { exfalso, revert h, rw [ha, hk, zero_mul, pow_zero], apply zero_ne_one }, { rw zero_pow (nat.pos_of_ne_zero hk) }}, by_cases hb : b = 0, { rw [hb, gcd_zero_right] at hab, use 1, rw one_pow, apply (associated_one_iff_is_unit.mpr (normalize_eq_one.mp hab)).symm }, by_cases hk : k = 0, { use 1, rw [hk, pow_zero] at h ⊢, use units.mk_of_mul_eq_one _ _ h, rw [units.coe_mk_of_mul_eq_one, one_mul] }, have hc : c ∣ a * b, { rw h, refine dvd_pow (dvd_refl c) hk }, obtain ⟨d₁, hd₁, d₂, hd₂, hc⟩ := exists_dvd_and_dvd_of_dvd_mul hc, use d₁, obtain ⟨h0₁, ⟨a', ha'⟩⟩ := pow_dvd_of_mul_eq_pow ha hab h hc hd₁, rw [mul_comm] at h hc, rw [gcd_comm] at hab, obtain ⟨h0₂, ⟨b', hb'⟩⟩ := pow_dvd_of_mul_eq_pow hb hab h hc hd₂, rw [ha', hb', hc, mul_pow] at h, have h' : a' * b' = 1, { apply (mul_right_inj' h0₁).mp, rw mul_one, apply (mul_right_inj' h0₂).mp, rw ← h, rw [mul_assoc, mul_comm a', ← mul_assoc (d₁ ^ k), ← mul_assoc _ (d₁ ^ k), mul_comm b'] }, use units.mk_of_mul_eq_one _ _ h', rw [units.coe_mk_of_mul_eq_one, ha'] end end gcd section lcm lemma lcm_dvd_iff {a b c : α} : lcm a b ∣ c ↔ a ∣ c ∧ b ∣ c := classical.by_cases (assume : a = 0 ∨ b = 0, by rcases this with rfl | rfl; simp only [iff_def, lcm_zero_left, lcm_zero_right, zero_dvd_iff, dvd_zero, eq_self_iff_true, and_true, imp_true_iff] {contextual:=tt}) (assume this : ¬ (a = 0 ∨ b = 0), let ⟨h1, h2⟩ := not_or_distrib.1 this in have h : gcd a b ≠ 0, from λ H, h1 ((gcd_eq_zero_iff _ _).1 H).1, by rw [← mul_dvd_mul_iff_left h, gcd_mul_lcm, normalize_dvd_iff, ← dvd_normalize_iff, normalize.map_mul, normalize_gcd, ← gcd_mul_right, dvd_gcd_iff, mul_comm b c, mul_dvd_mul_iff_left h1, mul_dvd_mul_iff_right h2, and_comm]) lemma dvd_lcm_left (a b : α) : a ∣ lcm a b := (lcm_dvd_iff.1 (dvd_refl _)).1 lemma dvd_lcm_right (a b : α) : b ∣ lcm a b := (lcm_dvd_iff.1 (dvd_refl _)).2 lemma lcm_dvd {a b c : α} (hab : a ∣ b) (hcb : c ∣ b) : lcm a c ∣ b := lcm_dvd_iff.2 ⟨hab, hcb⟩ @[simp] theorem lcm_eq_zero_iff (a b : α) : lcm a b = 0 ↔ a = 0 ∨ b = 0 := iff.intro (assume h : lcm a b = 0, have normalize (a * b) = 0, by rw [← gcd_mul_lcm _ _, h, mul_zero], by simpa only [normalize_eq_zero, mul_eq_zero, units.ne_zero, or_false]) (by rintro (rfl | rfl); [apply lcm_zero_left, apply lcm_zero_right]) @[simp] lemma normalize_lcm (a b : α) : normalize (lcm a b) = lcm a b := classical.by_cases (assume : lcm a b = 0, by rw [this, normalize_zero]) $ assume h_lcm : lcm a b ≠ 0, have h1 : gcd a b ≠ 0, from mt (by rw [gcd_eq_zero_iff, lcm_eq_zero_iff]; rintros ⟨rfl, rfl⟩; left; refl) h_lcm, have h2 : normalize (gcd a b * lcm a b) = gcd a b * lcm a b, by rw [gcd_mul_lcm, normalize_idem], by simpa only [normalize.map_mul, normalize_gcd, one_mul, mul_right_inj' h1] using h2 theorem lcm_comm (a b : α) : lcm a b = lcm b a := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) theorem lcm_assoc (m n k : α) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd (lcm_dvd (dvd_lcm_left _ _) ((dvd_lcm_left _ _).trans (dvd_lcm_right _ _))) ((dvd_lcm_right _ _).trans (dvd_lcm_right _ _))) (lcm_dvd ((dvd_lcm_left _ _).trans (dvd_lcm_left _ _)) (lcm_dvd ((dvd_lcm_right _ _).trans (dvd_lcm_left _ _)) (dvd_lcm_right _ _))) instance : is_commutative α lcm := ⟨lcm_comm⟩ instance : is_associative α lcm := ⟨lcm_assoc⟩ lemma lcm_eq_normalize {a b c : α} (habc : lcm a b ∣ c) (hcab : c ∣ lcm a b) : lcm a b = normalize c := normalize_lcm a b ▸ normalize_eq_normalize habc hcab theorem lcm_dvd_lcm {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) : lcm a c ∣ lcm b d := lcm_dvd (hab.trans (dvd_lcm_left _ _)) (hcd.trans (dvd_lcm_right _ _)) @[simp] theorem lcm_units_coe_left (u : units α) (a : α) : lcm ↑u a = normalize a := lcm_eq_normalize (lcm_dvd units.coe_dvd (dvd_refl _)) (dvd_lcm_right _ _) @[simp] theorem lcm_units_coe_right (a : α) (u : units α) : lcm a ↑u = normalize a := (lcm_comm a u).trans $ lcm_units_coe_left _ _ @[simp] theorem lcm_one_left (a : α) : lcm 1 a = normalize a := lcm_units_coe_left 1 a @[simp] theorem lcm_one_right (a : α) : lcm a 1 = normalize a := lcm_units_coe_right a 1 @[simp] theorem lcm_same (a : α) : lcm a a = normalize a := lcm_eq_normalize (lcm_dvd (dvd_refl _) (dvd_refl _)) (dvd_lcm_left _ _) @[simp] theorem lcm_eq_one_iff (a b : α) : lcm a b = 1 ↔ a ∣ 1 ∧ b ∣ 1 := iff.intro (assume eq, eq ▸ ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩) (assume ⟨⟨c, hc⟩, ⟨d, hd⟩⟩, show lcm (units.mk_of_mul_eq_one a c hc.symm : α) (units.mk_of_mul_eq_one b d hd.symm) = 1, by rw [lcm_units_coe_left, normalize_coe_units]) @[simp] theorem lcm_mul_left (a b c : α) : lcm (a * b) (a * c) = normalize a * lcm b c := classical.by_cases (by rintro rfl; simp only [zero_mul, lcm_zero_left, normalize_zero]) $ assume ha : a ≠ 0, suffices lcm (a * b) (a * c) = normalize (a * lcm b c), by simpa only [normalize.map_mul, normalize_lcm], have a ∣ lcm (a * b) (a * c), from (dvd_mul_right _ _).trans (dvd_lcm_left _ _), let ⟨d, eq⟩ := this in lcm_eq_normalize (lcm_dvd (mul_dvd_mul_left a (dvd_lcm_left _ _)) (mul_dvd_mul_left a (dvd_lcm_right _ _))) (eq.symm ▸ (mul_dvd_mul_left a $ lcm_dvd ((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_left _ _) ((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_right _ _))) @[simp] theorem lcm_mul_right (a b c : α) : lcm (b * a) (c * a) = lcm b c * normalize a := by simp only [mul_comm, lcm_mul_left] theorem lcm_eq_left_iff (a b : α) (h : normalize a = a) : lcm a b = a ↔ b ∣ a := iff.intro (assume eq, eq ▸ dvd_lcm_right _ _) $ assume hab, dvd_antisymm_of_normalize_eq (normalize_lcm _ _) h (lcm_dvd (dvd_refl a) hab) (dvd_lcm_left _ _) theorem lcm_eq_right_iff (a b : α) (h : normalize b = b) : lcm a b = b ↔ a ∣ b := by simpa only [lcm_comm b a] using lcm_eq_left_iff b a h theorem lcm_dvd_lcm_mul_left (m n k : α) : lcm m n ∣ lcm (k * m) n := lcm_dvd_lcm (dvd_mul_left _ _) (dvd_refl _) theorem lcm_dvd_lcm_mul_right (m n k : α) : lcm m n ∣ lcm (m * k) n := lcm_dvd_lcm (dvd_mul_right _ _) (dvd_refl _) theorem lcm_dvd_lcm_mul_left_right (m n k : α) : lcm m n ∣ lcm m (k * n) := lcm_dvd_lcm (dvd_refl _) (dvd_mul_left _ _) theorem lcm_dvd_lcm_mul_right_right (m n k : α) : lcm m n ∣ lcm m (n * k) := lcm_dvd_lcm (dvd_refl _) (dvd_mul_right _ _) theorem lcm_eq_of_associated_left {m n : α} (h : associated m n) (k : α) : lcm m k = lcm n k := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd_lcm h.dvd (dvd_refl _)) (lcm_dvd_lcm h.symm.dvd (dvd_refl _)) theorem lcm_eq_of_associated_right {m n : α} (h : associated m n) (k : α) : lcm k m = lcm k n := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd_lcm (dvd_refl _) h.dvd) (lcm_dvd_lcm (dvd_refl _) h.symm.dvd) end lcm namespace gcd_monoid theorem prime_of_irreducible {x : α} (hi: irreducible x) : prime x := ⟨hi.ne_zero, ⟨hi.1, λ a b h, begin cases gcd_dvd_left x a with y hy, cases hi.is_unit_or_is_unit hy with hu hu; cases hu with u hu, { right, transitivity (gcd (x * b) (a * b)), apply dvd_gcd (dvd_mul_right x b) h, rw gcd_mul_right, rw ← hu, apply associated.dvd, transitivity (normalize b), symmetry, use u, apply mul_comm, apply normalize_associated, }, { left, rw [hy, ← hu], transitivity, { apply associated.dvd, symmetry, use u }, apply gcd_dvd_right, } end ⟩⟩ theorem irreducible_iff_prime {p : α} : irreducible p ↔ prime p := ⟨prime_of_irreducible, prime.irreducible⟩ end gcd_monoid end gcd_monoid section unique_unit variables [comm_cancel_monoid_with_zero α] [unique (units α)] lemma units_eq_one (u : units α) : u = 1 := subsingleton.elim u 1 variable [nontrivial α] @[priority 100] -- see Note [lower instance priority] instance normalization_monoid_of_unique_units : normalization_monoid α := { norm_unit := λ x, 1, norm_unit_zero := rfl, norm_unit_mul := λ x y hx hy, (mul_one 1).symm, norm_unit_coe_units := λ u, subsingleton.elim _ _ } @[simp] lemma norm_unit_eq_one (x : α) : norm_unit x = 1 := rfl @[simp] lemma normalize_eq (x : α) : normalize x = x := mul_one x end unique_unit section integral_domain variables [integral_domain α] [gcd_monoid α] lemma gcd_eq_of_dvd_sub_right {a b c : α} (h : a ∣ b - c) : gcd a b = gcd a c := begin apply dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _); rw dvd_gcd_iff; refine ⟨gcd_dvd_left _ _, _⟩, { rcases h with ⟨d, hd⟩, rcases gcd_dvd_right a b with ⟨e, he⟩, rcases gcd_dvd_left a b with ⟨f, hf⟩, use e - f * d, rw [mul_sub, ← he, ← mul_assoc, ← hf, ← hd, sub_sub_cancel] }, { rcases h with ⟨d, hd⟩, rcases gcd_dvd_right a c with ⟨e, he⟩, rcases gcd_dvd_left a c with ⟨f, hf⟩, use e + f * d, rw [mul_add, ← he, ← mul_assoc, ← hf, ← hd, ← add_sub_assoc, add_comm c b, add_sub_cancel] } end lemma gcd_eq_of_dvd_sub_left {a b c : α} (h : a ∣ b - c) : gcd b a = gcd c a := by rw [gcd_comm _ a, gcd_comm _ a, gcd_eq_of_dvd_sub_right h] end integral_domain section constructors noncomputable theory open associates variables [comm_cancel_monoid_with_zero α] [nontrivial α] private lemma map_mk_unit_aux [decidable_eq α] {f : associates α →* α} (hinv : function.right_inverse f associates.mk) (a : α) : a * ↑(classical.some (associated_map_mk hinv a)) = f (associates.mk a) := classical.some_spec (associated_map_mk hinv a) /-- Define `normalization_monoid` on a structure from a `monoid_hom` inverse to `associates.mk`. -/ def normalization_monoid_of_monoid_hom_right_inverse [decidable_eq α] (f : associates α →* α) (hinv : function.right_inverse f associates.mk) : normalization_monoid α := { norm_unit := λ a, if a = 0 then 1 else classical.some (associates.mk_eq_mk_iff_associated.1 (hinv (associates.mk a)).symm), norm_unit_zero := if_pos rfl, norm_unit_mul := λ a b ha hb, by { rw [if_neg (mul_ne_zero ha hb), if_neg ha, if_neg hb, units.ext_iff, units.coe_mul], suffices : (a * b) * ↑(classical.some (associated_map_mk hinv (a * b))) = (a * ↑(classical.some (associated_map_mk hinv a))) * (b * ↑(classical.some (associated_map_mk hinv b))), { apply mul_left_cancel' (mul_ne_zero ha hb) _, simpa only [mul_assoc, mul_comm, mul_left_comm] using this }, rw [map_mk_unit_aux hinv a, map_mk_unit_aux hinv (a * b), map_mk_unit_aux hinv b, ← monoid_hom.map_mul, associates.mk_mul_mk] }, norm_unit_coe_units := λ u, by { rw [if_neg (units.ne_zero u), units.ext_iff], apply mul_left_cancel' (units.ne_zero u), rw [units.mul_inv, map_mk_unit_aux hinv u, associates.mk_eq_mk_iff_associated.2 (associated_one_iff_is_unit.2 ⟨u, rfl⟩), associates.mk_one, monoid_hom.map_one] } } variable [normalization_monoid α] /-- Define `gcd_monoid` on a structure just from the `gcd` and its properties. -/ noncomputable def gcd_monoid_of_gcd [decidable_eq α] (gcd : α → α → α) (gcd_dvd_left : ∀a b, gcd a b ∣ a) (gcd_dvd_right : ∀a b, gcd a b ∣ b) (dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b) (normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b) : gcd_monoid α := { gcd := gcd, gcd_dvd_left := gcd_dvd_left, gcd_dvd_right := gcd_dvd_right, dvd_gcd := λ a b c, dvd_gcd, normalize_gcd := normalize_gcd, lcm := λ a b, if a = 0 then 0 else classical.some (dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (dvd.intro b rfl))), gcd_mul_lcm := λ a b, by { split_ifs with a0, { rw [mul_zero, a0, zero_mul, normalize_zero] }, { exact (classical.some_spec (dvd_normalize_iff.2 ((gcd_dvd_left a b).trans (dvd.intro b rfl)))).symm } }, lcm_zero_left := λ a, if_pos rfl, lcm_zero_right := λ a, by { split_ifs with a0, { refl }, rw ← normalize_eq_zero at a0, have h := (classical.some_spec (dvd_normalize_iff.2 ((gcd_dvd_left a 0).trans (dvd.intro 0 rfl)))).symm, have gcd0 : gcd a 0 = normalize a, { rw ← normalize_gcd, exact normalize_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_zero a)) }, rw ← gcd0 at a0, apply or.resolve_left (mul_eq_zero.1 _) a0, rw [h, mul_zero, normalize_zero] }, .. (infer_instance : normalization_monoid α) } /-- Define `gcd_monoid` on a structure just from the `lcm` and its properties. -/ noncomputable def gcd_monoid_of_lcm [decidable_eq α] (lcm : α → α → α) (dvd_lcm_left : ∀a b, a ∣ lcm a b) (dvd_lcm_right : ∀a b, b ∣ lcm a b) (lcm_dvd : ∀{a b c}, c ∣ a → b ∣ a → lcm c b ∣ a) (normalize_lcm : ∀a b, normalize (lcm a b) = lcm a b) : gcd_monoid α := let exists_gcd := λ a b, dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl)) in { lcm := lcm, gcd := λ a b, if a = 0 then normalize b else (if b = 0 then normalize a else classical.some (exists_gcd a b)), gcd_mul_lcm := λ a b, by { split_ifs, { rw [h, zero_dvd_iff.1 (dvd_lcm_left _ _), mul_zero, zero_mul, normalize_zero] }, { rw [h_1, zero_dvd_iff.1 (dvd_lcm_right _ _), mul_zero, mul_zero, normalize_zero] }, apply eq.trans (mul_comm _ _) (classical.some_spec (dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl)))).symm }, normalize_gcd := λ a b, by { split_ifs, { apply normalize_idem }, { apply normalize_idem }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, apply mul_left_cancel' h0, refine trans _ (classical.some_spec (exists_gcd a b)), conv_lhs { congr, rw [← normalize_lcm a b] }, erw [← normalize.map_mul, ← classical.some_spec (exists_gcd a b), normalize_idem] }, lcm_zero_left := λ a, zero_dvd_iff.1 (dvd_lcm_left _ _), lcm_zero_right := λ a, zero_dvd_iff.1 (dvd_lcm_right _ _), gcd_dvd_left := λ a b, by { split_ifs, { rw h, apply dvd_zero }, { exact (normalize_associated _).dvd }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b), normalize_dvd_iff, mul_comm, mul_dvd_mul_iff_right h], apply dvd_lcm_right }, gcd_dvd_right := λ a b, by { split_ifs, { exact (normalize_associated _).dvd }, { rw h_1, apply dvd_zero }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b), normalize_dvd_iff, mul_dvd_mul_iff_right h_1], apply dvd_lcm_left }, dvd_gcd := λ a b c ac ab, by { split_ifs, { apply dvd_normalize_iff.2 ab }, { apply dvd_normalize_iff.2 ac }, have h0 : lcm c b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl))), dvd_normalize_iff], rcases ab with ⟨d, rfl⟩, rw mul_eq_zero at h_1, push_neg at h_1, rw [mul_comm a, ← mul_assoc, mul_dvd_mul_iff_right h_1.1], apply lcm_dvd (dvd.intro d rfl), rw [mul_comm, mul_dvd_mul_iff_right h_1.2], apply ac }, .. (infer_instance : normalization_monoid α) } /-- Define a `gcd_monoid` structure on a monoid just from the existence of a `gcd`. -/ noncomputable def gcd_monoid_of_exists_gcd [decidable_eq α] (h : ∀ a b : α, ∃ c : α, ∀ d : α, d ∣ a ∧ d ∣ b ↔ d ∣ c) : gcd_monoid α := gcd_monoid_of_gcd (λ a b, normalize (classical.some (h a b))) (λ a b, normalize_dvd_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 (dvd_refl _))).1) (λ a b, normalize_dvd_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 (dvd_refl _))).2) (λ a b c ac ab, dvd_normalize_iff.2 ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩)) (λ a b, normalize_idem _) /-- Define a `gcd_monoid` structure on a monoid just from the existence of an `lcm`. -/ noncomputable def gcd_monoid_of_exists_lcm [decidable_eq α] (h : ∀ a b : α, ∃ c : α, ∀ d : α, a ∣ d ∧ b ∣ d ↔ c ∣ d) : gcd_monoid α := gcd_monoid_of_lcm (λ a b, normalize (classical.some (h a b))) (λ a b, dvd_normalize_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 (dvd_refl _))).1) (λ a b, dvd_normalize_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 (dvd_refl _))).2) (λ a b c ac ab, normalize_dvd_iff.2 ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩)) (λ a b, normalize_idem _) end constructors
28ed6353b2162d1361c26d260799a0e6aaa5556b
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/impbug1.lean
5a2789f8cee5bd4f079f51d18088f0799608b68c
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
701
lean
-- category definition Prop := Type.{0} constant eq {A : Type} : A → A → Prop infix `=`:50 := eq inductive category (ob : Type) (mor : ob → ob → Type) : Type := mk : Π (id : Π (A : ob), mor A A), (Π (A B : ob) (f : mor A A), id A = f) → category ob mor definition id (ob : Type) (mor : ob → ob → Type) (Cat : category ob mor) := category.rec (λ id idl, id) Cat theorem id_left (ob : Type) (mor : ob → ob → Type) (Cat : category ob mor) (A : ob) (f : mor A A) : @eq (mor A A) (id ob mor Cat A) f := @category.rec ob mor (λ (C : category ob mor), @eq (mor A A) (id ob mor C A) f) (λ (id : Π (A : ob), mor A A) (idl : Π (A : ob), _), idl A A f) Cat
878a2cd1bc7071fc5428d1ee95978159c8c4f02b
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/group_theory/quotient_group.lean
bbe8894fc7c701166998d0caa297abfe5d477709
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,789
lean
/- Copyright (c) 2018 Kevin Buzzard, Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Patrick Massot This file is to a certain extent based on `quotient_module.lean` by Johannes Hölzl. -/ import group_theory.coset /-! # Quotients of groups by normal subgroups This files develops the basic theory of quotients of groups by normal subgroups. In particular it proves Noether's first and second isomorphism theorems. ## Main definitions * `mk'`: the canonical group homomorphism `G →* G/N` given a normal subgroup `N` of `G`. * `lift φ`: the group homomorphism `G/N →* H` given a group homomorphism `φ : G →* H` such that `N ⊆ ker φ`. * `map f`: the group homomorphism `G/N →* H/M` given a group homomorphism `f : G →* H` such that `N ⊆ f⁻¹(M)`. ## Main statements * `quotient_ker_equiv_range`: Noether's first isomorphism theorem, an explicit isomorphism `G/ker φ → range φ` for every group homomorphism `φ : G →* H`. * `quotient_inf_equiv_prod_normal_quotient`: Noether's second isomorphism theorem, an explicit isomorphism between `H/(H ∩ N)` and `(HN)/N` given a subgroup `H` and a normal subgroup `N` of a group `G`. ## Tags isomorphism theorems, quotient groups ## TODO Noether's third isomorphism theorem -/ universes u v namespace quotient_group variables {G : Type u} [group G] (N : subgroup G) [nN : N.normal] {H : Type v} [group H] include nN -- Define the `div_inv_monoid` before the `group` structure, -- to make sure we have `inv` fully defined before we show `mul_left_inv`. -- TODO: is there a non-invasive way of defining this in one declaration? @[to_additive quotient_add_group.div_inv_monoid] instance : div_inv_monoid (quotient N) := { one := (1 : G), mul := quotient.map₂' (*) (λ a₁ b₁ hab₁ a₂ b₂ hab₂, ((N.mul_mem_cancel_right (N.inv_mem hab₂)).1 (by rw [mul_inv_rev, mul_inv_rev, ← mul_assoc (a₂⁻¹ * a₁⁻¹), mul_assoc _ b₂, ← mul_assoc b₂, mul_inv_self, one_mul, mul_assoc (a₂⁻¹)]; exact nN.conj_mem _ hab₁ _))), mul_assoc := λ a b c, quotient.induction_on₃' a b c (λ a b c, congr_arg mk (mul_assoc a b c)), one_mul := λ a, quotient.induction_on' a (λ a, congr_arg mk (one_mul a)), mul_one := λ a, quotient.induction_on' a (λ a, congr_arg mk (mul_one a)), inv := λ a, quotient.lift_on' a (λ a, ((a⁻¹ : G) : quotient N)) (λ a b hab, quotient.sound' begin show a⁻¹⁻¹ * b⁻¹ ∈ N, rw ← mul_inv_rev, exact N.inv_mem (nN.mem_comm hab) end) } @[to_additive quotient_add_group.add_group] instance : group (quotient N) := { mul_left_inv := λ a, quotient.induction_on' a (λ a, congr_arg mk (mul_left_inv a)), .. quotient.div_inv_monoid _ } /-- The group homomorphism from `G` to `G/N`. -/ @[to_additive quotient_add_group.mk' "The additive group homomorphism from `G` to `G/N`."] def mk' : G →* quotient N := monoid_hom.mk' (quotient_group.mk) (λ _ _, rfl) @[simp, to_additive quotient_add_group.ker_mk] lemma ker_mk : monoid_hom.ker (quotient_group.mk' N : G →* quotient_group.quotient N) = N := begin ext g, rw [monoid_hom.mem_ker, eq_comm], show (((1 : G) : quotient_group.quotient N)) = g ↔ _, rw [quotient_group.eq, one_inv, one_mul], end -- for commutative groups we don't need normality assumption omit nN @[to_additive quotient_add_group.add_comm_group] instance {G : Type*} [comm_group G] (N : subgroup G) : comm_group (quotient N) := { mul_comm := λ a b, quotient.induction_on₂' a b (λ a b, congr_arg mk (mul_comm a b)), .. @quotient_group.quotient.group _ _ N N.normal_of_comm } include nN local notation ` Q ` := quotient N @[simp, to_additive quotient_add_group.coe_zero] lemma coe_one : ((1 : G) : Q) = 1 := rfl @[simp, to_additive quotient_add_group.coe_add] lemma coe_mul (a b : G) : ((a * b : G) : Q) = a * b := rfl @[simp, to_additive quotient_add_group.coe_neg] lemma coe_inv (a : G) : ((a⁻¹ : G) : Q) = a⁻¹ := rfl @[simp] lemma coe_pow (a : G) (n : ℕ) : ((a ^ n : G) : Q) = a ^ n := (mk' N).map_pow a n @[simp] lemma coe_gpow (a : G) (n : ℤ) : ((a ^ n : G) : Q) = a ^ n := (mk' N).map_gpow a n /-- A group homomorphism `φ : G →* H` with `N ⊆ ker(φ)` descends (i.e. `lift`s) to a group homomorphism `G/N →* H`. -/ @[to_additive quotient_add_group.lift "An `add_group` homomorphism `φ : G →+ H` with `N ⊆ ker(φ)` descends (i.e. `lift`s) to a group homomorphism `G/N →* H`."] def lift (φ : G →* H) (HN : ∀x∈N, φ x = 1) : Q →* H := monoid_hom.mk' (λ q : Q, q.lift_on' φ $ assume a b (hab : a⁻¹ * b ∈ N), (calc φ a = φ a * 1 : (mul_one _).symm ... = φ a * φ (a⁻¹ * b) : HN (a⁻¹ * b) hab ▸ rfl ... = φ (a * (a⁻¹ * b)) : (is_mul_hom.map_mul φ a (a⁻¹ * b)).symm ... = φ b : by rw mul_inv_cancel_left)) (λ q r, quotient.induction_on₂' q r $ is_mul_hom.map_mul φ) @[simp, to_additive quotient_add_group.lift_mk] lemma lift_mk {φ : G →* H} (HN : ∀x∈N, φ x = 1) (g : G) : lift N φ HN (g : Q) = φ g := rfl @[simp, to_additive quotient_add_group.lift_mk'] lemma lift_mk' {φ : G →* H} (HN : ∀x∈N, φ x = 1) (g : G) : lift N φ HN (mk g : Q) = φ g := rfl @[simp, to_additive quotient_add_group.lift_quot_mk] lemma lift_quot_mk {φ : G →* H} (HN : ∀x∈N, φ x = 1) (g : G) : lift N φ HN (quot.mk _ g : Q) = φ g := rfl /-- A group homomorphism `f : G →* H` induces a map `G/N →* H/M` if `N ⊆ f⁻¹(M)`. -/ @[to_additive quotient_add_group.map "An `add_group` homomorphism `f : G →+ H` induces a map `G/N →+ H/M` if `N ⊆ f⁻¹(M)`."] def map (M : subgroup H) [M.normal] (f : G →* H) (h : N ≤ M.comap f) : quotient N →* quotient M := begin refine quotient_group.lift N ((mk' M).comp f) _, assume x hx, refine quotient_group.eq.2 _, rw [mul_one, subgroup.inv_mem_iff], exact h hx, end omit nN variables (φ : G →* H) open function monoid_hom /-- The induced map from the quotient by the kernel to the codomain. -/ @[to_additive quotient_add_group.ker_lift "The induced map from the quotient by the kernel to the codomain."] def ker_lift : quotient (ker φ) →* H := lift _ φ $ λ g, φ.mem_ker.mp @[simp, to_additive quotient_add_group.ker_lift_mk] lemma ker_lift_mk (g : G) : (ker_lift φ) g = φ g := lift_mk _ _ _ @[simp, to_additive quotient_add_group.ker_lift_mk'] lemma ker_lift_mk' (g : G) : (ker_lift φ) (mk g) = φ g := lift_mk' _ _ _ @[to_additive quotient_add_group.injective_ker_lift] lemma ker_lift_injective : injective (ker_lift φ) := assume a b, quotient.induction_on₂' a b $ assume a b (h : φ a = φ b), quotient.sound' $ show a⁻¹ * b ∈ ker φ, by rw [mem_ker, is_mul_hom.map_mul φ, ← h, is_group_hom.map_inv φ, inv_mul_self] -- Note that ker φ isn't definitionally ker (to_range φ) -- so there is a bit of annoying code duplication here /-- The induced map from the quotient by the kernel to the range. -/ @[to_additive quotient_add_group.range_ker_lift "The induced map from the quotient by the kernel to the range."] def range_ker_lift : quotient (ker φ) →* φ.range := lift _ (to_range φ) $ λ g hg, (mem_ker _).mp $ by rwa to_range_ker @[to_additive quotient_add_group.range_ker_lift_injective] lemma range_ker_lift_injective : injective (range_ker_lift φ) := assume a b, quotient.induction_on₂' a b $ assume a b (h : to_range φ a = to_range φ b), quotient.sound' $ show a⁻¹ * b ∈ ker φ, by rw [←to_range_ker, mem_ker, is_mul_hom.map_mul (to_range φ), ← h, is_group_hom.map_inv (to_range φ), inv_mul_self] @[to_additive quotient_add_group.range_ker_lift_surjective] lemma range_ker_lift_surjective : surjective (range_ker_lift φ) := begin rintro ⟨_, g, rfl⟩, use mk g, refl, end /-- The first isomorphism theorem (a definition): the canonical isomorphism between `G/(ker φ)` to `range φ`. -/ @[to_additive quotient_add_group.quotient_ker_equiv_range "The first isomorphism theorem (a definition): the canonical isomorphism between `G/(ker φ)` to `range φ`."] noncomputable def quotient_ker_equiv_range : (quotient (ker φ)) ≃* range φ := mul_equiv.of_bijective (range_ker_lift φ) ⟨range_ker_lift_injective φ, range_ker_lift_surjective φ⟩ /-- The canonical isomorphism `G/(ker φ) ≃* H` induced by a surjection `φ : G →* H`. -/ @[to_additive quotient_add_group.quotient_ker_equiv_of_surjective "The canonical isomorphism `G/(ker φ) ≃+ H` induced by a surjection `φ : G →+ H`."] noncomputable def quotient_ker_equiv_of_surjective (hφ : function.surjective φ) : (quotient (ker φ)) ≃* H := mul_equiv.of_bijective (ker_lift φ) ⟨ker_lift_injective φ, λ h, begin rcases hφ h with ⟨g, rfl⟩, use mk g, refl end⟩ /-- If two normal subgroups `M` and `N` of `G` are the same, their quotient groups are isomorphic. -/ @[to_additive "If two normal subgroups `M` and `N` of `G` are the same, their quotient groups are isomorphic."] def equiv_quotient_of_eq {M N : subgroup G} [M.normal] [N.normal] (h : M = N) : quotient M ≃* quotient N := { to_fun := (lift M (mk' N) (λ m hm, quotient_group.eq.mpr (by simpa [← h] using M.inv_mem hm))), inv_fun := (lift N (mk' M) (λ n hn, quotient_group.eq.mpr (by simpa [← h] using N.inv_mem hn))), left_inv := λ x, x.induction_on' $ by { intro, refl }, right_inv := λ x, x.induction_on' $ by { intro, refl }, map_mul' := λ x y, by rw map_mul } section snd_isomorphism_thm open subgroup /-- The second isomorphism theorem: given two subgroups `H` and `N` of a group `G`, where `N` is normal, defines an isomorphism between `H/(H ∩ N)` and `(HN)/N`. -/ @[to_additive "The second isomorphism theorem: given two subgroups `H` and `N` of a group `G`, where `N` is normal, defines an isomorphism between `H/(H ∩ N)` and `(H + N)/N`"] noncomputable def quotient_inf_equiv_prod_normal_quotient (H N : subgroup G) [N.normal] : quotient ((H ⊓ N).comap H.subtype) ≃* quotient (N.comap (H ⊔ N).subtype) := /- φ is the natural homomorphism H →* (HN)/N. -/ let φ : H →* quotient (N.comap (H ⊔ N).subtype) := (mk' $ N.comap (H ⊔ N).subtype).comp (inclusion le_sup_left) in have φ_surjective : function.surjective φ := λ x, x.induction_on' $ begin rintro ⟨y, (hy : y ∈ ↑(H ⊔ N))⟩, rw mul_normal H N at hy, rcases hy with ⟨h, n, hh, hn, rfl⟩, use [h, hh], apply quotient.eq.mpr, change h⁻¹ * (h * n) ∈ N, rwa [←mul_assoc, inv_mul_self, one_mul], end, (equiv_quotient_of_eq (by simp [comap_comap, ←comap_ker])).trans (quotient_ker_equiv_of_surjective φ φ_surjective) end snd_isomorphism_thm end quotient_group
1d2e91e1a7fa01fffd5d83d3b093996eb22d03d3
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean/Elab/Tactic.lean
a69cca32ce558de74ae132b777dd7774fe90350e
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
622
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Elab.Term import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.ElabTerm import Lean.Elab.Tactic.Induction import Lean.Elab.Tactic.Generalize import Lean.Elab.Tactic.Injection import Lean.Elab.Tactic.Match import Lean.Elab.Tactic.Rewrite import Lean.Elab.Tactic.Location import Lean.Elab.Tactic.Simp import Lean.Elab.Tactic.BuiltinTactic import Lean.Elab.Tactic.Split import Lean.Elab.Tactic.Conv import Lean.Elab.Tactic.Delta
67b38a8e479d949c849f11a654e75ec19b38f543
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/preadditive/schur.lean
d6a64351fb9c212311f4f26698479bd78f7bee34
[ "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,929
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Scott Morrison -/ import algebra.group.ext import category_theory.simple import category_theory.linear import category_theory.endomorphism import algebra.algebra.spectrum /-! # Schur's lemma We first prove the part of Schur's Lemma that holds in any preadditive category with kernels, that any nonzero morphism between simple objects is an isomorphism. Second, we prove Schur's lemma for `𝕜`-linear categories with finite dimensional hom spaces, over an algebraically closed field `𝕜`: the hom space `X ⟶ Y` between simple objects `X` and `Y` is at most one dimensional, and is 1-dimensional iff `X` and `Y` are isomorphic. -/ namespace category_theory open category_theory.limits variables {C : Type*} [category C] variables [preadditive C] -- See also `epi_of_nonzero_to_simple`, which does not require `preadditive C`. lemma mono_of_nonzero_from_simple [has_kernels C] {X Y : C} [simple X] {f : X ⟶ Y} (w : f ≠ 0) : mono f := preadditive.mono_of_kernel_zero (kernel_zero_of_nonzero_from_simple w) /-- The part of **Schur's lemma** that holds in any preadditive category with kernels: that a nonzero morphism between simple objects is an isomorphism. -/ lemma is_iso_of_hom_simple [has_kernels C] {X Y : C} [simple X] [simple Y] {f : X ⟶ Y} (w : f ≠ 0) : is_iso f := begin haveI := mono_of_nonzero_from_simple w, exact is_iso_of_mono_of_nonzero w end /-- As a corollary of Schur's lemma for preadditive categories, any morphism between simple objects is (exclusively) either an isomorphism or zero. -/ lemma is_iso_iff_nonzero [has_kernels C] {X Y : C} [simple X] [simple Y] (f : X ⟶ Y) : is_iso f ↔ f ≠ 0 := ⟨λ I, begin introI h, apply id_nonzero X, simp only [←is_iso.hom_inv_id f, h, zero_comp], end, λ w, is_iso_of_hom_simple w⟩ /-- In any preadditive category with kernels, the endomorphisms of a simple object form a division ring. -/ noncomputable instance [has_kernels C] {X : C} [simple X] : division_ring (End X) := by classical; exact { inv := λ f, if h : f = 0 then 0 else by { haveI := is_iso_of_hom_simple h, exact inv f, }, exists_pair_ne := ⟨𝟙 X, 0, id_nonzero _⟩, inv_zero := dif_pos rfl, mul_inv_cancel := λ f h, begin haveI := is_iso_of_hom_simple h, convert is_iso.inv_hom_id f, exact dif_neg h, end, ..(infer_instance : ring (End X)) } open finite_dimensional section variables (𝕜 : Type*) [division_ring 𝕜] /-- Part of **Schur's lemma** for `𝕜`-linear categories: the hom space between two non-isomorphic simple objects is 0-dimensional. -/ lemma finrank_hom_simple_simple_eq_zero_of_not_iso [has_kernels C] [linear 𝕜 C] {X Y : C} [simple X] [simple Y] (h : (X ≅ Y) → false): finrank 𝕜 (X ⟶ Y) = 0 := begin haveI := subsingleton_of_forall_eq (0 : X ⟶ Y) (λ f, begin have p := not_congr (is_iso_iff_nonzero f), simp only [not_not, ne.def] at p, refine p.mp (λ _, by exactI h (as_iso f)), end), exact finrank_zero_of_subsingleton, end end variables (𝕜 : Type*) [field 𝕜] variables [is_alg_closed 𝕜] [linear 𝕜 C] -- In the proof below we have some difficulty using `I : finite_dimensional 𝕜 (X ⟶ X)` -- where we need a `finite_dimensional 𝕜 (End X)`. -- These are definitionally equal, but without eta reduction Lean can't see this. -- To get around this, we use `convert I`, -- then check the various instances agree field-by-field, /-- An auxiliary lemma for Schur's lemma. If `X ⟶ X` is finite dimensional, and every nonzero endomorphism is invertible, then `X ⟶ X` is 1-dimensional. -/ -- We prove this with the explicit `is_iso_iff_nonzero` assumption, -- rather than just `[simple X]`, as this form is useful for -- Müger's formulation of semisimplicity. lemma finrank_endomorphism_eq_one {X : C} (is_iso_iff_nonzero : ∀ f : X ⟶ X, is_iso f ↔ f ≠ 0) [I : finite_dimensional 𝕜 (X ⟶ X)] : finrank 𝕜 (X ⟶ X) = 1 := begin have id_nonzero := (is_iso_iff_nonzero (𝟙 X)).mp (by apply_instance), apply finrank_eq_one (𝟙 X), { exact id_nonzero, }, { intro f, haveI : nontrivial (End X) := nontrivial_of_ne _ _ id_nonzero, obtain ⟨c, nu⟩ := @spectrum.nonempty_of_is_alg_closed_of_finite_dimensional 𝕜 (End X) _ _ _ _ _ (by { convert I, ext, refl, ext, refl, }) (End.of f), use c, rw [spectrum.mem_iff, is_unit.sub_iff, is_unit_iff_is_iso, is_iso_iff_nonzero, ne.def, not_not, sub_eq_zero, algebra.algebra_map_eq_smul_one] at nu, exact nu.symm, }, end variables [has_kernels C] /-- **Schur's lemma** for endomorphisms in `𝕜`-linear categories. -/ lemma finrank_endomorphism_simple_eq_one (X : C) [simple X] [I : finite_dimensional 𝕜 (X ⟶ X)] : finrank 𝕜 (X ⟶ X) = 1 := finrank_endomorphism_eq_one 𝕜 is_iso_iff_nonzero lemma endomorphism_simple_eq_smul_id {X : C} [simple X] [I : finite_dimensional 𝕜 (X ⟶ X)] (f : X ⟶ X) : ∃ c : 𝕜, c • 𝟙 X = f := (finrank_eq_one_iff_of_nonzero' (𝟙 X) (id_nonzero X)).mp (finrank_endomorphism_simple_eq_one 𝕜 X) f /-- Endomorphisms of a simple object form a field if they are finite dimensional. This can't be an instance as `𝕜` would be undetermined. -/ noncomputable def field_End_of_finite_dimensional (X : C) [simple X] [I : finite_dimensional 𝕜 (X ⟶ X)] : field (End X) := by classical; exact { mul_comm := λ f g, begin obtain ⟨c, rfl⟩ := endomorphism_simple_eq_smul_id 𝕜 f, obtain ⟨d, rfl⟩ := endomorphism_simple_eq_smul_id 𝕜 g, simp [←mul_smul, mul_comm c d], end, ..(infer_instance : division_ring (End X)) } /-- **Schur's lemma** for `𝕜`-linear categories: if hom spaces are finite dimensional, then the hom space between simples is at most 1-dimensional. See `finrank_hom_simple_simple_eq_one_iff` and `finrank_hom_simple_simple_eq_zero_iff` below for the refinements when we know whether or not the simples are isomorphic. -/ -- There is a symmetric argument that uses `[finite_dimensional 𝕜 (Y ⟶ Y)]` instead, -- but we don't bother proving that here. lemma finrank_hom_simple_simple_le_one (X Y : C) [finite_dimensional 𝕜 (X ⟶ X)] [simple X] [simple Y] : finrank 𝕜 (X ⟶ Y) ≤ 1 := begin cases subsingleton_or_nontrivial (X ⟶ Y) with h, { resetI, rw finrank_zero_of_subsingleton, exact zero_le_one }, { obtain ⟨f, nz⟩ := (nontrivial_iff_exists_ne 0).mp h, haveI fi := (is_iso_iff_nonzero f).mpr nz, apply finrank_le_one f, intro g, obtain ⟨c, w⟩ := endomorphism_simple_eq_smul_id 𝕜 (g ≫ inv f), exact ⟨c, by simpa using w =≫ f⟩, }, end lemma finrank_hom_simple_simple_eq_one_iff (X Y : C) [finite_dimensional 𝕜 (X ⟶ X)] [finite_dimensional 𝕜 (X ⟶ Y)] [simple X] [simple Y] : finrank 𝕜 (X ⟶ Y) = 1 ↔ nonempty (X ≅ Y) := begin fsplit, { intro h, rw finrank_eq_one_iff' at h, obtain ⟨f, nz, -⟩ := h, rw ←is_iso_iff_nonzero at nz, exactI ⟨as_iso f⟩, }, { rintro ⟨f⟩, have le_one := finrank_hom_simple_simple_le_one 𝕜 X Y, have zero_lt : 0 < finrank 𝕜 (X ⟶ Y) := finrank_pos_iff_exists_ne_zero.mpr ⟨f.hom, (is_iso_iff_nonzero f.hom).mp infer_instance⟩, linarith, } end lemma finrank_hom_simple_simple_eq_zero_iff (X Y : C) [finite_dimensional 𝕜 (X ⟶ X)] [finite_dimensional 𝕜 (X ⟶ Y)] [simple X] [simple Y] : finrank 𝕜 (X ⟶ Y) = 0 ↔ is_empty (X ≅ Y) := begin rw [← not_nonempty_iff, ← not_congr (finrank_hom_simple_simple_eq_one_iff 𝕜 X Y)], refine ⟨λ h, by { rw h, simp, }, λ h, _⟩, have := finrank_hom_simple_simple_le_one 𝕜 X Y, interval_cases finrank 𝕜 (X ⟶ Y) with h', { exact h', }, { exact false.elim (h h'), }, end end category_theory
5b5150ab580e913e2927477ee9970e54fdf961a9
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/typeclass_metas_internal_goals1.lean
448a1e3a1fc714c69099115a3bc1402718bf5ba2
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
340
lean
new_frontend class Foo (α : Type) : Type := (u : Unit := ()) class Bar (α : Type) : Type := (u : Unit := ()) class Top : Type := (u : Unit := ()) instance FooAll (α : Type) : Foo α := {u:=()} instance BarNat : Bar Nat := {u:=()} instance FooBarToTop (α : Type) [Foo α] [Bar α] : Top := {u:=()} set_option pp.all true #synth Top
7967a21c3d45f6bb61dc831a15be4f2ad4f58176
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/1562.lean
a4bd2fde3e75d28b258a163b78bd42d11f92d5bb
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
490
lean
meta constant term : Type meta constant smt2.builder.int_const : int -> term meta constant smt2_state : Type @[reducible] meta def smt2_m (α : Type) := state_t smt2_state tactic α meta instance tactic_to_smt2_m (α : Type) : has_coe (tactic α) (smt2_m α) := ⟨ fun tc, fun s, do res ← tc, return (res, s) ⟩ meta def reflect_arith_formula : expr → smt2_m term | `(has_zero.zero) := smt2.builder.int_const <$> tactic.eval_expr int `(has_zero.zero int) | a := tactic.fail "foo"
e89ace4a58698c382f530c719ce7bce5c9ca9940
54f4ad05b219d444b709f56c2f619dd87d14ec29
/my_project/src/love09_hoare_logic_demo.lean
d46ce32eab505e156b58043b5a80afbd36082330
[]
no_license
yizhou7/learning-lean
8efcf838c7276e235a81bd291f467fa43ce56e0a
91fb366c624df6e56e19555b2e482ce767cd8224
refs/heads/master
1,675,649,087,737
1,609,022,281,000
1,609,022,281,000
272,072,779
0
0
null
null
null
null
UTF-8
Lean
false
false
14,904
lean
import .love08_operational_semantics_demo /- # LoVe Demo 9: Hoare Logic We review a second way to specify the semantics of a programming language: Hoare logic. If operational semantics corresponds to an idealized interpreter, __Hoare logic__ (also called __axiomatic semantics__) corresponds to a verifier. Hoare logic is particularly convenient to reason about concrete programs. -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe -- def stupid : Π (a : ℕ), ℕ := -- λ a, a + 1 -- #check stupid -- theorem t1 : Π (p q : Prop) (hp: p) (hq: q), p := -- λ (p q : Prop) (hp : p) (hq : q), hp -- #check t1 -- #print t1 /- ## First Things First: Formalization Projects Instead of two of the homework sheets, you can do a verification project, worth 20 points. If you choose to do so, please send your lecturer a message by email by the end of the week. For a fully successful project, we expect about 200 (or more) lines of Lean, including definitions and proofs. Some ideas for projects follow. Computer science: * extended WHILE language with static arrays or other features; * functional data structures (e.g., balanced trees); * functional algorithms (e.g., bubble sort, merge sort, Tarjan's algorithm); * compiler from expressions or imperative programs to, e.g., stack machine; * type systems (e.g., Benjamin Pierce's __Types and Programming Languages__); * security properties (e.g., Volpano–Smith-style noninterference analysis); * theory of first-order terms, including matching, term rewriting; * automata theory; * normalization of context-free grammars or regular expressions; * process algebras and bisimilarity; * soundness and possibly completeness of proof systems (e.g., Genzen's sequent calculus, natural deduction, tableaux); * separation logic; * verified program using Hoare logic. Mathematics: * graphs; * combinatorics; * number theory. Evaluation from 2018–2019: Q: How did you find the project? A: Enjoyable. A: Fun and hard. A: Good, I think the format was excellent in a way that it gave people the chance to do challenging exercises and hand them in incomplete. A: I really really liked it. I think it's a great way of learning—find something you like, dig in it a little, get stuck, ask for help. I wish I could do more of that! A: It was great to have some time to try to work out some stuff you find interesting yourself. A: lots of fun actually!!! A: Very helpful. It gave the opportunity to spend some more time on a particular aspect of the course. ## Hoare Triples The basic judgments of Hoare logic are often called __Hoare triples__. They have the form `{P} S {Q}` where `S` is a statement, and `P` and `Q` (called __precondition__ and __postcondition__) are logical formulas over the state variables. Intended meaning: If `P` holds before `S` is executed and the execution terminates normally, `Q` holds at termination. This is a __partial correctness__ statement: The program is correct if it terminates normally (i.e., no run-time error, no infinite loop or divergence). All of these Hoare triples are valid (with respect to the intended meaning): `{true} b := 4 {b = 4}` `{a = 2} b := 2 * a {a = 2 ∧ b = 4}` `{b ≥ 5} b := b + 1 {b ≥ 6}` `{false} skip {b = 100}` `{true} while i ≠ 100 do i := i + 1 {i = 100}` ## Hoare Rules The following is a complete set of rules for reasoning about WHILE programs: ———————————— Skip {P} skip {P} ——————————————————— Asn {Q[a/x]} x := a {Q} {P} S {R} {R} S' {Q} —————————————————————— Seq {P} S; S' {Q} {P ∧ b} S {Q} {P ∧ ¬b} S' {Q} ——————————————————————————————— If {P} if b then S else S' {Q} {I ∧ b} S {I} ————————————————————————— While {I} while b do S {I ∧ ¬b} P' → P {P} S {Q} Q → Q' ——————————————————————————— Conseq {P'} S {Q'} `Q[a/x]` denotes `Q` with `x` replaced by `a`. In the `While` rule, `I` is called an __invariant__. Except for `Conseq`, the rules are syntax-driven: by looking at a program, we see immediately which rule to apply. Example derivations: —————————————————————— Asn —————————————————————— Asn {a = 2} b := a {b = 2} {b = 2} c := b {c = 2} ——————————————————————————————————————————————————— Seq {a = 2} b := a; c := b {c = 2} —————————————————————— Asn x > 10 → x > 5 {x > 5} y := x {y > 5} y > 5 → y > 0 ——————————————————————————————————————————————————————— Conseq {x > 10} y := x {y > 0} Various __derived rules__ can be proved to be correct in terms of the standard rules. For example, we can derive bidirectional rules for `skip`, `:=`, and `while`: P → Q ———————————— Skip' {P} skip {Q} P → Q[a/x] —————————————— Asn' {P} x := a {Q} {P ∧ b} S {P} P ∧ ¬b → Q —————————————————————————— While' {P} while b do S {Q} ## A Semantic Approach to Hoare Logic We can, and will, define Hoare triples **semantically** in Lean. We will use predicates on states (`state → Prop`) to represent pre- and postconditions, following the shallow embedding style. -/ def partial_hoare (P : state → Prop) (S : stmt) (Q : state → Prop) : Prop := ∀s t, P s → (S, s) ⟹ t → Q t notation `{* ` P : 1 ` *} ` S : 1 ` {* ` Q : 1 ` *}` := partial_hoare P S Q namespace partial_hoare lemma skip_intro {P} : {* P *} stmt.skip {* P *} := begin intros s t hs hst, cases hst, assumption end lemma assign_intro (P : state → Prop) {x} {a : state → ℕ} : {* λs, P (s{x ↦ a s}) *} stmt.assign x a {* P *} := begin intros s t P hst, cases hst, assumption end lemma seq_intro {P Q R S T} (hS : {* P *} S {* Q *}) (hT : {* Q *} T {* R *}) : {* P *} S ;; T {* R *} := begin intros s t hs hst, cases hst, apply hT, { apply hS, { exact hs }, { assumption } }, { assumption } end lemma ite_intro {b P Q : state → Prop} {S T} (hS : {* λs, P s ∧ b s *} S {* Q *}) (hT : {* λs, P s ∧ ¬ b s *} T {* Q *}) : {* P *} stmt.ite b S T {* Q *} := begin intros s t hs hst, cases hst, { apply hS, exact and.intro hs hst_hcond, assumption }, { apply hT, exact and.intro hs hst_hcond, assumption } end lemma while_intro (P : state → Prop) {b : state → Prop} {S} (h : {* λs, P s ∧ b s *} S {* P *}) : {* P *} stmt.while b S {* λs, P s ∧ ¬ b s *} := begin intros s t hs hst, induction hst, case while_true { apply ih_hst_1 P h, exact h _ _ (and.intro hs hcond) hst }, case while_false { exact and.intro hs hcond } end lemma consequence {P P' Q Q' : state → Prop} {S} (h : {* P *} S {* Q *}) (hp : ∀s, P' s → P s) (hq : ∀s, Q s → Q' s) : {* P' *} S {* Q' *} := fix s t, assume hs : P' s, assume hst : (S, s) ⟹ t, show Q' t, from hq _ (h s t (hp s hs) hst) lemma consequence_left (P' : state → Prop) {P Q S} (h : {* P *} S {* Q *}) (hp : ∀s, P' s → P s) : {* P' *} S {* Q *} := consequence h hp (by cc) lemma consequence_right (Q) {Q' : state → Prop} {P S} (h : {* P *} S {* Q *}) (hq : ∀s, Q s → Q' s) : {* P *} S {* Q' *} := consequence h (by cc) hq lemma skip_intro' {P Q : state → Prop} (h : ∀s, P s → Q s) : {* P *} stmt.skip {* Q *} := consequence skip_intro h (by cc) lemma assign_intro' {P Q : state → Prop} {x} {a : state → ℕ} (h : ∀s, P s → Q (s{x ↦ a s})): {* P *} stmt.assign x a {* Q *} := consequence (assign_intro Q) h (by cc) lemma seq_intro' {P Q R S T} (hT : {* Q *} T {* R *}) (hS : {* P *} S {* Q *}) : {* P *} S ;; T {* R *} := seq_intro hS hT lemma while_intro' {b P Q : state → Prop} {S} (I : state → Prop) (hS : {* λs, I s ∧ b s *} S {* I *}) (hP : ∀s, P s → I s) (hQ : ∀s, ¬ b s → I s → Q s) : {* P *} stmt.while b S {* Q *} := consequence (while_intro I hS) hP (by finish) /- `finish` applies a combination of techniques, including normalization of logical connectives and quantifiers, simplification, congruence closure, and quantifier instantiation. It either fully succeeds or fails. -/ lemma assign_intro_forward (P) {x a} : {* P *} stmt.assign x a {* λs, ∃n₀, P (s{x ↦ n₀}) ∧ s x = a (s{x ↦ n₀}) *} := begin apply assign_intro', intros s hP, apply exists.intro (s x), simp [*] end lemma assign_intro_backward (Q : state → Prop) {x} {a : state → ℕ} : {* λs, ∃n', Q (s{x ↦ n'}) ∧ n' = a s *} stmt.assign x a {* Q *} := begin apply assign_intro', intros s hP, cases hP, cc end end partial_hoare /- ## First Program: Exchanging Two Variables -/ def SWAP : stmt := stmt.assign "t" (λs, s "a") ;; stmt.assign "a" (λs, s "b") ;; stmt.assign "b" (λs, s "t") lemma SWAP_correct (a₀ b₀ : ℕ) : {* λs, s "a" = a₀ ∧ s "b" = b₀ *} SWAP {* λs, s "a" = b₀ ∧ s "b" = a₀ *} := begin apply partial_hoare.seq_intro', apply partial_hoare.seq_intro', apply partial_hoare.assign_intro, apply partial_hoare.assign_intro, apply partial_hoare.assign_intro', simp { contextual := tt } end lemma SWAP_correct₂ (a₀ b₀ : ℕ) : {* λs, s "a" = a₀ ∧ s "b" = b₀ *} SWAP {* λs, s "a" = b₀ ∧ s "b" = a₀ *} := begin intros s t hP hstep, cases hstep, cases hP, cases hstep_hS, cases hstep_hT, finish end /- ## Second Program: Adding Two Numbers -/ def ADD : stmt := stmt.while (λs, s "n" ≠ 0) (stmt.assign "n" (λs, s "n" - 1) ;; stmt.assign "m" (λs, s "m" + 1)) lemma ADD_correct (n₀ m₀ : ℕ) : {* λs, s "n" = n₀ ∧ s "m" = m₀ *} ADD {* λs, s "n" = 0 ∧ s "m" = n₀ + m₀ *} := partial_hoare.while_intro' (λs, s "n" + s "m" = n₀ + m₀) begin apply partial_hoare.seq_intro', { apply partial_hoare.assign_intro }, { apply partial_hoare.assign_intro', simp, intros s hnm hnz, rw ←hnm, cases s "n", { finish }, { simp [nat.succ_eq_add_one], linarith } } end (by simp { contextual := true }) (by simp { contextual := true }) /- ## A Verification Condition Generator __Verification condition generators__ (VCGs) are programs that apply Hoare rules automatically, producing __verification conditions__ that must be proved by the user. The user must usually also provide strong enough loop invariants, as an annotation in their programs. We can use Lean's metaprogramming framework to define a simple VCG. Hundreds if not thousands of program verification tools are based on these principles. Often these are based on an extension called separation logic. VCGs typically work backwards from the postcondition, using backward rules (rules stated to have an arbitrary `Q` as their postcondition). This works well because `Asn` is backward. -/ def stmt.while_inv (I b : state → Prop) (S : stmt) : stmt := stmt.while b S namespace partial_hoare lemma while_inv_intro {b I Q : state → Prop} {S} (hS : {* λs, I s ∧ b s *} S {* I *}) (hQ : ∀s, ¬ b s → I s → Q s) : {* I *} stmt.while_inv I b S {* Q *} := while_intro' I hS (by cc) hQ lemma while_inv_intro' {b I P Q : state → Prop} {S} (hS : {* λs, I s ∧ b s *} S {* I *}) (hP : ∀s, P s → I s) (hQ : ∀s, ¬ b s → I s → Q s) : {* P *} stmt.while_inv I b S {* Q *} := while_intro' I hS hP hQ end partial_hoare meta def vcg : tactic unit := do t ← tactic.target, match t with | `({* %%P *} %%S {* _ *}) := match S with | `(stmt.skip) := tactic.applyc (if expr.is_mvar P then ``partial_hoare.skip_intro else ``partial_hoare.skip_intro') | `(stmt.assign _ _) := tactic.applyc (if expr.is_mvar P then ``partial_hoare.assign_intro else ``partial_hoare.assign_intro') | `(stmt.seq _ _) := tactic.applyc ``partial_hoare.seq_intro'; vcg | `(stmt.ite _ _ _) := tactic.applyc ``partial_hoare.ite_intro; vcg | `(stmt.while_inv _ _ _) := tactic.applyc (if expr.is_mvar P then ``partial_hoare.while_inv_intro else ``partial_hoare.while_inv_intro'); vcg | _ := tactic.fail (to_fmt "cannot analyze " ++ to_fmt S) end | _ := pure () end end LoVe /- Register `vcg` as a proper tactic: -/ meta def tactic.interactive.vcg : tactic unit := LoVe.vcg namespace LoVe /- ## Second Program Revisited: Adding Two Numbers -/ lemma ADD_correct₂ (n₀ m₀ : ℕ) : {* λs, s "n" = n₀ ∧ s "m" = m₀ *} ADD {* λs, s "n" = 0 ∧ s "m" = n₀ + m₀ *} := show {* λs, s "n" = n₀ ∧ s "m" = m₀ *} stmt.while_inv (λs, s "n" + s "m" = n₀ + m₀) (λs, s "n" ≠ 0) (stmt.assign "n" (λs, s "n" - 1) ;; stmt.assign "m" (λs, s "m" + 1)) {* λs, s "n" = 0 ∧ s "m" = n₀ + m₀ *}, from begin vcg; simp { contextual := tt }, intros s hnm hnz, rw ←hnm, cases s "n", { finish }, { simp [nat.succ_eq_add_one], linarith } end /- ## Hoare Triples for Total Correctness __Total correctness__ asserts that the program not only is partially correct but also that it always terminates normally. Hoare triples for total correctness have the form [P] S [Q] Intended meaning: If `P` holds before `S` is executed, the execution terminates normally and `Q` holds in the final state. For deterministic programs, an equivalent formulation is as follows: If `P` holds before `S` is executed, there exists a state in which execution terminates normally and `Q` holds in that state. Example: `[i ≤ 100] while i ≠ 100 do i := i + 1 [i = 100]` In our WHILE language, this only affects while loops, which must now be annotated by a __variant__ `V` (a natural number that decreases with each iteration): [I ∧ b ∧ V = v₀] S [I ∧ V < v₀] ——————————————————————————————— While-Var [I] while b do S [I ∧ ¬b] What is a suitable variant for the example above? -/ end LoVe
01fe1888f6da5f3606c0a11b2bed88689d90c59a
fe25de614feb5587799621c41487aaee0d083b08
/src/Lean/Data/Position.lean
2d235f945cf102bd982c83fa37935630c6ae2930
[ "Apache-2.0" ]
permissive
pollend/lean4
e8469c2f5fb8779b773618c3267883cf21fb9fac
c913886938c4b3b83238a3f99673c6c5a9cec270
refs/heads/master
1,687,973,251,481
1,628,039,739,000
1,628,039,739,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,452
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Data.Format namespace Lean structure Position where line : Nat column : Nat deriving Inhabited, DecidableEq, Repr namespace Position protected def lt : Position → Position → Bool | ⟨l₁, c₁⟩, ⟨l₂, c₂⟩ => (l₁, c₁) < (l₂, c₂) instance : ToFormat Position := ⟨fun ⟨l, c⟩ => "⟨" ++ format l ++ ", " ++ format c ++ "⟩"⟩ instance : ToString Position := ⟨fun ⟨l, c⟩ => "⟨" ++ toString l ++ ", " ++ toString c ++ "⟩"⟩ end Position structure FileMap where source : String positions : Array String.Pos lines : Array Nat deriving Inhabited class MonadFileMap (m : Type → Type) where getFileMap : m FileMap export MonadFileMap (getFileMap) namespace FileMap partial def ofString (s : String) : FileMap := let rec loop (i : String.Pos) (line : Nat) (ps : Array String.Pos) (lines : Array Nat) : FileMap := if s.atEnd i then { source := s, positions := ps.push i, lines := lines.push line } else let c := s.get i; let i := s.next i; if c == '\n' then loop i (line+1) (ps.push i) (lines.push (line+1)) else loop i line ps lines loop 0 1 (#[0]) (#[1]) partial def toPosition (fmap : FileMap) (pos : String.Pos) : Position := match fmap with | { source := str, positions := ps, lines := lines } => if ps.size >= 2 && pos <= ps.back then let rec toColumn (i : String.Pos) (c : Nat) : Nat := if i == pos || str.atEnd i then c else toColumn (str.next i) (c+1) let rec loop (b e : Nat) := let posB := ps[b] if e == b + 1 then { line := lines.get! b, column := toColumn posB 0 } else let m := (b + e) / 2; let posM := ps.get! m; if pos == posM then { line := lines.get! m, column := 0 } else if pos > posM then loop m e else loop b m loop 0 (ps.size -1) else -- Some systems like the delaborator use synthetic positions without an input file, -- which would violate `toPositionAux`'s invariant. -- Can also happen with EOF errors, which are not strictly inside the file. ⟨lines.back, pos - ps.back⟩ end FileMap end Lean def String.toFileMap (s : String) : Lean.FileMap := Lean.FileMap.ofString s
c30cd9b86578d9d317a432ed93c2cf9b9bbc2a09
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/stage0/src/Lean/Data/Lsp/InitShutdown.lean
bc1a4f5323bf694a37a6901834838b93836234ca
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,172
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Lean.Data.Lsp.Capabilities import Lean.Data.Lsp.Workspace import Lean.Data.Json /-! Functionality to do with initializing and shutting down the server ("General Messages" section of LSP spec). -/ namespace Lean namespace Lsp open Json structure ClientInfo where name : String version? : Option String := none deriving ToJson, FromJson inductive Trace where | off | messages | verbose instance : FromJson Trace := ⟨fun j => match j.getStr? with | some "off" => Trace.off | some "messages" => Trace.messages | some "verbose" => Trace.verbose | _ => none⟩ instance Trace.hasToJson : ToJson Trace := ⟨fun | Trace.off => "off" | Trace.messages => "messages" | Trace.verbose => "verbose"⟩ /-- Lean-specific initialization options. -/ structure InitializationOptions where /-- Time (in milliseconds) which must pass since latest edit until elaboration begins. Lower values may make editors feel faster at the cost of higher CPU usage. Defaults to 200ms. -/ editDelay? : Option Nat deriving ToJson, FromJson structure InitializeParams where processId? : Option Int := none clientInfo? : Option ClientInfo := none /- We don't support the deprecated rootPath (rootPath? : Option String) -/ rootUri? : Option String := none initializationOptions? : Option InitializationOptions := none capabilities : ClientCapabilities /- If omitted, we default to off. -/ trace : Trace := Trace.off workspaceFolders? : Option (Array WorkspaceFolder) := none deriving ToJson instance : FromJson InitializeParams := ⟨fun j => do /- Many of these params can be null instead of not present. For ease of implementation, we're liberal: missing params, wrong json types and null all map to none, even if LSP sometimes only allows some subset of these. In cases where LSP makes a meaningful distinction between different kinds of missing values, we'll follow accordingly. -/ let processId? := j.getObjValAs? Int "processId" let clientInfo? := j.getObjValAs? ClientInfo "clientInfo" let rootUri? := j.getObjValAs? String "rootUri" let initializationOptions? := j.getObjValAs? InitializationOptions "initializationOptions" let capabilities ← j.getObjValAs? ClientCapabilities "capabilities" let trace := (j.getObjValAs? Trace "trace").getD Trace.off let workspaceFolders? := j.getObjValAs? (Array WorkspaceFolder) "workspaceFolders" pure ⟨processId?, clientInfo?, rootUri?, initializationOptions?, capabilities, trace, workspaceFolders?⟩⟩ inductive InitializedParams where | mk instance : FromJson InitializedParams := ⟨fun _ => InitializedParams.mk⟩ instance : ToJson InitializedParams := ⟨fun _ => Json.null⟩ structure ServerInfo where name : String version? : Option String := none deriving ToJson, FromJson structure InitializeResult where capabilities : ServerCapabilities serverInfo? : Option ServerInfo := none deriving ToJson, FromJson end Lsp end Lean
b97d664b13efb1fbc96dc9cb3d72166abfe7cf2f
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/topology/uniform_space/completion.lean
0bf8ad1006cdf19484a49166322ae0b3839a4ae2
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
23,234
lean
/- Copyright (c) 2018 Patrick Massot. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Johannes Hölzl -/ import topology.uniform_space.abstract_completion /-! # Hausdorff completions of uniform spaces The goal is to construct a left-adjoint to the inclusion of complete Hausdorff uniform spaces into all uniform spaces. Any uniform space `α` gets a completion `completion α` and a morphism (ie. uniformly continuous map) `coe : α → completion α` which solves the universal mapping problem of factorizing morphisms from `α` to any complete Hausdorff uniform space `β`. It means any uniformly continuous `f : α → β` gives rise to a unique morphism `completion.extension f : completion α → β` such that `f = completion.extension f ∘ coe`. Actually `completion.extension f` is defined for all maps from `α` to `β` but it has the desired properties only if `f` is uniformly continuous. Beware that `coe` is not injective if `α` is not Hausdorff. But its image is always dense. The adjoint functor acting on morphisms is then constructed by the usual abstract nonsense. For every uniform spaces `α` and `β`, it turns `f : α → β` into a morphism `completion.map f : completion α → completion β` such that `coe ∘ f = (completion.map f) ∘ coe` provided `f` is uniformly continuous. This construction is compatible with composition. In this file we introduce the following concepts: * `Cauchy α` the uniform completion of the uniform space `α` (using Cauchy filters). These are not minimal filters. * `completion α := quotient (separation_setoid (Cauchy α))` the Hausdorff completion. ## References This formalization is mostly based on N. Bourbaki: General Topology I. M. James: Topologies and Uniformities From a slightly different perspective in order to reuse material in topology.uniform_space.basic. -/ noncomputable theory open filter set universes u v w x open_locale uniformity classical topological_space filter /-- Space of Cauchy filters This is essentially the completion of a uniform space. The embeddings are the neighbourhood filters. This space is not minimal, the separated uniform space (i.e. quotiented on the intersection of all entourages) is necessary for this. -/ def Cauchy (α : Type u) [uniform_space α] : Type u := { f : filter α // cauchy f } namespace Cauchy section parameters {α : Type u} [uniform_space α] variables {β : Type v} {γ : Type w} variables [uniform_space β] [uniform_space γ] def gen (s : set (α × α)) : set (Cauchy α × Cauchy α) := {p | s ∈ p.1.val ×ᶠ p.2.val } lemma monotone_gen : monotone gen := monotone_set_of $ assume p, @monotone_mem (α×α) (p.1.val ×ᶠ p.2.val) private lemma symm_gen : map prod.swap ((𝓤 α).lift' gen) ≤ (𝓤 α).lift' gen := calc map prod.swap ((𝓤 α).lift' gen) = (𝓤 α).lift' (λs:set (α×α), {p | s ∈ p.2.val ×ᶠ p.1.val }) : begin delta gen, simp [map_lift'_eq, monotone_set_of, monotone_mem, function.comp, image_swap_eq_preimage_swap, -subtype.val_eq_coe] end ... ≤ (𝓤 α).lift' gen : uniformity_lift_le_swap (monotone_principal.comp (monotone_set_of $ assume p, @monotone_mem (α×α) (p.2.val ×ᶠ p.1.val))) begin have h := λ(p:Cauchy α×Cauchy α), @filter.prod_comm _ _ (p.2.val) (p.1.val), simp [function.comp, h, -subtype.val_eq_coe, mem_map'], exact le_refl _, end private lemma comp_rel_gen_gen_subset_gen_comp_rel {s t : set (α×α)} : comp_rel (gen s) (gen t) ⊆ (gen (comp_rel s t) : set (Cauchy α × Cauchy α)) := assume ⟨f, g⟩ ⟨h, h₁, h₂⟩, let ⟨t₁, (ht₁ : t₁ ∈ f.val), t₂, (ht₂ : t₂ ∈ h.val), (h₁ : set.prod t₁ t₂ ⊆ s)⟩ := mem_prod_iff.mp h₁ in let ⟨t₃, (ht₃ : t₃ ∈ h.val), t₄, (ht₄ : t₄ ∈ g.val), (h₂ : set.prod t₃ t₄ ⊆ t)⟩ := mem_prod_iff.mp h₂ in have t₂ ∩ t₃ ∈ h.val, from inter_mem ht₂ ht₃, let ⟨x, xt₂, xt₃⟩ := h.property.left.nonempty_of_mem this in (f.val ×ᶠ g.val).sets_of_superset (prod_mem_prod ht₁ ht₄) (assume ⟨a, b⟩ ⟨(ha : a ∈ t₁), (hb : b ∈ t₄)⟩, ⟨x, h₁ (show (a, x) ∈ set.prod t₁ t₂, from ⟨ha, xt₂⟩), h₂ (show (x, b) ∈ set.prod t₃ t₄, from ⟨xt₃, hb⟩)⟩) private lemma comp_gen : ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) ≤ (𝓤 α).lift' gen := calc ((𝓤 α).lift' gen).lift' (λs, comp_rel s s) = (𝓤 α).lift' (λs, comp_rel (gen s) (gen s)) : begin rw [lift'_lift'_assoc], exact monotone_gen, exact (monotone_comp_rel monotone_id monotone_id) end ... ≤ (𝓤 α).lift' (λs, gen $ comp_rel s s) : lift'_mono' $ assume s hs, comp_rel_gen_gen_subset_gen_comp_rel ... = ((𝓤 α).lift' $ λs:set(α×α), comp_rel s s).lift' gen : begin rw [lift'_lift'_assoc], exact (monotone_comp_rel monotone_id monotone_id), exact monotone_gen end ... ≤ (𝓤 α).lift' gen : lift'_mono comp_le_uniformity (le_refl _) instance : uniform_space (Cauchy α) := uniform_space.of_core { uniformity := (𝓤 α).lift' gen, refl := principal_le_lift' $ assume s hs ⟨a, b⟩ (a_eq_b : a = b), a_eq_b ▸ a.property.right hs, symm := symm_gen, comp := comp_gen } theorem mem_uniformity {s : set (Cauchy α × Cauchy α)} : s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, gen t ⊆ s := mem_lift'_sets monotone_gen theorem mem_uniformity' {s : set (Cauchy α × Cauchy α)} : s ∈ 𝓤 (Cauchy α) ↔ ∃ t ∈ 𝓤 α, ∀ f g : Cauchy α, t ∈ f.1 ×ᶠ g.1 → (f, g) ∈ s := mem_uniformity.trans $ bex_congr $ λ t h, prod.forall /-- Embedding of `α` into its completion `Cauchy α` -/ def pure_cauchy (a : α) : Cauchy α := ⟨pure a, cauchy_pure⟩ lemma uniform_inducing_pure_cauchy : uniform_inducing (pure_cauchy : α → Cauchy α) := ⟨have (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) = id, from funext $ assume s, set.ext $ assume ⟨a₁, a₂⟩, by simp [preimage, gen, pure_cauchy, prod_principal_principal], calc comap (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ((𝓤 α).lift' gen) = (𝓤 α).lift' (preimage (λ (x : α × α), (pure_cauchy (x.fst), pure_cauchy (x.snd))) ∘ gen) : comap_lift'_eq monotone_gen ... = 𝓤 α : by simp [this]⟩ lemma uniform_embedding_pure_cauchy : uniform_embedding (pure_cauchy : α → Cauchy α) := { inj := assume a₁ a₂ h, pure_injective $ subtype.ext_iff_val.1 h, ..uniform_inducing_pure_cauchy } lemma dense_range_pure_cauchy : dense_range pure_cauchy := assume f, have h_ex : ∀ s ∈ 𝓤 (Cauchy α), ∃y:α, (f, pure_cauchy y) ∈ s, from assume s hs, let ⟨t'', ht''₁, (ht''₂ : gen t'' ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht'₁, ht'₂⟩ := comp_mem_uniformity_sets ht''₁ in have t' ∈ f.val ×ᶠ f.val, from f.property.right ht'₁, let ⟨t, ht, (h : set.prod t t ⊆ t')⟩ := mem_prod_same_iff.mp this in let ⟨x, (hx : x ∈ t)⟩ := f.property.left.nonempty_of_mem ht in have t'' ∈ f.val ×ᶠ pure x, from mem_prod_iff.mpr ⟨t, ht, {y:α | (x, y) ∈ t'}, h $ mk_mem_prod hx hx, assume ⟨a, b⟩ ⟨(h₁ : a ∈ t), (h₂ : (x, b) ∈ t')⟩, ht'₂ $ prod_mk_mem_comp_rel (@h (a, x) ⟨h₁, hx⟩) h₂⟩, ⟨x, ht''₂ $ by dsimp [gen]; exact this⟩, begin simp only [closure_eq_cluster_pts, cluster_pt, nhds_eq_uniformity, lift'_inf_principal_eq, set.inter_comm _ (range pure_cauchy), mem_set_of_eq], exact (lift'_ne_bot_iff $ monotone_inter monotone_const monotone_preimage).mpr (assume s hs, let ⟨y, hy⟩ := h_ex s hs in have pure_cauchy y ∈ range pure_cauchy ∩ {y : Cauchy α | (f, y) ∈ s}, from ⟨mem_range_self y, hy⟩, ⟨_, this⟩) end lemma dense_inducing_pure_cauchy : dense_inducing pure_cauchy := uniform_inducing_pure_cauchy.dense_inducing dense_range_pure_cauchy lemma dense_embedding_pure_cauchy : dense_embedding pure_cauchy := uniform_embedding_pure_cauchy.dense_embedding dense_range_pure_cauchy lemma nonempty_Cauchy_iff : nonempty (Cauchy α) ↔ nonempty α := begin split ; rintro ⟨c⟩, { have := eq_univ_iff_forall.1 dense_embedding_pure_cauchy.to_dense_inducing.closure_range c, obtain ⟨_, ⟨_, a, _⟩⟩ := mem_closure_iff.1 this _ is_open_univ trivial, exact ⟨a⟩ }, { exact ⟨pure_cauchy c⟩ } end section set_option eqn_compiler.zeta true instance : complete_space (Cauchy α) := complete_space_extension uniform_inducing_pure_cauchy dense_range_pure_cauchy $ assume f hf, let f' : Cauchy α := ⟨f, hf⟩ in have map pure_cauchy f ≤ (𝓤 $ Cauchy α).lift' (preimage (prod.mk f')), from le_lift' $ assume s hs, let ⟨t, ht₁, (ht₂ : gen t ⊆ s)⟩ := (mem_lift'_sets monotone_gen).mp hs in let ⟨t', ht', (h : set.prod t' t' ⊆ t)⟩ := mem_prod_same_iff.mp (hf.right ht₁) in have t' ⊆ { y : α | (f', pure_cauchy y) ∈ gen t }, from assume x hx, (f ×ᶠ pure x).sets_of_superset (prod_mem_prod ht' hx) h, f.sets_of_superset ht' $ subset.trans this (preimage_mono ht₂), ⟨f', by simp [nhds_eq_uniformity]; assumption⟩ end instance [inhabited α] : inhabited (Cauchy α) := ⟨pure_cauchy $ default α⟩ instance [h : nonempty α] : nonempty (Cauchy α) := h.rec_on $ assume a, nonempty.intro $ Cauchy.pure_cauchy a section extend def extend (f : α → β) : (Cauchy α → β) := if uniform_continuous f then dense_inducing_pure_cauchy.extend f else λ x, f (classical.inhabited_of_nonempty $ nonempty_Cauchy_iff.1 ⟨x⟩).default variables [separated_space β] lemma extend_pure_cauchy {f : α → β} (hf : uniform_continuous f) (a : α) : extend f (pure_cauchy a) = f a := begin rw [extend, if_pos hf], exact uniformly_extend_of_ind uniform_inducing_pure_cauchy dense_range_pure_cauchy hf _ end variables [_root_.complete_space β] lemma uniform_continuous_extend {f : α → β} : uniform_continuous (extend f) := begin by_cases hf : uniform_continuous f, { rw [extend, if_pos hf], exact uniform_continuous_uniformly_extend uniform_inducing_pure_cauchy dense_range_pure_cauchy hf }, { rw [extend, if_neg hf], exact uniform_continuous_of_const (assume a b, by congr) } end end extend end theorem Cauchy_eq {α : Type*} [inhabited α] [uniform_space α] [complete_space α] [separated_space α] {f g : Cauchy α} : Lim f.1 = Lim g.1 ↔ (f, g) ∈ separation_rel (Cauchy α) := begin split, { intros e s hs, rcases Cauchy.mem_uniformity'.1 hs with ⟨t, tu, ts⟩, apply ts, rcases comp_mem_uniformity_sets tu with ⟨d, du, dt⟩, refine mem_prod_iff.2 ⟨_, f.2.le_nhds_Lim (mem_nhds_right (Lim f.1) du), _, g.2.le_nhds_Lim (mem_nhds_left (Lim g.1) du), λ x h, _⟩, cases x with a b, cases h with h₁ h₂, rw ← e at h₂, exact dt ⟨_, h₁, h₂⟩ }, { intros H, refine separated_def.1 (by apply_instance) _ _ (λ t tu, _), rcases mem_uniformity_is_closed tu with ⟨d, du, dc, dt⟩, refine H {p | (Lim p.1.1, Lim p.2.1) ∈ t} (Cauchy.mem_uniformity'.2 ⟨d, du, λ f g h, _⟩), rcases mem_prod_iff.1 h with ⟨x, xf, y, yg, h⟩, have limc : ∀ (f : Cauchy α) (x ∈ f.1), Lim f.1 ∈ closure x, { intros f x xf, rw closure_eq_cluster_pts, exact f.2.1.mono (le_inf f.2.le_nhds_Lim (le_principal_iff.2 xf)) }, have := dc.closure_subset_iff.2 h, rw closure_prod_eq at this, refine dt (this ⟨_, _⟩); dsimp; apply limc; assumption } end section local attribute [instance] uniform_space.separation_setoid lemma separated_pure_cauchy_injective {α : Type*} [uniform_space α] [s : separated_space α] : function.injective (λa:α, ⟦pure_cauchy a⟧) | a b h := separated_def.1 s _ _ $ assume s hs, let ⟨t, ht, hts⟩ := by rw [← (@uniform_embedding_pure_cauchy α _).comap_uniformity, filter.mem_comap] at hs; exact hs in have (pure_cauchy a, pure_cauchy b) ∈ t, from quotient.exact h t ht, @hts (a, b) this end end Cauchy local attribute [instance] uniform_space.separation_setoid open Cauchy set namespace uniform_space variables (α : Type*) [uniform_space α] variables {β : Type*} [uniform_space β] variables {γ : Type*} [uniform_space γ] instance complete_space_separation [h : complete_space α] : complete_space (quotient (separation_setoid α)) := ⟨assume f, assume hf : cauchy f, have cauchy (f.comap (λx, ⟦x⟧)), from hf.comap' comap_quotient_le_uniformity $ hf.left.comap_of_surj (surjective_quotient_mk _), let ⟨x, (hx : f.comap (λx, ⟦x⟧) ≤ 𝓝 x)⟩ := complete_space.complete this in ⟨⟦x⟧, (comap_le_comap_iff $ by simp).1 (hx.trans $ map_le_iff_le_comap.1 continuous_quotient_mk.continuous_at)⟩⟩ /-- Hausdorff completion of `α` -/ def completion := quotient (separation_setoid $ Cauchy α) namespace completion instance [inhabited α] : inhabited (completion α) := by unfold completion; apply_instance @[priority 50] instance : uniform_space (completion α) := by dunfold completion ; apply_instance instance : complete_space (completion α) := by dunfold completion ; apply_instance instance : separated_space (completion α) := by dunfold completion ; apply_instance instance : regular_space (completion α) := separated_regular /-- Automatic coercion from `α` to its completion. Not always injective. -/ instance : has_coe_t α (completion α) := ⟨quotient.mk ∘ pure_cauchy⟩ -- note [use has_coe_t] protected lemma coe_eq : (coe : α → completion α) = quotient.mk ∘ pure_cauchy := rfl lemma comap_coe_eq_uniformity : (𝓤 _).comap (λ(p:α×α), ((p.1 : completion α), (p.2 : completion α))) = 𝓤 α := begin have : (λx:α×α, ((x.1 : completion α), (x.2 : completion α))) = (λx:(Cauchy α)×(Cauchy α), (⟦x.1⟧, ⟦x.2⟧)) ∘ (λx:α×α, (pure_cauchy x.1, pure_cauchy x.2)), { ext ⟨a, b⟩; simp; refl }, rw [this, ← filter.comap_comap], change filter.comap _ (filter.comap _ (𝓤 $ quotient $ separation_setoid $ Cauchy α)) = 𝓤 α, rw [comap_quotient_eq_uniformity, uniform_embedding_pure_cauchy.comap_uniformity] end lemma uniform_inducing_coe : uniform_inducing (coe : α → completion α) := ⟨comap_coe_eq_uniformity α⟩ variables {α} lemma dense_range_coe : dense_range (coe : α → completion α) := dense_range_pure_cauchy.quotient variables (α) def cpkg {α : Type*} [uniform_space α] : abstract_completion α := { space := completion α, coe := coe, uniform_struct := by apply_instance, complete := by apply_instance, separation := by apply_instance, uniform_inducing := completion.uniform_inducing_coe α, dense := completion.dense_range_coe } instance abstract_completion.inhabited : inhabited (abstract_completion α) := ⟨cpkg⟩ local attribute [instance] abstract_completion.uniform_struct abstract_completion.complete abstract_completion.separation lemma nonempty_completion_iff : nonempty (completion α) ↔ nonempty α := cpkg.dense.nonempty_iff.symm lemma uniform_continuous_coe : uniform_continuous (coe : α → completion α) := cpkg.uniform_continuous_coe lemma continuous_coe : continuous (coe : α → completion α) := cpkg.continuous_coe lemma uniform_embedding_coe [separated_space α] : uniform_embedding (coe : α → completion α) := { comap_uniformity := comap_coe_eq_uniformity α, inj := separated_pure_cauchy_injective } variable {α} lemma dense_inducing_coe : dense_inducing (coe : α → completion α) := { dense := dense_range_coe, ..(uniform_inducing_coe α).inducing } open topological_space instance separable_space_completion [separable_space α] : separable_space (completion α) := completion.dense_inducing_coe.separable_space lemma dense_embedding_coe [separated_space α]: dense_embedding (coe : α → completion α) := { inj := separated_pure_cauchy_injective, ..dense_inducing_coe } lemma dense_range_coe₂ : dense_range (λx:α × β, ((x.1 : completion α), (x.2 : completion β))) := dense_range_coe.prod_map dense_range_coe lemma dense_range_coe₃ : dense_range (λx:α × (β × γ), ((x.1 : completion α), ((x.2.1 : completion β), (x.2.2 : completion γ)))) := dense_range_coe.prod_map dense_range_coe₂ @[elab_as_eliminator] lemma induction_on {p : completion α → Prop} (a : completion α) (hp : is_closed {a | p a}) (ih : ∀a:α, p a) : p a := is_closed_property dense_range_coe hp ih a @[elab_as_eliminator] lemma induction_on₂ {p : completion α → completion β → Prop} (a : completion α) (b : completion β) (hp : is_closed {x : completion α × completion β | p x.1 x.2}) (ih : ∀(a:α) (b:β), p a b) : p a b := have ∀x : completion α × completion β, p x.1 x.2, from is_closed_property dense_range_coe₂ hp $ assume ⟨a, b⟩, ih a b, this (a, b) @[elab_as_eliminator] lemma induction_on₃ {p : completion α → completion β → completion γ → Prop} (a : completion α) (b : completion β) (c : completion γ) (hp : is_closed {x : completion α × completion β × completion γ | p x.1 x.2.1 x.2.2}) (ih : ∀(a:α) (b:β) (c:γ), p a b c) : p a b c := have ∀x : completion α × completion β × completion γ, p x.1 x.2.1 x.2.2, from is_closed_property dense_range_coe₃ hp $ assume ⟨a, b, c⟩, ih a b c, this (a, b, c) lemma ext [t2_space β] {f g : completion α → β} (hf : continuous f) (hg : continuous g) (h : ∀a:α, f a = g a) : f = g := cpkg.funext hf hg h section extension variables {f : α → β} /-- "Extension" to the completion. It is defined for any map `f` but returns an arbitrary constant value if `f` is not uniformly continuous -/ protected def extension (f : α → β) : completion α → β := cpkg.extend f variables [separated_space β] @[simp]lemma extension_coe (hf : uniform_continuous f) (a : α) : (completion.extension f) a = f a := cpkg.extend_coe hf a variables [complete_space β] lemma uniform_continuous_extension : uniform_continuous (completion.extension f) := cpkg.uniform_continuous_extend lemma continuous_extension : continuous (completion.extension f) := cpkg.continuous_extend lemma extension_unique (hf : uniform_continuous f) {g : completion α → β} (hg : uniform_continuous g) (h : ∀ a : α, f a = g (a : completion α)) : completion.extension f = g := cpkg.extend_unique hf hg h @[simp] lemma extension_comp_coe {f : completion α → β} (hf : uniform_continuous f) : completion.extension (f ∘ coe) = f := cpkg.extend_comp_coe hf end extension section map variables {f : α → β} /-- Completion functor acting on morphisms -/ protected def map (f : α → β) : completion α → completion β := cpkg.map cpkg f lemma uniform_continuous_map : uniform_continuous (completion.map f) := cpkg.uniform_continuous_map cpkg f lemma continuous_map : continuous (completion.map f) := cpkg.continuous_map cpkg f @[simp] lemma map_coe (hf : uniform_continuous f) (a : α) : (completion.map f) a = f a := cpkg.map_coe cpkg hf a lemma map_unique {f : α → β} {g : completion α → completion β} (hg : uniform_continuous g) (h : ∀a:α, ↑(f a) = g a) : completion.map f = g := cpkg.map_unique cpkg hg h @[simp] lemma map_id : completion.map (@id α) = id := cpkg.map_id lemma extension_map [complete_space γ] [separated_space γ] {f : β → γ} {g : α → β} (hf : uniform_continuous f) (hg : uniform_continuous g) : completion.extension f ∘ completion.map g = completion.extension (f ∘ g) := completion.ext (continuous_extension.comp continuous_map) continuous_extension $ by intro a; simp only [hg, hf, hf.comp hg, (∘), map_coe, extension_coe] lemma map_comp {g : β → γ} {f : α → β} (hg : uniform_continuous g) (hf : uniform_continuous f) : completion.map g ∘ completion.map f = completion.map (g ∘ f) := extension_map ((uniform_continuous_coe _).comp hg) hf end map /- In this section we construct isomorphisms between the completion of a uniform space and the completion of its separation quotient -/ section separation_quotient_completion def completion_separation_quotient_equiv (α : Type u) [uniform_space α] : completion (separation_quotient α) ≃ completion α := begin refine ⟨completion.extension (separation_quotient.lift (coe : α → completion α)), completion.map quotient.mk, _, _⟩, { assume a, refine induction_on a (is_closed_eq (continuous_map.comp continuous_extension) continuous_id) _, rintros ⟨a⟩, show completion.map quotient.mk (completion.extension (separation_quotient.lift coe) ↑⟦a⟧) = ↑⟦a⟧, rw [extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe α), completion.map_coe uniform_continuous_quotient_mk] ; apply_instance }, { assume a, refine completion.induction_on a (is_closed_eq (continuous_extension.comp continuous_map) continuous_id) (λ a, _), rw [map_coe uniform_continuous_quotient_mk, extension_coe (separation_quotient.uniform_continuous_lift _), separation_quotient.lift_mk (uniform_continuous_coe α) _] ; apply_instance } end lemma uniform_continuous_completion_separation_quotient_equiv : uniform_continuous ⇑(completion_separation_quotient_equiv α) := uniform_continuous_extension lemma uniform_continuous_completion_separation_quotient_equiv_symm : uniform_continuous ⇑(completion_separation_quotient_equiv α).symm := uniform_continuous_map end separation_quotient_completion section extension₂ variables (f : α → β → γ) open function protected def extension₂ (f : α → β → γ) : completion α → completion β → γ := cpkg.extend₂ cpkg f variables [separated_space γ] {f} @[simp] lemma extension₂_coe_coe (hf : uniform_continuous₂ f) (a : α) (b : β) : completion.extension₂ f a b = f a b := cpkg.extension₂_coe_coe cpkg hf a b variables [complete_space γ] (f) lemma uniform_continuous_extension₂ : uniform_continuous₂ (completion.extension₂ f) := cpkg.uniform_continuous_extension₂ cpkg f end extension₂ section map₂ open function protected def map₂ (f : α → β → γ) : completion α → completion β → completion γ := cpkg.map₂ cpkg cpkg f lemma uniform_continuous_map₂ (f : α → β → γ) : uniform_continuous₂ (completion.map₂ f) := cpkg.uniform_continuous_map₂ cpkg cpkg f lemma continuous_map₂ {δ} [topological_space δ] {f : α → β → γ} {a : δ → completion α} {b : δ → completion β} (ha : continuous a) (hb : continuous b) : continuous (λd:δ, completion.map₂ f (a d) (b d)) := cpkg.continuous_map₂ cpkg cpkg ha hb lemma map₂_coe_coe (a : α) (b : β) (f : α → β → γ) (hf : uniform_continuous₂ f) : completion.map₂ f (a : completion α) (b : completion β) = f a b := cpkg.map₂_coe_coe cpkg cpkg a b f hf end map₂ end completion end uniform_space
15c067b8610408a0fa67ea9a135e05facbf68c8c
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/topology/metric_space/basic.lean
c1c04fd599f1f3977c05fa1982705ae4112f1ba3
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
134,243
lean
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel -/ import data.int.interval import tactic.positivity import topology.algebra.order.compact import topology.metric_space.emetric_space import topology.bornology.constructions import topology.uniform_space.complete_separated /-! # Metric spaces This file defines metric spaces. Many definitions and theorems expected on metric spaces are already introduced on uniform spaces and topological spaces. For example: open and closed sets, compactness, completeness, continuity and uniform continuity ## Main definitions * `has_dist α`: Endows a space `α` with a function `dist a b`. * `pseudo_metric_space α`: A space endowed with a distance function, which can be zero even if the two elements are non-equal. * `metric.ball x ε`: The set of all points `y` with `dist y x < ε`. * `metric.bounded s`: Whether a subset of a `pseudo_metric_space` is bounded. * `metric_space α`: A `pseudo_metric_space` with the guarantee `dist x y = 0 → x = y`. Additional useful definitions: * `nndist a b`: `dist` as a function to the non-negative reals. * `metric.closed_ball x ε`: The set of all points `y` with `dist y x ≤ ε`. * `metric.sphere x ε`: The set of all points `y` with `dist y x = ε`. * `proper_space α`: A `pseudo_metric_space` where all closed balls are compact. * `metric.diam s` : The `supr` of the distances of members of `s`. Defined in terms of `emetric.diam`, for better handling of the case when it should be infinite. TODO (anyone): Add "Main results" section. ## Implementation notes Since a lot of elementary properties don't require `eq_of_dist_eq_zero` we start setting up the theory of `pseudo_metric_space`, where we don't require `dist x y = 0 → x = y` and we specialize to `metric_space` at the end. ## Tags metric, pseudo_metric, dist -/ open set filter topological_space bornology open_locale uniformity topological_space big_operators filter nnreal ennreal universes u v w variables {α : Type u} {β : Type v} {X : Type*} /-- Construct a uniform structure core from a distance function and metric space axioms. This is a technical construction that can be immediately used to construct a uniform structure from a distance function and metric space axioms but is also useful when discussing metrizable topologies, see `pseudo_metric_space.of_metrizable`. -/ def uniform_space.core_of_dist {α : Type*} (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : uniform_space.core α := { uniformity := (⨅ ε>0, 𝓟 {p:α×α | dist p.1 p.2 < ε}), refl := le_infi $ assume ε, le_infi $ by simp [set.subset_def, id_rel, dist_self, (>)] {contextual := tt}, comp := le_infi $ assume ε, le_infi $ assume h, lift'_le (mem_infi_of_mem (ε / 2) $ mem_infi_of_mem (div_pos h zero_lt_two) (subset.refl _)) $ have ∀ (a b c : α), dist a c < ε / 2 → dist c b < ε / 2 → dist a b < ε, from assume a b c hac hcb, calc dist a b ≤ dist a c + dist c b : dist_triangle _ _ _ ... < ε / 2 + ε / 2 : add_lt_add hac hcb ... = ε : by rw [div_add_div_same, add_self_div_two], by simpa [comp_rel], symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h, tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [dist_comm] } /-- Construct a uniform structure from a distance function and metric space axioms -/ def uniform_space_of_dist (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : uniform_space α := uniform_space.of_core (uniform_space.core_of_dist dist dist_self dist_comm dist_triangle) /-- This is an internal lemma used to construct a bornology from a metric in `bornology.of_dist`. -/ private lemma bounded_iff_aux {α : Type*} (dist : α → α → ℝ) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) (s : set α) (a : α) : (∃ c, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ c) ↔ (∃ r, ∀ ⦃x⦄, x ∈ s → dist x a ≤ r) := begin split; rintro ⟨C, hC⟩, { rcases s.eq_empty_or_nonempty with rfl | ⟨x, hx⟩, { exact ⟨0, by simp⟩ }, { exact ⟨C + dist x a, λ y hy, (dist_triangle y x a).trans (add_le_add_right (hC hy hx) _)⟩ } }, { exact ⟨C + C, λ x hx y hy, (dist_triangle x a y).trans (add_le_add (hC hx) (by {rw dist_comm, exact hC hy}))⟩ } end /-- Construct a bornology from a distance function and metric space axioms. -/ def bornology.of_dist {α : Type*} (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) : bornology α := bornology.of_bounded { s : set α | ∃ C, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C } ⟨0, λ x hx y, hx.elim⟩ (λ s ⟨c, hc⟩ t h, ⟨c, λ x hx y hy, hc (h hx) (h hy)⟩) (λ s hs t ht, begin rcases s.eq_empty_or_nonempty with rfl | ⟨z, hz⟩, { exact (empty_union t).symm ▸ ht }, { simp only [λ u, bounded_iff_aux dist dist_comm dist_triangle u z] at hs ht ⊢, rcases ⟨hs, ht⟩ with ⟨⟨r₁, hr₁⟩, ⟨r₂, hr₂⟩⟩, exact ⟨max r₁ r₂, λ x hx, or.elim hx (λ hx', (hr₁ hx').trans (le_max_left _ _)) (λ hx', (hr₂ hx').trans (le_max_right _ _))⟩ } end) (λ z, ⟨0, λ x hx y hy, by { rw [eq_of_mem_singleton hx, eq_of_mem_singleton hy], exact (dist_self z).le }⟩) /-- The distance function (given an ambient metric space on `α`), which returns a nonnegative real number `dist x y` given `x y : α`. -/ @[ext] class has_dist (α : Type*) := (dist : α → α → ℝ) export has_dist (dist) -- the uniform structure and the emetric space structure are embedded in the metric space structure -- to avoid instance diamond issues. See Note [forgetful inheritance]. /-- This is an internal lemma used inside the default of `pseudo_metric_space.edist`. -/ private theorem pseudo_metric_space.dist_nonneg' {α} {x y : α} (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z): 0 ≤ dist x y := have 2 * dist x y ≥ 0, from calc 2 * dist x y = dist x y + dist y x : by rw [dist_comm x y, two_mul] ... ≥ 0 : by rw ← dist_self x; apply dist_triangle, nonneg_of_mul_nonneg_right this zero_lt_two /-- This tactic is used to populate `pseudo_metric_space.edist_dist` when the default `edist` is used. -/ protected meta def pseudo_metric_space.edist_dist_tac : tactic unit := tactic.intros >> `[exact (ennreal.of_real_eq_coe_nnreal _).symm <|> control_laws_tac] /-- Metric space Each metric space induces a canonical `uniform_space` and hence a canonical `topological_space`. This is enforced in the type class definition, by extending the `uniform_space` structure. When instantiating a `metric_space` structure, the uniformity fields are not necessary, they will be filled in by default. In the same way, each metric space induces an emetric space structure. It is included in the structure, but filled in by default. -/ class pseudo_metric_space (α : Type u) extends has_dist α : Type u := (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) (edist : α → α → ℝ≥0∞ := λ x y, @coe (ℝ≥0) _ _ ⟨dist x y, pseudo_metric_space.dist_nonneg' _ ‹_› ‹_› ‹_›⟩) (edist_dist : ∀ x y : α, edist x y = ennreal.of_real (dist x y) . pseudo_metric_space.edist_dist_tac) (to_uniform_space : uniform_space α := uniform_space_of_dist dist dist_self dist_comm dist_triangle) (uniformity_dist : 𝓤 α = ⨅ ε>0, 𝓟 {p:α×α | dist p.1 p.2 < ε} . control_laws_tac) (to_bornology : bornology α := bornology.of_dist dist dist_self dist_comm dist_triangle) (cobounded_sets : (bornology.cobounded α).sets = { s | ∃ C, ∀ ⦃x⦄, x ∈ sᶜ → ∀ ⦃y⦄, y ∈ sᶜ → dist x y ≤ C } . control_laws_tac) /-- Two pseudo metric space structures with the same distance function coincide. -/ @[ext] lemma pseudo_metric_space.ext {α : Type*} {m m' : pseudo_metric_space α} (h : m.to_has_dist = m'.to_has_dist) : m = m' := begin unfreezingI { rcases m, rcases m' }, dsimp at h, unfreezingI { subst h }, congr, { ext x y : 2, dsimp at m_edist_dist m'_edist_dist, simp [m_edist_dist, m'_edist_dist] }, { dsimp at m_uniformity_dist m'_uniformity_dist, rw ← m'_uniformity_dist at m_uniformity_dist, exact uniform_space_eq m_uniformity_dist }, { ext1, dsimp at m_cobounded_sets m'_cobounded_sets, rw ← m'_cobounded_sets at m_cobounded_sets, exact filter_eq m_cobounded_sets } end variables [pseudo_metric_space α] attribute [priority 100, instance] pseudo_metric_space.to_uniform_space attribute [priority 100, instance] pseudo_metric_space.to_bornology @[priority 200] -- see Note [lower instance priority] instance pseudo_metric_space.to_has_edist : has_edist α := ⟨pseudo_metric_space.edist⟩ /-- Construct a pseudo-metric space structure whose underlying topological space structure (definitionally) agrees which a pre-existing topology which is compatible with a given distance function. -/ def pseudo_metric_space.of_metrizable {α : Type*} [topological_space α] (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) (H : ∀ s : set α, is_open s ↔ ∀ x ∈ s, ∃ ε > 0, ∀ y, dist x y < ε → y ∈ s) : pseudo_metric_space α := { dist := dist, dist_self := dist_self, dist_comm := dist_comm, dist_triangle := dist_triangle, to_uniform_space := { is_open_uniformity := begin dsimp only [uniform_space.core_of_dist], intros s, change is_open s ↔ _, rw H s, refine forall₂_congr (λ x x_in, _), erw (has_basis_binfi_principal _ nonempty_Ioi).mem_iff, { refine exists₂_congr (λ ε ε_pos, _), simp only [prod.forall, set_of_subset_set_of], split, { rintros h _ y H rfl, exact h y H }, { intros h y hxy, exact h _ _ hxy rfl } }, { exact λ r (hr : 0 < r) p (hp : 0 < p), ⟨min r p, lt_min hr hp, λ x (hx : dist _ _ < _), lt_of_lt_of_le hx (min_le_left r p), λ x (hx : dist _ _ < _), lt_of_lt_of_le hx (min_le_right r p)⟩ }, { apply_instance } end, ..uniform_space.core_of_dist dist dist_self dist_comm dist_triangle }, uniformity_dist := rfl, to_bornology := bornology.of_dist dist dist_self dist_comm dist_triangle, cobounded_sets := rfl } @[simp] theorem dist_self (x : α) : dist x x = 0 := pseudo_metric_space.dist_self x theorem dist_comm (x y : α) : dist x y = dist y x := pseudo_metric_space.dist_comm x y theorem edist_dist (x y : α) : edist x y = ennreal.of_real (dist x y) := pseudo_metric_space.edist_dist x y theorem dist_triangle (x y z : α) : dist x z ≤ dist x y + dist y z := pseudo_metric_space.dist_triangle x y z theorem dist_triangle_left (x y z : α) : dist x y ≤ dist z x + dist z y := by rw dist_comm z; apply dist_triangle theorem dist_triangle_right (x y z : α) : dist x y ≤ dist x z + dist y z := by rw dist_comm y; apply dist_triangle lemma dist_triangle4 (x y z w : α) : dist x w ≤ dist x y + dist y z + dist z w := calc dist x w ≤ dist x z + dist z w : dist_triangle x z w ... ≤ (dist x y + dist y z) + dist z w : add_le_add_right (dist_triangle x y z) _ lemma dist_triangle4_left (x₁ y₁ x₂ y₂ : α) : dist x₂ y₂ ≤ dist x₁ y₁ + (dist x₁ x₂ + dist y₁ y₂) := by { rw [add_left_comm, dist_comm x₁, ← add_assoc], apply dist_triangle4 } lemma dist_triangle4_right (x₁ y₁ x₂ y₂ : α) : dist x₁ y₁ ≤ dist x₁ x₂ + dist y₁ y₂ + dist x₂ y₂ := by { rw [add_right_comm, dist_comm y₁], apply dist_triangle4 } /-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/ lemma dist_le_Ico_sum_dist (f : ℕ → α) {m n} (h : m ≤ n) : dist (f m) (f n) ≤ ∑ i in finset.Ico m n, dist (f i) (f (i + 1)) := begin revert n, apply nat.le_induction, { simp only [finset.sum_empty, finset.Ico_self, dist_self] }, { assume n hn hrec, calc dist (f m) (f (n+1)) ≤ dist (f m) (f n) + dist _ _ : dist_triangle _ _ _ ... ≤ ∑ i in finset.Ico m n, _ + _ : add_le_add hrec le_rfl ... = ∑ i in finset.Ico m (n+1), _ : by rw [nat.Ico_succ_right_eq_insert_Ico hn, finset.sum_insert, add_comm]; simp } end /-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/ lemma dist_le_range_sum_dist (f : ℕ → α) (n : ℕ) : dist (f 0) (f n) ≤ ∑ i in finset.range n, dist (f i) (f (i + 1)) := nat.Ico_zero_eq_range ▸ dist_le_Ico_sum_dist f (nat.zero_le n) /-- A version of `dist_le_Ico_sum_dist` with each intermediate distance replaced with an upper estimate. -/ lemma dist_le_Ico_sum_of_dist_le {f : ℕ → α} {m n} (hmn : m ≤ n) {d : ℕ → ℝ} (hd : ∀ {k}, m ≤ k → k < n → dist (f k) (f (k + 1)) ≤ d k) : dist (f m) (f n) ≤ ∑ i in finset.Ico m n, d i := le_trans (dist_le_Ico_sum_dist f hmn) $ finset.sum_le_sum $ λ k hk, hd (finset.mem_Ico.1 hk).1 (finset.mem_Ico.1 hk).2 /-- A version of `dist_le_range_sum_dist` with each intermediate distance replaced with an upper estimate. -/ lemma dist_le_range_sum_of_dist_le {f : ℕ → α} (n : ℕ) {d : ℕ → ℝ} (hd : ∀ {k}, k < n → dist (f k) (f (k + 1)) ≤ d k) : dist (f 0) (f n) ≤ ∑ i in finset.range n, d i := nat.Ico_zero_eq_range ▸ dist_le_Ico_sum_of_dist_le (zero_le n) (λ _ _, hd) theorem swap_dist : function.swap (@dist α _) = dist := by funext x y; exact dist_comm _ _ theorem abs_dist_sub_le (x y z : α) : |dist x z - dist y z| ≤ dist x y := abs_sub_le_iff.2 ⟨sub_le_iff_le_add.2 (dist_triangle _ _ _), sub_le_iff_le_add.2 (dist_triangle_left _ _ _)⟩ theorem dist_nonneg {x y : α} : 0 ≤ dist x y := pseudo_metric_space.dist_nonneg' dist dist_self dist_comm dist_triangle section open tactic tactic.positivity /-- Extension for the `positivity` tactic: distances are nonnegative. -/ @[positivity] meta def _root_.tactic.positivity_dist : expr → tactic strictness | `(dist %%a %%b) := nonnegative <$> mk_app ``dist_nonneg [a, b] | _ := failed end @[simp] theorem abs_dist {a b : α} : |dist a b| = dist a b := abs_of_nonneg dist_nonneg /-- A version of `has_dist` that takes value in `ℝ≥0`. -/ class has_nndist (α : Type*) := (nndist : α → α → ℝ≥0) export has_nndist (nndist) /-- Distance as a nonnegative real number. -/ @[priority 100] -- see Note [lower instance priority] instance pseudo_metric_space.to_has_nndist : has_nndist α := ⟨λ a b, ⟨dist a b, dist_nonneg⟩⟩ /--Express `nndist` in terms of `edist`-/ lemma nndist_edist (x y : α) : nndist x y = (edist x y).to_nnreal := by simp [nndist, edist_dist, real.to_nnreal, max_eq_left dist_nonneg, ennreal.of_real] /--Express `edist` in terms of `nndist`-/ lemma edist_nndist (x y : α) : edist x y = ↑(nndist x y) := by { simpa only [edist_dist, ennreal.of_real_eq_coe_nnreal dist_nonneg] } @[simp, norm_cast] lemma coe_nnreal_ennreal_nndist (x y : α) : ↑(nndist x y) = edist x y := (edist_nndist x y).symm @[simp, norm_cast] lemma edist_lt_coe {x y : α} {c : ℝ≥0} : edist x y < c ↔ nndist x y < c := by rw [edist_nndist, ennreal.coe_lt_coe] @[simp, norm_cast] lemma edist_le_coe {x y : α} {c : ℝ≥0} : edist x y ≤ c ↔ nndist x y ≤ c := by rw [edist_nndist, ennreal.coe_le_coe] /--In a pseudometric space, the extended distance is always finite-/ lemma edist_lt_top {α : Type*} [pseudo_metric_space α] (x y : α) : edist x y < ⊤ := (edist_dist x y).symm ▸ ennreal.of_real_lt_top /--In a pseudometric space, the extended distance is always finite-/ lemma edist_ne_top (x y : α) : edist x y ≠ ⊤ := (edist_lt_top x y).ne /--`nndist x x` vanishes-/ @[simp] lemma nndist_self (a : α) : nndist a a = 0 := (nnreal.coe_eq_zero _).1 (dist_self a) /--Express `dist` in terms of `nndist`-/ lemma dist_nndist (x y : α) : dist x y = ↑(nndist x y) := rfl @[simp, norm_cast] lemma coe_nndist (x y : α) : ↑(nndist x y) = dist x y := (dist_nndist x y).symm @[simp, norm_cast] lemma dist_lt_coe {x y : α} {c : ℝ≥0} : dist x y < c ↔ nndist x y < c := iff.rfl @[simp, norm_cast] lemma dist_le_coe {x y : α} {c : ℝ≥0} : dist x y ≤ c ↔ nndist x y ≤ c := iff.rfl @[simp] lemma edist_lt_of_real {x y : α} {r : ℝ} : edist x y < ennreal.of_real r ↔ dist x y < r := by rw [edist_dist, ennreal.of_real_lt_of_real_iff_of_nonneg dist_nonneg] @[simp] lemma edist_le_of_real {x y : α} {r : ℝ} (hr : 0 ≤ r) : edist x y ≤ ennreal.of_real r ↔ dist x y ≤ r := by rw [edist_dist, ennreal.of_real_le_of_real_iff hr] /--Express `nndist` in terms of `dist`-/ lemma nndist_dist (x y : α) : nndist x y = real.to_nnreal (dist x y) := by rw [dist_nndist, real.to_nnreal_coe] theorem nndist_comm (x y : α) : nndist x y = nndist y x := by simpa only [dist_nndist, nnreal.coe_eq] using dist_comm x y /--Triangle inequality for the nonnegative distance-/ theorem nndist_triangle (x y z : α) : nndist x z ≤ nndist x y + nndist y z := dist_triangle _ _ _ theorem nndist_triangle_left (x y z : α) : nndist x y ≤ nndist z x + nndist z y := dist_triangle_left _ _ _ theorem nndist_triangle_right (x y z : α) : nndist x y ≤ nndist x z + nndist y z := dist_triangle_right _ _ _ /--Express `dist` in terms of `edist`-/ lemma dist_edist (x y : α) : dist x y = (edist x y).to_real := by rw [edist_dist, ennreal.to_real_of_real (dist_nonneg)] namespace metric /- instantiate pseudometric space as a topology -/ variables {x y z : α} {δ ε ε₁ ε₂ : ℝ} {s : set α} /-- `ball x ε` is the set of all points `y` with `dist y x < ε` -/ def ball (x : α) (ε : ℝ) : set α := {y | dist y x < ε} @[simp] theorem mem_ball : y ∈ ball x ε ↔ dist y x < ε := iff.rfl theorem mem_ball' : y ∈ ball x ε ↔ dist x y < ε := by rw [dist_comm, mem_ball] theorem pos_of_mem_ball (hy : y ∈ ball x ε) : 0 < ε := dist_nonneg.trans_lt hy theorem mem_ball_self (h : 0 < ε) : x ∈ ball x ε := show dist x x < ε, by rw dist_self; assumption @[simp] lemma nonempty_ball : (ball x ε).nonempty ↔ 0 < ε := ⟨λ ⟨x, hx⟩, pos_of_mem_ball hx, λ h, ⟨x, mem_ball_self h⟩⟩ @[simp] lemma ball_eq_empty : ball x ε = ∅ ↔ ε ≤ 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_ball, not_lt] @[simp] lemma ball_zero : ball x 0 = ∅ := by rw [ball_eq_empty] /-- If a point belongs to an open ball, then there is a strictly smaller radius whose ball also contains it. See also `exists_lt_subset_ball`. -/ lemma exists_lt_mem_ball_of_mem_ball (h : x ∈ ball y ε) : ∃ ε' < ε, x ∈ ball y ε' := begin simp only [mem_ball] at h ⊢, exact ⟨(ε + dist x y) / 2, by linarith, by linarith⟩, end lemma ball_eq_ball (ε : ℝ) (x : α) : uniform_space.ball x {p | dist p.2 p.1 < ε} = metric.ball x ε := rfl lemma ball_eq_ball' (ε : ℝ) (x : α) : uniform_space.ball x {p | dist p.1 p.2 < ε} = metric.ball x ε := by { ext, simp [dist_comm, uniform_space.ball] } @[simp] lemma Union_ball_nat (x : α) : (⋃ n : ℕ, ball x n) = univ := Union_eq_univ_iff.2 $ λ y, exists_nat_gt (dist y x) @[simp] lemma Union_ball_nat_succ (x : α) : (⋃ n : ℕ, ball x (n + 1)) = univ := Union_eq_univ_iff.2 $ λ y, (exists_nat_gt (dist y x)).imp $ λ n hn, hn.trans (lt_add_one _) /-- `closed_ball x ε` is the set of all points `y` with `dist y x ≤ ε` -/ def closed_ball (x : α) (ε : ℝ) := {y | dist y x ≤ ε} @[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ dist y x ≤ ε := iff.rfl theorem mem_closed_ball' : y ∈ closed_ball x ε ↔ dist x y ≤ ε := by rw [dist_comm, mem_closed_ball] /-- `sphere x ε` is the set of all points `y` with `dist y x = ε` -/ def sphere (x : α) (ε : ℝ) := {y | dist y x = ε} @[simp] theorem mem_sphere : y ∈ sphere x ε ↔ dist y x = ε := iff.rfl theorem mem_sphere' : y ∈ sphere x ε ↔ dist x y = ε := by rw [dist_comm, mem_sphere] theorem ne_of_mem_sphere (h : y ∈ sphere x ε) (hε : ε ≠ 0) : y ≠ x := by { contrapose! hε, symmetry, simpa [hε] using h } theorem sphere_eq_empty_of_subsingleton [subsingleton α] (hε : ε ≠ 0) : sphere x ε = ∅ := set.eq_empty_iff_forall_not_mem.mpr $ λ y hy, ne_of_mem_sphere hy hε (subsingleton.elim _ _) theorem sphere_is_empty_of_subsingleton [subsingleton α] (hε : ε ≠ 0) : is_empty (sphere x ε) := by simp only [sphere_eq_empty_of_subsingleton hε, set.has_emptyc.emptyc.is_empty α] theorem mem_closed_ball_self (h : 0 ≤ ε) : x ∈ closed_ball x ε := show dist x x ≤ ε, by rw dist_self; assumption @[simp] lemma nonempty_closed_ball : (closed_ball x ε).nonempty ↔ 0 ≤ ε := ⟨λ ⟨x, hx⟩, dist_nonneg.trans hx, λ h, ⟨x, mem_closed_ball_self h⟩⟩ @[simp] lemma closed_ball_eq_empty : closed_ball x ε = ∅ ↔ ε < 0 := by rw [← not_nonempty_iff_eq_empty, nonempty_closed_ball, not_le] theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε := assume y (hy : _ < _), le_of_lt hy theorem sphere_subset_closed_ball : sphere x ε ⊆ closed_ball x ε := λ y, le_of_eq lemma closed_ball_disjoint_ball (h : δ + ε ≤ dist x y) : disjoint (closed_ball x δ) (ball y ε) := λ a ha, (h.trans $ dist_triangle_left _ _ _).not_lt $ add_lt_add_of_le_of_lt ha.1 ha.2 lemma ball_disjoint_closed_ball (h : δ + ε ≤ dist x y) : disjoint (ball x δ) (closed_ball y ε) := (closed_ball_disjoint_ball $ by rwa [add_comm, dist_comm]).symm lemma ball_disjoint_ball (h : δ + ε ≤ dist x y) : disjoint (ball x δ) (ball y ε) := (closed_ball_disjoint_ball h).mono_left ball_subset_closed_ball lemma closed_ball_disjoint_closed_ball (h : δ + ε < dist x y) : disjoint (closed_ball x δ) (closed_ball y ε) := λ a ha, h.not_le $ (dist_triangle_left _ _ _).trans $ add_le_add ha.1 ha.2 theorem sphere_disjoint_ball : disjoint (sphere x ε) (ball x ε) := λ y ⟨hy₁, hy₂⟩, absurd hy₁ $ ne_of_lt hy₂ @[simp] theorem ball_union_sphere : ball x ε ∪ sphere x ε = closed_ball x ε := set.ext $ λ y, (@le_iff_lt_or_eq ℝ _ _ _).symm @[simp] theorem sphere_union_ball : sphere x ε ∪ ball x ε = closed_ball x ε := by rw [union_comm, ball_union_sphere] @[simp] theorem closed_ball_diff_sphere : closed_ball x ε \ sphere x ε = ball x ε := by rw [← ball_union_sphere, set.union_diff_cancel_right sphere_disjoint_ball.symm] @[simp] theorem closed_ball_diff_ball : closed_ball x ε \ ball x ε = sphere x ε := by rw [← ball_union_sphere, set.union_diff_cancel_left sphere_disjoint_ball.symm] theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε := by rw [mem_ball', mem_ball] theorem mem_closed_ball_comm : x ∈ closed_ball y ε ↔ y ∈ closed_ball x ε := by rw [mem_closed_ball', mem_closed_ball] theorem mem_sphere_comm : x ∈ sphere y ε ↔ y ∈ sphere x ε := by rw [mem_sphere', mem_sphere] theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ := λ y (yx : _ < ε₁), lt_of_lt_of_le yx h lemma ball_subset_ball' (h : ε₁ + dist x y ≤ ε₂) : ball x ε₁ ⊆ ball y ε₂ := λ z hz, calc dist z y ≤ dist z x + dist x y : dist_triangle _ _ _ ... < ε₁ + dist x y : add_lt_add_right hz _ ... ≤ ε₂ : h theorem closed_ball_subset_closed_ball (h : ε₁ ≤ ε₂) : closed_ball x ε₁ ⊆ closed_ball x ε₂ := λ y (yx : _ ≤ ε₁), le_trans yx h lemma closed_ball_subset_closed_ball' (h : ε₁ + dist x y ≤ ε₂) : closed_ball x ε₁ ⊆ closed_ball y ε₂ := λ z hz, calc dist z y ≤ dist z x + dist x y : dist_triangle _ _ _ ... ≤ ε₁ + dist x y : add_le_add_right hz _ ... ≤ ε₂ : h theorem closed_ball_subset_ball (h : ε₁ < ε₂) : closed_ball x ε₁ ⊆ ball x ε₂ := λ y (yh : dist y x ≤ ε₁), lt_of_le_of_lt yh h lemma dist_le_add_of_nonempty_closed_ball_inter_closed_ball (h : (closed_ball x ε₁ ∩ closed_ball y ε₂).nonempty) : dist x y ≤ ε₁ + ε₂ := let ⟨z, hz⟩ := h in calc dist x y ≤ dist z x + dist z y : dist_triangle_left _ _ _ ... ≤ ε₁ + ε₂ : add_le_add hz.1 hz.2 lemma dist_lt_add_of_nonempty_closed_ball_inter_ball (h : (closed_ball x ε₁ ∩ ball y ε₂).nonempty) : dist x y < ε₁ + ε₂ := let ⟨z, hz⟩ := h in calc dist x y ≤ dist z x + dist z y : dist_triangle_left _ _ _ ... < ε₁ + ε₂ : add_lt_add_of_le_of_lt hz.1 hz.2 lemma dist_lt_add_of_nonempty_ball_inter_closed_ball (h : (ball x ε₁ ∩ closed_ball y ε₂).nonempty) : dist x y < ε₁ + ε₂ := begin rw inter_comm at h, rw [add_comm, dist_comm], exact dist_lt_add_of_nonempty_closed_ball_inter_ball h end lemma dist_lt_add_of_nonempty_ball_inter_ball (h : (ball x ε₁ ∩ ball y ε₂).nonempty) : dist x y < ε₁ + ε₂ := dist_lt_add_of_nonempty_closed_ball_inter_ball $ h.mono (inter_subset_inter ball_subset_closed_ball subset.rfl) @[simp] lemma Union_closed_ball_nat (x : α) : (⋃ n : ℕ, closed_ball x n) = univ := Union_eq_univ_iff.2 $ λ y, exists_nat_ge (dist y x) lemma Union_inter_closed_ball_nat (s : set α) (x : α) : (⋃ (n : ℕ), s ∩ closed_ball x n) = s := by rw [← inter_Union, Union_closed_ball_nat, inter_univ] theorem ball_subset (h : dist x y ≤ ε₂ - ε₁) : ball x ε₁ ⊆ ball y ε₂ := λ z zx, by rw ← add_sub_cancel'_right ε₁ ε₂; exact lt_of_le_of_lt (dist_triangle z x y) (add_lt_add_of_lt_of_le zx h) theorem ball_half_subset (y) (h : y ∈ ball x (ε / 2)) : ball y (ε / 2) ⊆ ball x ε := ball_subset $ by rw sub_self_div_two; exact le_of_lt h theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε := ⟨_, sub_pos.2 h, ball_subset $ by rw sub_sub_self⟩ /-- If a property holds for all points in closed balls of arbitrarily large radii, then it holds for all points. -/ lemma forall_of_forall_mem_closed_ball (p : α → Prop) (x : α) (H : ∃ᶠ (R : ℝ) in at_top, ∀ y ∈ closed_ball x R, p y) (y : α) : p y := begin obtain ⟨R, hR, h⟩ : ∃ (R : ℝ) (H : dist y x ≤ R), ∀ (z : α), z ∈ closed_ball x R → p z := frequently_iff.1 H (Ici_mem_at_top (dist y x)), exact h _ hR end /-- If a property holds for all points in balls of arbitrarily large radii, then it holds for all points. -/ lemma forall_of_forall_mem_ball (p : α → Prop) (x : α) (H : ∃ᶠ (R : ℝ) in at_top, ∀ y ∈ ball x R, p y) (y : α) : p y := begin obtain ⟨R, hR, h⟩ : ∃ (R : ℝ) (H : dist y x < R), ∀ (z : α), z ∈ ball x R → p z := frequently_iff.1 H (Ioi_mem_at_top (dist y x)), exact h _ hR end theorem is_bounded_iff {s : set α} : is_bounded s ↔ ∃ C : ℝ, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C := by rw [is_bounded_def, ← filter.mem_sets, (@pseudo_metric_space.cobounded_sets α _).out, mem_set_of_eq, compl_compl] theorem is_bounded_iff_eventually {s : set α} : is_bounded s ↔ ∀ᶠ C in at_top, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C := is_bounded_iff.trans ⟨λ ⟨C, h⟩, eventually_at_top.2 ⟨C, λ C' hC' x hx y hy, (h hx hy).trans hC'⟩, eventually.exists⟩ theorem is_bounded_iff_exists_ge {s : set α} (c : ℝ) : is_bounded s ↔ ∃ C, c ≤ C ∧ ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → dist x y ≤ C := ⟨λ h, ((eventually_ge_at_top c).and (is_bounded_iff_eventually.1 h)).exists, λ h, is_bounded_iff.2 $ h.imp $ λ _, and.right⟩ theorem is_bounded_iff_nndist {s : set α} : is_bounded s ↔ ∃ C : ℝ≥0, ∀ ⦃x⦄, x ∈ s → ∀ ⦃y⦄, y ∈ s → nndist x y ≤ C := by simp only [is_bounded_iff_exists_ge 0, nnreal.exists, ← nnreal.coe_le_coe, ← dist_nndist, nnreal.coe_mk, exists_prop] theorem uniformity_basis_dist : (𝓤 α).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {p:α×α | dist p.1 p.2 < ε}) := begin rw ← pseudo_metric_space.uniformity_dist.symm, refine has_basis_binfi_principal _ nonempty_Ioi, exact λ r (hr : 0 < r) p (hp : 0 < p), ⟨min r p, lt_min hr hp, λ x (hx : dist _ _ < _), lt_of_lt_of_le hx (min_le_left r p), λ x (hx : dist _ _ < _), lt_of_lt_of_le hx (min_le_right r p)⟩ end /-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`. For specific bases see `uniformity_basis_dist`, `uniformity_basis_dist_inv_nat_succ`, and `uniformity_basis_dist_inv_nat_pos`. -/ protected theorem mk_uniformity_basis {β : Type*} {p : β → Prop} {f : β → ℝ} (hf₀ : ∀ i, p i → 0 < f i) (hf : ∀ ⦃ε⦄, 0 < ε → ∃ i (hi : p i), f i ≤ ε) : (𝓤 α).has_basis p (λ i, {p:α×α | dist p.1 p.2 < f i}) := begin refine ⟨λ s, uniformity_basis_dist.mem_iff.trans _⟩, split, { rintros ⟨ε, ε₀, hε⟩, obtain ⟨i, hi, H⟩ : ∃ i (hi : p i), f i ≤ ε, from hf ε₀, exact ⟨i, hi, λ x (hx : _ < _), hε $ lt_of_lt_of_le hx H⟩ }, { exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, H⟩ } end theorem uniformity_basis_dist_inv_nat_succ : (𝓤 α).has_basis (λ _, true) (λ n:ℕ, {p:α×α | dist p.1 p.2 < 1 / (↑n+1) }) := metric.mk_uniformity_basis (λ n _, div_pos zero_lt_one $ nat.cast_add_one_pos n) (λ ε ε0, (exists_nat_one_div_lt ε0).imp $ λ n hn, ⟨trivial, le_of_lt hn⟩) theorem uniformity_basis_dist_inv_nat_pos : (𝓤 α).has_basis (λ n:ℕ, 0<n) (λ n:ℕ, {p:α×α | dist p.1 p.2 < 1 / ↑n }) := metric.mk_uniformity_basis (λ n hn, div_pos zero_lt_one $ nat.cast_pos.2 hn) (λ ε ε0, let ⟨n, hn⟩ := exists_nat_one_div_lt ε0 in ⟨n+1, nat.succ_pos n, by exact_mod_cast hn.le⟩) theorem uniformity_basis_dist_pow {r : ℝ} (h0 : 0 < r) (h1 : r < 1) : (𝓤 α).has_basis (λ n:ℕ, true) (λ n:ℕ, {p:α×α | dist p.1 p.2 < r ^ n }) := metric.mk_uniformity_basis (λ n hn, pow_pos h0 _) (λ ε ε0, let ⟨n, hn⟩ := exists_pow_lt_of_lt_one ε0 h1 in ⟨n, trivial, hn.le⟩) theorem uniformity_basis_dist_lt {R : ℝ} (hR : 0 < R) : (𝓤 α).has_basis (λ r : ℝ, 0 < r ∧ r < R) (λ r, {p : α × α | dist p.1 p.2 < r}) := metric.mk_uniformity_basis (λ r, and.left) $ λ r hr, ⟨min r (R / 2), ⟨lt_min hr (half_pos hR), min_lt_iff.2 $ or.inr (half_lt_self hR)⟩, min_le_left _ _⟩ /-- Given `f : β → ℝ`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then closed neighborhoods of the diagonal of sizes `{f i | p i}` form a basis of `𝓤 α`. Currently we have only one specific basis `uniformity_basis_dist_le` based on this constructor. More can be easily added if needed in the future. -/ protected theorem mk_uniformity_basis_le {β : Type*} {p : β → Prop} {f : β → ℝ} (hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) : (𝓤 α).has_basis p (λ x, {p:α×α | dist p.1 p.2 ≤ f x}) := begin refine ⟨λ s, uniformity_basis_dist.mem_iff.trans _⟩, split, { rintros ⟨ε, ε₀, hε⟩, rcases exists_between ε₀ with ⟨ε', hε'⟩, rcases hf ε' hε'.1 with ⟨i, hi, H⟩, exact ⟨i, hi, λ x (hx : _ ≤ _), hε $ lt_of_le_of_lt (le_trans hx H) hε'.2⟩ }, { exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, λ x (hx : _ < _), H (le_of_lt hx)⟩ } end /-- Contant size closed neighborhoods of the diagonal form a basis of the uniformity filter. -/ theorem uniformity_basis_dist_le : (𝓤 α).has_basis (λ ε : ℝ, 0 < ε) (λ ε, {p:α×α | dist p.1 p.2 ≤ ε}) := metric.mk_uniformity_basis_le (λ _, id) (λ ε ε₀, ⟨ε, ε₀, le_refl ε⟩) theorem uniformity_basis_dist_le_pow {r : ℝ} (h0 : 0 < r) (h1 : r < 1) : (𝓤 α).has_basis (λ n:ℕ, true) (λ n:ℕ, {p:α×α | dist p.1 p.2 ≤ r ^ n }) := metric.mk_uniformity_basis_le (λ n hn, pow_pos h0 _) (λ ε ε0, let ⟨n, hn⟩ := exists_pow_lt_of_lt_one ε0 h1 in ⟨n, trivial, hn.le⟩) theorem mem_uniformity_dist {s : set (α×α)} : s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, dist a b < ε → (a, b) ∈ s) := uniformity_basis_dist.mem_uniformity_iff /-- A constant size neighborhood of the diagonal is an entourage. -/ theorem dist_mem_uniformity {ε:ℝ} (ε0 : 0 < ε) : {p:α×α | dist p.1 p.2 < ε} ∈ 𝓤 α := mem_uniformity_dist.2 ⟨ε, ε0, λ a b, id⟩ theorem uniform_continuous_iff [pseudo_metric_space β] {f : α → β} : uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0, ∀{a b:α}, dist a b < δ → dist (f a) (f b) < ε := uniformity_basis_dist.uniform_continuous_iff uniformity_basis_dist lemma uniform_continuous_on_iff [pseudo_metric_space β] {f : α → β} {s : set α} : uniform_continuous_on f s ↔ ∀ ε > 0, ∃ δ > 0, ∀ x y ∈ s, dist x y < δ → dist (f x) (f y) < ε := metric.uniformity_basis_dist.uniform_continuous_on_iff metric.uniformity_basis_dist lemma uniform_continuous_on_iff_le [pseudo_metric_space β] {f : α → β} {s : set α} : uniform_continuous_on f s ↔ ∀ ε > 0, ∃ δ > 0, ∀ x y ∈ s, dist x y ≤ δ → dist (f x) (f y) ≤ ε := metric.uniformity_basis_dist_le.uniform_continuous_on_iff metric.uniformity_basis_dist_le theorem uniform_embedding_iff [pseudo_metric_space β] {f : α → β} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ ∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ := uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl ⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (dist_mem_uniformity δ0), ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 tu in ⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩, λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in ⟨_, dist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩ /-- If a map between pseudometric spaces is a uniform embedding then the distance between `f x` and `f y` is controlled in terms of the distance between `x` and `y`. -/ theorem controlled_of_uniform_embedding [pseudo_metric_space β] {f : α → β} : uniform_embedding f → (∀ ε > 0, ∃ δ > 0, ∀ {a b : α}, dist a b < δ → dist (f a) (f b) < ε) ∧ (∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, dist (f a) (f b) < ε → dist a b < δ) := begin assume h, exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1, (uniform_embedding_iff.1 h).2.2⟩ end theorem totally_bounded_iff {s : set α} : totally_bounded s ↔ ∀ ε > 0, ∃t : set α, t.finite ∧ s ⊆ ⋃y∈t, ball y ε := ⟨λ H ε ε0, H _ (dist_mem_uniformity ε0), λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_dist.1 ru, ⟨t, ft, h⟩ := H ε ε0 in ⟨t, ft, h.trans $ Union₂_mono $ λ y yt z, hε⟩⟩ /-- A pseudometric space is totally bounded if one can reconstruct up to any ε>0 any element of the space from finitely many data. -/ lemma totally_bounded_of_finite_discretization {s : set α} (H : ∀ε > (0 : ℝ), ∃ (β : Type u) (_ : fintype β) (F : s → β), ∀x y, F x = F y → dist (x:α) y < ε) : totally_bounded s := begin cases s.eq_empty_or_nonempty with hs hs, { rw hs, exact totally_bounded_empty }, rcases hs with ⟨x0, hx0⟩, haveI : inhabited s := ⟨⟨x0, hx0⟩⟩, refine totally_bounded_iff.2 (λ ε ε0, _), rcases H ε ε0 with ⟨β, fβ, F, hF⟩, resetI, let Finv := function.inv_fun F, refine ⟨range (subtype.val ∘ Finv), finite_range _, λ x xs, _⟩, let x' := Finv (F ⟨x, xs⟩), have : F x' = F ⟨x, xs⟩ := function.inv_fun_eq ⟨⟨x, xs⟩, rfl⟩, simp only [set.mem_Union, set.mem_range], exact ⟨_, ⟨F ⟨x, xs⟩, rfl⟩, hF _ _ this.symm⟩ end theorem finite_approx_of_totally_bounded {s : set α} (hs : totally_bounded s) : ∀ ε > 0, ∃ t ⊆ s, set.finite t ∧ s ⊆ ⋃y∈t, ball y ε := begin intros ε ε_pos, rw totally_bounded_iff_subset at hs, exact hs _ (dist_mem_uniformity ε_pos), end /-- Expressing locally uniform convergence on a set using `dist`. -/ lemma tendsto_locally_uniformly_on_iff {ι : Type*} [topological_space β] {F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} : tendsto_locally_uniformly_on F f p s ↔ ∀ ε > 0, ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, ∀ᶠ n in p, ∀ y ∈ t, dist (f y) (F n y) < ε := begin refine ⟨λ H ε hε, H _ (dist_mem_uniformity hε), λ H u hu x hx, _⟩, rcases mem_uniformity_dist.1 hu with ⟨ε, εpos, hε⟩, rcases H ε εpos x hx with ⟨t, ht, Ht⟩, exact ⟨t, ht, Ht.mono (λ n hs x hx, hε (hs x hx))⟩ end /-- Expressing uniform convergence on a set using `dist`. -/ lemma tendsto_uniformly_on_iff {ι : Type*} {F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} : tendsto_uniformly_on F f p s ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x ∈ s, dist (f x) (F n x) < ε := begin refine ⟨λ H ε hε, H _ (dist_mem_uniformity hε), λ H u hu, _⟩, rcases mem_uniformity_dist.1 hu with ⟨ε, εpos, hε⟩, exact (H ε εpos).mono (λ n hs x hx, hε (hs x hx)) end /-- Expressing locally uniform convergence using `dist`. -/ lemma tendsto_locally_uniformly_iff {ι : Type*} [topological_space β] {F : ι → β → α} {f : β → α} {p : filter ι} : tendsto_locally_uniformly F f p ↔ ∀ ε > 0, ∀ (x : β), ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, dist (f y) (F n y) < ε := by simp only [← tendsto_locally_uniformly_on_univ, tendsto_locally_uniformly_on_iff, nhds_within_univ, mem_univ, forall_const, exists_prop] /-- Expressing uniform convergence using `dist`. -/ lemma tendsto_uniformly_iff {ι : Type*} {F : ι → β → α} {f : β → α} {p : filter ι} : tendsto_uniformly F f p ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x, dist (f x) (F n x) < ε := by { rw [← tendsto_uniformly_on_univ, tendsto_uniformly_on_iff], simp } protected lemma cauchy_iff {f : filter α} : cauchy f ↔ ne_bot f ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, dist x y < ε := uniformity_basis_dist.cauchy_iff theorem nhds_basis_ball : (𝓝 x).has_basis (λ ε:ℝ, 0 < ε) (ball x) := nhds_basis_uniformity uniformity_basis_dist theorem mem_nhds_iff : s ∈ 𝓝 x ↔ ∃ε>0, ball x ε ⊆ s := nhds_basis_ball.mem_iff theorem eventually_nhds_iff {p : α → Prop} : (∀ᶠ y in 𝓝 x, p y) ↔ ∃ε>0, ∀ ⦃y⦄, dist y x < ε → p y := mem_nhds_iff lemma eventually_nhds_iff_ball {p : α → Prop} : (∀ᶠ y in 𝓝 x, p y) ↔ ∃ ε>0, ∀ y ∈ ball x ε, p y := mem_nhds_iff theorem nhds_basis_closed_ball : (𝓝 x).has_basis (λ ε:ℝ, 0 < ε) (closed_ball x) := nhds_basis_uniformity uniformity_basis_dist_le theorem nhds_basis_ball_inv_nat_succ : (𝓝 x).has_basis (λ _, true) (λ n:ℕ, ball x (1 / (↑n+1))) := nhds_basis_uniformity uniformity_basis_dist_inv_nat_succ theorem nhds_basis_ball_inv_nat_pos : (𝓝 x).has_basis (λ n, 0<n) (λ n:ℕ, ball x (1 / ↑n)) := nhds_basis_uniformity uniformity_basis_dist_inv_nat_pos theorem nhds_basis_ball_pow {r : ℝ} (h0 : 0 < r) (h1 : r < 1) : (𝓝 x).has_basis (λ n, true) (λ n:ℕ, ball x (r ^ n)) := nhds_basis_uniformity (uniformity_basis_dist_pow h0 h1) theorem nhds_basis_closed_ball_pow {r : ℝ} (h0 : 0 < r) (h1 : r < 1) : (𝓝 x).has_basis (λ n, true) (λ n:ℕ, closed_ball x (r ^ n)) := nhds_basis_uniformity (uniformity_basis_dist_le_pow h0 h1) theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s := by simp only [is_open_iff_mem_nhds, mem_nhds_iff] theorem is_open_ball : is_open (ball x ε) := is_open_iff.2 $ λ y, exists_ball_subset_ball theorem ball_mem_nhds (x : α) {ε : ℝ} (ε0 : 0 < ε) : ball x ε ∈ 𝓝 x := is_open_ball.mem_nhds (mem_ball_self ε0) theorem closed_ball_mem_nhds (x : α) {ε : ℝ} (ε0 : 0 < ε) : closed_ball x ε ∈ 𝓝 x := mem_of_superset (ball_mem_nhds x ε0) ball_subset_closed_ball theorem closed_ball_mem_nhds_of_mem {x c : α} {ε : ℝ} (h : x ∈ ball c ε) : closed_ball c ε ∈ 𝓝 x := mem_of_superset (is_open_ball.mem_nhds h) ball_subset_closed_ball theorem nhds_within_basis_ball {s : set α} : (𝓝[s] x).has_basis (λ ε:ℝ, 0 < ε) (λ ε, ball x ε ∩ s) := nhds_within_has_basis nhds_basis_ball s theorem mem_nhds_within_iff {t : set α} : s ∈ 𝓝[t] x ↔ ∃ε>0, ball x ε ∩ t ⊆ s := nhds_within_basis_ball.mem_iff theorem tendsto_nhds_within_nhds_within [pseudo_metric_space β] {t : set β} {f : α → β} {a b} : tendsto f (𝓝[s] a) (𝓝[t] b) ↔ ∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → f x ∈ t ∧ dist (f x) b < ε := (nhds_within_basis_ball.tendsto_iff nhds_within_basis_ball).trans $ forall₂_congr $ λ ε hε, exists₂_congr $ λ δ hδ, forall_congr $ λ x, by simp; itauto theorem tendsto_nhds_within_nhds [pseudo_metric_space β] {f : α → β} {a b} : tendsto f (𝓝[s] a) (𝓝 b) ↔ ∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → dist (f x) b < ε := by { rw [← nhds_within_univ b, tendsto_nhds_within_nhds_within], simp only [mem_univ, true_and] } theorem tendsto_nhds_nhds [pseudo_metric_space β] {f : α → β} {a b} : tendsto f (𝓝 a) (𝓝 b) ↔ ∀ ε > 0, ∃ δ > 0, ∀{x:α}, dist x a < δ → dist (f x) b < ε := nhds_basis_ball.tendsto_iff nhds_basis_ball theorem continuous_at_iff [pseudo_metric_space β] {f : α → β} {a : α} : continuous_at f a ↔ ∀ ε > 0, ∃ δ > 0, ∀{x:α}, dist x a < δ → dist (f x) (f a) < ε := by rw [continuous_at, tendsto_nhds_nhds] theorem continuous_within_at_iff [pseudo_metric_space β] {f : α → β} {a : α} {s : set α} : continuous_within_at f s a ↔ ∀ ε > 0, ∃ δ > 0, ∀{x:α}, x ∈ s → dist x a < δ → dist (f x) (f a) < ε := by rw [continuous_within_at, tendsto_nhds_within_nhds] theorem continuous_on_iff [pseudo_metric_space β] {f : α → β} {s : set α} : continuous_on f s ↔ ∀ (b ∈ s) (ε > 0), ∃ δ > 0, ∀a ∈ s, dist a b < δ → dist (f a) (f b) < ε := by simp [continuous_on, continuous_within_at_iff] theorem continuous_iff [pseudo_metric_space β] {f : α → β} : continuous f ↔ ∀b (ε > 0), ∃ δ > 0, ∀a, dist a b < δ → dist (f a) (f b) < ε := continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds_nhds theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} : tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, dist (u x) a < ε := nhds_basis_ball.tendsto_right_iff theorem continuous_at_iff' [topological_space β] {f : β → α} {b : β} : continuous_at f b ↔ ∀ ε > 0, ∀ᶠ x in 𝓝 b, dist (f x) (f b) < ε := by rw [continuous_at, tendsto_nhds] theorem continuous_within_at_iff' [topological_space β] {f : β → α} {b : β} {s : set β} : continuous_within_at f s b ↔ ∀ ε > 0, ∀ᶠ x in 𝓝[s] b, dist (f x) (f b) < ε := by rw [continuous_within_at, tendsto_nhds] theorem continuous_on_iff' [topological_space β] {f : β → α} {s : set β} : continuous_on f s ↔ ∀ (b ∈ s) (ε > 0), ∀ᶠ x in 𝓝[s] b, dist (f x) (f b) < ε := by simp [continuous_on, continuous_within_at_iff'] theorem continuous_iff' [topological_space β] {f : β → α} : continuous f ↔ ∀a (ε > 0), ∀ᶠ x in 𝓝 a, dist (f x) (f a) < ε := continuous_iff_continuous_at.trans $ forall_congr $ λ b, tendsto_nhds theorem tendsto_at_top [nonempty β] [semilattice_sup β] {u : β → α} {a : α} : tendsto u at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, dist (u n) a < ε := (at_top_basis.tendsto_iff nhds_basis_ball).trans $ by { simp only [exists_prop, true_and], refl } /-- A variant of `tendsto_at_top` that uses `∃ N, ∀ n > N, ...` rather than `∃ N, ∀ n ≥ N, ...` -/ theorem tendsto_at_top' [nonempty β] [semilattice_sup β] [no_max_order β] {u : β → α} {a : α} : tendsto u at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n>N, dist (u n) a < ε := (at_top_basis_Ioi.tendsto_iff nhds_basis_ball).trans $ by { simp only [exists_prop, true_and], refl } lemma is_open_singleton_iff {α : Type*} [pseudo_metric_space α] {x : α} : is_open ({x} : set α) ↔ ∃ ε > 0, ∀ y, dist y x < ε → y = x := by simp [is_open_iff, subset_singleton_iff, mem_ball] /-- Given a point `x` in a discrete subset `s` of a pseudometric space, there is an open ball centered at `x` and intersecting `s` only at `x`. -/ lemma exists_ball_inter_eq_singleton_of_mem_discrete [discrete_topology s] {x : α} (hx : x ∈ s) : ∃ ε > 0, metric.ball x ε ∩ s = {x} := nhds_basis_ball.exists_inter_eq_singleton_of_mem_discrete hx /-- Given a point `x` in a discrete subset `s` of a pseudometric space, there is a closed ball of positive radius centered at `x` and intersecting `s` only at `x`. -/ lemma exists_closed_ball_inter_eq_singleton_of_discrete [discrete_topology s] {x : α} (hx : x ∈ s) : ∃ ε > 0, metric.closed_ball x ε ∩ s = {x} := nhds_basis_closed_ball.exists_inter_eq_singleton_of_mem_discrete hx lemma _root_.dense.exists_dist_lt {s : set α} (hs : dense s) (x : α) {ε : ℝ} (hε : 0 < ε) : ∃ y ∈ s, dist x y < ε := begin have : (ball x ε).nonempty, by simp [hε], simpa only [mem_ball'] using hs.exists_mem_open is_open_ball this end lemma _root_.dense_range.exists_dist_lt {β : Type*} {f : β → α} (hf : dense_range f) (x : α) {ε : ℝ} (hε : 0 < ε) : ∃ y, dist x (f y) < ε := exists_range_iff.1 (hf.exists_dist_lt x hε) end metric open metric /-Instantiate a pseudometric space as a pseudoemetric space. Before we can state the instance, we need to show that the uniform structure coming from the edistance and the distance coincide. -/ /-- Expressing the uniformity in terms of `edist` -/ protected lemma pseudo_metric.uniformity_basis_edist : (𝓤 α).has_basis (λ ε:ℝ≥0∞, 0 < ε) (λ ε, {p | edist p.1 p.2 < ε}) := ⟨begin intro t, refine mem_uniformity_dist.trans ⟨_, _⟩; rintro ⟨ε, ε0, Hε⟩, { use [ennreal.of_real ε, ennreal.of_real_pos.2 ε0], rintros ⟨a, b⟩, simp only [edist_dist, ennreal.of_real_lt_of_real_iff ε0], exact Hε }, { rcases ennreal.lt_iff_exists_real_btwn.1 ε0 with ⟨ε', _, ε0', hε⟩, rw [ennreal.of_real_pos] at ε0', refine ⟨ε', ε0', λ a b h, Hε (lt_trans _ hε)⟩, rwa [edist_dist, ennreal.of_real_lt_of_real_iff ε0'] } end⟩ theorem metric.uniformity_edist : 𝓤 α = (⨅ ε>0, 𝓟 {p:α×α | edist p.1 p.2 < ε}) := pseudo_metric.uniformity_basis_edist.eq_binfi /-- A pseudometric space induces a pseudoemetric space -/ @[priority 100] -- see Note [lower instance priority] instance pseudo_metric_space.to_pseudo_emetric_space : pseudo_emetric_space α := { edist := edist, edist_self := by simp [edist_dist], edist_comm := by simp only [edist_dist, dist_comm]; simp, edist_triangle := assume x y z, begin simp only [edist_dist, ← ennreal.of_real_add, dist_nonneg], rw ennreal.of_real_le_of_real_iff _, { exact dist_triangle _ _ _ }, { simpa using add_le_add (dist_nonneg : 0 ≤ dist x y) dist_nonneg } end, uniformity_edist := metric.uniformity_edist, ..‹pseudo_metric_space α› } /-- In a pseudometric space, an open ball of infinite radius is the whole space -/ lemma metric.eball_top_eq_univ (x : α) : emetric.ball x ∞ = set.univ := set.eq_univ_iff_forall.mpr (λ y, edist_lt_top y x) /-- Balls defined using the distance or the edistance coincide -/ @[simp] lemma metric.emetric_ball {x : α} {ε : ℝ} : emetric.ball x (ennreal.of_real ε) = ball x ε := begin ext y, simp only [emetric.mem_ball, mem_ball, edist_dist], exact ennreal.of_real_lt_of_real_iff_of_nonneg dist_nonneg end /-- Balls defined using the distance or the edistance coincide -/ @[simp] lemma metric.emetric_ball_nnreal {x : α} {ε : ℝ≥0} : emetric.ball x ε = ball x ε := by { convert metric.emetric_ball, simp } /-- Closed balls defined using the distance or the edistance coincide -/ lemma metric.emetric_closed_ball {x : α} {ε : ℝ} (h : 0 ≤ ε) : emetric.closed_ball x (ennreal.of_real ε) = closed_ball x ε := by ext y; simp [edist_dist]; rw ennreal.of_real_le_of_real_iff h /-- Closed balls defined using the distance or the edistance coincide -/ @[simp] lemma metric.emetric_closed_ball_nnreal {x : α} {ε : ℝ≥0} : emetric.closed_ball x ε = closed_ball x ε := by { convert metric.emetric_closed_ball ε.2, simp } @[simp] lemma metric.emetric_ball_top (x : α) : emetric.ball x ⊤ = univ := eq_univ_of_forall $ λ y, edist_lt_top _ _ lemma metric.inseparable_iff {x y : α} : inseparable x y ↔ dist x y = 0 := by rw [emetric.inseparable_iff, edist_nndist, dist_nndist, ennreal.coe_eq_zero, nnreal.coe_eq_zero] /-- Build a new pseudometric space from an old one where the bundled uniform structure is provably (but typically non-definitionaly) equal to some given uniform structure. See Note [forgetful inheritance]. -/ def pseudo_metric_space.replace_uniformity {α} [U : uniform_space α] (m : pseudo_metric_space α) (H : @uniformity _ U = @uniformity _ pseudo_emetric_space.to_uniform_space) : pseudo_metric_space α := { dist := @dist _ m.to_has_dist, dist_self := dist_self, dist_comm := dist_comm, dist_triangle := dist_triangle, edist := edist, edist_dist := edist_dist, to_uniform_space := U, uniformity_dist := H.trans pseudo_metric_space.uniformity_dist } lemma pseudo_metric_space.replace_uniformity_eq {α} [U : uniform_space α] (m : pseudo_metric_space α) (H : @uniformity _ U = @uniformity _ pseudo_emetric_space.to_uniform_space) : m.replace_uniformity H = m := by { ext, refl } /-- Build a new pseudo metric space from an old one where the bundled topological structure is provably (but typically non-definitionaly) equal to some given topological structure. See Note [forgetful inheritance]. -/ @[reducible] def pseudo_metric_space.replace_topology {γ} [U : topological_space γ] (m : pseudo_metric_space γ) (H : U = m.to_uniform_space.to_topological_space) : pseudo_metric_space γ := @pseudo_metric_space.replace_uniformity γ (m.to_uniform_space.replace_topology H) m rfl lemma pseudo_metric_space.replace_topology_eq {γ} [U : topological_space γ] (m : pseudo_metric_space γ) (H : U = m.to_uniform_space.to_topological_space) : m.replace_topology H = m := by { ext, refl } /-- One gets a pseudometric space from an emetric space if the edistance is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the uniformity are defeq in the pseudometric space and the pseudoemetric space. In this definition, the distance is given separately, to be able to prescribe some expression which is not defeq to the push-forward of the edistance to reals. -/ def pseudo_emetric_space.to_pseudo_metric_space_of_dist {α : Type u} [e : pseudo_emetric_space α] (dist : α → α → ℝ) (edist_ne_top : ∀x y: α, edist x y ≠ ⊤) (h : ∀x y, dist x y = ennreal.to_real (edist x y)) : pseudo_metric_space α := let m : pseudo_metric_space α := { dist := dist, dist_self := λx, by simp [h], dist_comm := λx y, by simp [h, pseudo_emetric_space.edist_comm], dist_triangle := λx y z, begin simp only [h], rw [← ennreal.to_real_add (edist_ne_top _ _) (edist_ne_top _ _), ennreal.to_real_le_to_real (edist_ne_top _ _)], { exact edist_triangle _ _ _ }, { simp [ennreal.add_eq_top, edist_ne_top] } end, edist := edist, edist_dist := λ x y, by simp [h, ennreal.of_real_to_real, edist_ne_top] } in m.replace_uniformity $ by { rw [uniformity_pseudoedist, metric.uniformity_edist], refl } /-- One gets a pseudometric space from an emetric space if the edistance is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the uniformity are defeq in the pseudometric space and the emetric space. -/ def pseudo_emetric_space.to_pseudo_metric_space {α : Type u} [e : pseudo_emetric_space α] (h : ∀x y: α, edist x y ≠ ⊤) : pseudo_metric_space α := pseudo_emetric_space.to_pseudo_metric_space_of_dist (λx y, ennreal.to_real (edist x y)) h (λx y, rfl) /-- Build a new pseudometric space from an old one where the bundled bornology structure is provably (but typically non-definitionaly) equal to some given bornology structure. See Note [forgetful inheritance]. -/ def pseudo_metric_space.replace_bornology {α} [B : bornology α] (m : pseudo_metric_space α) (H : ∀ s, @is_bounded _ B s ↔ @is_bounded _ pseudo_metric_space.to_bornology s) : pseudo_metric_space α := { to_bornology := B, cobounded_sets := set.ext $ compl_surjective.forall.2 $ λ s, (H s).trans $ by rw [is_bounded_iff, mem_set_of_eq, compl_compl], .. m } lemma pseudo_metric_space.replace_bornology_eq {α} [m : pseudo_metric_space α] [B : bornology α] (H : ∀ s, @is_bounded _ B s ↔ @is_bounded _ pseudo_metric_space.to_bornology s) : pseudo_metric_space.replace_bornology _ H = m := by { ext, refl } /-- A very useful criterion to show that a space is complete is to show that all sequences which satisfy a bound of the form `dist (u n) (u m) < B N` for all `n m ≥ N` are converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to `0`, which makes it possible to use arguments of converging series, while this is impossible to do in general for arbitrary Cauchy sequences. -/ theorem metric.complete_of_convergent_controlled_sequences (B : ℕ → real) (hB : ∀n, 0 < B n) (H : ∀u : ℕ → α, (∀N n m : ℕ, N ≤ n → N ≤ m → dist (u n) (u m) < B N) → ∃x, tendsto u at_top (𝓝 x)) : complete_space α := uniform_space.complete_of_convergent_controlled_sequences (λ n, {p:α×α | dist p.1 p.2 < B n}) (λ n, dist_mem_uniformity $ hB n) H theorem metric.complete_of_cauchy_seq_tendsto : (∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) → complete_space α := emetric.complete_of_cauchy_seq_tendsto section real /-- Instantiate the reals as a pseudometric space. -/ noncomputable instance real.pseudo_metric_space : pseudo_metric_space ℝ := { dist := λx y, |x - y|, dist_self := by simp [abs_zero], dist_comm := assume x y, abs_sub_comm _ _, dist_triangle := assume x y z, abs_sub_le _ _ _ } theorem real.dist_eq (x y : ℝ) : dist x y = |x - y| := rfl theorem real.nndist_eq (x y : ℝ) : nndist x y = real.nnabs (x - y) := rfl theorem real.nndist_eq' (x y : ℝ) : nndist x y = real.nnabs (y - x) := nndist_comm _ _ theorem real.dist_0_eq_abs (x : ℝ) : dist x 0 = |x| := by simp [real.dist_eq] theorem real.dist_left_le_of_mem_interval {x y z : ℝ} (h : y ∈ interval x z) : dist x y ≤ dist x z := by simpa only [dist_comm x] using abs_sub_left_of_mem_interval h theorem real.dist_right_le_of_mem_interval {x y z : ℝ} (h : y ∈ interval x z) : dist y z ≤ dist x z := by simpa only [dist_comm _ z] using abs_sub_right_of_mem_interval h theorem real.dist_le_of_mem_interval {x y x' y' : ℝ} (hx : x ∈ interval x' y') (hy : y ∈ interval x' y') : dist x y ≤ dist x' y' := abs_sub_le_of_subinterval $ interval_subset_interval (by rwa interval_swap) (by rwa interval_swap) theorem real.dist_le_of_mem_Icc {x y x' y' : ℝ} (hx : x ∈ Icc x' y') (hy : y ∈ Icc x' y') : dist x y ≤ y' - x' := by simpa only [real.dist_eq, abs_of_nonpos (sub_nonpos.2 $ hx.1.trans hx.2), neg_sub] using real.dist_le_of_mem_interval (Icc_subset_interval hx) (Icc_subset_interval hy) theorem real.dist_le_of_mem_Icc_01 {x y : ℝ} (hx : x ∈ Icc (0:ℝ) 1) (hy : y ∈ Icc (0:ℝ) 1) : dist x y ≤ 1 := by simpa only [sub_zero] using real.dist_le_of_mem_Icc hx hy instance : order_topology ℝ := order_topology_of_nhds_abs $ λ x, by simp only [nhds_basis_ball.eq_binfi, ball, real.dist_eq, abs_sub_comm] lemma real.ball_eq_Ioo (x r : ℝ) : ball x r = Ioo (x - r) (x + r) := set.ext $ λ y, by rw [mem_ball, dist_comm, real.dist_eq, abs_sub_lt_iff, mem_Ioo, ← sub_lt_iff_lt_add', sub_lt] lemma real.closed_ball_eq_Icc {x r : ℝ} : closed_ball x r = Icc (x - r) (x + r) := by ext y; rw [mem_closed_ball, dist_comm, real.dist_eq, abs_sub_le_iff, mem_Icc, ← sub_le_iff_le_add', sub_le] theorem real.Ioo_eq_ball (x y : ℝ) : Ioo x y = ball ((x + y) / 2) ((y - x) / 2) := by rw [real.ball_eq_Ioo, ← sub_div, add_comm, ← sub_add, add_sub_cancel', add_self_div_two, ← add_div, add_assoc, add_sub_cancel'_right, add_self_div_two] theorem real.Icc_eq_closed_ball (x y : ℝ) : Icc x y = closed_ball ((x + y) / 2) ((y - x) / 2) := by rw [real.closed_ball_eq_Icc, ← sub_div, add_comm, ← sub_add, add_sub_cancel', add_self_div_two, ← add_div, add_assoc, add_sub_cancel'_right, add_self_div_two] section metric_ordered variables [preorder α] [compact_Icc_space α] lemma totally_bounded_Icc (a b : α) : totally_bounded (Icc a b) := is_compact_Icc.totally_bounded lemma totally_bounded_Ico (a b : α) : totally_bounded (Ico a b) := totally_bounded_subset Ico_subset_Icc_self (totally_bounded_Icc a b) lemma totally_bounded_Ioc (a b : α) : totally_bounded (Ioc a b) := totally_bounded_subset Ioc_subset_Icc_self (totally_bounded_Icc a b) lemma totally_bounded_Ioo (a b : α) : totally_bounded (Ioo a b) := totally_bounded_subset Ioo_subset_Icc_self (totally_bounded_Icc a b) end metric_ordered /-- Special case of the sandwich theorem; see `tendsto_of_tendsto_of_tendsto_of_le_of_le'` for the general case. -/ lemma squeeze_zero' {α} {f g : α → ℝ} {t₀ : filter α} (hf : ∀ᶠ t in t₀, 0 ≤ f t) (hft : ∀ᶠ t in t₀, f t ≤ g t) (g0 : tendsto g t₀ (nhds 0)) : tendsto f t₀ (𝓝 0) := tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds g0 hf hft /-- Special case of the sandwich theorem; see `tendsto_of_tendsto_of_tendsto_of_le_of_le` and `tendsto_of_tendsto_of_tendsto_of_le_of_le'` for the general case. -/ lemma squeeze_zero {α} {f g : α → ℝ} {t₀ : filter α} (hf : ∀t, 0 ≤ f t) (hft : ∀t, f t ≤ g t) (g0 : tendsto g t₀ (𝓝 0)) : tendsto f t₀ (𝓝 0) := squeeze_zero' (eventually_of_forall hf) (eventually_of_forall hft) g0 theorem metric.uniformity_eq_comap_nhds_zero : 𝓤 α = comap (λp:α×α, dist p.1 p.2) (𝓝 (0 : ℝ)) := by { ext s, simp [mem_uniformity_dist, (nhds_basis_ball.comap _).mem_iff, subset_def, real.dist_0_eq_abs] } lemma cauchy_seq_iff_tendsto_dist_at_top_0 [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ tendsto (λ (n : β × β), dist (u n.1) (u n.2)) at_top (𝓝 0) := by rw [cauchy_seq_iff_tendsto, metric.uniformity_eq_comap_nhds_zero, tendsto_comap_iff, prod.map_def] lemma tendsto_uniformity_iff_dist_tendsto_zero {ι : Type*} {f : ι → α × α} {p : filter ι} : tendsto f p (𝓤 α) ↔ tendsto (λ x, dist (f x).1 (f x).2) p (𝓝 0) := by rw [metric.uniformity_eq_comap_nhds_zero, tendsto_comap_iff] lemma filter.tendsto.congr_dist {ι : Type*} {f₁ f₂ : ι → α} {p : filter ι} {a : α} (h₁ : tendsto f₁ p (𝓝 a)) (h : tendsto (λ x, dist (f₁ x) (f₂ x)) p (𝓝 0)) : tendsto f₂ p (𝓝 a) := h₁.congr_uniformity $ tendsto_uniformity_iff_dist_tendsto_zero.2 h alias filter.tendsto.congr_dist ← tendsto_of_tendsto_of_dist lemma tendsto_iff_of_dist {ι : Type*} {f₁ f₂ : ι → α} {p : filter ι} {a : α} (h : tendsto (λ x, dist (f₁ x) (f₂ x)) p (𝓝 0)) : tendsto f₁ p (𝓝 a) ↔ tendsto f₂ p (𝓝 a) := uniform.tendsto_congr $ tendsto_uniformity_iff_dist_tendsto_zero.2 h /-- If `u` is a neighborhood of `x`, then for small enough `r`, the closed ball `closed_ball x r` is contained in `u`. -/ lemma eventually_closed_ball_subset {x : α} {u : set α} (hu : u ∈ 𝓝 x) : ∀ᶠ r in 𝓝 (0 : ℝ), closed_ball x r ⊆ u := begin obtain ⟨ε, εpos, hε⟩ : ∃ ε (hε : 0 < ε), closed_ball x ε ⊆ u := nhds_basis_closed_ball.mem_iff.1 hu, have : Iic ε ∈ 𝓝 (0 : ℝ) := Iic_mem_nhds εpos, filter_upwards [this] with _ hr using subset.trans (closed_ball_subset_closed_ball hr) hε, end end real section cauchy_seq variables [nonempty β] [semilattice_sup β] /-- In a pseudometric space, Cauchy sequences are characterized by the fact that, eventually, the distance between its elements is arbitrarily small -/ @[nolint ge_or_gt] -- see Note [nolint_ge] theorem metric.cauchy_seq_iff {u : β → α} : cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, dist (u m) (u n) < ε := uniformity_basis_dist.cauchy_seq_iff /-- A variation around the pseudometric characterization of Cauchy sequences -/ theorem metric.cauchy_seq_iff' {u : β → α} : cauchy_seq u ↔ ∀ε>0, ∃N, ∀n≥N, dist (u n) (u N) < ε := uniformity_basis_dist.cauchy_seq_iff' /-- In a pseudometric space, unifom Cauchy sequences are characterized by the fact that, eventually, the distance between all its elements is uniformly, arbitrarily small -/ @[nolint ge_or_gt] -- see Note [nolint_ge] theorem metric.uniform_cauchy_seq_on_iff {γ : Type*} {F : β → γ → α} {s : set γ} : uniform_cauchy_seq_on F at_top s ↔ ∀ ε : ℝ, ε > 0 → ∃ (N : β), ∀ m : β, m ≥ N → ∀ n : β, n ≥ N → ∀ x : γ, x ∈ s → dist (F m x) (F n x) < ε := begin split, { intros h ε hε, let u := { a : α × α | dist a.fst a.snd < ε }, have hu : u ∈ 𝓤 α := metric.mem_uniformity_dist.mpr ⟨ε, hε, (λ a b, by simp)⟩, rw ←@filter.eventually_at_top_prod_self' _ _ _ (λ m, ∀ x : γ, x ∈ s → dist (F m.fst x) (F m.snd x) < ε), specialize h u hu, rw prod_at_top_at_top_eq at h, exact h.mono (λ n h x hx, set.mem_set_of_eq.mp (h x hx)), }, { intros h u hu, rcases (metric.mem_uniformity_dist.mp hu) with ⟨ε, hε, hab⟩, rcases h ε hε with ⟨N, hN⟩, rw [prod_at_top_at_top_eq, eventually_at_top], use (N, N), intros b hb x hx, rcases hb with ⟨hbl, hbr⟩, exact hab (hN b.fst hbl.ge b.snd hbr.ge x hx), }, end /-- If the distance between `s n` and `s m`, `n ≤ m` is bounded above by `b n` and `b` converges to zero, then `s` is a Cauchy sequence. -/ lemma cauchy_seq_of_le_tendsto_0' {s : β → α} (b : β → ℝ) (h : ∀ n m : β, n ≤ m → dist (s n) (s m) ≤ b n) (h₀ : tendsto b at_top (𝓝 0)) : cauchy_seq s := metric.cauchy_seq_iff'.2 $ λ ε ε0, (h₀.eventually (gt_mem_nhds ε0)).exists.imp $ λ N hN n hn, calc dist (s n) (s N) = dist (s N) (s n) : dist_comm _ _ ... ≤ b N : h _ _ hn ... < ε : hN /-- If the distance between `s n` and `s m`, `n, m ≥ N` is bounded above by `b N` and `b` converges to zero, then `s` is a Cauchy sequence. -/ lemma cauchy_seq_of_le_tendsto_0 {s : β → α} (b : β → ℝ) (h : ∀ n m N : β, N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) (h₀ : tendsto b at_top (𝓝 0)) : cauchy_seq s := cauchy_seq_of_le_tendsto_0' b (λ n m hnm, h _ _ _ le_rfl hnm) h₀ /-- A Cauchy sequence on the natural numbers is bounded. -/ theorem cauchy_seq_bdd {u : ℕ → α} (hu : cauchy_seq u) : ∃ R > 0, ∀ m n, dist (u m) (u n) < R := begin rcases metric.cauchy_seq_iff'.1 hu 1 zero_lt_one with ⟨N, hN⟩, suffices : ∃ R > 0, ∀ n, dist (u n) (u N) < R, { rcases this with ⟨R, R0, H⟩, exact ⟨_, add_pos R0 R0, λ m n, lt_of_le_of_lt (dist_triangle_right _ _ _) (add_lt_add (H m) (H n))⟩ }, let R := finset.sup (finset.range N) (λ n, nndist (u n) (u N)), refine ⟨↑R + 1, add_pos_of_nonneg_of_pos R.2 zero_lt_one, λ n, _⟩, cases le_or_lt N n, { exact lt_of_lt_of_le (hN _ h) (le_add_of_nonneg_left R.2) }, { have : _ ≤ R := finset.le_sup (finset.mem_range.2 h), exact lt_of_le_of_lt this (lt_add_of_pos_right _ zero_lt_one) } end /-- Yet another metric characterization of Cauchy sequences on integers. This one is often the most efficient. -/ lemma cauchy_seq_iff_le_tendsto_0 {s : ℕ → α} : cauchy_seq s ↔ ∃ b : ℕ → ℝ, (∀ n, 0 ≤ b n) ∧ (∀ n m N : ℕ, N ≤ n → N ≤ m → dist (s n) (s m) ≤ b N) ∧ tendsto b at_top (𝓝 0) := ⟨λ hs, begin /- `s` is a Cauchy sequence. The sequence `b` will be constructed by taking the supremum of the distances between `s n` and `s m` for `n m ≥ N`. First, we prove that all these distances are bounded, as otherwise the Sup would not make sense. -/ let S := λ N, (λ(p : ℕ × ℕ), dist (s p.1) (s p.2)) '' {p | p.1 ≥ N ∧ p.2 ≥ N}, have hS : ∀ N, ∃ x, ∀ y ∈ S N, y ≤ x, { rcases cauchy_seq_bdd hs with ⟨R, R0, hR⟩, refine λ N, ⟨R, _⟩, rintro _ ⟨⟨m, n⟩, _, rfl⟩, exact le_of_lt (hR m n) }, have bdd : bdd_above (range (λ(p : ℕ × ℕ), dist (s p.1) (s p.2))), { rcases cauchy_seq_bdd hs with ⟨R, R0, hR⟩, use R, rintro _ ⟨⟨m, n⟩, rfl⟩, exact le_of_lt (hR m n) }, -- Prove that it bounds the distances of points in the Cauchy sequence have ub : ∀ m n N, N ≤ m → N ≤ n → dist (s m) (s n) ≤ Sup (S N) := λ m n N hm hn, le_cSup (hS N) ⟨⟨_, _⟩, ⟨hm, hn⟩, rfl⟩, have S0m : ∀ n, (0:ℝ) ∈ S n := λ n, ⟨⟨n, n⟩, ⟨le_rfl, le_rfl⟩, dist_self _⟩, have S0 := λ n, le_cSup (hS n) (S0m n), -- Prove that it tends to `0`, by using the Cauchy property of `s` refine ⟨λ N, Sup (S N), S0, ub, metric.tendsto_at_top.2 (λ ε ε0, _)⟩, refine (metric.cauchy_seq_iff.1 hs (ε/2) (half_pos ε0)).imp (λ N hN n hn, _), rw [real.dist_0_eq_abs, abs_of_nonneg (S0 n)], refine lt_of_le_of_lt (cSup_le ⟨_, S0m _⟩ _) (half_lt_self ε0), rintro _ ⟨⟨m', n'⟩, ⟨hm', hn'⟩, rfl⟩, exact le_of_lt (hN _ (le_trans hn hm') _ (le_trans hn hn')) end, λ ⟨b, _, b_bound, b_lim⟩, cauchy_seq_of_le_tendsto_0 b b_bound b_lim⟩ end cauchy_seq /-- Pseudometric space structure pulled back by a function. -/ def pseudo_metric_space.induced {α β} (f : α → β) (m : pseudo_metric_space β) : pseudo_metric_space α := { dist := λ x y, dist (f x) (f y), dist_self := λ x, dist_self _, dist_comm := λ x y, dist_comm _ _, dist_triangle := λ x y z, dist_triangle _ _ _, edist := λ x y, edist (f x) (f y), edist_dist := λ x y, edist_dist _ _, to_uniform_space := uniform_space.comap f m.to_uniform_space, uniformity_dist := begin apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, dist (f x) (f y)), refine compl_surjective.forall.2 (λ s, compl_mem_comap.trans $ mem_uniformity_dist.trans _), simp only [mem_compl_iff, @imp_not_comm _ (_ ∈ _), ← prod.forall', prod.mk.eta, ball_image_iff] end, to_bornology := bornology.induced f, cobounded_sets := set.ext $ compl_surjective.forall.2 $ λ s, by simp only [compl_mem_comap, filter.mem_sets, ← is_bounded_def, mem_set_of_eq, compl_compl, is_bounded_iff, ball_image_iff] } /-- Pull back a pseudometric space structure by an inducing map. This is a version of `pseudo_metric_space.induced` useful in case if the domain already has a `topological_space` structure. -/ def inducing.comap_pseudo_metric_space {α β} [topological_space α] [pseudo_metric_space β] {f : α → β} (hf : inducing f) : pseudo_metric_space α := (pseudo_metric_space.induced f ‹_›).replace_topology hf.induced /-- Pull back a pseudometric space structure by a uniform inducing map. This is a version of `pseudo_metric_space.induced` useful in case if the domain already has a `uniform_space` structure. -/ def uniform_inducing.comap_pseudo_metric_space {α β} [uniform_space α] [pseudo_metric_space β] (f : α → β) (h : uniform_inducing f) : pseudo_metric_space α := (pseudo_metric_space.induced f ‹_›).replace_uniformity h.comap_uniformity.symm instance subtype.pseudo_metric_space {p : α → Prop} : pseudo_metric_space (subtype p) := pseudo_metric_space.induced coe ‹_› theorem subtype.dist_eq {p : α → Prop} (x y : subtype p) : dist x y = dist (x : α) y := rfl theorem subtype.nndist_eq {p : α → Prop} (x y : subtype p) : nndist x y = nndist (x : α) y := rfl namespace mul_opposite @[to_additive] instance : pseudo_metric_space (αᵐᵒᵖ) := pseudo_metric_space.induced mul_opposite.unop ‹_› @[simp, to_additive] theorem dist_unop (x y : αᵐᵒᵖ) : dist (unop x) (unop y) = dist x y := rfl @[simp, to_additive] theorem dist_op (x y : α) : dist (op x) (op y) = dist x y := rfl @[simp, to_additive] theorem nndist_unop (x y : αᵐᵒᵖ) : nndist (unop x) (unop y) = nndist x y := rfl @[simp, to_additive] theorem nndist_op (x y : α) : nndist (op x) (op y) = nndist x y := rfl end mul_opposite section nnreal noncomputable instance : pseudo_metric_space ℝ≥0 := subtype.pseudo_metric_space lemma nnreal.dist_eq (a b : ℝ≥0) : dist a b = |(a:ℝ) - b| := rfl lemma nnreal.nndist_eq (a b : ℝ≥0) : nndist a b = max (a - b) (b - a) := begin /- WLOG, `b ≤ a`. `wlog h : b ≤ a` works too but it is much slower because Lean tries to prove one case from the other and fails; `tactic.skip` tells Lean not to try. -/ wlog h : b ≤ a := le_total b a using [a b, b a] tactic.skip, { rw [← nnreal.coe_eq, ← dist_nndist, nnreal.dist_eq, tsub_eq_zero_iff_le.2 h, max_eq_left (zero_le $ a - b), ← nnreal.coe_sub h, abs_of_nonneg (a - b).coe_nonneg] }, { rwa [nndist_comm, max_comm] } end @[simp] lemma nnreal.nndist_zero_eq_val (z : ℝ≥0) : nndist 0 z = z := by simp only [nnreal.nndist_eq, max_eq_right, tsub_zero, zero_tsub, zero_le'] @[simp] lemma nnreal.nndist_zero_eq_val' (z : ℝ≥0) : nndist z 0 = z := by { rw nndist_comm, exact nnreal.nndist_zero_eq_val z, } lemma nnreal.le_add_nndist (a b : ℝ≥0) : a ≤ b + nndist a b := begin suffices : (a : ℝ) ≤ (b : ℝ) + (dist a b), { exact nnreal.coe_le_coe.mp this, }, linarith [le_of_abs_le (by refl : abs (a-b : ℝ) ≤ (dist a b))], end end nnreal section ulift variables [pseudo_metric_space β] instance : pseudo_metric_space (ulift β) := pseudo_metric_space.induced ulift.down ‹_› lemma ulift.dist_eq (x y : ulift β) : dist x y = dist x.down y.down := rfl lemma ulift.nndist_eq (x y : ulift β) : nndist x y = nndist x.down y.down := rfl @[simp] lemma ulift.dist_up_up (x y : β) : dist (ulift.up x) (ulift.up y) = dist x y := rfl @[simp] lemma ulift.nndist_up_up (x y : β) : nndist (ulift.up x) (ulift.up y) = nndist x y := rfl end ulift section prod variables [pseudo_metric_space β] noncomputable instance prod.pseudo_metric_space_max : pseudo_metric_space (α × β) := (pseudo_emetric_space.to_pseudo_metric_space_of_dist (λ x y : α × β, max (dist x.1 y.1) (dist x.2 y.2)) (λ x y, (max_lt (edist_lt_top _ _) (edist_lt_top _ _)).ne) (λ x y, by simp only [dist_edist, ← ennreal.to_real_max (edist_ne_top _ _) (edist_ne_top _ _), prod.edist_eq])).replace_bornology $ λ s, by { simp only [← is_bounded_image_fst_and_snd, is_bounded_iff_eventually, ball_image_iff, ← eventually_and, ← forall_and_distrib, ← max_le_iff], refl } lemma prod.dist_eq {x y : α × β} : dist x y = max (dist x.1 y.1) (dist x.2 y.2) := rfl @[simp] lemma dist_prod_same_left {x : α} {y₁ y₂ : β} : dist (x, y₁) (x, y₂) = dist y₁ y₂ := by simp [prod.dist_eq, dist_nonneg] @[simp] lemma dist_prod_same_right {x₁ x₂ : α} {y : β} : dist (x₁, y) (x₂, y) = dist x₁ x₂ := by simp [prod.dist_eq, dist_nonneg] theorem ball_prod_same (x : α) (y : β) (r : ℝ) : ball x r ×ˢ ball y r = ball (x, y) r := ext $ λ z, by simp [prod.dist_eq] theorem closed_ball_prod_same (x : α) (y : β) (r : ℝ) : closed_ball x r ×ˢ closed_ball y r = closed_ball (x, y) r := ext $ λ z, by simp [prod.dist_eq] end prod theorem uniform_continuous_dist : uniform_continuous (λp:α×α, dist p.1 p.2) := metric.uniform_continuous_iff.2 (λ ε ε0, ⟨ε/2, half_pos ε0, begin suffices, { intros p q h, cases p with p₁ p₂, cases q with q₁ q₂, cases max_lt_iff.1 h with h₁ h₂, clear h, dsimp at h₁ h₂ ⊢, rw real.dist_eq, refine abs_sub_lt_iff.2 ⟨_, _⟩, { revert p₁ p₂ q₁ q₂ h₁ h₂, exact this }, { apply this; rwa dist_comm } }, intros p₁ p₂ q₁ q₂ h₁ h₂, have := add_lt_add (abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₁ q₁ p₂) h₁)).1 (abs_sub_lt_iff.1 (lt_of_le_of_lt (abs_dist_sub_le p₂ q₂ q₁) h₂)).1, rwa [add_halves, dist_comm p₂, sub_add_sub_cancel, dist_comm q₂] at this end⟩) theorem uniform_continuous.dist [uniform_space β] {f g : β → α} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (λb, dist (f b) (g b)) := uniform_continuous_dist.comp (hf.prod_mk hg) @[continuity] theorem continuous_dist : continuous (λp:α×α, dist p.1 p.2) := uniform_continuous_dist.continuous @[continuity] theorem continuous.dist [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λb, dist (f b) (g b)) := continuous_dist.comp (hf.prod_mk hg : _) theorem filter.tendsto.dist {f g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) : tendsto (λx, dist (f x) (g x)) x (𝓝 (dist a b)) := (continuous_dist.tendsto (a, b)).comp (hf.prod_mk_nhds hg) lemma nhds_comap_dist (a : α) : (𝓝 (0 : ℝ)).comap (λa', dist a' a) = 𝓝 a := by simp only [@nhds_eq_comap_uniformity α, metric.uniformity_eq_comap_nhds_zero, comap_comap, (∘), dist_comm] lemma tendsto_iff_dist_tendsto_zero {f : β → α} {x : filter β} {a : α} : (tendsto f x (𝓝 a)) ↔ (tendsto (λb, dist (f b) a) x (𝓝 0)) := by rw [← nhds_comap_dist a, tendsto_comap_iff] lemma uniform_continuous_nndist : uniform_continuous (λp:α×α, nndist p.1 p.2) := uniform_continuous_subtype_mk uniform_continuous_dist _ lemma uniform_continuous.nndist [uniform_space β] {f g : β → α} (hf : uniform_continuous f) (hg : uniform_continuous g) : uniform_continuous (λ b, nndist (f b) (g b)) := uniform_continuous_nndist.comp (hf.prod_mk hg) lemma continuous_nndist : continuous (λp:α×α, nndist p.1 p.2) := uniform_continuous_nndist.continuous lemma continuous.nndist [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λb, nndist (f b) (g b)) := continuous_nndist.comp (hf.prod_mk hg : _) theorem filter.tendsto.nndist {f g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) : tendsto (λx, nndist (f x) (g x)) x (𝓝 (nndist a b)) := (continuous_nndist.tendsto (a, b)).comp (hf.prod_mk_nhds hg) namespace metric variables {x y z : α} {ε ε₁ ε₂ : ℝ} {s : set α} theorem is_closed_ball : is_closed (closed_ball x ε) := is_closed_le (continuous_id.dist continuous_const) continuous_const lemma is_closed_sphere : is_closed (sphere x ε) := is_closed_eq (continuous_id.dist continuous_const) continuous_const @[simp] theorem closure_closed_ball : closure (closed_ball x ε) = closed_ball x ε := is_closed_ball.closure_eq theorem closure_ball_subset_closed_ball : closure (ball x ε) ⊆ closed_ball x ε := closure_minimal ball_subset_closed_ball is_closed_ball theorem frontier_ball_subset_sphere : frontier (ball x ε) ⊆ sphere x ε := frontier_lt_subset_eq (continuous_id.dist continuous_const) continuous_const theorem frontier_closed_ball_subset_sphere : frontier (closed_ball x ε) ⊆ sphere x ε := frontier_le_subset_eq (continuous_id.dist continuous_const) continuous_const theorem ball_subset_interior_closed_ball : ball x ε ⊆ interior (closed_ball x ε) := interior_maximal ball_subset_closed_ball is_open_ball /-- ε-characterization of the closure in pseudometric spaces-/ theorem mem_closure_iff {s : set α} {a : α} : a ∈ closure s ↔ ∀ε>0, ∃b ∈ s, dist a b < ε := (mem_closure_iff_nhds_basis nhds_basis_ball).trans $ by simp only [mem_ball, dist_comm] lemma mem_closure_range_iff {e : β → α} {a : α} : a ∈ closure (range e) ↔ ∀ε>0, ∃ k : β, dist a (e k) < ε := by simp only [mem_closure_iff, exists_range_iff] lemma mem_closure_range_iff_nat {e : β → α} {a : α} : a ∈ closure (range e) ↔ ∀n : ℕ, ∃ k : β, dist a (e k) < 1 / ((n : ℝ) + 1) := (mem_closure_iff_nhds_basis nhds_basis_ball_inv_nat_succ).trans $ by simp only [mem_ball, dist_comm, exists_range_iff, forall_const] theorem mem_of_closed' {s : set α} (hs : is_closed s) {a : α} : a ∈ s ↔ ∀ε>0, ∃b ∈ s, dist a b < ε := by simpa only [hs.closure_eq] using @mem_closure_iff _ _ s a lemma closed_ball_zero' (x : α) : closed_ball x 0 = closure {x} := subset.antisymm (λ y hy, mem_closure_iff.2 $ λ ε ε0, ⟨x, mem_singleton x, (mem_closed_ball.1 hy).trans_lt ε0⟩) (closure_minimal (singleton_subset_iff.2 (dist_self x).le) is_closed_ball) lemma dense_iff {s : set α} : dense s ↔ ∀ x, ∀ r > 0, (ball x r ∩ s).nonempty := forall_congr $ λ x, by simp only [mem_closure_iff, set.nonempty, exists_prop, mem_inter_eq, mem_ball', and_comm] lemma dense_range_iff {f : β → α} : dense_range f ↔ ∀ x, ∀ r > 0, ∃ y, dist x (f y) < r := forall_congr $ λ x, by simp only [mem_closure_iff, exists_range_iff] /-- If a set `s` is separable, then the corresponding subtype is separable in a metric space. This is not obvious, as the countable set whose closure covers `s` does not need in general to be contained in `s`. -/ lemma _root_.topological_space.is_separable.separable_space {s : set α} (hs : is_separable s) : separable_space s := begin classical, rcases eq_empty_or_nonempty s with rfl|⟨⟨x₀, x₀s⟩⟩, { haveI : encodable (∅ : set α) := fintype.to_encodable ↥∅, exact encodable.to_separable_space }, rcases hs with ⟨c, hc, h'c⟩, haveI : encodable c := hc.to_encodable, obtain ⟨u, -, u_pos, u_lim⟩ : ∃ (u : ℕ → ℝ), strict_anti u ∧ (∀ (n : ℕ), 0 < u n) ∧ tendsto u at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ), let f : c × ℕ → α := λ p, if h : (metric.ball (p.1 : α) (u p.2) ∩ s).nonempty then h.some else x₀, have fs : ∀ p, f p ∈ s, { rintros ⟨y, n⟩, by_cases h : (ball (y : α) (u n) ∩ s).nonempty, { simpa only [f, h, dif_pos] using h.some_spec.2 }, { simpa only [f, h, not_false_iff, dif_neg] } }, let g : c × ℕ → s := λ p, ⟨f p, fs p⟩, apply separable_space_of_dense_range g, apply metric.dense_range_iff.2, rintros ⟨x, xs⟩ r (rpos : 0 < r), obtain ⟨n, hn⟩ : ∃ n, u n < r / 2 := ((tendsto_order.1 u_lim).2 _ (half_pos rpos)).exists, obtain ⟨z, zc, hz⟩ : ∃ z ∈ c, dist x z < u n := metric.mem_closure_iff.1 (h'c xs) _ (u_pos n), refine ⟨(⟨z, zc⟩, n), _⟩, change dist x (f (⟨z, zc⟩, n)) < r, have A : (metric.ball z (u n) ∩ s).nonempty := ⟨x, hz, xs⟩, dsimp [f], simp only [A, dif_pos], calc dist x A.some ≤ dist x z + dist z A.some : dist_triangle _ _ _ ... < r/2 + r/2 : add_lt_add (hz.trans hn) ((metric.mem_ball'.1 A.some_spec.1).trans hn) ... = r : add_halves _ end /-- The preimage of a separable set by an inducing map is separable. -/ protected lemma _root_.inducing.is_separable_preimage {f : β → α} [topological_space β] (hf : inducing f) {s : set α} (hs : is_separable s) : is_separable (f ⁻¹' s) := begin haveI : second_countable_topology s, { haveI : separable_space s := hs.separable_space, exact uniform_space.second_countable_of_separable _ }, let g : f ⁻¹' s → s := cod_restrict (f ∘ coe) s (λ x, x.2), have : inducing g := (hf.comp inducing_coe).cod_restrict _, haveI : second_countable_topology (f ⁻¹' s) := this.second_countable_topology, rw show f ⁻¹' s = coe '' (univ : set (f ⁻¹' s)), by simpa only [image_univ, subtype.range_coe_subtype], exact (is_separable_of_separable_space _).image continuous_subtype_coe end protected lemma _root_.embedding.is_separable_preimage {f : β → α} [topological_space β] (hf : embedding f) {s : set α} (hs : is_separable s) : is_separable (f ⁻¹' s) := hf.to_inducing.is_separable_preimage hs /-- If a map is continuous on a separable set `s`, then the image of `s` is also separable. -/ lemma _root_.continuous_on.is_separable_image [topological_space β] {f : α → β} {s : set α} (hf : continuous_on f s) (hs : is_separable s) : is_separable (f '' s) := begin rw show f '' s = s.restrict f '' univ, by ext ; simp, exact (is_separable_univ_iff.2 hs.separable_space).image (continuous_on_iff_continuous_restrict.1 hf), end end metric section pi open finset variables {π : β → Type*} [fintype β] [∀b, pseudo_metric_space (π b)] /-- A finite product of pseudometric spaces is a pseudometric space, with the sup distance. -/ noncomputable instance pseudo_metric_space_pi : pseudo_metric_space (Πb, π b) := begin /- we construct the instance from the pseudoemetric space instance to avoid checking again that the uniformity is the same as the product uniformity, but we register nevertheless a nice formula for the distance -/ refine (pseudo_emetric_space.to_pseudo_metric_space_of_dist (λf g : Π b, π b, ((sup univ (λb, nndist (f b) (g b)) : ℝ≥0) : ℝ)) (λ f g, _) (λ f g, _)).replace_bornology (λ s, _), show edist f g ≠ ⊤, from ne_of_lt ((finset.sup_lt_iff bot_lt_top).2 $ λ b hb, edist_lt_top _ _), show ↑(sup univ (λ b, nndist (f b) (g b))) = (sup univ (λ b, edist (f b) (g b))).to_real, by simp only [edist_nndist, ← ennreal.coe_finset_sup, ennreal.coe_to_real], show (@is_bounded _ pi.bornology s ↔ @is_bounded _ pseudo_metric_space.to_bornology _), { simp only [← is_bounded_def, is_bounded_iff_eventually, ← forall_is_bounded_image_eval_iff, ball_image_iff, ← eventually_all, function.eval_apply, @dist_nndist (π _)], refine eventually_congr ((eventually_ge_at_top 0).mono $ λ C hC, _), lift C to ℝ≥0 using hC, refine ⟨λ H x hx y hy, nnreal.coe_le_coe.2 $ finset.sup_le $ λ b hb, H b x hx y hy, λ H b x hx y hy, nnreal.coe_le_coe.2 _⟩, simpa only using finset.sup_le_iff.1 (nnreal.coe_le_coe.1 $ H hx hy) b (finset.mem_univ b) } end lemma nndist_pi_def (f g : Πb, π b) : nndist f g = sup univ (λb, nndist (f b) (g b)) := nnreal.eq rfl lemma dist_pi_def (f g : Πb, π b) : dist f g = (sup univ (λb, nndist (f b) (g b)) : ℝ≥0) := rfl @[simp] lemma dist_pi_const [nonempty β] (a b : α) : dist (λ x : β, a) (λ _, b) = dist a b := by simpa only [dist_edist] using congr_arg ennreal.to_real (edist_pi_const a b) @[simp] lemma nndist_pi_const [nonempty β] (a b : α) : nndist (λ x : β, a) (λ _, b) = nndist a b := nnreal.eq $ dist_pi_const a b lemma nndist_pi_le_iff {f g : Πb, π b} {r : ℝ≥0} : nndist f g ≤ r ↔ ∀b, nndist (f b) (g b) ≤ r := by simp [nndist_pi_def] lemma dist_pi_lt_iff {f g : Πb, π b} {r : ℝ} (hr : 0 < r) : dist f g < r ↔ ∀b, dist (f b) (g b) < r := begin lift r to ℝ≥0 using hr.le, simp [dist_pi_def, finset.sup_lt_iff (show ⊥ < r, from hr)], end lemma dist_pi_le_iff {f g : Πb, π b} {r : ℝ} (hr : 0 ≤ r) : dist f g ≤ r ↔ ∀b, dist (f b) (g b) ≤ r := begin lift r to ℝ≥0 using hr, exact nndist_pi_le_iff end lemma nndist_le_pi_nndist (f g : Πb, π b) (b : β) : nndist (f b) (g b) ≤ nndist f g := by { rw [nndist_pi_def], exact finset.le_sup (finset.mem_univ b) } lemma dist_le_pi_dist (f g : Πb, π b) (b : β) : dist (f b) (g b) ≤ dist f g := by simp only [dist_nndist, nnreal.coe_le_coe, nndist_le_pi_nndist f g b] /-- An open ball in a product space is a product of open balls. See also `metric.ball_pi'` for a version assuming `nonempty β` instead of `0 < r`. -/ lemma ball_pi (x : Πb, π b) {r : ℝ} (hr : 0 < r) : ball x r = set.pi univ (λ b, ball (x b) r) := by { ext p, simp [dist_pi_lt_iff hr] } /-- An open ball in a product space is a product of open balls. See also `metric.ball_pi` for a version assuming `0 < r` instead of `nonempty β`. -/ lemma ball_pi' [nonempty β] (x : Π b, π b) (r : ℝ) : ball x r = set.pi univ (λ b, ball (x b) r) := (lt_or_le 0 r).elim (ball_pi x) $ λ hr, by simp [ball_eq_empty.2 hr] /-- A closed ball in a product space is a product of closed balls. See also `metric.closed_ball_pi'` for a version assuming `nonempty β` instead of `0 ≤ r`. -/ lemma closed_ball_pi (x : Πb, π b) {r : ℝ} (hr : 0 ≤ r) : closed_ball x r = set.pi univ (λ b, closed_ball (x b) r) := by { ext p, simp [dist_pi_le_iff hr] } /-- A closed ball in a product space is a product of closed balls. See also `metric.closed_ball_pi` for a version assuming `0 ≤ r` instead of `nonempty β`. -/ lemma closed_ball_pi' [nonempty β] (x : Π b, π b) (r : ℝ) : closed_ball x r = set.pi univ (λ b, closed_ball (x b) r) := (le_or_lt 0 r).elim (closed_ball_pi x) $ λ hr, by simp [closed_ball_eq_empty.2 hr] @[simp] lemma fin.nndist_insert_nth_insert_nth {n : ℕ} {α : fin (n + 1) → Type*} [Π i, pseudo_metric_space (α i)] (i : fin (n + 1)) (x y : α i) (f g : Π j, α (i.succ_above j)) : nndist (i.insert_nth x f) (i.insert_nth y g) = max (nndist x y) (nndist f g) := eq_of_forall_ge_iff $ λ c, by simp [nndist_pi_le_iff, i.forall_iff_succ_above] @[simp] lemma fin.dist_insert_nth_insert_nth {n : ℕ} {α : fin (n + 1) → Type*} [Π i, pseudo_metric_space (α i)] (i : fin (n + 1)) (x y : α i) (f g : Π j, α (i.succ_above j)) : dist (i.insert_nth x f) (i.insert_nth y g) = max (dist x y) (dist f g) := by simp only [dist_nndist, fin.nndist_insert_nth_insert_nth, nnreal.coe_max] lemma real.dist_le_of_mem_pi_Icc {x y x' y' : β → ℝ} (hx : x ∈ Icc x' y') (hy : y ∈ Icc x' y') : dist x y ≤ dist x' y' := begin refine (dist_pi_le_iff dist_nonneg).2 (λ b, (real.dist_le_of_mem_interval _ _).trans (dist_le_pi_dist _ _ b)); refine Icc_subset_interval _, exacts [⟨hx.1 _, hx.2 _⟩, ⟨hy.1 _, hy.2 _⟩] end end pi section compact /-- Any compact set in a pseudometric space can be covered by finitely many balls of a given positive radius -/ lemma finite_cover_balls_of_compact {α : Type u} [pseudo_metric_space α] {s : set α} (hs : is_compact s) {e : ℝ} (he : 0 < e) : ∃t ⊆ s, set.finite t ∧ s ⊆ ⋃x∈t, ball x e := begin apply hs.elim_finite_subcover_image, { simp [is_open_ball] }, { intros x xs, simp, exact ⟨x, ⟨xs, by simpa⟩⟩ } end alias finite_cover_balls_of_compact ← is_compact.finite_cover_balls end compact section proper_space open metric /-- A pseudometric space is proper if all closed balls are compact. -/ class proper_space (α : Type u) [pseudo_metric_space α] : Prop := (is_compact_closed_ball : ∀x:α, ∀r, is_compact (closed_ball x r)) export proper_space (is_compact_closed_ball) /-- In a proper pseudometric space, all spheres are compact. -/ lemma is_compact_sphere {α : Type*} [pseudo_metric_space α] [proper_space α] (x : α) (r : ℝ) : is_compact (sphere x r) := compact_of_is_closed_subset (is_compact_closed_ball x r) is_closed_sphere sphere_subset_closed_ball /-- In a proper pseudometric space, any sphere is a `compact_space` when considered as a subtype. -/ instance {α : Type*} [pseudo_metric_space α] [proper_space α] (x : α) (r : ℝ) : compact_space (sphere x r) := is_compact_iff_compact_space.mp (is_compact_sphere _ _) /-- A proper pseudo metric space is sigma compact, and therefore second countable. -/ @[priority 100] -- see Note [lower instance priority] instance second_countable_of_proper [proper_space α] : second_countable_topology α := begin -- We already have `sigma_compact_space_of_locally_compact_second_countable`, so we don't -- add an instance for `sigma_compact_space`. suffices : sigma_compact_space α, by exactI emetric.second_countable_of_sigma_compact α, rcases em (nonempty α) with ⟨⟨x⟩⟩|hn, { exact ⟨⟨λ n, closed_ball x n, λ n, is_compact_closed_ball _ _, Union_closed_ball_nat _⟩⟩ }, { exact ⟨⟨λ n, ∅, λ n, is_compact_empty, Union_eq_univ_iff.2 $ λ x, (hn ⟨x⟩).elim⟩⟩ } end lemma tendsto_dist_right_cocompact_at_top [proper_space α] (x : α) : tendsto (λ y, dist y x) (cocompact α) at_top := (has_basis_cocompact.tendsto_iff at_top_basis).2 $ λ r hr, ⟨closed_ball x r, is_compact_closed_ball x r, λ y hy, (not_le.1 $ mt mem_closed_ball.2 hy).le⟩ lemma tendsto_dist_left_cocompact_at_top [proper_space α] (x : α) : tendsto (dist x) (cocompact α) at_top := by simpa only [dist_comm] using tendsto_dist_right_cocompact_at_top x /-- If all closed balls of large enough radius are compact, then the space is proper. Especially useful when the lower bound for the radius is 0. -/ lemma proper_space_of_compact_closed_ball_of_le (R : ℝ) (h : ∀x:α, ∀r, R ≤ r → is_compact (closed_ball x r)) : proper_space α := ⟨begin assume x r, by_cases hr : R ≤ r, { exact h x r hr }, { have : closed_ball x r = closed_ball x R ∩ closed_ball x r, { symmetry, apply inter_eq_self_of_subset_right, exact closed_ball_subset_closed_ball (le_of_lt (not_le.1 hr)) }, rw this, exact (h x R le_rfl).inter_right is_closed_ball } end⟩ /- A compact pseudometric space is proper -/ @[priority 100] -- see Note [lower instance priority] instance proper_of_compact [compact_space α] : proper_space α := ⟨assume x r, is_closed_ball.is_compact⟩ /-- A proper space is locally compact -/ @[priority 100] -- see Note [lower instance priority] instance locally_compact_of_proper [proper_space α] : locally_compact_space α := locally_compact_space_of_has_basis (λ x, nhds_basis_closed_ball) $ λ x ε ε0, is_compact_closed_ball _ _ /-- A proper space is complete -/ @[priority 100] -- see Note [lower instance priority] instance complete_of_proper [proper_space α] : complete_space α := ⟨begin intros f hf, /- We want to show that the Cauchy filter `f` is converging. It suffices to find a closed ball (therefore compact by properness) where it is nontrivial. -/ obtain ⟨t, t_fset, ht⟩ : ∃ t ∈ f, ∀ x y ∈ t, dist x y < 1 := (metric.cauchy_iff.1 hf).2 1 zero_lt_one, rcases hf.1.nonempty_of_mem t_fset with ⟨x, xt⟩, have : closed_ball x 1 ∈ f := mem_of_superset t_fset (λ y yt, (ht y yt x xt).le), rcases (compact_iff_totally_bounded_complete.1 (is_compact_closed_ball x 1)).2 f hf (le_principal_iff.2 this) with ⟨y, -, hy⟩, exact ⟨y, hy⟩ end⟩ /-- A finite product of proper spaces is proper. -/ instance pi_proper_space {π : β → Type*} [fintype β] [∀b, pseudo_metric_space (π b)] [h : ∀b, proper_space (π b)] : proper_space (Πb, π b) := begin refine proper_space_of_compact_closed_ball_of_le 0 (λx r hr, _), rw closed_ball_pi _ hr, apply is_compact_univ_pi (λb, _), apply (h b).is_compact_closed_ball end variables [proper_space α] {x : α} {r : ℝ} {s : set α} /-- If a nonempty ball in a proper space includes a closed set `s`, then there exists a nonempty ball with the same center and a strictly smaller radius that includes `s`. -/ lemma exists_pos_lt_subset_ball (hr : 0 < r) (hs : is_closed s) (h : s ⊆ ball x r) : ∃ r' ∈ Ioo 0 r, s ⊆ ball x r' := begin unfreezingI { rcases eq_empty_or_nonempty s with rfl|hne }, { exact ⟨r / 2, ⟨half_pos hr, half_lt_self hr⟩, empty_subset _⟩ }, have : is_compact s, from compact_of_is_closed_subset (is_compact_closed_ball x r) hs (subset.trans h ball_subset_closed_ball), obtain ⟨y, hys, hy⟩ : ∃ y ∈ s, s ⊆ closed_ball x (dist y x), from this.exists_forall_ge hne (continuous_id.dist continuous_const).continuous_on, have hyr : dist y x < r, from h hys, rcases exists_between hyr with ⟨r', hyr', hrr'⟩, exact ⟨r', ⟨dist_nonneg.trans_lt hyr', hrr'⟩, subset.trans hy $ closed_ball_subset_ball hyr'⟩ end /-- If a ball in a proper space includes a closed set `s`, then there exists a ball with the same center and a strictly smaller radius that includes `s`. -/ lemma exists_lt_subset_ball (hs : is_closed s) (h : s ⊆ ball x r) : ∃ r' < r, s ⊆ ball x r' := begin cases le_or_lt r 0 with hr hr, { rw [ball_eq_empty.2 hr, subset_empty_iff] at h, unfreezingI { subst s }, exact (exists_lt r).imp (λ r' hr', ⟨hr', empty_subset _⟩) }, { exact (exists_pos_lt_subset_ball hr hs h).imp (λ r' hr', ⟨hr'.fst.2, hr'.snd⟩) } end end proper_space lemma is_compact.is_separable {s : set α} (hs : is_compact s) : is_separable s := begin haveI : compact_space s := is_compact_iff_compact_space.mp hs, exact is_separable_of_separable_space_subtype s, end namespace metric section second_countable open topological_space /-- A pseudometric space is second countable if, for every `ε > 0`, there is a countable set which is `ε`-dense. -/ lemma second_countable_of_almost_dense_set (H : ∀ε > (0 : ℝ), ∃ s : set α, s.countable ∧ (∀x, ∃y ∈ s, dist x y ≤ ε)) : second_countable_topology α := begin refine emetric.second_countable_of_almost_dense_set (λ ε ε0, _), rcases ennreal.lt_iff_exists_nnreal_btwn.1 ε0 with ⟨ε', ε'0, ε'ε⟩, choose s hsc y hys hyx using H ε' (by exact_mod_cast ε'0), refine ⟨s, hsc, Union₂_eq_univ_iff.2 (λ x, ⟨y x, hys _, le_trans _ ε'ε.le⟩)⟩, exact_mod_cast hyx x end end second_countable end metric lemma lebesgue_number_lemma_of_metric {s : set α} {ι} {c : ι → set α} (hs : is_compact s) (hc₁ : ∀ i, is_open (c i)) (hc₂ : s ⊆ ⋃ i, c i) : ∃ δ > 0, ∀ x ∈ s, ∃ i, ball x δ ⊆ c i := let ⟨n, en, hn⟩ := lebesgue_number_lemma hs hc₁ hc₂, ⟨δ, δ0, hδ⟩ := mem_uniformity_dist.1 en in ⟨δ, δ0, assume x hx, let ⟨i, hi⟩ := hn x hx in ⟨i, assume y hy, hi (hδ (mem_ball'.mp hy))⟩⟩ lemma lebesgue_number_lemma_of_metric_sUnion {s : set α} {c : set (set α)} (hs : is_compact s) (hc₁ : ∀ t ∈ c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃ δ > 0, ∀ x ∈ s, ∃ t ∈ c, ball x δ ⊆ t := by rw sUnion_eq_Union at hc₂; simpa using lebesgue_number_lemma_of_metric hs (by simpa) hc₂ namespace metric /-- Boundedness of a subset of a pseudometric space. We formulate the definition to work even in the empty space. -/ def bounded (s : set α) : Prop := ∃C, ∀x y ∈ s, dist x y ≤ C section bounded variables {x : α} {s t : set α} {r : ℝ} lemma bounded_iff_is_bounded (s : set α) : bounded s ↔ is_bounded s := begin change bounded s ↔ sᶜ ∈ (cobounded α).sets, simp [pseudo_metric_space.cobounded_sets, metric.bounded], end @[simp] lemma bounded_empty : bounded (∅ : set α) := ⟨0, by simp⟩ lemma bounded_iff_mem_bounded : bounded s ↔ ∀ x ∈ s, bounded s := ⟨λ h _ _, h, λ H, s.eq_empty_or_nonempty.elim (λ hs, hs.symm ▸ bounded_empty) (λ ⟨x, hx⟩, H x hx)⟩ /-- Subsets of a bounded set are also bounded -/ lemma bounded.mono (incl : s ⊆ t) : bounded t → bounded s := Exists.imp $ λ C hC x hx y hy, hC x (incl hx) y (incl hy) /-- Closed balls are bounded -/ lemma bounded_closed_ball : bounded (closed_ball x r) := ⟨r + r, λ y hy z hz, begin simp only [mem_closed_ball] at *, calc dist y z ≤ dist y x + dist z x : dist_triangle_right _ _ _ ... ≤ r + r : add_le_add hy hz end⟩ /-- Open balls are bounded -/ lemma bounded_ball : bounded (ball x r) := bounded_closed_ball.mono ball_subset_closed_ball /-- Spheres are bounded -/ lemma bounded_sphere : bounded (sphere x r) := bounded_closed_ball.mono sphere_subset_closed_ball /-- Given a point, a bounded subset is included in some ball around this point -/ lemma bounded_iff_subset_ball (c : α) : bounded s ↔ ∃r, s ⊆ closed_ball c r := begin split; rintro ⟨C, hC⟩, { cases s.eq_empty_or_nonempty with h h, { subst s, exact ⟨0, by simp⟩ }, { rcases h with ⟨x, hx⟩, exact ⟨C + dist x c, λ y hy, calc dist y c ≤ dist y x + dist x c : dist_triangle _ _ _ ... ≤ C + dist x c : add_le_add_right (hC y hy x hx) _⟩ } }, { exact bounded_closed_ball.mono hC } end lemma bounded.subset_ball (h : bounded s) (c : α) : ∃ r, s ⊆ closed_ball c r := (bounded_iff_subset_ball c).1 h lemma bounded.subset_ball_lt (h : bounded s) (a : ℝ) (c : α) : ∃ r, a < r ∧ s ⊆ closed_ball c r := begin rcases h.subset_ball c with ⟨r, hr⟩, refine ⟨max r (a+1), lt_of_lt_of_le (by linarith) (le_max_right _ _), _⟩, exact subset.trans hr (closed_ball_subset_closed_ball (le_max_left _ _)) end lemma bounded_closure_of_bounded (h : bounded s) : bounded (closure s) := let ⟨C, h⟩ := h in ⟨C, λ a ha b hb, (is_closed_le' C).closure_subset $ map_mem_closure2 continuous_dist ha hb $ ball_mem_comm.mp h⟩ alias bounded_closure_of_bounded ← bounded.closure @[simp] lemma bounded_closure_iff : bounded (closure s) ↔ bounded s := ⟨λ h, h.mono subset_closure, λ h, h.closure⟩ /-- The union of two bounded sets is bounded. -/ lemma bounded.union (hs : bounded s) (ht : bounded t) : bounded (s ∪ t) := begin refine bounded_iff_mem_bounded.2 (λ x _, _), rw bounded_iff_subset_ball x at hs ht ⊢, rcases hs with ⟨Cs, hCs⟩, rcases ht with ⟨Ct, hCt⟩, exact ⟨max Cs Ct, union_subset (subset.trans hCs $ closed_ball_subset_closed_ball $ le_max_left _ _) (subset.trans hCt $ closed_ball_subset_closed_ball $ le_max_right _ _)⟩, end /-- The union of two sets is bounded iff each of the sets is bounded. -/ @[simp] lemma bounded_union : bounded (s ∪ t) ↔ bounded s ∧ bounded t := ⟨λ h, ⟨h.mono (by simp), h.mono (by simp)⟩, λ h, h.1.union h.2⟩ /-- A finite union of bounded sets is bounded -/ lemma bounded_bUnion {I : set β} {s : β → set α} (H : I.finite) : bounded (⋃i∈I, s i) ↔ ∀i ∈ I, bounded (s i) := finite.induction_on H (by simp) $ λ x I _ _ IH, by simp [or_imp_distrib, forall_and_distrib, IH] protected lemma bounded.prod [pseudo_metric_space β] {s : set α} {t : set β} (hs : bounded s) (ht : bounded t) : bounded (s ×ˢ t) := begin refine bounded_iff_mem_bounded.mpr (λ x hx, _), rcases hs.subset_ball x.1 with ⟨rs, hrs⟩, rcases ht.subset_ball x.2 with ⟨rt, hrt⟩, suffices : s ×ˢ t ⊆ closed_ball x (max rs rt), from bounded_closed_ball.mono this, rw [← @prod.mk.eta _ _ x, ← closed_ball_prod_same], exact prod_mono (hrs.trans $ closed_ball_subset_closed_ball $ le_max_left _ _) (hrt.trans $ closed_ball_subset_closed_ball $ le_max_right _ _) end /-- A totally bounded set is bounded -/ lemma _root_.totally_bounded.bounded {s : set α} (h : totally_bounded s) : bounded s := -- We cover the totally bounded set by finitely many balls of radius 1, -- and then argue that a finite union of bounded sets is bounded let ⟨t, fint, subs⟩ := (totally_bounded_iff.mp h) 1 zero_lt_one in bounded.mono subs $ (bounded_bUnion fint).2 $ λ i hi, bounded_ball /-- A compact set is bounded -/ lemma _root_.is_compact.bounded {s : set α} (h : is_compact s) : bounded s := -- A compact set is totally bounded, thus bounded h.totally_bounded.bounded /-- A finite set is bounded -/ lemma bounded_of_finite {s : set α} (h : s.finite) : bounded s := h.is_compact.bounded alias bounded_of_finite ← _root_.set.finite.bounded /-- A singleton is bounded -/ lemma bounded_singleton {x : α} : bounded ({x} : set α) := bounded_of_finite $ finite_singleton _ /-- Characterization of the boundedness of the range of a function -/ lemma bounded_range_iff {f : β → α} : bounded (range f) ↔ ∃C, ∀x y, dist (f x) (f y) ≤ C := exists_congr $ λ C, ⟨ λ H x y, H _ ⟨x, rfl⟩ _ ⟨y, rfl⟩, by rintro H _ ⟨x, rfl⟩ _ ⟨y, rfl⟩; exact H x y⟩ lemma bounded_range_of_tendsto_cofinite_uniformity {f : β → α} (hf : tendsto (prod.map f f) (cofinite ×ᶠ cofinite) (𝓤 α)) : bounded (range f) := begin rcases (has_basis_cofinite.prod_self.tendsto_iff uniformity_basis_dist).1 hf 1 zero_lt_one with ⟨s, hsf, hs1⟩, rw [← image_univ, ← union_compl_self s, image_union, bounded_union], use [(hsf.image f).bounded, 1], rintro _ ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩, exact le_of_lt (hs1 (x, y) ⟨hx, hy⟩) end lemma bounded_range_of_cauchy_map_cofinite {f : β → α} (hf : cauchy (map f cofinite)) : bounded (range f) := bounded_range_of_tendsto_cofinite_uniformity $ (cauchy_map_iff.1 hf).2 lemma _root_.cauchy_seq.bounded_range {f : ℕ → α} (hf : cauchy_seq f) : bounded (range f) := bounded_range_of_cauchy_map_cofinite $ by rwa nat.cofinite_eq_at_top lemma bounded_range_of_tendsto_cofinite {f : β → α} {a : α} (hf : tendsto f cofinite (𝓝 a)) : bounded (range f) := bounded_range_of_tendsto_cofinite_uniformity $ (hf.prod_map hf).mono_right $ nhds_prod_eq.symm.trans_le (nhds_le_uniformity a) /-- In a compact space, all sets are bounded -/ lemma bounded_of_compact_space [compact_space α] : bounded s := compact_univ.bounded.mono (subset_univ _) lemma bounded_range_of_tendsto {α : Type*} [pseudo_metric_space α] (u : ℕ → α) {x : α} (hu : tendsto u at_top (𝓝 x)) : bounded (range u) := hu.cauchy_seq.bounded_range /-- The **Heine–Borel theorem**: In a proper space, a closed bounded set is compact. -/ lemma is_compact_of_is_closed_bounded [proper_space α] (hc : is_closed s) (hb : bounded s) : is_compact s := begin unfreezingI { rcases eq_empty_or_nonempty s with (rfl|⟨x, hx⟩) }, { exact is_compact_empty }, { rcases hb.subset_ball x with ⟨r, hr⟩, exact compact_of_is_closed_subset (is_compact_closed_ball x r) hc hr } end /-- The **Heine–Borel theorem**: In a proper space, the closure of a bounded set is compact. -/ lemma bounded.is_compact_closure [proper_space α] (h : bounded s) : is_compact (closure s) := is_compact_of_is_closed_bounded is_closed_closure h.closure /-- The **Heine–Borel theorem**: In a proper Hausdorff space, a set is compact if and only if it is closed and bounded. -/ lemma compact_iff_closed_bounded [t2_space α] [proper_space α] : is_compact s ↔ is_closed s ∧ bounded s := ⟨λ h, ⟨h.is_closed, h.bounded⟩, λ h, is_compact_of_is_closed_bounded h.1 h.2⟩ lemma compact_space_iff_bounded_univ [proper_space α] : compact_space α ↔ bounded (univ : set α) := ⟨@bounded_of_compact_space α _ _, λ hb, ⟨is_compact_of_is_closed_bounded is_closed_univ hb⟩⟩ section conditionally_complete_linear_order variables [preorder α] [compact_Icc_space α] lemma bounded_Icc (a b : α) : bounded (Icc a b) := (totally_bounded_Icc a b).bounded lemma bounded_Ico (a b : α) : bounded (Ico a b) := (totally_bounded_Ico a b).bounded lemma bounded_Ioc (a b : α) : bounded (Ioc a b) := (totally_bounded_Ioc a b).bounded lemma bounded_Ioo (a b : α) : bounded (Ioo a b) := (totally_bounded_Ioo a b).bounded /-- In a pseudo metric space with a conditionally complete linear order such that the order and the metric structure give the same topology, any order-bounded set is metric-bounded. -/ lemma bounded_of_bdd_above_of_bdd_below {s : set α} (h₁ : bdd_above s) (h₂ : bdd_below s) : bounded s := let ⟨u, hu⟩ := h₁, ⟨l, hl⟩ := h₂ in bounded.mono (λ x hx, mem_Icc.mpr ⟨hl hx, hu hx⟩) (bounded_Icc l u) end conditionally_complete_linear_order end bounded section diam variables {s : set α} {x y z : α} /-- The diameter of a set in a metric space. To get controllable behavior even when the diameter should be infinite, we express it in terms of the emetric.diameter -/ noncomputable def diam (s : set α) : ℝ := ennreal.to_real (emetric.diam s) /-- The diameter of a set is always nonnegative -/ lemma diam_nonneg : 0 ≤ diam s := ennreal.to_real_nonneg lemma diam_subsingleton (hs : s.subsingleton) : diam s = 0 := by simp only [diam, emetric.diam_subsingleton hs, ennreal.zero_to_real] /-- The empty set has zero diameter -/ @[simp] lemma diam_empty : diam (∅ : set α) = 0 := diam_subsingleton subsingleton_empty /-- A singleton has zero diameter -/ @[simp] lemma diam_singleton : diam ({x} : set α) = 0 := diam_subsingleton subsingleton_singleton -- Does not work as a simp-lemma, since {x, y} reduces to (insert y {x}) lemma diam_pair : diam ({x, y} : set α) = dist x y := by simp only [diam, emetric.diam_pair, dist_edist] -- Does not work as a simp-lemma, since {x, y, z} reduces to (insert z (insert y {x})) lemma diam_triple : metric.diam ({x, y, z} : set α) = max (max (dist x y) (dist x z)) (dist y z) := begin simp only [metric.diam, emetric.diam_triple, dist_edist], rw [ennreal.to_real_max, ennreal.to_real_max]; apply_rules [ne_of_lt, edist_lt_top, max_lt] end /-- If the distance between any two points in a set is bounded by some constant `C`, then `ennreal.of_real C` bounds the emetric diameter of this set. -/ lemma ediam_le_of_forall_dist_le {C : ℝ} (h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) : emetric.diam s ≤ ennreal.of_real C := emetric.diam_le $ λ x hx y hy, (edist_dist x y).symm ▸ ennreal.of_real_le_of_real (h x hx y hy) /-- If the distance between any two points in a set is bounded by some non-negative constant, this constant bounds the diameter. -/ lemma diam_le_of_forall_dist_le {C : ℝ} (h₀ : 0 ≤ C) (h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) : diam s ≤ C := ennreal.to_real_le_of_le_of_real h₀ (ediam_le_of_forall_dist_le h) /-- If the distance between any two points in a nonempty set is bounded by some constant, this constant bounds the diameter. -/ lemma diam_le_of_forall_dist_le_of_nonempty (hs : s.nonempty) {C : ℝ} (h : ∀ (x ∈ s) (y ∈ s), dist x y ≤ C) : diam s ≤ C := have h₀ : 0 ≤ C, from let ⟨x, hx⟩ := hs in le_trans dist_nonneg (h x hx x hx), diam_le_of_forall_dist_le h₀ h /-- The distance between two points in a set is controlled by the diameter of the set. -/ lemma dist_le_diam_of_mem' (h : emetric.diam s ≠ ⊤) (hx : x ∈ s) (hy : y ∈ s) : dist x y ≤ diam s := begin rw [diam, dist_edist], rw ennreal.to_real_le_to_real (edist_ne_top _ _) h, exact emetric.edist_le_diam_of_mem hx hy end /-- Characterize the boundedness of a set in terms of the finiteness of its emetric.diameter. -/ lemma bounded_iff_ediam_ne_top : bounded s ↔ emetric.diam s ≠ ⊤ := iff.intro (λ ⟨C, hC⟩, ne_top_of_le_ne_top ennreal.of_real_ne_top $ ediam_le_of_forall_dist_le hC) (λ h, ⟨diam s, λ x hx y hy, dist_le_diam_of_mem' h hx hy⟩) lemma bounded.ediam_ne_top (h : bounded s) : emetric.diam s ≠ ⊤ := bounded_iff_ediam_ne_top.1 h lemma ediam_univ_eq_top_iff_noncompact [proper_space α] : emetric.diam (univ : set α) = ∞ ↔ noncompact_space α := by rw [← not_compact_space_iff, compact_space_iff_bounded_univ, bounded_iff_ediam_ne_top, not_not] @[simp] lemma ediam_univ_of_noncompact [proper_space α] [noncompact_space α] : emetric.diam (univ : set α) = ∞ := ediam_univ_eq_top_iff_noncompact.mpr ‹_› @[simp] lemma diam_univ_of_noncompact [proper_space α] [noncompact_space α] : diam (univ : set α) = 0 := by simp [diam] /-- The distance between two points in a set is controlled by the diameter of the set. -/ lemma dist_le_diam_of_mem (h : bounded s) (hx : x ∈ s) (hy : y ∈ s) : dist x y ≤ diam s := dist_le_diam_of_mem' h.ediam_ne_top hx hy lemma ediam_of_unbounded (h : ¬(bounded s)) : emetric.diam s = ∞ := by rwa [bounded_iff_ediam_ne_top, not_not] at h /-- An unbounded set has zero diameter. If you would prefer to get the value ∞, use `emetric.diam`. This lemma makes it possible to avoid side conditions in some situations -/ lemma diam_eq_zero_of_unbounded (h : ¬(bounded s)) : diam s = 0 := by rw [diam, ediam_of_unbounded h, ennreal.top_to_real] /-- If `s ⊆ t`, then the diameter of `s` is bounded by that of `t`, provided `t` is bounded. -/ lemma diam_mono {s t : set α} (h : s ⊆ t) (ht : bounded t) : diam s ≤ diam t := begin unfold diam, rw ennreal.to_real_le_to_real (bounded.mono h ht).ediam_ne_top ht.ediam_ne_top, exact emetric.diam_mono h end /-- The diameter of a union is controlled by the sum of the diameters, and the distance between any two points in each of the sets. This lemma is true without any side condition, since it is obviously true if `s ∪ t` is unbounded. -/ lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + dist x y + diam t := begin by_cases H : bounded (s ∪ t), { have hs : bounded s, from H.mono (subset_union_left _ _), have ht : bounded t, from H.mono (subset_union_right _ _), rw [bounded_iff_ediam_ne_top] at H hs ht, rw [dist_edist, diam, diam, diam, ← ennreal.to_real_add, ← ennreal.to_real_add, ennreal.to_real_le_to_real]; repeat { apply ennreal.add_ne_top.2; split }; try { assumption }; try { apply edist_ne_top }, exact emetric.diam_union xs yt }, { rw [diam_eq_zero_of_unbounded H], apply_rules [add_nonneg, diam_nonneg, dist_nonneg] } end /-- If two sets intersect, the diameter of the union is bounded by the sum of the diameters. -/ lemma diam_union' {t : set α} (h : (s ∩ t).nonempty) : diam (s ∪ t) ≤ diam s + diam t := begin rcases h with ⟨x, ⟨xs, xt⟩⟩, simpa using diam_union xs xt end lemma diam_le_of_subset_closed_ball {r : ℝ} (hr : 0 ≤ r) (h : s ⊆ closed_ball x r) : diam s ≤ 2 * r := diam_le_of_forall_dist_le (mul_nonneg zero_le_two hr) $ λa ha b hb, calc dist a b ≤ dist a x + dist b x : dist_triangle_right _ _ _ ... ≤ r + r : add_le_add (h ha) (h hb) ... = 2 * r : by simp [mul_two, mul_comm] /-- The diameter of a closed ball of radius `r` is at most `2 r`. -/ lemma diam_closed_ball {r : ℝ} (h : 0 ≤ r) : diam (closed_ball x r) ≤ 2 * r := diam_le_of_subset_closed_ball h subset.rfl /-- The diameter of a ball of radius `r` is at most `2 r`. -/ lemma diam_ball {r : ℝ} (h : 0 ≤ r) : diam (ball x r) ≤ 2 * r := diam_le_of_subset_closed_ball h ball_subset_closed_ball /-- If a family of complete sets with diameter tending to `0` is such that each finite intersection is nonempty, then the total intersection is also nonempty. -/ lemma _root_.is_complete.nonempty_Inter_of_nonempty_bInter {s : ℕ → set α} (h0 : is_complete (s 0)) (hs : ∀ n, is_closed (s n)) (h's : ∀ n, bounded (s n)) (h : ∀ N, (⋂ n ≤ N, s n).nonempty) (h' : tendsto (λ n, diam (s n)) at_top (𝓝 0)) : (⋂ n, s n).nonempty := begin let u := λ N, (h N).some, have I : ∀ n N, n ≤ N → u N ∈ s n, { assume n N hn, apply mem_of_subset_of_mem _ ((h N).some_spec), assume x hx, simp only [mem_Inter] at hx, exact hx n hn }, have : ∀ n, u n ∈ s 0 := λ n, I 0 n (zero_le _), have : cauchy_seq u, { apply cauchy_seq_of_le_tendsto_0 _ _ h', assume m n N hm hn, exact dist_le_diam_of_mem (h's N) (I _ _ hm) (I _ _ hn) }, obtain ⟨x, hx, xlim⟩ : ∃ (x : α) (H : x ∈ s 0), tendsto (λ (n : ℕ), u n) at_top (𝓝 x) := cauchy_seq_tendsto_of_is_complete h0 (λ n, I 0 n (zero_le _)) this, refine ⟨x, mem_Inter.2 (λ n, _)⟩, apply (hs n).mem_of_tendsto xlim, filter_upwards [Ici_mem_at_top n] with p hp, exact I n p hp, end /-- In a complete space, if a family of closed sets with diameter tending to `0` is such that each finite intersection is nonempty, then the total intersection is also nonempty. -/ lemma nonempty_Inter_of_nonempty_bInter [complete_space α] {s : ℕ → set α} (hs : ∀ n, is_closed (s n)) (h's : ∀ n, bounded (s n)) (h : ∀ N, (⋂ n ≤ N, s n).nonempty) (h' : tendsto (λ n, diam (s n)) at_top (𝓝 0)) : (⋂ n, s n).nonempty := (hs 0).is_complete.nonempty_Inter_of_nonempty_bInter hs h's h h' end diam end metric lemma comap_dist_right_at_top_le_cocompact (x : α) : comap (λ y, dist y x) at_top ≤ cocompact α := begin refine filter.has_basis_cocompact.ge_iff.2 (λ s hs, mem_comap.2 _), rcases hs.bounded.subset_ball x with ⟨r, hr⟩, exact ⟨Ioi r, Ioi_mem_at_top r, λ y hy hys, (mem_closed_ball.1 $ hr hys).not_lt hy⟩ end lemma comap_dist_left_at_top_le_cocompact (x : α) : comap (dist x) at_top ≤ cocompact α := by simpa only [dist_comm _ x] using comap_dist_right_at_top_le_cocompact x lemma comap_dist_right_at_top_eq_cocompact [proper_space α] (x : α) : comap (λ y, dist y x) at_top = cocompact α := (comap_dist_right_at_top_le_cocompact x).antisymm $ (tendsto_dist_right_cocompact_at_top x).le_comap lemma comap_dist_left_at_top_eq_cocompact [proper_space α] (x : α) : comap (dist x) at_top = cocompact α := (comap_dist_left_at_top_le_cocompact x).antisymm $ (tendsto_dist_left_cocompact_at_top x).le_comap lemma tendsto_cocompact_of_tendsto_dist_comp_at_top {f : β → α} {l : filter β} (x : α) (h : tendsto (λ y, dist (f y) x) l at_top) : tendsto f l (cocompact α) := by { refine tendsto.mono_right _ (comap_dist_right_at_top_le_cocompact x), rwa tendsto_comap_iff } namespace int open metric /-- Under the coercion from `ℤ` to `ℝ`, inverse images of compact sets are finite. -/ lemma tendsto_coe_cofinite : tendsto (coe : ℤ → ℝ) cofinite (cocompact ℝ) := begin refine tendsto_cocompact_of_tendsto_dist_comp_at_top (0 : ℝ) _, simp only [filter.tendsto_at_top, eventually_cofinite, not_le, ← mem_ball], change ∀ r : ℝ, (coe ⁻¹' (ball (0 : ℝ) r)).finite, simp [real.ball_eq_Ioo, set.finite_Ioo], end end int /-- We now define `metric_space`, extending `pseudo_metric_space`. -/ class metric_space (α : Type u) extends pseudo_metric_space α : Type u := (eq_of_dist_eq_zero : ∀ {x y : α}, dist x y = 0 → x = y) /-- Two metric space structures with the same distance coincide. -/ @[ext] lemma metric_space.ext {α : Type*} {m m' : metric_space α} (h : m.to_has_dist = m'.to_has_dist) : m = m' := begin have h' : m.to_pseudo_metric_space = m'.to_pseudo_metric_space := pseudo_metric_space.ext h, unfreezingI { rcases m, rcases m' }, dsimp at h', unfreezingI { subst h' }, end /-- Construct a metric space structure whose underlying topological space structure (definitionally) agrees which a pre-existing topology which is compatible with a given distance function. -/ def metric_space.of_metrizable {α : Type*} [topological_space α] (dist : α → α → ℝ) (dist_self : ∀ x : α, dist x x = 0) (dist_comm : ∀ x y : α, dist x y = dist y x) (dist_triangle : ∀ x y z : α, dist x z ≤ dist x y + dist y z) (H : ∀ s : set α, is_open s ↔ ∀ x ∈ s, ∃ ε > 0, ∀ y, dist x y < ε → y ∈ s) (eq_of_dist_eq_zero : ∀ x y : α, dist x y = 0 → x = y) : metric_space α := { eq_of_dist_eq_zero := eq_of_dist_eq_zero, ..pseudo_metric_space.of_metrizable dist dist_self dist_comm dist_triangle H } variables {γ : Type w} [metric_space γ] theorem eq_of_dist_eq_zero {x y : γ} : dist x y = 0 → x = y := metric_space.eq_of_dist_eq_zero @[simp] theorem dist_eq_zero {x y : γ} : dist x y = 0 ↔ x = y := iff.intro eq_of_dist_eq_zero (assume : x = y, this ▸ dist_self _) @[simp] theorem zero_eq_dist {x y : γ} : 0 = dist x y ↔ x = y := by rw [eq_comm, dist_eq_zero] theorem dist_ne_zero {x y : γ} : dist x y ≠ 0 ↔ x ≠ y := by simpa only [not_iff_not] using dist_eq_zero @[simp] theorem dist_le_zero {x y : γ} : dist x y ≤ 0 ↔ x = y := by simpa [le_antisymm_iff, dist_nonneg] using @dist_eq_zero _ _ x y @[simp] theorem dist_pos {x y : γ} : 0 < dist x y ↔ x ≠ y := by simpa only [not_le] using not_congr dist_le_zero theorem eq_of_forall_dist_le {x y : γ} (h : ∀ ε > 0, dist x y ≤ ε) : x = y := eq_of_dist_eq_zero (eq_of_le_of_forall_le_of_dense dist_nonneg h) /--Deduce the equality of points with the vanishing of the nonnegative distance-/ theorem eq_of_nndist_eq_zero {x y : γ} : nndist x y = 0 → x = y := by simp only [← nnreal.eq_iff, ← dist_nndist, imp_self, nnreal.coe_zero, dist_eq_zero] /--Characterize the equality of points with the vanishing of the nonnegative distance-/ @[simp] theorem nndist_eq_zero {x y : γ} : nndist x y = 0 ↔ x = y := by simp only [← nnreal.eq_iff, ← dist_nndist, imp_self, nnreal.coe_zero, dist_eq_zero] @[simp] theorem zero_eq_nndist {x y : γ} : 0 = nndist x y ↔ x = y := by simp only [← nnreal.eq_iff, ← dist_nndist, imp_self, nnreal.coe_zero, zero_eq_dist] namespace metric variables {x : γ} {s : set γ} @[simp] lemma closed_ball_zero : closed_ball x 0 = {x} := set.ext $ λ y, dist_le_zero @[simp] lemma sphere_zero : sphere x 0 = {x} := set.ext $ λ y, dist_eq_zero lemma subsingleton_closed_ball (x : γ) {r : ℝ} (hr : r ≤ 0) : (closed_ball x r).subsingleton := begin rcases hr.lt_or_eq with hr|rfl, { rw closed_ball_eq_empty.2 hr, exact subsingleton_empty }, { rw closed_ball_zero, exact subsingleton_singleton } end lemma subsingleton_sphere (x : γ) {r : ℝ} (hr : r ≤ 0) : (sphere x r).subsingleton := (subsingleton_closed_ball x hr).anti sphere_subset_closed_ball /-- A map between metric spaces is a uniform embedding if and only if the distance between `f x` and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/ theorem uniform_embedding_iff' [metric_space β] {f : γ → β} : uniform_embedding f ↔ (∀ ε > 0, ∃ δ > 0, ∀ {a b : γ}, dist a b < δ → dist (f a) (f b) < ε) ∧ (∀ δ > 0, ∃ ε > 0, ∀ {a b : γ}, dist (f a) (f b) < ε → dist a b < δ) := begin split, { assume h, exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1, (uniform_embedding_iff.1 h).2.2⟩ }, { rintros ⟨h₁, h₂⟩, refine uniform_embedding_iff.2 ⟨_, uniform_continuous_iff.2 h₁, h₂⟩, assume x y hxy, have : dist x y ≤ 0, { refine le_of_forall_lt' (λδ δpos, _), rcases h₂ δ δpos with ⟨ε, εpos, hε⟩, have : dist (f x) (f y) < ε, by simpa [hxy], exact hε this }, simpa using this } end @[priority 100] -- see Note [lower instance priority] instance _root_.metric_space.to_separated : separated_space γ := separated_def.2 $ λ x y h, eq_of_forall_dist_le $ λ ε ε0, le_of_lt (h _ (dist_mem_uniformity ε0)) /-- If a `pseudo_metric_space` is a T₀ space, then it is a `metric_space`. -/ def of_t0_pseudo_metric_space (α : Type*) [pseudo_metric_space α] [t0_space α] : metric_space α := { eq_of_dist_eq_zero := λ x y hdist, inseparable.eq $ metric.inseparable_iff.2 hdist, ..‹pseudo_metric_space α› } /-- A metric space induces an emetric space -/ @[priority 100] -- see Note [lower instance priority] instance metric_space.to_emetric_space : emetric_space γ := emetric.of_t0_pseudo_emetric_space γ lemma is_closed_of_pairwise_le_dist {s : set γ} {ε : ℝ} (hε : 0 < ε) (hs : s.pairwise (λ x y, ε ≤ dist x y)) : is_closed s := is_closed_of_spaced_out (dist_mem_uniformity hε) $ by simpa using hs lemma closed_embedding_of_pairwise_le_dist {α : Type*} [topological_space α] [discrete_topology α] {ε : ℝ} (hε : 0 < ε) {f : α → γ} (hf : pairwise (λ x y, ε ≤ dist (f x) (f y))) : closed_embedding f := closed_embedding_of_spaced_out (dist_mem_uniformity hε) $ by simpa using hf /-- If `f : β → α` sends any two distinct points to points at distance at least `ε > 0`, then `f` is a uniform embedding with respect to the discrete uniformity on `β`. -/ lemma uniform_embedding_bot_of_pairwise_le_dist {β : Type*} {ε : ℝ} (hε : 0 < ε) {f : β → α} (hf : pairwise (λ x y, ε ≤ dist (f x) (f y))) : @uniform_embedding _ _ ⊥ (by apply_instance) f := uniform_embedding_of_spaced_out (dist_mem_uniformity hε) $ by simpa using hf end metric /-- Build a new metric space from an old one where the bundled uniform structure is provably (but typically non-definitionaly) equal to some given uniform structure. See Note [forgetful inheritance]. -/ def metric_space.replace_uniformity {γ} [U : uniform_space γ] (m : metric_space γ) (H : @uniformity _ U = @uniformity _ pseudo_emetric_space.to_uniform_space) : metric_space γ := { eq_of_dist_eq_zero := @eq_of_dist_eq_zero _ _, ..pseudo_metric_space.replace_uniformity m.to_pseudo_metric_space H, } lemma metric_space.replace_uniformity_eq {γ} [U : uniform_space γ] (m : metric_space γ) (H : @uniformity _ U = @uniformity _ pseudo_emetric_space.to_uniform_space) : m.replace_uniformity H = m := by { ext, refl } /-- Build a new metric space from an old one where the bundled topological structure is provably (but typically non-definitionaly) equal to some given topological structure. See Note [forgetful inheritance]. -/ @[reducible] def metric_space.replace_topology {γ} [U : topological_space γ] (m : metric_space γ) (H : U = m.to_pseudo_metric_space.to_uniform_space.to_topological_space) : metric_space γ := @metric_space.replace_uniformity γ (m.to_uniform_space.replace_topology H) m rfl lemma metric_space.replace_topology_eq {γ} [U : topological_space γ] (m : metric_space γ) (H : U = m.to_pseudo_metric_space.to_uniform_space.to_topological_space) : m.replace_topology H = m := by { ext, refl } /-- One gets a metric space from an emetric space if the edistance is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the uniformity are defeq in the metric space and the emetric space. In this definition, the distance is given separately, to be able to prescribe some expression which is not defeq to the push-forward of the edistance to reals. -/ def emetric_space.to_metric_space_of_dist {α : Type u} [e : emetric_space α] (dist : α → α → ℝ) (edist_ne_top : ∀x y: α, edist x y ≠ ⊤) (h : ∀x y, dist x y = ennreal.to_real (edist x y)) : metric_space α := { dist := dist, eq_of_dist_eq_zero := λx y hxy, by simpa [h, ennreal.to_real_eq_zero_iff, edist_ne_top x y] using hxy, ..pseudo_emetric_space.to_pseudo_metric_space_of_dist dist edist_ne_top h, } /-- One gets a metric space from an emetric space if the edistance is everywhere finite, by pushing the edistance to reals. We set it up so that the edist and the uniformity are defeq in the metric space and the emetric space. -/ def emetric_space.to_metric_space {α : Type u} [e : emetric_space α] (h : ∀x y: α, edist x y ≠ ⊤) : metric_space α := emetric_space.to_metric_space_of_dist (λx y, ennreal.to_real (edist x y)) h (λx y, rfl) /-- Build a new metric space from an old one where the bundled bornology structure is provably (but typically non-definitionaly) equal to some given bornology structure. See Note [forgetful inheritance]. -/ def metric_space.replace_bornology {α} [B : bornology α] (m : metric_space α) (H : ∀ s, @is_bounded _ B s ↔ @is_bounded _ pseudo_metric_space.to_bornology s) : metric_space α := { to_bornology := B, .. pseudo_metric_space.replace_bornology _ H, .. m } lemma metric_space.replace_bornology_eq {α} [m : metric_space α] [B : bornology α] (H : ∀ s, @is_bounded _ B s ↔ @is_bounded _ pseudo_metric_space.to_bornology s) : metric_space.replace_bornology _ H = m := by { ext, refl } /-- Metric space structure pulled back by an injective function. Injectivity is necessary to ensure that `dist x y = 0` only if `x = y`. -/ def metric_space.induced {γ β} (f : γ → β) (hf : function.injective f) (m : metric_space β) : metric_space γ := { eq_of_dist_eq_zero := λ x y h, hf (dist_eq_zero.1 h), ..pseudo_metric_space.induced f m.to_pseudo_metric_space } /-- Pull back a metric space structure by a uniform embedding. This is a version of `metric_space.induced` useful in case if the domain already has a `uniform_space` structure. -/ @[reducible] def uniform_embedding.comap_metric_space {α β} [uniform_space α] [metric_space β] (f : α → β) (h : uniform_embedding f) : metric_space α := (metric_space.induced f h.inj ‹_›).replace_uniformity h.comap_uniformity.symm /-- Pull back a metric space structure by an embedding. This is a version of `metric_space.induced` useful in case if the domain already has a `topological_space` structure. -/ @[reducible] def embedding.comap_metric_space {α β} [topological_space α] [metric_space β] (f : α → β) (h : embedding f) : metric_space α := begin letI : uniform_space α := embedding.comap_uniform_space f h, exact uniform_embedding.comap_metric_space f (h.to_uniform_embedding f), end instance subtype.metric_space {α : Type*} {p : α → Prop} [metric_space α] : metric_space (subtype p) := metric_space.induced coe subtype.coe_injective ‹_› @[to_additive] instance {α : Type*} [metric_space α] : metric_space (αᵐᵒᵖ) := metric_space.induced mul_opposite.unop mul_opposite.unop_injective ‹_› instance : metric_space empty := { dist := λ _ _, 0, dist_self := λ _, rfl, dist_comm := λ _ _, rfl, eq_of_dist_eq_zero := λ _ _ _, subsingleton.elim _ _, dist_triangle := λ _ _ _, show (0:ℝ) ≤ 0 + 0, by rw add_zero, to_uniform_space := empty.uniform_space, uniformity_dist := subsingleton.elim _ _ } instance : metric_space punit.{u + 1} := { dist := λ _ _, 0, dist_self := λ _, rfl, dist_comm := λ _ _, rfl, eq_of_dist_eq_zero := λ _ _ _, subsingleton.elim _ _, dist_triangle := λ _ _ _, show (0:ℝ) ≤ 0 + 0, by rw add_zero, to_uniform_space := punit.uniform_space, uniformity_dist := begin simp only, haveI : ne_bot (⨅ ε > (0 : ℝ), 𝓟 {p : punit.{u + 1} × punit.{u + 1} | 0 < ε}), { exact @uniformity.ne_bot _ (uniform_space_of_dist (λ _ _, 0) (λ _, rfl) (λ _ _, rfl) (λ _ _ _, by rw zero_add)) _ }, refine (eq_top_of_ne_bot _).trans (eq_top_of_ne_bot _).symm, end} section real /-- Instantiate the reals as a metric space. -/ noncomputable instance real.metric_space : metric_space ℝ := { eq_of_dist_eq_zero := λ x y h, by simpa [dist, sub_eq_zero] using h, ..real.pseudo_metric_space } end real section nnreal noncomputable instance : metric_space ℝ≥0 := subtype.metric_space end nnreal instance [metric_space β] : metric_space (ulift β) := metric_space.induced ulift.down ulift.down_injective ‹_› section prod noncomputable instance prod.metric_space_max [metric_space β] : metric_space (γ × β) := { eq_of_dist_eq_zero := λ x y h, begin cases max_le_iff.1 (le_of_eq h) with h₁ h₂, exact prod.ext_iff.2 ⟨dist_le_zero.1 h₁, dist_le_zero.1 h₂⟩ end, ..prod.pseudo_metric_space_max, } end prod section pi open finset variables {π : β → Type*} [fintype β] [∀b, metric_space (π b)] /-- A finite product of metric spaces is a metric space, with the sup distance. -/ noncomputable instance metric_space_pi : metric_space (Πb, π b) := /- we construct the instance from the emetric space instance to avoid checking again that the uniformity is the same as the product uniformity, but we register nevertheless a nice formula for the distance -/ { eq_of_dist_eq_zero := assume f g eq0, begin have eq1 : edist f g = 0 := by simp only [edist_dist, eq0, ennreal.of_real_zero], have eq2 : sup univ (λ (b : β), edist (f b) (g b)) ≤ 0 := le_of_eq eq1, simp only [finset.sup_le_iff] at eq2, exact (funext $ assume b, edist_le_zero.1 $ eq2 b $ mem_univ b) end, ..pseudo_metric_space_pi } end pi namespace metric section second_countable open topological_space /-- A metric space is second countable if one can reconstruct up to any `ε>0` any element of the space from countably many data. -/ lemma second_countable_of_countable_discretization {α : Type u} [metric_space α] (H : ∀ε > (0 : ℝ), ∃ (β : Type*) (_ : encodable β) (F : α → β), ∀x y, F x = F y → dist x y ≤ ε) : second_countable_topology α := begin cases (univ : set α).eq_empty_or_nonempty with hs hs, { haveI : compact_space α := ⟨by rw hs; exact is_compact_empty⟩, by apply_instance }, rcases hs with ⟨x0, hx0⟩, letI : inhabited α := ⟨x0⟩, refine second_countable_of_almost_dense_set (λε ε0, _), rcases H ε ε0 with ⟨β, fβ, F, hF⟩, resetI, let Finv := function.inv_fun F, refine ⟨range Finv, ⟨countable_range _, λx, _⟩⟩, let x' := Finv (F x), have : F x' = F x := function.inv_fun_eq ⟨x, rfl⟩, exact ⟨x', mem_range_self _, hF _ _ this.symm⟩ end end second_countable end metric section eq_rel /-- The canonical equivalence relation on a pseudometric space. -/ def pseudo_metric.dist_setoid (α : Type u) [pseudo_metric_space α] : setoid α := setoid.mk (λx y, dist x y = 0) begin unfold equivalence, repeat { split }, { exact pseudo_metric_space.dist_self }, { assume x y h, rwa pseudo_metric_space.dist_comm }, { assume x y z hxy hyz, refine le_antisymm _ dist_nonneg, calc dist x z ≤ dist x y + dist y z : pseudo_metric_space.dist_triangle _ _ _ ... = 0 + 0 : by rw [hxy, hyz] ... = 0 : by simp } end local attribute [instance] pseudo_metric.dist_setoid /-- The canonical quotient of a pseudometric space, identifying points at distance `0`. -/ @[reducible] definition pseudo_metric_quot (α : Type u) [pseudo_metric_space α] : Type* := quotient (pseudo_metric.dist_setoid α) instance has_dist_metric_quot {α : Type u} [pseudo_metric_space α] : has_dist (pseudo_metric_quot α) := { dist := quotient.lift₂ (λp q : α, dist p q) begin assume x y x' y' hxx' hyy', have Hxx' : dist x x' = 0 := hxx', have Hyy' : dist y y' = 0 := hyy', have A : dist x y ≤ dist x' y' := calc dist x y ≤ dist x x' + dist x' y : pseudo_metric_space.dist_triangle _ _ _ ... = dist x' y : by simp [Hxx'] ... ≤ dist x' y' + dist y' y : pseudo_metric_space.dist_triangle _ _ _ ... = dist x' y' : by simp [pseudo_metric_space.dist_comm, Hyy'], have B : dist x' y' ≤ dist x y := calc dist x' y' ≤ dist x' x + dist x y' : pseudo_metric_space.dist_triangle _ _ _ ... = dist x y' : by simp [pseudo_metric_space.dist_comm, Hxx'] ... ≤ dist x y + dist y y' : pseudo_metric_space.dist_triangle _ _ _ ... = dist x y : by simp [Hyy'], exact le_antisymm A B end } lemma pseudo_metric_quot_dist_eq {α : Type u} [pseudo_metric_space α] (p q : α) : dist ⟦p⟧ ⟦q⟧ = dist p q := rfl instance metric_space_quot {α : Type u} [pseudo_metric_space α] : metric_space (pseudo_metric_quot α) := { dist_self := begin refine quotient.ind (λy, _), exact pseudo_metric_space.dist_self _ end, eq_of_dist_eq_zero := λxc yc, by exact quotient.induction_on₂ xc yc (λx y H, quotient.sound H), dist_comm := λxc yc, quotient.induction_on₂ xc yc (λx y, pseudo_metric_space.dist_comm _ _), dist_triangle := λxc yc zc, quotient.induction_on₃ xc yc zc (λx y z, pseudo_metric_space.dist_triangle _ _ _) } end eq_rel /-! ### `additive`, `multiplicative` The distance on those type synonyms is inherited without change. -/ open additive multiplicative section variables [has_dist X] instance : has_dist (additive X) := ‹has_dist X› instance : has_dist (multiplicative X) := ‹has_dist X› @[simp] lemma dist_of_mul (a b : X) : dist (of_mul a) (of_mul b) = dist a b := rfl @[simp] lemma dist_of_add (a b : X) : dist (of_add a) (of_add b) = dist a b := rfl @[simp] lemma dist_to_mul (a b : additive X) : dist (to_mul a) (to_mul b) = dist a b := rfl @[simp] lemma dist_to_add (a b : multiplicative X) : dist (to_add a) (to_add b) = dist a b := rfl end section variables [pseudo_metric_space X] instance : pseudo_metric_space (additive X) := ‹pseudo_metric_space X› instance : pseudo_metric_space (multiplicative X) := ‹pseudo_metric_space X› @[simp] lemma nndist_of_mul (a b : X) : nndist (of_mul a) (of_mul b) = nndist a b := rfl @[simp] lemma nndist_of_add (a b : X) : nndist (of_add a) (of_add b) = nndist a b := rfl @[simp] lemma nndist_to_mul (a b : additive X) : nndist (to_mul a) (to_mul b) = nndist a b := rfl @[simp] lemma nndist_to_add (a b : multiplicative X) : nndist (to_add a) (to_add b) = nndist a b := rfl end instance [metric_space X] : metric_space (additive X) := ‹metric_space X› instance [metric_space X] : metric_space (multiplicative X) := ‹metric_space X› instance [pseudo_metric_space X] [proper_space X] : proper_space (additive X) := ‹proper_space X› instance [pseudo_metric_space X] [proper_space X] : proper_space (multiplicative X) := ‹proper_space X› /-! ### Order dual The distance on this type synonym is inherited without change. -/ open order_dual section variables [has_dist X] instance : has_dist Xᵒᵈ := ‹has_dist X› @[simp] lemma dist_to_dual (a b : X) : dist (to_dual a) (to_dual b) = dist a b := rfl @[simp] lemma dist_of_dual (a b : Xᵒᵈ) : dist (of_dual a) (of_dual b) = dist a b := rfl end section variables [pseudo_metric_space X] instance : pseudo_metric_space Xᵒᵈ := ‹pseudo_metric_space X› @[simp] lemma nndist_to_dual (a b : X) : nndist (to_dual a) (to_dual b) = nndist a b := rfl @[simp] lemma nndist_of_dual (a b : Xᵒᵈ) : nndist (of_dual a) (of_dual b) = nndist a b := rfl end instance [metric_space X] : metric_space Xᵒᵈ := ‹metric_space X› instance [pseudo_metric_space X] [proper_space X] : proper_space Xᵒᵈ := ‹proper_space X›
b3bdbafae7cbd9261b8018e546318a369594e8f8
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/matrix/dual_number.lean
3f9e9c3ad10e4c4b97687475c2e96b265b914d03
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
1,629
lean
/- Copyright (c) 2023 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.dual_number import data.matrix.basic /-! # Matrices of dual numbers are isomorphic to dual numbers over matrices > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Showing this for the more general case of `triv_sq_zero_ext R M` would require an action between `matrix n n R` and `matrix n n M`, which would risk causing diamonds. -/ variables {R n : Type} [comm_semiring R] [fintype n] [decidable_eq n] open matrix triv_sq_zero_ext /-- Matrices over dual numbers and dual numbers over matrices are isomorphic. -/ @[simps] def matrix.dual_number_equiv : matrix n n (dual_number R) ≃ₐ[R] dual_number (matrix n n R) := { to_fun := λ A, ⟨of (λ i j, (A i j).fst), of (λ i j, (A i j).snd)⟩, inv_fun := λ d, of (λ i j, (d.fst i j, d.snd i j)), left_inv := λ A, matrix.ext $ λ i j, triv_sq_zero_ext.ext rfl rfl, right_inv := λ d, triv_sq_zero_ext.ext (matrix.ext $ λ i j, rfl) (matrix.ext $ λ i j, rfl), map_mul' := λ A B, begin ext; dsimp [mul_apply], { simp_rw [fst_sum, fst_mul] }, { simp_rw [snd_sum, snd_mul, smul_eq_mul, op_smul_eq_mul, finset.sum_add_distrib] }, end, map_add' := λ A B, triv_sq_zero_ext.ext rfl rfl, commutes' := λ r, begin simp_rw [algebra_map_eq_inl', algebra_map_eq_diagonal, pi.algebra_map_def, algebra.id.map_eq_self, algebra_map_eq_inl, ←diagonal_map (inl_zero R), map_apply, fst_inl, snd_inl], refl, end }
29af22943d4f4463e7411b879eb01db39fefdf8d
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Meta/DiscrTree.lean
8609250cfd0b9d2c56ccaf32b6a0dad216df1806
[ "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
26,542
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.Meta.WHNF import Lean.Meta.Transform import Lean.Meta.DiscrTreeTypes namespace Lean.Meta.DiscrTree /-! (Imperfect) discrimination trees. We use a hybrid representation. - A `PersistentHashMap` for the root node which usually contains many children. - A sorted array of key/node pairs for inner nodes. The edges are labeled by keys: - Constant names (and arity). Universe levels are ignored. - Free variables (and arity). Thus, an entry in the discrimination tree may reference hypotheses from the local context. - Literals - Star/Wildcard. We use them to represent metavariables and terms we want to ignore. We ignore implicit arguments and proofs. - Other. We use to represent other kinds of terms (e.g., nested lambda, forall, sort, etc). We reduce terms using `TransparencyMode.reducible`. Thus, all reducible definitions in an expression `e` are unfolded before we insert it into the discrimination tree. Recall that projections from classes are **NOT** reducible. For example, the expressions `Add.add α (ringAdd ?α ?s) ?x ?x` and `Add.add Nat Nat.hasAdd a b` generates paths with the following keys respctively ``` ⟨Add.add, 4⟩, *, *, *, * ⟨Add.add, 4⟩, *, *, ⟨a,0⟩, ⟨b,0⟩ ``` That is, we don't reduce `Add.add Nat inst a b` into `Nat.add a b`. We say the `Add.add` applications are the de-facto canonical forms in the metaprogramming framework. Moreover, it is the metaprogrammer's responsibility to re-pack applications such as `Nat.add a b` into `Add.add Nat inst a b`. Remark: we store the arity in the keys 1- To be able to implement the "skip" operation when retrieving "candidate" unifiers. 2- Distinguish partial applications `f a`, `f a b`, and `f a b c`. -/ def Key.ctorIdx : Key s → Nat | .star => 0 | .other => 1 | .lit .. => 2 | .fvar .. => 3 | .const .. => 4 | .arrow => 5 | .proj .. => 6 def Key.lt : Key s → Key s → Bool | .lit v₁, .lit v₂ => v₁ < v₂ | .fvar n₁ a₁, .fvar n₂ a₂ => Name.quickLt n₁.name n₂.name || (n₁ == n₂ && a₁ < a₂) | .const n₁ a₁, .const n₂ a₂ => Name.quickLt n₁ n₂ || (n₁ == n₂ && a₁ < a₂) | .proj s₁ i₁ a₁, .proj s₂ i₂ a₂ => Name.quickLt s₁ s₂ || (s₁ == s₂ && i₁ < i₂) || (s₁ == s₂ && i₁ == i₂ && a₁ < a₂) | k₁, k₂ => k₁.ctorIdx < k₂.ctorIdx instance : LT (Key s) := ⟨fun a b => Key.lt a b⟩ instance (a b : Key s) : Decidable (a < b) := inferInstanceAs (Decidable (Key.lt a b)) def Key.format : Key s → Format | .star => "*" | .other => "◾" | .lit (Literal.natVal v) => Std.format v | .lit (Literal.strVal v) => repr v | .const k _ => Std.format k | .proj s i _ => Std.format s ++ "." ++ Std.format i | .fvar k _ => Std.format k.name | .arrow => "→" instance : ToFormat (Key s) := ⟨Key.format⟩ def Key.arity : (Key s) → Nat | .const _ a => a | .fvar _ a => a | .arrow => 2 | .proj _ _ a => 1 + a | _ => 0 instance : Inhabited (Trie α s) := ⟨.node #[] #[]⟩ def empty : DiscrTree α s := { root := {} } partial def Trie.format [ToFormat α] : Trie α s → Format | .node vs cs => Format.group $ Format.paren $ "node" ++ (if vs.isEmpty then Format.nil else " " ++ Std.format vs) ++ Format.join (cs.toList.map fun ⟨k, c⟩ => Format.line ++ Format.paren (Std.format k ++ " => " ++ format c)) instance [ToFormat α] : ToFormat (Trie α s) := ⟨Trie.format⟩ partial def format [ToFormat α] (d : DiscrTree α s) : Format := let (_, r) := d.root.foldl (fun (p : Bool × Format) k c => (false, p.2 ++ (if p.1 then Format.nil else Format.line) ++ Format.paren (Std.format k ++ " => " ++ Std.format c))) (true, Format.nil) Format.group r instance [ToFormat α] : ToFormat (DiscrTree α s) := ⟨format⟩ /-- The discrimination tree ignores implicit arguments and proofs. We use the following auxiliary id as a "mark". -/ private def tmpMVarId : MVarId := { name := `_discr_tree_tmp } private def tmpStar := mkMVar tmpMVarId instance : Inhabited (DiscrTree α s) where default := {} /-- Return true iff the argument should be treated as a "wildcard" by the discrimination tree. - We ignore proofs because of proof irrelevance. It doesn't make sense to try to index their structure. - We ignore instance implicit arguments (e.g., `[Add α]`) because they are "morally" canonical. Moreover, we may have many definitionally equal terms floating around. Example: `Ring.hasAdd Int Int.isRing` and `Int.hasAdd`. - We considered ignoring implicit arguments (e.g., `{α : Type}`) since users don't "see" them, and may not even understand why some simplification rule is not firing. However, in type class resolution, we have instance such as `Decidable (@Eq Nat x y)`, where `Nat` is an implicit argument. Thus, we would add the path ``` Decidable -> Eq -> * -> * -> * -> [Nat.decEq] ``` to the discrimination tree IF we ignored the implict `Nat` argument. This would be BAD since **ALL** decidable equality instances would be in the same path. So, we index implicit arguments if they are types. This setting seems sensible for simplification theorems such as: ``` forall (x y : Unit), (@Eq Unit x y) = true ``` If we ignore the implicit argument `Unit`, the `DiscrTree` will say it is a candidate simplification theorem for any equality in our goal. Remark: if users have problems with the solution above, we may provide a `noIndexing` annotation, and `ignoreArg` would return true for any term of the form `noIndexing t`. -/ private def ignoreArg (a : Expr) (i : Nat) (infos : Array ParamInfo) : MetaM Bool := do if h : i < infos.size then let info := infos.get ⟨i, h⟩ if info.isInstImplicit then return true else if info.isImplicit || info.isStrictImplicit then return not (← isType a) else isProof a else isProof a private partial def pushArgsAux (infos : Array ParamInfo) : Nat → Expr → Array Expr → MetaM (Array Expr) | i, .app f a, todo => do if (← ignoreArg a i infos) then pushArgsAux infos (i-1) f (todo.push tmpStar) else pushArgsAux infos (i-1) f (todo.push a) | _, _, todo => return todo /-- Return true if `e` is one of the following - A nat literal (numeral) - `Nat.zero` - `Nat.succ x` where `isNumeral x` - `OfNat.ofNat _ x _` where `isNumeral x` -/ private partial def isNumeral (e : Expr) : Bool := if e.isNatLit then true else let f := e.getAppFn if !f.isConst then false else let fName := f.constName! if fName == ``Nat.succ && e.getAppNumArgs == 1 then isNumeral e.appArg! else if fName == ``OfNat.ofNat && e.getAppNumArgs == 3 then isNumeral (e.getArg! 1) else if fName == ``Nat.zero && e.getAppNumArgs == 0 then true else false private partial def toNatLit? (e : Expr) : Option Literal := if isNumeral e then if let some n := loop e then some (.natVal n) else none else none where loop (e : Expr) : OptionT Id Nat := do let f := e.getAppFn match f with | .lit (.natVal n) => return n | .const fName .. => if fName == ``Nat.succ && e.getAppNumArgs == 1 then let r ← loop e.appArg! return r+1 else if fName == ``OfNat.ofNat && e.getAppNumArgs == 3 then loop (e.getArg! 1) else if fName == ``Nat.zero && e.getAppNumArgs == 0 then return 0 else failure | _ => failure private def isNatType (e : Expr) : MetaM Bool := return (← whnf e).isConstOf ``Nat /-- Return true if `e` is one of the following - `Nat.add _ k` where `isNumeral k` - `Add.add Nat _ _ k` where `isNumeral k` - `HAdd.hAdd _ Nat _ _ k` where `isNumeral k` - `Nat.succ _` This function assumes `e.isAppOf fName` -/ private def isOffset (fName : Name) (e : Expr) : MetaM Bool := do if fName == ``Nat.add && e.getAppNumArgs == 2 then return isNumeral e.appArg! else if fName == ``Add.add && e.getAppNumArgs == 4 then if (← isNatType (e.getArg! 0)) then return isNumeral e.appArg! else return false else if fName == ``HAdd.hAdd && e.getAppNumArgs == 6 then if (← isNatType (e.getArg! 1)) then return isNumeral e.appArg! else return false else return fName == ``Nat.succ && e.getAppNumArgs == 1 /-- TODO: add hook for users adding their own functions for controlling `shouldAddAsStar` Different `DiscrTree` users may populate this set using, for example, attributes. Remark: we currently tag "offset" terms as star to avoid having to add special support for offset terms. Example, suppose the discrimination tree contains the entry `Nat.succ ?m |-> v`, and we are trying to retrieve the matches for `Expr.lit (Literal.natVal 1) _`. In this scenario, we want to retrieve `Nat.succ ?m |-> v` -/ private def shouldAddAsStar (fName : Name) (e : Expr) : MetaM Bool := do isOffset fName e def mkNoindexAnnotation (e : Expr) : Expr := mkAnnotation `noindex e def hasNoindexAnnotation (e : Expr) : Bool := annotation? `noindex e |>.isSome /-- Reduction procedure for the discrimination tree indexing. The parameter `simpleReduce` controls how aggressive the term is reduced. The parameter at type `DiscrTree` controls this value. See comment at `DiscrTree`. -/ partial def reduce (e : Expr) (simpleReduce : Bool) : MetaM Expr := do let e ← whnfCore e (simpleReduceOnly := simpleReduce) match (← unfoldDefinition? e) with | some e => reduce e simpleReduce | none => match e.etaExpandedStrict? with | some e => reduce e simpleReduce | none => return e /-- Return `true` if `fn` is a "bad" key. That is, `pushArgs` would add `Key.other` or `Key.star`. We use this function when processing "root terms, and will avoid unfolding terms. Note that without this trick the pattern `List.map f ∘ List.map g` would be mapped into the key `Key.other` since the function composition `∘` would be unfolded and we would get `fun x => List.map g (List.map f x)` -/ private def isBadKey (fn : Expr) : Bool := match fn with | .lit .. => false | .const .. => false | .fvar .. => false | .proj .. => false | .forallE _ _ b _ => b.hasLooseBVars | _ => true /-- Try to eliminate loose bound variables by performing beta-reduction. We use this method when processing terms in discrimination trees. These trees distinguish dependent arrows from nondependent ones. Recall that dependent arrows are indexed as `.other`, but nondependent arrows as `.arrow ..`. Motivation: we want to "discriminate" implications and simple arrows in our index. Now suppose we add the term `Foo (Nat → Nat)` to our index. The nested arrow appears as `.arrow ..`. Then, suppose we want to check whether the index contains `(x : Nat) → (fun _ => Nat) x`, but it will fail to retrieve `Foo (Nat → Nat)` because it assumes the nested arrow is a dependent one and uses `.other`. We use this method to address this issue by beta-reducing terms containing loose bound variables. See issue #2232. Remark: we expect the performance impact will be minimal. -/ private def elimLooseBVarsByBeta (e : Expr) : CoreM Expr := Core.transform e (pre := fun e => do if !e.hasLooseBVars then return .done e else if e.isHeadBetaTarget then return .visit e.headBeta else return .continue) /-- Reduce `e` until we get an irreducible term (modulo current reducibility setting) or the resulting term is a bad key (see comment at `isBadKey`). We use this method instead of `reduce` for root terms at `pushArgs`. -/ private partial def reduceUntilBadKey (e : Expr) (simpleReduce : Bool) : MetaM Expr := do let e ← step e match e.etaExpandedStrict? with | some e => reduceUntilBadKey e simpleReduce | none => return e where step (e : Expr) := do let e ← whnfCore e (simpleReduceOnly := simpleReduce) match (← unfoldDefinition? e) with | some e' => if isBadKey e'.getAppFn then return e else step e' | none => return e /-- whnf for the discrimination tree module -/ def reduceDT (e : Expr) (root : Bool) (simpleReduce : Bool) : MetaM Expr := if root then reduceUntilBadKey e simpleReduce else reduce e simpleReduce /- Remark: we use `shouldAddAsStar` only for nested terms, and `root == false` for nested terms -/ private def pushArgs (root : Bool) (todo : Array Expr) (e : Expr) : MetaM (Key s × Array Expr) := do if hasNoindexAnnotation e then return (.star, todo) else let e ← reduceDT e root (simpleReduce := s) let fn := e.getAppFn let push (k : Key s) (nargs : Nat) (todo : Array Expr): MetaM (Key s × Array Expr) := do let info ← getFunInfoNArgs fn nargs let todo ← pushArgsAux info.paramInfo (nargs-1) e todo return (k, todo) match fn with | .lit v => return (.lit v, todo) | .const c _ => unless root do if let some v := toNatLit? e then return (.lit v, todo) if (← shouldAddAsStar c e) then return (.star, todo) let nargs := e.getAppNumArgs push (.const c nargs) nargs todo | .proj s i a => /- If `s` is a class, then `a` is an instance. Thus, we annotate `a` with `no_index` since we do not index instances. This should only happen if users mark a class projection function as `[reducible]`. TODO: add better support for projections that are functions -/ let a := if isClass (← getEnv) s then mkNoindexAnnotation a else a let nargs := e.getAppNumArgs push (.proj s i nargs) nargs (todo.push a) | .fvar fvarId => let nargs := e.getAppNumArgs push (.fvar fvarId nargs) nargs todo | .mvar mvarId => if mvarId == tmpMVarId then -- We use `tmp to mark implicit arguments and proofs return (.star, todo) else if (← mvarId.isReadOnlyOrSyntheticOpaque) then return (.other, todo) else return (.star, todo) | .forallE _ d b _ => -- See comment at elimLooseBVarsByBeta let b ← if b.hasLooseBVars then elimLooseBVarsByBeta b else pure b if b.hasLooseBVars then return (.other, todo) else return (.arrow, todo.push d |>.push b) | _ => return (.other, todo) partial def mkPathAux (root : Bool) (todo : Array Expr) (keys : Array (Key s)) : MetaM (Array (Key s)) := do if todo.isEmpty then return keys else let e := todo.back let todo := todo.pop let (k, todo) ← pushArgs root todo e mkPathAux false todo (keys.push k) private def initCapacity := 8 def mkPath (e : Expr) : MetaM (Array (Key s)) := do withReducible do let todo : Array Expr := .mkEmpty initCapacity let keys : Array (Key s) := .mkEmpty initCapacity mkPathAux (root := true) (todo.push e) keys private partial def createNodes (keys : Array (Key s)) (v : α) (i : Nat) : Trie α s := if h : i < keys.size then let k := keys.get ⟨i, h⟩ let c := createNodes keys v (i+1) .node #[] #[(k, c)] else .node #[v] #[] /-- If `vs` contains an element `v'` such that `v == v'`, then replace `v'` with `v`. Otherwise, push `v`. See issue #2155 Recall that `BEq α` may not be Lawful. -/ private def insertVal [BEq α] (vs : Array α) (v : α) : Array α := loop 0 where loop (i : Nat) : Array α := if h : i < vs.size then if v == vs[i] then vs.set ⟨i,h⟩ v else loop (i+1) else vs.push v termination_by loop i => vs.size - i private partial def insertAux [BEq α] (keys : Array (Key s)) (v : α) : Nat → Trie α s → Trie α s | i, .node vs cs => if h : i < keys.size then let k := keys.get ⟨i, h⟩ let c := Id.run $ cs.binInsertM (fun a b => a.1 < b.1) (fun ⟨_, s⟩ => let c := insertAux keys v (i+1) s; (k, c)) -- merge with existing (fun _ => let c := createNodes keys v (i+1); (k, c)) (k, default) .node vs c else .node (insertVal vs v) cs def insertCore [BEq α] (d : DiscrTree α s) (keys : Array (Key s)) (v : α) : DiscrTree α s := if keys.isEmpty then panic! "invalid key sequence" else let k := keys[0]! match d.root.find? k with | none => let c := createNodes keys v 1 { root := d.root.insert k c } | some c => let c := insertAux keys v 1 c { root := d.root.insert k c } def insert [BEq α] (d : DiscrTree α s) (e : Expr) (v : α) : MetaM (DiscrTree α s) := do let keys ← mkPath e return d.insertCore keys v private def getKeyArgs (e : Expr) (isMatch root : Bool) : MetaM (Key s × Array Expr) := do let e ← reduceDT e root (simpleReduce := s) unless root do -- See pushArgs if let some v := toNatLit? e then return (.lit v, #[]) match e.getAppFn with | .lit v => return (.lit v, #[]) | .const c _ => if (← getConfig).isDefEqStuckEx && e.hasExprMVar then if (← isReducible c) then /- `e` is a term `c ...` s.t. `c` is reducible and `e` has metavariables, but it was not unfolded. This can happen if the metavariables in `e` are "blocking" smart unfolding. If `isDefEqStuckEx` is enabled, then we must throw the `isDefEqStuck` exception to postpone TC resolution. Here is an example. Suppose we have ``` inductive Ty where | bool | fn (a ty : Ty) @[reducible] def Ty.interp : Ty → Type | bool => Bool | fn a b => a.interp → b.interp ``` and we are trying to synthesize `BEq (Ty.interp ?m)` -/ Meta.throwIsDefEqStuck else if let some matcherInfo := isMatcherAppCore? (← getEnv) e then -- A matcher application is stuck is one of the discriminants has a metavariable let args := e.getAppArgs for arg in args[matcherInfo.getFirstDiscrPos: matcherInfo.getFirstDiscrPos + matcherInfo.numDiscrs] do if arg.hasExprMVar then Meta.throwIsDefEqStuck else if (← isRec c) then /- Similar to the previous case, but for `match` and recursor applications. It may be stuck (i.e., did not reduce) because of metavariables. -/ Meta.throwIsDefEqStuck let nargs := e.getAppNumArgs return (.const c nargs, e.getAppRevArgs) | .fvar fvarId => let nargs := e.getAppNumArgs return (.fvar fvarId nargs, e.getAppRevArgs) | .mvar mvarId => if isMatch then return (.other, #[]) else do let ctx ← read if ctx.config.isDefEqStuckEx then /- When the configuration flag `isDefEqStuckEx` is set to true, we want `isDefEq` to throw an exception whenever it tries to assign a read-only metavariable. This feature is useful for type class resolution where we may want to notify the caller that the TC problem may be solveable later after it assigns `?m`. The method `DiscrTree.getUnify e` returns candidates `c` that may "unify" with `e`. That is, `isDefEq c e` may return true. Now, consider `DiscrTree.getUnify d (Add ?m)` where `?m` is a read-only metavariable, and the discrimination tree contains the keys `HadAdd Nat` and `Add Int`. If `isDefEqStuckEx` is set to true, we must treat `?m` as a regular metavariable here, otherwise we return the empty set of candidates. This is incorrect because it is equivalent to saying that there is no solution even if the caller assigns `?m` and try again. -/ return (.star, #[]) else if (← mvarId.isReadOnlyOrSyntheticOpaque) then return (.other, #[]) else return (.star, #[]) | .proj s i a .. => let nargs := e.getAppNumArgs return (.proj s i nargs, #[a] ++ e.getAppRevArgs) | .forallE _ d b _ => -- See comment at elimLooseBVarsByBeta let b ← if b.hasLooseBVars then elimLooseBVarsByBeta b else pure b if b.hasLooseBVars then return (.other, #[]) else return (.arrow, #[d, b]) | _ => return (.other, #[]) private abbrev getMatchKeyArgs (e : Expr) (root : Bool) : MetaM (Key s × Array Expr) := getKeyArgs e (isMatch := true) (root := root) private abbrev getUnifyKeyArgs (e : Expr) (root : Bool) : MetaM (Key s × Array Expr) := getKeyArgs e (isMatch := false) (root := root) private def getStarResult (d : DiscrTree α s) : Array α := let result : Array α := .mkEmpty initCapacity match d.root.find? .star with | none => result | some (.node vs _) => result ++ vs private abbrev findKey (cs : Array (Key s × Trie α s)) (k : Key s) : Option (Key s × Trie α s) := cs.binSearch (k, default) (fun a b => a.1 < b.1) private partial def getMatchLoop (todo : Array Expr) (c : Trie α s) (result : Array α) : MetaM (Array α) := do match c with | .node vs cs => if todo.isEmpty then return result ++ vs else if cs.isEmpty then return result else let e := todo.back let todo := todo.pop let first := cs[0]! /- Recall that `Key.star` is the minimal key -/ let (k, args) ← getMatchKeyArgs e (root := false) /- We must always visit `Key.star` edges since they are wildcards. Thus, `todo` is not used linearly when there is `Key.star` edge and there is an edge for `k` and `k != Key.star`. -/ let visitStar (result : Array α) : MetaM (Array α) := if first.1 == .star then getMatchLoop todo first.2 result else return result let visitNonStar (k : Key s) (args : Array Expr) (result : Array α) : MetaM (Array α) := match findKey cs k with | none => return result | some c => getMatchLoop (todo ++ args) c.2 result let result ← visitStar result match k with | .star => return result /- Note: dep-arrow vs arrow Recall that dependent arrows are `(Key.other, #[])`, and non-dependent arrows are `(Key.arrow, #[a, b])`. A non-dependent arrow may be an instance of a dependent arrow (stored at `DiscrTree`). Thus, we also visit the `Key.other` child. -/ | .arrow => visitNonStar .other #[] (← visitNonStar k args result) | _ => visitNonStar k args result private def getMatchRoot (d : DiscrTree α s) (k : Key s) (args : Array Expr) (result : Array α) : MetaM (Array α) := match d.root.find? k with | none => return result | some c => getMatchLoop args c result private def getMatchCore (d : DiscrTree α s) (e : Expr) : MetaM (Key s × Array α) := withReducible do let result := getStarResult d let (k, args) ← getMatchKeyArgs e (root := true) match k with | .star => return (k, result) /- See note about "dep-arrow vs arrow" at `getMatchLoop` -/ | .arrow => return (k, (← getMatchRoot d k args (← getMatchRoot d .other #[] result))) | _ => return (k, (← getMatchRoot d k args result)) /-- Find values that match `e` in `d`. -/ def getMatch (d : DiscrTree α s) (e : Expr) : MetaM (Array α) := return (← getMatchCore d e).2 /-- Similar to `getMatch`, but returns solutions that are prefixes of `e`. We store the number of ignored arguments in the result.-/ partial def getMatchWithExtra (d : DiscrTree α s) (e : Expr) : MetaM (Array (α × Nat)) := do let (k, result) ← getMatchCore d e let result := result.map (·, 0) if !e.isApp then return result else if !(← mayMatchPrefix k) then return result else go e.appFn! 1 result where mayMatchPrefix (k : Key s) : MetaM Bool := let cont (k : Key s) : MetaM Bool := if d.root.find? k |>.isSome then return true else mayMatchPrefix k match k with | .const f (n+1) => cont (.const f n) | .fvar f (n+1) => cont (.fvar f n) | .proj s i (n+1) => cont (.proj s i n) | _ => return false go (e : Expr) (numExtra : Nat) (result : Array (α × Nat)) : MetaM (Array (α × Nat)) := do let result := result ++ (← getMatch d e).map (., numExtra) if e.isApp then go e.appFn! (numExtra + 1) result else return result partial def getUnify (d : DiscrTree α s) (e : Expr) : MetaM (Array α) := withReducible do let (k, args) ← getUnifyKeyArgs e (root := true) match k with | .star => d.root.foldlM (init := #[]) fun result k c => process k.arity #[] c result | _ => let result := getStarResult d match d.root.find? k with | none => return result | some c => process 0 args c result where process (skip : Nat) (todo : Array Expr) (c : Trie α s) (result : Array α) : MetaM (Array α) := do match skip, c with | skip+1, .node _ cs => if cs.isEmpty then return result else cs.foldlM (init := result) fun result ⟨k, c⟩ => process (skip + k.arity) todo c result | 0, .node vs cs => do if todo.isEmpty then return result ++ vs else if cs.isEmpty then return result else let e := todo.back let todo := todo.pop let (k, args) ← getUnifyKeyArgs e (root := false) let visitStar (result : Array α) : MetaM (Array α) := let first := cs[0]! if first.1 == .star then process 0 todo first.2 result else return result let visitNonStar (k : Key s) (args : Array Expr) (result : Array α) : MetaM (Array α) := match findKey cs k with | none => return result | some c => process 0 (todo ++ args) c.2 result match k with | .star => cs.foldlM (init := result) fun result ⟨k, c⟩ => process k.arity todo c result -- See comment a `getMatch` regarding non-dependent arrows vs dependent arrows | .arrow => visitNonStar .other #[] (← visitNonStar k args (← visitStar result)) | _ => visitNonStar k args (← visitStar result) end Lean.Meta.DiscrTree
cc24aa3f83a476f0debf9c3f73c02cee4f85fe49
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/contra1.lean
48900f66476b587e89e5a1f8b3d269e559f67f41
[ "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
233
lean
open nat tactic example (a b : nat) (h : false) : a = b := by contradiction example : ∀ (a b : nat), false → a = b := by do intros, contradiction example : ∀ (a b : nat), (0:nat) = 1 → a = b := by do intros, contradiction
1a3f6d06d3f2d86e02199012e9fff3217aa7dff5
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/topology/subset_properties.lean
a4cda1307a4ddb9c10bb38b93177331d033493b5
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
24,166
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Properties of subsets of topological spaces: compact, clopen, irreducible, connected, totally disconnected, totally separated. -/ import topology.basic open set filter lattice classical open_locale classical universes u v variables {α : Type u} {β : Type v} [topological_space α] /- compact sets -/ section compact /-- A set `s` is compact if for every filter `f` that contains `s`, every set of `f` also meets every neighborhood of some `a ∈ s`. -/ def compact (s : set α) := ∀f, f ≠ ⊥ → f ≤ principal s → ∃a∈s, f ⊓ nhds a ≠ ⊥ lemma compact_inter {s t : set α} (hs : compact s) (ht : is_closed t) : compact (s ∩ t) := assume f hnf hstf, let ⟨a, hsa, (ha : f ⊓ nhds a ≠ ⊥)⟩ := hs f hnf (le_trans hstf (le_principal_iff.2 (inter_subset_left _ _))) in have ∀a, principal t ⊓ nhds a ≠ ⊥ → a ∈ t, by intro a; rw [inf_comm]; rw [is_closed_iff_nhds] at ht; exact ht a, have a ∈ t, from this a $ neq_bot_of_le_neq_bot ha $ inf_le_inf (le_trans hstf (le_principal_iff.2 (inter_subset_right _ _))) (le_refl _), ⟨a, ⟨hsa, this⟩, ha⟩ lemma compact_diff {s t : set α} (hs : compact s) (ht : is_open t) : compact (s \ t) := compact_inter hs (is_closed_compl_iff.mpr ht) lemma compact_of_is_closed_subset {s t : set α} (hs : compact s) (ht : is_closed t) (h : t ⊆ s) : compact t := by convert ← compact_inter hs ht; exact inter_eq_self_of_subset_right h lemma compact_adherence_nhdset {s t : set α} {f : filter α} (hs : compact s) (hf₂ : f ≤ principal s) (ht₁ : is_open t) (ht₂ : ∀a∈s, nhds a ⊓ f ≠ ⊥ → a ∈ t) : t ∈ f := classical.by_cases mem_sets_of_neq_bot $ assume : f ⊓ principal (- t) ≠ ⊥, let ⟨a, ha, (hfa : f ⊓ principal (-t) ⊓ nhds a ≠ ⊥)⟩ := hs _ this $ inf_le_left_of_le hf₂ in have a ∈ t, from ht₂ a ha $ neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_left, have nhds a ⊓ principal (-t) ≠ ⊥, from neq_bot_of_le_neq_bot hfa $ le_inf inf_le_right $ inf_le_left_of_le inf_le_right, have ∀s∈(nhds a ⊓ principal (-t)).sets, s ≠ ∅, from forall_sets_neq_empty_iff_neq_bot.mpr this, have false, from this _ ⟨t, mem_nhds_sets ht₁ ‹a ∈ t›, -t, subset.refl _, subset.refl _⟩ (inter_compl_self _), by contradiction lemma compact_iff_ultrafilter_le_nhds {s : set α} : compact s ↔ (∀f, is_ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a) := ⟨assume hs : compact s, assume f hf hfs, let ⟨a, ha, h⟩ := hs _ hf.left hfs in ⟨a, ha, le_of_ultrafilter hf h⟩, assume hs : (∀f, is_ultrafilter f → f ≤ principal s → ∃a∈s, f ≤ nhds a), assume f hf hfs, let ⟨a, ha, (h : ultrafilter_of f ≤ nhds a)⟩ := hs (ultrafilter_of f) (ultrafilter_ultrafilter_of hf) (le_trans ultrafilter_of_le hfs) in have ultrafilter_of f ⊓ nhds a ≠ ⊥, by simp only [inf_of_le_left, h]; exact (ultrafilter_ultrafilter_of hf).left, ⟨a, ha, neq_bot_of_le_neq_bot this (inf_le_inf ultrafilter_of_le (le_refl _))⟩⟩ lemma compact_elim_finite_subcover {s : set α} {c : set (set α)} (hs : compact s) (hc₁ : ∀t∈c, is_open t) (hc₂ : s ⊆ ⋃₀ c) : ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c' := classical.by_contradiction $ assume h, have h : ∀{c'}, c' ⊆ c → finite c' → ¬ s ⊆ ⋃₀ c', from assume c' h₁ h₂ h₃, h ⟨c', h₁, h₂, h₃⟩, let f : filter α := (⨅c':{c' : set (set α) // c' ⊆ c ∧ finite c'}, principal (s - ⋃₀ c')), ⟨a, ha⟩ := @exists_mem_of_ne_empty α s (assume h', h (empty_subset _) finite_empty $ h'.symm ▸ empty_subset _) in have f ≠ ⊥, from infi_neq_bot_of_directed ⟨a⟩ (assume ⟨c₁, hc₁, hc'₁⟩ ⟨c₂, hc₂, hc'₂⟩, ⟨⟨c₁ ∪ c₂, union_subset hc₁ hc₂, finite_union hc'₁ hc'₂⟩, principal_mono.mpr $ diff_subset_diff_right $ sUnion_mono $ subset_union_left _ _, principal_mono.mpr $ diff_subset_diff_right $ sUnion_mono $ subset_union_right _ _⟩) (assume ⟨c', hc'₁, hc'₂⟩, show principal (s \ _) ≠ ⊥, by simp only [ne.def, principal_eq_bot_iff, diff_eq_empty]; exact h hc'₁ hc'₂), have f ≤ principal s, from infi_le_of_le ⟨∅, empty_subset _, finite_empty⟩ $ show principal (s \ ⋃₀∅) ≤ principal s, from le_principal_iff.2 (diff_subset _ _), let ⟨a, ha, (h : f ⊓ nhds a ≠ ⊥)⟩ := hs f ‹f ≠ ⊥› this, ⟨t, ht₁, (ht₂ : a ∈ t)⟩ := hc₂ ha in have f ≤ principal (-t), from infi_le_of_le ⟨{t}, by rwa singleton_subset_iff, finite_insert _ finite_empty⟩ $ principal_mono.mpr $ show s - ⋃₀{t} ⊆ - t, begin rw sUnion_singleton; exact assume x ⟨_, hnt⟩, hnt end, have is_closed (- t), from is_open_compl_iff.mp $ by rw lattice.neg_neg; exact hc₁ t ht₁, have a ∈ - t, from is_closed_iff_nhds.mp this _ $ neq_bot_of_le_neq_bot h $ le_inf inf_le_right (inf_le_left_of_le ‹f ≤ principal (- t)›), this ‹a ∈ t› lemma compact_elim_finite_subcover_image {s : set α} {b : set β} {c : β → set α} (hs : compact s) (hc₁ : ∀i∈b, is_open (c i)) (hc₂ : s ⊆ ⋃i∈b, c i) : ∃b'⊆b, finite b' ∧ s ⊆ ⋃i∈b', c i := if h : b = ∅ then ⟨∅, empty_subset _, finite_empty, h ▸ hc₂⟩ else let ⟨i, hi⟩ := exists_mem_of_ne_empty h in have hc'₁ : ∀i∈c '' b, is_open i, from assume i ⟨j, hj, h⟩, h ▸ hc₁ _ hj, have hc'₂ : s ⊆ ⋃₀ (c '' b), by rwa set.sUnion_image, let ⟨d, hd₁, hd₂, hd₃⟩ := compact_elim_finite_subcover hs hc'₁ hc'₂ in have ∀x : d, ∃i, i ∈ b ∧ c i = x, from assume ⟨x, hx⟩, hd₁ hx, let ⟨f', hf⟩ := axiom_of_choice this, f := λx:set α, (if h : x ∈ d then f' ⟨x, h⟩ else i : β) in have ∀(x : α) (i : set α), i ∈ d → x ∈ i → (∃ (i : β), i ∈ f '' d ∧ x ∈ c i), from assume x i hid hxi, ⟨f i, mem_image_of_mem f hid, by simpa only [f, dif_pos hid, (hf ⟨_, hid⟩).2] using hxi⟩, ⟨f '' d, assume i ⟨j, hj, h⟩, h ▸ by simpa only [f, dif_pos hj] using (hf ⟨_, hj⟩).1, finite_image f hd₂, subset.trans hd₃ $ by simpa only [subset_def, mem_sUnion, exists_imp_distrib, mem_Union, exists_prop, and_imp]⟩ section -- this proof times out without this local attribute [instance, priority 1000] classical.prop_decidable lemma compact_of_finite_subcover {s : set α} (h : ∀c, (∀t∈c, is_open t) → s ⊆ ⋃₀ c → ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c') : compact s := assume f hfn hfs, classical.by_contradiction $ assume : ¬ (∃x∈s, f ⊓ nhds x ≠ ⊥), have hf : ∀x∈s, nhds x ⊓ f = ⊥, by simpa only [not_exists, not_not, inf_comm], have ¬ ∃x∈s, ∀t∈f.sets, x ∈ closure t, from assume ⟨x, hxs, hx⟩, have ∅ ∈ nhds x ⊓ f, by rw [empty_in_sets_eq_bot, hf x hxs], let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := by rw [mem_inf_sets] at this; exact this in have ∅ ∈ nhds x ⊓ principal t₂, from (nhds x ⊓ principal t₂).sets_of_superset (inter_mem_inf_sets ht₁ (subset.refl t₂)) ht, have nhds x ⊓ principal t₂ = ⊥, by rwa [empty_in_sets_eq_bot] at this, by simp only [closure_eq_nhds] at hx; exact hx t₂ ht₂ this, have ∀x∈s, ∃t∈f.sets, x ∉ closure t, by simpa only [not_exists, not_forall], let c := (λt, - closure t) '' f.sets, ⟨c', hcc', hcf, hsc'⟩ := h c (assume t ⟨s, hs, h⟩, h ▸ is_closed_closure) (by simpa only [subset_def, sUnion_image, mem_Union]) in let ⟨b, hb⟩ := axiom_of_choice $ show ∀s:c', ∃t, t ∈ f ∧ - closure t = s, from assume ⟨x, hx⟩, hcc' hx in have (⋂s∈c', if h : s ∈ c' then b ⟨s, h⟩ else univ) ∈ f, from Inter_mem_sets hcf $ assume t ht, by rw [dif_pos ht]; exact (hb ⟨t, ht⟩).left, have s ∩ (⋂s∈c', if h : s ∈ c' then b ⟨s, h⟩ else univ) ∈ f, from inter_mem_sets (le_principal_iff.1 hfs) this, have ∅ ∈ f, from mem_sets_of_superset this $ assume x ⟨hxs, hxi⟩, let ⟨t, htc', hxt⟩ := (show ∃t ∈ c', x ∈ t, from hsc' hxs) in have -closure (b ⟨t, htc'⟩) = t, from (hb _).right, have x ∈ - t, from this ▸ (calc x ∈ b ⟨t, htc'⟩ : by rw mem_bInter_iff at hxi; have h := hxi t htc'; rwa [dif_pos htc'] at h ... ⊆ closure (b ⟨t, htc'⟩) : subset_closure ... ⊆ - - closure (b ⟨t, htc'⟩) : by rw lattice.neg_neg; exact subset.refl _), show false, from this hxt, hfn $ by rwa [empty_in_sets_eq_bot] at this end lemma compact_iff_finite_subcover {s : set α} : compact s ↔ (∀c, (∀t∈c, is_open t) → s ⊆ ⋃₀ c → ∃c'⊆c, finite c' ∧ s ⊆ ⋃₀ c') := ⟨assume hc c, compact_elim_finite_subcover hc, compact_of_finite_subcover⟩ lemma compact_empty : compact (∅ : set α) := assume f hnf hsf, not.elim hnf $ empty_in_sets_eq_bot.1 $ le_principal_iff.1 hsf lemma compact_singleton {a : α} : compact ({a} : set α) := compact_of_finite_subcover $ assume c hc₁ hc₂, let ⟨i, hic, hai⟩ := (show ∃i ∈ c, a ∈ i, from mem_sUnion.1 $ singleton_subset_iff.1 hc₂) in ⟨{i}, singleton_subset_iff.2 hic, finite_singleton _, by rwa [sUnion_singleton, singleton_subset_iff]⟩ lemma compact_bUnion_of_compact {s : set β} {f : β → set α} (hs : finite s) : (∀i ∈ s, compact (f i)) → compact (⋃i ∈ s, f i) := assume hf, compact_of_finite_subcover $ assume c c_open c_cover, have ∀i : subtype s, ∃c' ⊆ c, finite c' ∧ f i ⊆ ⋃₀ c', from assume ⟨i, hi⟩, compact_elim_finite_subcover (hf i hi) c_open (calc f i ⊆ ⋃i ∈ s, f i : subset_bUnion_of_mem hi ... ⊆ ⋃₀ c : c_cover), let ⟨finite_subcovers, h⟩ := axiom_of_choice this in let c' := ⋃i, finite_subcovers i in have c' ⊆ c, from Union_subset (λi, (h i).fst), have finite c', from @finite_Union _ _ hs.fintype _ (λi, (h i).snd.1), have (⋃i ∈ s, f i) ⊆ ⋃₀ c', from bUnion_subset $ λi hi, calc f i ⊆ ⋃₀ finite_subcovers ⟨i,hi⟩ : (h ⟨i,hi⟩).snd.2 ... ⊆ ⋃₀ c' : sUnion_mono (subset_Union _ _), ⟨c', ‹c' ⊆ c›, ‹finite c'›, this⟩ lemma compact_Union_of_compact {f : β → set α} [fintype β] (h : ∀i, compact (f i)) : compact (⋃i, f i) := by rw ← bUnion_univ; exact compact_bUnion_of_compact finite_univ (λ i _, h i) lemma compact_of_finite {s : set α} (hs : finite s) : compact s := let s' : set α := ⋃i ∈ s, {i} in have e : s' = s, from ext $ λi, by simp only [mem_bUnion_iff, mem_singleton_iff, exists_eq_right'], have compact s', from compact_bUnion_of_compact hs (λ_ _, compact_singleton), e ▸ this lemma compact_union_of_compact {s t : set α} (hs : compact s) (ht : compact t) : compact (s ∪ t) := by rw union_eq_Union; exact compact_Union_of_compact (λ b, by cases b; assumption) /-- Type class for compact spaces. Separation is sometimes included in the definition, especially in the French literature, but we do not include it here. -/ class compact_space (α : Type*) [topological_space α] : Prop := (compact_univ : compact (univ : set α)) lemma compact_univ [h : compact_space α] : compact (univ : set α) := h.compact_univ lemma compact_of_closed [compact_space α] {s : set α} (h : is_closed s) : compact s := compact_of_is_closed_subset compact_univ h (subset_univ _) lemma compact_image [topological_space β] {s : set α} {f : α → β} (hs : compact s) (hf : continuous f) : compact (f '' s) := compact_of_finite_subcover $ assume c hco hcs, have hdo : ∀t∈c, is_open (f ⁻¹' t), from assume t' ht, hf _ $ hco _ ht, have hds : s ⊆ ⋃i∈c, f ⁻¹' i, by simpa [subset_def, -mem_image] using hcs, let ⟨d', hcd', hfd', hd'⟩ := compact_elim_finite_subcover_image hs hdo hds in ⟨d', hcd', hfd', by simpa [subset_def, -mem_image, image_subset_iff] using hd'⟩ lemma compact_range [compact_space α] [topological_space β] {f : α → β} (hf : continuous f) : compact (range f) := by rw ← image_univ; exact compact_image compact_univ hf /-- There are various definitions of "locally compact space" in the literature, which agree for Hausdorff spaces but not in general. This one is the precise condition on X needed for the evaluation `map C(X, Y) × X → Y` to be continuous for all `Y` when `C(X, Y)` is given the compact-open topology. -/ class locally_compact_space (α : Type*) [topological_space α] : Prop := (local_compact_nhds : ∀ (x : α) (n ∈ nhds x), ∃ s ∈ nhds x, s ⊆ n ∧ compact s) end compact section clopen def is_clopen (s : set α) : Prop := is_open s ∧ is_closed s theorem is_clopen_union {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∪ t) := ⟨is_open_union hs.1 ht.1, is_closed_union hs.2 ht.2⟩ theorem is_clopen_inter {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ t) := ⟨is_open_inter hs.1 ht.1, is_closed_inter hs.2 ht.2⟩ @[simp] theorem is_clopen_empty : is_clopen (∅ : set α) := ⟨is_open_empty, is_closed_empty⟩ @[simp] theorem is_clopen_univ : is_clopen (univ : set α) := ⟨is_open_univ, is_closed_univ⟩ theorem is_clopen_compl {s : set α} (hs : is_clopen s) : is_clopen (-s) := ⟨hs.2, is_closed_compl_iff.2 hs.1⟩ @[simp] theorem is_clopen_compl_iff {s : set α} : is_clopen (-s) ↔ is_clopen s := ⟨λ h, compl_compl s ▸ is_clopen_compl h, is_clopen_compl⟩ theorem is_clopen_diff {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s-t) := is_clopen_inter hs (is_clopen_compl ht) end clopen section irreducible /-- A irreducible set is one where there is no non-trivial pair of disjoint opens. -/ def is_irreducible (s : set α) : Prop := ∀ (u v : set α), is_open u → is_open v → (∃ x, x ∈ s ∩ u) → (∃ x, x ∈ s ∩ v) → ∃ x, x ∈ s ∩ (u ∩ v) theorem is_irreducible_empty : is_irreducible (∅ : set α) := λ _ _ _ _ _ ⟨x, h1, h2⟩, h1.elim theorem is_irreducible_singleton {x} : is_irreducible ({x} : set α) := λ u v _ _ ⟨y, h1, h2⟩ ⟨z, h3, h4⟩, by rw mem_singleton_iff at h1 h3; substs y z; exact ⟨x, or.inl rfl, h2, h4⟩ theorem is_irreducible_closure {s : set α} (H : is_irreducible s) : is_irreducible (closure s) := λ u v hu hv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, let ⟨p, hpu, hps⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hycs u hu hyu) in let ⟨q, hqv, hqs⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hzcs v hv hzv) in let ⟨r, hrs, hruv⟩ := H u v hu hv ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in ⟨r, subset_closure hrs, hruv⟩ theorem exists_irreducible (s : set α) (H : is_irreducible s) : ∃ t : set α, is_irreducible t ∧ s ⊆ t ∧ ∀ u, is_irreducible u → t ⊆ u → u = t := let ⟨m, hm, hsm, hmm⟩ := zorn.zorn_subset₀ { t : set α | is_irreducible t } (λ c hc hcc hcn, let ⟨t, htc⟩ := exists_mem_of_ne_empty hcn in ⟨⋃₀ c, λ u v hu hv ⟨y, hy, hyu⟩ ⟨z, hz, hzv⟩, let ⟨p, hpc, hyp⟩ := mem_sUnion.1 hy, ⟨q, hqc, hzq⟩ := mem_sUnion.1 hz in or.cases_on (zorn.chain.total hcc hpc hqc) (assume hpq : p ⊆ q, let ⟨x, hxp, hxuv⟩ := hc hqc u v hu hv ⟨y, hpq hyp, hyu⟩ ⟨z, hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hqc, hxuv⟩) (assume hqp : q ⊆ p, let ⟨x, hxp, hxuv⟩ := hc hpc u v hu hv ⟨y, hyp, hyu⟩ ⟨z, hqp hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hpc, hxuv⟩), λ x hxc, set.subset_sUnion_of_mem hxc⟩) s H in ⟨m, hm, hsm, λ u hu hmu, hmm _ hu hmu⟩ def irreducible_component (x : α) : set α := classical.some (exists_irreducible {x} is_irreducible_singleton) theorem is_irreducible_irreducible_component {x : α} : is_irreducible (irreducible_component x) := (classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).1 theorem mem_irreducible_component {x : α} : x ∈ irreducible_component x := singleton_subset_iff.1 (classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).2.1 theorem eq_irreducible_component {x : α} : ∀ {s : set α}, is_irreducible s → irreducible_component x ⊆ s → s = irreducible_component x := (classical.some_spec (exists_irreducible {x} is_irreducible_singleton)).2.2 theorem is_closed_irreducible_component {x : α} : is_closed (irreducible_component x) := closure_eq_iff_is_closed.1 $ eq_irreducible_component (is_irreducible_closure is_irreducible_irreducible_component) subset_closure /-- A irreducible space is one where there is no non-trivial pair of disjoint opens. -/ class irreducible_space (α : Type u) [topological_space α] : Prop := (is_irreducible_univ : is_irreducible (univ : set α)) theorem irreducible_exists_mem_inter [irreducible_space α] {s t : set α} : is_open s → is_open t → (∃ x, x ∈ s) → (∃ x, x ∈ t) → ∃ x, x ∈ s ∩ t := by simpa only [univ_inter, univ_subset_iff] using @irreducible_space.is_irreducible_univ α _ _ s t end irreducible section connected /-- A connected set is one where there is no non-trivial open partition. -/ def is_connected (s : set α) : Prop := ∀ (u v : set α), is_open u → is_open v → s ⊆ u ∪ v → (∃ x, x ∈ s ∩ u) → (∃ x, x ∈ s ∩ v) → ∃ x, x ∈ s ∩ (u ∩ v) theorem is_connected_of_is_irreducible {s : set α} (H : is_irreducible s) : is_connected s := λ _ _ hu hv _, H _ _ hu hv theorem is_connected_empty : is_connected (∅ : set α) := is_connected_of_is_irreducible is_irreducible_empty theorem is_connected_singleton {x} : is_connected ({x} : set α) := is_connected_of_is_irreducible is_irreducible_singleton theorem is_connected_sUnion (x : α) (c : set (set α)) (H1 : ∀ s ∈ c, x ∈ s) (H2 : ∀ s ∈ c, is_connected s) : is_connected (⋃₀ c) := begin rintro u v hu hv hUcuv ⟨y, hyUc, hyu⟩ ⟨z, hzUc, hzv⟩, cases classical.em (c = ∅) with hc hc, { rw [hc, sUnion_empty] at hyUc, exact hyUc.elim }, cases ne_empty_iff_exists_mem.1 hc with s hs, cases hUcuv (mem_sUnion_of_mem (H1 s hs) hs) with hxu hxv, { rcases hzUc with ⟨t, htc, hzt⟩, specialize H2 t htc u v hu hv (subset.trans (subset_sUnion_of_mem htc) hUcuv), cases H2 ⟨x, H1 t htc, hxu⟩ ⟨z, hzt, hzv⟩ with r hr, exact ⟨r, mem_sUnion_of_mem hr.1 htc, hr.2⟩ }, { rcases hyUc with ⟨t, htc, hyt⟩, specialize H2 t htc u v hu hv (subset.trans (subset_sUnion_of_mem htc) hUcuv), cases H2 ⟨y, hyt, hyu⟩ ⟨x, H1 t htc, hxv⟩ with r hr, exact ⟨r, mem_sUnion_of_mem hr.1 htc, hr.2⟩ } end theorem is_connected_union (x : α) {s t : set α} (H1 : x ∈ s) (H2 : x ∈ t) (H3 : is_connected s) (H4 : is_connected t) : is_connected (s ∪ t) := have _ := is_connected_sUnion x {t,s} (by rintro r (rfl | rfl | h); [exact H1, exact H2, exact h.elim]) (by rintro r (rfl | rfl | h); [exact H3, exact H4, exact h.elim]), have h2 : ⋃₀ {t, s} = s ∪ t, from (sUnion_insert _ _).trans (by rw sUnion_singleton), by rwa h2 at this theorem is_connected_closure {s : set α} (H : is_connected s) : is_connected (closure s) := λ u v hu hv hcsuv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, let ⟨p, hpu, hps⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hycs u hu hyu) in let ⟨q, hqv, hqs⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 hzcs v hv hzv) in let ⟨r, hrs, hruv⟩ := H u v hu hv (subset.trans subset_closure hcsuv) ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in ⟨r, subset_closure hrs, hruv⟩ def connected_component (x : α) : set α := ⋃₀ { s : set α | is_connected s ∧ x ∈ s } theorem is_connected_connected_component {x : α} : is_connected (connected_component x) := is_connected_sUnion x _ (λ _, and.right) (λ _, and.left) theorem mem_connected_component {x : α} : x ∈ connected_component x := mem_sUnion_of_mem (mem_singleton x) ⟨is_connected_singleton, mem_singleton x⟩ theorem subset_connected_component {x : α} {s : set α} (H1 : is_connected s) (H2 : x ∈ s) : s ⊆ connected_component x := λ z hz, mem_sUnion_of_mem hz ⟨H1, H2⟩ theorem is_closed_connected_component {x : α} : is_closed (connected_component x) := closure_eq_iff_is_closed.1 $ subset.antisymm (subset_connected_component (is_connected_closure is_connected_connected_component) (subset_closure mem_connected_component)) subset_closure theorem irreducible_component_subset_connected_component {x : α} : irreducible_component x ⊆ connected_component x := subset_connected_component (is_connected_of_is_irreducible is_irreducible_irreducible_component) mem_irreducible_component /-- A connected space is one where there is no non-trivial open partition. -/ class connected_space (α : Type u) [topological_space α] : Prop := (is_connected_univ : is_connected (univ : set α)) instance irreducible_space.connected_space (α : Type u) [topological_space α] [irreducible_space α] : connected_space α := ⟨is_connected_of_is_irreducible $ irreducible_space.is_irreducible_univ α⟩ theorem exists_mem_inter [connected_space α] {s t : set α} : is_open s → is_open t → s ∪ t = univ → (∃ x, x ∈ s) → (∃ x, x ∈ t) → ∃ x, x ∈ s ∩ t := by simpa only [univ_inter, univ_subset_iff] using @connected_space.is_connected_univ α _ _ s t theorem is_clopen_iff [connected_space α] {s : set α} : is_clopen s ↔ s = ∅ ∨ s = univ := ⟨λ hs, classical.by_contradiction $ λ h, have h1 : s ≠ ∅ ∧ -s ≠ ∅, from ⟨mt or.inl h, mt (λ h2, or.inr $ (by rw [← compl_compl s, h2, compl_empty] : s = univ)) h⟩, let ⟨_, h2, h3⟩ := exists_mem_inter hs.1 hs.2 (union_compl_self s) (ne_empty_iff_exists_mem.1 h1.1) (ne_empty_iff_exists_mem.1 h1.2) in h3 h2, by rintro (rfl | rfl); [exact is_clopen_empty, exact is_clopen_univ]⟩ end connected section totally_disconnected def is_totally_disconnected (s : set α) : Prop := ∀ t, t ⊆ s → is_connected t → subsingleton t theorem is_totally_disconnected_empty : is_totally_disconnected (∅ : set α) := λ t ht _, ⟨λ ⟨_, h⟩, (ht h).elim⟩ theorem is_totally_disconnected_singleton {x} : is_totally_disconnected ({x} : set α) := λ t ht _, ⟨λ ⟨p, hp⟩ ⟨q, hq⟩, subtype.eq $ show p = q, from (eq_of_mem_singleton (ht hp)).symm ▸ (eq_of_mem_singleton (ht hq)).symm⟩ class totally_disconnected_space (α : Type u) [topological_space α] : Prop := (is_totally_disconnected_univ : is_totally_disconnected (univ : set α)) end totally_disconnected section totally_separated def is_totally_separated (s : set α) : Prop := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → ∃ u v : set α, is_open u ∧ is_open v ∧ x ∈ u ∧ y ∈ v ∧ s ⊆ u ∪ v ∧ u ∩ v = ∅ theorem is_totally_separated_empty : is_totally_separated (∅ : set α) := λ x, false.elim theorem is_totally_separated_singleton {x} : is_totally_separated ({x} : set α) := λ p hp q hq hpq, (hpq $ (eq_of_mem_singleton hp).symm ▸ (eq_of_mem_singleton hq).symm).elim theorem is_totally_disconnected_of_is_totally_separated {s : set α} (H : is_totally_separated s) : is_totally_disconnected s := λ t hts ht, ⟨λ ⟨x, hxt⟩ ⟨y, hyt⟩, subtype.eq $ classical.by_contradiction $ assume hxy : x ≠ y, let ⟨u, v, hu, hv, hxu, hyv, hsuv, huv⟩ := H x (hts hxt) y (hts hyt) hxy in let ⟨r, hrt, hruv⟩ := ht u v hu hv (subset.trans hts hsuv) ⟨x, hxt, hxu⟩ ⟨y, hyt, hyv⟩ in ((ext_iff _ _).1 huv r).1 hruv⟩ class totally_separated_space (α : Type u) [topological_space α] : Prop := (is_totally_separated_univ : is_totally_separated (univ : set α)) instance totally_separated_space.totally_disconnected_space (α : Type u) [topological_space α] [totally_separated_space α] : totally_disconnected_space α := ⟨is_totally_disconnected_of_is_totally_separated $ totally_separated_space.is_totally_separated_univ α⟩ end totally_separated
14b25254f08090bf6f260e984ba881d32db624b2
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/algebra/gcd_monoid.lean
7fe3510ae3f25b8bc5f75d8d5fa5f45164b8a8c3
[ "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
36,146
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, Jens Wagemaker TODO: Provide a GCD monoid instance for `ℕ`, port GCD facts about nats TODO: Generalize normalization monoids commutative (cancellative) monoids with or without zero TODO: Generalize GCD monoid to not require normalization in all cases -/ import algebra.associated import data.nat.gcd import algebra.group_power.lemmas /-! # Monoids with normalization functions, `gcd`, and `lcm` This file defines extra structures on `comm_cancel_monoid_with_zero`s, including `integral_domain`s. ## Main Definitions * `normalization_monoid` * `gcd_monoid` * `gcd_monoid_of_exists_gcd` * `gcd_monoid_of_exists_lcm` ## Implementation Notes * `normalization_monoid` is defined by assigning to each element a `norm_unit` such that multiplying by that unit normalizes the monoid, and `normalize` is an idempotent monoid homomorphism. This definition as currently implemented does casework on `0`. * `gcd_monoid` extends `normalization_monoid`, so the `gcd` and `lcm` are always normalized. This makes `gcd`s of polynomials easier to work with, but excludes Euclidean domains, and monoids without zero. * `gcd_monoid_of_gcd` noncomputably constructs a `gcd_monoid` structure just from the `gcd` and its properties. * `gcd_monoid_of_exists_gcd` noncomputably constructs a `gcd_monoid` structure just from a proof that any two elements have a (not necessarily normalized) `gcd`. * `gcd_monoid_of_lcm` noncomputably constructs a `gcd_monoid` structure just from the `lcm` and its properties. * `gcd_monoid_of_exists_lcm` noncomputably constructs a `gcd_monoid` structure just from a proof that any two elements have a (not necessarily normalized) `lcm`. ## TODO * Provide a GCD monoid instance for `ℕ`, port GCD facts about nats, definition of coprime * Generalize normalization monoids to commutative (cancellative) monoids with or without zero * Generalize GCD monoid to not require normalization in all cases ## Tags divisibility, gcd, lcm, normalize -/ variables {α : Type*} set_option old_structure_cmd true /-- Normalization monoid: multiplying with `norm_unit` gives a normal form for associated elements. -/ @[protect_proj] class normalization_monoid (α : Type*) [nontrivial α] [comm_cancel_monoid_with_zero α] := (norm_unit : α → units α) (norm_unit_zero : norm_unit 0 = 1) (norm_unit_mul : ∀{a b}, a ≠ 0 → b ≠ 0 → norm_unit (a * b) = norm_unit a * norm_unit b) (norm_unit_coe_units : ∀(u : units α), norm_unit u = u⁻¹) export normalization_monoid (norm_unit norm_unit_zero norm_unit_mul norm_unit_coe_units) attribute [simp] norm_unit_coe_units norm_unit_zero norm_unit_mul section normalization_monoid variables [comm_cancel_monoid_with_zero α] [nontrivial α] [normalization_monoid α] @[simp] theorem norm_unit_one : norm_unit (1:α) = 1 := norm_unit_coe_units 1 /-- Chooses an element of each associate class, by multiplying by `norm_unit` -/ def normalize : monoid_with_zero_hom α α := { to_fun := λ x, x * norm_unit x, map_zero' := by simp, map_one' := by rw [norm_unit_one, units.coe_one, mul_one], map_mul' := λ x y, classical.by_cases (λ hx : x = 0, by rw [hx, zero_mul, zero_mul, zero_mul]) $ λ hx, classical.by_cases (λ hy : y = 0, by rw [hy, mul_zero, zero_mul, mul_zero]) $ λ hy, by simp only [norm_unit_mul hx hy, units.coe_mul]; simp only [mul_assoc, mul_left_comm y], } theorem associated_normalize {x : α} : associated x (normalize x) := ⟨_, rfl⟩ theorem normalize_associated {x : α} : associated (normalize x) x := associated_normalize.symm lemma associates.mk_normalize {x : α} : associates.mk (normalize x) = associates.mk x := associates.mk_eq_mk_iff_associated.2 normalize_associated @[simp] lemma normalize_apply {x : α} : normalize x = x * norm_unit x := rfl @[simp] lemma normalize_zero : normalize (0 : α) = 0 := normalize.map_zero @[simp] lemma normalize_one : normalize (1 : α) = 1 := normalize.map_one lemma normalize_coe_units (u : units α) : normalize (u : α) = 1 := by simp lemma normalize_eq_zero {x : α} : normalize x = 0 ↔ x = 0 := ⟨λ hx, (associated_zero_iff_eq_zero x).1 $ hx ▸ associated_normalize, by rintro rfl; exact normalize_zero⟩ lemma normalize_eq_one {x : α} : normalize x = 1 ↔ is_unit x := ⟨λ hx, is_unit_iff_exists_inv.2 ⟨_, hx⟩, λ ⟨u, hu⟩, hu ▸ normalize_coe_units u⟩ @[simp] theorem norm_unit_mul_norm_unit (a : α) : norm_unit (a * norm_unit a) = 1 := classical.by_cases (assume : a = 0, by simp only [this, norm_unit_zero, zero_mul]) $ assume h, by rw [norm_unit_mul h (units.ne_zero _), norm_unit_coe_units, mul_inv_eq_one] theorem normalize_idem (x : α) : normalize (normalize x) = normalize x := by simp theorem normalize_eq_normalize {a b : α} (hab : a ∣ b) (hba : b ∣ a) : normalize a = normalize b := begin rcases associated_of_dvd_dvd hab hba with ⟨u, rfl⟩, refine classical.by_cases (by rintro rfl; simp only [zero_mul]) (assume ha : a ≠ 0, _), suffices : a * ↑(norm_unit a) = a * ↑u * ↑(norm_unit a) * ↑u⁻¹, by simpa only [normalize_apply, mul_assoc, norm_unit_mul ha u.ne_zero, norm_unit_coe_units], calc a * ↑(norm_unit a) = a * ↑(norm_unit a) * ↑u * ↑u⁻¹: (units.mul_inv_cancel_right _ _).symm ... = a * ↑u * ↑(norm_unit a) * ↑u⁻¹ : by rw mul_right_comm a end lemma normalize_eq_normalize_iff {x y : α} : normalize x = normalize y ↔ x ∣ y ∧ y ∣ x := ⟨λ h, ⟨units.dvd_mul_right.1 ⟨_, h.symm⟩, units.dvd_mul_right.1 ⟨_, h⟩⟩, λ ⟨hxy, hyx⟩, normalize_eq_normalize hxy hyx⟩ theorem dvd_antisymm_of_normalize_eq {a b : α} (ha : normalize a = a) (hb : normalize b = b) (hab : a ∣ b) (hba : b ∣ a) : a = b := ha ▸ hb ▸ normalize_eq_normalize hab hba --can be proven by simp lemma dvd_normalize_iff {a b : α} : a ∣ normalize b ↔ a ∣ b := units.dvd_mul_right --can be proven by simp lemma normalize_dvd_iff {a b : α} : normalize a ∣ b ↔ a ∣ b := units.mul_right_dvd end normalization_monoid namespace comm_group_with_zero variables [decidable_eq α] [comm_group_with_zero α] @[priority 100] -- see Note [lower instance priority] instance : normalization_monoid α := { norm_unit := λ x, if h : x = 0 then 1 else (units.mk0 x h)⁻¹, norm_unit_zero := dif_pos rfl, norm_unit_mul := λ x y x0 y0, units.eq_iff.1 (by simp [x0, y0, mul_inv']), norm_unit_coe_units := λ u, by { rw [dif_neg (units.ne_zero _), units.mk0_coe], apply_instance } } @[simp] lemma coe_norm_unit {a : α} (h0 : a ≠ 0) : (↑(norm_unit a) : α) = a⁻¹ := by simp [norm_unit, h0] end comm_group_with_zero namespace associates variables [comm_cancel_monoid_with_zero α] [nontrivial α] [normalization_monoid α] local attribute [instance] associated.setoid /-- Maps an element of `associates` back to the normalized element of its associate class -/ protected def out : associates α → α := quotient.lift (normalize : α → α) $ λ a b ⟨u, hu⟩, hu ▸ normalize_eq_normalize ⟨_, rfl⟩ (units.mul_right_dvd.2 $ dvd_refl a) lemma out_mk (a : α) : (associates.mk a).out = normalize a := rfl @[simp] lemma out_one : (1 : associates α).out = 1 := normalize_one lemma out_mul (a b : associates α) : (a * b).out = a.out * b.out := quotient.induction_on₂ a b $ assume a b, by simp only [associates.quotient_mk_eq_mk, out_mk, mk_mul_mk, normalize.map_mul] lemma dvd_out_iff (a : α) (b : associates α) : a ∣ b.out ↔ associates.mk a ≤ b := quotient.induction_on b $ by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff] lemma out_dvd_iff (a : α) (b : associates α) : b.out ∣ a ↔ b ≤ associates.mk a := quotient.induction_on b $ by simp [associates.out_mk, associates.quotient_mk_eq_mk, mk_le_mk_iff_dvd_iff] @[simp] lemma out_top : (⊤ : associates α).out = 0 := normalize_zero @[simp] lemma normalize_out (a : associates α) : normalize a.out = a.out := quotient.induction_on a normalize_idem end associates /-- GCD monoid: a `comm_cancel_monoid_with_zero` with normalization and `gcd` (greatest common divisor) and `lcm` (least common multiple) operations. In this setting `gcd` and `lcm` form a bounded lattice on the associated elements where `gcd` is the infimum, `lcm` is the supremum, `1` is bottom, and `0` is top. The type class focuses on `gcd` and we derive the corresponding `lcm` facts from `gcd`. -/ @[protect_proj] class gcd_monoid (α : Type*) [comm_cancel_monoid_with_zero α] [nontrivial α] extends normalization_monoid α := (gcd : α → α → α) (lcm : α → α → α) (gcd_dvd_left : ∀a b, gcd a b ∣ a) (gcd_dvd_right : ∀a b, gcd a b ∣ b) (dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b) (normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b) (gcd_mul_lcm : ∀a b, gcd a b * lcm a b = normalize (a * b)) (lcm_zero_left : ∀a, lcm 0 a = 0) (lcm_zero_right : ∀a, lcm a 0 = 0) export gcd_monoid (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right) attribute [simp] lcm_zero_left lcm_zero_right section gcd_monoid variables [comm_cancel_monoid_with_zero α] [nontrivial α] [gcd_monoid α] @[simp] theorem normalize_gcd : ∀a b:α, normalize (gcd a b) = gcd a b := gcd_monoid.normalize_gcd @[simp] theorem gcd_mul_lcm : ∀a b:α, gcd a b * lcm a b = normalize (a * b) := gcd_monoid.gcd_mul_lcm section gcd theorem dvd_gcd_iff (a b c : α) : a ∣ gcd b c ↔ (a ∣ b ∧ a ∣ c) := iff.intro (assume h, ⟨dvd_trans h (gcd_dvd_left _ _), dvd_trans h (gcd_dvd_right _ _)⟩) (assume ⟨hab, hac⟩, dvd_gcd hab hac) theorem gcd_comm (a b : α) : gcd a b = gcd b a := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) (dvd_gcd (gcd_dvd_right _ _) (gcd_dvd_left _ _)) theorem gcd_assoc (m n k : α) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (dvd_gcd (dvd.trans (gcd_dvd_left (gcd m n) k) (gcd_dvd_left m n)) (dvd_gcd (dvd.trans (gcd_dvd_left (gcd m n) k) (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) (dvd.trans (gcd_dvd_right m (gcd n k)) (gcd_dvd_left n k))) (dvd.trans (gcd_dvd_right m (gcd n k)) (gcd_dvd_right n k))) instance : is_commutative α gcd := ⟨gcd_comm⟩ instance : is_associative α gcd := ⟨gcd_assoc⟩ theorem gcd_eq_normalize {a b c : α} (habc : gcd a b ∣ c) (hcab : c ∣ gcd a b) : gcd a b = normalize c := normalize_gcd a b ▸ normalize_eq_normalize habc hcab @[simp] theorem gcd_zero_left (a : α) : gcd 0 a = normalize a := gcd_eq_normalize (gcd_dvd_right 0 a) (dvd_gcd (dvd_zero _) (dvd_refl a)) @[simp] theorem gcd_zero_right (a : α) : gcd a 0 = normalize a := gcd_eq_normalize (gcd_dvd_left a 0) (dvd_gcd (dvd_refl a) (dvd_zero _)) @[simp] theorem gcd_eq_zero_iff (a b : α) : gcd a b = 0 ↔ a = 0 ∧ b = 0 := iff.intro (assume h, let ⟨ca, ha⟩ := gcd_dvd_left a b, ⟨cb, hb⟩ := gcd_dvd_right a b in by rw [h, zero_mul] at ha hb; exact ⟨ha, hb⟩) (assume ⟨ha, hb⟩, by rw [ha, hb, gcd_zero_left, normalize_zero]) @[simp] theorem gcd_one_left (a : α) : gcd 1 a = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_left _ _) (one_dvd _) @[simp] theorem gcd_one_right (a : α) : gcd a 1 = 1 := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) normalize_one (gcd_dvd_right _ _) (one_dvd _) theorem gcd_dvd_gcd {a b c d: α} (hab : a ∣ b) (hcd : c ∣ d) : gcd a c ∣ gcd b d := dvd_gcd (dvd.trans (gcd_dvd_left _ _) hab) (dvd.trans (gcd_dvd_right _ _) hcd) @[simp] theorem gcd_same (a : α) : gcd a a = normalize a := gcd_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_refl a)) @[simp] theorem gcd_mul_left (a b c : α) : gcd (a * b) (a * c) = normalize a * gcd b c := classical.by_cases (by rintro rfl; simp only [zero_mul, gcd_zero_left, normalize_zero]) $ assume ha : a ≠ 0, suffices gcd (a * b) (a * c) = normalize (a * gcd b c), by simpa only [normalize.map_mul, normalize_gcd], let ⟨d, eq⟩ := dvd_gcd (dvd_mul_right a b) (dvd_mul_right a c) in gcd_eq_normalize (eq.symm ▸ mul_dvd_mul_left a $ show d ∣ gcd b c, from dvd_gcd ((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_left _ _) ((mul_dvd_mul_iff_left ha).1 $ eq ▸ gcd_dvd_right _ _)) (dvd_gcd (mul_dvd_mul_left a $ gcd_dvd_left _ _) (mul_dvd_mul_left a $ gcd_dvd_right _ _)) @[simp] theorem gcd_mul_right (a b c : α) : gcd (b * a) (c * a) = gcd b c * normalize a := by simp only [mul_comm, gcd_mul_left] theorem gcd_eq_left_iff (a b : α) (h : normalize a = a) : gcd a b = a ↔ a ∣ b := iff.intro (assume eq, eq ▸ gcd_dvd_right _ _) $ assume hab, dvd_antisymm_of_normalize_eq (normalize_gcd _ _) h (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) hab) theorem gcd_eq_right_iff (a b : α) (h : normalize b = b) : gcd a b = b ↔ b ∣ a := by simpa only [gcd_comm a b] using gcd_eq_left_iff b a h theorem gcd_dvd_gcd_mul_left (m n k : α) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd (dvd_mul_left _ _) (dvd_refl _) theorem gcd_dvd_gcd_mul_right (m n k : α) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd (dvd_mul_right _ _) (dvd_refl _) theorem gcd_dvd_gcd_mul_left_right (m n k : α) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd (dvd_refl _) (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (m n k : α) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd (dvd_refl _) (dvd_mul_right _ _) theorem gcd_eq_of_associated_left {m n : α} (h : associated m n) (k : α) : gcd m k = gcd n k := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd (dvd_of_associated h) (dvd_refl _)) (gcd_dvd_gcd (dvd_of_associated h.symm) (dvd_refl _)) theorem gcd_eq_of_associated_right {m n : α} (h : associated m n) (k : α) : gcd k m = gcd k n := dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _) (gcd_dvd_gcd (dvd_refl _) (dvd_of_associated h)) (gcd_dvd_gcd (dvd_refl _) (dvd_of_associated h.symm)) lemma dvd_gcd_mul_of_dvd_mul {m n k : α} (H : k ∣ m * n) : k ∣ (gcd k m) * n := begin transitivity gcd k m * normalize n, { rw ← gcd_mul_right, exact dvd_gcd (dvd_mul_right _ _) H }, { apply dvd.intro ↑(norm_unit n)⁻¹, rw [normalize_apply, mul_assoc, mul_assoc, ← units.coe_mul], simp } end lemma dvd_mul_gcd_of_dvd_mul {m n k : α} (H : k ∣ m * n) : k ∣ m * gcd k n := by { rw mul_comm at H ⊢, exact dvd_gcd_mul_of_dvd_mul H } /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. Note: In general, this representation is highly non-unique. -/ lemma exists_dvd_and_dvd_of_dvd_mul {m n k : α} (H : k ∣ m * n) : ∃ d₁ (hd₁ : d₁ ∣ m) d₂ (hd₂ : d₂ ∣ n), k = d₁ * d₂ := begin by_cases h0 : gcd k m = 0, { rw gcd_eq_zero_iff at h0, rcases h0 with ⟨rfl, rfl⟩, refine ⟨0, dvd_refl 0, n, dvd_refl n, _⟩, simp }, { obtain ⟨a, ha⟩ := gcd_dvd_left k m, refine ⟨gcd k m, gcd_dvd_right _ _, a, _, ha⟩, suffices h : gcd k m * a ∣ gcd k m * n, { cases h with b hb, use b, rw mul_assoc at hb, apply mul_left_cancel' h0 hb }, rw ← ha, exact dvd_gcd_mul_of_dvd_mul H } end theorem gcd_mul_dvd_mul_gcd (k m n : α) : gcd k (m * n) ∣ gcd k m * gcd k n := begin obtain ⟨m', hm', n', hn', h⟩ := (exists_dvd_and_dvd_of_dvd_mul $ gcd_dvd_right k (m * n)), replace h : gcd k (m * n) = m' * n' := h, rw h, have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _, apply mul_dvd_mul, { have hm'k : m' ∣ k := dvd_trans (dvd_mul_right m' n') hm'n', exact dvd_gcd hm'k hm' }, { have hn'k : n' ∣ k := dvd_trans (dvd_mul_left n' m') hm'n', exact dvd_gcd hn'k hn' } end theorem gcd_pow_right_dvd_pow_gcd {a b : α} {k : ℕ} : gcd a (b ^ k) ∣ (gcd a b) ^ k := begin by_cases hg : gcd a b = 0, { rw gcd_eq_zero_iff at hg, rcases hg with ⟨rfl, rfl⟩, simp }, { induction k with k hk, simp, rw [pow_succ, pow_succ], transitivity gcd a b * gcd a (b ^ k), apply gcd_mul_dvd_mul_gcd a b (b ^ k), refine (mul_dvd_mul_iff_left hg).mpr hk } end theorem gcd_pow_left_dvd_pow_gcd {a b : α} {k : ℕ} : gcd (a ^ k) b ∣ (gcd a b) ^ k := by { rw [gcd_comm, gcd_comm a b], exact gcd_pow_right_dvd_pow_gcd } theorem pow_dvd_of_mul_eq_pow {a b c d₁ d₂ : α} (ha : a ≠ 0) (hab : gcd a b = 1) {k : ℕ} (h : a * b = c ^ k) (hc : c = d₁ * d₂) (hd₁ : d₁ ∣ a) : d₁ ^ k ≠ 0 ∧ d₁ ^ k ∣ a := begin have h1 : gcd (d₁ ^ k) b = 1, { rw ← normalize_gcd (d₁ ^ k) b, rw normalize_eq_one, apply is_unit_of_dvd_one, transitivity (gcd d₁ b) ^ k, { exact gcd_pow_left_dvd_pow_gcd }, { apply is_unit.dvd, apply is_unit.pow, apply is_unit_of_dvd_one, rw ← hab, apply gcd_dvd_gcd hd₁ (dvd_refl b) } }, have h2 : d₁ ^ k ∣ a * b, { use d₂ ^ k, rw [h, hc], exact mul_pow d₁ d₂ k }, rw mul_comm at h2, have h3 : d₁ ^ k ∣ a, { rw [← one_mul a, ← h1], apply dvd_gcd_mul_of_dvd_mul h2 }, have h4 : d₁ ^ k ≠ 0, { intro hdk, rw hdk at h3, apply absurd (zero_dvd_iff.mp h3) ha }, tauto end theorem exists_associated_pow_of_mul_eq_pow {a b c : α} (hab : gcd a b = 1) {k : ℕ} (h : a * b = c ^ k) : ∃ (d : α), associated (d ^ k) a := begin by_cases ha : a = 0, { use 0, rw ha, by_cases hk : k = 0, { exfalso, revert h, rw [ha, hk, zero_mul, pow_zero], apply zero_ne_one }, { rw zero_pow (nat.pos_of_ne_zero hk) }}, by_cases hb : b = 0, { rw [hb, gcd_zero_right] at hab, use 1, rw one_pow, apply (associated_one_iff_is_unit.mpr (normalize_eq_one.mp hab)).symm }, by_cases hk : k = 0, { use 1, rw [hk, pow_zero] at h ⊢, use units.mk_of_mul_eq_one _ _ h, rw [units.coe_mk_of_mul_eq_one, one_mul] }, have hc : c ∣ a * b, { rw h, refine dvd_pow (dvd_refl c) hk }, obtain ⟨d₁, hd₁, d₂, hd₂, hc⟩ := exists_dvd_and_dvd_of_dvd_mul hc, use d₁, obtain ⟨h0₁, ⟨a', ha'⟩⟩ := pow_dvd_of_mul_eq_pow ha hab h hc hd₁, rw [mul_comm] at h hc, rw [gcd_comm] at hab, obtain ⟨h0₂, ⟨b', hb'⟩⟩ := pow_dvd_of_mul_eq_pow hb hab h hc hd₂, rw [ha', hb', hc, mul_pow] at h, have h' : a' * b' = 1, { apply (mul_right_inj' h0₁).mp, rw mul_one, apply (mul_right_inj' h0₂).mp, rw ← h, rw [mul_assoc, mul_comm a', ← mul_assoc (d₁ ^ k), ← mul_assoc _ (d₁ ^ k), mul_comm b'] }, use units.mk_of_mul_eq_one _ _ h', rw [units.coe_mk_of_mul_eq_one, ha'] end end gcd section lcm lemma lcm_dvd_iff {a b c : α} : lcm a b ∣ c ↔ a ∣ c ∧ b ∣ c := classical.by_cases (assume : a = 0 ∨ b = 0, by rcases this with rfl | rfl; simp only [iff_def, lcm_zero_left, lcm_zero_right, zero_dvd_iff, dvd_zero, eq_self_iff_true, and_true, imp_true_iff] {contextual:=tt}) (assume this : ¬ (a = 0 ∨ b = 0), let ⟨h1, h2⟩ := not_or_distrib.1 this in have h : gcd a b ≠ 0, from λ H, h1 ((gcd_eq_zero_iff _ _).1 H).1, by rw [← mul_dvd_mul_iff_left h, gcd_mul_lcm, normalize_dvd_iff, ← dvd_normalize_iff, normalize.map_mul, normalize_gcd, ← gcd_mul_right, dvd_gcd_iff, mul_comm b c, mul_dvd_mul_iff_left h1, mul_dvd_mul_iff_right h2, and_comm]) lemma dvd_lcm_left (a b : α) : a ∣ lcm a b := (lcm_dvd_iff.1 (dvd_refl _)).1 lemma dvd_lcm_right (a b : α) : b ∣ lcm a b := (lcm_dvd_iff.1 (dvd_refl _)).2 lemma lcm_dvd {a b c : α} (hab : a ∣ b) (hcb : c ∣ b) : lcm a c ∣ b := lcm_dvd_iff.2 ⟨hab, hcb⟩ @[simp] theorem lcm_eq_zero_iff (a b : α) : lcm a b = 0 ↔ a = 0 ∨ b = 0 := iff.intro (assume h : lcm a b = 0, have normalize (a * b) = 0, by rw [← gcd_mul_lcm _ _, h, mul_zero], by simpa only [normalize_eq_zero, mul_eq_zero, units.ne_zero, or_false]) (by rintro (rfl | rfl); [apply lcm_zero_left, apply lcm_zero_right]) @[simp] lemma normalize_lcm (a b : α) : normalize (lcm a b) = lcm a b := classical.by_cases (assume : lcm a b = 0, by rw [this, normalize_zero]) $ assume h_lcm : lcm a b ≠ 0, have h1 : gcd a b ≠ 0, from mt (by rw [gcd_eq_zero_iff, lcm_eq_zero_iff]; rintros ⟨rfl, rfl⟩; left; refl) h_lcm, have h2 : normalize (gcd a b * lcm a b) = gcd a b * lcm a b, by rw [gcd_mul_lcm, normalize_idem], by simpa only [normalize.map_mul, normalize_gcd, one_mul, mul_right_inj' h1] using h2 theorem lcm_comm (a b : α) : lcm a b = lcm b a := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) (lcm_dvd (dvd_lcm_right _ _) (dvd_lcm_left _ _)) theorem lcm_assoc (m n k : α) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd (lcm_dvd (dvd_lcm_left _ _) (dvd.trans (dvd_lcm_left _ _) (dvd_lcm_right _ _))) (dvd.trans (dvd_lcm_right _ _) (dvd_lcm_right _ _))) (lcm_dvd (dvd.trans (dvd_lcm_left _ _) (dvd_lcm_left _ _)) (lcm_dvd (dvd.trans (dvd_lcm_right _ _) (dvd_lcm_left _ _)) (dvd_lcm_right _ _))) instance : is_commutative α lcm := ⟨lcm_comm⟩ instance : is_associative α lcm := ⟨lcm_assoc⟩ lemma lcm_eq_normalize {a b c : α} (habc : lcm a b ∣ c) (hcab : c ∣ lcm a b) : lcm a b = normalize c := normalize_lcm a b ▸ normalize_eq_normalize habc hcab theorem lcm_dvd_lcm {a b c d : α} (hab : a ∣ b) (hcd : c ∣ d) : lcm a c ∣ lcm b d := lcm_dvd (dvd.trans hab (dvd_lcm_left _ _)) (dvd.trans hcd (dvd_lcm_right _ _)) @[simp] theorem lcm_units_coe_left (u : units α) (a : α) : lcm ↑u a = normalize a := lcm_eq_normalize (lcm_dvd units.coe_dvd (dvd_refl _)) (dvd_lcm_right _ _) @[simp] theorem lcm_units_coe_right (a : α) (u : units α) : lcm a ↑u = normalize a := (lcm_comm a u).trans $ lcm_units_coe_left _ _ @[simp] theorem lcm_one_left (a : α) : lcm 1 a = normalize a := lcm_units_coe_left 1 a @[simp] theorem lcm_one_right (a : α) : lcm a 1 = normalize a := lcm_units_coe_right a 1 @[simp] theorem lcm_same (a : α) : lcm a a = normalize a := lcm_eq_normalize (lcm_dvd (dvd_refl _) (dvd_refl _)) (dvd_lcm_left _ _) @[simp] theorem lcm_eq_one_iff (a b : α) : lcm a b = 1 ↔ a ∣ 1 ∧ b ∣ 1 := iff.intro (assume eq, eq ▸ ⟨dvd_lcm_left _ _, dvd_lcm_right _ _⟩) (assume ⟨⟨c, hc⟩, ⟨d, hd⟩⟩, show lcm (units.mk_of_mul_eq_one a c hc.symm : α) (units.mk_of_mul_eq_one b d hd.symm) = 1, by rw [lcm_units_coe_left, normalize_coe_units]) @[simp] theorem lcm_mul_left (a b c : α) : lcm (a * b) (a * c) = normalize a * lcm b c := classical.by_cases (by rintro rfl; simp only [zero_mul, lcm_zero_left, normalize_zero]) $ assume ha : a ≠ 0, suffices lcm (a * b) (a * c) = normalize (a * lcm b c), by simpa only [normalize.map_mul, normalize_lcm], have a ∣ lcm (a * b) (a * c), from dvd.trans (dvd_mul_right _ _) (dvd_lcm_left _ _), let ⟨d, eq⟩ := this in lcm_eq_normalize (lcm_dvd (mul_dvd_mul_left a (dvd_lcm_left _ _)) (mul_dvd_mul_left a (dvd_lcm_right _ _))) (eq.symm ▸ (mul_dvd_mul_left a $ lcm_dvd ((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_left _ _) ((mul_dvd_mul_iff_left ha).1 $ eq ▸ dvd_lcm_right _ _))) @[simp] theorem lcm_mul_right (a b c : α) : lcm (b * a) (c * a) = lcm b c * normalize a := by simp only [mul_comm, lcm_mul_left] theorem lcm_eq_left_iff (a b : α) (h : normalize a = a) : lcm a b = a ↔ b ∣ a := iff.intro (assume eq, eq ▸ dvd_lcm_right _ _) $ assume hab, dvd_antisymm_of_normalize_eq (normalize_lcm _ _) h (lcm_dvd (dvd_refl a) hab) (dvd_lcm_left _ _) theorem lcm_eq_right_iff (a b : α) (h : normalize b = b) : lcm a b = b ↔ a ∣ b := by simpa only [lcm_comm b a] using lcm_eq_left_iff b a h theorem lcm_dvd_lcm_mul_left (m n k : α) : lcm m n ∣ lcm (k * m) n := lcm_dvd_lcm (dvd_mul_left _ _) (dvd_refl _) theorem lcm_dvd_lcm_mul_right (m n k : α) : lcm m n ∣ lcm (m * k) n := lcm_dvd_lcm (dvd_mul_right _ _) (dvd_refl _) theorem lcm_dvd_lcm_mul_left_right (m n k : α) : lcm m n ∣ lcm m (k * n) := lcm_dvd_lcm (dvd_refl _) (dvd_mul_left _ _) theorem lcm_dvd_lcm_mul_right_right (m n k : α) : lcm m n ∣ lcm m (n * k) := lcm_dvd_lcm (dvd_refl _) (dvd_mul_right _ _) theorem lcm_eq_of_associated_left {m n : α} (h : associated m n) (k : α) : lcm m k = lcm n k := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd_lcm (dvd_of_associated h) (dvd_refl _)) (lcm_dvd_lcm (dvd_of_associated h.symm) (dvd_refl _)) theorem lcm_eq_of_associated_right {m n : α} (h : associated m n) (k : α) : lcm k m = lcm k n := dvd_antisymm_of_normalize_eq (normalize_lcm _ _) (normalize_lcm _ _) (lcm_dvd_lcm (dvd_refl _) (dvd_of_associated h)) (lcm_dvd_lcm (dvd_refl _) (dvd_of_associated h.symm)) end lcm namespace gcd_monoid theorem prime_of_irreducible {x : α} (hi: irreducible x) : prime x := ⟨hi.ne_zero, ⟨hi.1, λ a b h, begin cases gcd_dvd_left x a with y hy, cases hi.is_unit_or_is_unit hy with hu hu; cases hu with u hu, { right, transitivity (gcd (x * b) (a * b)), apply dvd_gcd (dvd_mul_right x b) h, rw gcd_mul_right, rw ← hu, apply dvd_of_associated, transitivity (normalize b), symmetry, use u, apply mul_comm, apply normalize_associated, }, { left, rw [hy, ← hu], transitivity, {apply dvd_of_associated, symmetry, use u}, apply gcd_dvd_right, } end ⟩⟩ theorem irreducible_iff_prime {p : α} : irreducible p ↔ prime p := ⟨prime_of_irreducible, irreducible_of_prime⟩ end gcd_monoid end gcd_monoid section unique_unit variables [comm_cancel_monoid_with_zero α] [unique (units α)] lemma units_eq_one (u : units α) : u = 1 := subsingleton.elim u 1 variable [nontrivial α] @[priority 100] -- see Note [lower instance priority] instance normalization_monoid_of_unique_units : normalization_monoid α := { norm_unit := λ x, 1, norm_unit_zero := rfl, norm_unit_mul := λ x y hx hy, (mul_one 1).symm, norm_unit_coe_units := λ u, subsingleton.elim _ _ } @[simp] lemma norm_unit_eq_one (x : α) : norm_unit x = 1 := rfl @[simp] lemma normalize_eq (x : α) : normalize x = x := mul_one x end unique_unit section integral_domain variables [integral_domain α] [gcd_monoid α] lemma gcd_eq_of_dvd_sub_right {a b c : α} (h : a ∣ b - c) : gcd a b = gcd a c := begin apply dvd_antisymm_of_normalize_eq (normalize_gcd _ _) (normalize_gcd _ _); rw dvd_gcd_iff; refine ⟨gcd_dvd_left _ _, _⟩, { rcases h with ⟨d, hd⟩, rcases gcd_dvd_right a b with ⟨e, he⟩, rcases gcd_dvd_left a b with ⟨f, hf⟩, use e - f * d, rw [mul_sub, ← he, ← mul_assoc, ← hf, ← hd, sub_sub_cancel] }, { rcases h with ⟨d, hd⟩, rcases gcd_dvd_right a c with ⟨e, he⟩, rcases gcd_dvd_left a c with ⟨f, hf⟩, use e + f * d, rw [mul_add, ← he, ← mul_assoc, ← hf, ← hd, ← add_sub_assoc, add_comm c b, add_sub_cancel] } end lemma gcd_eq_of_dvd_sub_left {a b c : α} (h : a ∣ b - c) : gcd b a = gcd c a := by rw [gcd_comm _ a, gcd_comm _ a, gcd_eq_of_dvd_sub_right h] end integral_domain section constructors noncomputable theory open associates variables [comm_cancel_monoid_with_zero α] [nontrivial α] private lemma map_mk_unit_aux [decidable_eq α] {f : associates α →* α} (hinv : function.right_inverse f associates.mk) (a : α) : a * ↑(classical.some (associated_map_mk hinv a)) = f (associates.mk a) := classical.some_spec (associated_map_mk hinv a) /-- Define `normalization_monoid` on a structure from a `monoid_hom` inverse to `associates.mk`. -/ def normalization_monoid_of_monoid_hom_right_inverse [decidable_eq α] (f : associates α →* α) (hinv : function.right_inverse f associates.mk) : normalization_monoid α := { norm_unit := λ a, if a = 0 then 1 else classical.some (associates.mk_eq_mk_iff_associated.1 (hinv (associates.mk a)).symm), norm_unit_zero := if_pos rfl, norm_unit_mul := λ a b ha hb, by { rw [if_neg (mul_ne_zero ha hb), if_neg ha, if_neg hb, units.ext_iff, units.coe_mul], suffices : (a * b) * ↑(classical.some (associated_map_mk hinv (a * b))) = (a * ↑(classical.some (associated_map_mk hinv a))) * (b * ↑(classical.some (associated_map_mk hinv b))), { apply mul_left_cancel' (mul_ne_zero ha hb) _, simpa only [mul_assoc, mul_comm, mul_left_comm] using this }, rw [map_mk_unit_aux hinv a, map_mk_unit_aux hinv (a * b), map_mk_unit_aux hinv b, ← monoid_hom.map_mul, associates.mk_mul_mk] }, norm_unit_coe_units := λ u, by { rw [if_neg (units.ne_zero u), units.ext_iff], apply mul_left_cancel' (units.ne_zero u), rw [units.mul_inv, map_mk_unit_aux hinv u, associates.mk_eq_mk_iff_associated.2 (associated_one_iff_is_unit.2 ⟨u, rfl⟩), associates.mk_one, monoid_hom.map_one] } } variable [normalization_monoid α] /-- Define `gcd_monoid` on a structure just from the `gcd` and its properties. -/ noncomputable def gcd_monoid_of_gcd [decidable_eq α] (gcd : α → α → α) (gcd_dvd_left : ∀a b, gcd a b ∣ a) (gcd_dvd_right : ∀a b, gcd a b ∣ b) (dvd_gcd : ∀{a b c}, a ∣ c → a ∣ b → a ∣ gcd c b) (normalize_gcd : ∀a b, normalize (gcd a b) = gcd a b) : gcd_monoid α := { gcd := gcd, gcd_dvd_left := gcd_dvd_left, gcd_dvd_right := gcd_dvd_right, dvd_gcd := λ a b c, dvd_gcd, normalize_gcd := normalize_gcd, lcm := λ a b, if a = 0 then 0 else classical.some (dvd_normalize_iff.2 (dvd.trans (gcd_dvd_left a b) (dvd.intro b rfl))), gcd_mul_lcm := λ a b, by { split_ifs with a0, { rw [mul_zero, a0, zero_mul, normalize_zero] }, { exact (classical.some_spec (dvd_normalize_iff.2 (dvd.trans (gcd_dvd_left a b) (dvd.intro b rfl)))).symm } }, lcm_zero_left := λ a, if_pos rfl, lcm_zero_right := λ a, by { split_ifs with a0, { refl }, rw ← normalize_eq_zero at a0, have h := (classical.some_spec (dvd_normalize_iff.2 (dvd.trans (gcd_dvd_left a 0) (dvd.intro 0 rfl)))).symm, have gcd0 : gcd a 0 = normalize a, { rw ← normalize_gcd, exact normalize_eq_normalize (gcd_dvd_left _ _) (dvd_gcd (dvd_refl a) (dvd_zero a)) }, rw ← gcd0 at a0, apply or.resolve_left (mul_eq_zero.1 _) a0, rw [h, mul_zero, normalize_zero] }, .. (infer_instance : normalization_monoid α) } /-- Define `gcd_monoid` on a structure just from the `lcm` and its properties. -/ noncomputable def gcd_monoid_of_lcm [decidable_eq α] (lcm : α → α → α) (dvd_lcm_left : ∀a b, a ∣ lcm a b) (dvd_lcm_right : ∀a b, b ∣ lcm a b) (lcm_dvd : ∀{a b c}, c ∣ a → b ∣ a → lcm c b ∣ a) (normalize_lcm : ∀a b, normalize (lcm a b) = lcm a b) : gcd_monoid α := let exists_gcd := λ a b, dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl)) in { lcm := lcm, gcd := λ a b, if a = 0 then normalize b else (if b = 0 then normalize a else classical.some (exists_gcd a b)), gcd_mul_lcm := λ a b, by { split_ifs, { rw [h, zero_dvd_iff.1 (dvd_lcm_left _ _), mul_zero, zero_mul, normalize_zero] }, { rw [h_1, zero_dvd_iff.1 (dvd_lcm_right _ _), mul_zero, mul_zero, normalize_zero] }, apply eq.trans (mul_comm _ _) (classical.some_spec (dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl)))).symm }, normalize_gcd := λ a b, by { split_ifs, { apply normalize_idem }, { apply normalize_idem }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, apply mul_left_cancel' h0, refine trans _ (classical.some_spec (exists_gcd a b)), conv_lhs { congr, rw [← normalize_lcm a b] }, erw [← normalize.map_mul, ← classical.some_spec (exists_gcd a b), normalize_idem] }, lcm_zero_left := λ a, zero_dvd_iff.1 (dvd_lcm_left _ _), lcm_zero_right := λ a, zero_dvd_iff.1 (dvd_lcm_right _ _), gcd_dvd_left := λ a b, by { split_ifs, { rw h, apply dvd_zero }, { apply dvd_of_associated normalize_associated }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b), normalize_dvd_iff, mul_comm, mul_dvd_mul_iff_right h], apply dvd_lcm_right }, gcd_dvd_right := λ a b, by { split_ifs, { apply dvd_of_associated normalize_associated }, { rw h_1, apply dvd_zero }, have h0 : lcm a b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left a rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (exists_gcd a b), normalize_dvd_iff, mul_dvd_mul_iff_right h_1], apply dvd_lcm_left }, dvd_gcd := λ a b c ac ab, by { split_ifs, { apply dvd_normalize_iff.2 ab }, { apply dvd_normalize_iff.2 ac }, have h0 : lcm c b ≠ 0, { intro con, have h := lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl), rw [con, zero_dvd_iff, mul_eq_zero] at h, cases h; tauto }, rw [← mul_dvd_mul_iff_left h0, ← classical.some_spec (dvd_normalize_iff.2 (lcm_dvd (dvd.intro b rfl) (dvd.intro_left c rfl))), dvd_normalize_iff], rcases ab with ⟨d, rfl⟩, rw mul_eq_zero at h_1, push_neg at h_1, rw [mul_comm a, ← mul_assoc, mul_dvd_mul_iff_right h_1.1], apply lcm_dvd (dvd.intro d rfl), rw [mul_comm, mul_dvd_mul_iff_right h_1.2], apply ac }, .. (infer_instance : normalization_monoid α) } /-- Define a `gcd_monoid` structure on a monoid just from the existence of a `gcd`. -/ noncomputable def gcd_monoid_of_exists_gcd [decidable_eq α] (h : ∀ a b : α, ∃ c : α, ∀ d : α, d ∣ a ∧ d ∣ b ↔ d ∣ c) : gcd_monoid α := gcd_monoid_of_gcd (λ a b, normalize (classical.some (h a b))) (λ a b, normalize_dvd_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 (dvd_refl _))).1) (λ a b, normalize_dvd_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 (dvd_refl _))).2) (λ a b c ac ab, dvd_normalize_iff.2 ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩)) (λ a b, normalize_idem _) /-- Define a `gcd_monoid` structure on a monoid just from the existence of an `lcm`. -/ noncomputable def gcd_monoid_of_exists_lcm [decidable_eq α] (h : ∀ a b : α, ∃ c : α, ∀ d : α, a ∣ d ∧ b ∣ d ↔ c ∣ d) : gcd_monoid α := gcd_monoid_of_lcm (λ a b, normalize (classical.some (h a b))) (λ a b, dvd_normalize_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 (dvd_refl _))).1) (λ a b, dvd_normalize_iff.2 (((classical.some_spec (h a b) (classical.some (h a b))).2 (dvd_refl _))).2) (λ a b c ac ab, normalize_dvd_iff.2 ((classical.some_spec (h c b) a).1 ⟨ac, ab⟩)) (λ a b, normalize_idem _) /-- `ℕ` is a `gcd_monoid` -/ instance : gcd_monoid ℕ := { gcd := nat.gcd, lcm := nat.lcm, gcd_dvd_left := nat.gcd_dvd_left, gcd_dvd_right := nat.gcd_dvd_right, dvd_gcd := λ _ _ _, nat.dvd_gcd, normalize_gcd := λ a b, nat.mul_one (a.gcd b), gcd_mul_lcm := λ a b, (a.gcd_mul_lcm b).trans (mul_one (a * b)).symm, lcm_zero_left := nat.lcm_zero_left, lcm_zero_right := nat.lcm_zero_right, norm_unit := λ _, 1, norm_unit_zero := rfl, norm_unit_mul := λ _ _ _ _, rfl, norm_unit_coe_units := λ u, eq_inv_of_eq_inv (by rw [one_inv, units.ext_iff, units.coe_one, nat.is_unit_iff.mp u.is_unit]) } @[simp] lemma nat.normalize_eq (n : ℕ) : normalize n = n := n.mul_one lemma nat.gcd_eq_gcd (m n : ℕ) : gcd m n = nat.gcd m n := rfl lemma nat.lcm_eq_lcm (m n : ℕ) : lcm m n = nat.lcm m n := rfl end constructors
f188adb6e3b7e4542d743f2efaf0e75467deb9fa
63abd62053d479eae5abf4951554e1064a4c45b4
/src/analysis/normed_space/extend.lean
853aed234035d9247b6ec6fb4ba31bf2da49d817
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
5,535
lean
/- Copyright (c) 2020 Ruben Van de Velde. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ruben Van de Velde -/ import analysis.complex.basic import analysis.normed_space.operator_norm /-! # Extending a continuous ℝ-linear map to a continuous ℂ-linear map In this file we provide a way to extend a continuous ℝ-linear map to a continuous ℂ-linear map in a way that bounds the norm by the norm of the original map. We motivate the form of the extension as follows. Note that `fc : F →ₗ[ℂ] ℂ` is determined fully by `Re fc`: for all `x : F`, `fc (I • x) = I * fc x`, so `Im (fc x) = -Re (fc (I • x))`. Therefore, given an `fr : F →ₗ[ℝ] ℝ`, we define `fc x = fr x - fr (I • x) * I`. -/ open complex variables {F : Type*} [normed_group F] [normed_space ℂ F] /-- Extend `fr : F →ₗ[ℝ] ℝ` to `F →ₗ[ℂ] ℂ` in a way that will also be continuous and have its norm bounded by `∥fr∥` if `fr` is continuous. -/ noncomputable def linear_map.extend_to_ℂ (fr : F →ₗ[ℝ] ℝ) : F →ₗ[ℂ] ℂ := begin let fc : F → ℂ := λ x, ⟨fr x, -fr (I • x)⟩, have add : ∀ x y : F, fc (x + y) = fc x + fc y, { intros, ext; dsimp, { rw fr.map_add }, { rw [smul_add, fr.map_add, neg_add] } }, have smul_ℝ : ∀ (c : ℝ) (x : F), fc (c • x) = c * fc x, { intros, ext; dsimp, { rw [fr.map_smul, smul_eq_mul, zero_mul, sub_zero] }, { rw [zero_mul, add_zero], calc -fr (I • c • x) = -fr (I • (c : ℂ) • x) : rfl ... = -fr ((c : ℂ) • I • x) : by rw smul_comm ... = -fr (c • I • x) : rfl ... = -(c * fr (I • x)) : by rw [fr.map_smul, smul_eq_mul] ... = c * -fr (I • x) : by rw neg_mul_eq_mul_neg } }, have smul_I : ∀ x : F, fc (I • x) = I * fc x, { intros, have h : -fr (I • I • x) = fr x, { calc -fr (I • I • x) = -fr (((-1 : ℝ) : ℂ) • x) : by rw [←mul_smul, I_mul_I, of_real_neg, of_real_one] ... = -fr ((-1 : ℝ) • x) : rfl ... = -((-1 : ℝ) * fr x : ℝ) : by rw [fr.map_smul, smul_eq_mul] ... = fr x : by ring }, { calc fc (I • x) = ⟨fr (I • x), -fr (I • I • x)⟩ : rfl ... = ⟨fr (I • x), fr x⟩ : by rw h ... = I * ⟨fr x, -fr (I • x)⟩ : by rw [I_mul, neg_neg] ... = I * fc x : rfl } }, have smul_ℂ : ∀ (c : ℂ) (x : F), fc (c • x) = c • fc x, { intros, let a : ℂ := c.re, let b : ℂ := c.im, have h : fc ((b * I) • x) = b * I * fc x, { calc fc ((b * I) • x) = fc (b • I • x) : by rw mul_smul ... = fc (c.im • I • x) : rfl ... = b * fc (I • x) : by rw smul_ℝ ... = b * (I * fc x) : by rw smul_I ... = b * I * fc x : by rw mul_assoc }, calc fc (c • x) = fc ((a + b * I) • x) : by rw re_add_im ... = fc (a • x + (b * I) • x) : by rw add_smul ... = fc (a • x) + fc ((b * I) • x) : by rw add ... = fc (c.re • x) + fc ((b * I) • x) : rfl ... = a * fc x + fc ((b * I) • x) : by rw smul_ℝ ... = a * fc x + b * I * fc x : by rw h ... = (a + b * I) * fc x : by rw add_mul ... = c * fc x : by rw re_add_im c }, exact { to_fun := fc, map_add' := add, map_smul' := smul_ℂ } end /-- The norm of the extension is bounded by ∥fr∥. -/ lemma norm_bound (fr : F →L[ℝ] ℝ) : ∀ x : F, ∥fr.to_linear_map.extend_to_ℂ x∥ ≤ ∥fr∥ * ∥x∥ := begin intros, let lm := fr.to_linear_map.extend_to_ℂ, -- We aim to find a `t : ℂ` such that -- * `lm (t • x) = fr (t • x)` (so `lm (t • x) = t * lm x ∈ ℝ`) -- * `∥lm x∥ = ∥lm (t • x)∥` (so `t.abs` must be 1) -- If `lm x ≠ 0`, `(lm x)⁻¹` satisfies the first requirement, and after normalizing, it -- satisfies the second. -- (If `lm x = 0`, the goal is trivial.) classical, by_cases h : lm x = 0, { rw [h, norm_zero], apply mul_nonneg; exact norm_nonneg _ }, let fx := (lm x)⁻¹, let t := fx / fx.abs, have ht : t.abs = 1, { simp only [abs_of_real, of_real_inv, complex.abs_div, complex.abs_inv, complex.abs_abs, t, fx], have : abs (lm x) ≠ 0 := abs_ne_zero.mpr h, have : (abs (lm x))⁻¹ ≠ 0 := inv_ne_zero this, exact div_self this }, have h1 : (fr (t • x) : ℂ) = lm (t • x), { ext, { refl }, { transitivity (0 : ℝ), { rw of_real_im }, { symmetry, calc (lm (t • x)).im = (t * lm x).im : by rw [lm.map_smul, smul_eq_mul] ... = ((lm x)⁻¹ / (lm x)⁻¹.abs * lm x).im : rfl ... = (1 / (lm x)⁻¹.abs : ℂ).im : by rw [div_mul_eq_mul_div, inv_mul_cancel h] ... = 0 : by rw [←complex.of_real_one, ←of_real_div, of_real_im] } } }, calc ∥lm x∥ = t.abs * ∥lm x∥ : by rw [ht, one_mul] ... = ∥t * lm x∥ : by rw [normed_field.norm_mul, t.norm_eq_abs] ... = ∥lm (t • x)∥ : by rw [←smul_eq_mul, lm.map_smul] ... = ∥(fr (t • x) : ℂ)∥ : by rw h1 ... = ∥fr (t • x)∥ : by rw norm_real ... ≤ ∥fr∥ * ∥t • x∥ : continuous_linear_map.le_op_norm _ _ ... = ∥fr∥ * (∥t∥ * ∥x∥) : by rw norm_smul ... = ∥fr∥ * ∥x∥ : by rw [norm_eq_abs, ht, one_mul], end /-- Extend `fr : F →L[ℝ] ℝ` to `F →L[ℂ] ℂ`. -/ noncomputable def continuous_linear_map.extend_to_ℂ (fr : F →L[ℝ] ℝ) : F →L[ℂ] ℂ := fr.to_linear_map.extend_to_ℂ.mk_continuous ∥fr∥ (norm_bound _)
58726f123d339307a3cadb84066dc8524f735d7d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/simp_result.lean
42fd22cf8b08353420cbf535d28e3591b3fb2b62
[ "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,914
lean
import logic.equiv.defs import tactic.simp_result open tactic -- Check that we can walk. example : true := by { simp_result { trivial } } -- Comparison without `dsimp_result`: example : true := begin exact (id trivial), (do `(id trivial) ← result, skip), success_if_fail { (do `(trivial) ← result, skip) }, end -- Check that `dsimp_result` removes unnecessary `id`s. example : true := begin dsimp_result { exact (id trivial) }, success_if_fail { (do `(id trivial) ← result, skip) }, (do `(trivial) ← result, skip), end -- Comparison without `dsimp_result`: example (a : ℕ) (b : list ℕ) (h : b.length < a) : ℕ := begin revert a, intros a h, exact 0, (do `((λ (h : list.length _ < _), 0) _) ← result, skip), success_if_fail { (do `(0) ← result, skip) }, end -- Check that `dsimp_result` does beta-reductions after `revert`. example (a : ℕ) (b : list ℕ) (h : b.length < a) : ℕ := begin dsimp_result { revert a, intros a h, exact 0, }, success_if_fail { (do `((λ (h : list.length _ < _), 0) _) ← result, skip), }, (do `(0) ← result, skip), end -- This test tactic internally sets `pp.all ff`, and `pp.proofs tt`. -- This isn't very robust, as the user setting any other `pp` options -- will cause tests to break, but I don't think it needs to be. meta def guard_result_pp (s : string) : tactic unit := do o ← get_options, set_options ((o.set_bool `pp.all ff).set_bool `pp.proofs tt), r ← (to_string <$> (result >>= pp)), guard (r = s) <|> fail format!"result was {r} but expected {s}" -- Comparison without `simp_result`: example {α β : Type} (e : α ≃ β) (a : α) : β := begin exact e (e.symm (e a)), guard_result_pp "⇑e (⇑(equiv.symm e) (⇑e a))", end -- Check that `simp_result` applies non-definitional simplifications to the result. example {α β : Type} (e : α ≃ β) (a : α) : β := begin simp_result { exact e (e.symm (e a)) }, guard_result_pp "⇑e a", end -- Check that `simp_result only [...]` behaves as expected. example {α β : Type} (e : α ≃ β) (a : α) : β := begin simp_result only [equiv.apply_symm_apply] { exact e (e.symm (e a)) }, guard_result_pp "⇑e a", end -- Check that `simp_result only []` does not simplify. -- (Note the `simp_result` succeeds even if no simplification occurs.) example {α β : Type} (e : α ≃ β) (a : α) : β := begin simp_result only [] { exact e (e.symm (e a)) }, guard_result_pp "⇑e (⇑(equiv.symm e) (⇑e a))", end -- Comparison without `simp_result` example {α : Type} (a b : α) (h : a = b) : ℕ := begin subst h, exact 0, guard_result_pp "eq.rec 0 h", end -- Check that we can remove `eq.rec` transports through constant families -- introduced by irrelevant use of `subst`. example {α : Type} (a b : α) (h : a = b) : ℕ := begin simp_result only [eq_rec_constant] { subst h, exact 0, }, guard_result_pp "0", end -- Check that `simp_result` performs simplifications on all results. example : ℕ × ℕ := begin split, simp_result { exact id 0, exact id 1, }, guard_result_pp "(0, 1)", end -- Check that `simp_result` can cope with incomplete goals. example {α β : Type} (e : α ≃ β) (a : α) : β := begin simp_result { apply e.to_fun, apply e.inv_fun, apply e.to_fun, }, guard_result_pp "⇑e ?m_1", exact a, end -- Check that we can: -- * cope with metavariables in the result -- * perform beta redex after `revert` -- * simplify `eq.rec` after `subst` example {α β : Type} (e : α ≃ β) (S : has_mul α) : has_mul β := begin fconstructor, simp_result { have mul := S.mul, have e' := equiv.arrow_congr e (equiv.arrow_congr e e), have h : mul = e'.symm (e' mul) := by simp, revert h, generalize : e' mul = mul', intro h, subst h, }, exact mul', guard_result_pp "{mul := ⇑(equiv.arrow_congr e (equiv.arrow_congr e e)) has_mul.mul}", end
ca75195239d3b18744c73a4d8dc46a30252a72b3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/algebra/continuous_monoid_hom.lean
75e8053ff2cacc1c67186eba86f93d1cc0b0549a
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
15,431
lean
/- Copyright (c) 2022 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import analysis.complex.circle import topology.continuous_function.algebra /-! # Continuous Monoid Homs > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the space of continuous homomorphisms between two topological groups. ## Main definitions * `continuous_monoid_hom A B`: The continuous homomorphisms `A →* B`. * `continuous_add_monoid_hom A B`: The continuous additive homomorphisms `A →+ B`. -/ open_locale pointwise open function variables (F A B C D E : Type*) [monoid A] [monoid B] [monoid C] [monoid D] [comm_group E] [topological_space A] [topological_space B] [topological_space C] [topological_space D] [topological_space E] [topological_group E] /-- The type of continuous additive monoid homomorphisms from `A` to `B`. When possible, instead of parametrizing results over `(f : continuous_add_monoid_hom A B)`, you should parametrize over `(F : Type*) [continuous_add_monoid_hom_class F A B] (f : F)`. When you extend this structure, make sure to extend `continuous_add_monoid_hom_class`. -/ structure continuous_add_monoid_hom (A B : Type*) [add_monoid A] [add_monoid B] [topological_space A] [topological_space B] extends A →+ B := (continuous_to_fun : continuous to_fun) /-- The type of continuous monoid homomorphisms from `A` to `B`. When possible, instead of parametrizing results over `(f : continuous_monoid_hom A B)`, you should parametrize over `(F : Type*) [continuous_monoid_hom_class F A B] (f : F)`. When you extend this structure, make sure to extend `continuous_add_monoid_hom_class`. -/ @[to_additive] structure continuous_monoid_hom extends A →* B := (continuous_to_fun : continuous to_fun) section set_option old_structure_cmd true /-- `continuous_add_monoid_hom_class F A B` states that `F` is a type of continuous additive monoid homomorphisms. You should also extend this typeclass when you extend `continuous_add_monoid_hom`. -/ class continuous_add_monoid_hom_class (A B : Type*) [add_monoid A] [add_monoid B] [topological_space A] [topological_space B] extends add_monoid_hom_class F A B := (map_continuous (f : F) : continuous f) /-- `continuous_monoid_hom_class F A B` states that `F` is a type of continuous additive monoid homomorphisms. You should also extend this typeclass when you extend `continuous_monoid_hom`. -/ @[to_additive] class continuous_monoid_hom_class extends monoid_hom_class F A B := (map_continuous (f : F) : continuous f) attribute [to_additive continuous_add_monoid_hom_class.to_add_monoid_hom_class] continuous_monoid_hom_class.to_monoid_hom_class end /-- Reinterpret a `continuous_monoid_hom` as a `monoid_hom`. -/ add_decl_doc continuous_monoid_hom.to_monoid_hom /-- Reinterpret a `continuous_add_monoid_hom` as an `add_monoid_hom`. -/ add_decl_doc continuous_add_monoid_hom.to_add_monoid_hom @[priority 100, to_additive] -- See note [lower instance priority] instance continuous_monoid_hom_class.to_continuous_map_class [continuous_monoid_hom_class F A B] : continuous_map_class F A B := { .. ‹continuous_monoid_hom_class F A B› } namespace continuous_monoid_hom variables {A B C D E} @[to_additive] instance : continuous_monoid_hom_class (continuous_monoid_hom A B) A B := { coe := λ f, f.to_fun, coe_injective' := λ f g h, by { obtain ⟨⟨_, _⟩, _⟩ := f, obtain ⟨⟨_, _⟩, _⟩ := g, congr' }, map_mul := λ f, f.map_mul', map_one := λ f, f.map_one', map_continuous := λ f, f.continuous_to_fun } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ @[to_additive "Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly."] instance : has_coe_to_fun (continuous_monoid_hom A B) (λ _, A → B) := fun_like.has_coe_to_fun @[to_additive, ext] lemma ext {f g : continuous_monoid_hom A B} (h : ∀ x, f x = g x) : f = g := fun_like.ext _ _ h /-- Reinterpret a `continuous_monoid_hom` as a `continuous_map`. -/ @[to_additive "Reinterpret a `continuous_add_monoid_hom` as a `continuous_map`."] def to_continuous_map (f : continuous_monoid_hom A B) : C(A, B) := { .. f} @[to_additive] lemma to_continuous_map_injective : injective (to_continuous_map : _ → C(A, B)) := λ f g h, ext $ by convert fun_like.ext_iff.1 h /-- Construct a `continuous_monoid_hom` from a `continuous` `monoid_hom`. -/ @[to_additive "Construct a `continuous_add_monoid_hom` from a `continuous` `add_monoid_hom`.", simps] def mk' (f : A →* B) (hf : continuous f) : continuous_monoid_hom A B := { continuous_to_fun := hf, .. f } /-- Composition of two continuous homomorphisms. -/ @[to_additive "Composition of two continuous homomorphisms.", simps] def comp (g : continuous_monoid_hom B C) (f : continuous_monoid_hom A B) : continuous_monoid_hom A C := mk' (g.to_monoid_hom.comp f.to_monoid_hom) (g.continuous_to_fun.comp f.continuous_to_fun) /-- Product of two continuous homomorphisms on the same space. -/ @[to_additive "Product of two continuous homomorphisms on the same space.", simps] def prod (f : continuous_monoid_hom A B) (g : continuous_monoid_hom A C) : continuous_monoid_hom A (B × C) := mk' (f.to_monoid_hom.prod g.to_monoid_hom) (f.continuous_to_fun.prod_mk g.continuous_to_fun) /-- Product of two continuous homomorphisms on different spaces. -/ @[to_additive "Product of two continuous homomorphisms on different spaces.", simps] def prod_map (f : continuous_monoid_hom A C) (g : continuous_monoid_hom B D) : continuous_monoid_hom (A × B) (C × D) := mk' (f.to_monoid_hom.prod_map g.to_monoid_hom) (f.continuous_to_fun.prod_map g.continuous_to_fun) variables (A B C D E) /-- The trivial continuous homomorphism. -/ @[to_additive "The trivial continuous homomorphism.", simps] def one : continuous_monoid_hom A B := mk' 1 continuous_const @[to_additive] instance : inhabited (continuous_monoid_hom A B) := ⟨one A B⟩ /-- The identity continuous homomorphism. -/ @[to_additive "The identity continuous homomorphism.", simps] def id : continuous_monoid_hom A A := mk' (monoid_hom.id A) continuous_id /-- The continuous homomorphism given by projection onto the first factor. -/ @[to_additive "The continuous homomorphism given by projection onto the first factor.", simps] def fst : continuous_monoid_hom (A × B) A := mk' (monoid_hom.fst A B) continuous_fst /-- The continuous homomorphism given by projection onto the second factor. -/ @[to_additive "The continuous homomorphism given by projection onto the second factor.", simps] def snd : continuous_monoid_hom (A × B) B := mk' (monoid_hom.snd A B) continuous_snd /-- The continuous homomorphism given by inclusion of the first factor. -/ @[to_additive "The continuous homomorphism given by inclusion of the first factor.", simps] def inl : continuous_monoid_hom A (A × B) := prod (id A) (one A B) /-- The continuous homomorphism given by inclusion of the second factor. -/ @[to_additive "The continuous homomorphism given by inclusion of the second factor.", simps] def inr : continuous_monoid_hom B (A × B) := prod (one B A) (id B) /-- The continuous homomorphism given by the diagonal embedding. -/ @[to_additive "The continuous homomorphism given by the diagonal embedding.", simps] def diag : continuous_monoid_hom A (A × A) := prod (id A) (id A) /-- The continuous homomorphism given by swapping components. -/ @[to_additive "The continuous homomorphism given by swapping components.", simps] def swap : continuous_monoid_hom (A × B) (B × A) := prod (snd A B) (fst A B) /-- The continuous homomorphism given by multiplication. -/ @[to_additive "The continuous homomorphism given by addition.", simps] def mul : continuous_monoid_hom (E × E) E := mk' mul_monoid_hom continuous_mul /-- The continuous homomorphism given by inversion. -/ @[to_additive "The continuous homomorphism given by negation.", simps] def inv : continuous_monoid_hom E E := mk' inv_monoid_hom continuous_inv variables {A B C D E} /-- Coproduct of two continuous homomorphisms to the same space. -/ @[to_additive "Coproduct of two continuous homomorphisms to the same space.", simps] def coprod (f : continuous_monoid_hom A E) (g : continuous_monoid_hom B E) : continuous_monoid_hom (A × B) E := (mul E).comp (f.prod_map g) @[to_additive] instance : comm_group (continuous_monoid_hom A E) := { mul := λ f g, (mul E).comp (f.prod g), mul_comm := λ f g, ext (λ x, mul_comm (f x) (g x)), mul_assoc := λ f g h, ext (λ x, mul_assoc (f x) (g x) (h x)), one := one A E, one_mul := λ f, ext (λ x, one_mul (f x)), mul_one := λ f, ext (λ x, mul_one (f x)), inv := λ f, (inv E).comp f, mul_left_inv := λ f, ext (λ x, mul_left_inv (f x)) } @[to_additive] instance : topological_space (continuous_monoid_hom A B) := topological_space.induced to_continuous_map continuous_map.compact_open variables (A B C D E) @[to_additive] lemma inducing_to_continuous_map : inducing (to_continuous_map : continuous_monoid_hom A B → C(A, B)) := ⟨rfl⟩ @[to_additive] lemma embedding_to_continuous_map : embedding (to_continuous_map : continuous_monoid_hom A B → C(A, B)) := ⟨inducing_to_continuous_map A B, to_continuous_map_injective⟩ @[to_additive] lemma closed_embedding_to_continuous_map [has_continuous_mul B] [t2_space B] : closed_embedding (to_continuous_map : continuous_monoid_hom A B → C(A, B)) := ⟨embedding_to_continuous_map A B, ⟨begin suffices : (set.range (to_continuous_map : continuous_monoid_hom A B → C(A, B))) = ({f | f '' {1} ⊆ {1}ᶜ} ∪ ⋃ (x y) (U V W) (hU : is_open U) (hV : is_open V) (hW : is_open W) (h : disjoint (U * V) W), {f | f '' {x} ⊆ U} ∩ {f | f '' {y} ⊆ V} ∩ {f | f '' {x * y} ⊆ W})ᶜ, { rw [this, compl_compl], refine (continuous_map.is_open_gen is_compact_singleton is_open_compl_singleton).union _, repeat { apply is_open_Union, intro, }, repeat { apply is_open.inter }, all_goals { apply continuous_map.is_open_gen is_compact_singleton, assumption } }, simp_rw [set.compl_union, set.compl_Union, set.image_singleton, set.singleton_subset_iff, set.ext_iff, set.mem_inter_iff, set.mem_Inter, set.mem_compl_iff], refine λ f, ⟨_, _⟩, { rintros ⟨f, rfl⟩, exact ⟨λ h, h (map_one f), λ x y U V W hU hV hW h ⟨⟨hfU, hfV⟩, hfW⟩, h.le_bot ⟨set.mul_mem_mul hfU hfV, (congr_arg (∈ W) (map_mul f x y)).mp hfW⟩⟩ }, { rintros ⟨hf1, hf2⟩, suffices : ∀ x y, f (x * y) = f x * f y, { refine ⟨({ map_one' := of_not_not hf1, map_mul' := this, .. f } : continuous_monoid_hom A B), continuous_map.ext (λ _, rfl)⟩, }, intros x y, contrapose! hf2, obtain ⟨UV, W, hUV, hW, hfUV, hfW, h⟩ := t2_separation hf2.symm, have hB := @continuous_mul B _ _ _, obtain ⟨U, V, hU, hV, hfU, hfV, h'⟩ := is_open_prod_iff.mp (hUV.preimage hB) (f x) (f y) hfUV, refine ⟨x, y, U, V, W, hU, hV, hW, h.mono_left _, ⟨hfU, hfV⟩, hfW⟩, rintros _ ⟨x, y, hx : (x, y).1 ∈ U, hy : (x, y).2 ∈ V, rfl⟩, exact h' ⟨hx, hy⟩ }, end⟩⟩ variables {A B C D E} @[to_additive] instance [t2_space B] : t2_space (continuous_monoid_hom A B) := (embedding_to_continuous_map A B).t2_space @[to_additive] instance : topological_group (continuous_monoid_hom A E) := let hi := inducing_to_continuous_map A E, hc := hi.continuous in { continuous_mul := hi.continuous_iff.mpr (continuous_mul.comp (continuous.prod_map hc hc)), continuous_inv := hi.continuous_iff.mpr (continuous_inv.comp hc) } @[to_additive] lemma continuous_of_continuous_uncurry {A : Type*} [topological_space A] (f : A → continuous_monoid_hom B C) (h : continuous (function.uncurry (λ x y, f x y))) : continuous f := (inducing_to_continuous_map _ _).continuous_iff.mpr (continuous_map.continuous_of_continuous_uncurry _ h) @[to_additive] lemma continuous_comp [locally_compact_space B] : continuous (λ f : continuous_monoid_hom A B × continuous_monoid_hom B C, f.2.comp f.1) := (inducing_to_continuous_map A C).continuous_iff.2 $ (continuous_map.continuous_comp'.comp ((inducing_to_continuous_map A B).prod_mk (inducing_to_continuous_map B C)).continuous) @[to_additive] lemma continuous_comp_left (f : continuous_monoid_hom A B) : continuous (λ g : continuous_monoid_hom B C, g.comp f) := (inducing_to_continuous_map A C).continuous_iff.2 $ f.to_continuous_map.continuous_comp_left.comp (inducing_to_continuous_map B C).continuous @[to_additive] lemma continuous_comp_right (f : continuous_monoid_hom B C) : continuous (λ g : continuous_monoid_hom A B, f.comp g) := (inducing_to_continuous_map A C).continuous_iff.2 $ f.to_continuous_map.continuous_comp.comp (inducing_to_continuous_map A B).continuous variables (E) /-- `continuous_monoid_hom _ f` is a functor. -/ @[to_additive "`continuous_add_monoid_hom _ f` is a functor."] def comp_left (f : continuous_monoid_hom A B) : continuous_monoid_hom (continuous_monoid_hom B E) (continuous_monoid_hom A E) := { to_fun := λ g, g.comp f, map_one' := rfl, map_mul' := λ g h, rfl, continuous_to_fun := f.continuous_comp_left } variables (A) {E} /-- `continuous_monoid_hom f _` is a functor. -/ @[to_additive "`continuous_add_monoid_hom f _` is a functor."] def comp_right {B : Type*} [comm_group B] [topological_space B] [topological_group B] (f : continuous_monoid_hom B E) : continuous_monoid_hom (continuous_monoid_hom A B) (continuous_monoid_hom A E) := { to_fun := λ g, f.comp g, map_one' := ext (λ a, map_one f), map_mul' := λ g h, ext (λ a, map_mul f (g a) (h a)), continuous_to_fun := f.continuous_comp_right } end continuous_monoid_hom /-- The Pontryagin dual of `A` is the group of continuous homomorphism `A → circle`. -/ @[derive [topological_space, t2_space, comm_group, topological_group, inhabited]] def pontryagin_dual := continuous_monoid_hom A circle variables {A B C D E} namespace pontryagin_dual open continuous_monoid_hom noncomputable instance : continuous_monoid_hom_class (pontryagin_dual A) A circle := continuous_monoid_hom.continuous_monoid_hom_class /-- `pontryagin_dual` is a functor. -/ noncomputable def map (f : continuous_monoid_hom A B) : continuous_monoid_hom (pontryagin_dual B) (pontryagin_dual A) := f.comp_left circle @[simp] lemma map_apply (f : continuous_monoid_hom A B) (x : pontryagin_dual B) (y : A) : map f x y = x (f y) := rfl @[simp] lemma map_one : map (one A B) = one (pontryagin_dual B) (pontryagin_dual A) := ext (λ x, ext (λ y, map_one x)) @[simp] lemma map_comp (g : continuous_monoid_hom B C) (f : continuous_monoid_hom A B) : map (comp g f) = comp (map f) (map g) := ext (λ x, ext (λ y, rfl)) @[simp] lemma map_mul (f g : continuous_monoid_hom A E) : map (f * g) = map f * map g := ext (λ x, ext (λ y, map_mul x (f y) (g y))) variables (A B C D E) /-- `continuous_monoid_hom.dual` as a `continuous_monoid_hom`. -/ noncomputable def map_hom [locally_compact_space E] : continuous_monoid_hom (continuous_monoid_hom A E) (continuous_monoid_hom (pontryagin_dual E) (pontryagin_dual A)) := { to_fun := map, map_one' := map_one, map_mul' := map_mul, continuous_to_fun := continuous_of_continuous_uncurry _ continuous_comp } end pontryagin_dual
c0392031a2e2e7b296e9e60ef8224129ce697820
b00eb947a9c4141624aa8919e94ce6dcd249ed70
/src/Init/Classical.lean
138c3b19843cd8cc441ae72bfc33eb3623006804
[ "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
5,364
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Core import Init.NotationExtra universes u v /- Classical reasoning support -/ namespace Classical axiom choice {α : Sort u} : Nonempty α → α noncomputable def indefiniteDescription {α : Sort u} (p : α → Prop) (h : ∃ x, p x) : {x // p x} := choice <| let ⟨x, px⟩ := h; ⟨⟨x, px⟩⟩ noncomputable def choose {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : α := (indefiniteDescription p h).val theorem chooseSpec {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : p (choose h) := (indefiniteDescription p h).property /- Diaconescu's theorem: excluded middle from choice, Function extensionality and propositional extensionality. -/ theorem em (p : Prop) : p ∨ ¬p := let U (x : Prop) : Prop := x = True ∨ p; let V (x : Prop) : Prop := x = False ∨ p; have exU : ∃ x, U x := ⟨True, Or.inl rfl⟩; have exV : ∃ x, V x := ⟨False, Or.inl rfl⟩; let u : Prop := choose exU; let v : Prop := choose exV; have uDef : U u := chooseSpec exU; have vDef : V v := chooseSpec exV; have notUvOrP : u ≠ v ∨ p := match uDef, vDef with | Or.inr h, _ => Or.inr h | _, Or.inr h => Or.inr h | Or.inl hut, Or.inl hvf => have hne : u ≠ v := hvf.symm ▸ hut.symm ▸ trueNeFalse Or.inl hne have pImpliesUv : p → u = v := fun hp => have hpred : U = V := funext fun x => have hl : (x = True ∨ p) → (x = False ∨ p) := fun a => Or.inr hp; have hr : (x = False ∨ p) → (x = True ∨ p) := fun a => Or.inr hp; show (x = True ∨ p) = (x = False ∨ p) from propext (Iff.intro hl hr); have h₀ : ∀ exU exV, @choose _ U exU = @choose _ V exV := hpred ▸ fun exU exV => rfl; show u = v from h₀ ..; match notUvOrP with | Or.inl hne => Or.inr (mt pImpliesUv hne) | Or.inr h => Or.inl h theorem existsTrueOfNonempty {α : Sort u} : Nonempty α → ∃ x : α, True | ⟨x⟩ => ⟨x, trivial⟩ noncomputable def inhabitedOfNonempty {α : Sort u} (h : Nonempty α) : Inhabited α := ⟨choice h⟩ noncomputable def inhabitedOfExists {α : Sort u} {p : α → Prop} (h : ∃ x, p x) : Inhabited α := inhabitedOfNonempty (Exists.elim h (fun w hw => ⟨w⟩)) /- all propositions are Decidable -/ noncomputable scoped instance (priority := low) propDecidable (a : Prop) : Decidable a := choice <| match em a with | Or.inl h => ⟨isTrue h⟩ | Or.inr h => ⟨isFalse h⟩ noncomputable def decidableInhabited (a : Prop) : Inhabited (Decidable a) where default := inferInstance noncomputable def typeDecidableEq (α : Sort u) : DecidableEq α := fun x y => inferInstance noncomputable def typeDecidable (α : Sort u) : PSum α (α → False) := match (propDecidable (Nonempty α)) with | (isTrue hp) => PSum.inl (@arbitrary _ (inhabitedOfNonempty hp)) | (isFalse hn) => PSum.inr (fun a => absurd (Nonempty.intro a) hn) noncomputable def strongIndefiniteDescription {α : Sort u} (p : α → Prop) (h : Nonempty α) : {x : α // (∃ y : α, p y) → p x} := @dite _ (∃ x : α, p x) (propDecidable _) (fun (hp : ∃ x : α, p x) => show {x : α // (∃ y : α, p y) → p x} from let xp := indefiniteDescription _ hp; ⟨xp.val, fun h' => xp.property⟩) (fun hp => ⟨choice h, fun h => absurd h hp⟩) /- the Hilbert epsilon Function -/ noncomputable def epsilon {α : Sort u} [h : Nonempty α] (p : α → Prop) : α := (strongIndefiniteDescription p h).val theorem epsilonSpecAux {α : Sort u} (h : Nonempty α) (p : α → Prop) : (∃ y, p y) → p (@epsilon α h p) := (strongIndefiniteDescription p h).property theorem epsilonSpec {α : Sort u} {p : α → Prop} (hex : ∃ y, p y) : p (@epsilon α (nonemptyOfExists hex) p) := epsilonSpecAux (nonemptyOfExists hex) p hex theorem epsilonSingleton {α : Sort u} (x : α) : @epsilon α ⟨x⟩ (fun y => y = x) = x := @epsilonSpec α (fun y => y = x) ⟨x, rfl⟩ /- the axiom of choice -/ theorem axiomOfChoice {α : Sort u} {β : α → Sort v} {r : ∀ x, β x → Prop} (h : ∀ x, ∃ y, r x y) : ∃ (f : ∀ x, β x), ∀ x, r x (f x) := ⟨_, fun x => chooseSpec (h x)⟩ theorem skolem {α : Sort u} {b : α → Sort v} {p : ∀ x, b x → Prop} : (∀ x, ∃ y, p x y) ↔ ∃ (f : ∀ x, b x), ∀ x, p x (f x) := ⟨axiomOfChoice, fun ⟨f, hw⟩ (x) => ⟨f x, hw x⟩⟩ theorem propComplete (a : Prop) : a = True ∨ a = False := by cases em a with | inl _ => apply Or.inl; apply propext; apply Iff.intro; { intros; apply True.intro }; { intro; assumption } | inr hn => apply Or.inr; apply propext; apply Iff.intro; { intro h; exact hn h }; { intro h; apply False.elim h } -- this supercedes byCases in Decidable theorem byCases {p q : Prop} (hpq : p → q) (hnpq : ¬p → q) : q := Decidable.byCases (dec := propDecidable _) hpq hnpq -- this supercedes byContradiction in Decidable theorem byContradiction {p : Prop} (h : ¬p → False) : p := Decidable.byContradiction (dec := propDecidable _) h macro "byCases" h:ident ":" e:term : tactic => `(cases em $e:term with | inl $h:ident => _ | inr $h:ident => _) end Classical
c948cf45309a7e2aac80deb89f002818ddaeef7c
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/match1.lean
24711c904d3cb235f0787b7c95a3f7a81301f993
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,945
lean
new_frontend #print "---- h1" def h1 (b : Bool) : Nat := match b with | true => 0 | false => 10 #eval h1 false #print "---- h2" def h2 (x : List Nat) : Nat := match x with | [x1, x2] => x1 + x2 | x::xs => x | _ => 0 #eval h2 [1, 2] #eval h2 [10, 4, 5] #eval h2 [] #print "---- h3" def h3 (x : Array Nat) : Nat := match x with | #[x] => x | #[x, y] => x + y | xs => xs.size #eval h3 #[10] #eval h3 #[10, 20] #eval h3 #[10, 20, 30, 40] #print "---- inv" inductive Image {α β : Type} (f : α → β) : β → Type | mk (a : α) : Image f (f a) def mkImage {α β : Type} (f : α → β) (a : α) : Image f (f a) := Image.mk a def inv {α β : Type} {f : α → β} {b : β} (t : Image f b) : α := match b, t with | _, Image.mk a => a #eval inv (mkImage Nat.succ 10) theorem foo {p q} (h : p ∨ q) : q ∨ p := match h with | Or.inl h => Or.inr h | Or.inr h => Or.inl h def f (x : Nat × Nat) : Bool × Bool × Bool → Nat := match x with | (a, b) => fun _ => a structure S := (x y z : Nat := 0) def f1 : S → S := fun { x := x, ..} => { y := x } theorem ex2 : f1 { x := 10 } = { y := 10 } := rfl universes u inductive Vec (α : Type u) : Nat → Type u | nil : Vec α 0 | cons {n} (head : α) (tail : Vec α n) : Vec α (n+1) inductive VecPred {α : Type u} (P : α → Prop) : {n : Nat} → Vec α n → Prop | nil : VecPred P Vec.nil | cons {n : Nat} {head : α} {tail : Vec α n} : P head → VecPred P tail → VecPred P (Vec.cons head tail) theorem ex3 {α : Type u} (P : α → Prop) : {n : Nat} → (v : Vec α (n+1)) → VecPred P v → Exists P | _, Vec.cons head _, VecPred.cons h _ => ⟨head, h⟩ theorem ex4 {α : Type u} (P : α → Prop) : {n : Nat} → (v : Vec α (n+1)) → VecPred P v → Exists P | _, Vec.cons head _, VecPred.cons h (w : VecPred P Vec.nil) => ⟨head, h⟩ -- ERROR axiom someNat : Nat noncomputable def f2 (x : Nat) := -- must mark as noncomputable since it uses axiom `someNat` x + someNat inductive Parity : Nat -> Type | even (n) : Parity (n + n) | odd (n) : Parity (Nat.succ (n + n)) axiom nDiv2 (n : Nat) : n % 2 = 0 → n = n/2 + n/2 axiom nDiv2Succ (n : Nat) : n % 2 ≠ 0 → n = Nat.succ (n/2 + n/2) def parity (n : Nat) : Parity n := if h : n % 2 = 0 then Eq.ndrec (Parity.even (n/2)) (nDiv2 n h).symm else Eq.ndrec (Parity.odd (n/2)) (nDiv2Succ n h).symm partial def natToBin : (n : Nat) → List Bool | 0 => [] | n => match n, parity n with | _, Parity.even j => false :: natToBin j | _, Parity.odd j => true :: natToBin j #eval natToBin 6 partial def natToBinBad (n : Nat) : List Bool := match n, parity n with | 0, _ => [] | _, Parity.even j => false :: natToBin j | _, Parity.odd j => true :: natToBin j partial def natToBin2 (n : Nat) : List Bool := match n, parity n with | _, Parity.even 0 => [] | _, Parity.even j => false :: natToBin j | _, Parity.odd j => true :: natToBin j #eval natToBin2 6
bd35059e3f7796889c31059849c31dc96aed8feb
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/linear_algebra/lagrange.lean
816d64bb0804f83f56dd51bbb72781044d2c10d9
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
7,086
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import ring_theory.polynomial import algebra.big_operators.basic /-! # Lagrange interpolation ## Main definitions * `lagrange.basis s x` where `s : finset F` and `x : F`: the Lagrange basis polynomial that evaluates to `1` at `x` and `0` at other elements of `s`. * `lagrange.interpolate s f` where `s : finset F` and `f : s → F`: the Lagrange interpolant that evaluates to `f x` at `x` for `x ∈ s`. -/ noncomputable theory open_locale big_operators classical universe u namespace lagrange variables {F : Type u} [decidable_eq F] [field F] (s : finset F) variables {F' : Type u} [field F'] (s' : finset F') open polynomial /-- Lagrange basis polynomials that evaluate to 1 at `x` and 0 at other elements of `s`. -/ def basis (x : F) : polynomial F := ∏ y in s.erase x, C (x - y)⁻¹ * (X - C y) @[simp] theorem basis_empty (x : F) : basis ∅ x = 1 := rfl @[simp] theorem eval_basis_self (x : F) : (basis s x).eval x = 1 := begin rw [basis, ← (s.erase x).prod_hom (eval x), finset.prod_eq_one], intros y hy, simp_rw [eval_mul, eval_sub, eval_C, eval_X], exact inv_mul_cancel (sub_ne_zero_of_ne (finset.ne_of_mem_erase hy).symm) end @[simp] theorem eval_basis_ne (x y : F) (h1 : y ∈ s) (h2 : y ≠ x) : (basis s x).eval y = 0 := begin rw [basis, ← (s.erase x).prod_hom (eval y), finset.prod_eq_zero (finset.mem_erase.2 ⟨h2, h1⟩)], simp_rw [eval_mul, eval_sub, eval_C, eval_X, sub_self, mul_zero] end theorem eval_basis (x y : F) (h : y ∈ s) : (basis s x).eval y = if y = x then 1 else 0 := by { split_ifs with H, { subst H, apply eval_basis_self }, { exact eval_basis_ne s x y h H } } @[simp] theorem nat_degree_basis (x : F) (hx : x ∈ s) : (basis s x).nat_degree = s.card - 1 := begin unfold basis, generalize hsx : s.erase x = sx, have : x ∉ sx := hsx ▸ finset.not_mem_erase x s, rw [← finset.insert_erase hx, hsx, finset.card_insert_of_not_mem this, nat.add_sub_cancel], clear hx hsx s, revert this, apply sx.induction_on, { intros hx, rw [finset.prod_empty, nat_degree_one], refl }, { intros y s hys ih hx, rw [finset.mem_insert, not_or_distrib] at hx, have h1 : C (x - y)⁻¹ ≠ C 0 := λ h, hx.1 (eq_of_sub_eq_zero $ inv_eq_zero.1 $ C_inj.1 h), have h2 : X ^ 1 - C y ≠ 0 := by convert X_pow_sub_C_ne_zero zero_lt_one y, rw C_0 at h1, rw pow_one at h2, rw [finset.prod_insert hys, nat_degree_mul (mul_ne_zero h1 h2), ih hx.2, finset.card_insert_of_not_mem hys, nat_degree_mul h1 h2, nat_degree_C, zero_add, nat_degree, degree_X_sub_C, add_comm], refl, rw [ne, finset.prod_eq_zero_iff], rintro ⟨z, hzs, hz⟩, rw mul_eq_zero at hz, cases hz with hz hz, { rw [← C_0, C_inj, inv_eq_zero, sub_eq_zero] at hz, exact hx.2 (hz.symm ▸ hzs) }, { rw ← pow_one (X : polynomial F) at hz, exact X_pow_sub_C_ne_zero zero_lt_one _ hz } } end variables (f : (↑s : set F) → F) /-- Lagrange interpolation: given a finset `s` and a function `f : s → F`, `interpolate s f` is the unique polynomial of degree `< s.card` that takes value `f x` on all `x` in `s`. -/ def interpolate : polynomial F := ∑ x in s.attach, C (f x) * basis s x @[simp] theorem interpolate_empty (f) : interpolate (∅ : finset F) f = 0 := rfl @[simp] theorem eval_interpolate (x) (H : x ∈ s) : eval x (interpolate s f) = f ⟨x, H⟩ := begin rw [interpolate, ← finset.sum_hom _ (eval x), finset.sum_eq_single (⟨x, H⟩ : { x // x ∈ s })], { rw [eval_mul, eval_C, subtype.coe_mk, eval_basis_self, mul_one] }, { rintros ⟨y, hy⟩ _ hyx, rw [eval_mul, subtype.coe_mk, eval_basis_ne s y x H, mul_zero], { rintros rfl, exact hyx rfl } }, { intro h, exact absurd (finset.mem_attach _ _) h } end theorem degree_interpolate_lt : (interpolate s f).degree < s.card := if H : s = ∅ then by { subst H, rw [interpolate_empty, degree_zero], exact with_bot.bot_lt_coe _ } else (degree_sum_le _ _).trans_lt $ (finset.sup_lt_iff $ with_bot.bot_lt_coe s.card).2 $ λ b _, calc (C (f b) * basis s b).degree ≤ (C (f b)).degree + (basis s b).degree : degree_mul_le _ _ ... ≤ 0 + (basis s b).degree : add_le_add_right degree_C_le _ ... = (basis s b).degree : zero_add _ ... ≤ (basis s b).nat_degree : degree_le_nat_degree ... = (s.card - 1 : ℕ) : by { rw nat_degree_basis s b b.2 } ... < s.card : with_bot.coe_lt_coe.2 (nat.pred_lt $ mt finset.card_eq_zero.1 H) /-- Linear version of `interpolate`. -/ def linterpolate : ((↑s : set F) → F) →ₗ[F] polynomial F := { to_fun := interpolate s, map_add' := λ f g, by { simp_rw [interpolate, ← finset.sum_add_distrib, ← add_mul, ← C_add], refl }, map_smul' := λ c f, by { simp_rw [interpolate, finset.smul_sum, C_mul', smul_smul], refl } } @[simp] lemma interpolate_add (f g) : interpolate s (f + g) = interpolate s f + interpolate s g := (linterpolate s).map_add f g @[simp] lemma interpolate_zero : interpolate s 0 = 0 := (linterpolate s).map_zero @[simp] lemma interpolate_neg (f) : interpolate s (-f) = -interpolate s f := (linterpolate s).map_neg f @[simp] lemma interpolate_sub (f g) : interpolate s (f - g) = interpolate s f - interpolate s g := (linterpolate s).map_sub f g @[simp] lemma interpolate_smul (c : F) (f) : interpolate s (c • f) = c • interpolate s f := (linterpolate s).map_smul c f theorem eq_zero_of_eval_eq_zero {f : polynomial F'} (hf1 : f.degree < s'.card) (hf2 : ∀ x ∈ s', f.eval x = 0) : f = 0 := by_contradiction $ λ hf3, not_le_of_lt hf1 $ calc (s'.card : with_bot ℕ) ≤ f.roots.to_finset.card : with_bot.coe_le_coe.2 $ finset.card_le_of_subset $ λ x hx, (multiset.mem_to_finset).mpr $ (mem_roots hf3).2 $ hf2 x hx ... ≤ f.roots.card : with_bot.coe_le_coe.2 $ f.roots.to_finset_card_le ... ≤ f.degree : card_roots hf3 theorem eq_of_eval_eq {f g : polynomial F'} (hf : f.degree < s'.card) (hg : g.degree < s'.card) (hfg : ∀ x ∈ s', f.eval x = g.eval x) : f = g := eq_of_sub_eq_zero $ eq_zero_of_eval_eq_zero s' (lt_of_le_of_lt (degree_sub_le f g) $ max_lt hf hg) (λ x hx, by rw [eval_sub, hfg x hx, sub_self]) theorem eq_interpolate (f : polynomial F) (hf : f.degree < s.card) : interpolate s (λ x, f.eval x) = f := eq_of_eval_eq s (degree_interpolate_lt s _) hf $ λ x hx, eval_interpolate s _ x hx /-- Lagrange interpolation induces isomorphism between functions from `s` and polynomials of degree less than `s.card`. -/ def fun_equiv_degree_lt : degree_lt F s.card ≃ₗ[F] ((↑s : set F) → F) := { to_fun := λ f x, f.1.eval x, map_add' := λ f g, funext $ λ x, eval_add, map_smul' := λ c f, funext $ λ x, by { rw [pi.smul_apply, smul_eq_mul, ← @eval_C F c _ x, ← eval_mul, eval_C, C_mul'], refl }, inv_fun := λ f, ⟨interpolate s f, mem_degree_lt.2 $ degree_interpolate_lt s f⟩, left_inv := λ f, subtype.eq $ eq_interpolate s f $ mem_degree_lt.1 f.2, right_inv := λ f, funext $ λ ⟨x, hx⟩, eval_interpolate s f x hx } end lagrange
70544f1626bdbf3d4ced4e9c3bcd9e4a830ecc6c
46125763b4dbf50619e8846a1371029346f4c3db
/src/topology/metric_space/baire.lean
6acf1c22ce4fe1eafed655f2ae698085fb4a0ef5
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
15,219
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.metric_space.basic analysis.specific_limits /-! # Baire theorem In a complete metric space, a countable intersection of dense open subsets is dense. The good concept underlying the theorem is that of a Gδ set, i.e., a countable intersection of open sets. Then Baire theorem can also be formulated as the fact that a countable intersection of dense Gδ sets is a dense Gδ set. We prove Baire theorem, giving several different formulations that can be handy. We also prove the important consequence that, if the space is covered by a countable union of closed sets, then the union of their interiors is dense. The names of the theorems do not contain the string "Baire", but are instead built from the form of the statement. "Baire" is however in the docstring of all the theorems, to facilitate grep searches. -/ noncomputable theory open_locale classical open filter lattice encodable set variables {α : Type*} {β : Type*} {γ : Type*} section is_Gδ variable [topological_space α] /-- A Gδ set is a countable intersection of open sets. -/ def is_Gδ (s : set α) : Prop := ∃T : set (set α), (∀t ∈ T, is_open t) ∧ countable T ∧ s = (⋂₀ T) /-- An open set is a Gδ set. -/ lemma is_open.is_Gδ {s : set α} (h : is_open s) : is_Gδ s := ⟨{s}, by simp [h], countable_singleton _, (set.sInter_singleton _).symm⟩ lemma is_Gδ_bInter_of_open {ι : Type*} {I : set ι} (hI : countable I) {f : ι → set α} (hf : ∀i ∈ I, is_open (f i)) : is_Gδ (⋂i∈I, f i) := ⟨f '' I, by rwa ball_image_iff, countable_image _ hI, by rw sInter_image⟩ lemma is_Gδ_Inter_of_open {ι : Type*} [encodable ι] {f : ι → set α} (hf : ∀i, is_open (f i)) : is_Gδ (⋂i, f i) := ⟨range f, by rwa forall_range_iff, countable_range _, by rw sInter_range⟩ /-- A countable intersection of Gδ sets is a Gδ set. -/ lemma is_Gδ_sInter {S : set (set α)} (h : ∀s∈S, is_Gδ s) (hS : countable S) : is_Gδ (⋂₀ S) := begin have : ∀s : set α, ∃T : set (set α), s ∈ S → ((∀t ∈ T, is_open t) ∧ countable T ∧ s = (⋂₀ T)), { assume s, by_cases hs : s ∈ S, { simp [hs], exact h s hs }, { simp [hs] }}, choose T hT using this, refine ⟨⋃s∈S, T s, λt ht, _, _, _⟩, { simp only [exists_prop, set.mem_Union] at ht, rcases ht with ⟨s, hs, tTs⟩, exact (hT s hs).1 t tTs }, { exact countable_bUnion hS (λs hs, (hT s hs).2.1) }, { exact (sInter_bUnion (λs hs, (hT s hs).2.2)).symm } end /-- The union of two Gδ sets is a Gδ set. -/ lemma is_Gδ.union {s t : set α} (hs : is_Gδ s) (ht : is_Gδ t) : is_Gδ (s ∪ t) := begin rcases hs with ⟨S, Sopen, Scount, sS⟩, rcases ht with ⟨T, Topen, Tcount, tT⟩, rw [sS, tT, sInter_union_sInter], apply is_Gδ_bInter_of_open (countable_prod Scount Tcount), rintros ⟨a, b⟩ hab, simp only [set.prod_mk_mem_set_prod_eq] at hab, have aopen : is_open a := Sopen a hab.1, have bopen : is_open b := Topen b hab.2, simp [aopen, bopen, is_open_union] end end is_Gδ section Baire_theorem open metric variables [metric_space α] [complete_space α] /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here when the source space is ℕ (and subsumed below by `dense_Inter_of_open` working with any encodable source space). -/ theorem dense_Inter_of_open_nat {f : ℕ → set α} (ho : ∀n, is_open (f n)) (hd : ∀n, closure (f n) = univ) : closure (⋂n, f n) = univ := begin let B : ℕ → ℝ := λn, ((1/2)^n : ℝ), have Bpos : ∀n, 0 < B n := λn, begin apply pow_pos, by norm_num end, /- Translate the density assumption into two functions `center` and `radius` associating to any n, x, δ, δpos a center and a positive radius such that `closed_ball center radius` is included both in `f n` and in `closed_ball x δ`. We can also require `radius ≤ (1/2)^(n+1), to ensure we get a Cauchy sequence later. -/ have : ∀n x δ, ∃y r, δ > 0 → (r > 0 ∧ r ≤ B (n+1) ∧ closed_ball y r ⊆ (closed_ball x δ) ∩ f n), { assume n x δ, by_cases δpos : δ > 0, { have : x ∈ closure (f n) := by simpa only [(hd n).symm] using mem_univ x, rcases metric.mem_closure_iff.1 this (δ/2) (half_pos δpos) with ⟨y, ys, xy⟩, rw dist_comm at xy, rcases is_open_iff.1 (ho n) y ys with ⟨r, rpos, hr⟩, refine ⟨y, min (min (δ/2) (r/2)) (B (n+1)), λ_, ⟨_, _, λz hz, ⟨_, _⟩⟩⟩, show 0 < min (min (δ / 2) (r/2)) (B (n+1)), from lt_min (lt_min (half_pos δpos) (half_pos rpos)) (Bpos (n+1)), show min (min (δ / 2) (r/2)) (B (n+1)) ≤ B (n+1), from min_le_right _ _, show z ∈ closed_ball x δ, from calc dist z x ≤ dist z y + dist y x : dist_triangle _ _ _ ... ≤ (min (min (δ / 2) (r/2)) (B (n+1))) + (δ/2) : add_le_add hz (le_of_lt xy) ... ≤ δ/2 + δ/2 : add_le_add (le_trans (min_le_left _ _) (min_le_left _ _)) (le_refl _) ... = δ : add_halves _, show z ∈ f n, from hr (calc dist z y ≤ min (min (δ / 2) (r/2)) (B (n+1)) : hz ... ≤ r/2 : le_trans (min_le_left _ _) (min_le_right _ _) ... < r : half_lt_self rpos) }, { use [x, 0] }}, choose center radius H using this, refine subset.antisymm (subset_univ _) (λx hx, _), refine metric.mem_closure_iff.2 (λε εpos, _), /- ε is positive. We have to find a point in the ball of radius ε around x belonging to all `f n`. For this, we construct inductively a sequence `F n = (c n, r n)` such that the closed ball `closed_ball (c n) (r n)` is included in the previous ball and in `f n`, and such that `r n` is small enough to ensure that `c n` is a Cauchy sequence. Then `c n` converges to a limit which belongs to all the `f n`. -/ let F : ℕ → (α × ℝ) := λn, nat.rec_on n (prod.mk x (min (ε/2) 1)) (λn p, prod.mk (center n p.1 p.2) (radius n p.1 p.2)), let c : ℕ → α := λn, (F n).1, let r : ℕ → ℝ := λn, (F n).2, have rpos : ∀n, r n > 0, { assume n, induction n with n hn, exact lt_min (half_pos εpos) (zero_lt_one), exact (H n (c n) (r n) hn).1 }, have rB : ∀n, r n ≤ B n, { assume n, induction n with n hn, exact min_le_right _ _, exact (H n (c n) (r n) (rpos n)).2.1 }, have incl : ∀n, closed_ball (c (n+1)) (r (n+1)) ⊆ (closed_ball (c n) (r n)) ∩ (f n) := λn, (H n (c n) (r n) (rpos n)).2.2, have cdist : ∀n, dist (c n) (c (n+1)) ≤ B n, { assume n, rw dist_comm, have A : c (n+1) ∈ closed_ball (c (n+1)) (r (n+1)) := mem_closed_ball_self (le_of_lt (rpos (n+1))), have I := calc closed_ball (c (n+1)) (r (n+1)) ⊆ closed_ball (c n) (r n) : subset.trans (incl n) (inter_subset_left _ _) ... ⊆ closed_ball (c n) (B n) : closed_ball_subset_closed_ball (rB n), exact I A }, have : cauchy_seq c, { refine cauchy_seq_of_le_geometric (1/2) 1 (by norm_num) (λn, _), rw one_mul, exact cdist n }, -- as the sequence `c n` is Cauchy in a complete space, it converges to a limit `y`. rcases cauchy_seq_tendsto_of_complete this with ⟨y, ylim⟩, -- this point `y` will be the desired point. We will check that it belongs to all -- `f n` and to `ball x ε`. use y, simp only [exists_prop, set.mem_Inter], have I : ∀n, ∀m ≥ n, closed_ball (c m) (r m) ⊆ closed_ball (c n) (r n), { assume n, refine nat.le_induction _ (λm hnm h, _), { exact subset.refl _ }, { exact subset.trans (incl m) (subset.trans (inter_subset_left _ _) h) }}, have yball : ∀n, y ∈ closed_ball (c n) (r n), { assume n, refine mem_of_closed_of_tendsto (by simp) ylim is_closed_ball _, simp only [filter.mem_at_top_sets, nonempty_of_inhabited, set.mem_preimage], exact ⟨n, λm hm, I n m hm (mem_closed_ball_self (le_of_lt (rpos m)))⟩ }, split, show ∀n, y ∈ f n, { assume n, have : closed_ball (c (n+1)) (r (n+1)) ⊆ f n := subset.trans (incl n) (inter_subset_right _ _), exact this (yball (n+1)) }, show dist x y < ε, from calc dist x y = dist y x : dist_comm _ _ ... ≤ r 0 : yball 0 ... < ε : lt_of_le_of_lt (min_le_left _ _) (half_lt_self εpos) end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with ⋂₀. -/ theorem dense_sInter_of_open {S : set (set α)} (ho : ∀s∈S, is_open s) (hS : countable S) (hd : ∀s∈S, closure s = univ) : closure (⋂₀S) = univ := begin cases S.eq_empty_or_nonempty with h h, { simp [h] }, { rcases exists_surjective_of_countable h hS with ⟨f, hf⟩, have F : ∀n, f n ∈ S := λn, by rw hf; exact mem_range_self _, rw [hf, sInter_range], exact dense_Inter_of_open_nat (λn, ho _ (F n)) (λn, hd _ (F n)) } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_open {S : set β} {f : β → set α} (ho : ∀s∈S, is_open (f s)) (hS : countable S) (hd : ∀s∈S, closure (f s) = univ) : closure (⋂s∈S, f s) = univ := begin rw ← sInter_image, apply dense_sInter_of_open, { rwa ball_image_iff }, { exact countable_image _ hS }, { rwa ball_image_iff } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_open [encodable β] {f : β → set α} (ho : ∀s, is_open (f s)) (hd : ∀s, closure (f s) = univ) : closure (⋂s, f s) = univ := begin rw ← sInter_range, apply dense_sInter_of_open, { rwa forall_range_iff }, { exact countable_range _ }, { rwa forall_range_iff } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with ⋂₀. -/ theorem dense_sInter_of_Gδ {S : set (set α)} (ho : ∀s∈S, is_Gδ s) (hS : countable S) (hd : ∀s∈S, closure s = univ) : closure (⋂₀S) = univ := begin -- the result follows from the result for a countable intersection of dense open sets, -- by rewriting each set as a countable intersection of open sets, which are of course dense. have : ∀s : set α, ∃T : set (set α), s ∈ S → ((∀t ∈ T, is_open t) ∧ countable T ∧ s = (⋂₀ T)), { assume s, by_cases hs : s ∈ S, { simp [hs], exact ho s hs }, { simp [hs] }}, choose T hT using this, have : ⋂₀ S = ⋂₀ (⋃s∈S, T s) := (sInter_bUnion (λs hs, (hT s hs).2.2)).symm, rw this, refine dense_sInter_of_open (λt ht, _) (countable_bUnion hS (λs hs, (hT s hs).2.1)) (λt ht, _), show is_open t, { simp only [exists_prop, set.mem_Union] at ht, rcases ht with ⟨s, hs, tTs⟩, exact (hT s hs).1 t tTs }, show closure t = univ, { simp only [exists_prop, set.mem_Union] at ht, rcases ht with ⟨s, hs, tTs⟩, apply subset.antisymm (subset_univ _), rw ← (hd s hs), apply closure_mono, have := sInter_subset_of_mem tTs, rwa ← (hT s hs).2.2 at this } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_Gδ {S : set β} {f : β → set α} (ho : ∀s∈S, is_Gδ (f s)) (hS : countable S) (hd : ∀s∈S, closure (f s) = univ) : closure (⋂s∈S, f s) = univ := begin rw ← sInter_image, apply dense_sInter_of_Gδ, { rwa ball_image_iff }, { exact countable_image _ hS }, { rwa ball_image_iff } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_Gδ [encodable β] {f : β → set α} (ho : ∀s, is_Gδ (f s)) (hd : ∀s, closure (f s) = univ) : closure (⋂s, f s) = univ := begin rw ← sInter_range, apply dense_sInter_of_Gδ, { rwa forall_range_iff }, { exact countable_range _ }, { rwa forall_range_iff } end /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bUnion_interior_of_closed {S : set β} {f : β → set α} (hc : ∀s∈S, is_closed (f s)) (hS : countable S) (hU : (⋃s∈S, f s) = univ) : closure (⋃s∈S, interior (f s)) = univ := begin let g := λs, - (frontier (f s)), have clos_g : closure (⋂s∈S, g s) = univ, { refine dense_bInter_of_open (λs hs, _) hS (λs hs, _), show is_open (g s), from is_open_compl_iff.2 is_closed_frontier, show closure (g s) = univ, { apply subset.antisymm (subset_univ _), simp [g, interior_frontier (hc s hs)] }}, have : (⋂s∈S, g s) ⊆ (⋃s∈S, interior (f s)), { assume x hx, have : x ∈ ⋃s∈S, f s, { have := mem_univ x, rwa ← hU at this }, rcases mem_bUnion_iff.1 this with ⟨s, hs, xs⟩, have : x ∈ g s := mem_bInter_iff.1 hx s hs, have : x ∈ interior (f s), { have : x ∈ f s \ (frontier (f s)) := mem_inter xs this, simpa [frontier, xs, closure_eq_of_is_closed (hc s hs)] using this }, exact mem_bUnion_iff.2 ⟨s, ⟨hs, this⟩⟩ }, have := closure_mono this, rw clos_g at this, exact subset.antisymm (subset_univ _) this end /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with ⋃₀. -/ theorem dense_sUnion_interior_of_closed {S : set (set α)} (hc : ∀s∈S, is_closed s) (hS : countable S) (hU : (⋃₀ S) = univ) : closure (⋃s∈S, interior s) = univ := by rw sUnion_eq_bUnion at hU; exact dense_bUnion_interior_of_closed hc hS hU /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Union_interior_of_closed [encodable β] {f : β → set α} (hc : ∀s, is_closed (f s)) (hU : (⋃s, f s) = univ) : closure (⋃s, interior (f s)) = univ := begin rw ← bUnion_univ, apply dense_bUnion_interior_of_closed, { simp [hc] }, { apply countable_encodable }, { rwa ← bUnion_univ at hU } end /-- One of the most useful consequences of Baire theorem: if a countable union of closed sets covers the space, then one of the sets has nonempty interior. -/ theorem nonempty_interior_of_Union_of_closed [nonempty α] [encodable β] {f : β → set α} (hc : ∀s, is_closed (f s)) (hU : (⋃s, f s) = univ) : ∃s x ε, ε > 0 ∧ ball x ε ⊆ f s := begin have : ∃s, (interior (f s)).nonempty, { by_contradiction h, simp only [not_exists, not_nonempty_iff_eq_empty] at h, have := calc ∅ = closure (⋃s, interior (f s)) : by simp [h] ... = univ : dense_Union_interior_of_closed hc hU, exact univ_nonempty.ne_empty this.symm }, rcases this with ⟨s, hs⟩, rcases hs with ⟨x, hx⟩, rcases mem_nhds_iff.1 (mem_interior_iff_mem_nhds.1 hx) with ⟨ε, εpos, hε⟩, exact ⟨s, x, ε, εpos, hε⟩, end end Baire_theorem
3db33772d302bbab8599c92383359ba2f20c5276
e953c38599905267210b87fb5d82dcc3e52a4214
/hott/function.hlean
bf0bace4e98085e165db59d9a211e9e26e35149c
[ "Apache-2.0" ]
permissive
c-cube/lean
563c1020bff98441c4f8ba60111fef6f6b46e31b
0fb52a9a139f720be418dafac35104468e293b66
refs/heads/master
1,610,753,294,113
1,440,451,356,000
1,440,499,588,000
41,748,334
0
0
null
1,441,122,656,000
1,441,122,656,000
null
UTF-8
Lean
false
false
2,967
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Ported from Coq HoTT Theorems about embeddings and surjections -/ import hit.trunc types.equiv open equiv sigma sigma.ops eq trunc is_trunc pi is_equiv fiber prod variables {A B : Type} {f : A → B} {b : B} structure is_embedding [class] (f : A → B) := (elim : Π(a a' : A), is_equiv (ap f : a = a' → f a = f a')) structure is_surjective [class] (f : A → B) := (elim : Π(b : B), ∥ fiber f b ∥) structure is_split_surjective [class] (f : A → B) := (elim : Π(b : B), fiber f b) structure is_retraction [class] (f : A → B) := (sect : B → A) (right_inverse : Π(b : B), f (sect b) = b) namespace function attribute is_embedding.elim [instance] definition is_surjective_rec_on {P : Type} (H : is_surjective f) (b : B) [Pt : is_hprop P] (IH : fiber f b → P) : P := trunc.rec_on (is_surjective.elim f b) IH definition is_surjective_of_is_split_surjective [instance] [H : is_split_surjective f] : is_surjective f := is_surjective.mk (λb, tr (is_split_surjective.elim f b)) definition is_injective_of_is_embedding [reducible] [H : is_embedding f] {a a' : A} : f a = f a' → a = a' := (ap f)⁻¹ definition is_embedding_of_is_injective [HA : is_hset A] [HB : is_hset B] (H : Π(a a' : A), f a = f a' → a = a') : is_embedding f := begin fapply is_embedding.mk, intro a a', fapply adjointify, {exact (H a a')}, {intro p, apply is_hset.elim}, {intro p, apply is_hset.elim} end definition is_hprop_is_embedding [instance] (f : A → B) : is_hprop (is_embedding f) := begin have H : (Π(a a' : A), is_equiv (@ap A B f a a')) ≃ is_embedding f, begin fapply equiv.MK, {exact is_embedding.mk}, {intro h, cases h, exact elim}, {intro h, cases h, apply idp}, {intro p, apply idp}, end, apply is_trunc_equiv_closed, exact H, end definition is_hprop_is_surjective [instance] (f : A → B) : is_hprop (is_surjective f) := begin have H : (Π(b : B), merely (fiber f b)) ≃ is_surjective f, begin fapply equiv.MK, {exact is_surjective.mk}, {intro h, cases h, exact elim}, {intro h, cases h, apply idp}, {intro p, apply idp}, end, apply is_trunc_equiv_closed, exact H, end definition is_embedding_of_is_equiv [instance] (f : A → B) [H : is_equiv f] : is_embedding f := is_embedding.mk _ definition is_equiv_of_is_surjective_of_is_embedding (f : A → B) [H : is_embedding f] [H' : is_surjective f] : is_equiv f := @is_equiv_of_is_contr_fun _ _ _ (λb, is_surjective_rec_on H' b (λa, is_contr.mk a (λa', fiber_eq ((ap f)⁻¹ ((point_eq a) ⬝ (point_eq a')⁻¹)) (by rewrite (right_inv (ap f)); rewrite inv_con_cancel_right)))) end function
dce2cfb943e8c7140f78b2ae33d1572df5e2085f
3aad12fe82645d2d3173fbedc2e5c2ba945a4d75
/src/data/serial/string.lean
916b4a03cffdfd66accacb70d0d47b82adf6fcec
[]
no_license
seanpm2001/LeanProver-Community_MathLIB-Nursery
4f88d539cb18d73a94af983092896b851e6640b5
0479b31fa5b4d39f41e89b8584c9f5bf5271e8ec
refs/heads/master
1,688,730,786,645
1,572,070,026,000
1,572,070,026,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,401
lean
import data.serial.medium universes u namespace string.medium abbreviation put_m' := medium.put_m'.{u} char abbreviation put_m := medium.put_m'.{u} char punit abbreviation get_m := medium.get_m.{u} char export medium (hiding put_m put_m' get_m) namespace put_m' export medium.put_m' end put_m' namespace get_m export medium.get_m end get_m def emit (s : string) : put_m := s.to_list.mmap' write_word @[reducible] def reader := reader_t ℕ (state_t (option char) get_m) def reader.run {α} (x : reader α) (n : ℕ) : get_m α := do (i,j) ← (x.run n).run none, guard j.is_none, pure i def read_char : reader char := do some c ← get | ulift.down <$> monad_lift (monad_lift read_word : state_t (option char) get_m (ulift char)), put none, pure c def unread (c : char) : reader unit := do none ← get | failure, put (some c) def expect_char (c : char) : reader punit := do c' ← read_char, -- unread c', if c = c' then pure punit.star else failure def expect (s : string) : reader unit := s.to_list.mmap' expect_char -- def loop def peek : reader char := do c ← read_char, c <$ unread c protected def to_string_aux : put_m → string → string | (put_m'.pure x) y := y | (put_m'.write w f) y := to_string_aux (f punit.star) (y.push w) instance : has_to_string put_m.{u} := ⟨ λ x, string.medium.to_string_aux x "" ⟩ end string.medium
b8563f47b8e2fc5a6cd7de66fad8aa181ec48efa
e6b8240a90527fd55d42d0ec6649253d5d0bd414
/src/topology/homeomorph.lean
1a212f5d29671056d64f73df854a076db5a9c080
[ "Apache-2.0" ]
permissive
mattearnshaw/mathlib
ac90f9fb8168aa642223bea3ffd0286b0cfde44f
d8dc1445cf8a8c74f8df60b9f7a1f5cf10946666
refs/heads/master
1,606,308,351,137
1,576,594,130,000
1,576,594,130,000
228,666,195
0
0
Apache-2.0
1,576,603,094,000
1,576,603,093,000
null
UTF-8
Lean
false
false
6,059
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Patrick Massot, Sébastien Gouëzel, Zhouhang Zhou, Reid Barton -/ import topology.subset_properties topology.dense_embedding open set variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- α and β are homeomorph, also called topological isomoph -/ structure homeomorph (α : Type*) (β : Type*) [topological_space α] [topological_space β] extends α ≃ β := (continuous_to_fun : continuous to_fun) (continuous_inv_fun : continuous inv_fun) infix ` ≃ₜ `:25 := homeomorph namespace homeomorph variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] instance : has_coe_to_fun (α ≃ₜ β) := ⟨λ_, α → β, λe, e.to_equiv⟩ lemma coe_eq_to_equiv (h : α ≃ₜ β) (a : α) : h a = h.to_equiv a := rfl protected def refl (α : Type*) [topological_space α] : α ≃ₜ α := { continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id, .. equiv.refl α } protected def trans (h₁ : α ≃ₜ β) (h₂ : β ≃ₜ γ) : α ≃ₜ γ := { continuous_to_fun := h₂.continuous_to_fun.comp h₁.continuous_to_fun, continuous_inv_fun := h₁.continuous_inv_fun.comp h₂.continuous_inv_fun, .. equiv.trans h₁.to_equiv h₂.to_equiv } protected def symm (h : α ≃ₜ β) : β ≃ₜ α := { continuous_to_fun := h.continuous_inv_fun, continuous_inv_fun := h.continuous_to_fun, .. h.to_equiv.symm } protected lemma continuous (h : α ≃ₜ β) : continuous h := h.continuous_to_fun lemma symm_comp_self (h : α ≃ₜ β) : ⇑h.symm ∘ ⇑h = id := funext $ assume a, h.to_equiv.left_inv a lemma self_comp_symm (h : α ≃ₜ β) : ⇑h ∘ ⇑h.symm = id := funext $ assume a, h.to_equiv.right_inv a lemma range_coe (h : α ≃ₜ β) : range h = univ := eq_univ_of_forall $ assume b, ⟨h.symm b, congr_fun h.self_comp_symm b⟩ lemma image_symm (h : α ≃ₜ β) : image h.symm = preimage h := funext h.symm.to_equiv.image_eq_preimage lemma preimage_symm (h : α ≃ₜ β) : preimage h.symm = image h := (funext h.to_equiv.image_eq_preimage).symm lemma induced_eq {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] (h : α ≃ₜ β) : tβ.induced h = tα := le_antisymm (calc topological_space.induced ⇑h tβ ≤ _ : induced_mono (coinduced_le_iff_le_induced.1 h.symm.continuous) ... ≤ tα : by rw [induced_compose, symm_comp_self, induced_id] ; exact le_refl _) (coinduced_le_iff_le_induced.1 h.continuous) lemma coinduced_eq {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] (h : α ≃ₜ β) : tα.coinduced h = tβ := le_antisymm h.continuous begin have : (tβ.coinduced h.symm).coinduced h ≤ tα.coinduced h := coinduced_mono h.symm.continuous, rwa [coinduced_compose, self_comp_symm, coinduced_id] at this, end lemma compact_image {s : set α} (h : α ≃ₜ β) : compact (h '' s) ↔ compact s := ⟨λ hs, by have := compact_image hs h.symm.continuous; rwa [← image_comp, symm_comp_self, image_id] at this, λ hs, compact_image hs h.continuous⟩ lemma compact_preimage {s : set β} (h : α ≃ₜ β) : compact (h ⁻¹' s) ↔ compact s := by rw ← image_symm; exact h.symm.compact_image protected lemma embedding (h : α ≃ₜ β) : embedding h := ⟨⟨h.induced_eq.symm⟩, h.to_equiv.injective⟩ protected lemma dense_embedding (h : α ≃ₜ β) : dense_embedding h := { dense := assume a, by rw [h.range_coe, closure_univ]; trivial, inj := h.to_equiv.injective, induced := (induced_iff_nhds_eq _).2 (assume a, by rw [← nhds_induced, h.induced_eq]) } protected lemma is_open_map (h : α ≃ₜ β) : is_open_map h := begin assume s, rw ← h.preimage_symm, exact h.symm.continuous s end protected lemma is_closed_map (h : α ≃ₜ β) : is_closed_map h := begin assume s, rw ← h.preimage_symm, exact continuous_iff_is_closed.1 (h.symm.continuous) _ end def homeomorph_of_continuous_open (e : α ≃ β) (h₁ : continuous e) (h₂ : is_open_map e) : α ≃ₜ β := { continuous_to_fun := h₁, continuous_inv_fun := begin intros s hs, convert ← h₂ s hs using 1, apply e.image_eq_preimage end, .. e } protected lemma quotient_map (h : α ≃ₜ β) : quotient_map h := ⟨h.to_equiv.surjective, h.coinduced_eq.symm⟩ def prod_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : α × γ ≃ₜ β × δ := { continuous_to_fun := continuous.prod_mk (h₁.continuous.comp continuous_fst) (h₂.continuous.comp continuous_snd), continuous_inv_fun := continuous.prod_mk (h₁.symm.continuous.comp continuous_fst) (h₂.symm.continuous.comp continuous_snd), .. h₁.to_equiv.prod_congr h₂.to_equiv } section variables (α β γ) def prod_comm : α × β ≃ₜ β × α := { continuous_to_fun := continuous.prod_mk continuous_snd continuous_fst, continuous_inv_fun := continuous.prod_mk continuous_snd continuous_fst, .. equiv.prod_comm α β } def prod_assoc : (α × β) × γ ≃ₜ α × (β × γ) := { continuous_to_fun := continuous.prod_mk (continuous_fst.comp continuous_fst) (continuous.prod_mk (continuous_snd.comp continuous_fst) continuous_snd), continuous_inv_fun := continuous.prod_mk (continuous.prod_mk continuous_fst (continuous_fst.comp continuous_snd)) (continuous_snd.comp continuous_snd), .. equiv.prod_assoc α β γ } end section distrib variables {ι : Type*} {σ : ι → Type*} [Π i, topological_space (σ i)] def sigma_prod_distrib : ((Σ i, σ i) × β) ≃ₜ (Σ i, (σ i × β)) := homeomorph.symm $ homeomorph_of_continuous_open (equiv.sigma_prod_distrib σ β).symm (continuous_sigma $ λ i, continuous.prod_mk (continuous_sigma_mk.comp continuous_fst) continuous_snd) (is_open_map_sigma $ λ i, (open_embedding.prod open_embedding_sigma_mk open_embedding_id).is_open_map) end distrib end homeomorph
a3f20ed9b5450808f88ff28a4b06c379f9ccd065
c777c32c8e484e195053731103c5e52af26a25d1
/src/order/category/BddDistLat.lean
7523cd5d45659b68cfb24b6f1ce4a7f0ba7936ff
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
3,161
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import order.category.BddLat import order.category.DistLat /-! # The category of bounded distributive lattices This defines `BddDistLat`, the category of bounded distributive lattices. Note that this category is sometimes called [`DistLat`](https://ncatlab.org/nlab/show/DistLat) when being a lattice is understood to entail having a bottom and a top element. -/ universes u open category_theory /-- The category of bounded distributive lattices with bounded lattice morphisms. -/ structure BddDistLat := (to_DistLat : DistLat) [is_bounded_order : bounded_order to_DistLat] namespace BddDistLat instance : has_coe_to_sort BddDistLat Type* := ⟨λ X, X.to_DistLat⟩ instance (X : BddDistLat) : distrib_lattice X := X.to_DistLat.str attribute [instance] BddDistLat.is_bounded_order /-- Construct a bundled `BddDistLat` from a `bounded_order` `distrib_lattice`. -/ def of (α : Type*) [distrib_lattice α] [bounded_order α] : BddDistLat := ⟨⟨α⟩⟩ @[simp] lemma coe_of (α : Type*) [distrib_lattice α] [bounded_order α] : ↥(of α) = α := rfl instance : inhabited BddDistLat := ⟨of punit⟩ /-- Turn a `BddDistLat` into a `BddLat` by forgetting it is distributive. -/ def to_BddLat (X : BddDistLat) : BddLat := BddLat.of X @[simp] lemma coe_to_BddLat (X : BddDistLat) : ↥X.to_BddLat = ↥X := rfl instance : large_category.{u} BddDistLat := induced_category.category to_BddLat instance : concrete_category BddDistLat := induced_category.concrete_category to_BddLat instance has_forget_to_DistLat : has_forget₂ BddDistLat DistLat := { forget₂ := { obj := λ X, ⟨X⟩, map := λ X Y, bounded_lattice_hom.to_lattice_hom } } instance has_forget_to_BddLat : has_forget₂ BddDistLat BddLat := induced_category.has_forget₂ to_BddLat lemma forget_BddLat_Lat_eq_forget_DistLat_Lat : forget₂ BddDistLat BddLat ⋙ forget₂ BddLat Lat = forget₂ BddDistLat DistLat ⋙ forget₂ DistLat Lat := rfl /-- Constructs an equivalence between bounded distributive lattices from an order isomorphism between them. -/ @[simps] def iso.mk {α β : BddDistLat.{u}} (e : α ≃o β) : α ≅ β := { hom := (e : bounded_lattice_hom α β), inv := (e.symm : bounded_lattice_hom β α), hom_inv_id' := by { ext, exact e.symm_apply_apply _ }, inv_hom_id' := by { ext, exact e.apply_symm_apply _ } } /-- `order_dual` as a functor. -/ @[simps] def dual : BddDistLat ⥤ BddDistLat := { obj := λ X, of Xᵒᵈ, map := λ X Y, bounded_lattice_hom.dual } /-- The equivalence between `BddDistLat` and itself induced by `order_dual` both ways. -/ @[simps functor inverse] def dual_equiv : BddDistLat ≌ BddDistLat := equivalence.mk dual dual (nat_iso.of_components (λ X, iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl) (nat_iso.of_components (λ X, iso.mk $ order_iso.dual_dual X) $ λ X Y f, rfl) end BddDistLat lemma BddDistLat_dual_comp_forget_to_DistLat : BddDistLat.dual ⋙ forget₂ BddDistLat DistLat = forget₂ BddDistLat DistLat ⋙ DistLat.dual := rfl
364c3d1389b0b4c40eb632a9a43626c876844a96
4a092885406df4e441e9bb9065d9405dacb94cd8
/src/for_mathlib/uniform_space/ring.lean
4c7b7ece5f2c398b3370816a84130aff1dd4935d
[ "Apache-2.0" ]
permissive
semorrison/lean-perfectoid-spaces
78c1572cedbfae9c3e460d8aaf91de38616904d8
bb4311dff45791170bcb1b6a983e2591bee88a19
refs/heads/master
1,588,841,765,494
1,554,805,620,000
1,554,805,620,000
180,353,546
0
1
null
1,554,809,880,000
1,554,809,880,000
null
UTF-8
Lean
false
false
14,999
lean
import tactic.ring import topology.algebra.group_completion topology.algebra.ring import for_mathlib.uniform_space.group noncomputable theory local attribute [instance, priority 0] classical.prop_decidable open classical set lattice filter topological_space add_comm_group local attribute [instance] classical.prop_decidable noncomputable theory namespace uniform_space.completion open dense_embedding uniform_space variables (α : Type*) [ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] [separated α] instance is_Z_bilin_mul : is_Z_bilin (λp:α×α, p.1 * p.2) := ⟨assume a a' b, add_mul a a' b, assume a b b', mul_add a b b'⟩ instance : has_one (completion α) := ⟨(1:α)⟩ instance : has_mul (completion α) := ⟨λa b, extend (dense_embedding_coe.prod dense_embedding_coe) ((coe : α → completion α) ∘ (λp:α×α, p.1 * p.2)) (a, b)⟩ lemma coe_one : ((1 : α) : completion α) = 1 := rfl lemma continuous_mul' : continuous (λp:completion α×completion α, p.1 * p.2) := suffices continuous $ extend (dense_embedding_coe.prod dense_embedding_coe) $ ((coe : α → completion α) ∘ (λp:α×α, p.1 * p.2)), { convert this, ext ⟨a, b⟩, refl }, extend_Z_bilin dense_embedding_coe dense_embedding_coe (continuous.comp continuous_mul' (continuous_coe α)) section rules variables {α} lemma coe_mul (a b : α) : ((a * b : α) : completion α) = a * b := eq.symm (extend_e_eq (dense_embedding_coe.prod dense_embedding_coe) (a, b)) lemma continuous_mul {β : Type*} [topological_space β] {f g : β → completion α} (hf : continuous f) (hg : continuous g) : continuous (λb, f b * g b) := (continuous.prod_mk hf hg).comp (continuous_mul' α) end rules instance : ring (completion α) := { one_mul := assume a, completion.induction_on a (is_closed_eq (continuous_mul continuous_const continuous_id) continuous_id) (assume a, by rw [← coe_one, ← coe_mul, one_mul]), mul_one := assume a, completion.induction_on a (is_closed_eq (continuous_mul continuous_id continuous_const) continuous_id) (assume a, by rw [← coe_one, ← coe_mul, mul_one]), mul_assoc := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous_mul (continuous_mul continuous_fst (continuous_snd.comp continuous_fst)) (continuous_snd.comp continuous_snd)) (continuous_mul continuous_fst (continuous_mul (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd)))) (assume a b c, by rw [← coe_mul, ← coe_mul, ← coe_mul, ← coe_mul, mul_assoc]), left_distrib := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous_mul continuous_fst (continuous_add (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd))) (continuous_add (continuous_mul continuous_fst (continuous_snd.comp continuous_fst)) (continuous_mul continuous_fst (continuous_snd.comp continuous_snd)))) (assume a b c, by rw [← coe_add, ← coe_mul, ← coe_mul, ← coe_mul, ←coe_add, mul_add]), right_distrib := assume a b c, completion.induction_on₃ a b c (is_closed_eq (continuous_mul (continuous_add continuous_fst (continuous_snd.comp continuous_fst)) (continuous_snd.comp continuous_snd)) (continuous_add (continuous_mul continuous_fst (continuous_snd.comp continuous_snd)) (continuous_mul (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd)))) (assume a b c, by rw [← coe_add, ← coe_mul, ← coe_mul, ← coe_mul, ←coe_add, add_mul]), ..completion.add_comm_group, ..completion.has_mul α, ..completion.has_one α } instance top_ring_compl : topological_ring (completion α) := { continuous_add := continuous_add', continuous_mul := uniform_space.completion.continuous_mul' α, continuous_neg := continuous_neg' } instance is_ring_hom_coe : is_ring_hom (coe : α → completion α) := ⟨coe_one α, assume a b, coe_mul a b, assume a b, coe_add a b⟩ universe u instance is_ring_hom_extension {β : Type u} [uniform_space β] [ring β] [uniform_add_group β] [topological_ring β] [complete_space β] [separated β] {f : α → β} [is_ring_hom f] (hf : continuous f) : is_ring_hom (completion.extension f) := have hf : uniform_continuous f, from uniform_continuous_of_continuous hf, { map_one := by rw [← coe_one, extension_coe hf, is_ring_hom.map_one f], map_add := assume a b, completion.induction_on₂ a b (is_closed_eq (continuous_add'.comp continuous_extension) (continuous_add (continuous_fst.comp continuous_extension) (continuous_snd.comp continuous_extension))) (assume a b, by rw [← coe_add, extension_coe hf, extension_coe hf, extension_coe hf, is_add_group_hom.add f]), map_mul := assume a b, completion.induction_on₂ a b (is_closed_eq ((continuous_mul' α).comp continuous_extension) (_root_.continuous_mul (continuous_fst.comp continuous_extension) (continuous_snd.comp continuous_extension))) (assume a b, by rw [← coe_mul, extension_coe hf, extension_coe hf, extension_coe hf, is_ring_hom.map_mul f]) } end uniform_space.completion section ring_sep_quot open uniform_space variables {α : Type*} [comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] variables {β : Type*} [comm_ring β] [uniform_space β] [uniform_add_group β] [topological_ring β] local attribute [instance] separation_setoid lemma separated_map_mul : separated_map₂ ((*) : α → α → α) := begin rintros ⟨x, y⟩ ⟨x', y'⟩ h, cases uniform_space.separation_prod.1 h with hx hy, clear h, unfold function.uncurry has_equiv.equiv setoid.r at *, rw group_separation_rel x x' at hx, rw group_separation_rel y y' at hy, rw group_separation_rel (x*y) _, rw show x*y - x'*y' = (x - x')*y + x'*(y - y'), by ring, let I := ideal.closure (⊥ : ideal α), exact I.add_mem (I.mul_mem_right hx) (I.mul_mem_left hy) end instance : comm_ring (sep_quot α) := { mul := sep_quot.map₂ (*), one := ⟦1⟧, one_mul := begin change ∀ a : sep_quot α, sep_quot.map₂ (*) ⟦1⟧ a = id a, rw ←sep_quot.map_id, exact sep_quot.map₂_const_left_eq_map separated_map_mul uniform_continuous_id.separated_map _ one_mul end, mul_one := begin change ∀ a : sep_quot α, sep_quot.map₂ (*) a ⟦1⟧ = id a, rw ←sep_quot.map_id, exact sep_quot.map₂_const_right_eq_map separated_map_mul uniform_continuous_id.separated_map _ mul_one end, mul_assoc := begin apply sep_quot.map₂_assoc ; try { exact separated_map_mul }, exact mul_assoc end, mul_comm := sep_quot.map₂_comm separated_map_mul mul_comm, left_distrib := begin apply sep_quot.map₂_left_distrib ; try { exact separated_map_mul <|> exact uniform_continuous₂_add.separated_map}, exact left_distrib end, right_distrib := begin apply sep_quot.map₂_right_distrib ; try { exact separated_map_mul <|> exact uniform_continuous₂_add.separated_map}, exact right_distrib end, ..uniform_space.sep_quot.add_comm_group } lemma continuous₂_mul {α : Type*} [ring α] [topological_space α] [topological_ring α] : continuous₂ ((*) : α → α → α) := begin dsimp [continuous₂], convert @continuous_mul' α _ _ _, ext x, cases x, refl end instance : topological_ring (sep_quot α) := { continuous_add := continuous_add', continuous_mul := by rw ←continuous₂_curry ; exact sep_quot.continuous_map₂ continuous₂_mul, continuous_neg := continuous_neg' } instance sep_quot.is_ring_hom_mk : is_ring_hom (quotient.mk : α → sep_quot α) := { map_one := rfl, map_mul := λ x y, eq.symm (sep_quot.map₂_mk_mk separated_map_mul x y), map_add := is_add_group_hom.add _ } -- Turning the following into an instance wouldn't work because of the continuity assumption def sep_quot.is_ring_hom_lift [separated β] {f : α → β} [hom : is_ring_hom f] (hf : continuous f) : is_ring_hom (sep_quot.lift f) := have sep : separated_map f, from separated_of_group_hom hf, { map_one := by change sep_quot.lift f ⟦1⟧ = 1 ; rw [sep_quot.lift_mk sep, hom.map_one ], map_mul := begin rintro ⟨x⟩ ⟨y⟩, rw [quot_mk_quotient_mk, quot_mk_quotient_mk, ←sep_quot.is_ring_hom_mk.map_mul], repeat {rw sep_quot.lift_mk sep} , rw hom.map_mul, end, map_add := by haveI := sep_quot.is_add_group_hom_lift hf ; exact is_add_group_hom.add _ } -- Turning the following into an instance wouldn't work because of the continuity assumption def sep_quot.is_ring_hom_map {f : α → β} [is_ring_hom f] (hf : continuous f) : is_ring_hom (sep_quot.map f) := sep_quot.is_ring_hom_lift (hf.comp continuous_quotient_mk) end ring_sep_quot namespace uniform_space.completion universes u v open uniform_space open uniform_space.completion variables {α : Type u} [uniform_space α] lemma extension_unique {β : Type*} [uniform_space β] [separated β] [complete_space β] {f : α → β} (hf : uniform_continuous f) {g : completion α → β} (hg : uniform_continuous g) (h : ∀ a : α, f a = g a) : completion.extension f = g := begin apply completion.ext uniform_continuous_extension.continuous hg.continuous, intro a, rw extension_coe hf, exact h a end variables (α) [ring α] [uniform_add_group α] [topological_ring α] [separated α] instance is_ring_hom_map {β : Type v} [uniform_space β] [ring β] [uniform_add_group β] [topological_ring β] [separated β] {f : α → β} [is_ring_hom f] (hf : continuous f) : is_ring_hom (completion.map f) := completion.is_ring_hom_extension α (hf.comp (continuous_coe β)) end uniform_space.completion section ring_completion open uniform_space variables (α : Type*) [uniform_space α] def ring_completion := completion (sep_quot α) local attribute [instance] separation_setoid instance : has_coe α (ring_completion α) := ⟨quotient.mk ∘ Cauchy.pure_cauchy ∘ quotient.mk⟩ instance : uniform_space (ring_completion α) := by dunfold ring_completion ; apply_instance instance : separated (ring_completion α) := by dunfold ring_completion ; apply_instance end ring_completion namespace ring_completion open uniform_space variables {α : Type*} [uniform_space α] variables {β : Type*} [uniform_space β] lemma uniform_continuous_coe : uniform_continuous (coe : α → ring_completion α) := completion.uniform_continuous_coe _ lemma continuous_coe : continuous (coe : α → ring_completion α) := completion.continuous_coe _ def extension [separated β] [complete_space β] (f : α → β) : ring_completion α → β := completion.extension $ sep_quot.lift f def uniform_continuous_extension [separated β] [complete_space β] (f : α → β) : uniform_continuous (extension f):= completion.uniform_continuous_extension def continuous_extension [separated β] [complete_space β] (f : α → β) : continuous (extension f):= (ring_completion.uniform_continuous_extension f).continuous lemma extension_coe [separated β] [complete_space β] {f : α → β} (hf : uniform_continuous f) (a : α) : (ring_completion.extension f) a = f a := begin convert completion.extension_coe (sep_quot.uniform_continuous_lift hf) _, rw sep_quot.lift_mk hf.separated_map, end local attribute [instance] separation_setoid lemma ext [t2_space β] {f g : ring_completion α → β} (hf : continuous f) (hg : continuous g) (h : ∀ a : α, f a = g a) : f = g := completion.ext hf hg (by rintro ⟨a⟩ ; exact h a) lemma extension_unique [separated β] [complete_space β] {f : α → β} (hf : uniform_continuous f) {g : ring_completion α → β} (hg : uniform_continuous g) (h : ∀ a : α, f a = g a) : ring_completion.extension f = g := begin apply completion.extension_unique (sep_quot.uniform_continuous_lift hf) hg, rintro ⟨a⟩, change sep_quot.lift f ⟦a⟧ = g _, rw sep_quot.lift_mk hf.separated_map, apply h, end def map (f : α → β) : ring_completion α → ring_completion β := completion.map $ sep_quot.map f @[simp] lemma map_id : map (id : α → α) = id := by { delta map, rw sep_quot.map_id, exact completion.map_id } @[simp] lemma map_comp {γ : Type*} [uniform_space γ] {f : α → β} {g : β → γ} (hf : uniform_continuous f) (hg : uniform_continuous g) : map (g ∘ f) = map g ∘ map f := begin delta map, rw [completion.map_comp, sep_quot.map_comp]; solve_by_elim [sep_quot.uniform_continuous_map, uniform_continuous.separated_map] end lemma map_uniform_continuous {f : α → β} (hf : uniform_continuous f) : uniform_continuous (map f) := completion.uniform_continuous_map lemma map_unique {f : α → β} (hf : uniform_continuous f) {g : ring_completion α → ring_completion β} (hg : uniform_continuous g) (h : ∀ a : α, (f a : ring_completion β) = g a) : map f = g := completion.map_unique hg $ begin rintro ⟨a⟩, change ↑(sep_quot.map f ⟦a⟧) = _, rw sep_quot.map_mk hf.separated_map, apply h, end end ring_completion section ring_completion open uniform_space ring_completion variables (α : Type*) [comm_ring α] [uniform_space α] [uniform_add_group α] [topological_ring α] variables (β : Type*) [comm_ring β] [uniform_space β] [uniform_add_group β] [topological_ring β] instance : ring (ring_completion α) := by dunfold ring_completion ; apply_instance instance : topological_ring (ring_completion α) := by dunfold ring_completion ; apply_instance variables {f : α → β} [is_ring_hom f] (hf : continuous f) include hf lemma ring_completion.extension_is_ring_hom [separated β] [complete_space β] : is_ring_hom (ring_completion.extension f) := by haveI := sep_quot.is_ring_hom_lift hf ; exact uniform_space.completion.is_ring_hom_extension (sep_quot α) (sep_quot.continuous_lift hf) lemma ring_completion.map_is_ring_hom : is_ring_hom (ring_completion.map f) := by haveI := sep_quot.is_ring_hom_map hf ; exact completion.is_ring_hom_map (sep_quot α) (sep_quot.continuous_map hf) omit hf def completion_of_complete_separated [complete_space α] [separated α] : α ≃ (ring_completion α) := { to_fun := coe, inv_fun := ring_completion.extension id, left_inv := ring_completion.extension_coe uniform_continuous_id, right_inv := begin apply congr_fun, change coe ∘ (extension id) = id, refine ext ((continuous_extension id).comp continuous_coe) continuous_id _, intro x, change coe ((extension id) ↑x) = id ↑x, rw ring_completion.extension_coe uniform_continuous_id, simp, end } lemma uniform_continuous_completion_equiv [complete_space α] [separated α] : uniform_continuous (completion_of_complete_separated α).to_fun := completion.uniform_continuous_coe _ lemma uniform_continuous_completion_equiv_symm [complete_space α] [separated α] : uniform_continuous ⇑(completion_of_complete_separated α).symm := completion.uniform_continuous_extension end ring_completion
5d412d74257243635e19754efa0f53937289b2ba
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/uni_issue1.lean
2302f4fcf4d41cad48cff697689118b5b379cf96
[ "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
139
lean
import standard inductive nat : Type := | zero : nat | succ : nat → nat definition is_zero (n : nat) := nat_rec true (λ n r, false) n
be1b7ecc12ac9dc396d899234d33dce87f9e5f9f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/function/strongly_measurable/basic.lean
a2efb0496676ceca38d096e51235472712c07b89
[ "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
89,974
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne, Sébastien Gouëzel -/ import analysis.normed_space.finite_dimension import analysis.normed_space.bounded_linear_maps import measure_theory.constructions.borel_space.metrizable import measure_theory.integral.lebesgue import measure_theory.function.simple_func_dense /-! # Strongly measurable and finitely strongly measurable functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A function `f` is said to be strongly measurable if `f` is the sequential limit of simple functions. It is said to be finitely strongly measurable with respect to a measure `μ` if the supports of those simple functions have finite measure. We also provide almost everywhere versions of these notions. Almost everywhere strongly measurable functions form the largest class of functions that can be integrated using the Bochner integral. If the target space has a second countable topology, strongly measurable and measurable are equivalent. If the measure is sigma-finite, strongly measurable and finitely strongly measurable are equivalent. The main property of finitely strongly measurable functions is `fin_strongly_measurable.exists_set_sigma_finite`: there exists a measurable set `t` such that the function is supported on `t` and `μ.restrict t` is sigma-finite. As a consequence, we can prove some results for those functions as if the measure was sigma-finite. ## Main definitions * `strongly_measurable f`: `f : α → β` is the limit of a sequence `fs : ℕ → simple_func α β`. * `fin_strongly_measurable f μ`: `f : α → β` is the limit of a sequence `fs : ℕ → simple_func α β` such that for all `n ∈ ℕ`, the measure of the support of `fs n` is finite. * `ae_strongly_measurable f μ`: `f` is almost everywhere equal to a `strongly_measurable` function. * `ae_fin_strongly_measurable f μ`: `f` is almost everywhere equal to a `fin_strongly_measurable` function. * `ae_fin_strongly_measurable.sigma_finite_set`: a measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `μ.restrict t` is sigma-finite. ## Main statements * `ae_fin_strongly_measurable.exists_set_sigma_finite`: there exists a measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `μ.restrict t` is sigma-finite. We provide a solid API for strongly measurable functions, and for almost everywhere strongly measurable functions, as a basis for the Bochner integral. ## References * Hytönen, Tuomas, Jan Van Neerven, Mark Veraar, and Lutz Weis. Analysis in Banach spaces. Springer, 2016. -/ open measure_theory filter topological_space function set measure_theory.measure open_locale ennreal topology measure_theory nnreal big_operators /-- The typeclass `second_countable_topology_either α β` registers the fact that at least one of the two spaces has second countable topology. This is the right assumption to ensure that continuous maps from `α` to `β` are strongly measurable. -/ class second_countable_topology_either (α β : Type*) [topological_space α] [topological_space β] : Prop := (out : second_countable_topology α ∨ second_countable_topology β) @[priority 100] instance second_countable_topology_either_of_left (α β : Type*) [topological_space α] [topological_space β] [second_countable_topology α] : second_countable_topology_either α β := { out := or.inl (by apply_instance) } @[priority 100] instance second_countable_topology_either_of_right (α β : Type*) [topological_space α] [topological_space β] [second_countable_topology β] : second_countable_topology_either α β := { out := or.inr (by apply_instance) } variables {α β γ ι : Type*} [countable ι] namespace measure_theory local infixr ` →ₛ `:25 := simple_func section definitions variable [topological_space β] /-- A function is `strongly_measurable` if it is the limit of simple functions. -/ def strongly_measurable [measurable_space α] (f : α → β) : Prop := ∃ fs : ℕ → α →ₛ β, ∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x)) localized "notation (name := strongly_measurable_of) `strongly_measurable[` m `]` := @measure_theory.strongly_measurable _ _ _ m" in measure_theory /-- A function is `fin_strongly_measurable` with respect to a measure if it is the limit of simple functions with support with finite measure. -/ def fin_strongly_measurable [has_zero β] {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ fs : ℕ → α →ₛ β, (∀ n, μ (support (fs n)) < ∞) ∧ (∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x))) /-- A function is `ae_strongly_measurable` with respect to a measure `μ` if it is almost everywhere equal to the limit of a sequence of simple functions. -/ def ae_strongly_measurable {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g, strongly_measurable g ∧ f =ᵐ[μ] g /-- A function is `ae_fin_strongly_measurable` with respect to a measure if it is almost everywhere equal to the limit of a sequence of simple functions with support with finite measure. -/ def ae_fin_strongly_measurable [has_zero β] {m0 : measurable_space α} (f : α → β) (μ : measure α) : Prop := ∃ g, fin_strongly_measurable g μ ∧ f =ᵐ[μ] g end definitions open_locale measure_theory /-! ## Strongly measurable functions -/ protected lemma strongly_measurable.ae_strongly_measurable {α β} {m0 : measurable_space α} [topological_space β] {f : α → β} {μ : measure α} (hf : strongly_measurable f) : ae_strongly_measurable f μ := ⟨f, hf, eventually_eq.refl _ _⟩ @[simp] lemma subsingleton.strongly_measurable {α β} [measurable_space α] [topological_space β] [subsingleton β] (f : α → β) : strongly_measurable f := begin let f_sf : α →ₛ β := ⟨f, λ x, _, set.subsingleton.finite set.subsingleton_of_subsingleton⟩, { exact ⟨λ n, f_sf, λ x, tendsto_const_nhds⟩, }, { have h_univ : f ⁻¹' {x} = set.univ, by { ext1 y, simp, }, rw h_univ, exact measurable_set.univ, }, end lemma simple_func.strongly_measurable {α β} {m : measurable_space α} [topological_space β] (f : α →ₛ β) : strongly_measurable f := ⟨λ _, f, λ x, tendsto_const_nhds⟩ lemma strongly_measurable_of_is_empty [is_empty α] {m : measurable_space α} [topological_space β] (f : α → β) : strongly_measurable f := ⟨λ n, simple_func.of_is_empty, is_empty_elim⟩ lemma strongly_measurable_const {α β} {m : measurable_space α} [topological_space β] {b : β} : strongly_measurable (λ a : α, b) := ⟨λ n, simple_func.const α b, λ a, tendsto_const_nhds⟩ @[to_additive] lemma strongly_measurable_one {α β} {m : measurable_space α} [topological_space β] [has_one β] : strongly_measurable (1 : α → β) := @strongly_measurable_const _ _ _ _ 1 /-- A version of `strongly_measurable_const` that assumes `f x = f y` for all `x, y`. This version works for functions between empty types. -/ lemma strongly_measurable_const' {α β} {m : measurable_space α} [topological_space β] {f : α → β} (hf : ∀ x y, f x = f y) : strongly_measurable f := begin casesI is_empty_or_nonempty α, { exact strongly_measurable_of_is_empty f }, { convert strongly_measurable_const, exact funext (λ x, hf x h.some) } end @[simp] lemma subsingleton.strongly_measurable' {α β} [measurable_space α] [topological_space β] [subsingleton α] (f : α → β) : strongly_measurable f := strongly_measurable_const' (λ x y, by rw subsingleton.elim x y) namespace strongly_measurable variables {f g : α → β} section basic_properties_in_any_topological_space variables [topological_space β] /-- A sequence of simple functions such that `∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x))`. That property is given by `strongly_measurable.tendsto_approx`. -/ protected noncomputable def approx {m : measurable_space α} (hf : strongly_measurable f) : ℕ → α →ₛ β := hf.some protected lemma tendsto_approx {m : measurable_space α} (hf : strongly_measurable f) : ∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x)) := hf.some_spec /-- Similar to `strongly_measurable.approx`, but enforces that the norm of every function in the sequence is less than `c` everywhere. If `‖f x‖ ≤ c` this sequence of simple functions verifies `tendsto (λ n, hf.approx_bounded n x) at_top (𝓝 (f x))`. -/ noncomputable def approx_bounded {m : measurable_space α} [has_norm β] [has_smul ℝ β] (hf : strongly_measurable f) (c : ℝ) : ℕ → simple_func α β := λ n, (hf.approx n).map (λ x, (min 1 (c / ‖x‖)) • x) lemma tendsto_approx_bounded_of_norm_le {β} {f : α → β} [normed_add_comm_group β] [normed_space ℝ β] {m : measurable_space α} (hf : strongly_measurable[m] f) {c : ℝ} {x : α} (hfx : ‖f x‖ ≤ c) : tendsto (λ n, hf.approx_bounded c n x) at_top (𝓝 (f x)) := begin have h_tendsto := hf.tendsto_approx x, simp only [strongly_measurable.approx_bounded, simple_func.coe_map, function.comp_app], by_cases hfx0 : ‖f x‖ = 0, { rw norm_eq_zero at hfx0, rw hfx0 at h_tendsto ⊢, have h_tendsto_norm : tendsto (λ n, ‖hf.approx n x‖) at_top (𝓝 0), { convert h_tendsto.norm, rw norm_zero, }, refine squeeze_zero_norm (λ n, _) h_tendsto_norm, calc ‖min 1 (c / ‖hf.approx n x‖) • hf.approx n x‖ = ‖min 1 (c / ‖hf.approx n x‖)‖ * ‖hf.approx n x‖ : norm_smul _ _ ... ≤ ‖(1 : ℝ)‖ * ‖hf.approx n x‖ : begin refine mul_le_mul_of_nonneg_right _ (norm_nonneg _), rw [norm_one, real.norm_of_nonneg], { exact min_le_left _ _, }, { exact le_min zero_le_one (div_nonneg ((norm_nonneg _).trans hfx) (norm_nonneg _)), }, end ... = ‖hf.approx n x‖ : by rw [norm_one, one_mul], }, rw ← one_smul ℝ (f x), refine tendsto.smul _ h_tendsto, have : min 1 (c / ‖f x‖) = 1, { rw [min_eq_left_iff, one_le_div (lt_of_le_of_ne (norm_nonneg _) (ne.symm hfx0))], exact hfx, }, nth_rewrite 0 this.symm, refine tendsto.min tendsto_const_nhds _, refine tendsto.div tendsto_const_nhds h_tendsto.norm hfx0, end lemma tendsto_approx_bounded_ae {β} {f : α → β} [normed_add_comm_group β] [normed_space ℝ β] {m m0 : measurable_space α} {μ : measure α} (hf : strongly_measurable[m] f) {c : ℝ} (hf_bound : ∀ᵐ x ∂μ, ‖f x‖ ≤ c) : ∀ᵐ x ∂μ, tendsto (λ n, hf.approx_bounded c n x) at_top (𝓝 (f x)) := by filter_upwards [hf_bound] with x hfx using tendsto_approx_bounded_of_norm_le hf hfx lemma norm_approx_bounded_le {β} {f : α → β} [seminormed_add_comm_group β] [normed_space ℝ β] {m : measurable_space α} {c : ℝ} (hf : strongly_measurable[m] f) (hc : 0 ≤ c) (n : ℕ) (x : α) : ‖hf.approx_bounded c n x‖ ≤ c := begin simp only [strongly_measurable.approx_bounded, simple_func.coe_map, function.comp_app], refine (norm_smul_le _ _).trans _, by_cases h0 : ‖hf.approx n x‖ = 0, { simp only [h0, div_zero, min_eq_right, zero_le_one, norm_zero, mul_zero], exact hc, }, cases le_total (‖hf.approx n x‖) c, { rw min_eq_left _, { simpa only [norm_one, one_mul] using h, }, { rwa one_le_div (lt_of_le_of_ne (norm_nonneg _) (ne.symm h0)), }, }, { rw min_eq_right _, { rw [norm_div, norm_norm, mul_comm, mul_div, div_eq_mul_inv, mul_comm, ← mul_assoc, inv_mul_cancel h0, one_mul, real.norm_of_nonneg hc], }, { rwa div_le_one (lt_of_le_of_ne (norm_nonneg _) (ne.symm h0)), }, }, end lemma _root_.strongly_measurable_bot_iff [nonempty β] [t2_space β] : strongly_measurable[⊥] f ↔ ∃ c, f = λ _, c := begin casesI is_empty_or_nonempty α with hα hα, { simp only [subsingleton.strongly_measurable', eq_iff_true_of_subsingleton, exists_const], }, refine ⟨λ hf, _, λ hf_eq, _⟩, { refine ⟨f hα.some, _⟩, let fs := hf.approx, have h_fs_tendsto : ∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x)) := hf.tendsto_approx, have : ∀ n, ∃ c, ∀ x, fs n x = c := λ n, simple_func.simple_func_bot (fs n), let cs := λ n, (this n).some, have h_cs_eq : ∀ n, ⇑(fs n) = (λ x, cs n) := λ n, funext (this n).some_spec, simp_rw h_cs_eq at h_fs_tendsto, have h_tendsto : tendsto cs at_top (𝓝 (f hα.some)) := h_fs_tendsto hα.some, ext1 x, exact tendsto_nhds_unique (h_fs_tendsto x) h_tendsto, }, { obtain ⟨c, rfl⟩ := hf_eq, exact strongly_measurable_const, }, end end basic_properties_in_any_topological_space lemma fin_strongly_measurable_of_set_sigma_finite [topological_space β] [has_zero β] {m : measurable_space α} {μ : measure α} (hf_meas : strongly_measurable f) {t : set α} (ht : measurable_set t) (hft_zero : ∀ x ∈ tᶜ, f x = 0) (htμ : sigma_finite (μ.restrict t)) : fin_strongly_measurable f μ := begin haveI : sigma_finite (μ.restrict t) := htμ, let S := spanning_sets (μ.restrict t), have hS_meas : ∀ n, measurable_set (S n), from measurable_spanning_sets (μ.restrict t), let f_approx := hf_meas.approx, let fs := λ n, simple_func.restrict (f_approx n) (S n ∩ t), have h_fs_t_compl : ∀ n, ∀ x ∉ t, fs n x = 0, { intros n x hxt, rw simple_func.restrict_apply _ ((hS_meas n).inter ht), refine set.indicator_of_not_mem _ _, simp [hxt], }, refine ⟨fs, _, λ x, _⟩, { simp_rw simple_func.support_eq, refine λ n, (measure_bUnion_finset_le _ _).trans_lt _, refine ennreal.sum_lt_top_iff.mpr (λ y hy, _), rw simple_func.restrict_preimage_singleton _ ((hS_meas n).inter ht), swap, { rw finset.mem_filter at hy, exact hy.2, }, refine (measure_mono (set.inter_subset_left _ _)).trans_lt _, have h_lt_top := measure_spanning_sets_lt_top (μ.restrict t) n, rwa measure.restrict_apply' ht at h_lt_top, }, { by_cases hxt : x ∈ t, swap, { rw [funext (λ n, h_fs_t_compl n x hxt), hft_zero x hxt], exact tendsto_const_nhds, }, have h : tendsto (λ n, (f_approx n) x) at_top (𝓝 (f x)), from hf_meas.tendsto_approx x, obtain ⟨n₁, hn₁⟩ : ∃ n, ∀ m, n ≤ m → fs m x = f_approx m x, { obtain ⟨n, hn⟩ : ∃ n, ∀ m, n ≤ m → x ∈ S m ∩ t, { rsuffices ⟨n, hn⟩ : ∃ n, ∀ m, n ≤ m → x ∈ S m, { exact ⟨n, λ m hnm, set.mem_inter (hn m hnm) hxt⟩, }, rsuffices ⟨n, hn⟩ : ∃ n, x ∈ S n, { exact ⟨n, λ m hnm, monotone_spanning_sets (μ.restrict t) hnm hn⟩, }, rw [← set.mem_Union, Union_spanning_sets (μ.restrict t)], trivial, }, refine ⟨n, λ m hnm, _⟩, simp_rw [fs, simple_func.restrict_apply _ ((hS_meas m).inter ht), set.indicator_of_mem (hn m hnm)], }, rw tendsto_at_top' at h ⊢, intros s hs, obtain ⟨n₂, hn₂⟩ := h s hs, refine ⟨max n₁ n₂, λ m hm, _⟩, rw hn₁ m ((le_max_left _ _).trans hm.le), exact hn₂ m ((le_max_right _ _).trans hm.le), }, end /-- If the measure is sigma-finite, all strongly measurable functions are `fin_strongly_measurable`. -/ protected lemma fin_strongly_measurable [topological_space β] [has_zero β] {m0 : measurable_space α} (hf : strongly_measurable f) (μ : measure α) [sigma_finite μ] : fin_strongly_measurable f μ := hf.fin_strongly_measurable_of_set_sigma_finite measurable_set.univ (by simp) (by rwa measure.restrict_univ) /-- A strongly measurable function is measurable. -/ protected lemma measurable {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] (hf : strongly_measurable f) : measurable f := measurable_of_tendsto_metrizable (λ n, (hf.approx n).measurable) (tendsto_pi_nhds.mpr hf.tendsto_approx) /-- A strongly measurable function is almost everywhere measurable. -/ protected lemma ae_measurable {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] {μ : measure α} (hf : strongly_measurable f) : ae_measurable f μ := hf.measurable.ae_measurable lemma _root_.continuous.comp_strongly_measurable {m : measurable_space α} [topological_space β] [topological_space γ] {g : β → γ} {f : α → β} (hg : continuous g) (hf : strongly_measurable f) : strongly_measurable (λ x, g (f x)) := ⟨λ n, simple_func.map g (hf.approx n), λ x, (hg.tendsto _).comp (hf.tendsto_approx x)⟩ @[to_additive] lemma measurable_set_mul_support {m : measurable_space α} [has_one β] [topological_space β] [metrizable_space β] (hf : strongly_measurable f) : measurable_set (mul_support f) := by { borelize β, exact measurable_set_mul_support hf.measurable } protected lemma mono {m m' : measurable_space α} [topological_space β] (hf : strongly_measurable[m'] f) (h_mono : m' ≤ m) : strongly_measurable[m] f := begin let f_approx : ℕ → @simple_func α m β := λ n, { to_fun := hf.approx n, measurable_set_fiber' := λ x, h_mono _ (simple_func.measurable_set_fiber' _ x), finite_range' := simple_func.finite_range (hf.approx n) }, exact ⟨f_approx, hf.tendsto_approx⟩, end protected lemma prod_mk {m : measurable_space α} [topological_space β] [topological_space γ] {f : α → β} {g : α → γ} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, (f x, g x)) := begin refine ⟨λ n, simple_func.pair (hf.approx n) (hg.approx n), λ x, _⟩, rw nhds_prod_eq, exact tendsto.prod_mk (hf.tendsto_approx x) (hg.tendsto_approx x), end lemma comp_measurable [topological_space β] {m : measurable_space α} {m' : measurable_space γ} {f : α → β} {g : γ → α} (hf : strongly_measurable f) (hg : measurable g) : strongly_measurable (f ∘ g) := ⟨λ n, simple_func.comp (hf.approx n) g hg, λ x, hf.tendsto_approx (g x)⟩ lemma of_uncurry_left [topological_space β] {mα : measurable_space α} {mγ : measurable_space γ} {f : α → γ → β} (hf : strongly_measurable (uncurry f)) {x : α} : strongly_measurable (f x) := hf.comp_measurable measurable_prod_mk_left lemma of_uncurry_right [topological_space β] {mα : measurable_space α} {mγ : measurable_space γ} {f : α → γ → β} (hf : strongly_measurable (uncurry f)) {y : γ} : strongly_measurable (λ x, f x y) := hf.comp_measurable measurable_prod_mk_right section arithmetic variables {mα : measurable_space α} [topological_space β] include mα @[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f * g) := ⟨λ n, hf.approx n * hg.approx n, λ x, (hf.tendsto_approx x).mul (hg.tendsto_approx x)⟩ @[to_additive] lemma mul_const [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, f x * c) := hf.mul strongly_measurable_const @[to_additive] lemma const_mul [has_mul β] [has_continuous_mul β] (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, c * f x) := strongly_measurable_const.mul hf @[to_additive] protected lemma inv [group β] [topological_group β] (hf : strongly_measurable f) : strongly_measurable f⁻¹ := ⟨λ n, (hf.approx n)⁻¹, λ x, (hf.tendsto_approx x).inv⟩ @[to_additive] protected lemma div [has_div β] [has_continuous_div β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f / g) := ⟨λ n, hf.approx n / hg.approx n, λ x, (hf.tendsto_approx x).div' (hg.tendsto_approx x)⟩ @[to_additive] protected lemma smul {𝕜} [topological_space 𝕜] [has_smul 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} {g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, f x • g x) := continuous_smul.comp_strongly_measurable (hf.prod_mk hg) protected lemma const_smul {𝕜} [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : strongly_measurable f) (c : 𝕜) : strongly_measurable (c • f) := ⟨λ n, c • (hf.approx n), λ x, (hf.tendsto_approx x).const_smul c⟩ protected lemma const_smul' {𝕜} [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : strongly_measurable f) (c : 𝕜) : strongly_measurable (λ x, c • (f x)) := hf.const_smul c @[to_additive] protected lemma smul_const {𝕜} [topological_space 𝕜] [has_smul 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} (hf : strongly_measurable f) (c : β) : strongly_measurable (λ x, f x • c) := continuous_smul.comp_strongly_measurable (hf.prod_mk strongly_measurable_const) end arithmetic section mul_action variables {M G G₀ : Type*} variables [topological_space β] variables [monoid M] [mul_action M β] [has_continuous_const_smul M β] variables [group G] [mul_action G β] [has_continuous_const_smul G β] variables [group_with_zero G₀] [mul_action G₀ β] [has_continuous_const_smul G₀ β] lemma _root_.strongly_measurable_const_smul_iff {m : measurable_space α} (c : G) : strongly_measurable (λ x, c • f x) ↔ strongly_measurable f := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ lemma _root_.is_unit.strongly_measurable_const_smul_iff {m : measurable_space α} {c : M} (hc : is_unit c) : strongly_measurable (λ x, c • f x) ↔ strongly_measurable f := let ⟨u, hu⟩ := hc in hu ▸ strongly_measurable_const_smul_iff u lemma _root_.strongly_measurable_const_smul_iff₀ {m : measurable_space α} {c : G₀} (hc : c ≠ 0) : strongly_measurable (λ x, c • f x) ↔ strongly_measurable f := (is_unit.mk0 _ hc).strongly_measurable_const_smul_iff end mul_action section order variables [measurable_space α] [topological_space β] open filter open_locale filter protected lemma sup [has_sup β] [has_continuous_sup β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f ⊔ g) := ⟨λ n, hf.approx n ⊔ hg.approx n, λ x, (hf.tendsto_approx x).sup_right_nhds (hg.tendsto_approx x)⟩ protected lemma inf [has_inf β] [has_continuous_inf β] (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (f ⊓ g) := ⟨λ n, hf.approx n ⊓ hg.approx n, λ x, (hf.tendsto_approx x).inf_right_nhds (hg.tendsto_approx x)⟩ end order /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M : Type*} [monoid M] [topological_space M] [has_continuous_mul M] {m : measurable_space α} include m @[to_additive] lemma _root_.list.strongly_measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable l.prod := begin induction l with f l ihl, { exact strongly_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[to_additive] lemma _root_.list.strongly_measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable (λ x, (l.map (λ f : α → M, f x)).prod) := by simpa only [← pi.list_prod_apply] using l.strongly_measurable_prod' hl end monoid section comm_monoid variables {M : Type*} [comm_monoid M] [topological_space M] [has_continuous_mul M] {m : measurable_space α} include m @[to_additive] lemma _root_.multiset.strongly_measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, strongly_measurable f) : strongly_measurable l.prod := by { rcases l with ⟨l⟩, simpa using l.strongly_measurable_prod' (by simpa using hl) } @[to_additive] lemma _root_.multiset.strongly_measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, strongly_measurable f) : strongly_measurable (λ x, (s.map (λ f : α → M, f x)).prod) := by simpa only [← pi.multiset_prod_apply] using s.strongly_measurable_prod' hs @[to_additive] lemma _root_.finset.strongly_measurable_prod' {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, strongly_measurable (f i)) : strongly_measurable (∏ i in s, f i) := finset.prod_induction _ _ (λ a b ha hb, ha.mul hb) (@strongly_measurable_one α M _ _ _) hf @[to_additive] lemma _root_.finset.strongly_measurable_prod {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, strongly_measurable (f i)) : strongly_measurable (λ a, ∏ i in s, f i a) := by simpa only [← finset.prod_apply] using s.strongly_measurable_prod' hf end comm_monoid /-- The range of a strongly measurable function is separable. -/ lemma is_separable_range {m : measurable_space α} [topological_space β] (hf : strongly_measurable f) : topological_space.is_separable (range f) := begin have : is_separable (closure (⋃ n, range (hf.approx n))) := (is_separable_Union (λ n, (simple_func.finite_range (hf.approx n)).is_separable)).closure, apply this.mono, rintros _ ⟨x, rfl⟩, apply mem_closure_of_tendsto (hf.tendsto_approx x), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact mem_range_self _ end lemma separable_space_range_union_singleton {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] (hf : strongly_measurable f) {b : β} : separable_space (range f ∪ {b} : set β) := begin letI := pseudo_metrizable_space_pseudo_metric β, exact (hf.is_separable_range.union (finite_singleton _).is_separable).separable_space end section second_countable_strongly_measurable variables {mα : measurable_space α} [measurable_space β] include mα /-- In a space with second countable topology, measurable implies strongly measurable. -/ lemma _root_.measurable.strongly_measurable [topological_space β] [pseudo_metrizable_space β] [second_countable_topology β] [opens_measurable_space β] (hf : measurable f) : strongly_measurable f := begin letI := pseudo_metrizable_space_pseudo_metric β, rcases is_empty_or_nonempty β; resetI, { exact subsingleton.strongly_measurable f, }, { inhabit β, exact ⟨simple_func.approx_on f hf set.univ default (set.mem_univ _), λ x, simple_func.tendsto_approx_on hf (set.mem_univ _) (by simp)⟩, }, end /-- In a space with second countable topology, strongly measurable and measurable are equivalent. -/ lemma _root_.strongly_measurable_iff_measurable [topological_space β] [metrizable_space β] [borel_space β] [second_countable_topology β] : strongly_measurable f ↔ measurable f := ⟨λ h, h.measurable, λ h, measurable.strongly_measurable h⟩ lemma _root_.strongly_measurable_id [topological_space α] [pseudo_metrizable_space α] [opens_measurable_space α] [second_countable_topology α] : strongly_measurable (id : α → α) := measurable_id.strongly_measurable end second_countable_strongly_measurable /-- A function is strongly measurable if and only if it is measurable and has separable range. -/ theorem _root_.strongly_measurable_iff_measurable_separable {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] : strongly_measurable f ↔ (measurable f ∧ is_separable (range f)) := begin refine ⟨λ H, ⟨H.measurable, H.is_separable_range⟩, _⟩, rintros ⟨H, H'⟩, letI := pseudo_metrizable_space_pseudo_metric β, let g := cod_restrict f (closure (range f)) (λ x, subset_closure (mem_range_self x)), have fg : f = (coe : closure (range f) → β) ∘ g, by { ext x, refl }, have T : measurable_embedding (coe : closure (range f) → β), { apply closed_embedding.measurable_embedding, exact closed_embedding_subtype_coe is_closed_closure }, have g_meas : measurable g, { rw fg at H, exact T.measurable_comp_iff.1 H }, haveI : second_countable_topology (closure (range f)), { suffices : separable_space (closure (range f)), by exactI uniform_space.second_countable_of_separable _, exact (is_separable.closure H').separable_space }, have g_smeas : strongly_measurable g := measurable.strongly_measurable g_meas, rw fg, exact continuous_subtype_coe.comp_strongly_measurable g_smeas, end /-- A continuous function is strongly measurable when either the source space or the target space is second-countable. -/ lemma _root_.continuous.strongly_measurable [measurable_space α] [topological_space α] [opens_measurable_space α] {β : Type*} [topological_space β] [pseudo_metrizable_space β] [h : second_countable_topology_either α β] {f : α → β} (hf : continuous f) : strongly_measurable f := begin borelize β, casesI h.out, { rw strongly_measurable_iff_measurable_separable, refine ⟨hf.measurable, _⟩, rw ← image_univ, exact (is_separable_of_separable_space univ).image hf }, { exact hf.measurable.strongly_measurable } end /-- If `g` is a topological embedding, then `f` is strongly measurable iff `g ∘ f` is. -/ lemma _root_.embedding.comp_strongly_measurable_iff {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] [topological_space γ] [pseudo_metrizable_space γ] {g : β → γ} {f : α → β} (hg : embedding g) : strongly_measurable (λ x, g (f x)) ↔ strongly_measurable f := begin letI := pseudo_metrizable_space_pseudo_metric γ, borelize [β, γ], refine ⟨λ H, strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, λ H, hg.continuous.comp_strongly_measurable H⟩, { let G : β → range g := cod_restrict g (range g) mem_range_self, have hG : closed_embedding G := { closed_range := begin convert is_closed_univ, apply eq_univ_of_forall, rintros ⟨-, ⟨x, rfl⟩⟩, exact mem_range_self x end, .. hg.cod_restrict _ _ }, have : measurable (G ∘ f) := measurable.subtype_mk H.measurable, exact hG.measurable_embedding.measurable_comp_iff.1 this }, { have : is_separable (g ⁻¹' (range (g ∘ f))) := hg.is_separable_preimage H.is_separable_range, convert this, ext x, simp [hg.inj.eq_iff] } end /-- A sequential limit of strongly measurable functions is strongly measurable. -/ lemma _root_.strongly_measurable_of_tendsto {ι : Type*} {m : measurable_space α} [topological_space β] [pseudo_metrizable_space β] (u : filter ι) [ne_bot u] [is_countably_generated u] {f : ι → α → β} {g : α → β} (hf : ∀ i, strongly_measurable (f i)) (lim : tendsto f u (𝓝 g)) : strongly_measurable g := begin borelize β, refine strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, { exact measurable_of_tendsto_metrizable' u (λ i, (hf i).measurable) lim }, { rcases u.exists_seq_tendsto with ⟨v, hv⟩, have : is_separable (closure (⋃ i, range (f (v i)))) := (is_separable_Union (λ i, (hf (v i)).is_separable_range)).closure, apply this.mono, rintros _ ⟨x, rfl⟩, rw [tendsto_pi_nhds] at lim, apply mem_closure_of_tendsto ((lim x).comp hv), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact mem_range_self _ } end protected lemma piecewise {m : measurable_space α} [topological_space β] {s : set α} {_ : decidable_pred (∈ s)} (hs : measurable_set s) (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (set.piecewise s f g) := begin refine ⟨λ n, simple_func.piecewise s hs (hf.approx n) (hg.approx n), λ x, _⟩, by_cases hx : x ∈ s, { simpa [hx] using hf.tendsto_approx x }, { simpa [hx] using hg.tendsto_approx x }, end /-- this is slightly different from `strongly_measurable.piecewise`. It can be used to show `strongly_measurable (ite (x=0) 0 1)` by `exact strongly_measurable.ite (measurable_set_singleton 0) strongly_measurable_const strongly_measurable_const`, but replacing `strongly_measurable.ite` by `strongly_measurable.piecewise` in that example proof does not work. -/ protected lemma ite {m : measurable_space α} [topological_space β] {p : α → Prop} {_ : decidable_pred p} (hp : measurable_set {a : α | p a}) (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, ite (p x) (f x) (g x)) := strongly_measurable.piecewise hp hf hg lemma _root_.strongly_measurable_of_strongly_measurable_union_cover {m : measurable_space α} [topological_space β] {f : α → β} (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (h : univ ⊆ s ∪ t) (hc : strongly_measurable (λ a : s, f a)) (hd : strongly_measurable (λ a : t, f a)) : strongly_measurable f := begin classical, let f : ℕ → α →ₛ β := λ n, { to_fun := λ x, if hx : x ∈ s then hc.approx n ⟨x, hx⟩ else hd.approx n ⟨x, by simpa [hx] using h (mem_univ x)⟩, measurable_set_fiber' := begin assume x, convert (hs.subtype_image ((hc.approx n).measurable_set_fiber x)).union ((ht.subtype_image ((hd.approx n).measurable_set_fiber x)).diff hs), ext1 y, simp only [mem_union, mem_preimage, mem_singleton_iff, mem_image, set_coe.exists, subtype.coe_mk, exists_and_distrib_right, exists_eq_right, mem_diff], by_cases hy : y ∈ s, { rw dif_pos hy, simp only [hy, exists_true_left, not_true, and_false, or_false]}, { rw dif_neg hy, have A : y ∈ t, by simpa [hy] using h (mem_univ y), simp only [A, hy, false_or, is_empty.exists_iff, not_false_iff, and_true, exists_true_left] } end, finite_range' := begin apply ((hc.approx n).finite_range.union (hd.approx n).finite_range).subset, rintros - ⟨y, rfl⟩, dsimp, by_cases hy : y ∈ s, { left, rw dif_pos hy, exact mem_range_self _ }, { right, rw dif_neg hy, exact mem_range_self _ } end }, refine ⟨f, λ y, _⟩, by_cases hy : y ∈ s, { convert hc.tendsto_approx ⟨y, hy⟩ using 1, ext1 n, simp only [dif_pos hy, simple_func.apply_mk] }, { have A : y ∈ t, by simpa [hy] using h (mem_univ y), convert hd.tendsto_approx ⟨y, A⟩ using 1, ext1 n, simp only [dif_neg hy, simple_func.apply_mk] } end lemma _root_.strongly_measurable_of_restrict_of_restrict_compl {m : measurable_space α} [topological_space β] {f : α → β} {s : set α} (hs : measurable_set s) (h₁ : strongly_measurable (s.restrict f)) (h₂ : strongly_measurable (sᶜ.restrict f)) : strongly_measurable f := strongly_measurable_of_strongly_measurable_union_cover s sᶜ hs hs.compl (union_compl_self s).ge h₁ h₂ protected lemma indicator {m : measurable_space α} [topological_space β] [has_zero β] (hf : strongly_measurable f) {s : set α} (hs : measurable_set s) : strongly_measurable (s.indicator f) := hf.piecewise hs strongly_measurable_const protected lemma dist {m : measurable_space α} {β : Type*} [pseudo_metric_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : strongly_measurable (λ x, dist (f x) (g x)) := continuous_dist.comp_strongly_measurable (hf.prod_mk hg) protected lemma norm {m : measurable_space α} {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : strongly_measurable f) : strongly_measurable (λ x, ‖f x‖) := continuous_norm.comp_strongly_measurable hf protected lemma nnnorm {m : measurable_space α} {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : strongly_measurable f) : strongly_measurable (λ x, ‖f x‖₊) := continuous_nnnorm.comp_strongly_measurable hf protected lemma ennnorm {m : measurable_space α} {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : strongly_measurable f) : measurable (λ a, (‖f a‖₊ : ℝ≥0∞)) := (ennreal.continuous_coe.comp_strongly_measurable hf.nnnorm).measurable protected lemma real_to_nnreal {m : measurable_space α} {f : α → ℝ} (hf : strongly_measurable f) : strongly_measurable (λ x, (f x).to_nnreal) := continuous_real_to_nnreal.comp_strongly_measurable hf lemma _root_.measurable_embedding.strongly_measurable_extend {f : α → β} {g : α → γ} {g' : γ → β} {mα : measurable_space α} {mγ : measurable_space γ} [topological_space β] (hg : measurable_embedding g) (hf : strongly_measurable f) (hg' : strongly_measurable g') : strongly_measurable (function.extend g f g') := begin refine ⟨λ n, simple_func.extend (hf.approx n) g hg (hg'.approx n), _⟩, assume x, by_cases hx : ∃ y, g y = x, { rcases hx with ⟨y, rfl⟩, simpa only [simple_func.extend_apply, hg.injective, injective.extend_apply] using hf.tendsto_approx y }, { simpa only [hx, simple_func.extend_apply', not_false_iff, extend_apply'] using hg'.tendsto_approx x } end lemma _root_.measurable_embedding.exists_strongly_measurable_extend {f : α → β} {g : α → γ} {mα : measurable_space α} {mγ : measurable_space γ} [topological_space β] (hg : measurable_embedding g) (hf : strongly_measurable f) (hne : γ → nonempty β) : ∃ f' : γ → β, strongly_measurable f' ∧ f' ∘ g = f := ⟨function.extend g f (λ x, classical.choice (hne x)), hg.strongly_measurable_extend hf (strongly_measurable_const' $ λ _ _, rfl), funext $ λ x, hg.injective.extend_apply _ _ _⟩ lemma measurable_set_eq_fun {m : measurable_space α} {E} [topological_space E] [metrizable_space E] {f g : α → E} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {x | f x = g x} := begin borelize E × E, exact (hf.prod_mk hg).measurable is_closed_diagonal.measurable_set end lemma measurable_set_lt {m : measurable_space α} [topological_space β] [linear_order β] [order_closed_topology β] [pseudo_metrizable_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {a | f a < g a} := begin borelize β × β, exact (hf.prod_mk hg).measurable is_open_lt_prod.measurable_set end lemma measurable_set_le {m : measurable_space α} [topological_space β] [preorder β] [order_closed_topology β] [pseudo_metrizable_space β] {f g : α → β} (hf : strongly_measurable f) (hg : strongly_measurable g) : measurable_set {a | f a ≤ g a} := begin borelize β × β, exact (hf.prod_mk hg).measurable is_closed_le_prod.measurable_set end lemma strongly_measurable_in_set {m : measurable_space α} [topological_space β] [has_zero β] {s : set α} {f : α → β} (hs : measurable_set s) (hf : strongly_measurable f) (hf_zero : ∀ x ∉ s, f x = 0) : ∃ fs : ℕ → α →ₛ β, (∀ x, tendsto (λ n, fs n x) at_top (𝓝 (f x))) ∧ (∀ (x ∉ s) n, fs n x = 0) := begin let g_seq_s : ℕ → @simple_func α m β := λ n, (hf.approx n).restrict s, have hg_eq : ∀ x ∈ s, ∀ n, g_seq_s n x = hf.approx n x, { intros x hx n, rw [simple_func.coe_restrict _ hs, set.indicator_of_mem hx], }, have hg_zero : ∀ x ∉ s, ∀ n, g_seq_s n x = 0, { intros x hx n, rw [simple_func.coe_restrict _ hs, set.indicator_of_not_mem hx], }, refine ⟨g_seq_s, λ x, _, hg_zero⟩, by_cases hx : x ∈ s, { simp_rw hg_eq x hx, exact hf.tendsto_approx x, }, { simp_rw [hg_zero x hx, hf_zero x hx], exact tendsto_const_nhds, }, end /-- If the restriction to a set `s` of a σ-algebra `m` is included in the restriction to `s` of another σ-algebra `m₂` (hypothesis `hs`), the set `s` is `m` measurable and a function `f` supported on `s` is `m`-strongly-measurable, then `f` is also `m₂`-strongly-measurable. -/ lemma strongly_measurable_of_measurable_space_le_on {α E} {m m₂ : measurable_space α} [topological_space E] [has_zero E] {s : set α} {f : α → E} (hs_m : measurable_set[m] s) (hs : ∀ t, measurable_set[m] (s ∩ t) → measurable_set[m₂] (s ∩ t)) (hf : strongly_measurable[m] f) (hf_zero : ∀ x ∉ s, f x = 0) : strongly_measurable[m₂] f := begin have hs_m₂ : measurable_set[m₂] s, { rw ← set.inter_univ s, refine hs set.univ _, rwa [set.inter_univ], }, obtain ⟨g_seq_s, hg_seq_tendsto, hg_seq_zero⟩ := strongly_measurable_in_set hs_m hf hf_zero, let g_seq_s₂ : ℕ → @simple_func α m₂ E := λ n, { to_fun := g_seq_s n, measurable_set_fiber' := λ x, begin rw [← set.inter_univ ((g_seq_s n) ⁻¹' {x}), ← set.union_compl_self s, set.inter_union_distrib_left, set.inter_comm ((g_seq_s n) ⁻¹' {x})], refine measurable_set.union (hs _ (hs_m.inter _)) _, { exact @simple_func.measurable_set_fiber _ _ m _ _, }, by_cases hx : x = 0, { suffices : (g_seq_s n) ⁻¹' {x} ∩ sᶜ = sᶜ, by { rw this, exact hs_m₂.compl, }, ext1 y, rw [hx, set.mem_inter_iff, set.mem_preimage, set.mem_singleton_iff], exact ⟨λ h, h.2, λ h, ⟨hg_seq_zero y h n, h⟩⟩, }, { suffices : (g_seq_s n) ⁻¹' {x} ∩ sᶜ = ∅, by { rw this, exact measurable_set.empty, }, ext1 y, simp only [mem_inter_iff, mem_preimage, mem_singleton_iff, mem_compl_iff, mem_empty_iff_false, iff_false, not_and, not_not_mem], refine imp_of_not_imp_not _ _ (λ hys, _), rw hg_seq_zero y hys n, exact ne.symm hx, }, end, finite_range' := @simple_func.finite_range _ _ m (g_seq_s n), }, have hg_eq : ∀ x n, g_seq_s₂ n x = g_seq_s n x := λ x n, rfl, refine ⟨g_seq_s₂, λ x, _⟩, simp_rw hg_eq, exact hg_seq_tendsto x, end /-- If a function `f` is strongly measurable w.r.t. a sub-σ-algebra `m` and the measure is σ-finite on `m`, then there exists spanning measurable sets with finite measure on which `f` has bounded norm. In particular, `f` is integrable on each of those sets. -/ lemma exists_spanning_measurable_set_norm_le [seminormed_add_comm_group β] {m m0 : measurable_space α} (hm : m ≤ m0) (hf : strongly_measurable[m] f) (μ : measure α) [sigma_finite (μ.trim hm)] : ∃ s : ℕ → set α, (∀ n, measurable_set[m] (s n) ∧ μ (s n) < ∞ ∧ ∀ x ∈ s n, ‖f x‖ ≤ n) ∧ (⋃ i, s i) = set.univ := begin let sigma_finite_sets := spanning_sets (μ.trim hm), let norm_sets := λ (n : ℕ), {x | ‖f x‖ ≤ n}, have norm_sets_spanning : (⋃ n, norm_sets n) = set.univ, { ext1 x, simp only [set.mem_Union, set.mem_set_of_eq, set.mem_univ, iff_true], exact ⟨⌈‖f x‖⌉₊, nat.le_ceil (‖f x‖)⟩, }, let sets := λ n, sigma_finite_sets n ∩ norm_sets n, have h_meas : ∀ n, measurable_set[m] (sets n), { refine λ n, measurable_set.inter _ _, { exact measurable_spanning_sets (μ.trim hm) n, }, { exact hf.norm.measurable_set_le strongly_measurable_const, }, }, have h_finite : ∀ n, μ (sets n) < ∞, { refine λ n, (measure_mono (set.inter_subset_left _ _)).trans_lt _, exact (le_trim hm).trans_lt (measure_spanning_sets_lt_top (μ.trim hm) n), }, refine ⟨sets, λ n, ⟨h_meas n, h_finite n, _⟩, _⟩, { exact λ x hx, hx.2, }, { have : (⋃ i, sigma_finite_sets i ∩ norm_sets i) = (⋃ i, sigma_finite_sets i) ∩ (⋃ i, norm_sets i), { refine set.Union_inter_of_monotone (monotone_spanning_sets (μ.trim hm)) (λ i j hij x, _), simp only [norm_sets, set.mem_set_of_eq], refine λ hif, hif.trans _, exact_mod_cast hij, }, rw [this, norm_sets_spanning, Union_spanning_sets (μ.trim hm), set.inter_univ], }, end end strongly_measurable /-! ## Finitely strongly measurable functions -/ lemma fin_strongly_measurable_zero {α β} {m : measurable_space α} {μ : measure α} [has_zero β] [topological_space β] : fin_strongly_measurable (0 : α → β) μ := ⟨0, by simp only [pi.zero_apply, simple_func.coe_zero, support_zero', measure_empty, with_top.zero_lt_top, forall_const], λ n, tendsto_const_nhds⟩ namespace fin_strongly_measurable variables {m0 : measurable_space α} {μ : measure α} {f g : α → β} lemma ae_fin_strongly_measurable [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) : ae_fin_strongly_measurable f μ := ⟨f, hf, ae_eq_refl f⟩ section sequence variables [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) /-- A sequence of simple functions such that `∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x))` and `∀ n, μ (support (hf.approx n)) < ∞`. These properties are given by `fin_strongly_measurable.tendsto_approx` and `fin_strongly_measurable.fin_support_approx`. -/ protected noncomputable def approx : ℕ → α →ₛ β := hf.some protected lemma fin_support_approx : ∀ n, μ (support (hf.approx n)) < ∞ := hf.some_spec.1 protected lemma tendsto_approx : ∀ x, tendsto (λ n, hf.approx n x) at_top (𝓝 (f x)) := hf.some_spec.2 end sequence protected lemma strongly_measurable [has_zero β] [topological_space β] (hf : fin_strongly_measurable f μ) : strongly_measurable f := ⟨hf.approx, hf.tendsto_approx⟩ lemma exists_set_sigma_finite [has_zero β] [topological_space β] [t2_space β] (hf : fin_strongly_measurable f μ) : ∃ t, measurable_set t ∧ (∀ x ∈ tᶜ, f x = 0) ∧ sigma_finite (μ.restrict t) := begin rcases hf with ⟨fs, hT_lt_top, h_approx⟩, let T := λ n, support (fs n), have hT_meas : ∀ n, measurable_set (T n), from λ n, simple_func.measurable_set_support (fs n), let t := ⋃ n, T n, refine ⟨t, measurable_set.Union hT_meas, _, _⟩, { have h_fs_zero : ∀ n, ∀ x ∈ tᶜ, fs n x = 0, { intros n x hxt, rw [set.mem_compl_iff, set.mem_Union, not_exists] at hxt, simpa using (hxt n), }, refine λ x hxt, tendsto_nhds_unique (h_approx x) _, rw funext (λ n, h_fs_zero n x hxt), exact tendsto_const_nhds, }, { refine ⟨⟨⟨λ n, tᶜ ∪ T n, λ n, trivial, λ n, _, _⟩⟩⟩, { rw [measure.restrict_apply' (measurable_set.Union hT_meas), set.union_inter_distrib_right, set.compl_inter_self t, set.empty_union], exact (measure_mono (set.inter_subset_left _ _)).trans_lt (hT_lt_top n), }, { rw ← set.union_Union tᶜ T, exact set.compl_union_self _ } } end /-- A finitely strongly measurable function is measurable. -/ protected lemma measurable [has_zero β] [topological_space β] [pseudo_metrizable_space β] [measurable_space β] [borel_space β] (hf : fin_strongly_measurable f μ) : measurable f := hf.strongly_measurable.measurable section arithmetic variables [topological_space β] protected lemma mul [monoid_with_zero β] [has_continuous_mul β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f * g) μ := begin refine ⟨λ n, hf.approx n * hg.approx n, _, λ x, (hf.tendsto_approx x).mul (hg.tendsto_approx x)⟩, intro n, exact (measure_mono (support_mul_subset_left _ _)).trans_lt (hf.fin_support_approx n), end protected lemma add [add_monoid β] [has_continuous_add β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f + g) μ := ⟨λ n, hf.approx n + hg.approx n, λ n, (measure_mono (function.support_add _ _)).trans_lt ((measure_union_le _ _).trans_lt (ennreal.add_lt_top.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩)), λ x, (hf.tendsto_approx x).add (hg.tendsto_approx x)⟩ protected lemma neg [add_group β] [topological_add_group β] (hf : fin_strongly_measurable f μ) : fin_strongly_measurable (-f) μ := begin refine ⟨λ n, -hf.approx n, λ n, _, λ x, (hf.tendsto_approx x).neg⟩, suffices : μ (function.support (λ x, - (hf.approx n) x)) < ∞, by convert this, rw function.support_neg (hf.approx n), exact hf.fin_support_approx n, end protected lemma sub [add_group β] [has_continuous_sub β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f - g) μ := ⟨λ n, hf.approx n - hg.approx n, λ n, (measure_mono (function.support_sub _ _)).trans_lt ((measure_union_le _ _).trans_lt (ennreal.add_lt_top.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩)), λ x, (hf.tendsto_approx x).sub (hg.tendsto_approx x)⟩ protected lemma const_smul {𝕜} [topological_space 𝕜] [add_monoid β] [monoid 𝕜] [distrib_mul_action 𝕜 β] [has_continuous_smul 𝕜 β] (hf : fin_strongly_measurable f μ) (c : 𝕜) : fin_strongly_measurable (c • f) μ := begin refine ⟨λ n, c • (hf.approx n), λ n, _, λ x, (hf.tendsto_approx x).const_smul c⟩, rw simple_func.coe_smul, refine (measure_mono (support_smul_subset_right c _)).trans_lt (hf.fin_support_approx n), end end arithmetic section order variables [topological_space β] [has_zero β] protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f ⊔ g) μ := begin refine ⟨λ n, hf.approx n ⊔ hg.approx n, λ n, _, λ x, (hf.tendsto_approx x).sup_right_nhds (hg.tendsto_approx x)⟩, refine (measure_mono (support_sup _ _)).trans_lt _, exact measure_union_lt_top_iff.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩, end protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : fin_strongly_measurable f μ) (hg : fin_strongly_measurable g μ) : fin_strongly_measurable (f ⊓ g) μ := begin refine ⟨λ n, hf.approx n ⊓ hg.approx n, λ n, _, λ x, (hf.tendsto_approx x).inf_right_nhds (hg.tendsto_approx x)⟩, refine (measure_mono (support_inf _ _)).trans_lt _, exact measure_union_lt_top_iff.mpr ⟨hf.fin_support_approx n, hg.fin_support_approx n⟩, end end order end fin_strongly_measurable lemma fin_strongly_measurable_iff_strongly_measurable_and_exists_set_sigma_finite {α β} {f : α → β} [topological_space β] [t2_space β] [has_zero β] {m : measurable_space α} {μ : measure α} : fin_strongly_measurable f μ ↔ (strongly_measurable f ∧ (∃ t, measurable_set t ∧ (∀ x ∈ tᶜ, f x = 0) ∧ sigma_finite (μ.restrict t))) := ⟨λ hf, ⟨hf.strongly_measurable, hf.exists_set_sigma_finite⟩, λ hf, hf.1.fin_strongly_measurable_of_set_sigma_finite hf.2.some_spec.1 hf.2.some_spec.2.1 hf.2.some_spec.2.2⟩ lemma ae_fin_strongly_measurable_zero {α β} {m : measurable_space α} (μ : measure α) [has_zero β] [topological_space β] : ae_fin_strongly_measurable (0 : α → β) μ := ⟨0, fin_strongly_measurable_zero, eventually_eq.rfl⟩ /-! ## Almost everywhere strongly measurable functions -/ lemma ae_strongly_measurable_const {α β} {m : measurable_space α} {μ : measure α} [topological_space β] {b : β} : ae_strongly_measurable (λ a : α, b) μ := strongly_measurable_const.ae_strongly_measurable @[to_additive] lemma ae_strongly_measurable_one {α β} {m : measurable_space α} {μ : measure α} [topological_space β] [has_one β] : ae_strongly_measurable (1 : α → β) μ := strongly_measurable_one.ae_strongly_measurable @[simp] lemma subsingleton.ae_strongly_measurable {m : measurable_space α} [topological_space β] [subsingleton β] {μ : measure α} (f : α → β) : ae_strongly_measurable f μ := (subsingleton.strongly_measurable f).ae_strongly_measurable @[simp] lemma subsingleton.ae_strongly_measurable' {m : measurable_space α} [topological_space β] [subsingleton α] {μ : measure α} (f : α → β) : ae_strongly_measurable f μ := (subsingleton.strongly_measurable' f).ae_strongly_measurable @[simp] lemma ae_strongly_measurable_zero_measure [measurable_space α] [topological_space β] (f : α → β) : ae_strongly_measurable f (0 : measure α) := begin nontriviality α, inhabit α, exact ⟨λ x, f default, strongly_measurable_const, rfl⟩ end lemma simple_func.ae_strongly_measurable {m : measurable_space α} {μ : measure α} [topological_space β] (f : α →ₛ β) : ae_strongly_measurable f μ := f.strongly_measurable.ae_strongly_measurable namespace ae_strongly_measurable variables {m : measurable_space α} {μ : measure α} [topological_space β] [topological_space γ] {f g : α → β} section mk /-- A `strongly_measurable` function such that `f =ᵐ[μ] hf.mk f`. See lemmas `strongly_measurable_mk` and `ae_eq_mk`. -/ protected noncomputable def mk (f : α → β) (hf : ae_strongly_measurable f μ) : α → β := hf.some lemma strongly_measurable_mk (hf : ae_strongly_measurable f μ) : strongly_measurable (hf.mk f) := hf.some_spec.1 lemma measurable_mk [pseudo_metrizable_space β] [measurable_space β] [borel_space β] (hf : ae_strongly_measurable f μ) : measurable (hf.mk f) := hf.strongly_measurable_mk.measurable lemma ae_eq_mk (hf : ae_strongly_measurable f μ) : f =ᵐ[μ] hf.mk f := hf.some_spec.2 protected lemma ae_measurable {β} [measurable_space β] [topological_space β] [pseudo_metrizable_space β] [borel_space β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_measurable f μ := ⟨hf.mk f, hf.strongly_measurable_mk.measurable, hf.ae_eq_mk⟩ end mk lemma congr (hf : ae_strongly_measurable f μ) (h : f =ᵐ[μ] g) : ae_strongly_measurable g μ := ⟨hf.mk f, hf.strongly_measurable_mk, h.symm.trans hf.ae_eq_mk⟩ lemma _root_.ae_strongly_measurable_congr (h : f =ᵐ[μ] g) : ae_strongly_measurable f μ ↔ ae_strongly_measurable g μ := ⟨λ hf, hf.congr h, λ hg, hg.congr h.symm⟩ lemma mono_measure {ν : measure α} (hf : ae_strongly_measurable f μ) (h : ν ≤ μ) : ae_strongly_measurable f ν := ⟨hf.mk f, hf.strongly_measurable_mk, eventually.filter_mono (ae_mono h) hf.ae_eq_mk⟩ protected lemma mono' {ν : measure α} (h : ae_strongly_measurable f μ) (h' : ν ≪ μ) : ae_strongly_measurable f ν := ⟨h.mk f, h.strongly_measurable_mk, h' h.ae_eq_mk⟩ lemma mono_set {s t} (h : s ⊆ t) (ht : ae_strongly_measurable f (μ.restrict t)) : ae_strongly_measurable f (μ.restrict s) := ht.mono_measure (restrict_mono h le_rfl) protected lemma restrict (hfm : ae_strongly_measurable f μ) {s} : ae_strongly_measurable f (μ.restrict s) := hfm.mono_measure measure.restrict_le_self lemma ae_mem_imp_eq_mk {s} (h : ae_strongly_measurable f (μ.restrict s)) : ∀ᵐ x ∂μ, x ∈ s → f x = h.mk f x := ae_imp_of_ae_restrict h.ae_eq_mk /-- The composition of a continuous function and an ae strongly measurable function is ae strongly measurable. -/ lemma _root_.continuous.comp_ae_strongly_measurable {g : β → γ} {f : α → β} (hg : continuous g) (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, g (f x)) μ := ⟨_, hg.comp_strongly_measurable hf.strongly_measurable_mk, eventually_eq.fun_comp hf.ae_eq_mk g⟩ /-- A continuous function from `α` to `β` is ae strongly measurable when one of the two spaces is second countable. -/ lemma _root_.continuous.ae_strongly_measurable [topological_space α] [opens_measurable_space α] [pseudo_metrizable_space β] [second_countable_topology_either α β] (hf : continuous f) : ae_strongly_measurable f μ := hf.strongly_measurable.ae_strongly_measurable protected lemma prod_mk {f : α → β} {g : α → γ} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, (f x, g x)) μ := ⟨λ x, (hf.mk f x, hg.mk g x), hf.strongly_measurable_mk.prod_mk hg.strongly_measurable_mk, hf.ae_eq_mk.prod_mk hg.ae_eq_mk⟩ /-- In a space with second countable topology, measurable implies ae strongly measurable. -/ lemma _root_.measurable.ae_strongly_measurable {m : measurable_space α} {μ : measure α} [measurable_space β] [pseudo_metrizable_space β] [second_countable_topology β] [opens_measurable_space β] (hf : measurable f) : ae_strongly_measurable f μ := hf.strongly_measurable.ae_strongly_measurable section arithmetic @[to_additive] protected lemma mul [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f * g) μ := ⟨hf.mk f * hg.mk g, hf.strongly_measurable_mk.mul hg.strongly_measurable_mk, hf.ae_eq_mk.mul hg.ae_eq_mk⟩ @[to_additive] protected lemma mul_const [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, f x * c) μ := hf.mul ae_strongly_measurable_const @[to_additive] protected lemma const_mul [has_mul β] [has_continuous_mul β] (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, c * f x) μ := ae_strongly_measurable_const.mul hf @[to_additive] protected lemma inv [group β] [topological_group β] (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (f⁻¹) μ := ⟨(hf.mk f)⁻¹, hf.strongly_measurable_mk.inv, hf.ae_eq_mk.inv⟩ @[to_additive] protected lemma div [group β] [topological_group β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f / g) μ := ⟨hf.mk f / hg.mk g, hf.strongly_measurable_mk.div hg.strongly_measurable_mk, hf.ae_eq_mk.div hg.ae_eq_mk⟩ @[to_additive] protected lemma smul {𝕜} [topological_space 𝕜] [has_smul 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} {g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, f x • g x) μ := continuous_smul.comp_ae_strongly_measurable (hf.prod_mk hg) protected lemma const_smul {𝕜} [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : ae_strongly_measurable f μ) (c : 𝕜) : ae_strongly_measurable (c • f) μ := ⟨c • hf.mk f, hf.strongly_measurable_mk.const_smul c, hf.ae_eq_mk.const_smul c⟩ protected lemma const_smul' {𝕜} [has_smul 𝕜 β] [has_continuous_const_smul 𝕜 β] (hf : ae_strongly_measurable f μ) (c : 𝕜) : ae_strongly_measurable (λ x, c • (f x)) μ := hf.const_smul c @[to_additive] protected lemma smul_const {𝕜} [topological_space 𝕜] [has_smul 𝕜 β] [has_continuous_smul 𝕜 β] {f : α → 𝕜} (hf : ae_strongly_measurable f μ) (c : β) : ae_strongly_measurable (λ x, f x • c) μ := continuous_smul.comp_ae_strongly_measurable (hf.prod_mk ae_strongly_measurable_const) end arithmetic section order protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f ⊔ g) μ := ⟨hf.mk f ⊔ hg.mk g, hf.strongly_measurable_mk.sup hg.strongly_measurable_mk, hf.ae_eq_mk.sup hg.ae_eq_mk⟩ protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (f ⊓ g) μ := ⟨hf.mk f ⊓ hg.mk g, hf.strongly_measurable_mk.inf hg.strongly_measurable_mk, hf.ae_eq_mk.inf hg.ae_eq_mk⟩ end order /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M : Type*} [monoid M] [topological_space M] [has_continuous_mul M] @[to_additive] lemma _root_.list.ae_strongly_measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable l.prod μ := begin induction l with f l ihl, { exact ae_strongly_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[to_additive] lemma _root_.list.ae_strongly_measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (l.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.list_prod_apply] using l.ae_strongly_measurable_prod' hl end monoid section comm_monoid variables {M : Type*} [comm_monoid M] [topological_space M] [has_continuous_mul M] @[to_additive] lemma _root_.multiset.ae_strongly_measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, ae_strongly_measurable f μ) : ae_strongly_measurable l.prod μ := by { rcases l with ⟨l⟩, simpa using l.ae_strongly_measurable_prod' (by simpa using hl) } @[to_additive] lemma _root_.multiset.ae_strongly_measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (s.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.multiset_prod_apply] using s.ae_strongly_measurable_prod' hs @[to_additive] lemma _root_.finset.ae_strongly_measurable_prod' {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, ae_strongly_measurable (f i) μ) : ae_strongly_measurable (∏ i in s, f i) μ := multiset.ae_strongly_measurable_prod' _ $ λ g hg, let ⟨i, hi, hg⟩ := multiset.mem_map.1 hg in (hg ▸ hf _ hi) @[to_additive] lemma _root_.finset.ae_strongly_measurable_prod {ι : Type*} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, ae_strongly_measurable (f i) μ) : ae_strongly_measurable (λ a, ∏ i in s, f i a) μ := by simpa only [← finset.prod_apply] using s.ae_strongly_measurable_prod' hf end comm_monoid section second_countable_ae_strongly_measurable variables [measurable_space β] /-- In a space with second countable topology, measurable implies strongly measurable. -/ lemma _root_.ae_measurable.ae_strongly_measurable [pseudo_metrizable_space β] [opens_measurable_space β] [second_countable_topology β] (hf : ae_measurable f μ) : ae_strongly_measurable f μ := ⟨hf.mk f, hf.measurable_mk.strongly_measurable, hf.ae_eq_mk⟩ lemma _root_.ae_strongly_measurable_id {α : Type*} [topological_space α] [pseudo_metrizable_space α] {m : measurable_space α} [opens_measurable_space α] [second_countable_topology α] {μ : measure α} : ae_strongly_measurable (id : α → α) μ := ae_measurable_id.ae_strongly_measurable /-- In a space with second countable topology, strongly measurable and measurable are equivalent. -/ lemma _root_.ae_strongly_measurable_iff_ae_measurable [pseudo_metrizable_space β] [borel_space β] [second_countable_topology β] : ae_strongly_measurable f μ ↔ ae_measurable f μ := ⟨λ h, h.ae_measurable, λ h, h.ae_strongly_measurable⟩ end second_countable_ae_strongly_measurable protected lemma dist {β : Type*} [pseudo_metric_space β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, dist (f x) (g x)) μ := continuous_dist.comp_ae_strongly_measurable (hf.prod_mk hg) protected lemma norm {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, ‖f x‖) μ := continuous_norm.comp_ae_strongly_measurable hf protected lemma nnnorm {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, ‖f x‖₊) μ := continuous_nnnorm.comp_ae_strongly_measurable hf protected lemma ennnorm {β : Type*} [seminormed_add_comm_group β] {f : α → β} (hf : ae_strongly_measurable f μ) : ae_measurable (λ a, (‖f a‖₊ : ℝ≥0∞)) μ := (ennreal.continuous_coe.comp_ae_strongly_measurable hf.nnnorm).ae_measurable protected lemma edist {β : Type*} [seminormed_add_comm_group β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_measurable (λ a, edist (f a) (g a)) μ := (continuous_edist.comp_ae_strongly_measurable (hf.prod_mk hg)).ae_measurable protected lemma real_to_nnreal {f : α → ℝ} (hf : ae_strongly_measurable f μ) : ae_strongly_measurable (λ x, (f x).to_nnreal) μ := continuous_real_to_nnreal.comp_ae_strongly_measurable hf lemma _root_.ae_strongly_measurable_indicator_iff [has_zero β] {s : set α} (hs : measurable_set s) : ae_strongly_measurable (indicator s f) μ ↔ ae_strongly_measurable f (μ.restrict s) := begin split, { intro h, exact (h.mono_measure measure.restrict_le_self).congr (indicator_ae_eq_restrict hs) }, { intro h, refine ⟨indicator s (h.mk f), h.strongly_measurable_mk.indicator hs, _⟩, have A : s.indicator f =ᵐ[μ.restrict s] s.indicator (h.mk f) := (indicator_ae_eq_restrict hs).trans (h.ae_eq_mk.trans $ (indicator_ae_eq_restrict hs).symm), have B : s.indicator f =ᵐ[μ.restrict sᶜ] s.indicator (h.mk f) := (indicator_ae_eq_restrict_compl hs).trans (indicator_ae_eq_restrict_compl hs).symm, exact ae_of_ae_restrict_of_ae_restrict_compl _ A B }, end protected lemma indicator [has_zero β] (hfm : ae_strongly_measurable f μ) {s : set α} (hs : measurable_set s) : ae_strongly_measurable (s.indicator f) μ := (ae_strongly_measurable_indicator_iff hs).mpr hfm.restrict lemma null_measurable_set_eq_fun {E} [topological_space E] [metrizable_space E] {f g : α → E} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : null_measurable_set {x | f x = g x} μ := begin apply (hf.strongly_measurable_mk.measurable_set_eq_fun hg.strongly_measurable_mk) .null_measurable_set.congr, filter_upwards [hf.ae_eq_mk, hg.ae_eq_mk] with x hfx hgx, change (hf.mk f x = hg.mk g x) = (f x = g x), simp only [hfx, hgx] end lemma null_measurable_set_lt [linear_order β] [order_closed_topology β] [pseudo_metrizable_space β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : null_measurable_set {a | f a < g a} μ := begin apply (hf.strongly_measurable_mk.measurable_set_lt hg.strongly_measurable_mk) .null_measurable_set.congr, filter_upwards [hf.ae_eq_mk, hg.ae_eq_mk] with x hfx hgx, change (hf.mk f x < hg.mk g x) = (f x < g x), simp only [hfx, hgx] end lemma null_measurable_set_le [preorder β] [order_closed_topology β] [pseudo_metrizable_space β] {f g : α → β} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : null_measurable_set {a | f a ≤ g a} μ := begin apply (hf.strongly_measurable_mk.measurable_set_le hg.strongly_measurable_mk) .null_measurable_set.congr, filter_upwards [hf.ae_eq_mk, hg.ae_eq_mk] with x hfx hgx, change (hf.mk f x ≤ hg.mk g x) = (f x ≤ g x), simp only [hfx, hgx] end lemma _root_.ae_strongly_measurable_of_ae_strongly_measurable_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → β} (hf : ae_strongly_measurable f (μ.trim hm)) : ae_strongly_measurable f μ := ⟨hf.mk f, strongly_measurable.mono hf.strongly_measurable_mk hm, ae_eq_of_ae_eq_trim hf.ae_eq_mk⟩ lemma comp_ae_measurable {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hg : ae_strongly_measurable g (measure.map f μ)) (hf : ae_measurable f μ) : ae_strongly_measurable (g ∘ f) μ := ⟨(hg.mk g) ∘ hf.mk f, hg.strongly_measurable_mk.comp_measurable hf.measurable_mk, (ae_eq_comp hf hg.ae_eq_mk).trans ((hf.ae_eq_mk).fun_comp (hg.mk g))⟩ lemma comp_measurable {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hg : ae_strongly_measurable g (measure.map f μ)) (hf : measurable f) : ae_strongly_measurable (g ∘ f) μ := hg.comp_ae_measurable hf.ae_measurable lemma comp_quasi_measure_preserving {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} {ν : measure α} (hg : ae_strongly_measurable g ν) (hf : quasi_measure_preserving f μ ν) : ae_strongly_measurable (g ∘ f) μ := (hg.mono' hf.absolutely_continuous).comp_measurable hf.measurable lemma is_separable_ae_range (hf : ae_strongly_measurable f μ) : ∃ (t : set β), is_separable t ∧ ∀ᵐ x ∂μ, f x ∈ t := begin refine ⟨range (hf.mk f), hf.strongly_measurable_mk.is_separable_range, _⟩, filter_upwards [hf.ae_eq_mk] with x hx, simp [hx] end /-- A function is almost everywhere strongly measurable if and only if it is almost everywhere measurable, and up to a zero measure set its range is contained in a separable set. -/ theorem _root_.ae_strongly_measurable_iff_ae_measurable_separable [pseudo_metrizable_space β] [measurable_space β] [borel_space β] : ae_strongly_measurable f μ ↔ (ae_measurable f μ ∧ ∃ (t : set β), is_separable t ∧ ∀ᵐ x ∂μ, f x ∈ t) := begin refine ⟨λ H, ⟨H.ae_measurable, H.is_separable_ae_range⟩, _⟩, rintros ⟨H, ⟨t, t_sep, ht⟩⟩, rcases eq_empty_or_nonempty t with rfl|h₀, { simp only [mem_empty_iff_false, eventually_false_iff_eq_bot, ae_eq_bot] at ht, rw ht, exact ae_strongly_measurable_zero_measure f }, { obtain ⟨g, g_meas, gt, fg⟩ : ∃ (g : α → β), measurable g ∧ range g ⊆ t ∧ f =ᵐ[μ] g := H.exists_ae_eq_range_subset ht h₀, refine ⟨g, _, fg⟩, exact strongly_measurable_iff_measurable_separable.2 ⟨g_meas, t_sep.mono gt⟩ } end lemma _root_.measurable_embedding.ae_strongly_measurable_map_iff {γ : Type*} {mγ : measurable_space γ} {mα : measurable_space α} {f : γ → α} {μ : measure γ} (hf : measurable_embedding f) {g : α → β} : ae_strongly_measurable g (measure.map f μ) ↔ ae_strongly_measurable (g ∘ f) μ := begin refine ⟨λ H, H.comp_measurable hf.measurable, _⟩, rintro ⟨g₁, hgm₁, heq⟩, rcases hf.exists_strongly_measurable_extend hgm₁ (λ x, ⟨g x⟩) with ⟨g₂, hgm₂, rfl⟩, exact ⟨g₂, hgm₂, hf.ae_map_iff.2 heq⟩ end lemma _root_.embedding.ae_strongly_measurable_comp_iff [pseudo_metrizable_space β] [pseudo_metrizable_space γ] {g : β → γ} {f : α → β} (hg : embedding g) : ae_strongly_measurable (λ x, g (f x)) μ ↔ ae_strongly_measurable f μ := begin letI := pseudo_metrizable_space_pseudo_metric γ, borelize [β, γ], refine ⟨λ H, ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨_, _⟩, λ H, hg.continuous.comp_ae_strongly_measurable H⟩, { let G : β → range g := cod_restrict g (range g) mem_range_self, have hG : closed_embedding G := { closed_range := begin convert is_closed_univ, apply eq_univ_of_forall, rintros ⟨-, ⟨x, rfl⟩⟩, exact mem_range_self x end, .. hg.cod_restrict _ _ }, have : ae_measurable (G ∘ f) μ := ae_measurable.subtype_mk H.ae_measurable, exact hG.measurable_embedding.ae_measurable_comp_iff.1 this }, { rcases (ae_strongly_measurable_iff_ae_measurable_separable.1 H).2 with ⟨t, ht, h't⟩, exact ⟨g⁻¹' t, hg.is_separable_preimage ht, h't⟩ } end lemma _root_.measure_theory.measure_preserving.ae_strongly_measurable_comp_iff {β : Type*} {f : α → β} {mα : measurable_space α} {μa : measure α} {mβ : measurable_space β} {μb : measure β} (hf : measure_preserving f μa μb) (h₂ : measurable_embedding f) {g : β → γ} : ae_strongly_measurable (g ∘ f) μa ↔ ae_strongly_measurable g μb := by rw [← hf.map_eq, h₂.ae_strongly_measurable_map_iff] /-- An almost everywhere sequential limit of almost everywhere strongly measurable functions is almost everywhere strongly measurable. -/ lemma _root_.ae_strongly_measurable_of_tendsto_ae {ι : Type*} [pseudo_metrizable_space β] (u : filter ι) [ne_bot u] [is_countably_generated u] {f : ι → α → β} {g : α → β} (hf : ∀ i, ae_strongly_measurable (f i) μ) (lim : ∀ᵐ x ∂μ, tendsto (λ n, f n x) u (𝓝 (g x))) : ae_strongly_measurable g μ := begin borelize β, refine ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨_, _⟩, { exact ae_measurable_of_tendsto_metrizable_ae _ (λ n, (hf n).ae_measurable) lim }, { rcases u.exists_seq_tendsto with ⟨v, hv⟩, have : ∀ (n : ℕ), ∃ (t : set β), is_separable t ∧ f (v n) ⁻¹' t ∈ μ.ae := λ n, (ae_strongly_measurable_iff_ae_measurable_separable.1 (hf (v n))).2, choose t t_sep ht using this, refine ⟨closure (⋃ i, (t i)), (is_separable_Union (λ i, (t_sep i))).closure, _⟩, filter_upwards [ae_all_iff.2 ht, lim] with x hx h'x, apply mem_closure_of_tendsto ((h'x).comp hv), apply eventually_of_forall (λ n, _), apply mem_Union_of_mem n, exact hx n } end /-- If a sequence of almost everywhere strongly measurable functions converges almost everywhere, one can select a strongly measurable function as the almost everywhere limit. -/ lemma _root_.exists_strongly_measurable_limit_of_tendsto_ae [pseudo_metrizable_space β] {f : ℕ → α → β} (hf : ∀ n, ae_strongly_measurable (f n) μ) (h_ae_tendsto : ∀ᵐ x ∂μ, ∃ l : β, tendsto (λ n, f n x) at_top (𝓝 l)) : ∃ (f_lim : α → β) (hf_lim_meas : strongly_measurable f_lim), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (f_lim x)) := begin borelize β, obtain ⟨g, g_meas, hg⟩ : ∃ (g : α → β) (g_meas : measurable g), ∀ᵐ x ∂μ, tendsto (λ n, f n x) at_top (𝓝 (g x)) := measurable_limit_of_tendsto_metrizable_ae (λ n, (hf n).ae_measurable) h_ae_tendsto, have Hg : ae_strongly_measurable g μ := ae_strongly_measurable_of_tendsto_ae _ hf hg, refine ⟨Hg.mk g, Hg.strongly_measurable_mk, _⟩, filter_upwards [hg, Hg.ae_eq_mk] with x hx h'x, rwa h'x at hx, end lemma sum_measure [pseudo_metrizable_space β] {m : measurable_space α} {μ : ι → measure α} (h : ∀ i, ae_strongly_measurable f (μ i)) : ae_strongly_measurable f (measure.sum μ) := begin borelize β, refine ae_strongly_measurable_iff_ae_measurable_separable.2 ⟨ae_measurable.sum_measure (λ i, (h i).ae_measurable), _⟩, have A : ∀ (i : ι), ∃ (t : set β), is_separable t ∧ f ⁻¹' t ∈ (μ i).ae := λ i, (ae_strongly_measurable_iff_ae_measurable_separable.1 (h i)).2, choose t t_sep ht using A, refine ⟨(⋃ i, t i), is_separable_Union t_sep, _⟩, simp only [measure.ae_sum_eq, mem_Union, eventually_supr], assume i, filter_upwards [ht i] with x hx, exact ⟨i, hx⟩ end @[simp] lemma _root_.ae_strongly_measurable_sum_measure_iff [pseudo_metrizable_space β] {m : measurable_space α} {μ : ι → measure α} : ae_strongly_measurable f (sum μ) ↔ ∀ i, ae_strongly_measurable f (μ i) := ⟨λ h i, h.mono_measure (measure.le_sum _ _), sum_measure⟩ @[simp] lemma _root_.ae_strongly_measurable_add_measure_iff [pseudo_metrizable_space β] {ν : measure α} : ae_strongly_measurable f (μ + ν) ↔ ae_strongly_measurable f μ ∧ ae_strongly_measurable f ν := by { rw [← sum_cond, ae_strongly_measurable_sum_measure_iff, bool.forall_bool, and.comm], refl } lemma add_measure [pseudo_metrizable_space β] {ν : measure α} {f : α → β} (hμ : ae_strongly_measurable f μ) (hν : ae_strongly_measurable f ν) : ae_strongly_measurable f (μ + ν) := ae_strongly_measurable_add_measure_iff.2 ⟨hμ, hν⟩ protected lemma Union [pseudo_metrizable_space β] {s : ι → set α} (h : ∀ i, ae_strongly_measurable f (μ.restrict (s i))) : ae_strongly_measurable f (μ.restrict (⋃ i, s i)) := (sum_measure h).mono_measure $ restrict_Union_le @[simp] lemma _root_.ae_strongly_measurable_Union_iff [pseudo_metrizable_space β] {s : ι → set α} : ae_strongly_measurable f (μ.restrict (⋃ i, s i)) ↔ ∀ i, ae_strongly_measurable f (μ.restrict (s i)) := ⟨λ h i, h.mono_measure $ restrict_mono (subset_Union _ _) le_rfl, ae_strongly_measurable.Union⟩ @[simp] lemma _root_.ae_strongly_measurable_union_iff [pseudo_metrizable_space β] {s t : set α} : ae_strongly_measurable f (μ.restrict (s ∪ t)) ↔ ae_strongly_measurable f (μ.restrict s) ∧ ae_strongly_measurable f (μ.restrict t) := by simp only [union_eq_Union, ae_strongly_measurable_Union_iff, bool.forall_bool, cond, and.comm] lemma ae_strongly_measurable_uIoc_iff [linear_order α] [pseudo_metrizable_space β] {f : α → β} {a b : α} : ae_strongly_measurable f (μ.restrict $ uIoc a b) ↔ ae_strongly_measurable f (μ.restrict $ Ioc a b) ∧ ae_strongly_measurable f (μ.restrict $ Ioc b a) := by rw [uIoc_eq_union, ae_strongly_measurable_union_iff] lemma smul_measure {R : Type*} [monoid R] [distrib_mul_action R ℝ≥0∞] [is_scalar_tower R ℝ≥0∞ ℝ≥0∞] (h : ae_strongly_measurable f μ) (c : R) : ae_strongly_measurable f (c • μ) := ⟨h.mk f, h.strongly_measurable_mk, ae_smul_measure h.ae_eq_mk c⟩ section normed_space variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] [complete_space 𝕜] variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] lemma _root_.ae_strongly_measurable_smul_const_iff {f : α → 𝕜} {c : E} (hc : c ≠ 0) : ae_strongly_measurable (λ x, f x • c) μ ↔ ae_strongly_measurable f μ := (closed_embedding_smul_left hc).to_embedding.ae_strongly_measurable_comp_iff end normed_space section mul_action variables {M G G₀ : Type*} variables [monoid M] [mul_action M β] [has_continuous_const_smul M β] variables [group G] [mul_action G β] [has_continuous_const_smul G β] variables [group_with_zero G₀] [mul_action G₀ β] [has_continuous_const_smul G₀ β] lemma _root_.ae_strongly_measurable_const_smul_iff (c : G) : ae_strongly_measurable (λ x, c • f x) μ ↔ ae_strongly_measurable f μ := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ lemma _root_.is_unit.ae_strongly_measurable_const_smul_iff {c : M} (hc : is_unit c) : ae_strongly_measurable (λ x, c • f x) μ ↔ ae_strongly_measurable f μ := let ⟨u, hu⟩ := hc in hu ▸ ae_strongly_measurable_const_smul_iff u lemma _root_.ae_strongly_measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : ae_strongly_measurable (λ x, c • f x) μ ↔ ae_strongly_measurable f μ := (is_unit.mk0 _ hc).ae_strongly_measurable_const_smul_iff end mul_action section continuous_linear_map_nontrivially_normed_field variables {𝕜 : Type*} [nontrivially_normed_field 𝕜] variables {E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E] variables {F : Type*} [normed_add_comm_group F] [normed_space 𝕜 F] variables {G : Type*} [normed_add_comm_group G] [normed_space 𝕜 G] lemma _root_.strongly_measurable.apply_continuous_linear_map {m : measurable_space α} {φ : α → F →L[𝕜] E} (hφ : strongly_measurable φ) (v : F) : strongly_measurable (λ a, φ a v) := (continuous_linear_map.apply 𝕜 E v).continuous.comp_strongly_measurable hφ lemma apply_continuous_linear_map {φ : α → F →L[𝕜] E} (hφ : ae_strongly_measurable φ μ) (v : F) : ae_strongly_measurable (λ a, φ a v) μ := (continuous_linear_map.apply 𝕜 E v).continuous.comp_ae_strongly_measurable hφ lemma _root_.continuous_linear_map.ae_strongly_measurable_comp₂ (L : E →L[𝕜] F →L[𝕜] G) {f : α → E} {g : α → F} (hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) : ae_strongly_measurable (λ x, L (f x) (g x)) μ := L.continuous₂.comp_ae_strongly_measurable $ hf.prod_mk hg end continuous_linear_map_nontrivially_normed_field lemma _root_.ae_strongly_measurable_with_density_iff {E : Type*} [normed_add_comm_group E] [normed_space ℝ E] {f : α → ℝ≥0} (hf : measurable f) {g : α → E} : ae_strongly_measurable g (μ.with_density (λ x, (f x : ℝ≥0∞))) ↔ ae_strongly_measurable (λ x, (f x : ℝ) • g x) μ := begin split, { rintros ⟨g', g'meas, hg'⟩, have A : measurable_set {x : α | f x ≠ 0} := (hf (measurable_set_singleton 0)).compl, refine ⟨λ x, (f x : ℝ) • g' x, hf.coe_nnreal_real.strongly_measurable.smul g'meas, _⟩, apply @ae_of_ae_restrict_of_ae_restrict_compl _ _ _ {x | f x ≠ 0}, { rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal] at hg', rw ae_restrict_iff' A, filter_upwards [hg'] with a ha h'a, have : (f a : ℝ≥0∞) ≠ 0, by simpa only [ne.def, ennreal.coe_eq_zero] using h'a, rw ha this }, { filter_upwards [ae_restrict_mem A.compl] with x hx, simp only [not_not, mem_set_of_eq, mem_compl_iff] at hx, simp [hx] } }, { rintros ⟨g', g'meas, hg'⟩, refine ⟨λ x, (f x : ℝ)⁻¹ • g' x, hf.coe_nnreal_real.inv.strongly_measurable.smul g'meas, _⟩, rw [eventually_eq, ae_with_density_iff hf.coe_nnreal_ennreal], filter_upwards [hg'] with x hx h'x, rw [← hx, smul_smul, _root_.inv_mul_cancel, one_smul], simp only [ne.def, ennreal.coe_eq_zero] at h'x, simpa only [nnreal.coe_eq_zero, ne.def] using h'x } end end ae_strongly_measurable lemma ae_strongly_measurable_of_absolutely_continuous {α β : Type*} [measurable_space α] [topological_space β] {μ ν : measure α} (h : ν ≪ μ) (g : α → β) (hμ : ae_strongly_measurable g μ) : ae_strongly_measurable g ν := begin obtain ⟨g₁, hg₁, hg₁'⟩ := hμ, refine ⟨g₁, hg₁, h.ae_eq hg₁'⟩, end /-! ## Almost everywhere finitely strongly measurable functions -/ namespace ae_fin_strongly_measurable variables {m : measurable_space α} {μ : measure α} [topological_space β] {f g : α → β} section mk variables [has_zero β] /-- A `fin_strongly_measurable` function such that `f =ᵐ[μ] hf.mk f`. See lemmas `fin_strongly_measurable_mk` and `ae_eq_mk`. -/ protected noncomputable def mk (f : α → β) (hf : ae_fin_strongly_measurable f μ) : α → β := hf.some lemma fin_strongly_measurable_mk (hf : ae_fin_strongly_measurable f μ) : fin_strongly_measurable (hf.mk f) μ := hf.some_spec.1 lemma ae_eq_mk (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ] hf.mk f := hf.some_spec.2 protected lemma ae_measurable {β} [has_zero β] [measurable_space β] [topological_space β] [pseudo_metrizable_space β] [borel_space β] {f : α → β} (hf : ae_fin_strongly_measurable f μ) : ae_measurable f μ := ⟨hf.mk f, hf.fin_strongly_measurable_mk.measurable, hf.ae_eq_mk⟩ end mk section arithmetic protected lemma mul [monoid_with_zero β] [has_continuous_mul β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f * g) μ := ⟨hf.mk f * hg.mk g, hf.fin_strongly_measurable_mk.mul hg.fin_strongly_measurable_mk, hf.ae_eq_mk.mul hg.ae_eq_mk⟩ protected lemma add [add_monoid β] [has_continuous_add β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f + g) μ := ⟨hf.mk f + hg.mk g, hf.fin_strongly_measurable_mk.add hg.fin_strongly_measurable_mk, hf.ae_eq_mk.add hg.ae_eq_mk⟩ protected lemma neg [add_group β] [topological_add_group β] (hf : ae_fin_strongly_measurable f μ) : ae_fin_strongly_measurable (-f) μ := ⟨-hf.mk f, hf.fin_strongly_measurable_mk.neg, hf.ae_eq_mk.neg⟩ protected lemma sub [add_group β] [has_continuous_sub β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f - g) μ := ⟨hf.mk f - hg.mk g, hf.fin_strongly_measurable_mk.sub hg.fin_strongly_measurable_mk, hf.ae_eq_mk.sub hg.ae_eq_mk⟩ protected lemma const_smul {𝕜} [topological_space 𝕜] [add_monoid β] [monoid 𝕜] [distrib_mul_action 𝕜 β] [has_continuous_smul 𝕜 β] (hf : ae_fin_strongly_measurable f μ) (c : 𝕜) : ae_fin_strongly_measurable (c • f) μ := ⟨c • hf.mk f, hf.fin_strongly_measurable_mk.const_smul c, hf.ae_eq_mk.const_smul c⟩ end arithmetic section order variables [has_zero β] protected lemma sup [semilattice_sup β] [has_continuous_sup β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f ⊔ g) μ := ⟨hf.mk f ⊔ hg.mk g, hf.fin_strongly_measurable_mk.sup hg.fin_strongly_measurable_mk, hf.ae_eq_mk.sup hg.ae_eq_mk⟩ protected lemma inf [semilattice_inf β] [has_continuous_inf β] (hf : ae_fin_strongly_measurable f μ) (hg : ae_fin_strongly_measurable g μ) : ae_fin_strongly_measurable (f ⊓ g) μ := ⟨hf.mk f ⊓ hg.mk g, hf.fin_strongly_measurable_mk.inf hg.fin_strongly_measurable_mk, hf.ae_eq_mk.inf hg.ae_eq_mk⟩ end order variables [has_zero β] [t2_space β] lemma exists_set_sigma_finite (hf : ae_fin_strongly_measurable f μ) : ∃ t, measurable_set t ∧ f =ᵐ[μ.restrict tᶜ] 0 ∧ sigma_finite (μ.restrict t) := begin rcases hf with ⟨g, hg, hfg⟩, obtain ⟨t, ht, hgt_zero, htμ⟩ := hg.exists_set_sigma_finite, refine ⟨t, ht, _, htμ⟩, refine eventually_eq.trans (ae_restrict_of_ae hfg) _, rw [eventually_eq, ae_restrict_iff' ht.compl], exact eventually_of_forall hgt_zero, end /-- A measurable set `t` such that `f =ᵐ[μ.restrict tᶜ] 0` and `sigma_finite (μ.restrict t)`. -/ def sigma_finite_set (hf : ae_fin_strongly_measurable f μ) : set α := hf.exists_set_sigma_finite.some protected lemma measurable_set (hf : ae_fin_strongly_measurable f μ) : measurable_set hf.sigma_finite_set := hf.exists_set_sigma_finite.some_spec.1 lemma ae_eq_zero_compl (hf : ae_fin_strongly_measurable f μ) : f =ᵐ[μ.restrict hf.sigma_finite_setᶜ] 0 := hf.exists_set_sigma_finite.some_spec.2.1 instance sigma_finite_restrict (hf : ae_fin_strongly_measurable f μ) : sigma_finite (μ.restrict hf.sigma_finite_set) := hf.exists_set_sigma_finite.some_spec.2.2 end ae_fin_strongly_measurable section second_countable_topology variables {G : Type*} {p : ℝ≥0∞} {m m0 : measurable_space α} {μ : measure α} [seminormed_add_comm_group G] [measurable_space G] [borel_space G] [second_countable_topology G] {f : α → G} /-- In a space with second countable topology and a sigma-finite measure, `fin_strongly_measurable` and `measurable` are equivalent. -/ lemma fin_strongly_measurable_iff_measurable {m0 : measurable_space α} (μ : measure α) [sigma_finite μ] : fin_strongly_measurable f μ ↔ measurable f := ⟨λ h, h.measurable, λ h, (measurable.strongly_measurable h).fin_strongly_measurable μ⟩ /-- In a space with second countable topology and a sigma-finite measure, `ae_fin_strongly_measurable` and `ae_measurable` are equivalent. -/ lemma ae_fin_strongly_measurable_iff_ae_measurable {m0 : measurable_space α} (μ : measure α) [sigma_finite μ] : ae_fin_strongly_measurable f μ ↔ ae_measurable f μ := by simp_rw [ae_fin_strongly_measurable, ae_measurable, fin_strongly_measurable_iff_measurable] end second_countable_topology lemma measurable_uncurry_of_continuous_of_measurable {α β ι : Type*} [topological_space ι] [metrizable_space ι] [measurable_space ι] [second_countable_topology ι] [opens_measurable_space ι] {mβ : measurable_space β} [topological_space β] [pseudo_metrizable_space β] [borel_space β] {m : measurable_space α} {u : ι → α → β} (hu_cont : ∀ x, continuous (λ i, u i x)) (h : ∀ i, measurable (u i)) : measurable (function.uncurry u) := begin obtain ⟨t_sf, ht_sf⟩ : ∃ t : ℕ → simple_func ι ι, ∀ j x, tendsto (λ n, u (t n j) x) at_top (𝓝 $ u j x), { have h_str_meas : strongly_measurable (id : ι → ι), from strongly_measurable_id, refine ⟨h_str_meas.approx, λ j x, _⟩, exact ((hu_cont x).tendsto j).comp (h_str_meas.tendsto_approx j), }, let U := λ (n : ℕ) (p : ι × α), u (t_sf n p.fst) p.snd, have h_tendsto : tendsto U at_top (𝓝 (λ p, u p.fst p.snd)), { rw tendsto_pi_nhds, exact λ p, ht_sf p.fst p.snd, }, refine measurable_of_tendsto_metrizable (λ n, _) h_tendsto, have h_meas : measurable (λ (p : (t_sf n).range × α), u ↑p.fst p.snd), { have : (λ (p : ↥((t_sf n).range) × α), u ↑(p.fst) p.snd) = (λ (p : α × ((t_sf n).range)), u ↑(p.snd) p.fst) ∘ prod.swap := rfl, rw [this, @measurable_swap_iff α ↥((t_sf n).range) β m], exact measurable_from_prod_countable (λ j, h j), }, have : (λ p : ι × α, u (t_sf n p.fst) p.snd) = (λ p : ↥(t_sf n).range × α, u p.fst p.snd) ∘ (λ p : ι × α, (⟨t_sf n p.fst, simple_func.mem_range_self _ _⟩, p.snd)) := rfl, simp_rw [U, this], refine h_meas.comp (measurable.prod_mk _ measurable_snd), exact ((t_sf n).measurable.comp measurable_fst).subtype_mk, end lemma strongly_measurable_uncurry_of_continuous_of_strongly_measurable {α β ι : Type*} [topological_space ι] [metrizable_space ι] [measurable_space ι] [second_countable_topology ι] [opens_measurable_space ι] [topological_space β] [pseudo_metrizable_space β] [measurable_space α] {u : ι → α → β} (hu_cont : ∀ x, continuous (λ i, u i x)) (h : ∀ i, strongly_measurable (u i)) : strongly_measurable (function.uncurry u) := begin borelize β, obtain ⟨t_sf, ht_sf⟩ : ∃ t : ℕ → simple_func ι ι, ∀ j x, tendsto (λ n, u (t n j) x) at_top (𝓝 $ u j x), { have h_str_meas : strongly_measurable (id : ι → ι), from strongly_measurable_id, refine ⟨h_str_meas.approx, λ j x, _⟩, exact ((hu_cont x).tendsto j).comp (h_str_meas.tendsto_approx j), }, let U := λ (n : ℕ) (p : ι × α), u (t_sf n p.fst) p.snd, have h_tendsto : tendsto U at_top (𝓝 (λ p, u p.fst p.snd)), { rw tendsto_pi_nhds, exact λ p, ht_sf p.fst p.snd, }, refine strongly_measurable_of_tendsto _ (λ n, _) h_tendsto, have h_str_meas : strongly_measurable (λ (p : (t_sf n).range × α), u ↑p.fst p.snd), { refine strongly_measurable_iff_measurable_separable.2 ⟨_, _⟩, { have : (λ (p : ↥((t_sf n).range) × α), u ↑(p.fst) p.snd) = (λ (p : α × ((t_sf n).range)), u ↑(p.snd) p.fst) ∘ prod.swap := rfl, rw [this, measurable_swap_iff], exact measurable_from_prod_countable (λ j, (h j).measurable), }, { have : is_separable (⋃ (i : (t_sf n).range), range (u i)) := is_separable_Union (λ i, (h i).is_separable_range), apply this.mono, rintros _ ⟨⟨i, x⟩, rfl⟩, simp only [mem_Union, mem_range], exact ⟨i, x, rfl⟩ } }, have : (λ p : ι × α, u (t_sf n p.fst) p.snd) = (λ p : ↥(t_sf n).range × α, u p.fst p.snd) ∘ (λ p : ι × α, (⟨t_sf n p.fst, simple_func.mem_range_self _ _⟩, p.snd)) := rfl, simp_rw [U, this], refine h_str_meas.comp_measurable (measurable.prod_mk _ measurable_snd), exact ((t_sf n).measurable.comp measurable_fst).subtype_mk, end end measure_theory -- Guard against import creep assert_not_exists inner_product_space
26d875c379da689a55aa5ba9463a5506898b6a5d
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/convex/strict_convex_between.lean
2e32dcbc204d5b4db8f303c2450b7feb4fe35cc0
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
3,473
lean
/- Copyright (c) 2022 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import analysis.convex.between import analysis.convex.strict_convex_space /-! # Betweenness in affine spaces for strictly convex spaces This file proves results about betweenness for points in an affine space for a strictly convex space. -/ variables {V P : Type*} [normed_add_comm_group V] [normed_space ℝ V] [pseudo_metric_space P] variables [normed_add_torsor V P] [strict_convex_space ℝ V] include V lemma sbtw.dist_lt_max_dist (p : P) {p₁ p₂ p₃ : P} (h : sbtw ℝ p₁ p₂ p₃) : dist p₂ p < max (dist p₁ p) (dist p₃ p) := begin have hp₁p₃ : p₁ -ᵥ p ≠ p₃ -ᵥ p, { by simpa using h.left_ne_right }, rw [sbtw, ←wbtw_vsub_const_iff p, wbtw, affine_segment_eq_segment, ←insert_endpoints_open_segment, set.mem_insert_iff, set.mem_insert_iff] at h, rcases h with ⟨h | h | h, hp₂p₁, hp₂p₃⟩, { rw vsub_left_cancel_iff at h, exact false.elim (hp₂p₁ h) }, { rw vsub_left_cancel_iff at h, exact false.elim (hp₂p₃ h) }, { rw [open_segment_eq_image, set.mem_image] at h, rcases h with ⟨r, ⟨hr0, hr1⟩, hr⟩, simp_rw [@dist_eq_norm_vsub V, ←hr], exact norm_combo_lt_of_ne (le_max_left _ _) (le_max_right _ _) hp₁p₃ (sub_pos.2 hr1) hr0 (by abel) } end lemma wbtw.dist_le_max_dist (p : P) {p₁ p₂ p₃ : P} (h : wbtw ℝ p₁ p₂ p₃) : dist p₂ p ≤ max (dist p₁ p) (dist p₃ p) := begin by_cases hp₁ : p₂ = p₁, { simp [hp₁] }, by_cases hp₃ : p₂ = p₃, { simp [hp₃] }, have hs : sbtw ℝ p₁ p₂ p₃ := ⟨h, hp₁, hp₃⟩, exact (hs.dist_lt_max_dist _).le end /-- Given three collinear points, two (not equal) with distance `r` from `p` and one with distance at most `r` from `p`, the third point is weakly between the other two points. -/ lemma collinear.wbtw_of_dist_eq_of_dist_le {p p₁ p₂ p₃ : P} {r : ℝ} (h : collinear ℝ ({p₁, p₂, p₃} : set P)) (hp₁ : dist p₁ p = r) (hp₂ : dist p₂ p ≤ r) (hp₃ : dist p₃ p = r) (hp₁p₃ : p₁ ≠ p₃) : wbtw ℝ p₁ p₂ p₃ := begin rcases h.wbtw_or_wbtw_or_wbtw with hw | hw | hw, { exact hw }, { by_cases hp₃p₂ : p₃ = p₂, { simp [hp₃p₂] }, have hs : sbtw ℝ p₂ p₃ p₁ := ⟨hw, hp₃p₂, hp₁p₃.symm⟩, have hs' := hs.dist_lt_max_dist p, rw [hp₁, hp₃, lt_max_iff, lt_self_iff_false, or_false] at hs', exact false.elim (hp₂.not_lt hs') }, { by_cases hp₁p₂ : p₁ = p₂, { simp [hp₁p₂] }, have hs : sbtw ℝ p₃ p₁ p₂ := ⟨hw, hp₁p₃, hp₁p₂⟩, have hs' := hs.dist_lt_max_dist p, rw [hp₁, hp₃, lt_max_iff, lt_self_iff_false, false_or] at hs', exact false.elim (hp₂.not_lt hs') } end /-- Given three collinear points, two (not equal) with distance `r` from `p` and one with distance less than `r` from `p`, the third point is strictly between the other two points. -/ lemma collinear.sbtw_of_dist_eq_of_dist_lt {p p₁ p₂ p₃ : P} {r : ℝ} (h : collinear ℝ ({p₁, p₂, p₃} : set P)) (hp₁ : dist p₁ p = r) (hp₂ : dist p₂ p < r) (hp₃ : dist p₃ p = r) (hp₁p₃ : p₁ ≠ p₃) : sbtw ℝ p₁ p₂ p₃ := begin refine ⟨h.wbtw_of_dist_eq_of_dist_le hp₁ hp₂.le hp₃ hp₁p₃, _, _⟩, { rintro rfl, exact hp₂.ne hp₁ }, { rintro rfl, exact hp₂.ne hp₃ } end
f14160374d4c53eb7c6f6c34a9ff1834429c5f2a
7cef822f3b952965621309e88eadf618da0c8ae9
/src/tactic/omega/coeffs.lean
5c2811beed1f737ca1a008c4b40f90ce8c5ce863
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
10,111
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Non-constant terms of linear constraints are represented by storing their coefficients in integer lists. -/ import data.list.basic import tactic.ring import tactic.omega.misc namespace omega namespace coeffs open list.func variable {v : nat → int} @[simp] def val_between (v : nat → int) (as : list int) (l : nat) : nat → int | 0 := 0 | (o+1) := (val_between o) + (get (l+o) as * v (l+o)) @[simp] lemma val_between_nil {l : nat} : ∀ m, val_between v [] l m = 0 | 0 := by simp only [val_between] | (m+1) := by simp only [val_between_nil m, omega.coeffs.val_between, get_nil, zero_add, zero_mul, int.default_eq_zero] def val (v : nat → int) (as : list int) : int := val_between v as 0 as.length @[simp] lemma val_nil : val v [] = 0 := rfl lemma val_between_eq_of_le {as : list int} {l : nat} : ∀ m, as.length ≤ l + m → val_between v as l m = val_between v as l (as.length - l) | 0 h1 := begin rw (nat.sub_eq_zero_iff_le.elim_right _), apply h1 end | (m+1) h1 := begin rw le_iff_eq_or_lt at h1, cases h1, { rw [h1, add_comm l, nat.add_sub_cancel] }, have h2 : list.length as ≤ l + m, { rw ← nat.lt_succ_iff, apply h1 }, simpa [ get_eq_default_of_le _ h2, zero_mul, add_zero, val_between ] using val_between_eq_of_le _ h2 end lemma val_eq_of_le {as : list int} {k : nat} : as.length ≤ k → val v as = val_between v as 0 k := begin intro h1, unfold val, rw [val_between_eq_of_le k _], refl, rw zero_add, exact h1 end lemma val_between_eq_val_between {v w : nat → int} {as bs : list int} {l : nat} : ∀ {m}, (∀ x, l ≤ x → x < l + m → v x = w x) → (∀ x, l ≤ x → x < l + m → get x as = get x bs) → val_between v as l m = val_between w bs l m | 0 h1 h2 := rfl | (m+1) h1 h2 := begin unfold val_between, have h3 : l + m < l + (m + 1), { rw ← add_assoc, apply lt_add_one }, apply fun_mono_2, apply val_between_eq_val_between; intros x h4 h5, { apply h1 _ h4 (lt_trans h5 h3) }, { apply h2 _ h4 (lt_trans h5 h3) }, rw [h1 _ _ h3, h2 _ _ h3]; apply nat.le_add_right end open_locale list.func lemma val_between_set {a : int} {l n : nat} : ∀ {m}, l ≤ n → n < l + m → val_between v ([] {n ↦ a}) l m = a * v n | 0 h1 h2 := begin exfalso, apply lt_irrefl l (lt_of_le_of_lt h1 h2) end | (m+1) h1 h2 := begin rw [← add_assoc, nat.lt_succ_iff, le_iff_eq_or_lt] at h2, cases h2; unfold val_between, { have h3 : val_between v ([] {l + m ↦ a}) l m = 0, { apply @eq.trans _ _ (val_between v [] l m), { apply val_between_eq_val_between, { intros, refl }, { intros x h4 h5, rw [get_nil, get_set_eq_of_ne, get_nil], apply ne_of_lt h5 } }, apply val_between_nil }, rw h2, simp only [h3, zero_add, list.func.get_set] }, { have h3 : l + m ≠ n, { apply ne_of_gt h2 }, rw [@val_between_set m h1 h2, get_set_eq_of_ne _ _ h3], simp only [h3, get_nil, add_zero, zero_mul, int.default_eq_zero] } end @[simp] lemma val_set {m : nat} {a : int} : val v ([] {m ↦ a}) = a * v m := begin apply val_between_set, apply zero_le, apply lt_of_lt_of_le (lt_add_one _), simp only [length_set, zero_add, le_max_right], end lemma val_between_neg {as : list int} {l : nat} : ∀ {o}, val_between v (neg as) l o = -(val_between v as l o) | 0 := rfl | (o+1) := begin unfold val_between, rw [neg_add, neg_mul_eq_neg_mul], apply fun_mono_2, apply val_between_neg, apply fun_mono_2 _ rfl, apply get_neg end @[simp] lemma val_neg {as : list int} : val v (neg as) = -(val v as) := by simpa only [val, length_neg] using val_between_neg lemma val_between_add {is js : list int} {l : nat} : ∀ m, val_between v (add is js) l m = (val_between v is l m) + (val_between v js l m) | 0 := rfl | (m+1) := by { simp only [val_between, val_between_add m, list.func.get, get_add], ring } @[simp] lemma val_add {is js : list int} : val v (add is js) = (val v is) + (val v js) := begin unfold val, rw val_between_add, apply fun_mono_2; apply val_between_eq_of_le; rw [zero_add, length_add], apply le_max_left, apply le_max_right end lemma val_between_sub {is js : list int} {l : nat} : ∀ m, val_between v (sub is js) l m = (val_between v is l m) - (val_between v js l m) | 0 := rfl | (m+1) := by { simp only [val_between, val_between_sub m, list.func.get, get_sub], ring } @[simp] lemma val_sub {is js : list int} : val v (sub is js) = (val v is) - (val v js) := begin unfold val, rw val_between_sub, apply fun_mono_2; apply val_between_eq_of_le; rw [zero_add, length_sub], apply le_max_left, apply le_max_right end def val_except (k : nat) (v : nat → int) (as) := val_between v as 0 k + val_between v as (k+1) (as.length - (k+1)) lemma val_except_eq_val_except {k : nat} {is js : list int} {v w : nat → int} : (∀ x ≠ k, v x = w x) → (∀ x ≠ k, get x is = get x js) → val_except k v is = val_except k w js := begin intros h1 h2, unfold val_except, apply fun_mono_2, { apply val_between_eq_val_between; intros x h3 h4; [ {apply h1}, {apply h2} ]; apply ne_of_lt; rw zero_add at h4; apply h4 }, { repeat { rw ← val_between_eq_of_le ((max is.length js.length) - (k+1)) }, { apply val_between_eq_val_between; intros x h3 h4; [ {apply h1}, {apply h2} ]; apply ne.symm; apply ne_of_lt; rw nat.lt_iff_add_one_le; exact h3 }, repeat { rw add_comm, apply le_trans _ (nat.le_sub_add _ _), { apply le_max_right <|> apply le_max_left } } } end open_locale omega lemma val_except_update_set {n : nat} {as : list int} {i j : int} : val_except n (v⟨n ↦ i⟩) (as {n ↦ j}) = val_except n v as := by apply val_except_eq_val_except update_eq_of_ne (get_set_eq_of_ne _) lemma val_between_add_val_between {as : list int} {l m : nat} : ∀ {n}, val_between v as l m + val_between v as (l+m) n = val_between v as l (m+n) | 0 := by simp only [val_between, add_zero] | (n+1) := begin rw ← add_assoc, unfold val_between, rw add_assoc, rw ← @val_between_add_val_between n, ring, end lemma val_except_add_eq (n : nat) {as : list int} : (val_except n v as) + ((get n as) * (v n)) = val v as := begin unfold val_except, unfold val, by_cases h1 : n + 1 ≤ as.length, { have h4 := @val_between_add_val_between v as 0 (n+1) (as.length - (n+1)), have h5 : n + 1 + (as.length - (n + 1)) = as.length, { rw [add_comm, nat.sub_add_cancel h1] }, rw h5 at h4, apply eq.trans _ h4, simp only [val_between, zero_add], ring }, rw not_lt at h1, have h2 : (list.length as - (n + 1)) = 0, { apply nat.sub_eq_zero_of_le (le_trans h1 (nat.le_add_right _ _)) }, have h3 : val_between v as 0 (list.length as) = val_between v as 0 (n + 1), { simpa only [val] using @val_eq_of_le v as (n+1) (le_trans h1 (nat.le_add_right _ _)) }, simp only [add_zero, val_between, zero_add, h2, h3] end @[simp] lemma val_between_map_mul {i : int} {as: list int} {l : nat} : ∀ {m}, val_between v (list.map ((*) i) as) l m = i * val_between v as l m | 0 := by simp only [val_between, mul_zero, list.map] | (m+1) := begin unfold val_between, rw [@val_between_map_mul m, mul_add], apply fun_mono_2 rfl, by_cases h1 : l + m < as.length, { rw [get_map h1, mul_assoc] }, rw not_lt at h1, rw [get_eq_default_of_le, get_eq_default_of_le]; try {simp}; apply h1 end lemma forall_val_dvd_of_forall_mem_dvd {i : int} {as : list int} : (∀ x ∈ as, i ∣ x) → (∀ n, i ∣ get n as) | h1 n := by { apply forall_val_of_forall_mem _ h1, apply dvd_zero } lemma dvd_val_between {i} {as: list int} {l : nat} : ∀ {m}, (∀ x ∈ as, i ∣ x) → (i ∣ val_between v as l m) | 0 h1 := dvd_zero _ | (m+1) h1 := begin unfold val_between, apply dvd_add, apply dvd_val_between h1, apply dvd_mul_of_dvd_left, by_cases h2 : get (l+m) as = 0, { rw h2, apply dvd_zero }, apply h1, apply mem_get_of_ne_zero h2 end lemma dvd_val {as : list int} {i : int} : (∀ x ∈ as, i ∣ x) → (i ∣ val v as) := by apply dvd_val_between @[simp] lemma val_between_map_div {as: list int} {i : int} {l : nat} (h1 : ∀ x ∈ as, i ∣ x) : ∀ {m}, val_between v (list.map (λ x, x / i) as) l m = (val_between v as l m) / i | 0 := by simp only [int.zero_div, val_between, list.map] | (m+1) := begin unfold val_between, rw [@val_between_map_div m, int.add_div_of_dvd (dvd_val_between h1)], apply fun_mono_2 rfl, { apply calc get (l + m) (list.map (λ (x : ℤ), x / i) as) * v (l + m) = ((get (l + m) as) / i) * v (l + m) : begin apply fun_mono_2 _ rfl, rw get_map', apply int.zero_div end ... = get (l + m) as * v (l + m) / i : begin repeat {rw mul_comm _ (v (l+m))}, rw int.mul_div_assoc, apply forall_val_dvd_of_forall_mem_dvd h1 end }, apply dvd_mul_of_dvd_left, apply forall_val_dvd_of_forall_mem_dvd h1, end @[simp] lemma val_map_div {as : list int} {i : int} : (∀ x ∈ as, i ∣ x) → val v (list.map (λ x, x / i) as) = (val v as) / i := by {intro h1, simpa only [val, list.length_map] using val_between_map_div h1} lemma val_between_eq_zero {is: list int} {l : nat} : ∀ {m}, (∀ x : int, x ∈ is → x = 0) → val_between v is l m = 0 | 0 h1 := rfl | (m+1) h1 := begin have h2 := @forall_val_of_forall_mem _ _ is (λ x, x = 0) rfl h1, simpa only [val_between, h2 (l+m), zero_mul, add_zero] using @val_between_eq_zero m h1, end lemma val_eq_zero {is : list int} : (∀ x : int, x ∈ is → x = 0) → val v is = 0 := by apply val_between_eq_zero end coeffs end omega
de16aee7d1c38df908838aeebf430a01a07158a0
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/AssocTimesRingoid.lean
e11c0f8dd14b87ed4fb65bc6efddce2558c4681a
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
9,229
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section AssocTimesRingoid structure AssocTimesRingoid (A : Type) : Type := (times : (A → (A → A))) (plus : (A → (A → A))) (associative_times : (∀ {x y z : A} , (times (times x y) z) = (times x (times y z)))) open AssocTimesRingoid structure Sig (AS : Type) : Type := (timesS : (AS → (AS → AS))) (plusS : (AS → (AS → AS))) structure Product (A : Type) : Type := (timesP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (plusP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (associative_timesP : (∀ {xP yP zP : (Prod A A)} , (timesP (timesP xP yP) zP) = (timesP xP (timesP yP zP)))) structure Hom {A1 : Type} {A2 : Type} (As1 : (AssocTimesRingoid A1)) (As2 : (AssocTimesRingoid A2)) : Type := (hom : (A1 → A2)) (pres_times : (∀ {x1 x2 : A1} , (hom ((times As1) x1 x2)) = ((times As2) (hom x1) (hom x2)))) (pres_plus : (∀ {x1 x2 : A1} , (hom ((plus As1) x1 x2)) = ((plus As2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (As1 : (AssocTimesRingoid A1)) (As2 : (AssocTimesRingoid A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_times : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((times As1) x1 x2) ((times As2) y1 y2)))))) (interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus As1) x1 x2) ((plus As2) y1 y2)))))) inductive AssocTimesRingoidTerm : Type | timesL : (AssocTimesRingoidTerm → (AssocTimesRingoidTerm → AssocTimesRingoidTerm)) | plusL : (AssocTimesRingoidTerm → (AssocTimesRingoidTerm → AssocTimesRingoidTerm)) open AssocTimesRingoidTerm inductive ClAssocTimesRingoidTerm (A : Type) : Type | sing : (A → ClAssocTimesRingoidTerm) | timesCl : (ClAssocTimesRingoidTerm → (ClAssocTimesRingoidTerm → ClAssocTimesRingoidTerm)) | plusCl : (ClAssocTimesRingoidTerm → (ClAssocTimesRingoidTerm → ClAssocTimesRingoidTerm)) open ClAssocTimesRingoidTerm inductive OpAssocTimesRingoidTerm (n : ℕ) : Type | v : ((fin n) → OpAssocTimesRingoidTerm) | timesOL : (OpAssocTimesRingoidTerm → (OpAssocTimesRingoidTerm → OpAssocTimesRingoidTerm)) | plusOL : (OpAssocTimesRingoidTerm → (OpAssocTimesRingoidTerm → OpAssocTimesRingoidTerm)) open OpAssocTimesRingoidTerm inductive OpAssocTimesRingoidTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpAssocTimesRingoidTerm2) | sing2 : (A → OpAssocTimesRingoidTerm2) | timesOL2 : (OpAssocTimesRingoidTerm2 → (OpAssocTimesRingoidTerm2 → OpAssocTimesRingoidTerm2)) | plusOL2 : (OpAssocTimesRingoidTerm2 → (OpAssocTimesRingoidTerm2 → OpAssocTimesRingoidTerm2)) open OpAssocTimesRingoidTerm2 def simplifyCl {A : Type} : ((ClAssocTimesRingoidTerm A) → (ClAssocTimesRingoidTerm A)) | (timesCl x1 x2) := (timesCl (simplifyCl x1) (simplifyCl x2)) | (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpAssocTimesRingoidTerm n) → (OpAssocTimesRingoidTerm n)) | (timesOL x1 x2) := (timesOL (simplifyOpB x1) (simplifyOpB x2)) | (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpAssocTimesRingoidTerm2 n A) → (OpAssocTimesRingoidTerm2 n A)) | (timesOL2 x1 x2) := (timesOL2 (simplifyOp x1) (simplifyOp x2)) | (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((AssocTimesRingoid A) → (AssocTimesRingoidTerm → A)) | As (timesL x1 x2) := ((times As) (evalB As x1) (evalB As x2)) | As (plusL x1 x2) := ((plus As) (evalB As x1) (evalB As x2)) def evalCl {A : Type} : ((AssocTimesRingoid A) → ((ClAssocTimesRingoidTerm A) → A)) | As (sing x1) := x1 | As (timesCl x1 x2) := ((times As) (evalCl As x1) (evalCl As x2)) | As (plusCl x1 x2) := ((plus As) (evalCl As x1) (evalCl As x2)) def evalOpB {A : Type} {n : ℕ} : ((AssocTimesRingoid A) → ((vector A n) → ((OpAssocTimesRingoidTerm n) → A))) | As vars (v x1) := (nth vars x1) | As vars (timesOL x1 x2) := ((times As) (evalOpB As vars x1) (evalOpB As vars x2)) | As vars (plusOL x1 x2) := ((plus As) (evalOpB As vars x1) (evalOpB As vars x2)) def evalOp {A : Type} {n : ℕ} : ((AssocTimesRingoid A) → ((vector A n) → ((OpAssocTimesRingoidTerm2 n A) → A))) | As vars (v2 x1) := (nth vars x1) | As vars (sing2 x1) := x1 | As vars (timesOL2 x1 x2) := ((times As) (evalOp As vars x1) (evalOp As vars x2)) | As vars (plusOL2 x1 x2) := ((plus As) (evalOp As vars x1) (evalOp As vars x2)) def inductionB {P : (AssocTimesRingoidTerm → Type)} : ((∀ (x1 x2 : AssocTimesRingoidTerm) , ((P x1) → ((P x2) → (P (timesL x1 x2))))) → ((∀ (x1 x2 : AssocTimesRingoidTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → (∀ (x : AssocTimesRingoidTerm) , (P x)))) | ptimesl pplusl (timesL x1 x2) := (ptimesl _ _ (inductionB ptimesl pplusl x1) (inductionB ptimesl pplusl x2)) | ptimesl pplusl (plusL x1 x2) := (pplusl _ _ (inductionB ptimesl pplusl x1) (inductionB ptimesl pplusl x2)) def inductionCl {A : Type} {P : ((ClAssocTimesRingoidTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClAssocTimesRingoidTerm A)) , ((P x1) → ((P x2) → (P (timesCl x1 x2))))) → ((∀ (x1 x2 : (ClAssocTimesRingoidTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → (∀ (x : (ClAssocTimesRingoidTerm A)) , (P x))))) | psing ptimescl ppluscl (sing x1) := (psing x1) | psing ptimescl ppluscl (timesCl x1 x2) := (ptimescl _ _ (inductionCl psing ptimescl ppluscl x1) (inductionCl psing ptimescl ppluscl x2)) | psing ptimescl ppluscl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing ptimescl ppluscl x1) (inductionCl psing ptimescl ppluscl x2)) def inductionOpB {n : ℕ} {P : ((OpAssocTimesRingoidTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpAssocTimesRingoidTerm n)) , ((P x1) → ((P x2) → (P (timesOL x1 x2))))) → ((∀ (x1 x2 : (OpAssocTimesRingoidTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → (∀ (x : (OpAssocTimesRingoidTerm n)) , (P x))))) | pv ptimesol pplusol (v x1) := (pv x1) | pv ptimesol pplusol (timesOL x1 x2) := (ptimesol _ _ (inductionOpB pv ptimesol pplusol x1) (inductionOpB pv ptimesol pplusol x2)) | pv ptimesol pplusol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv ptimesol pplusol x1) (inductionOpB pv ptimesol pplusol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpAssocTimesRingoidTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpAssocTimesRingoidTerm2 n A)) , ((P x1) → ((P x2) → (P (timesOL2 x1 x2))))) → ((∀ (x1 x2 : (OpAssocTimesRingoidTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → (∀ (x : (OpAssocTimesRingoidTerm2 n A)) , (P x)))))) | pv2 psing2 ptimesol2 pplusol2 (v2 x1) := (pv2 x1) | pv2 psing2 ptimesol2 pplusol2 (sing2 x1) := (psing2 x1) | pv2 psing2 ptimesol2 pplusol2 (timesOL2 x1 x2) := (ptimesol2 _ _ (inductionOp pv2 psing2 ptimesol2 pplusol2 x1) (inductionOp pv2 psing2 ptimesol2 pplusol2 x2)) | pv2 psing2 ptimesol2 pplusol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 ptimesol2 pplusol2 x1) (inductionOp pv2 psing2 ptimesol2 pplusol2 x2)) def stageB : (AssocTimesRingoidTerm → (Staged AssocTimesRingoidTerm)) | (timesL x1 x2) := (stage2 timesL (codeLift2 timesL) (stageB x1) (stageB x2)) | (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClAssocTimesRingoidTerm A) → (Staged (ClAssocTimesRingoidTerm A))) | (sing x1) := (Now (sing x1)) | (timesCl x1 x2) := (stage2 timesCl (codeLift2 timesCl) (stageCl x1) (stageCl x2)) | (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpAssocTimesRingoidTerm n) → (Staged (OpAssocTimesRingoidTerm n))) | (v x1) := (const (code (v x1))) | (timesOL x1 x2) := (stage2 timesOL (codeLift2 timesOL) (stageOpB x1) (stageOpB x2)) | (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpAssocTimesRingoidTerm2 n A) → (Staged (OpAssocTimesRingoidTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (timesOL2 x1 x2) := (stage2 timesOL2 (codeLift2 timesOL2) (stageOp x1) (stageOp x2)) | (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (timesT : ((Repr A) → ((Repr A) → (Repr A)))) (plusT : ((Repr A) → ((Repr A) → (Repr A)))) end AssocTimesRingoid
de8d1751c1d4d63edb7240ed132340c269d4ec64
f7315930643edc12e76c229a742d5446dad77097
/tests/lean/run/tac1.lean
be474766405867846fe3c6fa48382d15ad8e2117
[ "Apache-2.0" ]
permissive
bmalehorn/lean
8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1
53653c352643751c4b62ff63ec5e555f11dae8eb
refs/heads/master
1,610,945,684,489
1,429,681,220,000
1,429,681,449,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
93
lean
import logic open tactic definition mytac := apply @and.intro; apply @eq.refl check @mytac
17c3fb3004d0c3fa8379538b9fb25f197c49530c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/covering/besicovitch_vector_space.lean
8c4b787747f5c3b4aaff81a506e8487a444ca6a4
[ "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
26,136
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.haar_lebesgue import measure_theory.covering.besicovitch /-! # Satellite configurations for Besicovitch covering lemma in vector spaces The Besicovitch covering theorem ensures that, in a nice metric space, there exists a number `N` such that, from any family of balls with bounded radii, one can extract `N` families, each made of disjoint balls, covering together all the centers of the initial family. A key tool in the proof of this theorem is the notion of a satellite configuration, i.e., a family of `N + 1` balls, where the first `N` balls all intersect the last one, but none of them contains the center of another one and their radii are controlled. This is a technical notion, but it shows up naturally in the proof of the Besicovitch theorem (which goes through a greedy algorithm): to ensure that in the end one needs at most `N` families of balls, the crucial property of the underlying metric space is that there should be no satellite configuration of `N + 1` points. This file is devoted to the study of this property in vector spaces: we prove the main result of [Füredi and Loeb, On the best constant for the Besicovitch covering theorem][furedi-loeb1994], which shows that the optimal such `N` in a vector space coincides with the maximal number of points one can put inside the unit ball of radius `2` under the condition that their distances are bounded below by `1`. In particular, this number is bounded by `5 ^ dim` by a straightforward measure argument. ## Main definitions and results * `multiplicity E` is the maximal number of points one can put inside the unit ball of radius `2` in the vector space `E`, under the condition that their distances are bounded below by `1`. * `multiplicity_le E` shows that `multiplicity E ≤ 5 ^ (dim E)`. * `good_τ E` is a constant `> 1`, but close enough to `1` that satellite configurations with this parameter `τ` are not worst than for `τ = 1`. * `is_empty_satellite_config_multiplicity` is the main theorem, saying that there are no satellite configurations of `(multiplicity E) + 1` points, for the parameter `good_τ E`. -/ universe u open metric set finite_dimensional measure_theory filter fin open_locale ennreal topological_space noncomputable theory namespace besicovitch variables {E : Type*} [normed_add_comm_group E] namespace satellite_config variables [normed_space ℝ E] {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) /-- Rescaling a satellite configuration in a vector space, to put the basepoint at `0` and the base radius at `1`. -/ def center_and_rescale : satellite_config E N τ := { c := λ i, (a.r (last N))⁻¹ • (a.c i - a.c (last N)), r := λ i, (a.r (last N))⁻¹ * a.r i, rpos := λ i, mul_pos (inv_pos.2 (a.rpos _)) (a.rpos _), h := λ i j hij, begin rcases a.h i j hij with H|H, { left, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } }, { right, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } }, end, hlast := λ i hi, begin have H := a.hlast i hi, split, { rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le)], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw [dist_eq_norm] at H, convert H.1 using 2, abel }, { rw [← mul_assoc, mul_comm τ, mul_assoc], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), exact H.2 } end, inter := λ i hi, begin have H := a.inter i hi, rw [dist_eq_norm, ← smul_sub, norm_smul, real.norm_eq_abs, abs_of_nonneg (inv_nonneg.2 ((a.rpos _)).le), ← mul_add], refine mul_le_mul_of_nonneg_left _ (inv_nonneg.2 ((a.rpos _)).le), rw dist_eq_norm at H, convert H using 2, abel end } lemma center_and_rescale_center : a.center_and_rescale.c (last N) = 0 := by simp [satellite_config.center_and_rescale] lemma center_and_rescale_radius {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) : a.center_and_rescale.r (last N) = 1 := by simp [satellite_config.center_and_rescale, inv_mul_cancel (a.rpos _).ne'] end satellite_config /-! ### Disjoint balls of radius close to `1` in the radius `2` ball. -/ /-- The maximum cardinality of a `1`-separated set in the ball of radius `2`. This is also the optimal number of families in the Besicovitch covering theorem. -/ def multiplicity (E : Type*) [normed_add_comm_group E] := Sup {N | ∃ s : finset E, s.card = N ∧ (∀ c ∈ s, ‖c‖ ≤ 2) ∧ (∀ c ∈ s, ∀ d ∈ s, c ≠ d → 1 ≤ ‖c - d‖)} section variables [normed_space ℝ E] [finite_dimensional ℝ E] /-- Any `1`-separated set in the ball of radius `2` has cardinality at most `5 ^ dim`. This is useful to show that the supremum in the definition of `besicovitch.multiplicity E` is well behaved. -/ lemma card_le_of_separated (s : finset E) (hs : ∀ c ∈ s, ‖c‖ ≤ 2) (h : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ‖c - d‖) : s.card ≤ 5 ^ (finrank ℝ E) := begin /- We consider balls of radius `1/2` around the points in `s`. They are disjoint, and all contained in the ball of radius `5/2`. A volume argument gives `s.card * (1/2)^dim ≤ (5/2)^dim`, i.e., `s.card ≤ 5^dim`. -/ borelize E, let μ : measure E := measure.add_haar, let δ : ℝ := (1 : ℝ)/2, let ρ : ℝ := (5 : ℝ)/2, have ρpos : 0 < ρ := by norm_num [ρ], set A := ⋃ (c ∈ s), ball (c : E) δ with hA, have D : set.pairwise (s : set E) (disjoint on (λ c, ball (c : E) δ)), { rintros c hc d hd hcd, apply ball_disjoint_ball, rw dist_eq_norm, convert h c hc d hd hcd, norm_num }, have A_subset : A ⊆ ball (0 : E) ρ, { refine Union₂_subset (λ x hx, _), apply ball_subset_ball', calc δ + dist x 0 ≤ δ + 2 : by { rw dist_zero_right, exact add_le_add le_rfl (hs x hx) } ... = 5 / 2 : by norm_num [δ] }, have I : (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) * μ (ball 0 1) ≤ ennreal.of_real (ρ ^ (finrank ℝ E)) * μ (ball 0 1) := calc (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) * μ (ball 0 1) = μ A : begin rw [hA, measure_bUnion_finset D (λ c hc, measurable_set_ball)], have I : 0 < δ, by norm_num [δ], simp only [μ.add_haar_ball_of_pos _ I, one_div, one_pow, finset.sum_const, nsmul_eq_mul, div_pow, mul_assoc] end ... ≤ μ (ball (0 : E) ρ) : measure_mono A_subset ... = ennreal.of_real (ρ ^ (finrank ℝ E)) * μ (ball 0 1) : by simp only [μ.add_haar_ball_of_pos _ ρpos], have J : (s.card : ℝ≥0∞) * ennreal.of_real (δ ^ (finrank ℝ E)) ≤ ennreal.of_real (ρ ^ (finrank ℝ E)) := (ennreal.mul_le_mul_right (measure_ball_pos _ _ zero_lt_one).ne' measure_ball_lt_top.ne).1 I, have K : (s.card : ℝ) ≤ (5 : ℝ) ^ finrank ℝ E, by simpa [ennreal.to_real_mul, div_eq_mul_inv] using ennreal.to_real_le_of_le_of_real (pow_nonneg ρpos.le _) J, exact_mod_cast K, end lemma multiplicity_le : multiplicity E ≤ 5 ^ (finrank ℝ E) := begin apply cSup_le, { refine ⟨0, ⟨∅, by simp⟩⟩ }, { rintros _ ⟨s, ⟨rfl, h⟩⟩, exact besicovitch.card_le_of_separated s h.1 h.2 } end lemma card_le_multiplicity {s : finset E} (hs : ∀ c ∈ s, ‖c‖ ≤ 2) (h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ‖c - d‖) : s.card ≤ multiplicity E := begin apply le_cSup, { refine ⟨5 ^ (finrank ℝ E), _⟩, rintros _ ⟨s, ⟨rfl, h⟩⟩, exact besicovitch.card_le_of_separated s h.1 h.2 }, { simp only [mem_set_of_eq, ne.def], exact ⟨s, rfl, hs, h's⟩ } end variable (E) /-- If `δ` is small enough, a `(1-δ)`-separated set in the ball of radius `2` also has cardinality at most `multiplicity E`. -/ lemma exists_good_δ : ∃ (δ : ℝ), 0 < δ ∧ δ < 1 ∧ ∀ (s : finset E), (∀ c ∈ s, ‖c‖ ≤ 2) → (∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - δ ≤ ‖c - d‖) → s.card ≤ multiplicity E := begin /- This follows from a compactness argument: otherwise, one could extract a converging subsequence, to obtain a `1`-separated set in the ball of radius `2` with cardinality `N = multiplicity E + 1`. To formalize this, we work with functions `fin N → E`. -/ classical, by_contra' h, set N := multiplicity E + 1 with hN, have : ∀ (δ : ℝ), 0 < δ → ∃ f : fin N → E, (∀ (i : fin N), ‖f i‖ ≤ 2) ∧ (∀ i j, i ≠ j → 1 - δ ≤ ‖f i - f j‖), { assume δ hδ, rcases lt_or_le δ 1 with hδ'|hδ', { rcases h δ hδ hδ' with ⟨s, hs, h's, s_card⟩, obtain ⟨f, f_inj, hfs⟩ : ∃ (f : fin N → E), function.injective f ∧ range f ⊆ ↑s, { have : fintype.card (fin N) ≤ s.card, { simp only [fintype.card_fin], exact s_card }, rcases function.embedding.exists_of_card_le_finset this with ⟨f, hf⟩, exact ⟨f, f.injective, hf⟩ }, simp only [range_subset_iff, finset.mem_coe] at hfs, refine ⟨f, λ i, hs _ (hfs i), λ i j hij, h's _ (hfs i) _ (hfs j) (f_inj.ne hij)⟩ }, { exact ⟨λ i, 0, λ i, by simp, λ i j hij, by simpa only [norm_zero, sub_nonpos, sub_self]⟩ } }, -- For `δ > 0`, `F δ` is a function from `fin N` to the ball of radius `2` for which two points -- in the image are separated by `1 - δ`. choose! F hF using this, -- Choose a converging subsequence when `δ → 0`. have : ∃ f : fin N → E, (∀ (i : fin N), ‖f i‖ ≤ 2) ∧ (∀ i j, i ≠ j → 1 ≤ ‖f i - f j‖), { obtain ⟨u, u_mono, zero_lt_u, hu⟩ : ∃ (u : ℕ → ℝ), (∀ (m n : ℕ), m < n → u n < u m) ∧ (∀ (n : ℕ), 0 < u n) ∧ filter.tendsto u filter.at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ), have A : ∀ n, F (u n) ∈ closed_ball (0 : fin N → E) 2, { assume n, simp only [pi_norm_le_iff_of_nonneg zero_le_two, mem_closed_ball, dist_zero_right, (hF (u n) (zero_lt_u n)).left, forall_const], }, obtain ⟨f, fmem, φ, φ_mono, hf⟩ : ∃ (f ∈ closed_ball (0 : fin N → E) 2) (φ : ℕ → ℕ), strict_mono φ ∧ tendsto ((F ∘ u) ∘ φ) at_top (𝓝 f) := is_compact.tendsto_subseq (is_compact_closed_ball _ _) A, refine ⟨f, λ i, _, λ i j hij, _⟩, { simp only [pi_norm_le_iff_of_nonneg zero_le_two, mem_closed_ball, dist_zero_right] at fmem, exact fmem i }, { have A : tendsto (λ n, ‖F (u (φ n)) i - F (u (φ n)) j‖) at_top (𝓝 (‖f i - f j‖)) := ((hf.apply i).sub (hf.apply j)).norm, have B : tendsto (λ n, 1 - u (φ n)) at_top (𝓝 (1 - 0)) := tendsto_const_nhds.sub (hu.comp φ_mono.tendsto_at_top), rw sub_zero at B, exact le_of_tendsto_of_tendsto' B A (λ n, (hF (u (φ n)) (zero_lt_u _)).2 i j hij) } }, rcases this with ⟨f, hf, h'f⟩, -- the range of `f` contradicts the definition of `multiplicity E`. have finj : function.injective f, { assume i j hij, by_contra, have : 1 ≤ ‖f i - f j‖ := h'f i j h, simp only [hij, norm_zero, sub_self] at this, exact lt_irrefl _ (this.trans_lt zero_lt_one) }, let s := finset.image f finset.univ, have s_card : s.card = N, by { rw finset.card_image_of_injective _ finj, exact finset.card_fin N }, have hs : ∀ c ∈ s, ‖c‖ ≤ 2, by simp only [hf, forall_apply_eq_imp_iff', forall_const, forall_exists_index, finset.mem_univ, finset.mem_image], have h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 ≤ ‖c - d‖, { simp only [s, forall_apply_eq_imp_iff', forall_exists_index, finset.mem_univ, finset.mem_image, ne.def, exists_true_left, forall_apply_eq_imp_iff', forall_true_left], assume i j hij, have : i ≠ j := λ h, by { rw h at hij, exact hij rfl }, exact h'f i j this }, have : s.card ≤ multiplicity E := card_le_multiplicity hs h's, rw [s_card, hN] at this, exact lt_irrefl _ ((nat.lt_succ_self (multiplicity E)).trans_le this), end /-- A small positive number such that any `1 - δ`-separated set in the ball of radius `2` has cardinality at most `besicovitch.multiplicity E`. -/ def good_δ : ℝ := (exists_good_δ E).some lemma good_δ_lt_one : good_δ E < 1 := (exists_good_δ E).some_spec.2.1 /-- A number `τ > 1`, but chosen close enough to `1` so that the construction in the Besicovitch covering theorem using this parameter `τ` will give the smallest possible number of covering families. -/ def good_τ : ℝ := 1 + (good_δ E) / 4 lemma one_lt_good_τ : 1 < good_τ E := by { dsimp [good_τ, good_δ], linarith [(exists_good_δ E).some_spec.1] } variable {E} lemma card_le_multiplicity_of_δ {s : finset E} (hs : ∀ c ∈ s, ‖c‖ ≤ 2) (h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - good_δ E ≤ ‖c - d‖) : s.card ≤ multiplicity E := (classical.some_spec (exists_good_δ E)).2.2 s hs h's lemma le_multiplicity_of_δ_of_fin {n : ℕ} (f : fin n → E) (h : ∀ i, ‖f i‖ ≤ 2) (h' : ∀ i j, i ≠ j → 1 - good_δ E ≤ ‖f i - f j‖) : n ≤ multiplicity E := begin classical, have finj : function.injective f, { assume i j hij, by_contra, have : 1 - good_δ E ≤ ‖f i - f j‖ := h' i j h, simp only [hij, norm_zero, sub_self] at this, linarith [good_δ_lt_one E] }, let s := finset.image f finset.univ, have s_card : s.card = n, by { rw finset.card_image_of_injective _ finj, exact finset.card_fin n }, have hs : ∀ c ∈ s, ‖c‖ ≤ 2, by simp only [h, forall_apply_eq_imp_iff', forall_const, forall_exists_index, finset.mem_univ, finset.mem_image, implies_true_iff], have h's : ∀ (c ∈ s) (d ∈ s), c ≠ d → 1 - good_δ E ≤ ‖c - d‖, { simp only [s, forall_apply_eq_imp_iff', forall_exists_index, finset.mem_univ, finset.mem_image, ne.def, exists_true_left, forall_apply_eq_imp_iff', forall_true_left], assume i j hij, have : i ≠ j := λ h, by { rw h at hij, exact hij rfl }, exact h' i j this }, have : s.card ≤ multiplicity E := card_le_multiplicity_of_δ hs h's, rwa [s_card] at this, end end namespace satellite_config /-! ### Relating satellite configurations to separated points in the ball of radius `2`. We prove that the number of points in a satellite configuration is bounded by the maximal number of `1`-separated points in the ball of radius `2`. For this, start from a satellite congifuration `c`. Without loss of generality, one can assume that the last ball is centered at `0` and of radius `1`. Define `c' i = c i` if `‖c i‖ ≤ 2`, and `c' i = (2/‖c i‖) • c i` if `‖c i‖ > 2`. It turns out that these points are `1 - δ`-separated, where `δ` is arbitrarily small if `τ` is close enough to `1`. The number of such configurations is bounded by `multiplicity E` if `δ` is suitably small. To check that the points `c' i` are `1 - δ`-separated, one treats separately the cases where both `‖c i‖` and `‖c j‖` are `≤ 2`, where one of them is `≤ 2` and the other one is `> 2`, and where both of them are `> 2`. -/ lemma exists_normalized_aux1 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) (i j : fin N.succ) (inej : i ≠ j) : 1 - δ ≤ ‖a.c i - a.c j‖ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ‖a.c i - a.c j‖ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ‖a.c j - a.c i‖ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have D : 0 ≤ 1 - δ / 4, by linarith only [hδ2], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have I : (1 - δ / 4) * τ ≤ 1 := calc (1 - δ / 4) * τ ≤ (1 - δ / 4) * (1 + δ / 4) : mul_le_mul_of_nonneg_left hδ1 D ... = 1 - δ^2 / 16 : by ring ... ≤ 1 : (by linarith only [sq_nonneg δ]), have J : 1 - δ ≤ 1 - δ / 4, by linarith only [δnonneg], have K : 1 - δ / 4 ≤ τ⁻¹, by { rw [inv_eq_one_div, le_div_iff τpos], exact I }, suffices L : τ⁻¹ ≤ ‖a.c i - a.c j‖, by linarith only [J, K, L], have hτ' : ∀ k, τ⁻¹ ≤ a.r k, { assume k, rw [inv_eq_one_div, div_le_iff τpos, ← lastr, mul_comm], exact a.hlast' k hτ }, rcases ah i j inej with H|H, { apply le_trans _ H.1, exact hτ' i }, { rw norm_sub_rev, apply le_trans _ H.1, exact hτ' j } end variable [normed_space ℝ E] lemma exists_normalized_aux2 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) (i j : fin N.succ) (inej : i ≠ j) (hi : ‖a.c i‖ ≤ 2) (hj : 2 < ‖a.c j‖) : 1 - δ ≤ ‖a.c i - (2 / ‖a.c j‖) • a.c j‖ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ‖a.c i - a.c j‖ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ‖a.c j - a.c i‖ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have D : 0 ≤ 1 - δ / 4, by linarith only [hδ2], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have hcrj : ‖a.c j‖ ≤ a.r j + 1, by simpa only [lastc, lastr, dist_zero_right] using a.inter' j, have I : a.r i ≤ 2, { rcases lt_or_le i (last N) with H|H, { apply (a.hlast i H).1.trans, simpa only [dist_eq_norm, lastc, sub_zero] using hi }, { have : i = last N := top_le_iff.1 H, rw [this, lastr], exact one_le_two } }, have J : (1 - δ / 4) * τ ≤ 1 := calc (1 - δ / 4) * τ ≤ (1 - δ / 4) * (1 + δ / 4) : mul_le_mul_of_nonneg_left hδ1 D ... = 1 - δ^2 / 16 : by ring ... ≤ 1 : (by linarith only [sq_nonneg δ]), have A : a.r j - δ ≤ ‖a.c i - a.c j‖, { rcases ah j i inej.symm with H|H, { rw norm_sub_rev, linarith [H.1] }, have C : a.r j ≤ 4 := calc a.r j ≤ τ * a.r i : H.2 ... ≤ τ * 2 : mul_le_mul_of_nonneg_left I τpos.le ... ≤ (5/4) * 2 : mul_le_mul_of_nonneg_right (by linarith only [hδ1, hδ2]) zero_le_two ... ≤ 4 : by norm_num, calc a.r j - δ ≤ a.r j - (a.r j / 4) * δ : begin refine sub_le_sub le_rfl _, refine mul_le_of_le_one_left δnonneg _, linarith only [C], end ... = (1 - δ / 4) * a.r j : by ring ... ≤ (1 - δ / 4) * (τ * a.r i) : mul_le_mul_of_nonneg_left (H.2) D ... ≤ 1 * a.r i : by { rw [← mul_assoc], apply mul_le_mul_of_nonneg_right J (a.rpos _).le } ... ≤ ‖a.c i - a.c j‖ : by { rw [one_mul], exact H.1 } }, set d := (2 / ‖a.c j‖) • a.c j with hd, have : a.r j - δ ≤ ‖a.c i - d‖ + (a.r j - 1) := calc a.r j - δ ≤ ‖a.c i - a.c j‖ : A ... ≤ ‖a.c i - d‖ + ‖d - a.c j‖ : by simp only [← dist_eq_norm, dist_triangle] ... ≤ ‖a.c i - d‖ + (a.r j - 1) : begin apply add_le_add_left, have A : 0 ≤ 1 - 2 / ‖a.c j‖, by simpa [div_le_iff (zero_le_two.trans_lt hj)] using hj.le, rw [← one_smul ℝ (a.c j), hd, ← sub_smul, norm_smul, norm_sub_rev, real.norm_eq_abs, abs_of_nonneg A, sub_mul], field_simp [(zero_le_two.trans_lt hj).ne'], linarith only [hcrj] end, linarith only [this] end lemma exists_normalized_aux3 {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (i j : fin N.succ) (inej : i ≠ j) (hi : 2 < ‖a.c i‖) (hij : ‖a.c i‖ ≤ ‖a.c j‖) : 1 - δ ≤ ‖(2 / ‖a.c i‖) • a.c i - (2 / ‖a.c j‖) • a.c j‖ := begin have ah : ∀ i j, i ≠ j → (a.r i ≤ ‖a.c i - a.c j‖ ∧ a.r j ≤ τ * a.r i) ∨ (a.r j ≤ ‖a.c j - a.c i‖ ∧ a.r i ≤ τ * a.r j), by simpa only [dist_eq_norm] using a.h, have δnonneg : 0 ≤ δ := by linarith only [hτ, hδ1], have τpos : 0 < τ := _root_.zero_lt_one.trans_le hτ, have hcrj : ‖a.c j‖ ≤ a.r j + 1, by simpa only [lastc, lastr, dist_zero_right] using a.inter' j, have A : a.r i ≤ ‖a.c i‖, { have : i < last N, { apply lt_top_iff_ne_top.2, assume iN, change i = last N at iN, rw [iN, lastc, norm_zero] at hi, exact lt_irrefl _ (zero_le_two.trans_lt hi) }, convert (a.hlast i this).1, rw [dist_eq_norm, lastc, sub_zero] }, have hj : 2 < ‖a.c j‖ := hi.trans_le hij, set s := ‖a.c i‖ with hs, have spos : 0 < s := zero_lt_two.trans hi, set d := (s/‖a.c j‖) • a.c j with hd, have I : ‖a.c j - a.c i‖ ≤ ‖a.c j‖ - s + ‖d - a.c i‖ := calc ‖a.c j - a.c i‖ ≤ ‖a.c j - d‖ + ‖d - a.c i‖ : by simp [← dist_eq_norm, dist_triangle] ... = ‖a.c j‖ - ‖a.c i‖ + ‖d - a.c i‖ : begin nth_rewrite 0 ← one_smul ℝ (a.c j), rw [add_left_inj, hd, ← sub_smul, norm_smul, real.norm_eq_abs, abs_of_nonneg, sub_mul, one_mul, div_mul_cancel _ (zero_le_two.trans_lt hj).ne'], rwa [sub_nonneg, div_le_iff (zero_lt_two.trans hj), one_mul], end, have J : a.r j - ‖a.c j - a.c i‖ ≤ s / 2 * δ := calc a.r j - ‖a.c j - a.c i‖ ≤ s * (τ - 1) : begin rcases ah j i inej.symm with H|H, { calc a.r j - ‖a.c j - a.c i‖ ≤ 0 : sub_nonpos.2 H.1 ... ≤ s * (τ - 1) : mul_nonneg spos.le (sub_nonneg.2 hτ) }, { rw norm_sub_rev at H, calc a.r j - ‖a.c j - a.c i‖ ≤ τ * a.r i - a.r i : sub_le_sub H.2 H.1 ... = a.r i * (τ - 1) : by ring ... ≤ s * (τ - 1) : mul_le_mul_of_nonneg_right A (sub_nonneg.2 hτ) } end ... ≤ s * (δ / 2) : mul_le_mul_of_nonneg_left (by linarith only [δnonneg, hδ1]) spos.le ... = s / 2 * δ : by ring, have invs_nonneg : 0 ≤ 2 / s := (div_nonneg zero_le_two (zero_le_two.trans hi.le)), calc 1 - δ = (2 / s) * (s / 2 - (s / 2) * δ) : by { field_simp [spos.ne'], ring } ... ≤ (2 / s) * ‖d - a.c i‖ : mul_le_mul_of_nonneg_left (by linarith only [hcrj, I, J, hi]) invs_nonneg ... = ‖(2 / s) • a.c i - (2 / ‖a.c j‖) • a.c j‖ : begin conv_lhs { rw [norm_sub_rev, ← abs_of_nonneg invs_nonneg] }, rw [← real.norm_eq_abs, ← norm_smul, smul_sub, hd, smul_smul], congr' 3, field_simp [spos.ne'], end end lemma exists_normalized {N : ℕ} {τ : ℝ} (a : satellite_config E N τ) (lastc : a.c (last N) = 0) (lastr : a.r (last N) = 1) (hτ : 1 ≤ τ) (δ : ℝ) (hδ1 : τ ≤ 1 + δ / 4) (hδ2 : δ ≤ 1) : ∃ (c' : fin N.succ → E), (∀ n, ‖c' n‖ ≤ 2) ∧ (∀ i j, i ≠ j → 1 - δ ≤ ‖c' i - c' j‖) := begin let c' : fin N.succ → E := λ i, if ‖a.c i‖ ≤ 2 then a.c i else (2 / ‖a.c i‖) • a.c i, have norm_c'_le : ∀ i, ‖c' i‖ ≤ 2, { assume i, simp only [c'], split_ifs, { exact h }, by_cases hi : ‖a.c i‖ = 0; field_simp [norm_smul, hi] }, refine ⟨c', λ n, norm_c'_le n, λ i j inej, _⟩, -- up to exchanging `i` and `j`, one can assume `‖c i‖ ≤ ‖c j‖`. wlog hij : ‖a.c i‖ ≤ ‖a.c j‖ := le_total (‖a.c i‖) (‖a.c j‖) using [i j, j i] tactic.skip, swap, { assume i_ne_j, rw norm_sub_rev, exact this i_ne_j.symm }, rcases le_or_lt (‖a.c j‖) 2 with Hj|Hj, -- case `‖c j‖ ≤ 2` (and therefore also `‖c i‖ ≤ 2`) { simp_rw [c', Hj, hij.trans Hj, if_true], exact exists_normalized_aux1 a lastr hτ δ hδ1 hδ2 i j inej }, -- case `2 < ‖c j‖` { have H'j : (‖a.c j‖ ≤ 2) ↔ false, by simpa only [not_le, iff_false] using Hj, rcases le_or_lt (‖a.c i‖) 2 with Hi|Hi, { -- case `‖c i‖ ≤ 2` simp_rw [c', Hi, if_true, H'j, if_false], exact exists_normalized_aux2 a lastc lastr hτ δ hδ1 hδ2 i j inej Hi Hj }, { -- case `2 < ‖c i‖` have H'i : (‖a.c i‖ ≤ 2) ↔ false, by simpa only [not_le, iff_false] using Hi, simp_rw [c', H'i, if_false, H'j, if_false], exact exists_normalized_aux3 a lastc lastr hτ δ hδ1 i j inej Hi hij } } end end satellite_config variables (E) [normed_space ℝ E] [finite_dimensional ℝ E] /-- In a normed vector space `E`, there can be no satellite configuration with `multiplicity E + 1` points and the parameter `good_τ E`. This will ensure that in the inductive construction to get the Besicovitch covering families, there will never be more than `multiplicity E` nonempty families. -/ theorem is_empty_satellite_config_multiplicity : is_empty (satellite_config E (multiplicity E) (good_τ E)) := ⟨begin assume a, let b := a.center_and_rescale, rcases b.exists_normalized (a.center_and_rescale_center) (a.center_and_rescale_radius) (one_lt_good_τ E).le (good_δ E) le_rfl (good_δ_lt_one E).le with ⟨c', c'_le_two, hc'⟩, exact lt_irrefl _ ((nat.lt_succ_self _).trans_le (le_multiplicity_of_δ_of_fin c' c'_le_two hc')) end⟩ @[priority 100] instance : has_besicovitch_covering E := ⟨⟨multiplicity E, good_τ E, one_lt_good_τ E, is_empty_satellite_config_multiplicity E⟩⟩ end besicovitch
1efc3ae4009d111500884faddd2d942fb2cab7a3
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/hott/notation_with_nested_tactics.hlean
075b231645fa3b24608d2702da680153a6ffcba0
[ "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
353
hlean
open is_equiv constants (A B : Type) (f : A → B) definition H : is_equiv f := sorry definition loop [instance] [h : is_equiv f] : is_equiv f := h notation `noinstances` t:max := by with_options [elaborator.ignore_instances true] (exact t) example (a : A) : let H' : is_equiv f := H in @(is_equiv.inv f) H' (f a) = a := noinstances (left_inv f a)
8a708222fb60ad24a51de04fd3da63829d8dbb40
8930e38ac0fae2e5e55c28d0577a8e44e2639a6d
/tactic/rcases.lean
d852c880c6a6ae4846773d413f4c3a5e872bc572
[ "Apache-2.0" ]
permissive
SG4316/mathlib
3d64035d02a97f8556ad9ff249a81a0a51a3321a
a7846022507b531a8ab53b8af8a91953fceafd3a
refs/heads/master
1,584,869,960,527
1,530,718,645,000
1,530,724,110,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,645
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.dlist tactic.basic open lean lean.parser namespace tactic /- These synonyms for `list` are used to clarify the meanings of the many usages of lists in this module. - `listΣ` is used where a list represents a disjunction, such as the list of possible constructors of an inductive type. - `listΠ` is used where a list represents a conjunction, such as the list of arguments of an individual constructor. These are merely type synonyms, and so are not checked for consistency by the compiler. The `def`/`local notation` combination makes Lean retain these annotations in reported types. -/ @[reducible] def list_Sigma := list @[reducible] def list_Pi := list local notation `listΣ` := list_Sigma local notation `listΠ` := list_Pi @[reducible] meta def goals := list expr inductive rcases_patt : Type | one : name → rcases_patt | many : listΣ (listΠ rcases_patt) → rcases_patt instance rcases_patt.inhabited : inhabited rcases_patt := ⟨rcases_patt.one `_⟩ def rcases_patt.name : rcases_patt → name | (rcases_patt.one n) := n | _ := `_ meta instance rcases_patt.has_reflect : has_reflect rcases_patt | (rcases_patt.one n) := `(_) | (rcases_patt.many l) := `(λ l, rcases_patt.many l).subst $ begin have := rcases_patt.has_reflect, tactic.reset_instance_cache, -- this combo will be `haveI` later exact list.reflect l end /-- Takes the number of fields of a single constructor and patterns to match its fields against (not necessarily the same number). The returned lists each contain one element per field of the constructor. The `name` is the name which will be used in the top-level `cases` tactic, and the `rcases_patt` is the pattern which the field will be matched against by subsequent `cases` tactics. -/ meta def rcases.process_constructor : nat → listΠ rcases_patt → listΠ name × listΠ rcases_patt | 0 ids := ([], []) | 1 [] := ([`_], [default _]) | 1 [id] := ([id.name], [id]) -- The interesting case: we matched the last field against multiple -- patterns, so split off the remaining patterns into a subsequent -- match. This handles matching `α × β × γ` against `⟨a, b, c⟩`. | 1 ids := ([`_], [rcases_patt.many [ids]]) | (n+1) ids := let (ns, ps) := rcases.process_constructor n ids.tail, p := ids.head in (p.name :: ns, p :: ps) meta def rcases.process_constructors (params : nat) : listΣ name → listΣ (listΠ rcases_patt) → tactic (dlist name × listΣ (name × listΠ rcases_patt)) | [] ids := pure (dlist.empty, []) | (c::cs) ids := do fn ← mk_const c, n ← get_arity fn, let (h, t) := by from match cs, ids.tail with -- We matched the last constructor against multiple patterns, -- so split off the remaining constructors. This handles matching -- `α ⊕ β ⊕ γ` against `a|b|c`. | [], _::_ := ([rcases_patt.many ids], []) | _, _ := (ids.head, ids.tail) end, let (ns, ps) := rcases.process_constructor (n - params) h, (l, r) ← rcases.process_constructors cs t, pure (dlist.of_list ns ++ l, (c, ps) :: r) private def align {α β} (p : α → β → Prop) [∀ a b, decidable (p a b)] : list α → list β → list (α × β) | (a::as) (b::bs) := if p a b then (a, b) :: align as bs else align as (b::bs) | _ _ := [] private meta def get_local_and_type (e : expr) : tactic (expr × expr) := (do t ← infer_type e, pure (t, e)) <|> (do e ← get_local e.local_pp_name, t ← infer_type e, pure (t, e)) meta def rcases.continue (rcases_core : listΣ (listΠ rcases_patt) → expr → tactic goals) : listΠ (rcases_patt × expr) → tactic goals | [] := get_goals | ((rcases_patt.many ids, e) :: l) := do gs ← rcases_core ids e, list.join <$> gs.mmap (λ g, set_goals [g] >> rcases.continue l) | ((rcases_patt.one `rfl, e) :: l) := do (t, e) ← get_local_and_type e, subst e, rcases.continue l -- If the pattern is any other name, we already bound the name in the -- top-level `cases` tactic, so there is no more work to do for it. | (_ :: l) := rcases.continue l meta def rcases_core : listΣ (listΠ rcases_patt) → expr → tactic goals | ids e := do (t, e) ← get_local_and_type e, t ← whnf t, env ← get_env, let I := t.get_app_fn.const_name, when (¬env.is_inductive I) $ fail format!"rcases tactic failed, {e} is not an inductive datatype", let params := env.inductive_num_params I, let c := env.constructors_of I, (ids, r) ← rcases.process_constructors params c ids, l ← cases_core e ids.to_list, gs ← get_goals, -- `cases_core` may not generate a new goal for every constructor, -- as some constructors may be impossible for type reasons. (See its -- documentation.) Match up the new goals with our remaining work -- by constructor name. list.join <$> (align (λ (a : name × _) (b : _ × name × _), a.1 = b.2.1) r (gs.zip l)).mmap (λ⟨⟨_, ps⟩, g, _, hs, _⟩, set_goals [g] >> rcases.continue rcases_core (ps.zip hs)) meta def rcases (p : pexpr) (ids : list (list rcases_patt)) : tactic unit := do e ← i_to_expr p, if e.is_local_constant then focus1 (rcases_core ids e >>= set_goals) else do x ← mk_fresh_name, n ← revert_kdependencies e semireducible, (tactic.generalize e x) <|> (do t ← infer_type e, tactic.assertv x t e, get_local x >>= tactic.revert, return ()), h ← tactic.intro1, focus1 (rcases_core ids h >>= set_goals) end tactic
8205d9ce7861b7ebe9dd17b14db1638ac7a33896
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Data.lean
047f522f7a3129c8678bb29de532666eb32c4044
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
867
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ import Lean.Data.AssocList import Lean.Data.Format import Lean.Data.HashMap import Lean.Data.HashSet import Lean.Data.Json import Lean.Data.JsonRpc import Lean.Data.KVMap import Lean.Data.LBool import Lean.Data.LOption import Lean.Data.Lsp import Lean.Data.Name import Lean.Data.NameMap import Lean.Data.Occurrences import Lean.Data.OpenDecl import Lean.Data.Options import Lean.Data.Parsec import Lean.Data.PersistentArray import Lean.Data.PersistentHashMap import Lean.Data.PersistentHashSet import Lean.Data.Position import Lean.Data.PrefixTree import Lean.Data.SMap import Lean.Data.Trie import Lean.Data.Xml import Lean.Data.NameTrie import Lean.Data.RBTree import Lean.Data.RBMap import Lean.Data.Rat
f351173fed471c2e55e17856b872c7e3af6e1485
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/algebra/algebra.lean
539fcb76299880467c3f93c68cb28d136d73dae9
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
6,451
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.algebra.subalgebra.basic import topology.algebra.module.basic import ring_theory.adjoin.basic /-! # Topological (sub)algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A topological algebra over a topological semiring `R` is a topological semiring with a compatible continuous scalar multiplication by elements of `R`. We reuse typeclass `has_continuous_smul` for topological algebras. ## Results This is just a minimal stub for now! The topological closure of a subalgebra is still a subalgebra, which as an algebra is a topological algebra. -/ open classical set topological_space algebra open_locale classical universes u v w section topological_algebra variables (R : Type*) (A : Type u) variables [comm_semiring R] [semiring A] [algebra R A] variables [topological_space R] [topological_space A] [topological_semiring A] lemma continuous_algebra_map_iff_smul : continuous (algebra_map R A) ↔ continuous (λ p : R × A, p.1 • p.2) := begin refine ⟨λ h, _, λ h, _⟩, { simp only [algebra.smul_def], exact (h.comp continuous_fst).mul continuous_snd }, { rw algebra_map_eq_smul_one', exact h.comp (continuous_id.prod_mk continuous_const) } end @[continuity] lemma continuous_algebra_map [has_continuous_smul R A] : continuous (algebra_map R A) := (continuous_algebra_map_iff_smul R A).2 continuous_smul lemma has_continuous_smul_of_algebra_map (h : continuous (algebra_map R A)) : has_continuous_smul R A := ⟨(continuous_algebra_map_iff_smul R A).1 h⟩ variables [has_continuous_smul R A] /-- The inclusion of the base ring in a topological algebra as a continuous linear map. -/ @[simps] def algebra_map_clm : R →L[R] A := { to_fun := algebra_map R A, cont := continuous_algebra_map R A, .. algebra.linear_map R A } lemma algebra_map_clm_coe : ⇑(algebra_map_clm R A) = algebra_map R A := rfl lemma algebra_map_clm_to_linear_map : (algebra_map_clm R A).to_linear_map = algebra.linear_map R A := rfl end topological_algebra section topological_algebra variables {R : Type*} [comm_semiring R] variables {A : Type u} [topological_space A] variables [semiring A] [algebra R A] instance subalgebra.has_continuous_smul [topological_space R] [has_continuous_smul R A] (s : subalgebra R A) : has_continuous_smul R s := s.to_submodule.has_continuous_smul variables [topological_semiring A] /-- The closure of a subalgebra in a topological algebra as a subalgebra. -/ def subalgebra.topological_closure (s : subalgebra R A) : subalgebra R A := { carrier := closure (s : set A), algebra_map_mem' := λ r, s.to_subsemiring.le_topological_closure (s.algebra_map_mem r), .. s.to_subsemiring.topological_closure } @[simp] lemma subalgebra.topological_closure_coe (s : subalgebra R A) : (s.topological_closure : set A) = closure (s : set A) := rfl instance subalgebra.topological_semiring (s : subalgebra R A) : topological_semiring s := s.to_subsemiring.topological_semiring lemma subalgebra.le_topological_closure (s : subalgebra R A) : s ≤ s.topological_closure := subset_closure lemma subalgebra.is_closed_topological_closure (s : subalgebra R A) : is_closed (s.topological_closure : set A) := by convert is_closed_closure lemma subalgebra.topological_closure_minimal (s : subalgebra R A) {t : subalgebra R A} (h : s ≤ t) (ht : is_closed (t : set A)) : s.topological_closure ≤ t := closure_minimal h ht /-- If a subalgebra of a topological algebra is commutative, then so is its topological closure. -/ def subalgebra.comm_semiring_topological_closure [t2_space A] (s : subalgebra R A) (hs : ∀ (x y : s), x * y = y * x) : comm_semiring s.topological_closure := { ..s.topological_closure.to_semiring, ..s.to_submonoid.comm_monoid_topological_closure hs } /-- This is really a statement about topological algebra isomorphisms, but we don't have those, so we use the clunky approach of talking about an algebra homomorphism, and a separate homeomorphism, along with a witness that as functions they are the same. -/ lemma subalgebra.topological_closure_comap_homeomorph (s : subalgebra R A) {B : Type*} [topological_space B] [ring B] [topological_ring B] [algebra R B] (f : B →ₐ[R] A) (f' : B ≃ₜ A) (w : (f : B → A) = f') : s.topological_closure.comap f = (s.comap f).topological_closure := begin apply set_like.ext', simp only [subalgebra.topological_closure_coe], simp only [subalgebra.coe_comap, subsemiring.coe_comap, alg_hom.coe_to_ring_hom], rw [w], exact f'.preimage_closure _, end end topological_algebra section ring variables {R : Type*} [comm_ring R] variables {A : Type u} [topological_space A] variables [ring A] variables [algebra R A] [topological_ring A] /-- If a subalgebra of a topological algebra is commutative, then so is its topological closure. See note [reducible non-instances]. -/ @[reducible] def subalgebra.comm_ring_topological_closure [t2_space A] (s : subalgebra R A) (hs : ∀ (x y : s), x * y = y * x) : comm_ring s.topological_closure := { ..s.topological_closure.to_ring, ..s.to_submonoid.comm_monoid_topological_closure hs } variables (R) /-- The topological closure of the subalgebra generated by a single element. -/ def algebra.elemental_algebra (x : A) : subalgebra R A := (algebra.adjoin R ({x} : set A)).topological_closure lemma algebra.self_mem_elemental_algebra (x : A) : x ∈ algebra.elemental_algebra R x := set_like.le_def.mp (subalgebra.le_topological_closure (algebra.adjoin R ({x} : set A))) $ algebra.self_mem_adjoin_singleton R x variables {R} instance [t2_space A] {x : A} : comm_ring (algebra.elemental_algebra R x) := subalgebra.comm_ring_topological_closure _ begin letI : comm_ring (algebra.adjoin R ({x} : set A)) := algebra.adjoin_comm_ring_of_comm R (λ y hy z hz, by {rw [mem_singleton_iff] at hy hz, rw [hy, hz]}), exact λ _ _, mul_comm _ _, end end ring section division_ring /-- The action induced by `algebra_rat` is continuous. -/ instance division_ring.has_continuous_const_smul_rat {A} [division_ring A] [topological_space A] [has_continuous_mul A] [char_zero A] : has_continuous_const_smul ℚ A := ⟨λ r, by { simpa only [algebra.smul_def] using continuous_const.mul continuous_id }⟩ end division_ring
5a750157dc6dd8475295386c0253a120f29e206d
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/group_theory/p_group.lean
c2e9f150550375fcaf1fcdd68882363d02d3ec30
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
14,667
lean
/- Copyright (c) 2018 . All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Thomas Browning -/ import data.zmod.basic import group_theory.index import group_theory.group_action.conj_act import group_theory.group_action.quotient import group_theory.perm.cycle.type import group_theory.specific_groups.cyclic import tactic.interval_cases /-! # p-groups This file contains a proof that if `G` is a `p`-group acting on a finite set `α`, then the number of fixed points of the action is congruent mod `p` to the cardinality of `α`. It also contains proofs of some corollaries of this lemma about existence of fixed points. -/ open_locale big_operators open fintype mul_action variables (p : ℕ) (G : Type*) [group G] /-- A p-group is a group in which every element has prime power order -/ def is_p_group : Prop := ∀ g : G, ∃ k : ℕ, g ^ (p ^ k) = 1 variables {p} {G} namespace is_p_group lemma iff_order_of [hp : fact p.prime] : is_p_group p G ↔ ∀ g : G, ∃ k : ℕ, order_of g = p ^ k := forall_congr (λ g, ⟨λ ⟨k, hk⟩, exists_imp_exists (by exact λ j, Exists.snd) ((nat.dvd_prime_pow hp.out).mp (order_of_dvd_of_pow_eq_one hk)), exists_imp_exists (λ k hk, by rw [←hk, pow_order_of_eq_one])⟩) lemma of_card [fintype G] {n : ℕ} (hG : card G = p ^ n) : is_p_group p G := λ g, ⟨n, by rw [←hG, pow_card_eq_one]⟩ lemma of_bot : is_p_group p (⊥ : subgroup G) := of_card (subgroup.card_bot.trans (pow_zero p).symm) lemma iff_card [fact p.prime] [fintype G] : is_p_group p G ↔ ∃ n : ℕ, card G = p ^ n := begin have hG : card G ≠ 0 := card_ne_zero, refine ⟨λ h, _, λ ⟨n, hn⟩, of_card hn⟩, suffices : ∀ q ∈ nat.factors (card G), q = p, { use (card G).factors.length, rw [←list.prod_repeat, ←list.eq_repeat_of_mem this, nat.prod_factors hG] }, intros q hq, obtain ⟨hq1, hq2⟩ := (nat.mem_factors hG).mp hq, haveI : fact q.prime := ⟨hq1⟩, obtain ⟨g, hg⟩ := exists_prime_order_of_dvd_card q hq2, obtain ⟨k, hk⟩ := (iff_order_of.mp h) g, exact (hq1.pow_eq_iff.mp (hg.symm.trans hk).symm).1.symm, end section G_is_p_group variables (hG : is_p_group p G) include hG lemma of_injective {H : Type*} [group H] (ϕ : H →* G) (hϕ : function.injective ϕ) : is_p_group p H := begin simp_rw [is_p_group, ←hϕ.eq_iff, ϕ.map_pow, ϕ.map_one], exact λ h, hG (ϕ h), end lemma to_subgroup (H : subgroup G) : is_p_group p H := hG.of_injective H.subtype subtype.coe_injective lemma of_surjective {H : Type*} [group H] (ϕ : G →* H) (hϕ : function.surjective ϕ) : is_p_group p H := begin refine λ h, exists.elim (hϕ h) (λ g hg, exists_imp_exists (λ k hk, _) (hG g)), rw [←hg, ←ϕ.map_pow, hk, ϕ.map_one], end lemma to_quotient (H : subgroup G) [H.normal] : is_p_group p (G ⧸ H) := hG.of_surjective (quotient_group.mk' H) quotient.surjective_quotient_mk' lemma of_equiv {H : Type*} [group H] (ϕ : G ≃* H) : is_p_group p H := hG.of_surjective ϕ.to_monoid_hom ϕ.surjective variables [hp : fact p.prime] include hp lemma index (H : subgroup G) [finite (G ⧸ H)] : ∃ n : ℕ, H.index = p ^ n := begin casesI nonempty_fintype (G ⧸ H), obtain ⟨n, hn⟩ := iff_card.mp (hG.to_quotient H.normal_core), obtain ⟨k, hk1, hk2⟩ := (nat.dvd_prime_pow hp.out).mp ((congr_arg _ (H.normal_core.index_eq_card.trans hn)).mp (subgroup.index_dvd_of_le H.normal_core_le)), exact ⟨k, hk2⟩, end lemma nontrivial_iff_card [fintype G] : nontrivial G ↔ ∃ n > 0, card G = p ^ n := ⟨λ hGnt, let ⟨k, hk⟩ := iff_card.1 hG in ⟨k, nat.pos_of_ne_zero $ λ hk0, by rw [hk0, pow_zero] at hk; exactI fintype.one_lt_card.ne' hk, hk⟩, λ ⟨k, hk0, hk⟩, one_lt_card_iff_nontrivial.1 $ hk.symm ▸ one_lt_pow (fact.out p.prime).one_lt (ne_of_gt hk0)⟩ variables {α : Type*} [mul_action G α] lemma card_orbit (a : α) [fintype (orbit G a)] : ∃ n : ℕ, card (orbit G a) = p ^ n := begin let ϕ := orbit_equiv_quotient_stabilizer G a, haveI := fintype.of_equiv (orbit G a) ϕ, rw [card_congr ϕ, ←subgroup.index_eq_card], exact hG.index (stabilizer G a), end variables (α) [fintype α] /-- If `G` is a `p`-group acting on a finite set `α`, then the number of fixed points of the action is congruent mod `p` to the cardinality of `α` -/ lemma card_modeq_card_fixed_points [fintype (fixed_points G α)] : card α ≡ card (fixed_points G α) [MOD p] := begin classical, calc card α = card (Σ y : quotient (orbit_rel G α), {x // quotient.mk' x = y}) : card_congr (equiv.sigma_fiber_equiv (@quotient.mk' _ (orbit_rel G α))).symm ... = ∑ a : quotient (orbit_rel G α), card {x // quotient.mk' x = a} : card_sigma _ ... ≡ ∑ a : fixed_points G α, 1 [MOD p] : _ ... = _ : by simp; refl, rw [←zmod.eq_iff_modeq_nat p, nat.cast_sum, nat.cast_sum], have key : ∀ x, card {y // (quotient.mk' y : quotient (orbit_rel G α)) = quotient.mk' x} = card (orbit G x) := λ x, by simp only [quotient.eq']; congr, refine eq.symm (finset.sum_bij_ne_zero (λ a _ _, quotient.mk' a.1) (λ _ _ _, finset.mem_univ _) (λ a₁ a₂ _ _ _ _ h, subtype.eq ((mem_fixed_points' α).mp a₂.2 a₁.1 (quotient.exact' h))) (λ b, quotient.induction_on' b (λ b _ hb, _)) (λ a ha _, by { rw [key, mem_fixed_points_iff_card_orbit_eq_one.mp a.2] })), obtain ⟨k, hk⟩ := hG.card_orbit b, have : k = 0 := nat.le_zero_iff.1 (nat.le_of_lt_succ (lt_of_not_ge (mt (pow_dvd_pow p) (by rwa [pow_one, ←hk, ←nat.modeq_zero_iff_dvd, ←zmod.eq_iff_modeq_nat, ←key, nat.cast_zero])))), exact ⟨⟨b, mem_fixed_points_iff_card_orbit_eq_one.2 $ by rw [hk, this, pow_zero]⟩, finset.mem_univ _, (ne_of_eq_of_ne nat.cast_one one_ne_zero), rfl⟩, end /-- If a p-group acts on `α` and the cardinality of `α` is not a multiple of `p` then the action has a fixed point. -/ lemma nonempty_fixed_point_of_prime_not_dvd_card (hpα : ¬ p ∣ card α) [finite (fixed_points G α)] : (fixed_points G α).nonempty := @set.nonempty_of_nonempty_subtype _ _ begin casesI nonempty_fintype (fixed_points G α), rw [←card_pos_iff, pos_iff_ne_zero], contrapose! hpα, rw [←nat.modeq_zero_iff_dvd, ←hpα], exact hG.card_modeq_card_fixed_points α, end /-- If a p-group acts on `α` and the cardinality of `α` is a multiple of `p`, and the action has one fixed point, then it has another fixed point. -/ lemma exists_fixed_point_of_prime_dvd_card_of_fixed_point (hpα : p ∣ card α) {a : α} (ha : a ∈ fixed_points G α) : ∃ b, b ∈ fixed_points G α ∧ a ≠ b := begin casesI nonempty_fintype (fixed_points G α), have hpf : p ∣ card (fixed_points G α) := nat.modeq_zero_iff_dvd.mp ((hG.card_modeq_card_fixed_points α).symm.trans hpα.modeq_zero_nat), have hα : 1 < card (fixed_points G α) := (fact.out p.prime).one_lt.trans_le (nat.le_of_dvd (card_pos_iff.2 ⟨⟨a, ha⟩⟩) hpf), exact let ⟨⟨b, hb⟩, hba⟩ := exists_ne_of_one_lt_card hα ⟨a, ha⟩ in ⟨b, hb, λ hab, hba (by simp_rw [hab])⟩ end lemma center_nontrivial [nontrivial G] [finite G] : nontrivial (subgroup.center G) := begin classical, casesI nonempty_fintype G, have := (hG.of_equiv conj_act.to_conj_act).exists_fixed_point_of_prime_dvd_card_of_fixed_point G, rw conj_act.fixed_points_eq_center at this, obtain ⟨g, hg⟩ := this _ (subgroup.center G).one_mem, { exact ⟨⟨1, ⟨g, hg.1⟩, mt subtype.ext_iff.mp hg.2⟩⟩ }, { obtain ⟨n, hn0, hn⟩ := hG.nontrivial_iff_card.mp infer_instance, exact hn.symm ▸ dvd_pow_self _ (ne_of_gt hn0) }, end lemma bot_lt_center [nontrivial G] [finite G] : ⊥ < subgroup.center G := begin haveI := center_nontrivial hG, casesI nonempty_fintype G, classical, exact bot_lt_iff_ne_bot.mpr ((subgroup.center G).one_lt_card_iff_ne_bot.mp fintype.one_lt_card), end end G_is_p_group lemma to_le {H K : subgroup G} (hK : is_p_group p K) (hHK : H ≤ K) : is_p_group p H := hK.of_injective (subgroup.inclusion hHK) (λ a b h, subtype.ext (show _, from subtype.ext_iff.mp h)) lemma to_inf_left {H K : subgroup G} (hH : is_p_group p H) : is_p_group p (H ⊓ K : subgroup G) := hH.to_le inf_le_left lemma to_inf_right {H K : subgroup G} (hK : is_p_group p K) : is_p_group p (H ⊓ K : subgroup G) := hK.to_le inf_le_right lemma map {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : G →* K) : is_p_group p (H.map ϕ) := begin rw [←H.subtype_range, monoid_hom.map_range], exact hH.of_surjective (ϕ.restrict H).range_restrict (ϕ.restrict H).range_restrict_surjective, end lemma comap_of_ker_is_p_group {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : K →* G) (hϕ : is_p_group p ϕ.ker) : is_p_group p (H.comap ϕ) := begin intro g, obtain ⟨j, hj⟩ := hH ⟨ϕ g.1, g.2⟩, rw [subtype.ext_iff, H.coe_pow, subtype.coe_mk, ←ϕ.map_pow] at hj, obtain ⟨k, hk⟩ := hϕ ⟨g.1 ^ p ^ j, hj⟩, rwa [subtype.ext_iff, ϕ.ker.coe_pow, subtype.coe_mk, ←pow_mul, ←pow_add] at hk, exact ⟨j + k, by rwa [subtype.ext_iff, (H.comap ϕ).coe_pow]⟩, end lemma ker_is_p_group_of_injective {K : Type*} [group K] {ϕ : K →* G} (hϕ : function.injective ϕ) : is_p_group p ϕ.ker := (congr_arg (λ Q : subgroup K, is_p_group p Q) (ϕ.ker_eq_bot_iff.mpr hϕ)).mpr is_p_group.of_bot lemma comap_of_injective {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : K →* G) (hϕ : function.injective ϕ) : is_p_group p (H.comap ϕ) := hH.comap_of_ker_is_p_group ϕ (ker_is_p_group_of_injective hϕ) lemma comap_subtype {H : subgroup G} (hH : is_p_group p H) {K : subgroup G} : is_p_group p (H.comap K.subtype) := hH.comap_of_injective K.subtype subtype.coe_injective lemma to_sup_of_normal_right {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) [K.normal] : is_p_group p (H ⊔ K : subgroup G) := begin rw [←quotient_group.ker_mk K, ←subgroup.comap_map_eq], apply (hH.map (quotient_group.mk' K)).comap_of_ker_is_p_group, rwa quotient_group.ker_mk, end lemma to_sup_of_normal_left {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) [H.normal] : is_p_group p (H ⊔ K : subgroup G) := (congr_arg (λ H : subgroup G, is_p_group p H) sup_comm).mp (to_sup_of_normal_right hK hH) lemma to_sup_of_normal_right' {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) (hHK : H ≤ K.normalizer) : is_p_group p (H ⊔ K : subgroup G) := let hHK' := to_sup_of_normal_right (hH.of_equiv (subgroup.comap_subtype_equiv_of_le hHK).symm) (hK.of_equiv (subgroup.comap_subtype_equiv_of_le subgroup.le_normalizer).symm) in ((congr_arg (λ H : subgroup K.normalizer, is_p_group p H) (subgroup.sup_subgroup_of_eq hHK subgroup.le_normalizer)).mp hHK').of_equiv (subgroup.comap_subtype_equiv_of_le (sup_le hHK subgroup.le_normalizer)) lemma to_sup_of_normal_left' {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) (hHK : K ≤ H.normalizer) : is_p_group p (H ⊔ K : subgroup G) := (congr_arg (λ H : subgroup G, is_p_group p H) sup_comm).mp (to_sup_of_normal_right' hK hH hHK) /-- finite p-groups with different p have coprime orders -/ lemma coprime_card_of_ne {G₂ : Type*} [group G₂] (p₁ p₂ : ℕ) [hp₁ : fact p₁.prime] [hp₂ : fact p₂.prime] (hne : p₁ ≠ p₂) (H₁ : subgroup G) (H₂ : subgroup G₂) [fintype H₁] [fintype H₂] (hH₁ : is_p_group p₁ H₁) (hH₂ : is_p_group p₂ H₂) : nat.coprime (fintype.card H₁) (fintype.card H₂) := begin obtain ⟨n₁, heq₁⟩ := iff_card.mp hH₁, rw heq₁, clear heq₁, obtain ⟨n₂, heq₂⟩ := iff_card.mp hH₂, rw heq₂, clear heq₂, exact nat.coprime_pow_primes _ _ (hp₁.elim) (hp₂.elim) hne, end /-- p-groups with different p are disjoint -/ lemma disjoint_of_ne (p₁ p₂ : ℕ) [hp₁ : fact p₁.prime] [hp₂ : fact p₂.prime] (hne : p₁ ≠ p₂) (H₁ H₂ : subgroup G) (hH₁ : is_p_group p₁ H₁) (hH₂ : is_p_group p₂ H₂) : disjoint H₁ H₂ := begin rintro x ⟨hx₁, hx₂⟩, rw subgroup.mem_bot, obtain ⟨n₁, hn₁⟩ := iff_order_of.mp hH₁ ⟨x, hx₁⟩, obtain ⟨n₂, hn₂⟩ := iff_order_of.mp hH₂ ⟨x, hx₂⟩, rw [← order_of_subgroup, subgroup.coe_mk] at hn₁ hn₂, have : p₁ ^ n₁ = p₂ ^ n₂, by rw [← hn₁, ← hn₂], have : n₁ = 0, { contrapose! hne with h, rw ← associated_iff_eq at this ⊢, exact associated.of_pow_associated_of_prime (nat.prime_iff.mp hp₁.elim) (nat.prime_iff.mp hp₂.elim) (ne.bot_lt h) this }, simpa [this] using hn₁, end section p2comm variables [fintype G] [fact p.prime] {n : ℕ} (hGpn : card G = p ^ n) include hGpn open subgroup /-- The cardinality of the `center` of a `p`-group is `p ^ k` where `k` is positive. -/ lemma card_center_eq_prime_pow (hn : 0 < n) [fintype (center G)] : ∃ k > 0, card (center G) = p ^ k := begin have hcG := to_subgroup (of_card hGpn) (center G), rcases iff_card.1 hcG with ⟨k, hk⟩, haveI : nontrivial G := (nontrivial_iff_card $ of_card hGpn).2 ⟨n, hn, hGpn⟩, exact (nontrivial_iff_card hcG).mp (center_nontrivial (of_card hGpn)), end omit hGpn /-- The quotient by the center of a group of cardinality `p ^ 2` is cyclic. -/ lemma cyclic_center_quotient_of_card_eq_prime_sq (hG : card G = p ^ 2) : is_cyclic (G ⧸ (center G)) := begin classical, rcases card_center_eq_prime_pow hG zero_lt_two with ⟨k, hk0, hk⟩, rw [card_eq_card_quotient_mul_card_subgroup (center G), mul_comm, hk] at hG, have hk2 := (nat.pow_dvd_pow_iff_le_right (fact.out p.prime).one_lt).1 ⟨_, hG.symm⟩, interval_cases k, { rw [sq, pow_one, nat.mul_right_inj (fact.out p.prime).pos] at hG, exact is_cyclic_of_prime_card hG }, { exact @is_cyclic_of_subsingleton _ _ ⟨fintype.card_le_one_iff.1 ((nat.mul_right_inj (pow_pos (fact.out p.prime).pos 2)).1 (hG.trans (mul_one (p ^ 2)).symm)).le⟩ }, end /-- A group of order `p ^ 2` is commutative. See also `is_p_group.commutative_of_card_eq_prime_sq` for just the proof that `∀ a b, a * b = b * a` -/ def comm_group_of_card_eq_prime_sq (hG : card G = p ^ 2) : comm_group G := @comm_group_of_cycle_center_quotient _ _ _ _ (cyclic_center_quotient_of_card_eq_prime_sq hG) _ (quotient_group.ker_mk (center G)).le /-- A group of order `p ^ 2` is commutative. See also `is_p_group.comm_group_of_card_eq_prime_sq` for the `comm_group` instance. -/ lemma commutative_of_card_eq_prime_sq (hG : card G = p ^ 2) : ∀ a b : G, a * b = b * a := (comm_group_of_card_eq_prime_sq hG).mul_comm end p2comm end is_p_group
2b8881fa15be2d555773b7a7bc26b9a31551b97a
35b83be3126daae10419b573c55e1fed009d3ae8
/_target/deps/mathlib/data/set/basic.lean
6d11f259d914c27dd20e5142851ab7a7dfac775d
[]
no_license
AHassan1024/Lean_Playground
ccb25b72029d199c0d23d002db2d32a9f2689ebc
a00b004c3a2eb9e3e863c361aa2b115260472414
refs/heads/master
1,586,221,905,125
1,544,951,310,000
1,544,951,310,000
157,934,290
0
0
null
null
null
null
UTF-8
Lean
false
false
43,940
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, 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 @[simp] lemma sep_set_of {α} {p q : α → Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl @[simp] lemma set_of_mem {α} {s : set α} : {a | a ∈ s} = s := 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 instance univ_decidable : decidable_pred (@set.univ α) := λ x, is_true trivial /- 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) := ext $ assume a, by simp [or.comm, or.left_comm] @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := 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} := 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] @[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} := set.ext $ by simp /- 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] @[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 := 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] 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 := 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 = ∅ := 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] @[simp] lemma diff_self {s : set α} : s \ s = ∅ := ext $ by simp /- 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, 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} := 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} := 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 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₁⟩) 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 exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ (∃ i, p (f i)) := ⟨assume ⟨a, ⟨i, eq⟩, h⟩, ⟨i, eq.symm ▸ h⟩, assume ⟨i, h⟩, ⟨f i, mem_range_self _, h⟩⟩ 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 := ext $ by 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 : α → β} : 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 '' (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]⟩ theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := set.ext $ λ x, and_iff_left ⟨x, rfl⟩ 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 subtype_val_range {p : α → Prop} : range (@subtype.val _ p) = {x | p x} := by rw ← image_univ; simp [-image_univ, subtype_val_image] 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 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 (α × β)) := ext $ by simp [set.prod] @[simp] theorem empty_prod {t : set β} : set.prod ∅ t = (∅ : 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 := 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 := 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 := 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) := 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)) := ext $ by simp [range] @[simp] theorem prod_singleton_singleton {a : α} {b : β} : set.prod {a} {b} = ({(a, b)} : 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 := 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 section pi variables {α : Type*} {π : α → Type*} def pi (i : set α) (s : Πa, set (π a)) : set (Πa, π a) := { f | ∀a∈i, f a ∈ s a } @[simp] lemma pi_empty_index (s : Πa, set (π a)) : pi ∅ s = univ := by ext; simp [pi] @[simp] lemma pi_insert_index (a : α) (i : set α) (s : Πa, set (π a)) : pi (insert a i) s = ((λf, f a) ⁻¹' s a) ∩ pi i s := by ext; simp [pi, or_imp_distrib, forall_and_distrib] @[simp] lemma pi_singleton_index (a : α) (s : Πa, set (π a)) : pi {a} s = ((λf:(Πa, π a), f a) ⁻¹' s a) := by ext; simp [pi] lemma pi_if {p : α → Prop} [h : decidable_pred p] (i : set α) (s t : Πa, set (π a)) : pi i (λa, if p a then s a else t a) = pi {a ∈ i | p a} s ∩ pi {a ∈ i | ¬ p a} t := begin ext f, split, { assume h, split; { rintros a ⟨hai, hpa⟩, simpa [*] using h a } }, { rintros ⟨hs, ht⟩ a hai, by_cases p a; simp [*, pi] at * } end end pi end set
8f92ce8dc00e748f51e95a49ec505b4d5a3ead15
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/Elab/Command.lean
b55f3c48cddc6b09c1003a990d2457841ac18eda
[ "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
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
20,675
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.Elab.Binders import Lean.Elab.SyntheticMVars namespace Lean.Elab.Command structure Scope where header : String opts : Options := {} currNamespace : Name := Name.anonymous openDecls : List OpenDecl := [] levelNames : List Name := [] /-- section variables -/ varDecls : Array (TSyntax ``Parser.Term.bracketedBinder) := #[] /-- Globally unique internal identifiers for the `varDecls` -/ varUIds : Array Name := #[] /-- noncomputable sections automatically add the `noncomputable` modifier to any declaration we cannot generate code for. -/ isNoncomputable : Bool := false deriving Inhabited structure State where env : Environment messages : MessageLog := {} scopes : List Scope := [{ header := "" }] nextMacroScope : Nat := firstFrontendMacroScope + 1 maxRecDepth : Nat nextInstIdx : Nat := 1 -- for generating anonymous instance names ngen : NameGenerator := {} infoState : InfoState := {} traceState : TraceState := {} deriving Inhabited structure Context where fileName : String fileMap : FileMap currRecDepth : Nat := 0 cmdPos : String.Pos := 0 macroStack : MacroStack := [] currMacroScope : MacroScope := firstFrontendMacroScope ref : Syntax := Syntax.missing tacticCache? : Option (IO.Ref Tactic.Cache) abbrev CommandElabCoreM (ε) := ReaderT Context $ StateRefT State $ EIO ε abbrev CommandElabM := CommandElabCoreM Exception abbrev CommandElab := Syntax → CommandElabM Unit abbrev Linter := Syntax → CommandElabM Unit -- Make the compiler generate specialized `pure`/`bind` so we do not have to optimize through the -- whole monad stack at every use site. May eventually be covered by `deriving`. instance : Monad CommandElabM := let i := inferInstanceAs (Monad CommandElabM); { pure := i.pure, bind := i.bind } def mkState (env : Environment) (messages : MessageLog := {}) (opts : Options := {}) : State := { env := env messages := messages scopes := [{ header := "", opts := opts }] maxRecDepth := maxRecDepth.get opts } /- Linters should be loadable as plugins, so store in a global IO ref instead of an attribute managed by the environment (which only contains `import`ed objects). -/ builtin_initialize lintersRef : IO.Ref (Array Linter) ← IO.mkRef #[] def addLinter (l : Linter) : IO Unit := do let ls ← lintersRef.get lintersRef.set (ls.push l) instance : MonadInfoTree CommandElabM where getInfoState := return (← get).infoState modifyInfoState f := modify fun s => { s with infoState := f s.infoState } instance : MonadEnv CommandElabM where getEnv := do pure (← get).env modifyEnv f := modify fun s => { s with env := f s.env } instance : MonadOptions CommandElabM where getOptions := do pure (← get).scopes.head!.opts protected def getRef : CommandElabM Syntax := return (← read).ref instance : AddMessageContext CommandElabM where addMessageContext := addMessageContextPartial instance : MonadRef CommandElabM where getRef := Command.getRef withRef ref x := withReader (fun ctx => { ctx with ref := ref }) x instance : MonadTrace CommandElabM where getTraceState := return (← get).traceState modifyTraceState f := modify fun s => { s with traceState := f s.traceState } instance : AddErrorMessageContext CommandElabM where add ref msg := do let ctx ← read let ref := getBetterRef ref ctx.macroStack let msg ← addMessageContext msg let msg ← addMacroStack msg ctx.macroStack return (ref, msg) def mkMessageAux (ctx : Context) (ref : Syntax) (msgData : MessageData) (severity : MessageSeverity) : Message := let pos := ref.getPos?.getD ctx.cmdPos let endPos := ref.getTailPos?.getD pos mkMessageCore ctx.fileName ctx.fileMap msgData severity pos endPos private def mkCoreContext (ctx : Context) (s : State) (heartbeats : Nat) : Core.Context := let scope := s.scopes.head! { fileName := ctx.fileName fileMap := ctx.fileMap options := scope.opts currRecDepth := ctx.currRecDepth maxRecDepth := s.maxRecDepth ref := ctx.ref currNamespace := scope.currNamespace openDecls := scope.openDecls initHeartbeats := heartbeats currMacroScope := ctx.currMacroScope } private def addTraceAsMessagesCore (ctx : Context) (log : MessageLog) (traceState : TraceState) : MessageLog := Id.run do if traceState.traces.isEmpty then return log let mut traces : HashMap (String.Pos × String.Pos) (Array MessageData) := ∅ for traceElem in traceState.traces do let ref := replaceRef traceElem.ref ctx.ref let pos := ref.getPos?.getD 0 let endPos := ref.getTailPos?.getD pos traces := traces.insert (pos, endPos) <| traces.findD (pos, endPos) #[] |>.push traceElem.msg let mut log := log let traces' := traces.toArray.qsort fun ((a, _), _) ((b, _), _) => a < b for ((pos, endPos), traceMsg) in traces' do log := log.add <| mkMessageCore ctx.fileName ctx.fileMap (.joinSep traceMsg.toList "\n") .information pos endPos return log private def addTraceAsMessages : CommandElabM Unit := do let ctx ← read modify fun s => { s with messages := addTraceAsMessagesCore ctx s.messages s.traceState traceState.traces := {} } def liftCoreM (x : CoreM α) : CommandElabM α := do let s ← get let ctx ← read let heartbeats ← IO.getNumHeartbeats let Eα := Except Exception α let x : CoreM Eα := try let a ← x; pure <| Except.ok a catch ex => pure <| Except.error ex let x : EIO Exception (Eα × Core.State) := (ReaderT.run x (mkCoreContext ctx s heartbeats)).run { env := s.env, ngen := s.ngen, traceState := s.traceState, messages := {}, infoState.enabled := s.infoState.enabled } let (ea, coreS) ← liftM x modify fun s => { s with env := coreS.env ngen := coreS.ngen messages := addTraceAsMessagesCore ctx (s.messages ++ coreS.messages) coreS.traceState traceState := coreS.traceState infoState.trees := s.infoState.trees.append coreS.infoState.trees } match ea with | Except.ok a => pure a | Except.error e => throw e private def ioErrorToMessage (ctx : Context) (ref : Syntax) (err : IO.Error) : Message := let ref := getBetterRef ref ctx.macroStack mkMessageAux ctx ref (toString err) MessageSeverity.error @[inline] def liftEIO {α} (x : EIO Exception α) : CommandElabM α := liftM x @[inline] def liftIO {α} (x : IO α) : CommandElabM α := do let ctx ← read IO.toEIO (fun (ex : IO.Error) => Exception.error ctx.ref ex.toString) x instance : MonadLiftT IO CommandElabM where monadLift := liftIO def getScope : CommandElabM Scope := do pure (← get).scopes.head! instance : MonadResolveName CommandElabM where getCurrNamespace := return (← getScope).currNamespace getOpenDecls := return (← getScope).openDecls instance : MonadLog CommandElabM where getRef := getRef getFileMap := return (← read).fileMap getFileName := return (← read).fileName hasErrors := return (← get).messages.hasErrors logMessage msg := do let currNamespace ← getCurrNamespace let openDecls ← getOpenDecls let msg := { msg with data := MessageData.withNamingContext { currNamespace := currNamespace, openDecls := openDecls } msg.data } modify fun s => { s with messages := s.messages.add msg } def runLinters (stx : Syntax) : CommandElabM Unit := do profileitM Exception "linting" (← getOptions) do let linters ← lintersRef.get unless linters.isEmpty do for linter in linters do let savedState ← get try linter stx catch ex => logException ex finally modify fun s => { savedState with messages := s.messages } protected def getCurrMacroScope : CommandElabM Nat := do pure (← read).currMacroScope protected def getMainModule : CommandElabM Name := do pure (← getEnv).mainModule protected def withFreshMacroScope {α} (x : CommandElabM α) : CommandElabM α := do let fresh ← modifyGet (fun st => (st.nextMacroScope, { st with nextMacroScope := st.nextMacroScope + 1 })) withReader (fun ctx => { ctx with currMacroScope := fresh }) x instance : MonadQuotation CommandElabM where getCurrMacroScope := Command.getCurrMacroScope getMainModule := Command.getMainModule withFreshMacroScope := Command.withFreshMacroScope unsafe def mkCommandElabAttributeUnsafe : IO (KeyedDeclsAttribute CommandElab) := mkElabAttribute CommandElab `Lean.Elab.Command.commandElabAttribute `builtinCommandElab `commandElab `Lean.Parser.Command `Lean.Elab.Command.CommandElab "command" @[implementedBy mkCommandElabAttributeUnsafe] opaque mkCommandElabAttribute : IO (KeyedDeclsAttribute CommandElab) builtin_initialize commandElabAttribute : KeyedDeclsAttribute CommandElab ← mkCommandElabAttribute private def mkInfoTree (elaborator : Name) (stx : Syntax) (trees : PersistentArray InfoTree) : CommandElabM InfoTree := do let ctx ← read let s ← get let scope := s.scopes.head! let tree := InfoTree.node (Info.ofCommandInfo { elaborator, stx }) trees return InfoTree.context { env := s.env, fileMap := ctx.fileMap, mctx := {}, currNamespace := scope.currNamespace, openDecls := scope.openDecls, options := scope.opts, ngen := s.ngen } tree private def elabCommandUsing (s : State) (stx : Syntax) : List (KeyedDeclsAttribute.AttributeEntry CommandElab) → CommandElabM Unit | [] => withInfoTreeContext (mkInfoTree := mkInfoTree `no_elab stx) <| throwError "unexpected syntax{indentD stx}" | (elabFn::elabFns) => catchInternalId unsupportedSyntaxExceptionId (withInfoTreeContext (mkInfoTree := mkInfoTree elabFn.declName stx) <| elabFn.value stx) (fun _ => do set s; elabCommandUsing s stx elabFns) /-- Elaborate `x` with `stx` on the macro stack -/ def withMacroExpansion {α} (beforeStx afterStx : Syntax) (x : CommandElabM α) : CommandElabM α := withInfoContext (mkInfo := pure <| .ofMacroExpansionInfo { stx := beforeStx, output := afterStx, lctx := .empty }) do withReader (fun ctx => { ctx with macroStack := { before := beforeStx, after := afterStx } :: ctx.macroStack }) x instance : MonadMacroAdapter CommandElabM where getCurrMacroScope := getCurrMacroScope getNextMacroScope := return (← get).nextMacroScope setNextMacroScope next := modify fun s => { s with nextMacroScope := next } instance : MonadRecDepth CommandElabM where withRecDepth d x := withReader (fun ctx => { ctx with currRecDepth := d }) x getRecDepth := return (← read).currRecDepth getMaxRecDepth := return (← get).maxRecDepth register_builtin_option showPartialSyntaxErrors : Bool := { defValue := false descr := "show elaboration errors from partial syntax trees (i.e. after parser recovery)" } builtin_initialize registerTraceClass `Elab.command partial def elabCommand (stx : Syntax) : CommandElabM Unit := do withLogging <| withRef stx <| withIncRecDepth <| withFreshMacroScope do match stx with | Syntax.node _ k args => if k == nullKind then -- list of commands => elaborate in order -- The parser will only ever return a single command at a time, but syntax quotations can return multiple ones args.forM elabCommand else do trace `Elab.command fun _ => stx; let s ← get match (← liftMacroM <| expandMacroImpl? s.env stx) with | some (decl, stxNew?) => withInfoTreeContext (mkInfoTree := mkInfoTree decl stx) do let stxNew ← liftMacroM <| liftExcept stxNew? withMacroExpansion stx stxNew do elabCommand stxNew | _ => match commandElabAttribute.getEntries s.env k with | [] => withInfoTreeContext (mkInfoTree := mkInfoTree `no_elab stx) <| throwError "elaboration function for '{k}' has not been implemented" | elabFns => elabCommandUsing s stx elabFns | _ => throwError "unexpected command" builtin_initialize registerTraceClass `Elab.input /-- `elabCommand` wrapper that should be used for the initial invocation, not for recursive calls after macro expansion etc. -/ def elabCommandTopLevel (stx : Syntax) : CommandElabM Unit := withRef stx do trace[Elab.input] stx let initMsgs ← modifyGet fun st => (st.messages, { st with messages := {} }) let initInfoTrees ← getResetInfoTrees -- We should *not* factor out `elabCommand`'s `withLogging` to here since it would make its error -- recovery more coarse. In particular, If `c` in `set_option ... in $c` fails, the remaining -- `end` command of the `in` macro would be skipped and the option would be leaked to the outside! elabCommand stx withLogging do runLinters stx -- note the order: first process current messages & info trees, then add back old messages & trees, -- then convert new traces to messages let mut msgs := (← get).messages -- `stx.hasMissing` should imply `initMsgs.hasErrors`, but the latter should be cheaper to check in general if !showPartialSyntaxErrors.get (← getOptions) && initMsgs.hasErrors && stx.hasMissing then -- discard elaboration errors, except for a few important and unlikely misleading ones, on parse error msgs := ⟨msgs.msgs.filter fun msg => msg.data.hasTag (fun tag => tag == `Elab.synthPlaceholder || tag == `Tactic.unsolvedGoals || (`_traceMsg).isSuffixOf tag)⟩ for tree in (← getInfoTrees) do trace[Elab.info] (← tree.format) modify fun st => { st with messages := initMsgs ++ msgs infoState := { st.infoState with trees := initInfoTrees ++ st.infoState.trees } } addTraceAsMessages /-- Adapt a syntax transformation to a regular, command-producing elaborator. -/ def adaptExpander (exp : Syntax → CommandElabM Syntax) : CommandElab := fun stx => do let stx' ← exp stx withMacroExpansion stx stx' <| elabCommand stx' private def getVarDecls (s : State) : Array Syntax := s.scopes.head!.varDecls instance {α} : Inhabited (CommandElabM α) where default := throw default private def mkMetaContext : Meta.Context := { config := { foApprox := true, ctxApprox := true, quasiPatternApprox := true } } /-- Return identifier names in the given bracketed binder. -/ def getBracketedBinderIds : Syntax → Array Name | `(bracketedBinder|($ids* $[: $ty?]? $(_annot?)?)) => ids.map Syntax.getId | `(bracketedBinder|{$ids* $[: $ty?]?}) => ids.map Syntax.getId | `(bracketedBinder|[$id : $_]) => #[id.getId] | `(bracketedBinder|[$_]) => #[Name.anonymous] | _ => #[] private def mkTermContext (ctx : Context) (s : State) : Term.Context := Id.run do let scope := s.scopes.head! let mut sectionVars := {} for id in scope.varDecls.concatMap getBracketedBinderIds, uid in scope.varUIds do sectionVars := sectionVars.insert id uid { macroStack := ctx.macroStack sectionVars := sectionVars isNoncomputableSection := scope.isNoncomputable tacticCache? := ctx.tacticCache? } /-- Lift the `TermElabM` monadic action `x` into a `CommandElabM` monadic action. Note that `x` is executed with an empty message log. Thus, `x` cannot modify/view messages produced by previous commands. If you need to access the free variables corresponding to the ones declared using the `variable` command, consider using `runTermElabM`. Recall that `TermElabM` actions can automatically lift `MetaM` and `CoreM` actions. Example: ``` import Lean open Lean Elab Command Meta def printExpr (e : Expr) : MetaM Unit := do IO.println s!"{← ppExpr e} : {← ppExpr (← inferType e)}" #eval liftTermElabM do printExpr (mkConst ``Nat) ``` -/ def liftTermElabM (x : TermElabM α) : CommandElabM α := do let ctx ← read let s ← get let heartbeats ← IO.getNumHeartbeats -- dbg_trace "heartbeats: {heartbeats}" let scope := s.scopes.head! -- We execute `x` with an empty message log. Thus, `x` cannot modify/view messages produced by previous commands. -- This is useful for implementing `runTermElabM` where we use `Term.resetMessageLog` let x : TermElabM _ := withSaveInfoContext x let x : MetaM _ := (observing x).run (mkTermContext ctx s) { levelNames := scope.levelNames } let x : CoreM _ := x.run mkMetaContext {} let x : EIO _ _ := x.run (mkCoreContext ctx s heartbeats) { env := s.env, ngen := s.ngen, nextMacroScope := s.nextMacroScope, infoState.enabled := s.infoState.enabled } let (((ea, _), _), coreS) ← liftEIO x modify fun s => { s with env := coreS.env nextMacroScope := coreS.nextMacroScope ngen := coreS.ngen infoState.trees := s.infoState.trees.append coreS.infoState.trees messages := addTraceAsMessagesCore ctx (s.messages ++ coreS.messages) coreS.traceState } match ea with | Except.ok a => pure a | Except.error ex => throw ex /-- Execute the monadic action `elabFn xs` as a `CommandElabM` monadic action, where `xs` are free variables corresponding to all active scoped variables declared using the `variable` command. This method is similar to `liftTermElabM`, but it elaborates all scoped variables declared using the `variable` command. Example: ``` import Lean open Lean Elab Command Meta variable {α : Type u} {f : α → α} variable (n : Nat) #eval runTermElabM fun xs => do for x in xs do IO.println s!"{← ppExpr x} : {← ppExpr (← inferType x)}" ``` -/ def runTermElabM (elabFn : Array Expr → TermElabM α) : CommandElabM α := do let scope ← getScope liftTermElabM <| Term.withAutoBoundImplicit <| Term.elabBinders scope.varDecls fun xs => do -- We need to synthesize postponed terms because this is a checkpoint for the auto-bound implicit feature -- If we don't use this checkpoint here, then auto-bound implicits in the postponed terms will not be handled correctly. Term.synthesizeSyntheticMVarsNoPostponing let mut sectionFVars := {} for uid in scope.varUIds, x in xs do sectionFVars := sectionFVars.insert uid x withReader ({ · with sectionFVars := sectionFVars }) do -- We don't want to store messages produced when elaborating `(getVarDecls s)` because they have already been saved when we elaborated the `variable`(s) command. -- So, we use `Core.resetMessageLog`. Core.resetMessageLog let someType := mkSort levelZero Term.addAutoBoundImplicits' xs someType fun xs _ => Term.withoutAutoBoundImplicit <| elabFn xs @[inline] def catchExceptions (x : CommandElabM Unit) : CommandElabCoreM Empty Unit := fun ctx ref => EIO.catchExceptions (withLogging x ctx ref) (fun _ => pure ()) private def liftAttrM {α} (x : AttrM α) : CommandElabM α := do liftCoreM x def getScopes : CommandElabM (List Scope) := do pure (← get).scopes def modifyScope (f : Scope → Scope) : CommandElabM Unit := modify fun s => { s with scopes := match s.scopes with | h::t => f h :: t | [] => unreachable! } def withScope (f : Scope → Scope) (x : CommandElabM α) : CommandElabM α := do match (← get).scopes with | [] => x | h :: t => try modify fun s => { s with scopes := f h :: t } x finally modify fun s => { s with scopes := h :: t } def getLevelNames : CommandElabM (List Name) := return (← getScope).levelNames def addUnivLevel (idStx : Syntax) : CommandElabM Unit := withRef idStx do let id := idStx.getId let levelNames ← getLevelNames if levelNames.elem id then throwAlreadyDeclaredUniverseLevel id else modifyScope fun scope => { scope with levelNames := id :: scope.levelNames } def expandDeclId (declId : Syntax) (modifiers : Modifiers) : CommandElabM ExpandDeclIdResult := do let currNamespace ← getCurrNamespace let currLevelNames ← getLevelNames let r ← Elab.expandDeclId currNamespace currLevelNames declId modifiers for id in (← getScope).varDecls.concatMap getBracketedBinderIds do if id == r.shortName then throwError "invalid declaration name '{r.shortName}', there is a section variable with the same name" return r end Elab.Command export Elab.Command (Linter addLinter) end Lean
ca83a2ada4feb16f9e406eb82df29428291b3434
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/logic/relator.lean
6f8dd4f5d676306c0feb6128c9af20f3c34b3618
[ "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
3,509
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Relator for functions, pairs, sums, and lists. -/ import logic.basic namespace relator universe variables u₁ u₂ v₁ v₂ /- TODO(johoelzl): * should we introduce relators of datatypes as recursive function or as inductive predicate? For now we stick to the recursor approach. * relation lift for datatypes, Π, Σ, set, and subtype types * proof composition and identity laws * implement method to derive relators from datatype -/ section variables {α : Sort u₁} {β : Sort u₂} {γ : Sort v₁} {δ : Sort v₂} variables (R : α → β → Prop) (S : γ → δ → Prop) def lift_fun (f : α → γ) (g : β → δ) : Prop := ∀⦃a b⦄, R a b → S (f a) (g b) infixr ⇒ := lift_fun end section variables {α : Type u₁} {β : Type u₂} (R : α → β → Prop) def right_total : Prop := ∀ b, ∃ a, R a b def left_total : Prop := ∀ a, ∃ b, R a b def bi_total : Prop := left_total R ∧ right_total R def left_unique : Prop := ∀ ⦃a b c⦄, R a c → R b c → a = b def right_unique : Prop := ∀ ⦃a b c⦄, R a b → R a c → b = c def bi_unique : Prop := left_unique R ∧ right_unique R variable {R} lemma right_total.rel_forall (h : right_total R) : ((R ⇒ implies) ⇒ implies) (λp, ∀i, p i) (λq, ∀i, q i) := assume p q Hrel H b, exists.elim (h b) (assume a Rab, Hrel Rab (H _)) lemma left_total.rel_exists (h : left_total R) : ((R ⇒ implies) ⇒ implies) (λp, ∃i, p i) (λq, ∃i, q i) := assume p q Hrel ⟨a, pa⟩, (h a).imp $ λ b Rab, Hrel Rab pa lemma bi_total.rel_forall (h : bi_total R) : ((R ⇒ iff) ⇒ iff) (λp, ∀i, p i) (λq, ∀i, q i) := assume p q Hrel, ⟨assume H b, exists.elim (h.right b) (assume a Rab, (Hrel Rab).mp (H _)), assume H a, exists.elim (h.left a) (assume b Rab, (Hrel Rab).mpr (H _))⟩ lemma bi_total.rel_exists (h : bi_total R) : ((R ⇒ iff) ⇒ iff) (λp, ∃i, p i) (λq, ∃i, q i) := assume p q Hrel, ⟨assume ⟨a, pa⟩, (h.left a).imp $ λ b Rab, (Hrel Rab).1 pa, assume ⟨b, qb⟩, (h.right b).imp $ λ a Rab, (Hrel Rab).2 qb⟩ lemma left_unique_of_rel_eq {eq' : β → β → Prop} (he : (R ⇒ (R ⇒ iff)) eq eq') : left_unique R := λ a b c (ac : R a c) (bc : R b c), (he ac bc).mpr ((he bc bc).mp rfl) end lemma rel_imp : (iff ⇒ (iff ⇒ iff)) implies implies := assume p q h r s l, imp_congr h l lemma rel_not : (iff ⇒ iff) not not := assume p q h, not_congr h lemma bi_total_eq {α : Type u₁} : relator.bi_total (@eq α) := { left := λ a, ⟨a, rfl⟩, right := λ a, ⟨a, rfl⟩ } variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop} lemma left_unique.flip (h : left_unique r) : right_unique (flip r) := λ a b c h₁ h₂, h h₁ h₂ lemma rel_and : ((↔) ⇒ (↔) ⇒ (↔)) (∧) (∧) := assume a b h₁ c d h₂, and_congr h₁ h₂ lemma rel_or : ((↔) ⇒ (↔) ⇒ (↔)) (∨) (∨) := assume a b h₁ c d h₂, or_congr h₁ h₂ lemma rel_iff : ((↔) ⇒ (↔) ⇒ (↔)) (↔) (↔) := assume a b h₁ c d h₂, iff_congr h₁ h₂ lemma rel_eq {r : α → β → Prop} (hr : bi_unique r) : (r ⇒ r ⇒ (↔)) (=) (=) := assume a b h₁ c d h₂, iff.intro begin intro h, subst h, exact hr.right h₁ h₂ end begin intro h, subst h, exact hr.left h₁ h₂ end end relator
f7eb9c99c069954398d6bba00760a4a28419060f
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Meta/Match/MatcherInfo.lean
0e5ef2f1f90026e2fb3b141656cd70710fc19dd9
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
4,978
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Basic namespace Lean.Meta namespace Match /-- A "matcher" auxiliary declaration has the following structure: - `numParams` parameters - motive - `numDiscrs` discriminators (aka major premises) - `altNumParams.size` alternatives (aka minor premises) where alternative `i` has `altNumParams[i]` parameters - `uElimPos?` is `some pos` when the matcher can eliminate in different universe levels, and `pos` is the position of the universe level parameter that specifies the elimination universe. It is `none` if the matcher only eliminates into `Prop`. -/ structure MatcherInfo where numParams : Nat numDiscrs : Nat altNumParams : Array Nat uElimPos? : Option Nat def MatcherInfo.numAlts (info : MatcherInfo) : Nat := info.altNumParams.size def MatcherInfo.arity (info : MatcherInfo) : Nat := info.numParams + 1 + info.numDiscrs + info.numAlts def MatcherInfo.getFirstDiscrPos (info : MatcherInfo) : Nat := info.numParams + 1 def MatcherInfo.getMotivePos (info : MatcherInfo) : Nat := info.numParams namespace Extension structure Entry where name : Name info : MatcherInfo structure State where map : SMap Name MatcherInfo := {} instance : Inhabited State := ⟨{}⟩ def State.addEntry (s : State) (e : Entry) : State := { s with map := s.map.insert e.name e.info } def State.switch (s : State) : State := { s with map := s.map.switch } builtin_initialize extension : SimplePersistentEnvExtension Entry State ← registerSimplePersistentEnvExtension { name := `matcher addEntryFn := State.addEntry addImportedFn := fun es => (mkStateFromImportedEntries State.addEntry {} es).switch } def addMatcherInfo (env : Environment) (matcherName : Name) (info : MatcherInfo) : Environment := extension.addEntry env { name := matcherName, info := info } def getMatcherInfo? (env : Environment) (declName : Name) : Option MatcherInfo := (extension.getState env).map.find? declName end Extension def addMatcherInfo (matcherName : Name) (info : MatcherInfo) : MetaM Unit := modifyEnv fun env => Extension.addMatcherInfo env matcherName info end Match export Match (MatcherInfo) def getMatcherInfoCore? (env : Environment) (declName : Name) : Option MatcherInfo := Match.Extension.getMatcherInfo? env declName def getMatcherInfo? [Monad m] [MonadEnv m] (declName : Name) : m (Option MatcherInfo) := return getMatcherInfoCore? (← getEnv) declName @[export lean_is_matcher] def isMatcherCore (env : Environment) (declName : Name) : Bool := getMatcherInfoCore? env declName |>.isSome def isMatcher [Monad m] [MonadEnv m] (declName : Name) : m Bool := return isMatcherCore (← getEnv) declName def isMatcherAppCore? (env : Environment) (e : Expr) : Option MatcherInfo := let fn := e.getAppFn if fn.isConst then if let some matcherInfo := getMatcherInfoCore? env fn.constName! then if e.getAppNumArgs ≥ matcherInfo.arity then some matcherInfo else none else none else none def isMatcherAppCore (env : Environment) (e : Expr) : Bool := isMatcherAppCore? env e |>.isSome def isMatcherApp [Monad m] [MonadEnv m] (e : Expr) : m Bool := return isMatcherAppCore (← getEnv) e structure MatcherApp where matcherName : Name matcherLevels : Array Level uElimPos? : Option Nat params : Array Expr motive : Expr discrs : Array Expr altNumParams : Array Nat alts : Array Expr remaining : Array Expr def matchMatcherApp? [Monad m] [MonadEnv m] (e : Expr) : m (Option MatcherApp) := do match e.getAppFn with | Expr.const declName declLevels _ => match (← getMatcherInfo? declName) with | none => return none | some info => let args := e.getAppArgs if args.size < info.arity then return none else return some { matcherName := declName matcherLevels := declLevels.toArray uElimPos? := info.uElimPos? params := args.extract 0 info.numParams motive := args[info.getMotivePos] discrs := args[info.numParams + 1 : info.numParams + 1 + info.numDiscrs] altNumParams := info.altNumParams alts := args[info.numParams + 1 + info.numDiscrs : info.numParams + 1 + info.numDiscrs + info.numAlts] remaining := args[info.numParams + 1 + info.numDiscrs + info.numAlts : args.size] } | _ => return none def MatcherApp.toExpr (matcherApp : MatcherApp) : Expr := let result := mkAppN (mkConst matcherApp.matcherName matcherApp.matcherLevels.toList) matcherApp.params let result := mkApp result matcherApp.motive let result := mkAppN result matcherApp.discrs let result := mkAppN result matcherApp.alts mkAppN result matcherApp.remaining end Lean.Meta
4c23164ca29d00f153beaff30e57344f600bab04
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/bounded_lattice_auto.lean
5d7440f32066d487513fd85f959ebb6f9982b457
[]
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
44,591
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Defines bounded lattice type class hierarchy. Includes the Prop and fun instances. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.lattice import Mathlib.data.option.basic import Mathlib.tactic.pi_instances import Mathlib.logic.nontrivial import Mathlib.PostPort universes u l v u_1 u_2 namespace Mathlib /-- Typeclass for the `⊤` (`\top`) notation -/ /-- Typeclass for the `⊥` (`\bot`) notation -/ class has_top (α : Type u) where top : α class has_bot (α : Type u) where bot : α notation:1024 "⊤" => Mathlib.has_top.top notation:1024 "⊥" => Mathlib.has_bot.bot /-- An `order_top` is a partial order with a maximal element. (We could state this on preorders, but then it wouldn't be unique so distinguishing one would seem odd.) -/ class order_top (α : Type u) extends has_top α, partial_order α where le_top : ∀ (a : α), a ≤ ⊤ @[simp] theorem le_top {α : Type u} [order_top α] {a : α} : a ≤ ⊤ := order_top.le_top a theorem top_unique {α : Type u} [order_top α] {a : α} (h : ⊤ ≤ a) : a = ⊤ := le_antisymm le_top h -- TODO: delete in favor of the next? theorem eq_top_iff {α : Type u} [order_top α] {a : α} : a = ⊤ ↔ ⊤ ≤ a := { mp := fun (eq : a = ⊤) => Eq.symm eq ▸ le_refl ⊤, mpr := top_unique } @[simp] theorem top_le_iff {α : Type u} [order_top α] {a : α} : ⊤ ≤ a ↔ a = ⊤ := { mp := top_unique, mpr := fun (h : a = ⊤) => Eq.symm h ▸ le_refl ⊤ } @[simp] theorem not_top_lt {α : Type u} [order_top α] {a : α} : ¬⊤ < a := fun (h : ⊤ < a) => lt_irrefl a (lt_of_le_of_lt le_top h) theorem eq_top_mono {α : Type u} [order_top α] {a : α} {b : α} (h : a ≤ b) (h₂ : a = ⊤) : b = ⊤ := iff.mp top_le_iff (h₂ ▸ h) theorem lt_top_iff_ne_top {α : Type u} [order_top α] {a : α} : a < ⊤ ↔ a ≠ ⊤ := sorry theorem ne_top_of_lt {α : Type u} [order_top α] {a : α} {b : α} (h : a < b) : a ≠ ⊤ := iff.mp lt_top_iff_ne_top (lt_of_lt_of_le h le_top) theorem ne_top_of_le_ne_top {α : Type u} [order_top α] {a : α} {b : α} (hb : b ≠ ⊤) (hab : a ≤ b) : a ≠ ⊤ := fun (ha : a = ⊤) => hb (top_unique (ha ▸ hab)) theorem strict_mono.top_preimage_top' {α : Type u} {β : Type v} [linear_order α] [order_top β] {f : α → β} (H : strict_mono f) {a : α} (h_top : f a = ⊤) (x : α) : x ≤ a := strict_mono.top_preimage_top H (fun (p : β) => eq.mpr (id (Eq._oldrec (Eq.refl (p ≤ f a)) h_top)) le_top) x theorem order_top.ext_top {α : Type u_1} {A : order_top α} {B : order_top α} (H : ∀ (x y : α), x ≤ y ↔ x ≤ y) : ⊤ = ⊤ := top_unique (eq.mpr (id (Eq._oldrec (Eq.refl (⊤ ≤ ⊤)) (Eq.symm (propext (H ⊤ ⊤))))) le_top) theorem order_top.ext {α : Type u_1} {A : order_top α} {B : order_top α} (H : ∀ (x y : α), x ≤ y ↔ x ≤ y) : A = B := sorry /-- An `order_bot` is a partial order with a minimal element. (We could state this on preorders, but then it wouldn't be unique so distinguishing one would seem odd.) -/ class order_bot (α : Type u) extends has_bot α, partial_order α where bot_le : ∀ (a : α), ⊥ ≤ a @[simp] theorem bot_le {α : Type u} [order_bot α] {a : α} : ⊥ ≤ a := order_bot.bot_le a theorem bot_unique {α : Type u} [order_bot α] {a : α} (h : a ≤ ⊥) : a = ⊥ := le_antisymm h bot_le -- TODO: delete? theorem eq_bot_iff {α : Type u} [order_bot α] {a : α} : a = ⊥ ↔ a ≤ ⊥ := { mp := fun (eq : a = ⊥) => Eq.symm eq ▸ le_refl ⊥, mpr := bot_unique } @[simp] theorem le_bot_iff {α : Type u} [order_bot α] {a : α} : a ≤ ⊥ ↔ a = ⊥ := { mp := bot_unique, mpr := fun (h : a = ⊥) => Eq.symm h ▸ le_refl ⊥ } @[simp] theorem not_lt_bot {α : Type u} [order_bot α] {a : α} : ¬a < ⊥ := fun (h : a < ⊥) => lt_irrefl a (lt_of_lt_of_le h bot_le) theorem ne_bot_of_le_ne_bot {α : Type u} [order_bot α] {a : α} {b : α} (hb : b ≠ ⊥) (hab : b ≤ a) : a ≠ ⊥ := fun (ha : a = ⊥) => hb (bot_unique (ha ▸ hab)) theorem eq_bot_mono {α : Type u} [order_bot α] {a : α} {b : α} (h : a ≤ b) (h₂ : b = ⊥) : a = ⊥ := iff.mp le_bot_iff (h₂ ▸ h) theorem bot_lt_iff_ne_bot {α : Type u} [order_bot α] {a : α} : ⊥ < a ↔ a ≠ ⊥ := sorry theorem ne_bot_of_gt {α : Type u} [order_bot α] {a : α} {b : α} (h : a < b) : b ≠ ⊥ := iff.mp bot_lt_iff_ne_bot (lt_of_le_of_lt bot_le h) theorem strict_mono.bot_preimage_bot' {α : Type u} {β : Type v} [linear_order α] [order_bot β] {f : α → β} (H : strict_mono f) {a : α} (h_bot : f a = ⊥) (x : α) : a ≤ x := strict_mono.bot_preimage_bot H (fun (p : β) => eq.mpr (id (Eq._oldrec (Eq.refl (f a ≤ p)) h_bot)) bot_le) x theorem order_bot.ext_bot {α : Type u_1} {A : order_bot α} {B : order_bot α} (H : ∀ (x y : α), x ≤ y ↔ x ≤ y) : ⊥ = ⊥ := bot_unique (eq.mpr (id (Eq._oldrec (Eq.refl (⊥ ≤ ⊥)) (Eq.symm (propext (H ⊥ ⊥))))) bot_le) theorem order_bot.ext {α : Type u_1} {A : order_bot α} {B : order_bot α} (H : ∀ (x y : α), x ≤ y ↔ x ≤ y) : A = B := sorry /-- A `semilattice_sup_top` is a semilattice with top and join. -/ class semilattice_sup_top (α : Type u) extends semilattice_sup α, order_top α where @[simp] theorem top_sup_eq {α : Type u} [semilattice_sup_top α] {a : α} : ⊤ ⊔ a = ⊤ := sup_of_le_left le_top @[simp] theorem sup_top_eq {α : Type u} [semilattice_sup_top α] {a : α} : a ⊔ ⊤ = ⊤ := sup_of_le_right le_top /-- A `semilattice_sup_bot` is a semilattice with bottom and join. -/ class semilattice_sup_bot (α : Type u) extends order_bot α, semilattice_sup α where @[simp] theorem bot_sup_eq {α : Type u} [semilattice_sup_bot α] {a : α} : ⊥ ⊔ a = a := sup_of_le_right bot_le @[simp] theorem sup_bot_eq {α : Type u} [semilattice_sup_bot α] {a : α} : a ⊔ ⊥ = a := sup_of_le_left bot_le @[simp] theorem sup_eq_bot_iff {α : Type u} [semilattice_sup_bot α] {a : α} {b : α} : a ⊔ b = ⊥ ↔ a = ⊥ ∧ b = ⊥ := sorry protected instance nat.semilattice_sup_bot : semilattice_sup_bot ℕ := semilattice_sup_bot.mk 0 distrib_lattice.le distrib_lattice.lt distrib_lattice.le_refl distrib_lattice.le_trans distrib_lattice.le_antisymm nat.zero_le distrib_lattice.sup distrib_lattice.le_sup_left distrib_lattice.le_sup_right distrib_lattice.sup_le /-- A `semilattice_inf_top` is a semilattice with top and meet. -/ class semilattice_inf_top (α : Type u) extends semilattice_inf α, order_top α where @[simp] theorem top_inf_eq {α : Type u} [semilattice_inf_top α] {a : α} : ⊤ ⊓ a = a := inf_of_le_right le_top @[simp] theorem inf_top_eq {α : Type u} [semilattice_inf_top α] {a : α} : a ⊓ ⊤ = a := inf_of_le_left le_top @[simp] theorem inf_eq_top_iff {α : Type u} [semilattice_inf_top α] {a : α} {b : α} : a ⊓ b = ⊤ ↔ a = ⊤ ∧ b = ⊤ := sorry /-- A `semilattice_inf_bot` is a semilattice with bottom and meet. -/ class semilattice_inf_bot (α : Type u) extends order_bot α, semilattice_inf α where @[simp] theorem bot_inf_eq {α : Type u} [semilattice_inf_bot α] {a : α} : ⊥ ⊓ a = ⊥ := inf_of_le_left bot_le @[simp] theorem inf_bot_eq {α : Type u} [semilattice_inf_bot α] {a : α} : a ⊓ ⊥ = ⊥ := inf_of_le_right bot_le /- Bounded lattices -/ /-- A bounded lattice is a lattice with a top and bottom element, denoted `⊤` and `⊥` respectively. This allows for the interpretation of all finite suprema and infima, taking `inf ∅ = ⊤` and `sup ∅ = ⊥`. -/ class bounded_lattice (α : Type u) extends order_bot α, order_top α, lattice α where protected instance semilattice_inf_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_top α := semilattice_inf_top.mk bounded_lattice.top bounded_lattice.le bounded_lattice.lt bounded_lattice.le_refl bounded_lattice.le_trans bounded_lattice.le_antisymm sorry bounded_lattice.inf bounded_lattice.inf_le_left bounded_lattice.inf_le_right bounded_lattice.le_inf protected instance semilattice_inf_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_bot α := semilattice_inf_bot.mk bounded_lattice.bot bounded_lattice.le bounded_lattice.lt bounded_lattice.le_refl bounded_lattice.le_trans bounded_lattice.le_antisymm sorry bounded_lattice.inf bounded_lattice.inf_le_left bounded_lattice.inf_le_right bounded_lattice.le_inf protected instance semilattice_sup_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_top α := semilattice_sup_top.mk bounded_lattice.top bounded_lattice.le bounded_lattice.lt bounded_lattice.le_refl bounded_lattice.le_trans bounded_lattice.le_antisymm sorry bounded_lattice.sup bounded_lattice.le_sup_left bounded_lattice.le_sup_right bounded_lattice.sup_le protected instance semilattice_sup_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_bot α := semilattice_sup_bot.mk bounded_lattice.bot bounded_lattice.le bounded_lattice.lt bounded_lattice.le_refl bounded_lattice.le_trans bounded_lattice.le_antisymm sorry bounded_lattice.sup bounded_lattice.le_sup_left bounded_lattice.le_sup_right bounded_lattice.sup_le theorem bounded_lattice.ext {α : Type u_1} {A : bounded_lattice α} {B : bounded_lattice α} (H : ∀ (x y : α), x ≤ y ↔ x ≤ y) : A = B := sorry /-- A bounded distributive lattice is exactly what it sounds like. -/ class bounded_distrib_lattice (α : Type u_1) extends distrib_lattice α, bounded_lattice α where theorem inf_eq_bot_iff_le_compl {α : Type u} [bounded_distrib_lattice α] {a : α} {b : α} {c : α} (h₁ : b ⊔ c = ⊤) (h₂ : b ⊓ c = ⊥) : a ⊓ b = ⊥ ↔ a ≤ c := sorry /- Prop instance -/ protected instance bounded_distrib_lattice_Prop : bounded_distrib_lattice Prop := bounded_distrib_lattice.mk Or (fun (a b : Prop) => a → b) (distrib_lattice.lt._default fun (a b : Prop) => a → b) sorry sorry sorry Or.inl Or.inr sorry And and.left and.right sorry sorry True sorry False false.elim protected instance Prop.linear_order : linear_order Prop := linear_order.mk partial_order.le partial_order.lt sorry sorry sorry sorry (classical.dec_rel LessEq) Mathlib.decidable_eq_of_decidable_le Mathlib.decidable_lt_of_decidable_le @[simp] theorem le_iff_imp {p : Prop} {q : Prop} : p ≤ q ↔ p → q := iff.rfl theorem monotone_and {α : Type u} [preorder α] {p : α → Prop} {q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone fun (x : α) => p x ∧ q x := fun (a b : α) (h : a ≤ b) => and.imp (m_p h) (m_q h) -- Note: by finish [monotone] doesn't work theorem monotone_or {α : Type u} [preorder α] {p : α → Prop} {q : α → Prop} (m_p : monotone p) (m_q : monotone q) : monotone fun (x : α) => p x ∨ q x := fun (a b : α) (h : a ≤ b) => or.imp (m_p h) (m_q h) protected instance pi.order_bot {α : Type u_1} {β : α → Type u_2} [(a : α) → order_bot (β a)] : order_bot ((a : α) → β a) := order_bot.mk (fun (_x : α) => ⊥) partial_order.le partial_order.lt sorry sorry sorry sorry /- Function lattices -/ protected instance pi.has_sup {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → has_sup (α i)] : has_sup ((i : ι) → α i) := has_sup.mk fun (f g : (i : ι) → α i) (i : ι) => f i ⊔ g i @[simp] theorem sup_apply {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → has_sup (α i)] (f : (i : ι) → α i) (g : (i : ι) → α i) (i : ι) : has_sup.sup f g i = f i ⊔ g i := rfl protected instance pi.has_inf {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → has_inf (α i)] : has_inf ((i : ι) → α i) := has_inf.mk fun (f g : (i : ι) → α i) (i : ι) => f i ⊓ g i @[simp] theorem inf_apply {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → has_inf (α i)] (f : (i : ι) → α i) (g : (i : ι) → α i) (i : ι) : has_inf.inf f g i = f i ⊓ g i := rfl protected instance pi.has_bot {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → has_bot (α i)] : has_bot ((i : ι) → α i) := has_bot.mk fun (i : ι) => ⊥ @[simp] theorem bot_apply {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → has_bot (α i)] (i : ι) : ⊥ = ⊥ := rfl protected instance pi.has_top {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → has_top (α i)] : has_top ((i : ι) → α i) := has_top.mk fun (i : ι) => ⊤ @[simp] theorem top_apply {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → has_top (α i)] (i : ι) : ⊤ = ⊤ := rfl protected instance pi.semilattice_sup {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → semilattice_sup (α i)] : semilattice_sup ((i : ι) → α i) := semilattice_sup.mk has_sup.sup partial_order.le partial_order.lt sorry sorry sorry sorry sorry sorry protected instance pi.semilattice_inf {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → semilattice_inf (α i)] : semilattice_inf ((i : ι) → α i) := semilattice_inf.mk has_inf.inf partial_order.le partial_order.lt sorry sorry sorry sorry sorry sorry protected instance pi.semilattice_inf_bot {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → semilattice_inf_bot (α i)] : semilattice_inf_bot ((i : ι) → α i) := semilattice_inf_bot.mk ⊥ partial_order.le partial_order.lt sorry sorry sorry sorry has_inf.inf sorry sorry sorry protected instance pi.semilattice_inf_top {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → semilattice_inf_top (α i)] : semilattice_inf_top ((i : ι) → α i) := semilattice_inf_top.mk ⊤ partial_order.le partial_order.lt sorry sorry sorry sorry has_inf.inf sorry sorry sorry protected instance pi.semilattice_sup_bot {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → semilattice_sup_bot (α i)] : semilattice_sup_bot ((i : ι) → α i) := semilattice_sup_bot.mk ⊥ partial_order.le partial_order.lt sorry sorry sorry sorry has_sup.sup sorry sorry sorry protected instance pi.semilattice_sup_top {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → semilattice_sup_top (α i)] : semilattice_sup_top ((i : ι) → α i) := semilattice_sup_top.mk ⊤ partial_order.le partial_order.lt sorry sorry sorry sorry has_sup.sup sorry sorry sorry protected instance pi.lattice {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → lattice (α i)] : lattice ((i : ι) → α i) := lattice.mk semilattice_sup.sup semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry protected instance pi.bounded_lattice {ι : Type u_1} {α : ι → Type u_2} [(i : ι) → bounded_lattice (α i)] : bounded_lattice ((i : ι) → α i) := bounded_lattice.mk semilattice_sup_top.sup semilattice_sup_top.le semilattice_sup_top.lt sorry sorry sorry sorry sorry sorry semilattice_inf_bot.inf sorry sorry sorry semilattice_sup_top.top sorry semilattice_inf_bot.bot sorry theorem eq_bot_of_bot_eq_top {α : Type u_1} [bounded_lattice α] (hα : ⊥ = ⊤) (x : α) : x = ⊥ := eq_bot_mono le_top (Eq.symm hα) theorem eq_top_of_bot_eq_top {α : Type u_1} [bounded_lattice α] (hα : ⊥ = ⊤) (x : α) : x = ⊤ := eq_top_mono bot_le hα theorem subsingleton_of_top_le_bot {α : Type u_1} [bounded_lattice α] (h : ⊤ ≤ ⊥) : subsingleton α := subsingleton.intro fun (a b : α) => le_antisymm (le_trans le_top (le_trans h bot_le)) (le_trans le_top (le_trans h bot_le)) theorem subsingleton_of_bot_eq_top {α : Type u_1} [bounded_lattice α] (hα : ⊥ = ⊤) : subsingleton α := subsingleton_of_top_le_bot (ge_of_eq hα) theorem subsingleton_iff_bot_eq_top {α : Type u_1} [bounded_lattice α] : ⊥ = ⊤ ↔ subsingleton α := { mp := subsingleton_of_bot_eq_top, mpr := fun (h : subsingleton α) => subsingleton.elim ⊥ ⊤ } /-- Attach `⊥` to a type. -/ def with_bot (α : Type u_1) := Option α namespace with_bot protected instance has_coe_t {α : Type u} : has_coe_t α (with_bot α) := has_coe_t.mk some protected instance has_bot {α : Type u} : has_bot (with_bot α) := has_bot.mk none protected instance inhabited {α : Type u} : Inhabited (with_bot α) := { default := ⊥ } theorem none_eq_bot {α : Type u} : none = ⊥ := rfl theorem some_eq_coe {α : Type u} (a : α) : some a = ↑a := rfl /-- Recursor for `with_bot` using the preferred forms `⊥` and `↑a`. -/ def rec_bot_coe {α : Type u} {C : with_bot α → Sort u_1} (h₁ : C ⊥) (h₂ : (a : α) → C ↑a) (n : with_bot α) : C n := Option.rec h₁ h₂ theorem coe_eq_coe {α : Type u} {a : α} {b : α} : ↑a = ↑b ↔ a = b := eq.mpr (id (Eq._oldrec (Eq.refl (↑a = ↑b ↔ a = b)) (Eq.symm (option.some.inj_eq a b)))) (iff.refl (↑a = ↑b)) protected instance has_lt {α : Type u} [HasLess α] : HasLess (with_bot α) := { Less := fun (o₁ o₂ : Option α) => ∃ (b : α), ∃ (H : b ∈ o₂), ∀ (a : α), a ∈ o₁ → a < b } @[simp] theorem some_lt_some {α : Type u} [HasLess α] {a : α} {b : α} : some a < some b ↔ a < b := sorry theorem bot_lt_some {α : Type u} [HasLess α] (a : α) : ⊥ < some a := Exists.intro a (Exists.intro rfl fun (b : α) (hb : b ∈ ⊥) => false.elim (option.not_mem_none b hb)) theorem bot_lt_coe {α : Type u} [HasLess α] (a : α) : ⊥ < ↑a := bot_lt_some a protected instance preorder {α : Type u} [preorder α] : preorder (with_bot α) := preorder.mk (fun (o₁ o₂ : Option α) => ∀ (a : α) (H : a ∈ o₁), ∃ (b : α), ∃ (H : b ∈ o₂), a ≤ b) Less sorry sorry protected instance partial_order {α : Type u} [partial_order α] : partial_order (with_bot α) := partial_order.mk preorder.le preorder.lt sorry sorry sorry protected instance order_bot {α : Type u} [partial_order α] : order_bot (with_bot α) := order_bot.mk ⊥ partial_order.le partial_order.lt sorry sorry sorry sorry @[simp] theorem coe_le_coe {α : Type u} [preorder α] {a : α} {b : α} : ↑a ≤ ↑b ↔ a ≤ b := sorry @[simp] theorem some_le_some {α : Type u} [preorder α] {a : α} {b : α} : some a ≤ some b ↔ a ≤ b := coe_le_coe theorem coe_le {α : Type u} [partial_order α] {a : α} {b : α} {o : Option α} : b ∈ o → (↑a ≤ o ↔ a ≤ b) := sorry theorem coe_lt_coe {α : Type u} [partial_order α] {a : α} {b : α} : ↑a < ↑b ↔ a < b := some_lt_some theorem le_coe_get_or_else {α : Type u} [preorder α] (a : with_bot α) (b : α) : a ≤ ↑(option.get_or_else a b) := sorry @[simp] theorem get_or_else_bot {α : Type u} (a : α) : option.get_or_else ⊥ a = a := rfl theorem get_or_else_bot_le_iff {α : Type u} [order_bot α] {a : with_bot α} {b : α} : option.get_or_else a ⊥ ≤ b ↔ a ≤ ↑b := sorry protected instance decidable_le {α : Type u} [preorder α] [DecidableRel LessEq] : DecidableRel LessEq := sorry protected instance decidable_lt {α : Type u} [HasLess α] [DecidableRel Less] : DecidableRel Less := sorry protected instance linear_order {α : Type u} [linear_order α] : linear_order (with_bot α) := linear_order.mk partial_order.le partial_order.lt sorry sorry sorry sorry with_bot.decidable_le Mathlib.decidable_eq_of_decidable_le with_bot.decidable_lt protected instance semilattice_sup {α : Type u} [semilattice_sup α] : semilattice_sup_bot (with_bot α) := semilattice_sup_bot.mk order_bot.bot order_bot.le order_bot.lt sorry sorry sorry sorry (option.lift_or_get has_sup.sup) sorry sorry sorry protected instance semilattice_inf {α : Type u} [semilattice_inf α] : semilattice_inf_bot (with_bot α) := semilattice_inf_bot.mk order_bot.bot order_bot.le order_bot.lt sorry sorry sorry sorry (fun (o₁ o₂ : with_bot α) => option.bind o₁ fun (a : α) => option.map (fun (b : α) => a ⊓ b) o₂) sorry sorry sorry protected instance lattice {α : Type u} [lattice α] : lattice (with_bot α) := lattice.mk semilattice_sup_bot.sup semilattice_sup_bot.le semilattice_sup_bot.lt sorry sorry sorry sorry sorry sorry semilattice_inf_bot.inf sorry sorry sorry theorem lattice_eq_DLO {α : Type u} [linear_order α] : Mathlib.lattice_of_linear_order = with_bot.lattice := lattice.ext fun (x y : with_bot α) => iff.rfl theorem sup_eq_max {α : Type u} [linear_order α] (x : with_bot α) (y : with_bot α) : x ⊔ y = max x y := eq.mpr (id (Eq._oldrec (Eq.refl (x ⊔ y = max x y)) (Eq.symm sup_eq_max))) (eq.mpr (id (Eq._oldrec (Eq.refl (x ⊔ y = x ⊔ y)) lattice_eq_DLO)) (Eq.refl (x ⊔ y))) theorem inf_eq_min {α : Type u} [linear_order α] (x : with_bot α) (y : with_bot α) : x ⊓ y = min x y := eq.mpr (id (Eq._oldrec (Eq.refl (x ⊓ y = min x y)) (Eq.symm inf_eq_min))) (eq.mpr (id (Eq._oldrec (Eq.refl (x ⊓ y = x ⊓ y)) lattice_eq_DLO)) (Eq.refl (x ⊓ y))) protected instance order_top {α : Type u} [order_top α] : order_top (with_bot α) := order_top.mk (some ⊤) partial_order.le partial_order.lt sorry sorry sorry sorry protected instance bounded_lattice {α : Type u} [bounded_lattice α] : bounded_lattice (with_bot α) := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry theorem well_founded_lt {α : Type u} [partial_order α] (h : well_founded Less) : well_founded Less := sorry protected instance densely_ordered {α : Type u} [partial_order α] [densely_ordered α] [no_bot_order α] : densely_ordered (with_bot α) := densely_ordered.mk fun (a b : with_bot α) => sorry end with_bot --TODO(Mario): Construct using order dual on with_bot /-- Attach `⊤` to a type. -/ def with_top (α : Type u_1) := Option α namespace with_top protected instance has_coe_t {α : Type u} : has_coe_t α (with_top α) := has_coe_t.mk some protected instance has_top {α : Type u} : has_top (with_top α) := has_top.mk none protected instance inhabited {α : Type u} : Inhabited (with_top α) := { default := ⊤ } theorem none_eq_top {α : Type u} : none = ⊤ := rfl theorem some_eq_coe {α : Type u} (a : α) : some a = ↑a := rfl /-- Recursor for `with_top` using the preferred forms `⊤` and `↑a`. -/ def rec_top_coe {α : Type u} {C : with_top α → Sort u_1} (h₁ : C ⊤) (h₂ : (a : α) → C ↑a) (n : with_top α) : C n := Option.rec h₁ h₂ theorem coe_eq_coe {α : Type u} {a : α} {b : α} : ↑a = ↑b ↔ a = b := eq.mpr (id (Eq._oldrec (Eq.refl (↑a = ↑b ↔ a = b)) (Eq.symm (option.some.inj_eq a b)))) (iff.refl (↑a = ↑b)) @[simp] theorem top_ne_coe {α : Type u} {a : α} : ⊤ ≠ ↑a := fun (ᾰ : ⊤ = ↑a) => eq.dcases_on ᾰ (fun (a_1 : some a = none) => option.no_confusion a_1) (Eq.refl ↑a) (HEq.refl ᾰ) @[simp] theorem coe_ne_top {α : Type u} {a : α} : ↑a ≠ ⊤ := fun (ᾰ : ↑a = ⊤) => eq.dcases_on ᾰ (fun (a_1 : none = some a) => option.no_confusion a_1) (Eq.refl ⊤) (HEq.refl ᾰ) protected instance has_lt {α : Type u} [HasLess α] : HasLess (with_top α) := { Less := fun (o₁ o₂ : Option α) => ∃ (b : α), ∃ (H : b ∈ o₁), ∀ (a : α), a ∈ o₂ → b < a } protected instance has_le {α : Type u} [HasLessEq α] : HasLessEq (with_top α) := { LessEq := fun (o₁ o₂ : Option α) => ∀ (a : α) (H : a ∈ o₂), ∃ (b : α), ∃ (H : b ∈ o₁), b ≤ a } @[simp] theorem some_lt_some {α : Type u} [HasLess α] {a : α} {b : α} : some a < some b ↔ a < b := sorry @[simp] theorem some_le_some {α : Type u} [HasLessEq α] {a : α} {b : α} : some a ≤ some b ↔ a ≤ b := sorry @[simp] theorem le_none {α : Type u} [HasLessEq α] {a : with_top α} : a ≤ none := sorry @[simp] theorem some_lt_none {α : Type u} [HasLess α] {a : α} : some a < none := sorry protected instance can_lift {α : Type u} : can_lift (with_top α) α := can_lift.mk coe (fun (r : with_top α) => r ≠ ⊤) sorry protected instance preorder {α : Type u} [preorder α] : preorder (with_top α) := preorder.mk (fun (o₁ o₂ : Option α) => ∀ (a : α) (H : a ∈ o₂), ∃ (b : α), ∃ (H : b ∈ o₁), b ≤ a) Less sorry sorry protected instance partial_order {α : Type u} [partial_order α] : partial_order (with_top α) := partial_order.mk preorder.le preorder.lt sorry sorry sorry protected instance order_top {α : Type u} [partial_order α] : order_top (with_top α) := order_top.mk ⊤ partial_order.le partial_order.lt sorry sorry sorry sorry @[simp] theorem coe_le_coe {α : Type u} [partial_order α] {a : α} {b : α} : ↑a ≤ ↑b ↔ a ≤ b := sorry theorem le_coe {α : Type u} [partial_order α] {a : α} {b : α} {o : Option α} : a ∈ o → (o ≤ ↑b ↔ a ≤ b) := sorry theorem le_coe_iff {α : Type u} [partial_order α] {b : α} {x : with_top α} : x ≤ ↑b ↔ ∃ (a : α), x = ↑a ∧ a ≤ b := sorry theorem coe_le_iff {α : Type u} [partial_order α] {a : α} {x : with_top α} : ↑a ≤ x ↔ ∀ (b : α), x = ↑b → a ≤ b := sorry theorem lt_iff_exists_coe {α : Type u} [partial_order α] {a : with_top α} {b : with_top α} : a < b ↔ ∃ (p : α), a = ↑p ∧ ↑p < b := sorry theorem coe_lt_coe {α : Type u} [partial_order α] {a : α} {b : α} : ↑a < ↑b ↔ a < b := some_lt_some theorem coe_lt_top {α : Type u} [partial_order α] (a : α) : ↑a < ⊤ := some_lt_none theorem coe_lt_iff {α : Type u} [partial_order α] {a : α} {x : with_top α} : ↑a < x ↔ ∀ (b : α), x = ↑b → a < b := sorry theorem not_top_le_coe {α : Type u} [partial_order α] (a : α) : ¬⊤ ≤ ↑a := fun (h : ⊤ ≤ ↑a) => false.elim (lt_irrefl ⊤ (lt_of_le_of_lt h (coe_lt_top a))) protected instance decidable_le {α : Type u} [preorder α] [DecidableRel LessEq] : DecidableRel LessEq := fun (x y : with_top α) => with_bot.decidable_le y x protected instance decidable_lt {α : Type u} [HasLess α] [DecidableRel Less] : DecidableRel Less := fun (x y : with_top α) => with_bot.decidable_lt y x protected instance linear_order {α : Type u} [linear_order α] : linear_order (with_top α) := linear_order.mk partial_order.le partial_order.lt sorry sorry sorry sorry with_top.decidable_le Mathlib.decidable_eq_of_decidable_le with_top.decidable_lt protected instance semilattice_inf {α : Type u} [semilattice_inf α] : semilattice_inf_top (with_top α) := semilattice_inf_top.mk order_top.top order_top.le order_top.lt sorry sorry sorry sorry (option.lift_or_get has_inf.inf) sorry sorry sorry theorem coe_inf {α : Type u} [semilattice_inf α] (a : α) (b : α) : ↑(a ⊓ b) = ↑a ⊓ ↑b := rfl protected instance semilattice_sup {α : Type u} [semilattice_sup α] : semilattice_sup_top (with_top α) := semilattice_sup_top.mk order_top.top order_top.le order_top.lt sorry sorry sorry sorry (fun (o₁ o₂ : with_top α) => option.bind o₁ fun (a : α) => option.map (fun (b : α) => a ⊔ b) o₂) sorry sorry sorry theorem coe_sup {α : Type u} [semilattice_sup α] (a : α) (b : α) : ↑(a ⊔ b) = ↑a ⊔ ↑b := rfl protected instance lattice {α : Type u} [lattice α] : lattice (with_top α) := lattice.mk semilattice_sup_top.sup semilattice_sup_top.le semilattice_sup_top.lt sorry sorry sorry sorry sorry sorry semilattice_inf_top.inf sorry sorry sorry theorem lattice_eq_DLO {α : Type u} [linear_order α] : Mathlib.lattice_of_linear_order = with_top.lattice := lattice.ext fun (x y : with_top α) => iff.rfl theorem sup_eq_max {α : Type u} [linear_order α] (x : with_top α) (y : with_top α) : x ⊔ y = max x y := eq.mpr (id (Eq._oldrec (Eq.refl (x ⊔ y = max x y)) (Eq.symm sup_eq_max))) (eq.mpr (id (Eq._oldrec (Eq.refl (x ⊔ y = x ⊔ y)) lattice_eq_DLO)) (Eq.refl (x ⊔ y))) theorem inf_eq_min {α : Type u} [linear_order α] (x : with_top α) (y : with_top α) : x ⊓ y = min x y := eq.mpr (id (Eq._oldrec (Eq.refl (x ⊓ y = min x y)) (Eq.symm inf_eq_min))) (eq.mpr (id (Eq._oldrec (Eq.refl (x ⊓ y = x ⊓ y)) lattice_eq_DLO)) (Eq.refl (x ⊓ y))) protected instance order_bot {α : Type u} [order_bot α] : order_bot (with_top α) := order_bot.mk (some ⊥) partial_order.le partial_order.lt sorry sorry sorry sorry protected instance bounded_lattice {α : Type u} [bounded_lattice α] : bounded_lattice (with_top α) := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry theorem well_founded_lt {α : Type u_1} [partial_order α] (h : well_founded Less) : well_founded Less := sorry protected instance densely_ordered {α : Type u} [partial_order α] [densely_ordered α] [no_top_order α] : densely_ordered (with_top α) := densely_ordered.mk fun (a b : with_top α) => sorry theorem lt_iff_exists_coe_btwn {α : Type u} [partial_order α] [densely_ordered α] [no_top_order α] {a : with_top α} {b : with_top α} : a < b ↔ ∃ (x : α), a < ↑x ∧ ↑x < b := sorry end with_top namespace subtype /-- A subtype forms a `⊔`-`⊥`-semilattice if `⊥` and `⊔` preserve the property. -/ protected def semilattice_sup_bot {α : Type u} [semilattice_sup_bot α] {P : α → Prop} (Pbot : P ⊥) (Psup : ∀ {x y : α}, P x → P y → P (x ⊔ y)) : semilattice_sup_bot (Subtype fun (x : α) => P x) := semilattice_sup_bot.mk { val := ⊥, property := Pbot } semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry semilattice_sup.sup sorry sorry sorry /-- A subtype forms a `⊓`-`⊥`-semilattice if `⊥` and `⊓` preserve the property. -/ protected def semilattice_inf_bot {α : Type u} [semilattice_inf_bot α] {P : α → Prop} (Pbot : P ⊥) (Pinf : ∀ {x y : α}, P x → P y → P (x ⊓ y)) : semilattice_inf_bot (Subtype fun (x : α) => P x) := semilattice_inf_bot.mk { val := ⊥, property := Pbot } semilattice_inf.le semilattice_inf.lt sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry /-- A subtype forms a `⊓`-`⊤`-semilattice if `⊤` and `⊓` preserve the property. -/ protected def semilattice_inf_top {α : Type u} [semilattice_inf_top α] {P : α → Prop} (Ptop : P ⊤) (Pinf : ∀ {x y : α}, P x → P y → P (x ⊓ y)) : semilattice_inf_top (Subtype fun (x : α) => P x) := semilattice_inf_top.mk { val := ⊤, property := Ptop } semilattice_inf.le semilattice_inf.lt sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry end subtype namespace order_dual protected instance has_top (α : Type u) [has_bot α] : has_top (order_dual α) := has_top.mk ⊥ protected instance has_bot (α : Type u) [has_top α] : has_bot (order_dual α) := has_bot.mk ⊤ protected instance order_top (α : Type u) [order_bot α] : order_top (order_dual α) := order_top.mk ⊤ partial_order.le partial_order.lt sorry sorry sorry bot_le protected instance order_bot (α : Type u) [order_top α] : order_bot (order_dual α) := order_bot.mk ⊥ partial_order.le partial_order.lt sorry sorry sorry le_top protected instance semilattice_sup_top (α : Type u) [semilattice_inf_bot α] : semilattice_sup_top (order_dual α) := semilattice_sup_top.mk order_top.top semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry semilattice_sup.sup sorry sorry sorry protected instance semilattice_sup_bot (α : Type u) [semilattice_inf_top α] : semilattice_sup_bot (order_dual α) := semilattice_sup_bot.mk order_bot.bot semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry semilattice_sup.sup sorry sorry sorry protected instance semilattice_inf_top (α : Type u) [semilattice_sup_bot α] : semilattice_inf_top (order_dual α) := semilattice_inf_top.mk order_top.top semilattice_inf.le semilattice_inf.lt sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry protected instance semilattice_inf_bot (α : Type u) [semilattice_sup_top α] : semilattice_inf_bot (order_dual α) := semilattice_inf_bot.mk order_bot.bot semilattice_inf.le semilattice_inf.lt sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry protected instance bounded_lattice (α : Type u) [bounded_lattice α] : bounded_lattice (order_dual α) := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry protected instance bounded_distrib_lattice (α : Type u) [bounded_distrib_lattice α] : bounded_distrib_lattice (order_dual α) := bounded_distrib_lattice.mk bounded_lattice.sup bounded_lattice.le bounded_lattice.lt sorry sorry sorry sorry sorry sorry bounded_lattice.inf sorry sorry sorry sorry bounded_lattice.top sorry bounded_lattice.bot sorry end order_dual namespace prod protected instance has_top (α : Type u) (β : Type v) [has_top α] [has_top β] : has_top (α × β) := has_top.mk (⊤, ⊤) protected instance has_bot (α : Type u) (β : Type v) [has_bot α] [has_bot β] : has_bot (α × β) := has_bot.mk (⊥, ⊥) protected instance order_top (α : Type u) (β : Type v) [order_top α] [order_top β] : order_top (α × β) := order_top.mk ⊤ partial_order.le partial_order.lt sorry sorry sorry sorry protected instance order_bot (α : Type u) (β : Type v) [order_bot α] [order_bot β] : order_bot (α × β) := order_bot.mk ⊥ partial_order.le partial_order.lt sorry sorry sorry sorry protected instance semilattice_sup_top (α : Type u) (β : Type v) [semilattice_sup_top α] [semilattice_sup_top β] : semilattice_sup_top (α × β) := semilattice_sup_top.mk order_top.top semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry semilattice_sup.sup sorry sorry sorry protected instance semilattice_inf_top (α : Type u) (β : Type v) [semilattice_inf_top α] [semilattice_inf_top β] : semilattice_inf_top (α × β) := semilattice_inf_top.mk order_top.top semilattice_inf.le semilattice_inf.lt sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry protected instance semilattice_sup_bot (α : Type u) (β : Type v) [semilattice_sup_bot α] [semilattice_sup_bot β] : semilattice_sup_bot (α × β) := semilattice_sup_bot.mk order_bot.bot semilattice_sup.le semilattice_sup.lt sorry sorry sorry sorry semilattice_sup.sup sorry sorry sorry protected instance semilattice_inf_bot (α : Type u) (β : Type v) [semilattice_inf_bot α] [semilattice_inf_bot β] : semilattice_inf_bot (α × β) := semilattice_inf_bot.mk order_bot.bot semilattice_inf.le semilattice_inf.lt sorry sorry sorry sorry semilattice_inf.inf sorry sorry sorry protected instance bounded_lattice (α : Type u) (β : Type v) [bounded_lattice α] [bounded_lattice β] : bounded_lattice (α × β) := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry order_top.top sorry order_bot.bot sorry protected instance bounded_distrib_lattice (α : Type u) (β : Type v) [bounded_distrib_lattice α] [bounded_distrib_lattice β] : bounded_distrib_lattice (α × β) := bounded_distrib_lattice.mk bounded_lattice.sup bounded_lattice.le bounded_lattice.lt sorry sorry sorry sorry sorry sorry bounded_lattice.inf sorry sorry sorry sorry bounded_lattice.top sorry bounded_lattice.bot sorry end prod /-- Two elements of a lattice are disjoint if their inf is the bottom element. (This generalizes disjoint sets, viewed as members of the subset lattice.) -/ def disjoint {α : Type u} [semilattice_inf_bot α] (a : α) (b : α) := a ⊓ b ≤ ⊥ theorem disjoint.eq_bot {α : Type u} [semilattice_inf_bot α] {a : α} {b : α} (h : disjoint a b) : a ⊓ b = ⊥ := iff.mpr eq_bot_iff h theorem disjoint_iff {α : Type u} [semilattice_inf_bot α] {a : α} {b : α} : disjoint a b ↔ a ⊓ b = ⊥ := iff.symm eq_bot_iff theorem disjoint.comm {α : Type u} [semilattice_inf_bot α] {a : α} {b : α} : disjoint a b ↔ disjoint b a := eq.mpr (id (Eq._oldrec (Eq.refl (disjoint a b ↔ disjoint b a)) (disjoint.equations._eqn_1 a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ⊓ b ≤ ⊥ ↔ disjoint b a)) (disjoint.equations._eqn_1 b a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a ⊓ b ≤ ⊥ ↔ b ⊓ a ≤ ⊥)) inf_comm)) (iff.refl (b ⊓ a ≤ ⊥)))) theorem disjoint.symm {α : Type u} [semilattice_inf_bot α] {a : α} {b : α} : disjoint a b → disjoint b a := iff.mp disjoint.comm @[simp] theorem disjoint_bot_left {α : Type u} [semilattice_inf_bot α] {a : α} : disjoint ⊥ a := inf_le_left @[simp] theorem disjoint_bot_right {α : Type u} [semilattice_inf_bot α] {a : α} : disjoint a ⊥ := inf_le_right theorem disjoint.mono {α : Type u} [semilattice_inf_bot α] {a : α} {b : α} {c : α} {d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) : disjoint b d → disjoint a c := le_trans (inf_le_inf h₁ h₂) theorem disjoint.mono_left {α : Type u} [semilattice_inf_bot α] {a : α} {b : α} {c : α} (h : a ≤ b) : disjoint b c → disjoint a c := disjoint.mono h (le_refl c) theorem disjoint.mono_right {α : Type u} [semilattice_inf_bot α] {a : α} {b : α} {c : α} (h : b ≤ c) : disjoint a c → disjoint a b := disjoint.mono (le_refl a) h @[simp] theorem disjoint_self {α : Type u} [semilattice_inf_bot α] {a : α} : disjoint a a ↔ a = ⊥ := sorry theorem disjoint.ne {α : Type u} [semilattice_inf_bot α] {a : α} {b : α} (ha : a ≠ ⊥) (hab : disjoint a b) : a ≠ b := sorry @[simp] theorem disjoint_top {α : Type u} [bounded_lattice α] {a : α} : disjoint a ⊤ ↔ a = ⊥ := sorry @[simp] theorem top_disjoint {α : Type u} [bounded_lattice α] {a : α} : disjoint ⊤ a ↔ a = ⊥ := sorry @[simp] theorem disjoint_sup_left {α : Type u} [bounded_distrib_lattice α] {a : α} {b : α} {c : α} : disjoint (a ⊔ b) c ↔ disjoint a c ∧ disjoint b c := sorry @[simp] theorem disjoint_sup_right {α : Type u} [bounded_distrib_lattice α] {a : α} {b : α} {c : α} : disjoint a (b ⊔ c) ↔ disjoint a b ∧ disjoint a c := sorry theorem disjoint.sup_left {α : Type u} [bounded_distrib_lattice α] {a : α} {b : α} {c : α} (ha : disjoint a c) (hb : disjoint b c) : disjoint (a ⊔ b) c := iff.mpr disjoint_sup_left { left := ha, right := hb } theorem disjoint.sup_right {α : Type u} [bounded_distrib_lattice α] {a : α} {b : α} {c : α} (hb : disjoint a b) (hc : disjoint a c) : disjoint a (b ⊔ c) := iff.mpr disjoint_sup_right { left := hb, right := hc } theorem disjoint.left_le_of_le_sup_right {α : Type u} [bounded_distrib_lattice α] {a : α} {b : α} {c : α} (h : a ≤ b ⊔ c) (hd : disjoint a c) : a ≤ b := (fun (x : a ⊓ c ≤ b ⊓ c) => le_of_inf_le_sup_le x (sup_le h le_sup_right)) (Eq.symm (iff.mp disjoint_iff hd) ▸ bot_le) theorem disjoint.left_le_of_le_sup_left {α : Type u} [bounded_distrib_lattice α] {a : α} {b : α} {c : α} (h : a ≤ c ⊔ b) (hd : disjoint a c) : a ≤ b := le_of_inf_le_sup_le (Eq.symm (iff.mp disjoint_iff hd) ▸ bot_le) (sup_comm ▸ sup_le h le_sup_left) /-! ### `is_compl` predicate -/ /-- Two elements `x` and `y` are complements of each other if `x ⊔ y = ⊤` and `x ⊓ y = ⊥`. -/ structure is_compl {α : Type u} [bounded_lattice α] (x : α) (y : α) where inf_le_bot : x ⊓ y ≤ ⊥ top_le_sup : ⊤ ≤ x ⊔ y namespace is_compl protected theorem disjoint {α : Type u} [bounded_lattice α] {x : α} {y : α} (h : is_compl x y) : disjoint x y := inf_le_bot h protected theorem symm {α : Type u} [bounded_lattice α] {x : α} {y : α} (h : is_compl x y) : is_compl y x := mk (eq.mpr (id (Eq._oldrec (Eq.refl (y ⊓ x ≤ ⊥)) inf_comm)) (inf_le_bot h)) (eq.mpr (id (Eq._oldrec (Eq.refl (⊤ ≤ y ⊔ x)) sup_comm)) (top_le_sup h)) theorem of_eq {α : Type u} [bounded_lattice α] {x : α} {y : α} (h₁ : x ⊓ y = ⊥) (h₂ : x ⊔ y = ⊤) : is_compl x y := mk (le_of_eq h₁) (le_of_eq (Eq.symm h₂)) theorem inf_eq_bot {α : Type u} [bounded_lattice α] {x : α} {y : α} (h : is_compl x y) : x ⊓ y = ⊥ := disjoint.eq_bot (is_compl.disjoint h) theorem sup_eq_top {α : Type u} [bounded_lattice α] {x : α} {y : α} (h : is_compl x y) : x ⊔ y = ⊤ := top_unique (top_le_sup h) theorem to_order_dual {α : Type u} [bounded_lattice α] {x : α} {y : α} (h : is_compl x y) : is_compl x y := mk (top_le_sup h) (inf_le_bot h) theorem le_left_iff {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (h : is_compl x y) : z ≤ x ↔ disjoint z y := { mp := fun (hz : z ≤ x) => disjoint.mono_left hz (is_compl.disjoint h), mpr := fun (hz : disjoint z y) => le_of_inf_le_sup_le (le_trans hz bot_le) (le_trans le_top (top_le_sup h)) } theorem le_right_iff {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (h : is_compl x y) : z ≤ y ↔ disjoint z x := le_left_iff (is_compl.symm h) theorem left_le_iff {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (h : is_compl x y) : x ≤ z ↔ ⊤ ≤ z ⊔ y := le_left_iff (to_order_dual h) theorem right_le_iff {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (h : is_compl x y) : y ≤ z ↔ ⊤ ≤ z ⊔ x := left_le_iff (is_compl.symm h) theorem antimono {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {x' : α} {y' : α} (h : is_compl x y) (h' : is_compl x' y') (hx : x ≤ x') : y' ≤ y := iff.mpr (right_le_iff h') (le_trans (top_le_sup (is_compl.symm h)) (sup_le_sup_left hx y)) theorem right_unique {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (hxy : is_compl x y) (hxz : is_compl x z) : y = z := le_antisymm (antimono hxz hxy (le_refl x)) (antimono hxy hxz (le_refl x)) theorem left_unique {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (hxz : is_compl x z) (hyz : is_compl y z) : x = y := right_unique (is_compl.symm hxz) (is_compl.symm hyz) theorem sup_inf {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {x' : α} {y' : α} (h : is_compl x y) (h' : is_compl x' y') : is_compl (x ⊔ x') (y ⊓ y') := sorry theorem inf_sup {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {x' : α} {y' : α} (h : is_compl x y) (h' : is_compl x' y') : is_compl (x ⊓ x') (y ⊔ y') := is_compl.symm (sup_inf (is_compl.symm h) (is_compl.symm h')) theorem inf_left_eq_bot_iff {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (h : is_compl y z) : x ⊓ y = ⊥ ↔ x ≤ z := inf_eq_bot_iff_le_compl (sup_eq_top h) (inf_eq_bot h) theorem inf_right_eq_bot_iff {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (h : is_compl y z) : x ⊓ z = ⊥ ↔ x ≤ y := inf_left_eq_bot_iff (is_compl.symm h) theorem disjoint_left_iff {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (h : is_compl y z) : disjoint x y ↔ x ≤ z := iff.trans disjoint_iff (inf_left_eq_bot_iff h) theorem disjoint_right_iff {α : Type u} [bounded_distrib_lattice α] {x : α} {y : α} {z : α} (h : is_compl y z) : disjoint x z ↔ x ≤ y := disjoint_left_iff (is_compl.symm h) end is_compl theorem is_compl_bot_top {α : Type u} [bounded_lattice α] : is_compl ⊥ ⊤ := is_compl.of_eq bot_inf_eq sup_top_eq theorem is_compl_top_bot {α : Type u} [bounded_lattice α] : is_compl ⊤ ⊥ := is_compl.of_eq inf_bot_eq top_sup_eq theorem bot_ne_top {α : Type u} [bounded_lattice α] [nontrivial α] : ⊥ ≠ ⊤ := fun (H : ⊥ = ⊤) => iff.mpr not_nontrivial_iff_subsingleton (subsingleton_of_bot_eq_top H) _inst_2 theorem top_ne_bot {α : Type u} [bounded_lattice α] [nontrivial α] : ⊤ ≠ ⊥ := ne.symm bot_ne_top namespace bool protected instance bounded_lattice : bounded_lattice Bool := bounded_lattice.mk lattice.sup lattice.le lattice.lt sorry sorry sorry sorry sorry sorry lattice.inf sorry sorry sorry tt sorry false sorry end bool @[simp] theorem top_eq_tt : ⊤ = tt := rfl @[simp] theorem bot_eq_ff : ⊥ = false := rfl end Mathlib
d0a7ed8a2b89162ca3a7c3d2fef794b7269896d1
ec5a7ae10c533e1b1f4b0bc7713e91ecf829a3eb
/ijcar16/examples/cc16.lean
29bdcc16b202adefe07d43dee1dca7fb62442495
[ "MIT" ]
permissive
leanprover/leanprover.github.io
cf248934af7c7e9aeff17cf8df3c12c5e7e73f1a
071a20d2e059a2c3733e004c681d3949cac3c07a
refs/heads/master
1,692,621,047,417
1,691,396,994,000
1,691,396,994,000
19,366,263
18
27
MIT
1,693,989,071,000
1,399,006,345,000
Lean
UTF-8
Lean
false
false
3,014
lean
/- Example/test file for the congruence closure procedure described in the paper: "Congruence Closure for Intensional Type Theory" Daniel Selsam and Leonardo de Moura The tactic `by blast` has been configured in this file to use just the congruence closure procedure using the command set_option blast.strategy "cc" -/ universes l1 l2 l3 l4 l5 l6 constants (A : Type.{l1}) (B : A → Type.{l2}) (C : ∀ (a : A) (ba : B a), Type.{l3}) (D : ∀ (a : A) (ba : B a) (cba : C a ba), Type.{l4}) (E : ∀ (a : A) (ba : B a) (cba : C a ba) (dcba : D a ba cba), Type.{l5}) (F : ∀ (a : A) (ba : B a) (cba : C a ba) (dcba : D a ba cba) (edcba : E a ba cba dcba), Type.{l6}) (C_ss : ∀ a ba, subsingleton (C a ba)) (a1 a2 a3 : A) (mk_B1 mk_B2 : ∀ a, B a) (mk_C1 mk_C2 : ∀ {a} ba, C a ba) (tr_B : ∀ {a}, B a → B a) (x y z : A → A) (f f' : ∀ {a : A} {ba : B a} (cba : C a ba), D a ba cba) (g : ∀ {a : A} {ba : B a} {cba : C a ba} (dcba : D a ba cba), E a ba cba dcba) (h : ∀ {a : A} {ba : B a} {cba : C a ba} {dcba : D a ba cba} (edcba : E a ba cba dcba), F a ba cba dcba edcba) attribute C_ss [instance] set_option blast.strategy "cc" example : ∀ {a a' : A}, a == a' → mk_B1 a == mk_B1 a' := by blast example : ∀ {a a' : A}, a == a' → mk_B2 a == mk_B2 a' := by blast example : a1 == y a2 → mk_B1 a1 == mk_B1 (y a2) := by blast example : a1 == x a2 → a2 == y a1 → mk_B1 (x (y a1)) == mk_B1 (x (y (x a2))) := by blast example : a1 == y a2 → mk_B1 a1 == mk_B2 (y a2) → f (mk_C1 (mk_B2 a1)) == f (mk_C2 (mk_B1 (y a2))) := by blast example : a1 == y a2 → tr_B (mk_B1 a1) == mk_B2 (y a2) → f (mk_C1 (mk_B2 a1)) == f (mk_C2 (tr_B (mk_B1 (y a2)))) := by blast example : a1 == y a2 → mk_B1 a1 == mk_B2 (y a2) → g (f (mk_C1 (mk_B2 a1))) == g (f (mk_C2 (mk_B1 (y a2)))) := by blast example : a1 == y a2 → tr_B (mk_B1 a1) == mk_B2 (y a2) → g (f (mk_C1 (mk_B2 a1))) == g (f (mk_C2 (tr_B (mk_B1 (y a2))))) := by blast example : a1 == y a2 → a2 == z a3 → a3 == x a1 → mk_B1 a1 == mk_B2 (y (z (x a1))) → f (mk_C1 (mk_B2 (y (z (x a1))))) == f' (mk_C2 (mk_B1 a1)) → g (f (mk_C1 (mk_B2 (y (z (x a1)))))) == g (f' (mk_C2 (mk_B1 a1))) := by blast example : a1 == y a2 → a2 == z a3 → a3 == x a1 → mk_B1 a1 == mk_B2 (y (z (x a1))) → f (mk_C1 (mk_B2 (y (z (x a1))))) == f' (mk_C2 (mk_B1 a1)) → f' (mk_C1 (mk_B1 a1)) == f (mk_C2 (mk_B2 (y (z (x a1))))) → g (f (mk_C1 (mk_B1 (y (z (x a1)))))) == g (f' (mk_C2 (mk_B2 a1))) := by blast example : a1 == y a2 → a2 == z a3 → a3 == x a1 → tr_B (mk_B1 a1) == mk_B2 (y (z (x a1))) → f (mk_C1 (mk_B2 (y (z (x a1))))) == f' (mk_C2 (tr_B (mk_B1 a1))) → f' (mk_C1 (tr_B (mk_B1 a1))) == f (mk_C2 (mk_B2 (y (z (x a1))))) → g (f (mk_C1 (tr_B (mk_B1 (y (z (x a1))))))) == g (f' (mk_C2 (mk_B2 a1))) := by blast
66e8c347e94fd1a0f9e0f1a9c5a3d8cde7b62356
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/group_theory/group_action/sub_mul_action.lean
6e3b4915f03960516ba6cfc4aff24f98b66634c2
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,114
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.group_theory.group_action.basic import Mathlib.algebra.group_action_hom import Mathlib.algebra.module.basic import Mathlib.PostPort universes u v l namespace Mathlib /-! # Sets invariant to a `mul_action` In this file we define `sub_mul_action R M`; a subset of a `mul_action M` which is closed with respect to scalar multiplication. For most uses, typically `submodule R M` is more powerful. ## Tags submodule, mul_action -/ /-- A sub_mul_action is a set which is closed under scalar multiplication. -/ structure sub_mul_action (R : Type u) (M : Type v) [has_scalar R M] where carrier : set M smul_mem' : ∀ (c : R) {x : M}, x ∈ carrier → c • x ∈ carrier namespace sub_mul_action protected instance set.has_coe_t {R : Type u} {M : Type v} [has_scalar R M] : has_coe_t (sub_mul_action R M) (set M) := has_coe_t.mk fun (s : sub_mul_action R M) => carrier s protected instance has_mem {R : Type u} {M : Type v} [has_scalar R M] : has_mem M (sub_mul_action R M) := has_mem.mk fun (x : M) (p : sub_mul_action R M) => x ∈ ↑p protected instance has_coe_to_sort {R : Type u} {M : Type v} [has_scalar R M] : has_coe_to_sort (sub_mul_action R M) := has_coe_to_sort.mk (Type (max 0 v)) fun (p : sub_mul_action R M) => Subtype fun (x : M) => x ∈ p protected instance has_top {R : Type u} {M : Type v} [has_scalar R M] : has_top (sub_mul_action R M) := has_top.mk (mk set.univ sorry) protected instance has_bot {R : Type u} {M : Type v} [has_scalar R M] : has_bot (sub_mul_action R M) := has_bot.mk (mk ∅ sorry) protected instance inhabited {R : Type u} {M : Type v} [has_scalar R M] : Inhabited (sub_mul_action R M) := { default := ⊥ } @[simp] theorem coe_sort_coe {R : Type u} {M : Type v} [has_scalar R M] (p : sub_mul_action R M) : ↥↑p = ↥p := rfl protected theorem exists {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} {q : ↥p → Prop} : (∃ (x : ↥p), q x) ↔ ∃ (x : M), ∃ (H : x ∈ p), q { val := x, property := H } := set_coe.exists protected theorem forall {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} {q : ↥p → Prop} : (∀ (x : ↥p), q x) ↔ ∀ (x : M) (H : x ∈ p), q { val := x, property := H } := set_coe.forall theorem coe_injective {R : Type u} {M : Type v} [has_scalar R M] : function.injective coe := sorry @[simp] theorem coe_set_eq {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} {q : sub_mul_action R M} : ↑p = ↑q ↔ p = q := function.injective.eq_iff coe_injective theorem ext'_iff {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} {q : sub_mul_action R M} : p = q ↔ ↑p = ↑q := iff.symm coe_set_eq theorem ext {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} {q : sub_mul_action R M} (h : ∀ (x : M), x ∈ p ↔ x ∈ q) : p = q := coe_injective (set.ext h) end sub_mul_action namespace sub_mul_action @[simp] theorem mem_coe {R : Type u} {M : Type v} [has_scalar R M] (p : sub_mul_action R M) {x : M} : x ∈ ↑p ↔ x ∈ p := iff.rfl theorem smul_mem {R : Type u} {M : Type v} [has_scalar R M] (p : sub_mul_action R M) {x : M} (r : R) (h : x ∈ p) : r • x ∈ p := smul_mem' p r h protected instance has_scalar {R : Type u} {M : Type v} [has_scalar R M] (p : sub_mul_action R M) : has_scalar R ↥p := has_scalar.mk fun (c : R) (x : ↥p) => { val := c • subtype.val x, property := sorry } @[simp] theorem coe_eq_coe {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} {x : ↥p} {y : ↥p} : ↑x = ↑y ↔ x = y := iff.symm subtype.ext_iff_val @[simp] theorem coe_smul {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} (r : R) (x : ↥p) : ↑(r • x) = r • ↑x := rfl @[simp] theorem coe_mk {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} (x : M) (hx : x ∈ p) : ↑{ val := x, property := hx } = x := rfl @[simp] theorem coe_mem {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} (x : ↥p) : ↑x ∈ p := subtype.property x @[simp] protected theorem eta {R : Type u} {M : Type v} [has_scalar R M] {p : sub_mul_action R M} (x : ↥p) (hx : ↑x ∈ p) : { val := ↑x, property := hx } = x := subtype.eta x hx /-- Embedding of a submodule `p` to the ambient space `M`. -/ protected def subtype {R : Type u} {M : Type v} [has_scalar R M] (p : sub_mul_action R M) : mul_action_hom R (↥p) M := mul_action_hom.mk coe sorry @[simp] theorem subtype_apply {R : Type u} {M : Type v} [has_scalar R M] (p : sub_mul_action R M) (x : ↥p) : coe_fn (sub_mul_action.subtype p) x = ↑x := rfl theorem subtype_eq_val {R : Type u} {M : Type v} [has_scalar R M] (p : sub_mul_action R M) : ⇑(sub_mul_action.subtype p) = subtype.val := rfl @[simp] theorem smul_mem_iff' {R : Type u} {M : Type v} [monoid R] [mul_action R M] (p : sub_mul_action R M) {x : M} (u : units R) : ↑u • x ∈ p ↔ x ∈ p := sorry /-- If the scalar product forms a `mul_action`, then the subset inherits this action -/ protected instance mul_action {R : Type u} {M : Type v} [monoid R] [mul_action R M] (p : sub_mul_action R M) : mul_action R ↥p := mul_action.mk sorry sorry theorem zero_mem {R : Type u} {M : Type v} [semiring R] [add_comm_monoid M] [semimodule R M] (p : sub_mul_action R M) (h : set.nonempty ↑p) : 0 ∈ p := sorry /-- If the scalar product forms a `semimodule`, and the `sub_mul_action` is not `⊥`, then the subset inherits the zero. -/ protected instance has_zero {R : Type u} {M : Type v} [semiring R] [add_comm_monoid M] [semimodule R M] (p : sub_mul_action R M) [n_empty : Nonempty ↥p] : HasZero ↥p := { zero := { val := 0, property := sorry } } theorem neg_mem {R : Type u} {M : Type v} [ring R] [add_comm_group M] [semimodule R M] (p : sub_mul_action R M) {x : M} (hx : x ∈ p) : -x ∈ p := eq.mpr (id (Eq._oldrec (Eq.refl (-x ∈ p)) (Eq.symm (neg_one_smul R x)))) (smul_mem p (-1) hx) @[simp] theorem neg_mem_iff {R : Type u} {M : Type v} [ring R] [add_comm_group M] [semimodule R M] (p : sub_mul_action R M) {x : M} : -x ∈ p ↔ x ∈ p := { mp := fun (h : -x ∈ p) => eq.mpr (id (Eq._oldrec (Eq.refl (x ∈ p)) (Eq.symm (neg_neg x)))) (neg_mem p h), mpr := neg_mem p } protected instance has_neg {R : Type u} {M : Type v} [ring R] [add_comm_group M] [semimodule R M] (p : sub_mul_action R M) : Neg ↥p := { neg := fun (x : ↥p) => { val := -subtype.val x, property := sorry } } @[simp] theorem coe_neg {R : Type u} {M : Type v} [ring R] [add_comm_group M] [semimodule R M] (p : sub_mul_action R M) (x : ↥p) : ↑(-x) = -↑x := rfl end sub_mul_action namespace sub_mul_action theorem smul_mem_iff {R : Type u} {M : Type v} [division_ring R] [add_comm_group M] [module R M] (p : sub_mul_action R M) {r : R} {x : M} (r0 : r ≠ 0) : r • x ∈ p ↔ x ∈ p := smul_mem_iff' p (units.mk0 r r0)
f8fd2a6f3d9e34b9973262951147349398f6f247
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/normed_space/dual.lean
0b67550b84eb256e26c9ddab3f0f8bcf4ab73adf
[]
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,639
lean
/- Copyright (c) 2020 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth, Frédéric Dupuis -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.normed_space.hahn_banach import Mathlib.analysis.normed_space.inner_product import Mathlib.PostPort universes u_1 u_2 u v namespace Mathlib /-! # The topological dual of a normed space In this file we define the topological dual of a normed space, and the bounded linear map from a normed space into its double dual. We also prove that, for base field `𝕜` with `[is_R_or_C 𝕜]`, this map is an isometry. We then consider inner product spaces, with base field over `ℝ` (the corresponding results for `ℂ` will require the definition of conjugate-linear maps). We define `to_dual_map`, a continuous linear map from `E` to its dual, which maps an element `x` of the space to `λ y, ⟪x, y⟫`. We check (`to_dual_map_isometry`) that this map is an isometry onto its image, and particular is injective. We also define `to_dual'` as the function taking taking a vector to its dual for a base field `𝕜` with `[is_R_or_C 𝕜]`; this is a function and not a linear map. Finally, under the hypothesis of completeness (i.e., for Hilbert spaces), we prove the Fréchet-Riesz representation (`to_dual_map_eq_top`), which states the surjectivity: every element of the dual of a Hilbert space `E` has the form `λ u, ⟪x, u⟫` for some `x : E`. This permits the map `to_dual_map` to be upgraded to an (isometric) continuous linear equivalence, `to_dual`, between a Hilbert space and its dual. ## References * [M. Einsiedler and T. Ward, *Functional Analysis, Spectral Theory, and Applications*] [EinsiedlerWard2017] ## Tags dual, Fréchet-Riesz -/ namespace normed_space /-- The topological dual of a normed space `E`. -/ def dual (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] := continuous_linear_map 𝕜 E 𝕜 protected instance dual.inhabited (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] : Inhabited (dual 𝕜 E) := { default := 0 } /-- The inclusion of a normed space in its double (topological) dual. -/ def inclusion_in_double_dual' (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] (x : E) : dual 𝕜 (dual 𝕜 E) := linear_map.mk_continuous (linear_map.mk (fun (f : dual 𝕜 E) => coe_fn f x) sorry sorry) (norm x) sorry @[simp] theorem dual_def (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] (x : E) (f : dual 𝕜 E) : coe_fn (inclusion_in_double_dual' 𝕜 E x) f = coe_fn f x := rfl theorem double_dual_bound (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] (x : E) : norm (inclusion_in_double_dual' 𝕜 E x) ≤ norm x := sorry /-- The inclusion of a normed space in its double (topological) dual, considered as a bounded linear map. -/ def inclusion_in_double_dual (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] (E : Type u_2) [normed_group E] [normed_space 𝕜 E] : continuous_linear_map 𝕜 E (dual 𝕜 (dual 𝕜 E)) := linear_map.mk_continuous (linear_map.mk (fun (x : E) => inclusion_in_double_dual' 𝕜 E x) sorry sorry) 1 sorry /-- If one controls the norm of every `f x`, then one controls the norm of `x`. Compare `continuous_linear_map.op_norm_le_bound`. -/ theorem norm_le_dual_bound {𝕜 : Type v} [is_R_or_C 𝕜] {E : Type u} [normed_group E] [normed_space 𝕜 E] (x : E) {M : ℝ} (hMp : 0 ≤ M) (hM : ∀ (f : dual 𝕜 E), norm (coe_fn f x) ≤ M * norm f) : norm x ≤ M := sorry /-- The inclusion of a normed space in its double dual is an isometry onto its image.-/ theorem inclusion_in_double_dual_isometry {𝕜 : Type v} [is_R_or_C 𝕜] {E : Type u} [normed_group E] [normed_space 𝕜 E] (x : E) : norm (coe_fn (inclusion_in_double_dual 𝕜 E) x) = norm x := sorry end normed_space namespace inner_product_space /-- Given some `x` in an inner product space, we can define its dual as the continuous linear map `λ y, ⟪x, y⟫`. Consider using `to_dual` or `to_dual_map` instead in the real case. -/ def to_dual' (𝕜 : Type u_1) {E : Type u_2} [is_R_or_C 𝕜] [inner_product_space 𝕜 E] : E →+ normed_space.dual 𝕜 E := add_monoid_hom.mk (fun (x : E) => linear_map.mk_continuous (linear_map.mk (fun (y : E) => inner x y) sorry sorry) (norm x) sorry) sorry sorry @[simp] theorem to_dual'_apply (𝕜 : Type u_1) {E : Type u_2} [is_R_or_C 𝕜] [inner_product_space 𝕜 E] {x : E} {y : E} : coe_fn (coe_fn (to_dual' 𝕜) x) y = inner x y := rfl /-- In an inner product space, the norm of the dual of a vector `x` is `∥x∥` -/ @[simp] theorem norm_to_dual'_apply (𝕜 : Type u_1) {E : Type u_2} [is_R_or_C 𝕜] [inner_product_space 𝕜 E] (x : E) : norm (coe_fn (to_dual' 𝕜) x) = norm x := sorry theorem to_dual'_isometry (𝕜 : Type u_1) (E : Type u_2) [is_R_or_C 𝕜] [inner_product_space 𝕜 E] : isometry ⇑(to_dual' 𝕜) := add_monoid_hom.isometry_of_norm (to_dual' 𝕜) (norm_to_dual'_apply 𝕜) /-- Fréchet-Riesz representation: any `ℓ` in the dual of a Hilbert space `E` is of the form `λ u, ⟪y, u⟫` for some `y : E`, i.e. `to_dual'` is surjective. -/ theorem to_dual'_surjective (𝕜 : Type u_1) (E : Type u_2) [is_R_or_C 𝕜] [inner_product_space 𝕜 E] [complete_space E] : function.surjective ⇑(to_dual' 𝕜) := sorry /-- In a real inner product space `F`, the function that takes a vector `x` in `F` to its dual `λ y, ⟪x, y⟫` is a continuous linear map. If the space is complete (i.e. is a Hilbert space), consider using `to_dual` instead. -/ -- TODO extend to `is_R_or_C` (requires a definition of conjugate linear maps) def to_dual_map {F : Type u_1} [inner_product_space ℝ F] : continuous_linear_map ℝ F (normed_space.dual ℝ F) := linear_map.mk_continuous (linear_map.mk ⇑(to_dual' ℝ) sorry sorry) 1 sorry @[simp] theorem to_dual_map_apply {F : Type u_1} [inner_product_space ℝ F] {x : F} {y : F} : coe_fn (coe_fn to_dual_map x) y = inner x y := rfl /-- In an inner product space, the norm of the dual of a vector `x` is `∥x∥` -/ @[simp] theorem norm_to_dual_map_apply {F : Type u_1} [inner_product_space ℝ F] (x : F) : norm (coe_fn to_dual_map x) = norm x := norm_to_dual'_apply ℝ x theorem to_dual_map_isometry {F : Type u_1} [inner_product_space ℝ F] : isometry ⇑to_dual_map := add_monoid_hom.isometry_of_norm (to_dual' ℝ) norm_to_dual_map_apply theorem to_dual_map_injective {F : Type u_1} [inner_product_space ℝ F] : function.injective ⇑to_dual_map := isometry.injective to_dual_map_isometry @[simp] theorem ker_to_dual_map {F : Type u_1} [inner_product_space ℝ F] : continuous_linear_map.ker to_dual_map = ⊥ := iff.mpr linear_map.ker_eq_bot to_dual_map_injective @[simp] theorem to_dual_map_eq_iff_eq {F : Type u_1} [inner_product_space ℝ F] {x : F} {y : F} : coe_fn to_dual_map x = coe_fn to_dual_map y ↔ x = y := function.injective.eq_iff (iff.mp linear_map.ker_eq_bot ker_to_dual_map) /-- Fréchet-Riesz representation: any `ℓ` in the dual of a real Hilbert space `F` is of the form `λ u, ⟪y, u⟫` for some `y` in `F`. See `inner_product_space.to_dual` for the continuous linear equivalence thus induced. -/ -- TODO extend to `is_R_or_C` (requires a definition of conjugate linear maps) theorem range_to_dual_map {F : Type u_1} [inner_product_space ℝ F] [complete_space F] : continuous_linear_map.range to_dual_map = ⊤ := iff.mpr linear_map.range_eq_top (to_dual'_surjective ℝ F) /-- Fréchet-Riesz representation: If `F` is a Hilbert space, the function that takes a vector in `F` to its dual is a continuous linear equivalence. -/ def to_dual {F : Type u_1} [inner_product_space ℝ F] [complete_space F] : continuous_linear_equiv ℝ F (normed_space.dual ℝ F) := continuous_linear_equiv.of_isometry (continuous_linear_map.to_linear_map to_dual_map) to_dual_map_isometry range_to_dual_map /-- Fréchet-Riesz representation: If `F` is a Hilbert space, the function that takes a vector in `F` to its dual is an isometry. -/ def isometric.to_dual {F : Type u_1} [inner_product_space ℝ F] [complete_space F] : F ≃ᵢ normed_space.dual ℝ F := isometric.mk (linear_equiv.to_equiv (continuous_linear_equiv.to_linear_equiv to_dual)) (to_dual'_isometry ℝ F) @[simp] theorem to_dual_apply {F : Type u_1} [inner_product_space ℝ F] [complete_space F] {x : F} {y : F} : coe_fn (coe_fn to_dual x) y = inner x y := rfl @[simp] theorem to_dual_eq_iff_eq {F : Type u_1} [inner_product_space ℝ F] [complete_space F] {x : F} {y : F} : coe_fn to_dual x = coe_fn to_dual y ↔ x = y := function.injective.eq_iff (continuous_linear_equiv.injective to_dual) theorem to_dual_eq_iff_eq' {F : Type u_1} [inner_product_space ℝ F] [complete_space F] {x : F} {x' : F} : (∀ (y : F), inner x y = inner x' y) ↔ x = x' := sorry @[simp] theorem norm_to_dual_apply {F : Type u_1} [inner_product_space ℝ F] [complete_space F] (x : F) : norm (coe_fn to_dual x) = norm x := norm_to_dual_map_apply x /-- In a Hilbert space, the norm of a vector in the dual space is the norm of its corresponding primal vector. -/ theorem norm_to_dual_symm_apply {F : Type u_1} [inner_product_space ℝ F] [complete_space F] (ℓ : normed_space.dual ℝ F) : norm (coe_fn (continuous_linear_equiv.symm to_dual) ℓ) = norm ℓ := sorry
2c4046788beb0aa898f6eebd368a9ca6f636aa4c
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/univ_bug2.lean
8b3be7671ffcae2ddee4ab504d69386bda11afdd
[ "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
1,255
lean
---------------------------------------------------------------------------------------------------- --- Copyright (c) 2014 Microsoft Corporation. All rights reserved. --- Released under Apache 2.0 license as described in the file LICENSE. --- Author: Jeremy Avigad ---------------------------------------------------------------------------------------------------- import logic data.nat open nat -- first define a class of homogeneous equality inductive simplifies_to [class] {T : Type} (t1 t2 : T) : Prop := mk : t1 = t2 → simplifies_to t1 t2 namespace simplifies_to theorem get_eq {T : Type} {t1 t2 : T} (C : simplifies_to t1 t2) : t1 = t2 := simplifies_to.rec (λx, x) C theorem infer_eq {T : Type} (t1 t2 : T) [C : simplifies_to t1 t2] : t1 = t2 := simplifies_to.rec (λx, x) C theorem simp_app [instance] (S : Type) (T : Type) (f1 f2 : S → T) (s1 s2 : S) [C1 : simplifies_to f1 f2] [C2 : simplifies_to s1 s2] : simplifies_to (f1 s1) (f2 s2) := mk (congr (get_eq C1) (get_eq C2)) theorem test1 (S : Type) (T : Type) (f1 f2 : S → T) (s1 s2 : S) (Hf : f1 = f2) (Hs : s1 = s2) : f1 s1 = f2 s2 := have Rs [visible] : simplifies_to f1 f2, from mk Hf, have Cs [visible] : simplifies_to s1 s2, from mk Hs, infer_eq _ _ end simplifies_to
34a75208719df584cbcd9734cdbd601472d2820b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/macroSwizzle.lean
1f27eaae3b75eb41d108b814931f74854aab1e59
[ "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
125
lean
notation "swizzle" a:max b => b + a -- should *not* produce a negative span #check swizzle "s" true #check "x" |> Nat.succ
fcc797da81e85fb0b5e620e32582acc1367fe34b
40c8b73f5a82a3c09e3d1956df44aad9ffd510b7
/src/section_13b.lean
28475e6d21d309d5b8898bc307b73aab708be560
[]
no_license
lean-forward/cap_set_problem
343c02d42775eb25b32b6c567d13769fd42dd4ca
095a2f18f81c551a0053f2e65806de751e438fc4
refs/heads/master
1,626,463,115,144
1,561,829,195,000
1,561,829,195,000
178,678,372
7
1
null
1,625,412,745,000
1,554,031,306,000
Lean
UTF-8
Lean
false
false
33,366
lean
/- Copyright (c) 2018 Sander Dahmen, Johannes Hölzl, Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sander Dahmen, Johannes Hölzl, Robert Y. Lewis "On large subsets of 𝔽ⁿ_q with no three-term arithmetic progression" by J. S. Ellenberg and D. Gijswijt This file establishes the asymptotic behavior of the cap set cardinality bound proved in section_10_11_12.lean. It corresponds to the second half of section 13 of our blueprint. -/ import data.vector2 data.zmod.basic data.nat.modeq import tactic.library_search import section_10_11_12 has_deriv def iseg (q j) : finset (fin q) := finset.univ.filter (λ k, k.val ≤ j) lemma val_le_right_of_iseg {q j} {v : fin q} (h : v ∈ iseg q j) : v.val ≤ j := (finset.mem_filter.1 h).2 lemma mem_iseg_of_le {q j} {v : fin q} (h : v.val ≤ j) : v ∈ iseg q j := by simp [iseg, h] section fix_q parameter (q : ℕ) section cf_def parameter (n : ℕ) def sf (j : ℕ) : finset (vector (fin q) n) := finset.univ.filter (λ f, (f.nat_sum = j)) parameter {n} lemma mem_sf {j} {v : vector (fin q) n} (h : v.nat_sum = j) : v ∈ sf j := finset.mem_filter.2 ⟨finset.mem_univ _, h⟩ lemma sf_sum {j : ℕ} {f : vector (fin q) n} (h : f ∈ sf j) : f.nat_sum = j := (finset.mem_filter.1 h).2 lemma sf_disjoint {j1 j2 : ℕ} (h : j1 ≠ j2) : sf j1 ∩ sf j2 = ∅ := finset.eq_empty_of_forall_not_mem $ λ x hx, h $ by simp at hx; rw [←sf_sum hx.1, ←sf_sum hx.2] def sf_lift_i (i : fin q) (sfj : finset (vector (fin q) n)) : finset (vector (fin q) (n+1)) := sfj.image $ vector.cons i lemma sf_lift_i_sum {i : fin q} {j} {f} (hf : f ∈ sf_lift_i i (sf j)) : f.nat_sum = i.val + j := by rcases finset.mem_image.1 hf with ⟨_, hg, hfg⟩; simp [hfg.symm, sf_sum hg] lemma sf_lift_i_card (i : fin q) (sfj : finset (vector (fin q) n)) : (sf_lift_i i sfj).card = sfj.card := finset.card_image_of_inj_on $ λ _ _ _ _, vector.cons_inj lemma sf_lift_i_inter_empty {i j : fin q} (h : i ≠ j) (s1 s2 : finset (vector (fin q) n)) : sf_lift_i i s1 ∩ sf_lift_i j s2 = ∅ := finset.eq_empty_of_forall_not_mem $ λ x hx, let ⟨hx1, hx2⟩ := finset.mem_inter.1 hx, ⟨a1, hm1, ha1⟩ := finset.mem_image.1 hx1, ⟨a2, hm2, ha2⟩ := finset.mem_image.1 hx2 in h $ vector.cons_inj_left $ by rwa ←ha2 at ha1 lemma mem_sf_lift_i (v) : v ∈ sf_lift_i (vector.head v) (sf (v.nat_sum - (vector.head v).val)) := finset.mem_image.2 $ ⟨v.tail, mem_sf $ eq.symm $ nat.sub_eq_of_eq_add $ by rw [←vector.nat_sum_head_tail], by simp⟩ parameter (n) def sf_lift (j : ℕ) : finset (vector (fin q) (n+1)) := finset.bind (iseg q j) (λ i, sf_lift_i i (sf (j - i))) def cf (j : ℕ) : ℕ := (sf j).card parameter {n} lemma sf_sum_mem_lift {j : ℕ} {f : vector (fin q) (n+1)} (h : f ∈ sf_lift j) : f.nat_sum = j := begin simp [sf_lift, sf_lift_i] at h, rcases h with ⟨k, ⟨hk, g, hg, hfg⟩⟩, suffices : k.val + (j - k.val) = j, {simpa [hfg.symm, sf_sum hg]}, rw [nat.add_comm, nat.sub_add_cancel], apply val_le_right_of_iseg hk end end cf_def lemma head_le_of_mem_sf {j n : ℕ} : ∀ {v : vector (fin q) (n+1)} (h : v ∈ sf (n+1) j), v.head.val ≤ j | p@⟨h::t,_⟩ hmem := have hsum : _, from vector.nat_sum_head_tail p, by rw sf_sum hmem at hsum; linarith lemma sf_lift_succ (n j : ℕ) : sf (n + 1) j = sf_lift n j := begin ext v, constructor, { intro hv, apply finset.mem_bind.2, use [v.head, mem_iseg_of_le (head_le_of_mem_sf hv)], rw ←sf_sum hv, apply mem_sf_lift_i }, { intro hv, simp [sf_sum_mem_lift hv, sf] } end lemma sf_lift_card' (n j : ℕ) : (sf_lift n j).card = (iseg q j).sum (λ u, finset.card ((λ i, sf_lift_i i (sf n (j - ↑i))) u)) := finset.card_bind $ λ x hx y hy hxy, sf_lift_i_inter_empty hxy _ _ lemma sf_lift_card (n j : ℕ) : (sf_lift n j).card = (iseg q j).sum (λ u, cf n (j - u)) := by simp only [sf_lift_card', sf_lift_i_card, cf] lemma cf_zero_zero (h : q > 0) : cf 0 0 = 1 := finset.card_eq_one.2 ⟨ vector.nil, finset.ext.2 $ λ v, ⟨λ _, finset.mem_singleton.2 v.eq_nil, λ hv, mem_sf (by rw finset.mem_singleton.1 hv; simp)⟩ ⟩ lemma cf_zero_succ (j : ℕ) : cf 0 (j + 1) = 0 := finset.card_eq_zero.2 $ finset.eq_empty_of_forall_not_mem $ λ v hv, nat.succ_ne_zero j $ by simpa using sf_sum hv lemma cf_one_lt {j : ℕ} (hj : j < q) : cf 1 j = 1 := begin have hfs : finset.univ.filter (λ f : vector (fin q) 1, f.nat_sum = j) = {⟨j, hj⟩::vector.nil}, { ext v, simp, constructor, { rw vector.vec_one v, intro h, congr, rw fin.ext_iff, simpa using h }, { intro hv, simp [hv] } }, simp only [cf, sf, hfs], refl end lemma cf_one_ge {j : ℕ} (hj : j ≥ q) : cf 1 j = 0 := begin simp only [cf, sf], convert finset.card_empty, apply finset.eq_empty_of_forall_not_mem, intros x hx, cases finset.mem_filter.1 hx with _ hx, apply not_lt_of_ge hj, rw ←hx, apply vector.nat_sum_one end lemma cf_succ (n j : ℕ) : cf (n + 1) j = (iseg q j).sum (λ u, cf n (j - u)) := by rw [cf, sf_lift_succ, sf_lift_card] lemma cf_mul (n j : ℕ) : (finset.range (j + 1)).sum (λ i, (cf 1 (j - i)) * cf (n + 1) i) = cf (n+2) j := have hfil : finset.range (j + 1) = (finset.range (j + 1)).filter (λ k, j - k < q) ∪ (finset.range _).filter _, from eq.symm (finset.filter_union_filter_neg_eq _), begin rw [cf_succ, hfil, finset.sum_union], { have hgeq : ((finset.range (j + 1)).filter (λ k, ¬ j - k < q)).sum (λ i, cf 1 (j - i) * cf (n + 1) i) = 0, { apply finset.sum_eq_zero, intros k hk, simp [cf_one_ge (le_of_not_gt (finset.mem_filter.1 hk).2)] }, rw [hgeq, add_zero], symmetry, apply finset.sum_bij (λ (a : fin q) _, j - a.val), { intros a ha, simp, refine ⟨nat.sub_lt_succ _ _, _⟩, rw nat.sub_sub_self (val_le_right_of_iseg ha), exact a.is_lt }, { intros a ha, simp [nat.sub_sub_self (val_le_right_of_iseg ha), cf_one_lt a.is_lt], refl }, { intros a a2 ha1 ha2 heq, apply (fin.ext_iff _ _).2, rwa ← nat.sub_eq_sub_iff; apply val_le_right_of_iseg; assumption }, { intros b hb, simp at hb, use [⟨j-b, hb.2⟩, mem_iseg_of_le (nat.sub_le_self _ _)], simp [nat.sub_sub_self (nat.le_of_lt_succ hb.1)] } }, { rw [finset.filter_not], simp } end def one_coeff_poly : polynomial ℕ := (finset.range q).sum (λ k, (polynomial.X : polynomial ℕ) ^ k) lemma one_coeff_poly_coeff {n : ℕ} : one_coeff_poly.coeff n = if n < q then 1 else 0 := begin rw [one_coeff_poly], symmetry, have hlx : (λ k : ℕ, ((polynomial.X : polynomial ℕ) ^k).coeff n) = (λ k : ℕ, if n = k then 1 else 0), { ext k, rw [polynomial.coeff_X_pow k n] }, have hfr : finset.range q = (finset.range q).filter (λ k, n = k) ∪ (finset.range q).filter (λ k, n ≠ k), from eq.symm (finset.filter_union_filter_neg_eq _), have hoeq : (finset.range q).sum (λ k, ((polynomial.X : polynomial ℕ)^k).coeff n) = if n < q then 1 else 0, { rw [hlx, hfr, finset.sum_union, ←add_zero (ite _ _ _)], { congr, { by_cases hnq : n < q, { have hs : (finset.range q).filter (λ k, n = k) = finset.singleton n, { ext a, simp, constructor, { rintros ⟨_, ha⟩, rw ha }, { intro ha, rw ha, exact ⟨hnq, rfl⟩ } }, simp [hs, hnq] }, { simp [hnq], apply finset.sum_eq_zero, intros x hx, exfalso, apply hnq, rw (finset.mem_filter.1 hx).2, apply finset.mem_range.1 (finset.mem_filter.1 hx).1 } }, { apply finset.sum_eq_zero, intros _ hx, simp [(finset.mem_filter.1 hx).2] } }, { apply finset.eq_empty_of_forall_not_mem, intros x hx, cases finset.mem_inter.1 hx with hx1 hx2, cases finset.mem_filter.1 hx1 with _ hxn, cases finset.mem_filter.1 hx2 with _ hxn2, exact hxn2 hxn }}, rw ←hoeq, apply finset.sum_hom (λ p, polynomial.coeff p n), apply_instance, convert is_add_monoid_hom.mk _, { convert is_add_hom.mk _, intros, apply polynomial.coeff_add }, { apply polynomial.coeff_zero } end lemma one_coeff_poly_coeff_lt {n : ℕ} (h : n < q): one_coeff_poly.coeff n = 1 := have he : _ := @one_coeff_poly_coeff n, have 1 = (if n < q then 1 else 0), by split_ifs; refl, by rw this; exact he lemma one_coeff_poly_coeff_ge {n : ℕ} (h : n ≥ q): one_coeff_poly.coeff n = 0 := have he : _ := @one_coeff_poly_coeff n, have 0 = (if n < q then 1 else 0), by simp [not_lt_of_ge h], by rw this; exact he lemma one_coeff_poly_eval₂_nonneg {β} [linear_ordered_semiring β] {f : ℕ → β} (hf : ∀ z, f z ≥ 0) {b : β} (hb : b ≥ 0) : 0 ≤ one_coeff_poly.eval₂ f b := finset.zero_le_sum $ λ x _, mul_nonneg (hf _) (pow_nonneg hb _) theorem lemma_13_9 (hq : q > 0) : ∀ n j : ℕ, (one_coeff_poly ^ n).coeff j = cf n j | 0 0 := by rw [pow_zero, polynomial.coeff_one, cf_zero_zero]; [refl, assumption] | 0 (j+1) := by rw [pow_zero, polynomial.coeff_one, cf_zero_succ]; simp [show 0 ≠ j + 1, from (nat.succ_ne_zero _).symm] | 1 j := begin by_cases h : j < q, { rw [pow_one, cf_one_lt h, one_coeff_poly_coeff_lt h] }, { rw [pow_one, cf_one_ge (le_of_not_gt h), one_coeff_poly_coeff_ge (le_of_not_gt h)] } end | (n+2) j := calc (one_coeff_poly ^ (n + 2)).coeff j = (one_coeff_poly * one_coeff_poly ^ (n + 1)).coeff j : rfl ... = _ : polynomial.coeff_mul_right _ _ _ ... = (finset.range (j + 1)).sum (λ i, one_coeff_poly.coeff (j - i) * cf (n + 1) i) : by congr; ext; rw lemma_13_9 ... = (finset.range (j + 1)).sum (λ i, (one_coeff_poly ^ 1).coeff (j - i) * cf (n + 1) i) : by rw pow_one ... = (finset.range (j + 1)).sum (λ i, cf 1 (j - i) * cf (n + 1) i) : by congr; ext i; rw lemma_13_9 ... = cf (n + 2) j : cf_mul _ _ parameter {q} def vector_of_fin_fn : ∀ {n} (v : fin n → fin q), vector (fin q) n | 0 v := vector.nil | (n+1) v := v ⟨n, nat.lt_succ_self _⟩ :: vector_of_fin_fn (λ k : fin n, v ⟨k, nat.lt_succ_of_lt k.is_lt⟩) lemma vector_of_fin_fn_nth : ∀ n (v : fin (n+1) → fin q) (i : fin (n+1)), (vector_of_fin_fn v).nth i = v ⟨n - i.val, nat.lt_succ_of_le (nat.sub_le_self _ _)⟩ | 0 v x := have hx : x = ⟨0, zero_lt_one⟩, from (fin.ext_iff _ _).2 (nat.eq_zero_of_le_zero (nat.le_of_lt_succ x.is_lt)), by simp [vector_of_fin_fn, hx]; refl | (n+1) v x := begin rw [vector_of_fin_fn], by_cases hx : x.val = 0, { have hx' : x = 0, by simp [fin.ext_iff, hx, fin.val_zero], simp [hx', vector.nth_zero, fin.val_zero] }, { have : x = fin.succ ⟨x.val - 1, _⟩, { apply fin.eq_of_veq, rw fin.succ_val, symmetry, apply nat.sub_add_cancel (nat.succ_le_of_lt (nat.pos_of_ne_zero hx)) }, { rw [this, vector.nth_cons_succ, vector_of_fin_fn_nth], congr, simp, refl }, { rw nat.sub_lt_right_iff_lt_add, apply x.is_lt, exact nat.succ_le_of_lt (nat.pos_of_ne_zero hx) }} end def fin_fn_of_vector : ∀ {n} (v : vector (fin q) n), fin n → fin q | 0 v i := fin_zero_elim i | (n+1) v i := v.nth ⟨n - i.val, nat.lt_succ_of_le (nat.sub_le_self _ _)⟩ lemma vector_of_fin_fn_inv : ∀ {n} (v : vector (fin q) n), vector_of_fin_fn (fin_fn_of_vector v) = v | 0 v := vector.ext $ λ k, fin_zero_elim k | (k+1) v := begin ext m, simp only [fin_fn_of_vector, vector_of_fin_fn_nth], congr, rw fin.ext_iff, apply nat.sub_sub_self, exact nat.le_of_lt_succ m.is_lt end lemma sum_fin_fn_of_vector : ∀ {n} (v : vector (fin q) n), fin.sum (↑(fin_fn_of_vector v) : fin n → ℕ) = v.nat_sum | 0 v := by simp [fin.sum_zero] | (k+1) v := begin rw [fin.sum_succ, vector.nat_sum_head_tail], congr, { simp [cast_fin_fn, fin_fn_of_vector], congr, apply vector.nth_zero }, { convert sum_fin_fn_of_vector _, ext x, simp [cast_fin_fn, fin_fn_of_vector], cases k, { apply fin_zero_elim x }, { rw [fin_fn_of_vector, vector.nth_tail], congr, rw nat.succ_sub; [refl, exact nat.le_of_lt_succ x.is_lt] } } end lemma inv_in_range {d : ℚ} (hd : 0 ≤ d) {n} {v : vector (fin q) n} (hv : v ∈ (finset.range (int.nat_abs ⌊d⌋ + 1)).bind (sf n)) : ↑(fin.sum (↑(fin_fn_of_vector v) : fin n → ℕ)) ≤ d := begin rw sum_fin_fn_of_vector, simp [-add_comm] at hv, rcases hv with ⟨a, ha, ha2⟩, replace ha2 := sf_sum ha2, have : 0 ≤ ⌊d⌋ := le_floor.2 hd, calc (vector.nat_sum v : ℚ) ≤ int.nat_abs ⌊d⌋ : by rw ha2; exact nat.cast_le.2 (nat.succ_le_succ_iff.1 ha) ... = ((int.nat_abs ⌊d⌋ : ℤ) : ℚ) : by rw [← coe_coe] ... = _ : by rw [← int.eq_nat_abs_of_zero_le this] ... ≤ d : floor_le _ end lemma nat_sum_vector_of_fin_fn : ∀ {n} (v : fin n → fin q), (vector_of_fin_fn v).nat_sum = fin.sum (↑v : fin n → ℕ) | 0 v := by simp [vector_of_fin_fn, fin.sum_zero] | (n+1) v := by simp [vector_of_fin_fn, nat_sum_vector_of_fin_fn, fin.sum_succ]; refl lemma vector_of_fin_fn_mem_bind {n} {d : ℚ} {v : fin n → fin q} (hv : ↑(fin.sum (↑v : fin n → ℕ)) ≤ d) : vector_of_fin_fn v ∈ (finset.bind (finset.range (int.nat_abs ⌊d⌋ + 1)) (sf n)) := begin apply finset.mem_bind.2, use fin.sum (↑v : fin n → ℕ), split, { apply finset.mem_range.2, refine (nat.cast_lt.1 $ lt_of_le_of_lt hv _), rw [nat.cast_add, nat.cast_one], have hd : 0 ≤ ⌊d⌋ := le_floor.2 (le_trans (nat.cast_le.2 (nat.zero_le _)) hv), have : (int.nat_abs ⌊d⌋ : ℚ) = ((int.nat_abs ⌊d⌋ : ℤ) : ℚ), { rw ← coe_coe }, rw [this, ← int.eq_nat_abs_of_zero_le hd], exact lt_floor_add_one d }, { apply mem_sf, apply nat_sum_vector_of_fin_fn } end lemma vector_of_fin_fn_inj : ∀ {n} {v1 v2 : fin n → fin q}, vector_of_fin_fn v1 = vector_of_fin_fn v2 → v1 = v2 | 0 v1 v2 h := funext $ λ x, fin_zero_elim x | (n+1) v1 v2 h := funext $ λ x, have (λ k : fin n, v1 ⟨k, nat.lt_succ_of_lt k.is_lt⟩) = (λ k : fin n, v2 ⟨k, nat.lt_succ_of_lt k.is_lt⟩), from vector_of_fin_fn_inj $ vector.cons_inj h, begin by_cases hx : x.val < n, { convert congr_fun this ⟨x.val, _⟩, repeat {simp [fin.ext_iff], refl }, exact hx }, { have hxn : x = ⟨n, nat.lt_succ_self _⟩, from fin.eq_of_veq (nat.eq_of_lt_succ_of_not_lt x.is_lt hx), rw hxn, apply vector.cons_inj_left h } end end fix_q noncomputable def crq (r : ℝ) (q : ℕ) := ((one_coeff_poly q).eval₂ coe r) / r ^ ((q-1 : ℝ)/3) lemma one_coeff_poly_support (q : ℕ) : finsupp.support (one_coeff_poly q) = finset.range q := begin ext x, constructor, { intro hx, have : (one_coeff_poly q).coeff x ≠ 0, from finsupp.mem_support_iff.1 hx, rw finset.mem_range, apply lt_of_not_ge, exact mt (one_coeff_poly_coeff_ge _) this }, { intro hx, rw finsupp.mem_support_iff, apply ne_of_gt, convert zero_lt_one, apply one_coeff_poly_coeff_lt, simpa using hx } end lemma one_coeff_poly_nat_degree {q : ℕ} (hq : q > 0) : (one_coeff_poly q).nat_degree = q - 1 := begin have hq : ∃ n, q = n + 1, from nat.exists_eq_succ_of_ne_zero (ne_of_gt hq), cases hq with n hn, rw [hn, polynomial.nat_degree, polynomial.degree, one_coeff_poly_support, finset.sup_range], simp [option.get_or_else] end lemma one_coeff_poly_pow_nat_degree {q : ℕ} (n : ℕ) (hq : q > 0) : ((one_coeff_poly q)^n).nat_degree = (q-1)*n := begin rw [polynomial.nat_degree_pow_eq', one_coeff_poly_nat_degree hq, mul_comm], apply ne_of_gt, apply pow_pos, rw [polynomial.leading_coeff, one_coeff_poly_nat_degree hq, one_coeff_poly_coeff_lt], { exact zero_lt_one }, { apply nat.sub_lt hq zero_lt_one } end section variables {α : Type} [discrete_field α] [fintype α] (n : ℕ) local notation `m` := m α n local notation `q` := @q α _ _ lemma q_ge_two : q ≥ 2 := finset.card_le_of_inj_on (λ n, if n = 0 then 0 else 1) (λ _ _, finset.mem_univ _) $ λ i j hi hj, begin split_ifs; simp *, have hi' : i = 1, from nat.eq_of_lt_succ_of_not_lt hi (not_lt_of_ge (nat.succ_le_of_lt (nat.pos_of_ne_zero h))), have hj' : j = 1, from nat.eq_of_lt_succ_of_not_lt hj (not_lt_of_ge (nat.succ_le_of_lt (nat.pos_of_ne_zero h_1))), rwa ←hj' at hi' end theorem lemma_13_8 {d : ℚ} (hd : d ≥ 0) : m d = (finset.range (⌊d⌋.nat_abs + 1)).sum (cf q n) := begin have hbc : finset.card (finset.univ.filter (λ v : fin n → fin q, ↑(fin.sum (↑v : fin n → ℕ)) ≤ d)) = m d, { have h := h_B_card α n d, simp only [l125_A, l125_B, total_degree_monom] at h, convert h }, have hse : finset.card (finset.univ.filter (λ v : fin n → fin q, ↑(fin.sum (↑v : fin n → ℕ)) ≤ d)) = finset.card (finset.bind (finset.range (⌊d⌋.nat_abs + 1)) (λ k, sf q n k)), { apply finset.card_congr (λ v _, vector_of_fin_fn v), { intros _ ha, exact vector_of_fin_fn_mem_bind (finset.mem_filter.1 ha).2 }, { intros _ _ _ _ hv1v2, exact vector_of_fin_fn_inj hv1v2 }, { dsimp, intros b hb, exact ⟨ fin_fn_of_vector b, finset.mem_filter.2 ⟨finset.mem_univ _, (inv_in_range hd hb)⟩, vector_of_fin_fn_inv _ ⟩ }}, rw [←hbc, hse, finset.card_bind], refl, intros _ _ _ _, apply sf_disjoint end lemma int.cast_nat_abs {z : ℤ} (hz : 0 ≤ z) : (z.nat_abs : ℝ) = z := by norm_cast; exact int.nat_abs_of_nonneg hz lemma one_coeff_poly_pow_eval₂_nonneg {r : ℝ} (hr : 0 ≤ r) (k) : 0 ≤ polynomial.eval₂ coe r (one_coeff_poly k ^ n) := begin rw polynomial.eval₂_pow, apply pow_nonneg, apply one_coeff_poly_eval₂_nonneg, { exact nat.cast_nonneg }, { assumption } end lemma finset_sum_range {r : ℝ} (hr : 0 < r) (hr2 : r < 1) : (finset.range ((q - 1) * n + 1)).sum (λ (j : ℕ), r ^ j * ↑(cf q n j)) = ((one_coeff_poly q) ^ n).eval₂ coe r := begin convert polynomial.poly_eq_deg_sum _ _ _, { rw one_coeff_poly_pow_nat_degree _ q_pos }, { ext, rw [lemma_13_9, mul_comm], exact q_pos }, { simp } end theorem theorem_14_1 {r : ℝ} (hr : 0 < r) (hr2 : r < 1) : ↑(m (((q : ℚ)- 1)*n / 3)) ≤ (crq r q) ^ n := let e := ⌊((q - 1 : ℝ))*n / 3⌋.nat_abs in have hpos : ((q - 1 : ℝ))*n / 3 ≥ 0, from div_nonneg (mul_nonneg (sub_nonneg_of_le (by exact_mod_cast one_le_q)) (nat.cast_nonneg _)) (by norm_num), have r^e * m (((q : ℚ)- 1)*n / 3) = (finset.range (e+1)).sum (λ j, r^e * cf q n j), begin rw [lemma_13_8, ←finset.sum_hom coe, finset.mul_sum], { congr' 2, dsimp [e], congr' 2, rw ←@rat.cast_floor ℝ _ _ (((q : ℚ) + -1) * n /3), simp }, { apply_instance }, { assumption_mod_cast } end, have hmr : ∀ x, x ∈ finset.range (e + 1) → x < (q-1)*n + 1, from λ x hx, begin dsimp only [e] at hx, have hx' := finset.mem_range.1 hx, apply lt_of_lt_of_le hx', apply nat.succ_le_succ, suffices : (int.nat_abs ⌊((q : ℝ) -1)*n / 3⌋ : ℝ) ≤ ↑((q-1)*n), from nat.cast_le.1 this, rw int.cast_nat_abs, { apply le_trans, { apply floor_le }, { apply (div_le_iff _).2, convert le_mul_of_ge_one_right _ _, { simp [one_le_q], }, { apply mul_nonneg, { apply sub_nonneg_of_le, exact_mod_cast one_le_q }, { apply nat.cast_nonneg } }, repeat { norm_num } } }, rw floor_nonneg, exact hpos end, have r^e * m (((q : ℚ)- 1)*n / 3) ≤ ((one_coeff_poly q)^n).eval₂ coe r, from calc _ = (finset.range (e+1)).sum (λ j, r^e * cf q n j) : this ... ≤ (finset.range (e+1)).sum (λ j, r^j * cf q n j) : finset.sum_le_sum $ λ x hx,mul_le_mul_of_nonneg_right (pow_le_pow_of_le_one (le_of_lt hr) (le_of_lt hr2) (by linarith [finset.mem_range.1 hx])) (nat.cast_nonneg _) ... ≤ (finset.range ((q - 1) * n + 1)).sum (λ j, r^j * cf q n j) : finset.sum_le_sum_of_subset_of_nonneg (λ x hx, finset.mem_range.2 (hmr _ hx)) $ λ x hx1 hx2, mul_nonneg (pow_nonneg (le_of_lt hr) _) (nat.cast_nonneg _) ... = ((one_coeff_poly q)^n).eval₂ coe r : finset_sum_range _ hr hr2, calc _ ≤ ((one_coeff_poly q)^n).eval₂ coe r / r^e : (le_div_iff (pow_pos hr _)).2 (by convert this using 1; apply mul_comm) ... ≤ ((one_coeff_poly q)^n).eval₂ coe r / r^(((q - 1 : ℝ))*n / 3) : div_le_div_of_le_left (one_coeff_poly_pow_eval₂_nonneg _ (le_of_lt hr) _) (pow_pos hr _) (real.rpow_pos_of_pos hr _) begin rw ←real.rpow_nat_cast, apply real.rpow_le hr (le_of_lt hr2), dsimp [e], rw int.cast_nat_abs, apply floor_le, apply floor_nonneg.2 hpos end ... = ((one_coeff_poly q)^n).eval₂ coe r / (r^(((q - 1 : ℝ)) / 3))^n : begin rw [←real.rpow_nat_cast _ n, ←real.rpow_mul], {congr, ring}, {exact le_of_lt hr} end ... = ((one_coeff_poly q).eval₂ coe r / (r^(((q - 1 : ℝ)) / 3)))^n : begin rw [polynomial.eval₂_pow, ←div_pow], apply ne_of_gt, apply real.rpow_pos_of_pos hr end end lemma one_coeff_poly_eval_ge_one {r : ℝ} {q : ℕ} (hr : 0 ≤ r) (hq : 1 ≤ q) : 1 ≤ (one_coeff_poly q).eval₂ coe r := begin cases nat.exists_eq_succ_of_ne_zero (ne_of_gt (lt_of_lt_of_le zero_lt_one hq)) with k hqk, rw [hqk, polynomial.eval₂, finsupp.sum, one_coeff_poly_support, finset.sum_range_succ', pow_zero, mul_one], change 1 ≤ _ + (↑((one_coeff_poly (k+1)).coeff 0) : ℝ), rw [one_coeff_poly_coeff_lt (k+1) (nat.succ_pos _)], convert (le_add_iff_nonneg_left _).2 _, { simp }, { apply finset.zero_le_sum, intros x hx, apply mul_nonneg, { convert zero_le_one, show ((one_coeff_poly (k+1)).coeff (x+1) : ℝ) = 1, rw [one_coeff_poly_coeff_lt]; simp, simpa using hx }, { apply pow_nonneg, exact hr } } end lemma crq_ge_one {r : ℝ} {q : ℕ} (hr : 0 < r) (hr2 : r ≤ 1) (hq : 1 ≤ q) : 1 ≤ crq r q := begin rw [crq, le_div_iff, one_mul], { transitivity (1 : ℝ), { apply real.rpow_le_one _ (le_of_lt hr) hr2, apply div_nonneg, { apply sub_nonneg_of_le, suffices : ((1 : ℕ) : ℝ) ≤ q, by simpa using this, exact nat.cast_le.2 hq }, { norm_num } }, { apply one_coeff_poly_eval_ge_one (le_of_lt hr) hq } }, { apply real.rpow_pos_of_pos hr } end lemma crq_eq (r : ℝ) (q : ℕ) : crq r q = (finset.range q).sum (λi:ℕ, r ^ i) / r ^ ((q - 1 : ℝ) / 3) := begin rw [crq, one_coeff_poly, ← finset.sum_hom (polynomial.eval₂ (coe : ℕ → ℝ) r)], congr' 2, funext i, rw [polynomial.eval₂_pow, polynomial.eval₂_X] end theorem lemma_13_15 {q : ℕ} (hq : 2 ≤ q) : ∃ r : ℝ, 0 < r ∧ r < 1 ∧ crq r q < q := begin have q_pos : 0 < q := le_trans dec_trivial hq, have q_eq : q = (q - 1) + 1 := (nat.sub_add_cancel $ le_trans dec_trivial hq).symm, have q_pred : ((q - 1 : ℕ) : ℝ) = (q : ℝ) - 1, { rw [q_eq] { occs := occurrences.pos [2] }, rw [nat.cast_add, nat.cast_one, add_sub_cancel] }, have sum_pow_one : (finset.range q).sum (pow 1 : ℕ → ℝ) = (finset.range q).sum (λi, (1 : ℝ)), { congr, funext i, rw one_pow }, let f : ℝ → ℝ := λr, ((finset.range q).sum (λi, r ^ i)) ^ 3 - (q : ℝ)^3 * r^(q-1), let f' : ℝ → ℝ := λr, 3 * (finset.range q).sum (λi, r ^ i) ^ 2 * (finset.range (q - 1)).sum (λi, (i + 1) * r ^ i) - (q : ℝ)^3 * (q - 1) * r^(q-2), have f1 : f 1 = 0, { simp only [f], rw [sum_pow_one, finset.sum_const, finset.card_range, add_monoid.smul_one, one_pow, mul_one, sub_self] }, have f'1 : 0 < f' 1, { simp only [f'], rw [one_pow, sum_pow_one, finset.sum_const, finset.card_range, add_monoid.smul_one, mul_one], conv { find (finset.sum _ _) { congr, skip, funext, rw [one_pow, mul_one] } }, have : ((2:ℕ) : ℝ) ≠ 0, { rw [(≠), nat.cast_eq_zero], exact dec_trivial }, rw [finset.sum_add_distrib, finset.sum_const, finset.card_range, add_monoid.smul_one, add_comm, ← finset.sum_range_succ, ← finset.sum_nat_cast, ← nat.add_one, ← q_eq, ← mul_div_cancel (coe (finset.sum _ _ : ℕ) : ℝ) this, ← nat.cast_mul, finset.sum_range_id_mul_two, nat.cast_mul, mul_assoc, ← mul_div_assoc, mul_div_comm, sub_pos, ← mul_assoc, ← mul_one (_ * _ : ℝ), ← pow_succ', q_pred], refine mul_lt_mul_of_pos_left _ _, { suffices : ((1 : ℕ) : ℝ) < (3 : ℕ) / (2 : ℕ), { simpa }, rw [lt_div_iff, ← nat.cast_mul, nat.cast_lt], exact dec_trivial, rw [← nat.cast_zero, nat.cast_lt], exact dec_trivial }, { refine mul_pos (pow_pos (nat.cast_pos.2 q_pos) _) (sub_pos.2 _), rw [← nat.cast_one, nat.cast_lt], exact hq } }, have has_deriv_f_f' : ∀r, has_deriv f (f' r) r, { assume r, refine has_deriv.sub ((has_deriv.pow (has_deriv_finset_sum _ _) 3).congr₂ _) _, { exact λi:ℕ, i * r ^ (i - 1) }, { congr' 1, { simp only [nat.succ_sub_succ_eq_sub, eq_self_iff_true, nat.cast_bit1, nat.sub_zero, nat.cast_one] }, { rw [q_eq] { occs := occurrences.pos [1] }, rw [finset.sum_range_succ'], simp only [add_zero, nat.succ_sub_succ_eq_sub, nat.zero_sub, nat.cast_zero, zero_mul, add_comm, eq_self_iff_true, (∘), nat.cast_succ, nat.sub_zero, pow_zero, zero_add] } }, { refine assume n, (has_deriv.pow (has_deriv_id r) n).congr₂ _, rw mul_one }, { rw mul_assoc, refine ((has_deriv.pow (has_deriv_id r) _).congr₂ _).mul_left _, rw [q_eq] { occs := occurrences.pos [3] }, rw [nat.cast_add, nat.cast_one, add_sub_cancel, mul_one], refl } }, rcases decreasing_of_fderiv_pos _ _ _ (has_deriv_f_f' 1) f'1 with ⟨ε, hε, h⟩, let ε' : ℝ := 1 - (min ε 1) / 2, have hε₁ : 1 - ε < ε', { refine (sub_lt_sub_iff_left _).2 (lt_of_le_of_lt (div_le_div_of_le_of_pos (min_le_left _ _) _) _); linarith }, have hε₂ : ε' < 1, { refine (sub_lt_self _ $ div_pos (lt_min hε _) _); norm_num }, have hε₃ : 0 < ε', { refine lt_sub.2 (lt_of_le_of_lt (div_le_div_of_le_of_pos (min_le_right _ _) _) _); norm_num }, refine ⟨ε', hε₃, hε₂, _⟩, specialize h ε' hε₁ hε₂, rw f1 at h, simp only [f, sub_lt_iff_lt_add, zero_add] at h, rw [crq_eq, div_lt_iff], refine lt_of_pow_lt_pow 3 _ _, { exact zero_le_mul (nat.cast_nonneg _) (real.rpow_nonneg_of_nonneg (le_of_lt hε₃) _) }, { have : ((3 : ℕ) : ℝ) = 3 := by norm_num, rwa [mul_pow, ← real.rpow_nat_cast (ε' ^ _), ← real.rpow_mul (le_of_lt hε₃), this, div_mul_cancel, ← q_pred, real.rpow_nat_cast], norm_num }, { exact real.rpow_pos_of_pos hε₃ _ } end lemma coeff_ge_one {r : ℝ} (hr : 0 < r) (hr2 : r < 1) {q : ℕ} (hq : q ≥ 1) : ((3 * (crq r q)^2)/(1 - r)) ≥ 1 := have 1 ≤ crq r q^2, from one_le_pow_of_one_le (crq_ge_one hr (le_of_lt hr2) hq) _, by apply (le_div_iff _).2; linarith lemma finset.card_le_card_univ {α} [fintype α] [decidable_eq α] (A : finset α) : A.card ≤ (@finset.univ α _).card := finset.card_le_card_of_inj_on id (λ _ _, finset.mem_univ _) $ by simp section set_option class.instance_max_depth 200 variables {α : Type} [discrete_field α] [fintype α] {n : ℕ} {a b c : α} (hc : c ≠ 0) (habc : a + b + c = 0) {A : finset (fin n → α)} (hall : ∀ x y z : fin n → α, x ∈ A → y ∈ A → z ∈ A → a • x + b • y + c • z = 0 → x = y ∧ x = z) include hc habc hall local notation `q` := @q α _ _ theorem theorem_13_14 {r : ℝ} (hr : 0 < r) (hr2 : r < 1) : ↑A.card ≤ 3 * (crq r q)^n := if hn : n = 0 then have hcu : (@finset.univ (fin n → α) _).card = 1, by { convert finset.card_univ, rw hn, refl }, have hac : A.card ≤ 1, by { convert finset.card_le_card_univ _, {exact eq.symm hcu}, {apply_instance}}, have hac : (A.card : ℝ) ≤ 1, from suffices (A.card : ℝ) ≤ (1 : ℕ), by simpa, nat.cast_le.2 hac, le_trans hac (by rw [hn, pow_zero, mul_one]; norm_num) else have hn : n > 0, from nat.pos_of_ne_zero hn, begin transitivity, { apply nat.cast_le.2, apply theorem_12_1 n hc habc hn hall }, { rw nat.cast_mul, convert mul_le_mul_of_nonneg_left _ _, { norm_cast }, { rw [mul_comm, ←div_eq_mul_one_div], apply theorem_14_1; assumption }, { norm_num } } end end section set_option class.instance_max_depth 200 theorem general_cap_set {α : Type} [discrete_field α] [fintype α] : ∃ C B : ℝ, B > 0 ∧ C > 0 ∧ C < fintype.card α ∧ ∀ {a b c : α} {n : ℕ} {A : finset (fin n → α)}, c ≠ 0 → a + b + c = 0 → (∀ x y z : fin n → α, x ∈ A → y ∈ A → z ∈ A → a • x + b • y + c • z = 0 → x = y ∧ x = z) → ↑A.card ≤ B * C^n := begin rcases lemma_13_15 q_ge_two with ⟨B, hB, hB2, hBr⟩, repeat {apply exists.intro}, refine ⟨_, _, hBr, λ a b c n A hc habc hall, theorem_13_14 hc habc hall hB hB2⟩, { norm_num }, { apply lt_of_lt_of_le zero_lt_one, exact crq_ge_one hB (le_of_lt hB2) one_le_q } end end lemma one_coeff_poly_real_eval_aux (x : ℝ) : (one_coeff_poly 3).eval₂ coe x = (((1 : ℕ) : ℝ) * (1 : ℝ)) + ( (((1 : ℕ) : ℝ) * (x * (1 : ℝ))) + ((((1 : ℕ) : ℝ) * (x * (x * (1 : ℝ)))) + (0 : ℝ) ) ) := rfl lemma one_coeff_poly_real_eval (x : ℝ) : (one_coeff_poly 3).eval₂ coe x = 1 + x + x^2 := by convert one_coeff_poly_real_eval_aux x using 1; simp [pow_two] lemma calc_lemma (r a : ℝ) (hr : r = (a - 1)/8) : (3 / 8) ^ 3 * (207 + 33 * (8 * r + 1)) * r ^ 2 - (1 + r + r ^ 2) ^ 3 = -(140481/262144) - (70389*a)/131072 + (14553*a^2)/262144 + (1215*a^3)/65536 - (279*a^4)/262144 - (9*a^5)/131072 - a^6/262144 := by rw hr; ring noncomputable def r : ℝ := (real.sqrt 33 - 1) / 8 lemma r_pos : r > 0 := div_pos (sub_pos_of_lt (lt_of_pow_lt_pow _ (real.sqrt_nonneg _) (show (1 : ℝ) ^ 2 < (real.sqrt 33)^2, by rw [real.sqr_sqrt, one_pow]; norm_num))) (by norm_num) lemma r_lt_one : r < 1 := begin apply (div_lt_iff _).2, apply sub_lt_iff_lt_add.2, apply @lt_of_pow_lt_pow _ _ _ _ 2 _, rw real.sqr_sqrt, all_goals {norm_num} end lemma r_def : real.sqrt 33 = 8*r + 1 := by rw [r, mul_div_cancel', sub_add_cancel]; norm_num lemma c_r_cubed : (crq r 3)^3 = ((3 : ℝ) / 8)^3 * (207 + 33*real.sqrt 33) := begin rw [crq, one_coeff_poly_real_eval, div_pow], conv {to_lhs, congr, skip, rw [←real.rpow_nat_cast _ 3, ←real.rpow_mul (le_of_lt r_pos)], simp, norm_num}, symmetry, apply eq_div_of_mul_eq, { apply ne_of_gt, apply real.rpow_pos_of_pos, exact r_pos }, { rw [r_def], apply eq_of_sub_eq_zero, have hr2 : r^(2 : ℝ) = r^((2 : ℕ) : ℝ), by simp, rw [hr2, real.rpow_nat_cast, calc_lemma r _ rfl], change 6 with 2+2+2, change 5 with 2+2+1, change 4 with 2 + 2, change 3 with 2+1, simp only [pow_add, pow_one, real.sqr_sqrt (show (0:ℝ) ≤ 33, by norm_num)], ring }, { apply ne_of_gt, apply real.rpow_pos_of_pos, exact r_pos } end lemma c_r_cuberoot : crq r 3 = (((3 : ℝ) / 8)^3 * (207 + 33*real.sqrt 33))^(1/3 : ℝ) := have h : _ := congr_arg (λ r : ℝ, r^(1/3:ℝ)) c_r_cubed, begin rw [←real.rpow_nat_cast _ 3, ←real.rpow_mul] at h, { simp at h, rw [mul_inv_cancel] at h, { simpa using h }, { norm_num } }, { linarith [crq_ge_one r_pos (le_of_lt r_lt_one) (show 1 ≤ 3, by norm_num)] } end section set_option class.instance_max_depth 200 theorem theorem_13_17 {α : Type} [discrete_field α] [fintype α] {a b c : α} (hq : fintype.card α = 3) (hc : c ≠ 0) (habc : a + b + c = 0) : ∃ B : ℝ, ∀ {n : ℕ} {A : finset (fin n → α)}, (∀ x y z : fin n → α, x ∈ A → y ∈ A → z ∈ A → a • x + b • y + c • z = 0 → x = y ∧ x = z) → ↑A.card ≤ B * ((((3 : ℝ) / 8)^3 * (207 + 33*real.sqrt 33))^(1/3 : ℝ))^n := begin apply exists.intro, intros n A ha, convert theorem_13_14 hc habc ha r_pos r_lt_one, rw [q, hq, c_r_cuberoot] end end section private lemma three_prime : nat.prime 3 := by norm_num local notation `ℤ/3ℤ` := zmodp 3 three_prime set_option class.instance_max_depth 100 theorem cap_set_problem : ∃ B : ℝ, ∀ {n : ℕ} {A : finset (fin n → ℤ/3ℤ)}, (∀ x y z : fin n → ℤ/3ℤ, x ∈ A → y ∈ A → z ∈ A → x + y + z = 0 → x = y ∧ x = z) → ↑A.card ≤ B * ((((3 : ℝ) / 8)^3 * (207 + 33*real.sqrt 33))^(1/3 : ℝ))^n := begin have habc : (1 : ℤ/3ℤ) + 1 + 1 = 0, from rfl, have hcard : fintype.card ℤ/3ℤ = 3, from zmodp.card_zmodp three_prime, have hone : (1 : ℤ/3ℤ) ≠ 0, from dec_trivial, cases theorem_13_17 hcard hone habc with B hB, use B, intros n A hxyz, apply hB, simpa using hxyz end theorem cap_set_problem_specific (n : ℕ) {A : finset (fin n → ℤ/3ℤ)} (hxyz : ∀ x y z : fin n → ℤ/3ℤ, x ∈ A → y ∈ A → z ∈ A → x + y + z = 0 → x = y ∧ x = z) : ↑A.card ≤ 3 * ((((3 : ℝ) / 8)^3 * (207 + 33*real.sqrt 33))^(1/3 : ℝ))^n := have hone : (1 : ℤ/3ℤ) ≠ 0, from dec_trivial, have hq : @q (ℤ/3ℤ) _ _ = 3, from zmodp.card_zmodp three_prime, begin transitivity, { convert theorem_13_14 hone _ _ r_pos r_lt_one, repeat {exact 1}, { refl }, { simpa using hxyz } }, rw hq, convert mul_le_mul_of_nonneg_right _ _, { symmetry, exact c_r_cuberoot }, { refl }, { apply pow_nonneg, apply le_trans zero_le_one, apply crq_ge_one r_pos (le_of_lt r_lt_one), norm_num } end end
ca467f633a19bdeebf098fabc13261696eb6610f
94637389e03c919023691dcd05bd4411b1034aa5
/src/inClassNotes/langs/arith_expr.lean
66bd1362485f1d21e310f128316c4f6eafcc8425
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
722
lean
/- Define the abstract and concrete syntax and semantics of simple arithmetic expressions, to include variables. -/ inductive avar : Type | mk (n : nat) def a_state := avar → nat inductive aexp : Type | lit_expr (n : nat) | var_expr (a : avar) | add_expr (e1 e2 : aexp) | mul_expr (e1 e2 : aexp) open aexp def aeval : aexp → a_state → nat | (lit_expr n) st := n | (var_expr v) st := st v | (add_expr e1 e2) st := (aeval e1 st) + (aeval e2 st) | (mul_expr e1 e2) st := (aeval e1 st) * (aeval e2 st) notation `[` n `]` := lit_expr n notation `[` v `]` := var_expr v notation e1 + e2 := add_expr e1 e2 notation e1 * e2 := mul_expr e1 e2 def st0 := λ (v : avar), 0 example : aeval ([3] + [5]) st0 = 8 := rfl
1ea73df2db443c5e712fd6fa4894d777fb899805
a45212b1526d532e6e83c44ddca6a05795113ddc
/test/finish2.lean
de9e13ef1925459a98bdfd39718e7cccaa87dd98
[ "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
12,717
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Nathaniel Thomas More examples to test automation, stolen shamelessly by Jeremy from Nathaniel's "tauto". -/ import tactic.finish open nat section variables (a b c d e f : Prop) variable even : ℕ → Prop variable P : ℕ → Prop -- these next five are things that tauto doesn't get example : (∀ x, P x) ∧ b → (∀ y, P y) ∧ P 0 ∨ b ∧ P 0 := by finish example : (∀ A, A ∨ ¬A) → ∀ x y : ℕ, x = y ∨ x ≠ y := by finish example : ∀ b1 b2, b1 = b2 ↔ (b1 = tt ↔ b2 = tt) := begin intro b1, cases b1; finish [iff_def] end example : ∀ (P Q : nat → Prop), (∀ n, Q n → P n) → (∀ n, Q n) → P 2 := by finish example (a b c : Prop) : ¬ true ∨ false ∨ b ↔ b := by finish example : true := by finish example : false → a := by finish example : a → a := by finish example : (a → b) → a → b := by finish example : ¬ a → ¬ a := by finish example : a → (false ∨ a) := by finish example : (a → b → c) → (a → b) → a → c := by finish example : a → ¬ a → (a → b) → (a ∨ b) → (a ∧ b) → a → false := by finish example : ((a ∧ b) ∧ c) → b := by finish example : ((a → b) → c) → b → c := by finish example : (a ∨ b) → (b ∨ a) := by finish example : (a → b ∧ c) → (a → b) ∨ (a → c) := by finish example : ∀ (x0 : a ∨ b) (x1 : b ∧ c), a → b := by finish example : a → b → (c ∨ b) := by finish example : (a ∧ b → c) → b → a → c := by finish example : (a ∨ b → c) → a → c := by finish example : (a ∨ b → c) → b → c := by finish example : (a ∧ b) → (b ∧ a) := by finish example : (a ↔ b) → a → b := by finish example : a → ¬¬a := by finish example : ¬¬(a ∨ ¬a) := by finish example : ¬¬(a ∨ b → a ∨ b) := by finish example : ¬¬((∀ n, even n) ∨ ¬(∀ m, even m)) := by finish example : (¬¬b → b) → (a → b) → ¬¬a → b := by finish example : (¬¬b → b) → (¬b → ¬ a) → ¬¬a → b := by finish example : ((a → b → false) → false) → (b → false) → false := by finish example : ((((c → false) → a) → ((b → false) → a) → false) → false) → (((c → b → false) → false) → false) → ¬a → a := by finish example (p q r : Prop) (a b : nat) : true → a = a → q → q → p → p := by finish example : ∀ (F F' : Prop), F ∧ F' → F := by finish example : ∀ (F1 F2 F3 : Prop), ((¬F1 ∧ F3) ∨ (F2 ∧ ¬F3)) → (F2 → F1) → (F2 → F3) → ¬F2 := by finish example : ∀ (f : nat → Prop), f 2 → ∃ x, f x := by finish example : true ∧ true ∧ true ∧ true ∧ true ∧ true ∧ true := by finish example : ∀ (P : nat → Prop), P 0 → (P 0 → P 1) → (P 1 → P 2) → (P 2) := by finish example : ¬¬¬¬¬a → ¬¬¬¬¬¬¬¬a → false := by finish example : ∀ n, ¬¬(even n ∨ ¬even n) := by finish example : ∀ (p q r s : Prop) (a b : nat), r ∨ s → p ∨ q → a = b → q ∨ p := by finish example : (∀ x, P x) → (∀ y, P y) := by finish /- TODO(Jeremy): reinstate after simp * at * bug is fixed. example : ((a ↔ b) → (b ↔ c)) → ((b ↔ c) → (c ↔ a)) → ((c ↔ a) → (a ↔ b)) → (a ↔ b) := by finish [iff_def] -/ example : ((¬a ∨ b) ∧ (¬b ∨ b) ∧ (¬a ∨ ¬b) ∧ (¬b ∨ ¬b) → false) → ¬((a → b) → b) → false := by finish example : ¬((a → b) → b) → ((¬b ∨ ¬b) ∧ (¬b ∨ ¬a) ∧ (b ∨ ¬b) ∧ (b ∨ ¬a) → false) → false := by finish example : (¬a ↔ b) → (¬b ↔ a) → (¬¬a ↔ a) := by finish example : (¬ a ↔ b) → (¬ (c ∨ e) ↔ d ∧ f) → (¬ (c ∨ a ∨ e) ↔ d ∧ b ∧ f) := by finish example {A : Type} (p q : A → Prop) (a b : A) : q a → p b → ∃ x, (p x ∧ x = b) ∨ q x := by finish example {A : Type} (p q : A → Prop) (a b : A) : p b → ∃ x, q x ∨ (p x ∧ x = b) := by finish example : ¬ a → b → a → c := by finish example : a → b → b → ¬ a → c := by finish example (a b : nat) : a = b → b = a := by finish -- good examples of things we don't get, even using the simplifier example (a b c : nat) : a = b → a = c → b = c := by finish example (p : nat → Prop) (a b c : nat) : a = b → a = c → p b → p c := by finish example (p : Prop) (a b : nat) : a = b → p → p := by finish -- safe should look for contradictions with constructors example (a : nat) : (0 : ℕ) = succ a → a = a → false := by finish example (p : Prop) (a b c : nat) : [a, b, c] = [] → p := by finish example (a b c : nat) : succ (succ a) = succ (succ b) → c = c := by finish example (p : Prop) (a b : nat) : a = b → b ≠ a → p := by finish example : (a ↔ b) → ((b ↔ a) ↔ (a ↔ b)) := by finish example (a b c : nat) : b = c → (a = b ↔ c = a) := by finish [iff_def] example : ¬¬¬¬¬¬¬¬a → ¬¬¬¬¬a → false := by finish example (a b c : Prop) : a ∧ b ∧ c ↔ c ∧ b ∧ a := by finish example (a b c : Prop) : a ∧ false ∧ c ↔ false := by finish example (a b c : Prop) : a ∨ false ∨ b ↔ b ∨ a := by finish example : a ∧ not a ↔ false := by finish example : a ∧ b ∧ true → b ∧ a := by finish example (A : Type) (a₁ a₂ : A) : a₁ = a₂ → (λ (B : Type) (f : A → B), f a₁) = (λ (B : Type) (f : A → B), f a₂) := by finish example (a : nat) : ¬ a = a → false := by finish example (A : Type) (p : Prop) (a b c : A) : a = b → b ≠ a → p := by finish example (p q r s : Prop) : r ∧ s → p ∧ q → q ∧ p := by finish example (p q : Prop) : p ∧ p ∧ q ∧ q → q ∧ p := by finish example (p : nat → Prop) (q : nat → nat → Prop) : (∃ x y, p x ∧ q x y) → q 0 0 ∧ q 1 1 → (∃ x, p x) := by finish example (p q r s : Prop) (a b : nat) : r ∨ s → p ∨ q → a = b → q ∨ p := by finish example (p q r : Prop) (a b : nat) : true → a = a → q → q → p → p := by finish example (a b : Prop) : a → b → a := by finish example (p q : nat → Prop) (a b : nat) : p a → q b → ∃ x, p x := by finish example : ∀ b1 b2, b1 && b2 = ff ↔ (b1 = ff ∨ b2 = ff) := by finish example : ∀ b1 b2, b1 && b2 = tt ↔ (b1 = tt ∧ b2 = tt) := by finish example : ∀ b1 b2, b1 || b2 = ff ↔ (b1 = ff ∧ b2 = ff) := by finish example : ∀ b1 b2, b1 || b2 = tt ↔ (b1 = tt ∨ b2 = tt) := by finish example : ∀ b, bnot b = tt ↔ b = ff := by finish example : ∀ b, bnot b = ff ↔ b = tt := by finish example : ∀ b c, b = c ↔ ¬ (b = bnot c) := by intros b c; cases b; cases c; finish [iff_def] inductive and3 (a b c : Prop) : Prop | mk : a → b → c → and3 example (h : and3 a b c) : and3 b c a := by cases h; split; finish inductive or3 (a b c : Prop) : Prop | in1 : a → or3 | in2 : b → or3 | in3 : c → or3 /- TODO(Jeremy): write a tactic that tries all constructors example (h : a) : or3 a b c := sorry example (h : b) : or3 a b c := sorry example (h : c) : or3 a b c := sorry -/ variables (A₁ A₂ A₃ A₄ B₁ B₂ B₃ B₄ : Prop) -- H first, all pos example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) : B₄ := by finish example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₄) : B₃ := by finish example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n3 : ¬B₃) (n3 : ¬B₄) : B₂ := by finish example (H1 : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : B₁ := by finish example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₃ := by finish example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₂ := by finish example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) : ¬A₁ := by finish -- H last, all pos example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₄ := by finish example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₃ := by finish example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₂ := by finish example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₁ := by finish example (a1 : A₁) (a2 : A₂) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₃ := by finish example (a1 : A₁) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₂ := by finish example (a2 : A₂) (a3 : A₃) (n1 : ¬B₁) (n2 : ¬B₂) (n3 : ¬B₃) (n3 : ¬B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬A₁ := by finish -- H first, all neg example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) : ¬B₄ := by finish example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b4 : B₄) : ¬B₃ := by finish example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b3 : B₃) (b4 : B₄) : ¬B₂ := by finish example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬B₁ := by finish example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n2 : ¬A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₃ := by finish example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n1 : ¬A₁) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₂ := by finish example (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬¬A₁ := by finish -- H last, all neg example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₄ := by finish example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₃ := by finish example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₂ := by finish example (n1 : ¬A₁) (n2 : ¬A₂) (n3 : ¬A₃) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬B₁ := by finish example (n1 : ¬A₁) (n2 : ¬A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₃ := by finish example (n1 : ¬A₁) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₂ := by finish example (n2 : ¬A₂) (n3 : ¬A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬A₁ → ¬A₂ → ¬A₃ → ¬B₁ ∨ ¬B₂ ∨ ¬B₃ ∨ ¬B₄) : ¬¬A₁ := by finish section club variables Scottish RedSocks WearKilt Married GoOutSunday : Prop theorem NoMember : (¬Scottish → RedSocks) → (WearKilt ∨ ¬RedSocks) → (Married → ¬GoOutSunday) → (GoOutSunday ↔ Scottish) → (WearKilt → Scottish ∧ Married) → (Scottish → WearKilt) → false := by finish end club end
03c8970ffd12fc43fd2142c000b5b7f1a27e23f8
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/02_Dependent_Type_Theory.org.10.lean
0e6a2e91a86c72cac761231e61d7884e2070c8ca
[]
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
734
lean
/- page 16 -/ import standard import data.bool -- BEGIN constants A B : Type constants a1 a2 : A constants b1 b2 : B constant f : A → A constant g : A → B constant h : A → B → A constant p : A → A → bool check fun x : A, f x -- A → A check λ x : A, f x -- A → A check λ x : A, f (f x) -- A → A check λ x : A, h x b1 -- A → A check λ y : B, h a1 y -- B → A check λ x : A, p (f (f x)) (h (f a1) b2) -- A → bool check λ x : A, λ y : B, h (f x) y -- A → B → A check λ (x : A) (y : B), h (f x) y -- A → B → A check λ x y, h (f x) y -- A → B → A -- END
0a1c1d07fdf7aa585e0d2f230a4ee102239910b2
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/topology/continuous_function/compact.lean
63caf6b14ce60e63d88d4d06fa16119aa8de5803
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,172
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import topology.continuous_function.bounded import analysis.normed_space.linear_isometry import topology.uniform_space.compact_separated import tactic.equiv_rw /-! # Continuous functions on a compact space Continuous functions `C(α, β)` from a compact space `α` to a metric space `β` are automatically bounded, and so acquire various structures inherited from `α →ᵇ β`. This file transfers these structures, and restates some lemmas characterising these structures. If you need a lemma which is proved about `α →ᵇ β` but not for `C(α, β)` when `α` is compact, you should restate it here. You can also use `bounded_continuous_function.equiv_continuous_map_of_compact` to functions back and forth. -/ noncomputable theory open_locale topological_space classical nnreal bounded_continuous_function open set filter metric open bounded_continuous_function namespace continuous_map variables (α : Type*) (β : Type*) [topological_space α] [compact_space α] [normed_group β] /-- When `α` is compact, the bounded continuous maps `α →ᵇ 𝕜` are equivalent to `C(α, 𝕜)`. -/ @[simps] def equiv_bounded_of_compact : C(α, β) ≃ (α →ᵇ β) := ⟨mk_of_compact, forget_boundedness α β, λ f, by { ext, refl, }, λ f, by { ext, refl, }⟩ /-- When `α` is compact, the bounded continuous maps `α →ᵇ 𝕜` are additively equivalent to `C(α, 𝕜)`. -/ @[simps] def add_equiv_bounded_of_compact : C(α, β) ≃+ (α →ᵇ β) := ({ ..forget_boundedness_add_hom α β, ..(equiv_bounded_of_compact α β).symm, } : (α →ᵇ β) ≃+ C(α, β)).symm -- It would be nice if `@[simps]` produced this directly, -- instead of the unhelpful `add_equiv_bounded_of_compact_apply_to_continuous_map`. @[simp] lemma add_equiv_bounded_of_compact_apply_apply (f : C(α, β)) (a : α) : add_equiv_bounded_of_compact α β f a = f a := rfl @[simp] lemma add_equiv_bounded_of_compact_to_equiv : (add_equiv_bounded_of_compact α β).to_equiv = equiv_bounded_of_compact α β := rfl instance : metric_space C(α,β) := metric_space.induced (equiv_bounded_of_compact α β) (equiv_bounded_of_compact α β).injective (by apply_instance) section variables {α β} (f g : C(α, β)) {C : ℝ} /-- The distance between two functions is controlled by the supremum of the pointwise distances -/ lemma dist_le (C0 : (0 : ℝ) ≤ C) : dist f g ≤ C ↔ ∀x:α, dist (f x) (g x) ≤ C := @bounded_continuous_function.dist_le _ _ _ _ ((equiv_bounded_of_compact α β) f) ((equiv_bounded_of_compact α β) g) _ C0 lemma dist_le_iff_of_nonempty [nonempty α] : dist f g ≤ C ↔ ∀ x, dist (f x) (g x) ≤ C := @bounded_continuous_function.dist_le_iff_of_nonempty _ _ _ _ ((equiv_bounded_of_compact α β) f) ((equiv_bounded_of_compact α β) g) _ _ lemma dist_lt_of_nonempty [nonempty α] (w : ∀x:α, dist (f x) (g x) < C) : dist f g < C := @bounded_continuous_function.dist_lt_of_nonempty_compact _ _ _ _ ((equiv_bounded_of_compact α β) f) ((equiv_bounded_of_compact α β) g) _ _ _ w lemma dist_lt_iff (C0 : (0 : ℝ) < C) : dist f g < C ↔ ∀x:α, dist (f x) (g x) < C := @bounded_continuous_function.dist_lt_iff_of_compact _ _ _ _ ((equiv_bounded_of_compact α β) f) ((equiv_bounded_of_compact α β) g) _ _ C0 lemma dist_lt_iff_of_nonempty [nonempty α] : dist f g < C ↔ ∀x:α, dist (f x) (g x) < C := @bounded_continuous_function.dist_lt_iff_of_nonempty_compact _ _ _ _ ((equiv_bounded_of_compact α β) f) ((equiv_bounded_of_compact α β) g) _ _ _ end variables (α β) /-- When `α` is compact, and `β` is a metric space, the bounded continuous maps `α →ᵇ β` are isometric to `C(α, β)`. -/ @[simps] def isometric_bounded_of_compact : C(α, β) ≃ᵢ (α →ᵇ β) := { isometry_to_fun := λ x y, rfl, to_equiv := equiv_bounded_of_compact α β } -- TODO at some point we will need lemmas characterising this norm! -- At the moment the only way to reason about it is to transfer `f : C(α,β)` back to `α →ᵇ β`. instance : has_norm C(α,β) := { norm := λ x, dist x 0 } instance : normed_group C(α,β) := { dist_eq := λ x y, begin change dist x y = dist (x-y) 0, -- it would be nice if `equiv_rw` could rewrite in multiple places at once equiv_rw (equiv_bounded_of_compact α β) at x, equiv_rw (equiv_bounded_of_compact α β) at y, have p : dist x y = dist (x-y) 0, { rw dist_eq_norm, rw dist_zero_right, }, convert p, exact ((add_equiv_bounded_of_compact α β).symm.map_sub _ _).symm, end, } section variables {α β} (f : C(α, β)) -- The corresponding lemmas for `bounded_continuous_function` are stated with `{f}`, -- and so can not be used in dot notation. lemma norm_coe_le_norm (x : α) : ∥f x∥ ≤ ∥f∥ := ((equiv_bounded_of_compact α β) f).norm_coe_le_norm x /-- Distance between the images of any two points is at most twice the norm of the function. -/ lemma dist_le_two_norm (x y : α) : dist (f x) (f y) ≤ 2 * ∥f∥ := ((equiv_bounded_of_compact α β) f).dist_le_two_norm x y /-- The norm of a function is controlled by the supremum of the pointwise norms -/ lemma norm_le {C : ℝ} (C0 : (0 : ℝ) ≤ C) : ∥f∥ ≤ C ↔ ∀x:α, ∥f x∥ ≤ C := @bounded_continuous_function.norm_le _ _ _ _ ((equiv_bounded_of_compact α β) f) _ C0 lemma norm_le_of_nonempty [nonempty α] {M : ℝ} : ∥f∥ ≤ M ↔ ∀ x, ∥f x∥ ≤ M := @bounded_continuous_function.norm_le_of_nonempty _ _ _ _ _ ((equiv_bounded_of_compact α β) f) _ lemma norm_lt_iff {M : ℝ} (M0 : 0 < M) : ∥f∥ < M ↔ ∀ x, ∥f x∥ < M := @bounded_continuous_function.norm_lt_iff_of_compact _ _ _ _ _ ((equiv_bounded_of_compact α β) f) _ M0 lemma norm_lt_iff_of_nonempty [nonempty α] {M : ℝ} : ∥f∥ < M ↔ ∀ x, ∥f x∥ < M := @bounded_continuous_function.norm_lt_iff_of_nonempty_compact _ _ _ _ _ _ ((equiv_bounded_of_compact α β) f) _ lemma apply_le_norm (f : C(α, ℝ)) (x : α) : f x ≤ ∥f∥ := le_trans (le_abs.mpr (or.inl (le_refl (f x)))) (f.norm_coe_le_norm x) lemma neg_norm_le_apply (f : C(α, ℝ)) (x : α) : -∥f∥ ≤ f x := le_trans (neg_le_neg (f.norm_coe_le_norm x)) (neg_le.mp (neg_le_abs_self (f x))) end section variables {R : Type*} [normed_ring R] instance : normed_ring C(α,R) := { norm_mul := λ f g, begin equiv_rw (equiv_bounded_of_compact α R) at f, equiv_rw (equiv_bounded_of_compact α R) at g, exact norm_mul_le f g, end, ..(infer_instance : normed_group C(α,R)) } end section variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] instance : normed_space 𝕜 C(α,β) := { norm_smul_le := λ c f, begin equiv_rw (equiv_bounded_of_compact α β) at f, exact le_of_eq (norm_smul c f), end } variables (α 𝕜) /-- When `α` is compact and `𝕜` is a normed field, the `𝕜`-algebra of bounded continuous maps `α →ᵇ β` is `𝕜`-linearly isometric to `C(α, β)`. -/ def linear_isometry_bounded_of_compact : C(α, β) ≃ₗᵢ[𝕜] (α →ᵇ β) := { map_smul' := λ c f, by { ext, simp, }, norm_map' := λ f, rfl, ..add_equiv_bounded_of_compact α β } @[simp] lemma linear_isometry_bounded_of_compact_to_isometric : (linear_isometry_bounded_of_compact α β 𝕜).to_isometric = isometric_bounded_of_compact α β := rfl @[simp] lemma linear_isometry_bounded_of_compact_to_add_equiv : (linear_isometry_bounded_of_compact α β 𝕜).to_linear_equiv.to_add_equiv = add_equiv_bounded_of_compact α β := rfl @[simp] lemma linear_isometry_bounded_of_compact_of_compact_to_equiv : (linear_isometry_bounded_of_compact α β 𝕜).to_linear_equiv.to_equiv = equiv_bounded_of_compact α β := rfl end section variables {𝕜 : Type*} {γ : Type*} [normed_field 𝕜] [normed_ring γ] [normed_algebra 𝕜 γ] instance [nonempty α] : normed_algebra 𝕜 C(α, γ) := { norm_algebra_map_eq := λ c, (norm_algebra_map_eq (α →ᵇ γ) c : _), } end end continuous_map namespace continuous_map section uniform_continuity variables {α β : Type*} variables [metric_space α] [compact_space α] [metric_space β] /-! We now set up some declarations making it convenient to use uniform continuity. -/ lemma uniform_continuity (f : C(α, β)) (ε : ℝ) (h : 0 < ε) : ∃ δ > 0, ∀ {x y}, dist x y < δ → dist (f x) (f y) < ε := metric.uniform_continuous_iff.mp (compact_space.uniform_continuous_of_continuous f.continuous) ε h /-- An arbitrarily chosen modulus of uniform continuity for a given function `f` and `ε > 0`. -/ -- This definition allows us to separate the choice of some `δ`, -- and the corresponding use of `dist a b < δ → dist (f a) (f b) < ε`, -- even across different declarations. def modulus (f : C(α, β)) (ε : ℝ) (h : 0 < ε) : ℝ := classical.some (uniform_continuity f ε h) lemma modulus_pos (f : C(α, β)) {ε : ℝ} {h : 0 < ε} : 0 < f.modulus ε h := classical.some (classical.some_spec (uniform_continuity f ε h)) lemma dist_lt_of_dist_lt_modulus (f : C(α, β)) (ε : ℝ) (h : 0 < ε) {a b : α} (w : dist a b < f.modulus ε h) : dist (f a) (f b) < ε := classical.some_spec (classical.some_spec (uniform_continuity f ε h)) w end uniform_continuity /-! We now setup variations on `comp_right_* f`, where `f : C(X, Y)` (that is, precomposition by a continuous map), as a morphism `C(Y, T) → C(X, T)`, respecting various types of structure. In particular: * `comp_right_continuous_map`, the bundled continuous map (for this we need `X Y` compact). * `comp_right_homeomorph`, when we precompose by a homeomorphism. * `comp_right_alg_hom`, when `T = R` is a topological ring. -/ section comp_right /-- Precomposition by a continuous map is itself a continuous map between spaces of continuous maps. -/ def comp_right_continuous_map {X Y : Type*} (T : Type*) [topological_space X] [compact_space X] [topological_space Y] [compact_space Y] [normed_group T] (f : C(X, Y)) : C(C(Y, T), C(X, T)) := { to_fun := λ g, g.comp f, continuous_to_fun := begin refine metric.continuous_iff.mpr _, intros g ε ε_pos, refine ⟨ε, ε_pos, λ g' h, _⟩, rw continuous_map.dist_lt_iff _ _ ε_pos at h ⊢, { exact λ x, h (f x), }, end } @[simp] lemma comp_right_continuous_map_apply {X Y : Type*} (T : Type*) [topological_space X] [compact_space X] [topological_space Y] [compact_space Y] [normed_group T] (f : C(X, Y)) (g : C(Y, T)) : (comp_right_continuous_map T f) g = g.comp f := rfl /-- Precomposition by a homeomorphism is itself a homeomorphism between spaces of continuous maps. -/ def comp_right_homeomorph {X Y : Type*} (T : Type*) [topological_space X] [compact_space X] [topological_space Y] [compact_space Y] [normed_group T] (f : X ≃ₜ Y) : C(Y, T) ≃ₜ C(X, T) := { to_fun := comp_right_continuous_map T f.to_continuous_map, inv_fun := comp_right_continuous_map T f.symm.to_continuous_map, left_inv := by tidy, right_inv := by tidy, } /-- Precomposition of functions into a normed ring by continuous map is an algebra homomorphism. -/ def comp_right_alg_hom {X Y : Type*} (R : Type*) [topological_space X] [topological_space Y] [normed_comm_ring R] (f : C(X, Y)) : C(Y, R) →ₐ[R] C(X, R) := { to_fun := λ g, g.comp f, map_zero' := by { ext, simp, }, map_add' := λ g₁ g₂, by { ext, simp, }, map_one' := by { ext, simp, }, map_mul' := λ g₁ g₂, by { ext, simp, }, commutes' := λ r, by { ext, simp, }, } @[simp] lemma comp_right_alg_hom_apply {X Y : Type*} (R : Type*) [topological_space X] [topological_space Y] [normed_comm_ring R] (f : C(X, Y)) (g : C(Y, R)) : (comp_right_alg_hom R f) g = g.comp f := rfl lemma comp_right_alg_hom_continuous {X Y : Type*} (R : Type*) [topological_space X] [compact_space X] [topological_space Y] [compact_space Y] [normed_comm_ring R] (f : C(X, Y)) : continuous (comp_right_alg_hom R f) := begin change continuous (comp_right_continuous_map R f), continuity, end end comp_right end continuous_map
2d1b6c3b4b1f2d49f47fbf1d20450563094185f9
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/show1.lean
b57e62dc33cbf3868f325b58e3b8d217e144f518
[ "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
356
lean
import logic open bool eq.ops tactic constants a b c : bool axiom H1 : a = b axiom H2 : b = c check show a = c, from H1 ⬝ H2 print "------------" check have e1 : a = b, from H1, have e2 : a = c, by apply eq.trans; apply e1; apply H2, have e3 : c = a, from e2⁻¹, have e4 : b = a, from e1⁻¹, show b = c, from e1⁻¹ ⬝ e2
752140ebc8e1c634390c38591f659ccc6d677857
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/coeSort2.lean
29452dd0c773717ed98e380e2c34a2d26d25de9a
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
263
lean
new_frontend universe u structure Group := (carrier : Type u) (mul : carrier → carrier → carrier) (one : carrier) instance GroupToType : CoeSort Group (Type u) := CoeSort.mk (fun g => g.carrier) variable (g : Group.{1}) #check fun (a b : g) => g.mul a b
eefd7a8d47c882727298fede3fd1df73925e0891
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Util/WHNF.lean
6e0bcae800ae2827b01b0e14d77ee542684e45ff
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
15,707
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.ToExpr import Init.Lean.Declaration import Init.Lean.LocalContext namespace Lean namespace WHNF /- =========================== Smart unfolding support =========================== -/ def smartUnfoldingSuffix := "_sunfold" @[inline] def mkSmartUnfoldingNameFor (n : Name) : Name := mkNameStr n smartUnfoldingSuffix /- =========================== Helper functions =========================== -/ @[inline] def matchConstAux {α : Type} {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (e : Expr) (failK : Unit → m α) (k : ConstantInfo → List Level → m α) : m α := match e with | Expr.const name lvls _ => do (some cinfo) ← getConst name | failK (); k cinfo lvls | _ => failK () /- =========================== Helper functions for reducing recursors =========================== -/ private def getFirstCtor {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (d : Name) : m (Option Name) := do some (ConstantInfo.inductInfo { ctors := ctor::_, ..}) ← getConst d | pure none; pure (some ctor) private def mkNullaryCtor {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (type : Expr) (nparams : Nat) : m (Option Expr) := match type.getAppFn with | Expr.const d lvls _ => do (some ctor) ← getFirstCtor getConst d | pure none; pure $ mkAppN (mkConst ctor lvls) (type.getAppArgs.shrink nparams) | _ => pure none def toCtorIfLit : Expr → Expr | Expr.lit (Literal.natVal v) _ => if v == 0 then mkConst `Nat.zero else mkApp (mkConst `Nat.succ) (mkNatLit (v-1)) | Expr.lit (Literal.strVal v) _ => mkApp (mkConst `String.mk) (toExpr v.toList) | e => e private def getRecRuleFor (rec : RecursorVal) (major : Expr) : Option RecursorRule := match major.getAppFn with | Expr.const fn _ _ => rec.rules.find? $ fun r => r.ctor == fn | _ => none @[specialize] private def toCtorWhenK {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (whnf : Expr → m Expr) (inferType : Expr → m Expr) (isDefEq : Expr → Expr → m Bool) (rec : RecursorVal) (major : Expr) : m (Option Expr) := do majorType ← inferType major; majorType ← whnf majorType; let majorTypeI := majorType.getAppFn; if !majorTypeI.isConstOf rec.getInduct then pure none else if majorType.hasExprMVar && majorType.getAppArgs.anyFrom rec.nparams Expr.hasExprMVar then pure none else do (some newCtorApp) ← mkNullaryCtor getConst majorType rec.nparams | pure none; newType ← inferType newCtorApp; defeq ← isDefEq majorType newType; pure $ if defeq then newCtorApp else none /-- Auxiliary function for reducing recursor applications. -/ @[specialize] def reduceRec {α} {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (whnf : Expr → m Expr) (inferType : Expr → m Expr) (isDefEq : Expr → Expr → m Bool) (rec : RecursorVal) (recLvls : List Level) (recArgs : Array Expr) (failK : Unit → m α) (successK : Expr → m α) : m α := let majorIdx := rec.getMajorIdx; if h : majorIdx < recArgs.size then do let major := recArgs.get ⟨majorIdx, h⟩; major ← whnf major; major ← if !rec.k then pure major else do { newMajor ← toCtorWhenK getConst whnf inferType isDefEq rec major; pure (newMajor.getD major) }; let major := toCtorIfLit major; match getRecRuleFor rec major with | some rule => let majorArgs := major.getAppArgs; if recLvls.length != rec.lparams.length then failK () else let rhs := rule.rhs.instantiateLevelParams rec.lparams recLvls; -- Apply parameters, motives and minor premises from recursor application. let rhs := mkAppRange rhs 0 (rec.nparams+rec.nmotives+rec.nminors) recArgs; /- The number of parameters in the constructor is not necessarily equal to the number of parameters in the recursor when we have nested inductive types. -/ let nparams := majorArgs.size - rule.nfields; let rhs := mkAppRange rhs nparams majorArgs.size majorArgs; let rhs := mkAppRange rhs (majorIdx + 1) recArgs.size recArgs; successK rhs | none => failK () else failK () @[specialize] def isRecStuck? {m : Type → Type} [Monad m] (whnf : Expr → m Expr) (isStuck? : Expr → m (Option MVarId)) (rec : RecursorVal) (recLvls : List Level) (recArgs : Array Expr) : m (Option MVarId) := if rec.k then -- TODO: improve this case pure none else do let majorIdx := rec.getMajorIdx; if h : majorIdx < recArgs.size then do let major := recArgs.get ⟨majorIdx, h⟩; major ← whnf major; isStuck? major else pure none /- =========================== Helper functions for reducing Quot.lift and Quot.ind =========================== -/ /-- Auxiliary function for reducing `Quot.lift` and `Quot.ind` applications. -/ @[specialize] def reduceQuotRec {α} {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (whnf : Expr → m Expr) (rec : QuotVal) (recLvls : List Level) (recArgs : Array Expr) (failK : Unit → m α) (successK : Expr → m α) : m α := let process (majorPos argPos : Nat) : m α := if h : majorPos < recArgs.size then do let major := recArgs.get ⟨majorPos, h⟩; major ← whnf major; match major with | Expr.app (Expr.app (Expr.app (Expr.const majorFn _ _) _ _) _ _) majorArg _ => do some (ConstantInfo.quotInfo { kind := QuotKind.ctor, .. }) ← getConst majorFn | failK (); let f := recArgs.get! argPos; let r := mkApp f majorArg; let recArity := majorPos + 1; successK $ mkAppRange r recArity recArgs.size recArgs | _ => failK () else failK (); match rec.kind with | QuotKind.lift => process 5 3 | QuotKind.ind => process 4 3 | _ => failK () @[specialize] def isQuotRecStuck? {m : Type → Type} [Monad m] (whnf : Expr → m Expr) (isStuck? : Expr → m (Option MVarId)) (rec : QuotVal) (recLvls : List Level) (recArgs : Array Expr) : m (Option MVarId) := let process? (majorPos : Nat) : m (Option MVarId) := if h : majorPos < recArgs.size then do let major := recArgs.get ⟨majorPos, h⟩; major ← whnf major; isStuck? major else pure none; match rec.kind with | QuotKind.lift => process? 5 | QuotKind.ind => process? 4 | _ => pure none /- =========================== Helper function for extracting "stuck term" =========================== -/ /-- Return `some (Expr.mvar mvarId)` if metavariable `mvarId` is blocking reduction. -/ @[specialize] partial def getStuckMVar? {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (whnf : Expr → m Expr) : Expr → m (Option MVarId) | Expr.mdata _ e _ => getStuckMVar? e | Expr.proj _ _ e _ => do e ← whnf e; getStuckMVar? e | e@(Expr.mvar mvarId _) => pure (some mvarId) | e@(Expr.app f _ _) => let f := f.getAppFn; match f with | Expr.mvar mvarId _ => pure (some mvarId) | Expr.const fName fLvls _ => do cinfo? ← getConst fName; match cinfo? with | some $ ConstantInfo.recInfo rec => isRecStuck? whnf getStuckMVar? rec fLvls e.getAppArgs | some $ ConstantInfo.quotInfo rec => isQuotRecStuck? whnf getStuckMVar? rec fLvls e.getAppArgs | _ => pure none | _ => pure none | _ => pure none /- =========================== Weak Head Normal Form auxiliary combinators =========================== -/ /-- Auxiliary combinator for handling easy WHNF cases. It takes a function for handling the "hard" cases as an argument -/ @[specialize] partial def whnfEasyCases {m : Type → Type} [Monad m] (getLocalDecl : Name → m LocalDecl) (getMVarAssignment : Name → m (Option Expr)) : Expr → (Expr → m Expr) → m Expr | e@(Expr.forallE _ _ _ _), _ => pure e | e@(Expr.lam _ _ _ _), _ => pure e | e@(Expr.sort _ _), _ => pure e | e@(Expr.lit _ _), _ => pure e | e@(Expr.bvar _ _), _ => unreachable! | Expr.mdata _ e _, k => whnfEasyCases e k | e@(Expr.letE _ _ _ _ _), k => k e | e@(Expr.fvar fvarId _), k => do decl ← getLocalDecl fvarId; match decl.value? with | none => pure e | some v => whnfEasyCases v k | e@(Expr.mvar mvarId _), k => do v? ← getMVarAssignment mvarId; match v? with | some v => whnfEasyCases v k | none => pure e | e@(Expr.const _ _ _), k => k e | e@(Expr.app _ _ _), k => k e | e@(Expr.proj _ _ _ _), k => k e | Expr.localE _ _ _ _, _ => unreachable! /-- Return true iff term is of the form `idRhs ...` -/ private def isIdRhsApp (e : Expr) : Bool := e.isAppOf `idRhs /-- (@idRhs T f a_1 ... a_n) ==> (f a_1 ... a_n) -/ private def extractIdRhs (e : Expr) : Expr := if !isIdRhsApp e then e else let args := e.getAppArgs; if args.size < 2 then e else mkAppRange (args.get! 1) 2 args.size args @[specialize] private def deltaDefinition {α} (c : ConstantInfo) (lvls : List Level) (failK : Unit → α) (successK : Expr → α) : α := if c.lparams.length != lvls.length then failK () else let val := c.instantiateValueLevelParams lvls; successK (extractIdRhs val) @[specialize] private def deltaBetaDefinition {α} (c : ConstantInfo) (lvls : List Level) (revArgs : Array Expr) (failK : Unit → α) (successK : Expr → α) : α := if c.lparams.length != lvls.length then failK () else let val := c.instantiateValueLevelParams lvls; let val := val.betaRev revArgs; successK (extractIdRhs val) /-- Apply beta-reduction, zeta-reduction (i.e., unfold let local-decls), iota-reduction, expand let-expressions, expand assigned meta-variables. This method does *not* apply delta-reduction at the head symbol `f` unless `isAuxDef? f` returns true. Reason: we want to perform these reductions lazily at `isDefEq`. -/ @[specialize] partial def whnfCore {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (isAuxDef? : Name → m Bool) (whnf : Expr → m Expr) (inferType : Expr → m Expr) (isDefEq : Expr → Expr → m Bool) (getLocalDecl : FVarId → m LocalDecl) (getMVarAssignment : MVarId → m (Option Expr)) : Expr → m Expr | e => whnfEasyCases getLocalDecl getMVarAssignment e $ fun e => match e with | e@(Expr.const _ _ _) => pure e | e@(Expr.letE _ _ v b _) => whnfCore $ b.instantiate1 v | e@(Expr.app f _ _) => do let f := f.getAppFn; f' ← whnfCore f; if f'.isLambda then let revArgs := e.getAppRevArgs; whnfCore $ f'.betaRev revArgs else do let done : Unit → m Expr := fun _ => if f == f' then pure e else pure $ e.updateFn f'; matchConstAux getConst f' done $ fun cinfo lvls => match cinfo with | ConstantInfo.recInfo rec => reduceRec getConst whnf inferType isDefEq rec lvls e.getAppArgs done whnfCore | ConstantInfo.quotInfo rec => reduceQuotRec getConst whnf rec lvls e.getAppArgs done whnfCore | c@(ConstantInfo.defnInfo _) => do unfold? ← isAuxDef? c.name; if unfold? then deltaBetaDefinition c lvls e.getAppRevArgs done whnfCore else done () | _ => done () | e@(Expr.proj _ i c _) => do c ← whnf c; matchConstAux getConst c.getAppFn (fun _ => pure e) $ fun cinfo lvls => match cinfo with | ConstantInfo.ctorInfo ctorVal => pure $ c.getArgD (ctorVal.nparams + i) e | _ => pure e | _ => unreachable! /-- Similar to `whnfCore`, but uses `synthesizePending` to (try to) synthesize metavariables that are blocking reduction. -/ @[specialize] private partial def whnfCoreUnstuck {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (isAuxDef? : Name → m Bool) (whnf : Expr → m Expr) (inferType : Expr → m Expr) (isDefEq : Expr → Expr → m Bool) (synthesizePending : MVarId → m Bool) (getLocalDecl : FVarId → m LocalDecl) (getMVarAssignment : MVarId → m (Option Expr)) : Expr → m Expr | e => do e ← whnfCore getConst isAuxDef? whnf inferType isDefEq getLocalDecl getMVarAssignment e; (some mvarId) ← getStuckMVar? getConst whnf e | pure e; succeeded ← synthesizePending mvarId; if succeeded then whnfCoreUnstuck e else pure e /-- Unfold definition using "smart unfolding" if possible. -/ @[specialize] def unfoldDefinitionAux {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (isAuxDef? : Name → m Bool) (whnf : Expr → m Expr) (inferType : Expr → m Expr) (isDefEq : Expr → Expr → m Bool) (synthesizePending : MVarId → m Bool) (getLocalDecl : FVarId → m LocalDecl) (getMVarAssignment : MVarId → m (Option Expr)) (e : Expr) : m (Option Expr) := match e with | Expr.app f _ _ => matchConstAux getConst f.getAppFn (fun _ => pure none) $ fun fInfo fLvls => if fInfo.lparams.length != fLvls.length then pure none else do fAuxInfo? ← getConst (mkSmartUnfoldingNameFor fInfo.name); match fAuxInfo? with | some $ fAuxInfo@(ConstantInfo.defnInfo _) => deltaBetaDefinition fAuxInfo fLvls e.getAppRevArgs (fun _ => pure none) $ fun e₁ => do e₂ ← whnfCoreUnstuck getConst isAuxDef? whnf inferType isDefEq synthesizePending getLocalDecl getMVarAssignment e₁; if isIdRhsApp e₂ then pure (some (extractIdRhs e₂)) else pure none | _ => if fInfo.hasValue then deltaBetaDefinition fInfo fLvls e.getAppRevArgs (fun _ => pure none) (fun e => pure (some e)) else pure none | Expr.const name lvls _ => do (some (cinfo@(ConstantInfo.defnInfo _))) ← getConst name | pure none; deltaDefinition cinfo lvls (fun _ => pure none) (fun e => pure (some e)) | _ => pure none /- Reference implementation for `whnf`. It does not cache any results. How to use: - `getConst constName` retrieves `constName` from environment. Caller may make definitions opaque by returning `none`. - `isAuxDef? constName` returns `true` is `constName` is an auxiliary declaration automatically generated by Lean and used by equation compiler, and must be eagerly reduced by `whnfCore`. This method is usually implemented using `isAuxRecursor`. - `synthesizePending` is used to (try to) synthesize synthetic metavariables that may be blocking reduction. The other parameters should be self explanatory. -/ @[specialize] partial def whnfMain {m : Type → Type} [Monad m] (getConst : Name → m (Option ConstantInfo)) (isAuxDef? : Name → m Bool) (inferType : Expr → m Expr) (isDefEq : Expr → Expr → m Bool) (synthesizePending : MVarId → m Bool) (getLocalDecl : FVarId → m LocalDecl) (getMVarAssignment : MVarId → m (Option Expr)) : Expr → m Expr | e => do e ← whnfCore getConst isAuxDef? whnfMain inferType isDefEq getLocalDecl getMVarAssignment e; e? ← unfoldDefinitionAux getConst isAuxDef? whnfMain inferType isDefEq synthesizePending getLocalDecl getMVarAssignment e; match e? with | some e => whnfMain e | none => pure e end WHNF end Lean
1ce99cb5d9fc5ca32810411e57f72b142d04d96f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/aexp.lean
fe09a219efb7c6c7b720070174ec6f036455ac2e
[ "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
1,663
lean
namespace imp open tactic @[reducible] def uname := string inductive aexp | val : nat → aexp | var : uname → aexp | plus : aexp → aexp → aexp | times : aexp → aexp → aexp instance : decidable_eq aexp := by mk_dec_eq_instance -- @[reducible] -- def value := nat def state := uname → nat open aexp def aval : aexp → state → nat | (val n) s := n | (var x) s := s x | (plus a₁ a₂) s := aval a₁ s + aval a₂ s | (times a₁ a₂) s := aval a₁ s * aval a₂ s example : aval (plus (val 3) (var "x")) (λ x, 0) = 3 := rfl def updt (s : state) (x : uname) (v : nat) : state := λ y, if x = y then v else s y def asimp_const : aexp → aexp | (val n) := val n | (var x) := var x | (plus a₁ a₂) := match asimp_const a₁, asimp_const a₂ with | val n₁, val n₂ := val (n₁ + n₂) | b₁, b₂ := plus b₁ b₂ end | (times a₁ a₂) := match asimp_const a₁, asimp_const a₂ with | val n₁, val n₂ := val (n₁ * n₂) | b₁, b₂ := times b₁ b₂ end example : asimp_const (plus (plus (val 2) (val 3)) (var "x")) = plus (val 5) (var "x") := rfl attribute [ematch] asimp_const aval set_option trace.smt.ematch true lemma aval_asimp_const (a : aexp) (s : state) : aval (asimp_const a) s = aval a s := begin [smt] induction a, all_goals {destruct (asimp_const a_ᾰ_1), all_goals {destruct (asimp_const a_ᾰ), eblast}} end lemma ex2 (a : aexp) (s : state) : aval (asimp_const a) s = aval a s := begin [smt] induction a, all_goals {destruct (asimp_const a_ᾰ_1), all_goals {destruct (asimp_const a_ᾰ), eblast_using [asimp_const, aval]}} end end imp
58d7b1440c5573e90cd3e1b92183d533c6e57a7f
390c7d44c0470f935f1ff06637607521791400da
/auto.lean
65657ff1f78676e83961fd6fbf34ee14162e3859
[]
no_license
avigad/auto
32baee2744dd3ae53f1cffd3f04523b953e5a9eb
1aa4ebf60f900e8d61bb423105592a4010fa6ec5
refs/heads/master
1,594,791,920,875
1,475,608,824,000
1,475,608,824,000
67,415,120
2
0
null
null
null
null
UTF-8
Lean
false
false
45,708
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Automated tableaux reasoners, inspired by the ones in Isabelle. clarify : applies safe rules only, doesn't split goals safe : applies safe rules only, though it may split goals auto : applies safe rules, and then does a backtracking search with unsafe rules. All can be called in classical or intuitionistic mode, and all can use the simplifier as well to simplify the goal and hypotheses. To do: - use attribute manager to keep track of rules - use tactics to build introduction and elimination rules automatically - need version of apply that doesn't instantiate metavariables - bound split depth in safe, backtracking depth in force, and iterations in clarify - rules should match goal w/ or w/out reduction to whnf (reducible) - improve tracing output by keeping track of backtracking depth - provide better error messages when the name of a theorem doesn't exist; for example, replace mk_const with mk_const_with_warning (when installing the rule?) - write a splitter, to help simp - use a backtracking version of reflexivity - do more instantiations of quantifiers - with bintro and belim rules, "num_subgoals" is really "num_branches" - for intuitionistic logic, add a safe elimination rule for A → B, A, and also for A ∨ B → C, and similarly for (∃ x, A x) → C - in fact, for intuitionistic logic, add the rules for Dyckhoff's LJT - add safe rules for quantifiers, e.g. ∀ x, P x |- P a Questions: - In backtracking search, use better selection of rules, e.g. to chose those with fewest subgoals? - Should calls to simplifier use clarify as the prover? - Use more sophisticated handling of quantifiers? - Should we ever call cases, or induction? Maybe with user hints? - Better handling of equality? E.g. use symmetry as an elim rule? - When backtracking, can we intelligently detect that some subgoals can be thrown away? For example, in the intuitionistic elim rule for A → B, we may end up not using B. The tactic that handles that can detect this. - Check and remove duplicate hypotheses? Note: the rules are not complete for intuitionistic propositional logic, which may require using an elim rule on A → B multiple times. -/ open expr tactic list nat universe uₗ declare_trace auto set_option trace.auto false /- logic rules for the tableau prover -/ theorem not_or_of_imp {A B : Prop} (H : A → B) : ¬ A ∨ B := or.elim (classical.em A) (λ H', or.inr (H H')) (λ H', or.inl H') lemma classical_swap {A : Prop} (B : Prop) (H₁ : ¬ A) (H₂ : ¬ B → A) : B := classical.by_contradiction (λ H, H₁ (H₂ H)) theorem imp_classical_elim {A B C : Prop} (H : A → B) (H₁ : ¬ A → C) (H₂ : B → C) : C := or.elim (not_or_of_imp H) (λ H', H₁ H') (λ H', H₂ H') theorem imp_intuit_elim {A B C : Prop} (H : A → B) (H₁ : A) (H₂ : B → C) : C := H₂ (H H₁) theorem or_classical_intro {A B : Prop} (H : ¬ A → B) : A ∨ B := or.elim (classical.em A) (λ H', or.inl H') (λ H', or.inr (H H')) theorem iff_elim {A B C : Prop} (H : A ↔ B) (H' : (A → B) → (B → A) → C) : C := iff.elim H' H theorem exists.intro2 {A : Type} {P : A → Prop} (a₁ a₂ : A) (H : P a₁ ∨ P a₂) : ∃ x, P x := or.elim H (λ H', exists.intro _ H') (λ H', exists.intro _ H') theorem forall_elim {A : Type} {P : A → Prop} {C : Prop} (H : ∀ x, P x) {y : A} (H' : P y → C) : C := H' (H y) theorem forall_elim2 {A : Type} {P : A → Prop} {C : Prop} (H : ∀ x, P x) {y₁ y₂ : A} (H' : P y₁ ∧ P y₂ → C) : C := H' (and.intro (H y₁) (H y₂)) theorem not_true_elim {C : Prop} (H : ¬ true) : C := false.elim (H trivial) theorem not_of_not_or_left {A B : Prop} (H : ¬ (A ∨ B)) : ¬ A := λ H', H (or.inl H') theorem not_of_not_or_right {A B : Prop} (H : ¬ (A ∨ B)) : ¬ B := λ H', H (or.inr H') theorem forall_not_of_not_exists {A : Type} {P : A → Prop} (H : ¬ ∃ x, P x) : ∀ x, ¬ P x := take x H', H (exists.intro x H') theorem exists_not_of_not_forall {A : Type} {P : A → Prop} (H : ¬ ∀ x, P x) : ∃ x, ¬ P x := classical.by_contradiction (assume H' : ¬ ∃ x, ¬ P x, H (take x, show P x, from classical.by_contradiction (λ H'', H' (exists.intro x H'')))) theorem not_not_dest {A : Prop} (H : ¬ ¬ A) : A := classical.by_contradiction (λ H', H H') theorem not_not_not_dest {A : Prop} (H : ¬ ¬ ¬ A) : ¬ A := λ H', H (λ H'', H'' H') theorem not_not_of_not_imp {A B : Prop} (H : ¬ (A → B)) : ¬ ¬ A := λ H', H (λ H'', absurd H'' H') theorem of_not_imp {A B : Prop} (H : ¬ (A → B)) : A := not_not_dest (not_not_of_not_imp H) theorem not_of_not_imp {A B : Prop} (H : ¬ (A → B)) : ¬ B := λ H', H (λ H'', H') theorem not_or_not_of_not_and {A B : Prop} (H : ¬ (A ∧ B)) : ¬ A ∨ ¬ B := or.elim (classical.em A) (λ HA, or.inr (λ HB, H (and.intro HA HB))) (λ HnA, or.inl HnA) theorem contrapos {A B : Prop} (H : A → B) : ¬ B → ¬ A := λ H₁ H₂, H₁ (H H₂) theorem not_iff {A B : Prop} (H : ¬ (A ↔ B)) : ¬ ((A → B) ∧ (B → A)) := H theorem not_of_imp_false {A : Prop} (H : A → false) : ¬ A := H theorem imp_of_or_imp_left {A B C : Prop} (H : A ∨ B → C) : A → C := λ H', H (or.inl H') theorem imp_of_or_imp_right {A B C : Prop} (H : A ∨ B → C) : B → C := λ H', H (or.inr H') namespace tactic /- utils -/ meta def collect_props : list expr → tactic (list expr) | [] := return [] | (h :: hs) := do props ← collect_props hs, ht ← infer_type h, htt ← infer_type ht, (unify htt prop >> return (h :: props)) <|> return props meta def unfold_all (ns : list name) : tactic unit := do unfold ns, local_context >>= collect_props >>= monad.mapM' (unfold_at ns) meta def head_symbol : expr → name | (const n a) := n | (app e a) := match (get_app_fn e) with | (const n l) := n | a := `none end | (pi a₁ a₂ a₃ a₄) := `pi | a := `none private meta def whnf_red : expr → tactic expr := whnf_core reducible meta def is_forall (e : expr) : tactic bool := if head_symbol e ≠ `pi then return ff else do et ← infer_type e, if et ≠ prop then return ff else do dt ← infer_type (binding_domain e), if dt ≠ prop then return tt else return ff meta def is_negation (e : expr) : tactic bool := do e' ← whnf_red e, if head_symbol e' = `not then return tt else if is_pi e' = tt then (do b' ← whnf_red (binding_body e'), cond (is_false b') (return tt) (return ff)) else return ff meta def at_least_once (t : tactic unit) : tactic unit := t >> repeat t -- assert_fresh P infers the type T of P, creates a fresh name H, and -- asserts H : T meta def assert_fresh (P : expr) : tactic unit := do n ← mk_fresh_name, t ← infer_type P, assertv n t P meta def expr_with_type_to_string (h : expr) : tactic string := do ht ← infer_type h, pph ← pp h, ppht ← pp ht, return (to_string pph ++ " : " ++ to_string ppht) /- versions of the simplifier that call themselves recursively -/ -- simp_add_prove_max_depth l d uses the simplifier as its own prover, recursing up to depth d meta def simp_add_prove_max_depth (lemmas : list expr) : ℕ → tactic unit | 0 := failed | (succ d) := do l ← local_context >>= collect_props, simplify_goal (simp_add_prove_max_depth d) (l ++ lemmas), triv meta def strong_simp_add (lemmas : list expr) : tactic unit := do l ← local_context >>= collect_props, simplify_goal (simp_add_prove_max_depth lemmas 10) (l ++ lemmas), try triv meta def strong_simp : tactic unit := strong_simp_add [] meta def strong_simp_at_add (h : expr) (lemmas : list expr) : tactic unit := do simp_core_at (simp_add_prove_max_depth lemmas 10) lemmas h meta def strong_simp_at (h : expr) : tactic unit := do strong_simp_at_add h [] -- TODO: how to inline this? private meta def strong_simp_hyps_add_aux (lemmas : list expr) : list expr → tactic unit | [] := skip | (h :: hs) := try (strong_simp_at_add h lemmas) >> strong_simp_hyps_add_aux hs meta def strong_simp_hyps_add (lemmas : list expr) : tactic unit := do l ← local_context, strong_simp_hyps_add_aux lemmas l meta def strong_simp_hyps : tactic unit := strong_simp_hyps_add [] /- These are for tracing. We use a thunk to avoid computing a string when it is not needed. -/ -- show a trace message meta def auto_trace (s : unit → string) : tactic unit := if is_trace_enabled_for `auto = tt then trace (s ()) else skip -- a version where the string is in the tactic monad meta def auto_traceM (s : unit → tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then s () >>= trace else skip -- trace a step, e.g. an application of a rule, and show the result meta def auto_trace_step (tac : tactic unit) (s : unit → string) : tactic unit := if is_trace_enabled_for `auto = tt then do trace (s ()), (tac >> trace ("result:") >> trace_state >> trace "-----") <|> (trace ("failed:") >> trace_state >> trace "-----" >> failed) else tac -- a version where the string is in the tactic monad meta def auto_trace_stepM (tac : tactic unit) (s : unit → tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then do s () >>= trace, (tac >> trace ("result:") >> trace_state >> trace "-----") <|> (trace ("failed:") >> trace_state >> trace "-----" >> failed) else tac -- this can be used to print a message after a tactic if it fails, e.g. a continuation. meta def auto_trace_with_fail_message (tac : tactic unit) (s : unit → string) : tactic unit := if is_trace_enabled_for `auto = tt then do tac <|> (trace (s ()) >> failed) else tac meta def auto_trace_with_fail_messageM (tac : tactic unit) (s : unit → tactic string) : tactic unit := if is_trace_enabled_for `auto = tt then do tac <|> (s () >>= trace >> failed) else tac /- Safe versions of some tactics, i.e. tactics that do not instantiate metavariables and hence can be applied in safe mode. -/ -- we really want: e₁ and e₂ can be unified without instantiating metavariables meta def unify_safe_core (t : transparency) (e₁ e₂ : expr) : tactic unit := cond (has_meta_var e₁ || has_meta_var e₂) failed (unify_core t e₁ e₂) meta def unify_safe (e₁ e₂ : expr) : tactic unit := unify_safe_core semireducible e₁ e₂ -- we really want: try to apply e, without instantiation any metavariables in the goal -- maybe we also want the same for fapply? meta def apply_safe_core (t : transparency) (all : bool) (insts : bool) (e : expr) : tactic unit := apply_core t all insts e meta def apply_safe : expr → tactic unit := apply_safe_core semireducible ff tt /- a safe version of assumption -/ meta def find_same_type_safe (e : expr) (l : list expr) : tactic expr := first $ list.for l (λ h, do ht ← infer_type h, unify_safe e ht >> return h) meta def find_hyp_with_type (e : expr) : tactic expr := local_context >>= find_same_type_safe e meta def assumption_safe : tactic unit := do goal ← target, h ← find_hyp_with_type goal, auto_trace_stepM (exact h) (λ u, do s ← expr_with_type_to_string h, return ("applying assumption " ++ s)) /- a safe version of contradiction -/ private meta def contra_A_not_A_safe : list expr → list expr → tactic unit | [] Hs := failed | (H1 :: Rs) Hs := do t_0 ← infer_type H1, t ← whnf t_0, (do a ← match_not t, H2 ← find_same_type_safe a Hs, tgt ← target, pr ← mk_app `absurd [tgt, H2, H1], auto_trace_stepM (exact pr) (λ u, do s2 ← expr_with_type_to_string H2, s1 ← expr_with_type_to_string H1, return ("using contradiction, " ++ s2 ++ ", " ++ s1))) <|> contra_A_not_A_safe Rs Hs meta def contradiction_safe : tactic unit := do ctx ← local_context, contra_A_not_A_safe ctx ctx /- The structure storing a rule has the following data: key : name := the head symbol that triggers the rule num_subgoals : nat := number of subgoals introduced classical : bool := whether to use in classical mode intuit : bool := whether to use in intuitionistic mode tac : ... := the tactic used to execute the rule Notice that all the work is done by tac, which is arbitrary. Helper functions build suitable tactics in common situations, but users can write more complicated ones. All the other data is used to find the rules quickly and decide when to apply them. Currently, the only thing that varies is the type of the tactic, so this is given as a parameter: intro_rule == tactic unit elim_rule == expr → tactic unit bintro_rule == tactic unit → tactic unit belim_rule == tactic unit → expr → tactic unit Intro rules are keyed on the head symbol of the goal. Elim rules are keyed on the head symbol of a hypothesis, and take that hypothesis as an argument. We actually have a separate rule database for rules where they head symbol is a negation, keyed to the next head symbol. The intro and elim rules should be safe, which is to say, they can be applied without backtracking. In the other rules, the letter "b" is for "backtracking." Those rules take continuations that carry out the rest of the search, so that they can backtrack on failure. Note that many some elimination rules that would otherwise be safe become unsafe when there are metavariables involved. For example, applying (or.elim H) is unsafe if H has metavariables; if those metavariables are not instantiated by the end of the search, then the attempt was unsuccessful, and needs to be retracted. So there are both safe and unsafe versions of the rule for or. -/ structure rule_data (A : Type) := (key : name) (num_subgoals : ℕ) (classical : bool) (intuit : bool) (tac : A) meta def rule_key {A : Type} : rule_data A → name := rule_data.key meta def intro_rule : Type := rule_data (tactic unit) meta def elim_rule : Type := rule_data (expr → tactic unit) meta def bintro_rule : Type := rule_data (tactic unit → tactic unit) meta def belim_rule : Type := rule_data (expr → tactic unit → tactic unit) meta def rule_database (A : Type) : Type := rb_lmap name (rule_data A) meta def intro_rule_database : Type := rb_lmap name intro_rule meta def elim_rule_database : Type := rb_lmap name elim_rule meta def nelim_rule_database : Type := rb_lmap name elim_rule meta def bintro_rule_database : Type := rb_lmap name bintro_rule meta def belim_rule_database : Type := rb_lmap name belim_rule meta def bnelim_rule_database : Type := rb_lmap name belim_rule meta def mk_rule_database (A : Type) : rule_database A := rb_lmap.mk _ _ meta def insert_rule {A : Type} (db : rule_database A) (r : rule_data A) : rule_database A := rb_lmap.insert db (rule_key r) r meta def insert_rule_list {A : Type} (db : rule_database A) : list (rule_data A) → rule_database A | [] := db | (r :: rs) := insert_rule (insert_rule_list rs) r meta def initialize_rule_database {A : Type} (l : list (rule_data A)) : rule_database A := insert_rule_list (mk_rule_database A) l meta def find_rules {A : Type} (db : rule_database A) (key : name) : list (rule_data A) := rb_lmap.find db key /- set up attributes -/ meta def intro_rule_database_of_list_name (ns : list name) : tactic (intro_rule_database) := do env ← get_env, rule_list ← monad.forM ns (λ n, do e ← mk_const n, eval_expr intro_rule e), return (initialize_rule_database rule_list) meta def intro_rule_attr : caching_user_attribute (intro_rule_database) := { name := `auto.intro_rule, descr := "intro rule for tableau provers", mk_cache := intro_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``intro_rule_attr meta def elim_rule_database_of_list_name (ns : list name) : tactic (elim_rule_database) := do env ← get_env, rule_list ← monad.forM ns (λ n, do e ← mk_const n, eval_expr elim_rule e), return (initialize_rule_database rule_list) meta def elim_rule_attr : caching_user_attribute (elim_rule_database) := { name := `auto.elim_rule, descr := "elim rule for tableau provers", mk_cache := elim_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``elim_rule_attr meta def nelim_rule_attr : caching_user_attribute (elim_rule_database) := { name := `auto.nelim_rule, descr := "negated elim rule for tableau provers", mk_cache := elim_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``nelim_rule_attr meta def bintro_rule_database_of_list_name (ns : list name) : tactic (bintro_rule_database) := do env ← get_env, rule_list ← monad.forM ns (λ n, do e ← mk_const n, eval_expr bintro_rule e), return (initialize_rule_database rule_list) meta def bintro_rule_attr : caching_user_attribute (bintro_rule_database) := { name := `auto.bintro_rule, descr := "backtracking intro rule for tableau provers", mk_cache := bintro_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``bintro_rule_attr meta def belim_rule_database_of_list_name (ns : list name) : tactic (belim_rule_database) := do env ← get_env, rule_list ← monad.forM ns (λ n, do e ← mk_const n, eval_expr belim_rule e), return (initialize_rule_database rule_list) meta def belim_rule_attr : caching_user_attribute (belim_rule_database) := { name := `auto.belim_rule, descr := "backtracking elim rule for tableau provers", mk_cache := belim_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``belim_rule_attr meta def bnelim_rule_attr : caching_user_attribute (belim_rule_database) := { name := `auto.bnelim_rule, descr := "backtracking negated elim rule for tableau provers", mk_cache := belim_rule_database_of_list_name, dependencies := [] } run_command attribute.register ``bnelim_rule_attr /- intro rules -/ meta def apply_intro_rule (db : intro_rule_database) (max_subgoals : ℕ) (classical : bool) : tactic unit := do goal ← target >>= whnf_red, first $ list.for (find_rules db (head_symbol goal)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r else failed) /- procedures for building particular intro rules -/ meta def deploy_intro (op : name) : tactic unit := auto_trace_step (mk_const op >>= apply) (λ u, "applying introduction " ++ to_string op) meta def deploy_intro_then_intros (op : name) : tactic unit := auto_trace_step (mk_const op >>= apply >> intros >> return ()) (λ u, "applying introduction " ++ to_string op) /- elim rules -/ meta def apply_elim_rule_at (edb : elim_rule_database) (nedb : nelim_rule_database) (h : expr) (max_subgoals : ℕ) (classical : bool) : tactic unit := do ht ← infer_type h >>= whnf_red, (first $ list.for (find_rules edb (head_symbol ht)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h else failed)) <|> if head_symbol ht = `not then do unneg ← return (app_arg ht) >>= whnf_red, first $ list.for (find_rules nedb (head_symbol (unneg))) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h else failed) else failed meta def apply_elim_rule (edb : elim_rule_database) (nedb : nelim_rule_database) (max_subgoals : ℕ) (classical : bool) : tactic unit := do hs ← local_context >>= collect_props, first $ list.for hs (λ h, apply_elim_rule_at edb nedb h max_subgoals classical) /- procedures for building particular elim rules general elimination rules: All the arguments are assumed to be inferrable from the motive and major premise. The rule is not applied if the hypothesis has metavariables -- backtracking is needed for that. destruct rules: This eliminates a hypothesis by applying a single theorem or a list of theorems in the forward direction. The arguments are assume to be inferrable from the premise. It is safe even if the hypothesis has variables. -/ private meta def elim_instance_mapp_args (motive major : ℕ) (emotive emajor : expr) : list (option expr) := let diff := major - motive in nat.rec_on major [] (λ n l, if n = diff then some emotive :: l else if n = 0 then some emajor :: l else none :: l) meta def deploy_elim_at (op : name) (motive : ℕ) (major : ℕ) : expr → tactic unit := λ h : expr, do auto_trace_stepM (do goal ← target, el ← mk_mapp op (elim_instance_mapp_args motive major goal h), clear h, apply el ; (intros >> skip), return ()) (λ u, do s ← expr_with_type_to_string h, return ("applying elimination " ++ to_string op ++ " at " ++ s)) -- only apply the elim rule if there are no metavars meta def deploy_elim_at_safe (op : name) (motive : ℕ) (major : ℕ) : expr → tactic unit := λ h : expr, do ht ← infer_type h, when (has_meta_var ht = tt) failed, deploy_elim_at op motive major h private meta def dest_instance_mapp_args (prem : ℕ) (hyp : expr) : list (option expr) := nat.rec_on (prem - 1) [some hyp] (λ n l, none :: l) meta def deploy_dest_at (op : name) (prem : ℕ) : expr → tactic unit := λ h : expr, auto_trace_stepM (mk_mapp op (dest_instance_mapp_args prem h) >>= assert_fresh >> clear h) (λ u, do s ← expr_with_type_to_string h, return ("applying destructor " ++ to_string op ++ " at " ++ s)) meta def deploy_dests_at (ops : list (name × ℕ)) : expr → tactic unit := λ h : expr, auto_trace_stepM (monad.forM' ops (λ p, mk_mapp (p.1) (dest_instance_mapp_args (p.2) h) >>= assert_fresh) >> clear h) (λ u, do s ← expr_with_type_to_string h, return ("applying destructors " ++ (map prod.fst ops)^.to_string ++ " at " ++ s)) meta def deploy_clear_at : expr → tactic unit := λ h : expr, auto_trace_stepM (clear h) (λ u, do s ← expr_with_type_to_string h, return ("clearing " ++ s)) -- convert (... h : ¬ A ... ==> B) to (... hn : ¬ B ... ==> A), where h' has a fresh name meta def do_classical_swap (h : expr) : tactic expr := do goal ← target, mk_mapp `classical_swap [none, some goal, some h] >>= apply, clear h, mk_fresh_name >>= intro meta def classical_apply_intro_rule_at (db : intro_rule_database) (h : expr) (max_subgoals : ℕ) (classical : bool) : tactic unit := do n ← mk_fresh_name, negated_concl ← do_classical_swap h, apply_intro_rule db max_subgoals classical ; (intros >> do_classical_swap negated_concl >> skip) /- backtracking intro rules -/ meta def apply_bintro_rule (db : bintro_rule_database) (max_subgoals : ℕ) (classical : bool) (cont : tactic unit) : tactic unit := do goal ← target >>= whnf_red, first $ list.for (find_rules db (head_symbol goal)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r cont else failed) /- procedure for building particular bintro rules -/ meta def deploy_bintro_choices (l : list (tactic unit)) : tactic unit → tactic unit := take cont, first $ list.for l (λ t, do auto_trace (λ u, "setting backtracking point for intro rule"), t, auto_trace_with_fail_message cont (λ u, "backtracking intro rule")) /- backtracking elim rules -/ meta def classical_apply_bintro_rule_at (db : bintro_rule_database) (h : expr) (max_subgoals : ℕ) (classical : bool) (cont : tactic unit) : tactic unit := do n ← mk_fresh_name, negated_concl ← do_classical_swap h, apply_bintro_rule db max_subgoals classical (intros >> do_classical_swap negated_concl >> cont) meta def apply_belim_rule_at (bedb : belim_rule_database) (bnedb : belim_rule_database) (h : expr) (max_subgoals : ℕ) (classical : bool) (cont : tactic unit) : tactic unit := do ht ← infer_type h >>= whnf_red, (first $ list.for (find_rules bedb (head_symbol ht)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h cont else failed)) <|> (monad.condM (is_negation ht) (do dt ← infer_type (binding_domain h), first $ list.for (find_rules bnedb (head_symbol dt)) (λ r, if rule_data.num_subgoals r ≤ max_subgoals ∧ cond classical (rule_data.classical r) (rule_data.intuit r) = tt then rule_data.tac r h cont else failed)) failed) meta def apply_belim_rule (bedb : belim_rule_database) (bnedb : belim_rule_database) (max_subgoals : ℕ) (classical : bool) (cont : tactic unit) : tactic unit := do hs ← local_context >>= collect_props, first (list.for hs (λ h, apply_belim_rule_at bedb bnedb h max_subgoals classical cont)) /- procedure for building particular belim rules -/ meta def deploy_belim_choices (l : list (expr → tactic unit)) : expr → tactic unit → tactic unit := take h cont, (first $ list.for l (λ t, do auto_traceM (λ u, do s ← expr_with_type_to_string h, return ("setting backtracking point for elim rule at " ++ s)), t h, auto_trace_with_fail_messageM cont (λ u, do s ← expr_with_type_to_string h, return ("backtracking elim rule at " ++ s)))) /- try to do a subst or injection on a hypothesis -/ meta def has_eq_type (h : expr) : tactic bool := do htype ← infer_type h >>= whnf_red, return (match (expr.is_eq htype) with (some _) := tt | none := ff end) meta def try_subst_and_injection_on_hyps : tactic unit := do ctx ← local_context, first $ list.for ctx (λ h, do b ← has_eq_type h, when (b = ff) failed, (do subst h, auto_trace_stepM skip (λ u, do s ← expr_with_type_to_string h, return ("performing subst with " ++ s)), clear h) <|> (do injection h, auto_trace_stepM skip (λ u, do s ← expr_with_type_to_string h, return ("performing injection with " ++ s)), clear h)) /- Standard rule sets -/ /- standard introduction rules -/ @[auto.intro_rule] meta def true_intro_rule : intro_rule := { key := ``true, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_intro ``true.intro } @[auto.intro_rule] meta def and_intro_rule : intro_rule := { key := ``and, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_intro ``and.intro } @[auto.intro_rule] meta def or_classical_intro_rule : intro_rule := { key := ``or, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_intro_then_intros ``or_classical_intro } -- TODO: eliminate trick to get the recursive call private meta def auto_intros_aux : unit → tactic unit | unit.star := do goal ← target >>= whnf_red, when (head_symbol goal = `pi ∨ head_symbol goal = `not) (do n ← mk_fresh_name, intro n, auto_intros_aux unit.star) meta def auto_intros : tactic unit := auto_intros_aux unit.star meta def deploy_intros : tactic unit := auto_trace_step auto_intros (λ u, "applying intros") @[auto.intro_rule] meta def Pi_intro_rule : intro_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_intros } @[auto.intro_rule] meta def not_intro_rule : intro_rule := { key := ``not, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_intros } @[auto.intro_rule] meta def iff_intro_rule : intro_rule := { key := ``iff, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_intro ``iff.intro } /- standard backtracking intro rules -/ @[auto.bintro_rule] meta def or_intuit_bintro_rule : bintro_rule := { key := ``or, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_bintro_choices [deploy_intro ``or.inl, deploy_intro ``or.inr] } @[auto.bintro_rule] meta def exists_bintro_rule : bintro_rule := { key := ``Exists, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_bintro_choices [deploy_intro ``exists.intro, deploy_intro ``false.elim] } /- standard elimination rules -/ @[auto.elim_rule] meta def and_elim_rule : elim_rule := { key := ``and, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``and.left, 3), (``and.right, 3)] } @[auto.elim_rule] meta def iff_elim_rule : elim_rule := { key := ``iff, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``iff.mp, 3), (``iff.mpr, 3)] } @[auto.elim_rule] meta def or_elim_rule : elim_rule := { key := ``or, num_subgoals := 2, classical := tt, intuit := tt, tac := deploy_elim_at_safe ``or.elim 3 4 } @[auto.elim_rule] meta def false_elim_rule : elim_rule := { key := ``false, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_elim_at ``false.elim 1 2 } @[auto.elim_rule] meta def exists_elim_rule : elim_rule := { key := ``Exists, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_elim_at_safe ``exists.elim 3 4 } -- given a hypothesis h with type ht, an implication, try to find something -- in the context to apply it to meta def try_modus_ponens_at (h : expr) (ht : expr) : tactic unit := do h' ← find_hyp_with_type (binding_domain ht), auto_trace_stepM (assert_fresh (expr.app h h') >> clear h) (λ u, do s₁ ← expr_with_type_to_string h, s₂ ← expr_with_type_to_string h', return ("applying " ++ s₁ ++ " to " ++ s₂)) -- if h is of the form A → B: -- if B = false, replace by h' : ¬ A -- if h' : A is in the context, apply h to h' -- if A if of the form C ∨ D, replace with C → B, D → B meta def deploy_imp_elim_at (h : expr) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt ≠ prop then failed else do conc ← return (binding_body ht) >>= whnf_red, if head_symbol conc = ``false then deploy_dest_at ``not_of_imp_false 2 h else try_modus_ponens_at h ht <|> (do hyp ← return (binding_domain ht) >>= whnf_red, if head_symbol hyp = `or then deploy_dests_at [(``imp_of_or_imp_left, 4), (``imp_of_or_imp_right, 4)] h else failed) @[auto.elim_rule] meta def imp_elim_rule : elim_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_imp_elim_at } meta def deploy_imp_classical_elim_at (h : expr) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt ≠ prop then failed else monad.condM (is_negation ht) failed (deploy_elim_at ``imp_classical_elim 3 4 h) @[auto.elim_rule] meta def imp_classical_elim_rule : elim_rule := { key := `pi, num_subgoals := 2, classical := tt, intuit := ff, tac := deploy_imp_classical_elim_at } -- try to find a contradiction meta def deploy_not_elim_at (h : expr) : tactic unit := do ht ← infer_type h, h' ← find_hyp_with_type (app_arg ht), goal ← target, t ← mk_app `absurd [goal, h', h], auto_trace_stepM (exact t) (λ u, do s₁ ← expr_with_type_to_string h', s₂ ← expr_with_type_to_string h, return ("using contradiction, " ++ s₁ ++ " and " ++ s₂)) @[auto.elim_rule] meta def not_elim_rule : elim_rule := { key := `not, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_not_elim_at } /- elimination rules for negated formulas -/ @[auto.nelim_rule] meta def not_true_elim_rule : elim_rule := { key := ``true, num_subgoals := 0, classical := tt, intuit := tt, tac := deploy_elim_at ``not_true_elim 1 2 } @[auto.nelim_rule] meta def not_or_elim_rule : elim_rule := { key := ``or, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dests_at [(``not_of_not_or_left, 3), (``not_of_not_or_right, 3)] } @[auto.nelim_rule] meta def not_and_elim_rule : elim_rule := { key := ``and, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dest_at ``not_or_not_of_not_and 3 } @[auto.nelim_rule] meta def not_imp_elim_rule : elim_rule := { key := `pi, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dests_at [(``of_not_imp, 3), (``not_of_not_imp, 3)] } @[auto.nelim_rule] meta def not_not_elim_rule : elim_rule := { key := ``not, num_subgoals := 1, classical := tt, intuit := ff, tac := deploy_dest_at ``not_not_dest 2 } @[auto.nelim_rule] meta def not_not_not_elim_rule : elim_rule := { key := ``not, num_subgoals := 1, classical := ff, intuit := tt, tac := deploy_dest_at ``not_not_not_dest 2 } @[auto.nelim_rule] meta def not_iff_elim_rule : elim_rule := { key := ``iff, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dest_at ``not_iff 3 } @[auto.nelim_rule] meta def not_exists_elim_rule : elim_rule := { key := ``Exists, num_subgoals := 1, classical := tt, intuit := tt, tac := deploy_dest_at ``forall_not_of_not_exists 3 } /- standard backtracking elim rules -/ meta def deploy_imp_intuit_belim_at (h : expr) (cont : tactic unit) : tactic unit := do ht ← infer_type h >>= whnf_red, dt ← infer_type (binding_domain ht), if dt ≠ prop then failed else deploy_belim_choices [deploy_elim_at ``imp_intuit_elim 3 4, deploy_clear_at] h cont @[auto.belim_rule] meta def imp_intuit_belim_rule : belim_rule := { key := `pi, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_imp_intuit_belim_at } --meta def imp_classical_belim_rule := --{ key := `pi, num_subgoals := 2, classical := tt, intuit := ff, -- tac := deploy_belim_choices [deploy_clear_at, deploy_imp_classical_elim_at] } @[auto.belim_rule] meta def or_belim_rule : belim_rule := { key := `or, num_subgoals := 2, classical := ff, intuit := tt, tac := deploy_belim_choices [deploy_clear_at, deploy_elim_at ``or.elim 3 4] } /- standard backtracking negated elim rules -/ -- none yet /- backtracking assumption tactic -/ meta def try_assumptions (cont : tactic unit) := do ctx ← local_context, goal ← target, first $ list.for ctx (λ h, do ht ← infer_type h, unify ht goal, auto_trace_stepM (exact h) (λ u, do s ← expr_with_type_to_string h, return ("try applying assumption " ++ s)), auto_trace_with_fail_messageM cont (λ u, do s ← expr_with_type_to_string h, return ("backtracking assumption " ++ s))) meta def try_contradictions (cont : tactic unit) := do ctx ← local_context, goal ← target, first $ list.for ctx (λ h, do ht ← infer_type h, unneg_ht ← match_not ht, first $ list.for ctx (λ h', do ht' ← infer_type h', unify ht' unneg_ht, t ← mk_app ``absurd [goal, h', h], auto_trace_stepM (exact t) (λ u, do s₁ ← expr_with_type_to_string h', s₂ ← expr_with_type_to_string h, return ("try using contradiction " ++ s₁ ++ ", " ++ s₂)), auto_trace_with_fail_messageM cont (λ u, do s₁ ← expr_with_type_to_string h', s₂ ← expr_with_type_to_string h, return ("backtracking contradiction " ++ s₁ ++ ", " ++ s₂)))) /- instantiating quantifiers in the backtracking search -/ meta def has_forall_type (h : expr) : tactic bool := do ht ← infer_type h, is_forall ht -- TODO: eliminate meta def apply_to_metavars_while_universal_aux : unit → expr → tactic expr | unit.star h := do ht ← infer_type h, if head_symbol ht ≠ `pi then return h else do htt ← infer_type ht, if htt ≠ prop then return h else do dt ← infer_type (binding_domain ht), if dt = prop then return h else do v ← mk_meta_var (binding_domain ht), apply_to_metavars_while_universal_aux unit.star (expr.app h v) meta def apply_to_metavars_while_universal (h : expr) : tactic expr := apply_to_metavars_while_universal_aux unit.star h meta def try_instantiate_quantifiers (cont : tactic unit) : tactic unit := do hs ← (local_context >>= monad.filterM has_forall_type), gt ← target, when (hs = []/- ∧ head_symbol gt ≠ `Exists-/) failed, monad.forM' hs (λ h, do h' ← apply_to_metavars_while_universal h, assert_fresh h'), -- when (head_symbol gt = `Exists) split, monad.forM' hs clear, monad.whenb (is_trace_enabled_for `auto) (trace "instantiating quantifiers" >> trace_state >> trace "-----"), cont /- Safe automation. These operate on the first goal, and apply only safe rules (the new state is logically equivalent to the original one). They make whatever progress they can, and leave the user with zero or more goals. They take the following arguments: classical : classical or intuitionistic use_simp : whether to use the simplifier simp_lemmas : in the latter case, extra lemmas to use -/ -- perform safe rules that do not split the goal meta def clarify_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : nelim_rule_database) (simp_lemmas : list expr) : tactic unit := do repeat (apply_intro_rule idb 1 classical), repeat (apply_elim_rule edb nedb 1 classical), repeat try_subst_and_injection_on_hyps, (now <|> assumption_safe <|> -- contradiction_safe <|> when (use_simp = tt) (do when_tracing `auto (trace "calling simplifier"), try (strong_simp_hyps_add simp_lemmas), try (strong_simp_add simp_lemmas), try (now <|> assumption_safe))) -- perform safe rules -- TODO: fix recursion meta def safe_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : nelim_rule_database) (simp_lemmas : list expr) : unit → tactic unit | unit.star := do clarify_core classical use_simp idb edb nedb simp_lemmas, now <|> try ((apply_intro_rule idb 10 classical <|> apply_elim_rule edb nedb 10 classical) ; (safe_core /- classical use_simp idb edb nedb simp_lemmas -/ unit.star)) /- The backtracking tableau prover. The function force_all_core_aux is the heart of the tableau prover. It takes a list of goals, which are assumed to have been processed with the safe rules already and are not visible on the goal stack. It applies safe rules to the goals in the current goal list (if any), and then starts calling backtracking rules. This function is meant to be passed as a continuation to the backtracking tactics, which are called with a single goal. The tactics should operate on the goal, resulting in a new goal list. They should then call the continuation to finish the backtracking search. The function succeeds if all the goals are ultimately proved, and it fails otherwise. -/ -- TODO: fix recursion meta def force_all_core_aux (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : elim_rule_database) (bidb : bintro_rule_database) (bedb : belim_rule_database) (bnedb : belim_rule_database) (simp_lemmas : list expr) (final_check : tactic unit) /- (preprocessed_goals : list expr) -/ : unit → list expr → tactic unit | unit.star := let force_core_rec := force_all_core_aux /- classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas final_check -/ unit.star in let process_goals_with_backtracking : list expr → tactic unit := λ l, match l with | [] := final_check -- if it passes, we have success! | (g :: gs) := do {set_goals [g], try_assumptions (force_core_rec gs) <|> try_contradictions (force_core_rec gs) <|> try_instantiate_quantifiers (force_core_rec gs) <|> apply_bintro_rule bidb 10 classical (force_core_rec gs) <|> apply_belim_rule bedb bnedb 10 classical (force_core_rec gs)} end in λ preprocessed_goals, do n ← num_goals, if n ≠ 0 then do all_goals (safe_core classical use_simp idb edb nedb simp_lemmas unit.star), gs ← get_goals, set_goals [], force_all_core_aux /-classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas final_check-/ unit.star (gs ++ preprocessed_goals) else do process_goals_with_backtracking preprocessed_goals meta def final_check_for_metavariables (g : expr) : tactic unit := do result ← instantiate_mvars g, monad.whenb (has_meta_var result) (when_tracing `auto (trace_state >> trace "result has metavariables:" >> trace result) >> failed) -- the main tableaux prover: acts on one goal, makes sure there are no metavariables at the end meta def force_core (classical : bool) (use_simp : bool) (idb : intro_rule_database) (edb : elim_rule_database) (nedb : elim_rule_database) (bidb : bintro_rule_database) (bedb : belim_rule_database) (bnedb : belim_rule_database) (simp_lemmas : list expr) : tactic unit := do auto_trace_step skip (λ u, ">>> entering force"), gs ← get_goals, match gs with | [] := fail "force failed, there are no goals" | (g :: gs') := do set_goals [g], force_all_core_aux classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas (final_check_for_metavariables g) unit.star [], set_goals gs end /- front ends -/ /- -- TODO: a temporary hack: using trace_option to declare a boolean option declare_trace auto.classical set_option trace.auto.classical true declare_trace auto.use_simp set_option trace.auto.use_simp false -/ -- applies to first goal, never splits it, applies only safe rules, always succeeds meta def clarify (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, clarify_core classical use_simp idb edb nedb simp_lemmas -- applies to first goal, applies only safe rules, always succeeds meta def safe (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, safe_core classical use_simp idb edb nedb simp_lemmas unit.star -- applies safe to all goals meta def safe_all (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (simp_lemmas : list expr) : tactic unit := all_goals (safe classical use_simp irules erules nerules simp_lemmas) -- applies to first goal, fails if it does not solve it meta def force (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (birules : list bintro_rule) (berules : list belim_rule) (bnerules : list belim_rule) (simp_lemmas : list expr) : tactic unit := do idb ← caching_user_attribute.get_cache intro_rule_attr, edb ← caching_user_attribute.get_cache elim_rule_attr, nedb ← caching_user_attribute.get_cache nelim_rule_attr, bidb ← caching_user_attribute.get_cache bintro_rule_attr, bedb ← caching_user_attribute.get_cache belim_rule_attr, bnedb ← caching_user_attribute.get_cache bnelim_rule_attr, force_core classical use_simp idb edb nedb bidb bedb bnedb simp_lemmas -- applies to all goals, always succeeds meta def auto (classical : bool) (use_simp : bool) (irules : list intro_rule) (erules : list elim_rule) (nerules : list elim_rule) (birules : list bintro_rule) (berules : list belim_rule) (bnerules : list belim_rule) (simp_lemmas : list expr) : tactic unit := safe_all classical use_simp irules erules nerules simp_lemmas >> all_goals (try (force classical use_simp irules erules nerules birules berules bnerules simp_lemmas)) /- for testing -/ meta def clarify' : tactic unit := clarify tt ff [] [] [] [] meta def safe' : tactic unit := safe tt ff [] [] [] [] meta def ssafe' : tactic unit := safe tt tt [] [] [] [] -- with simplification meta def force' : tactic unit := force tt ff [] [] [] [] [] [] [] meta def sforce' : tactic unit := force tt tt [] [] [] [] [] [] [] meta def auto' : tactic unit := auto tt ff [] [] [] [] [] [] [] meta def sauto' : tactic unit := auto tt tt [] [] [] [] [] [] [] meta def iauto' : tactic unit := auto ff ff [] [] [] [] [] [] [] meta def isauto' : tactic unit := auto ff tt [] [] [] [] [] [] [] end tactic
87acda7d2be6b0f091192c9c124b7b5109ab59cf
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/541b.lean
7aeb0c92752bdb511dfd2346448b5e7b38b412cb
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
1,731
lean
import data.list inductive typ : Type := | nat : typ | arr : typ → typ → typ inductive const : Type := | z | s inductive exp : Type := | var : nat → exp | cnst : const → exp | lam : nat → typ → exp → exp | ap : exp → exp → exp open exp inductive direct_subterm : exp → exp → Prop := | lam : ∀ n t e, direct_subterm e (lam n t e) | ap_fn : ∀ f a, direct_subterm f (ap f a) | ap_arg : ∀ f a, direct_subterm a (ap f a) theorem direct_subterm_wf : well_founded direct_subterm := begin constructor, intro e, induction e, repeat (constructor; intro y hlt; cases hlt; repeat assumption) end definition subterm := tc direct_subterm theorem subterm_wf : well_founded subterm := tc.wf direct_subterm_wf inductive is_val : exp → Prop := | vcnst : Π c, is_val (cnst c) | vlam : Π x t e, is_val (lam x t e) | vcnst_ap : Π {e} c, is_val e → is_val (ap (cnst c) e) inductive step : exp → exp → Prop := infix `➤`:50 := step | stepl : Π {e1 e1'} e2, e1 ➤ e1' → ap e1 e2 ➤ ap e1' e2 | stepr : Π {e1 e2 e2'}, is_val e1 → e2 ➤ e2' → ap e1 e2 ➤ ap e1 e2' | subst : Π {x e1 e1' e2} t, is_val e2 → ap (lam x t e1) e2 ➤ e1' infix `➤`:50 := step open is_val open step theorem nostep : ∀ {e} e', is_val e → e ➤ e' → false | nostep e' (vcnst c) Hsteps := by cases Hsteps | nostep e' (vlam x t e) Hsteps := by cases Hsteps | nostep (ap e' e) (@vcnst_ap e c Hval) (stepl e Hbad) := have Hvalc : is_val (cnst c), from vcnst c, have IH : not (cnst c ➤ e'), from nostep e' Hvalc, absurd Hbad IH | nostep (ap (cnst c) e') (@vcnst_ap e c Hvale) (stepr Hvalc Hbad) := have IH : not (e ➤ e'), from nostep e' Hvale, absurd Hbad IH
c835a8dd6a5d157f0b271bef981648401a8e632e
acc85b4be2c618b11fc7cb3005521ae6858a8d07
/algebra/field.lean
99bfd8135c2b7dcbb2f9b799cc1164c6bd096881
[ "Apache-2.0" ]
permissive
linpingchuan/mathlib
d49990b236574df2a45d9919ba43c923f693d341
5ad8020f67eb13896a41cc7691d072c9331b1f76
refs/heads/master
1,626,019,377,808
1,508,048,784,000
1,508,048,784,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,003
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import data.set algebra.group open set universe u variables {α : Type u} section division_ring variables [division_ring α] {a b : α} lemma division_ring.neg_inv (h : a ≠ 0) : - a⁻¹ = (- a)⁻¹ := by rwa [inv_eq_one_div, inv_eq_one_div, div_neg_eq_neg_div] end division_ring section variables [discrete_field α] {a b c : α} lemma inv_sub_inv_eq (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ - b⁻¹ = (b - a) / (a * b) := have a * b ≠ 0, by simp [mul_eq_zero_iff_eq_zero_or_eq_zero, ha, hb], calc (a⁻¹ - b⁻¹) = ((a⁻¹ - b⁻¹) * (a * b)) / (a * b) : by rwa [mul_div_cancel] ... = _ : begin simp [mul_add, add_mul, hb], rw [mul_comm a, mul_assoc, mul_comm a⁻¹, mul_inv_cancel ha], simp end end section variables [linear_ordered_field α] {a b c : α} lemma le_div_iff_mul_le_of_pos (hc : 0 < c) : a ≤ b / c ↔ a * c ≤ b := ⟨mul_le_of_le_div hc, le_div_of_mul_le hc⟩ lemma div_le_iff_le_mul_of_pos (hb : 0 < b) : a / b ≤ c ↔ a ≤ c * b := ⟨le_mul_of_div_le hb, by rw [mul_comm]; exact div_le_of_le_mul hb⟩ lemma lt_div_iff (h : 0 < c) : a < b / c ↔ a * c < b := ⟨mul_lt_of_lt_div h, lt_div_of_mul_lt h⟩ lemma ivl_translate : (λx, x + c) '' {r:α | a ≤ r ∧ r ≤ b } = {r:α | a + c ≤ r ∧ r ≤ b + c} := calc (λx, x + c) '' {r | a ≤ r ∧ r ≤ b } = (λx, x - c) ⁻¹' {r | a ≤ r ∧ r ≤ b } : congr_fun (image_eq_preimage_of_inverse (assume a, add_sub_cancel a c) (assume b, sub_add_cancel b c)) _ ... = {r | a + c ≤ r ∧ r ≤ b + c} : set.ext $ by simp [-sub_eq_add_neg, le_sub_iff_add_le, sub_le_iff_le_add] lemma ivl_stretch (hc : 0 < c) : (λx, x * c) '' {r | a ≤ r ∧ r ≤ b } = {r | a * c ≤ r ∧ r ≤ b * c} := calc (λx, x * c) '' {r | a ≤ r ∧ r ≤ b } = (λx, x / c) ⁻¹' {r | a ≤ r ∧ r ≤ b } : congr_fun (image_eq_preimage_of_inverse (assume a, mul_div_cancel _ $ ne_of_gt hc) (assume b, div_mul_cancel _ $ ne_of_gt hc)) _ ... = {r | a * c ≤ r ∧ r ≤ b * c} : set.ext $ by simp [le_div_iff_mul_le_of_pos, div_le_iff_le_mul_of_pos, hc] instance linear_ordered_field.to_densely_ordered [linear_ordered_field α] : densely_ordered α := { dense := assume a₁ a₂ h, ⟨(a₁ + a₂) / 2, calc a₁ = (a₁ + a₁) / 2 : (add_self_div_two a₁).symm ... < (a₁ + a₂) / 2 : div_lt_div_of_lt_of_pos (add_lt_add_left h _) two_pos, calc (a₁ + a₂) / 2 < (a₂ + a₂) / 2 : div_lt_div_of_lt_of_pos (add_lt_add_right h _) two_pos ... = a₂ : add_self_div_two a₂⟩ } instance linear_ordered_field.to_no_top_order [linear_ordered_field α] : no_top_order α := { no_top := assume a, ⟨a + 1, lt_add_of_le_of_pos (le_refl a) zero_lt_one ⟩ } instance linear_ordered_field.to_no_bot_order [linear_ordered_field α] : no_bot_order α := { no_bot := assume a, ⟨a + -1, add_lt_of_le_of_neg (le_refl _) (neg_lt_of_neg_lt $ by simp [zero_lt_one]) ⟩ } lemma inv_pos {a : α} : 0 < a → 0 < a⁻¹ := by rw [inv_eq_one_div]; exact div_pos_of_pos_of_pos zero_lt_one lemma inv_lt_one {a : α} (ha : 1 < a) : a⁻¹ < 1 := by rw [inv_eq_one_div]; exact div_lt_of_mul_lt_of_pos (lt_trans zero_lt_one ha) (by simp *) lemma one_lt_inv (h₁ : 0 < a) (h₂ : a < 1) : 1 < a⁻¹ := by rw [inv_eq_one_div, lt_div_iff h₁]; simp [h₂] end section variables [discrete_linear_ordered_field α] (a b c: α) lemma abs_inv : abs a⁻¹ = (abs a)⁻¹ := have h : abs (1 / a) = 1 / abs a, begin rw [abs_div, abs_of_nonneg], exact zero_le_one end, by simp [*] at * lemma inv_neg : (-a)⁻¹ = -(a⁻¹) := if h : a = 0 then by simp [h, inv_zero] else by rwa [inv_eq_one_div, inv_eq_one_div, div_neg_eq_neg_div] lemma inv_le_inv (hb : 0 < b) (h : b ≤ a) : a⁻¹ ≤ b⁻¹ := begin rw [inv_eq_one_div, inv_eq_one_div], exact one_div_le_one_div_of_le hb h end end
ee867fd4a04543b06e72d3d62f15344eecf834ed
fbf512ee44de430e3d3c5869751ad95325c938d7
/nnf.lean
c322eb7a0054b99a3e61140fa8288322239d558f
[]
no_license
skbaek/clausify
4858c9005fb86a5e410bfcaa77524f82d955f655
d09b071bdcce7577c3fffacd0893b776285b1590
refs/heads/master
1,588,360,590,818
1,553,553,880,000
1,553,553,880,000
177,644,542
0
0
null
null
null
null
UTF-8
Lean
false
false
1,464
lean
import .form variables {α : Type} namespace form @[fol] def push_neg : form → form | ⊤* := ⊥* | ⊥* := ⊤* | (k ** ts) := ¬* (k ** ts) | (p ∨* q) := push_neg p ∧* push_neg q | (p ∧* q) := push_neg p ∨* push_neg q | (∀* p) := ∃* (push_neg p) | (∃* p) := ∀* (push_neg p) | (¬* p) := p @[fol] def nnf : form → form | ⊤* := ⊤* | ⊥* := ⊥* | (k ** ts) := (k ** ts) | (¬* p) := push_neg (nnf p) | (p ∨* q) := (nnf p) ∨* (nnf q) | (p ∧* q) := (nnf p) ∧* (nnf q) | (∀* p) := ∀* (nnf p) | (∃* p) := ∃* (nnf p) open tactic local attribute [instance] classical.prop_decidable lemma push_neg_equiv : ∀ p, equiv α (push_neg p) (¬* p) := begin induce `[intros M v, try {simp_fol}], simp, simp, simp, { rw not_or_distrib, apply and_iff_and, apply ihp, apply ihq }, { rw not_and_distrib, apply or_iff_or, apply ihp, apply ihq }, { rw not_forall, apply exists_iff_exists, intro x, apply ih }, { rw not_exists, apply forall_iff_forall, intro x, apply ih } end lemma nnf_equiv : ∀ p, equiv α (nnf p) p := begin induce `[intros M v, try {simp_fol}, try {trivial}], { apply iff.trans, apply push_neg_equiv, apply not_iff_not_of_iff, apply ih }, { apply or_iff_or, apply ihp, apply ihq }, { apply and_iff_and, apply ihp, apply ihq }, { apply forall_iff_forall, intro x, apply ih }, { apply exists_iff_exists, intro x, apply ih } end end form
2e4a20c2c8cfd9c1da65ee1e57555fa2b240890b
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/interactive/trace.lean
6acbe058dace268aabfeafb57ac05b5a6b0d38f1
[ "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
111
lean
example (p q : Prop) : p → q → p := begin trace "foo", intros, trace "hello world", assumption end
69a4bafd1c42c88a873a1ac63d04b42f68a1d8b4
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/dfinsupp.lean
40714eac50abfba1077b8fb353246fe103d0ba21
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
32,960
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau -/ import algebra.module.pi import algebra.big_operators.basic import data.set.finite /-! # Dependent functions with finite support For a non-dependent version see `data/finsupp.lean`. -/ universes u u₁ u₂ v v₁ v₂ v₃ w x y l open_locale big_operators variables (ι : Type u) (β : ι → Type v) namespace dfinsupp variable [Π i, has_zero (β i)] structure pre : Type (max u v) := (to_fun : Π i, β i) (pre_support : multiset ι) (zero : ∀ i, i ∈ pre_support ∨ to_fun i = 0) instance inhabited_pre : inhabited (pre ι β) := ⟨⟨λ i, 0, ∅, λ i, or.inr rfl⟩⟩ instance : setoid (pre ι β) := { r := λ x y, ∀ i, x.to_fun i = y.to_fun i, iseqv := ⟨λ f i, rfl, λ f g H i, (H i).symm, λ f g h H1 H2 i, (H1 i).trans (H2 i)⟩ } end dfinsupp variable {ι} /-- A dependent function `Π i, β i` with finite support. -/ @[reducible] def dfinsupp [Π i, has_zero (β i)] : Type* := quotient (dfinsupp.setoid ι β) variable {β} notation `Π₀` binders `, ` r:(scoped f, dfinsupp f) := r infix ` →ₚ `:25 := dfinsupp namespace dfinsupp section basic variables [Π i, has_zero (β i)] variables {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] instance : has_coe_to_fun (Π₀ i, β i) := ⟨λ _, Π i, β i, λ f, quotient.lift_on f pre.to_fun $ λ _ _, funext⟩ instance : has_zero (Π₀ i, β i) := ⟨⟦⟨λ i, 0, ∅, λ i, or.inr rfl⟩⟧⟩ instance : inhabited (Π₀ i, β i) := ⟨0⟩ @[simp] lemma zero_apply {i : ι} : (0 : Π₀ i, β i) i = 0 := rfl @[ext] lemma ext {f g : Π₀ i, β i} (H : ∀ i, f i = g i) : f = g := quotient.induction_on₂ f g (λ _ _ H, quotient.sound H) H /-- The composition of `f : β₁ → β₂` and `g : Π₀ i, β₁ i` is `map_range f hf g : Π₀ i, β₂ i`, well defined when `f 0 = 0`. -/ def map_range (f : Π i, β₁ i → β₂ i) (hf : ∀ i, f i 0 = 0) (g : Π₀ i, β₁ i) : Π₀ i, β₂ i := quotient.lift_on g (λ x, ⟦(⟨λ i, f i (x.1 i), x.2, λ i, or.cases_on (x.3 i) or.inl $ λ H, or.inr $ by rw [H, hf]⟩ : pre ι β₂)⟧) $ λ x y H, quotient.sound $ λ i, by simp only [H i] @[simp] lemma map_range_apply {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} {i : ι} : map_range f hf g i = f i (g i) := quotient.induction_on g $ λ x, rfl /-- Let `f i` be a binary operation `β₁ i → β₂ i → β i` such that `f i 0 0 = 0`. Then `zip_with f hf` is a binary operation `Π₀ i, β₁ i → Π₀ i, β₂ i → Π₀ i, β i`. -/ def zip_with (f : Π i, β₁ i → β₂ i → β i) (hf : ∀ i, f i 0 0 = 0) (g₁ : Π₀ i, β₁ i) (g₂ : Π₀ i, β₂ i) : (Π₀ i, β i) := begin refine quotient.lift_on₂ g₁ g₂ (λ x y, ⟦(⟨λ i, f i (x.1 i) (y.1 i), x.2 + y.2, λ i, _⟩ : pre ι β)⟧) _, { cases x.3 i with h1 h1, { left, rw multiset.mem_add, left, exact h1 }, cases y.3 i with h2 h2, { left, rw multiset.mem_add, right, exact h2 }, right, rw [h1, h2, hf] }, exact λ x₁ x₂ y₁ y₂ H1 H2, quotient.sound $ λ i, by simp only [H1 i, H2 i] end @[simp] lemma zip_with_apply {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} {i : ι} : zip_with f hf g₁ g₂ i = f i (g₁ i) (g₂ i) := quotient.induction_on₂ g₁ g₂ $ λ _ _, rfl end basic section algebra instance [Π i, add_monoid (β i)] : has_add (Π₀ i, β i) := ⟨zip_with (λ _, (+)) (λ _, add_zero 0)⟩ @[simp] lemma add_apply [Π i, add_monoid (β i)] {g₁ g₂ : Π₀ i, β i} {i : ι} : (g₁ + g₂) i = g₁ i + g₂ i := zip_with_apply instance [Π i, add_monoid (β i)] : add_monoid (Π₀ i, β i) := { add_monoid . zero := 0, add := (+), add_assoc := λ f g h, ext $ λ i, by simp only [add_apply, add_assoc], zero_add := λ f, ext $ λ i, by simp only [add_apply, zero_apply, zero_add], add_zero := λ f, ext $ λ i, by simp only [add_apply, zero_apply, add_zero] } instance [Π i, add_monoid (β i)] {i : ι} : is_add_monoid_hom (λ g : Π₀ i : ι, β i, g i) := { map_add := λ _ _, add_apply, map_zero := zero_apply } instance [Π i, add_group (β i)] : has_neg (Π₀ i, β i) := ⟨λ f, f.map_range (λ _, has_neg.neg) (λ _, neg_zero)⟩ instance [Π i, add_comm_monoid (β i)] : add_comm_monoid (Π₀ i, β i) := { add_comm := λ f g, ext $ λ i, by simp only [add_apply, add_comm], .. dfinsupp.add_monoid } @[simp] lemma neg_apply [Π i, add_group (β i)] {g : Π₀ i, β i} {i : ι} : (- g) i = - g i := map_range_apply instance [Π i, add_group (β i)] : add_group (Π₀ i, β i) := { add_left_neg := λ f, ext $ λ i, by simp only [add_apply, neg_apply, zero_apply, add_left_neg], .. dfinsupp.add_monoid, .. (infer_instance : has_neg (Π₀ i, β i)) } @[simp] lemma sub_apply [Π i, add_group (β i)] {g₁ g₂ : Π₀ i, β i} {i : ι} : (g₁ - g₂) i = g₁ i - g₂ i := by rw [sub_eq_add_neg]; simp [sub_eq_add_neg] instance [Π i, add_comm_group (β i)] : add_comm_group (Π₀ i, β i) := { add_comm := λ f g, ext $ λ i, by simp only [add_apply, add_comm], ..dfinsupp.add_group } /-- Dependent functions with finite support inherit a semiring action from an action on each coordinate. -/ def to_has_scalar {γ : Type w} [semiring γ] [Π i, add_comm_group (β i)] [Π i, semimodule γ (β i)] : has_scalar γ (Π₀ i, β i) := ⟨λc v, v.map_range (λ _, (•) c) (λ _, smul_zero _)⟩ local attribute [instance] to_has_scalar @[simp] lemma smul_apply {γ : Type w} [semiring γ] [Π i, add_comm_group (β i)] [Π i, semimodule γ (β i)] {i : ι} {b : γ} {v : Π₀ i, β i} : (b • v) i = b • (v i) := map_range_apply /-- Dependent functions with finite support inherit a semimodule structure from such a structure on each coordinate. -/ def to_semimodule {γ : Type w} [semiring γ] [Π i, add_comm_group (β i)] [Π i, semimodule γ (β i)] : semimodule γ (Π₀ i, β i) := semimodule.of_core { smul_add := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, smul_add], add_smul := λ c x y, ext $ λ i, by simp only [add_apply, smul_apply, add_smul], one_smul := λ x, ext $ λ i, by simp only [smul_apply, one_smul], mul_smul := λ r s x, ext $ λ i, by simp only [smul_apply, smul_smul], .. (infer_instance : has_scalar γ (Π₀ i, β i)) } end algebra section filter_and_subtype_domain /-- `filter p f` is the function which is `f i` if `p i` is true and 0 otherwise. -/ def filter [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] (f : Π₀ i, β i) : Π₀ i, β i := quotient.lift_on f (λ x, ⟦(⟨λ i, if p i then x.1 i else 0, x.2, λ i, or.cases_on (x.3 i) or.inl $ λ H, or.inr $ by rw [H, if_t_t]⟩ : pre ι β)⟧) $ λ x y H, quotient.sound $ λ i, by simp only [H i] @[simp] lemma filter_apply [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {i : ι} {f : Π₀ i, β i} : f.filter p i = if p i then f i else 0 := quotient.induction_on f $ λ x, rfl lemma filter_apply_pos [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {f : Π₀ i, β i} {i : ι} (h : p i) : f.filter p i = f i := by simp only [filter_apply, if_pos h] lemma filter_apply_neg [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {f : Π₀ i, β i} {i : ι} (h : ¬ p i) : f.filter p i = 0 := by simp only [filter_apply, if_neg h] lemma filter_pos_add_filter_neg [Π i, add_monoid (β i)] {f : Π₀ i, β i} {p : ι → Prop} [decidable_pred p] : f.filter p + f.filter (λi, ¬ p i) = f := ext $ λ i, by simp only [add_apply, filter_apply]; split_ifs; simp only [add_zero, zero_add] /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to the subtype `p`. -/ def subtype_domain [Π i, has_zero (β i)] (p : ι → Prop) [decidable_pred p] (f : Π₀ i, β i) : Π₀ i : subtype p, β i := begin fapply quotient.lift_on f, { intro x, refine ⟦⟨λ i, x.1 (i : ι), (x.2.filter p).attach.map $ λ j, ⟨j, (multiset.mem_filter.1 j.2).2⟩, _⟩⟧, refine λ i, or.cases_on (x.3 i) (λ H, _) or.inr, left, rw multiset.mem_map, refine ⟨⟨i, multiset.mem_filter.2 ⟨H, i.2⟩⟩, _, subtype.eta _ _⟩, apply multiset.mem_attach }, intros x y H, exact quotient.sound (λ i, H i) end @[simp] lemma subtype_domain_zero [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] : subtype_domain p (0 : Π₀ i, β i) = 0 := rfl @[simp] lemma subtype_domain_apply [Π i, has_zero (β i)] {p : ι → Prop} [decidable_pred p] {i : subtype p} {v : Π₀ i, β i} : (subtype_domain p v) i = v (i.val) := quotient.induction_on v $ λ x, rfl @[simp] lemma subtype_domain_add [Π i, add_monoid (β i)] {p : ι → Prop} [decidable_pred p] {v v' : Π₀ i, β i} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ i, by simp only [add_apply, subtype_domain_apply] instance subtype_domain.is_add_monoid_hom [Π i, add_monoid (β i)] {p : ι → Prop} [decidable_pred p] : is_add_monoid_hom (subtype_domain p : (Π₀ i : ι, β i) → Π₀ i : subtype p, β i) := { map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero } @[simp] lemma subtype_domain_neg [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p] {v : Π₀ i, β i} : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ i, by simp only [neg_apply, subtype_domain_apply] @[simp] lemma subtype_domain_sub [Π i, add_group (β i)] {p : ι → Prop} [decidable_pred p] {v v' : Π₀ i, β i} : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ i, by simp only [sub_apply, subtype_domain_apply] end filter_and_subtype_domain variable [dec : decidable_eq ι] include dec section basic variable [Π i, has_zero (β i)] omit dec lemma finite_supp (f : Π₀ i, β i) : set.finite {i | f i ≠ 0} := begin classical, exact quotient.induction_on f (λ x, x.2.to_finset.finite_to_set.subset (λ i H, multiset.mem_to_finset.2 ((x.3 i).resolve_right H))) end include dec /-- Create an element of `Π₀ i, β i` from a finset `s` and a function `x` defined on this `finset`. -/ def mk (s : finset ι) (x : Π i : (↑s : set ι), β (i : ι)) : Π₀ i, β i := ⟦⟨λ i, if H : i ∈ s then x ⟨i, H⟩ else 0, s.1, λ i, if H : i ∈ s then or.inl H else or.inr $ dif_neg H⟩⟧ @[simp] lemma mk_apply {s : finset ι} {x : Π i : (↑s : set ι), β i} {i : ι} : (mk s x : Π i, β i) i = if H : i ∈ s then x ⟨i, H⟩ else 0 := rfl theorem mk_injective (s : finset ι) : function.injective (@mk ι β _ _ s) := begin intros x y H, ext i, have h1 : (mk s x : Π i, β i) i = (mk s y : Π i, β i) i, {rw H}, cases i with i hi, change i ∈ s at hi, dsimp only [mk_apply, subtype.coe_mk] at h1, simpa only [dif_pos hi] using h1 end /-- The function `single i b : Π₀ i, β i` sends `i` to `b` and all other points to `0`. -/ def single (i : ι) (b : β i) : Π₀ i, β i := mk {i} $ λ j, eq.rec_on (finset.mem_singleton.1 j.prop).symm b @[simp] lemma single_apply {i i' b} : (single i b : Π₀ i, β i) i' = (if h : i = i' then eq.rec_on h b else 0) := begin dsimp only [single], by_cases h : i = i', { have h1 : i' ∈ ({i} : finset ι) := finset.mem_singleton.2 h.symm, simp only [mk_apply, dif_pos h, dif_pos h1], refl }, { have h1 : i' ∉ ({i} : finset ι) := finset.not_mem_singleton.2 (ne.symm h), simp only [mk_apply, dif_neg h, dif_neg h1] } end @[simp] lemma single_zero {i} : (single i 0 : Π₀ i, β i) = 0 := quotient.sound $ λ j, if H : j ∈ ({i} : finset _) then by dsimp only; rw [dif_pos H]; cases finset.mem_singleton.1 H; refl else dif_neg H @[simp] lemma single_eq_same {i b} : (single i b : Π₀ i, β i) i = b := by simp only [single_apply, dif_pos rfl] lemma single_eq_of_ne {i i' b} (h : i ≠ i') : (single i b : Π₀ i, β i) i' = 0 := by simp only [single_apply, dif_neg h] /-- Redefine `f i` to be `0`. -/ def erase (i : ι) (f : Π₀ i, β i) : Π₀ i, β i := quotient.lift_on f (λ x, ⟦(⟨λ j, if j = i then 0 else x.1 j, x.2, λ j, or.cases_on (x.3 j) or.inl $ λ H, or.inr $ by simp only [H, if_t_t]⟩ : pre ι β)⟧) $ λ x y H, quotient.sound $ λ j, if h : j = i then by simp only [if_pos h] else by simp only [if_neg h, H j] @[simp] lemma erase_apply {i j : ι} {f : Π₀ i, β i} : (f.erase i) j = if j = i then 0 else f j := quotient.induction_on f $ λ x, rfl @[simp] lemma erase_same {i : ι} {f : Π₀ i, β i} : (f.erase i) i = 0 := by simp lemma erase_ne {i i' : ι} {f : Π₀ i, β i} (h : i' ≠ i) : (f.erase i) i' = f i' := by simp [h] end basic section add_monoid variable [Π i, add_monoid (β i)] @[simp] lemma single_add {i : ι} {b₁ b₂ : β i} : single i (b₁ + b₂) = single i b₁ + single i b₂ := ext $ assume i', begin by_cases h : i = i', { subst h, simp only [add_apply, single_eq_same] }, { simp only [add_apply, single_eq_of_ne h, zero_add] } end lemma single_add_erase {i : ι} {f : Π₀ i, β i} : single i (f i) + f.erase i = f := ext $ λ i', if h : i = i' then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, add_zero] else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), zero_add] lemma erase_add_single {i : ι} {f : Π₀ i, β i} : f.erase i + single i (f i) = f := ext $ λ i', if h : i = i' then by subst h; simp only [add_apply, single_apply, erase_apply, dif_pos rfl, if_pos, zero_add] else by simp only [add_apply, single_apply, erase_apply, dif_neg h, if_neg (ne.symm h), add_zero] protected theorem induction {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i) (h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (single i b + f)) : p f := begin refine quotient.induction_on f (λ x, _), cases x with f s H, revert f H, apply multiset.induction_on s, { intros f H, convert h0, ext i, exact (H i).resolve_left id }, intros i s ih f H, by_cases H1 : i ∈ s, { have H2 : ∀ j, j ∈ s ∨ f j = 0, { intro j, cases H j with H2 H2, { cases multiset.mem_cons.1 H2 with H3 H3, { left, rw H3, exact H1 }, { left, exact H3 } }, right, exact H2 }, have H3 : (⟦{to_fun := f, pre_support := i :: s, zero := H}⟧ : Π₀ i, β i) = ⟦{to_fun := f, pre_support := s, zero := H2}⟧, { exact quotient.sound (λ i, rfl) }, rw H3, apply ih }, have H2 : p (erase i ⟦{to_fun := f, pre_support := i :: s, zero := H}⟧), { dsimp only [erase, quotient.lift_on_beta], have H2 : ∀ j, j ∈ s ∨ ite (j = i) 0 (f j) = 0, { intro j, cases H j with H2 H2, { cases multiset.mem_cons.1 H2 with H3 H3, { right, exact if_pos H3 }, { left, exact H3 } }, right, split_ifs; [refl, exact H2] }, have H3 : (⟦{to_fun := λ (j : ι), ite (j = i) 0 (f j), pre_support := i :: s, zero := _}⟧ : Π₀ i, β i) = ⟦{to_fun := λ (j : ι), ite (j = i) 0 (f j), pre_support := s, zero := H2}⟧ := quotient.sound (λ i, rfl), rw H3, apply ih }, have H3 : single i _ + _ = (⟦{to_fun := f, pre_support := i :: s, zero := H}⟧ : Π₀ i, β i) := single_add_erase, rw ← H3, change p (single i (f i) + _), cases classical.em (f i = 0) with h h, { rw [h, single_zero, zero_add], exact H2 }, refine ha _ _ _ _ h H2, rw erase_same end lemma induction₂ {p : (Π₀ i, β i) → Prop} (f : Π₀ i, β i) (h0 : p 0) (ha : ∀i b (f : Π₀ i, β i), f i = 0 → b ≠ 0 → p f → p (f + single i b)) : p f := dfinsupp.induction f h0 $ λ i b f h1 h2 h3, have h4 : f + single i b = single i b + f, { ext j, by_cases H : i = j, { subst H, simp [h1] }, { simp [H] } }, eq.rec_on h4 $ ha i b f h1 h2 h3 end add_monoid @[simp] lemma mk_add [Π i, add_monoid (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i} : mk s (x + y) = mk s x + mk s y := ext $ λ i, by simp only [add_apply, mk_apply]; split_ifs; [refl, rw zero_add] @[simp] lemma mk_zero [Π i, has_zero (β i)] {s : finset ι} : mk s (0 : Π i : (↑s : set ι), β i.1) = 0 := ext $ λ i, by simp only [mk_apply]; split_ifs; refl @[simp] lemma mk_neg [Π i, add_group (β i)] {s : finset ι} {x : Π i : (↑s : set ι), β i.1} : mk s (-x) = -mk s x := ext $ λ i, by simp only [neg_apply, mk_apply]; split_ifs; [refl, rw neg_zero] @[simp] lemma mk_sub [Π i, add_group (β i)] {s : finset ι} {x y : Π i : (↑s : set ι), β i.1} : mk s (x - y) = mk s x - mk s y := ext $ λ i, by simp only [sub_apply, mk_apply]; split_ifs; [refl, rw sub_zero] instance [Π i, add_group (β i)] {s : finset ι} : is_add_group_hom (@mk ι β _ _ s) := { map_add := λ _ _, mk_add } section local attribute [instance] to_semimodule variables (γ : Type w) [semiring γ] [Π i, add_comm_group (β i)] [Π i, semimodule γ (β i)] include γ @[simp] lemma mk_smul {s : finset ι} {c : γ} (x : Π i : (↑s : set ι), β i.1) : mk s (c • x) = c • mk s x := ext $ λ i, by simp only [smul_apply, mk_apply]; split_ifs; [refl, rw smul_zero] @[simp] lemma single_smul {i : ι} {c : γ} {x : β i} : single i (c • x) = c • single i x := ext $ λ i, by simp only [smul_apply, single_apply]; split_ifs; [cases h, rw smul_zero]; refl variable β /-- `dfinsupp.mk` as a `linear_map`. -/ def lmk (s : finset ι) : (Π i : (↑s : set ι), β i.1) →ₗ[γ] Π₀ i, β i := ⟨mk s, λ _ _, mk_add, λ c x, by rw [mk_smul γ x]⟩ /-- `dfinsupp.single` as a `linear_map` -/ def lsingle (i) : β i →ₗ[γ] Π₀ i, β i := ⟨single i, λ _ _, single_add, λ _ _, single_smul _⟩ variable {β} @[simp] lemma lmk_apply {s : finset ι} {x} : lmk β γ s x = mk s x := rfl @[simp] lemma lsingle_apply {i : ι} {x : β i} : lsingle β γ i x = single i x := rfl end section support_basic variables [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] /-- Set `{i | f x ≠ 0}` as a `finset`. -/ def support (f : Π₀ i, β i) : finset ι := quotient.lift_on f (λ x, x.2.to_finset.filter $ λ i, x.1 i ≠ 0) $ begin intros x y Hxy, ext i, split, { intro H, rcases finset.mem_filter.1 H with ⟨h1, h2⟩, rw Hxy i at h2, exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (y.3 i).resolve_right h2, h2⟩ }, { intro H, rcases finset.mem_filter.1 H with ⟨h1, h2⟩, rw ← Hxy i at h2, exact finset.mem_filter.2 ⟨multiset.mem_to_finset.2 $ (x.3 i).resolve_right h2, h2⟩ }, end @[simp] theorem support_mk_subset {s : finset ι} {x : Π i : (↑s : set ι), β i.1} : (mk s x).support ⊆ s := λ i H, multiset.mem_to_finset.1 (finset.mem_filter.1 H).1 @[simp] theorem mem_support_to_fun (f : Π₀ i, β i) (i) : i ∈ f.support ↔ f i ≠ 0 := begin refine quotient.induction_on f (λ x, _), dsimp only [support, quotient.lift_on_beta], rw [finset.mem_filter, multiset.mem_to_finset], exact and_iff_right_of_imp (x.3 i).resolve_right end theorem eq_mk_support (f : Π₀ i, β i) : f = mk f.support (λ i, f i) := begin change f = mk f.support (λ i, f i.1), ext i, by_cases h : f i ≠ 0; [skip, rw [not_not] at h]; simp [h] end @[simp] lemma support_zero : (0 : Π₀ i, β i).support = ∅ := rfl lemma mem_support_iff (f : Π₀ i, β i) : ∀i:ι, i ∈ f.support ↔ f i ≠ 0 := f.mem_support_to_fun @[simp] lemma support_eq_empty {f : Π₀ i, β i} : f.support = ∅ ↔ f = 0 := ⟨λ H, ext $ by simpa [finset.ext_iff] using H, by simp {contextual:=tt}⟩ instance decidable_zero : decidable_pred (eq (0 : Π₀ i, β i)) := λ f, decidable_of_iff _ $ support_eq_empty.trans eq_comm lemma support_subset_iff {s : set ι} {f : Π₀ i, β i} : ↑f.support ⊆ s ↔ (∀i∉s, f i = 0) := by simp [set.subset_def]; exact forall_congr (assume i, not_imp_comm) lemma support_single_ne_zero {i : ι} {b : β i} (hb : b ≠ 0) : (single i b).support = {i} := begin ext j, by_cases h : i = j, { subst h, simp [hb] }, simp [ne.symm h, h] end lemma support_single_subset {i : ι} {b : β i} : (single i b).support ⊆ {i} := support_mk_subset section map_range_and_zip_with variables {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} variables [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] lemma map_range_def [Π i (x : β₁ i), decidable (x ≠ 0)] {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} : map_range f hf g = mk g.support (λ i, f i.1 (g i.1)) := begin ext i, by_cases h : g i ≠ 0; simp at h; simp [h, hf] end @[simp] lemma map_range_single {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {i : ι} {b : β₁ i} : map_range f hf (single i b) = single i (f i b) := dfinsupp.ext $ λ i', by by_cases i = i'; [{subst i', simp}, simp [h, hf]] variables [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)] lemma support_map_range {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} : (map_range f hf g).support ⊆ g.support := by simp [map_range_def] lemma zip_with_def {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} : zip_with f hf g₁ g₂ = mk (g₁.support ∪ g₂.support) (λ i, f i.1 (g₁ i.1) (g₂ i.1)) := begin ext i, by_cases h1 : g₁ i ≠ 0; by_cases h2 : g₂ i ≠ 0; simp only [not_not, ne.def] at h1 h2; simp [h1, h2, hf] end lemma support_zip_with {f : Π i, β₁ i → β₂ i → β i} {hf : ∀ i, f i 0 0 = 0} {g₁ : Π₀ i, β₁ i} {g₂ : Π₀ i, β₂ i} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support := by simp [zip_with_def] end map_range_and_zip_with lemma erase_def (i : ι) (f : Π₀ i, β i) : f.erase i = mk (f.support.erase i) (λ j, f j.1) := by { ext j, by_cases h1 : j = i; by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] } @[simp] lemma support_erase (i : ι) (f : Π₀ i, β i) : (f.erase i).support = f.support.erase i := by { ext j, by_cases h1 : j = i; by_cases h2 : f j ≠ 0; simp at h2; simp [h1, h2] } section filter_and_subtype_domain variables {p : ι → Prop} [decidable_pred p] lemma filter_def (f : Π₀ i, β i) : f.filter p = mk (f.support.filter p) (λ i, f i.1) := by ext i; by_cases h1 : p i; by_cases h2 : f i ≠ 0; simp at h2; simp [h1, h2] @[simp] lemma support_filter (f : Π₀ i, β i) : (f.filter p).support = f.support.filter p := by ext i; by_cases h : p i; simp [h] lemma subtype_domain_def (f : Π₀ i, β i) : f.subtype_domain p = mk (f.support.subtype p) (λ i, f i.1) := by ext i; cases i with i hi; by_cases h1 : p i; by_cases h2 : f i ≠ 0; try {simp at h2}; dsimp; simp [h1, h2] @[simp] lemma support_subtype_domain {f : Π₀ i, β i} : (subtype_domain p f).support = f.support.subtype p := by ext i; cases i with i hi; by_cases h1 : p i; by_cases h2 : f i ≠ 0; try {simp at h2}; dsimp; simp [h1, h2] end filter_and_subtype_domain end support_basic lemma support_add [Π i, add_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] {g₁ g₂ : Π₀ i, β i} : (g₁ + g₂).support ⊆ g₁.support ∪ g₂.support := support_zip_with @[simp] lemma support_neg [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)] {f : Π₀ i, β i} : support (-f) = support f := by ext i; simp local attribute [instance] dfinsupp.to_semimodule lemma support_smul {γ : Type w} [ring γ] [Π i, add_comm_group (β i)] [Π i, module γ (β i)] [Π (i : ι) (x : β i), decidable (x ≠ 0)] {b : γ} {v : Π₀ i, β i} : (b • v).support ⊆ v.support := support_map_range instance [Π i, has_zero (β i)] [Π i, decidable_eq (β i)] : decidable_eq (Π₀ i, β i) := assume f g, decidable_of_iff (f.support = g.support ∧ (∀i∈f.support, f i = g i)) ⟨assume ⟨h₁, h₂⟩, ext $ assume i, if h : i ∈ f.support then h₂ i h else have hf : f i = 0, by rwa [f.mem_support_iff, not_not] at h, have hg : g i = 0, by rwa [h₁, g.mem_support_iff, not_not] at h, by rw [hf, hg], by intro h; subst h; simp⟩ section prod_and_sum variables {γ : Type w} -- [to_additive sum] for dfinsupp.prod doesn't work, the equation lemmas are not generated /-- `sum f g` is the sum of `g i (f i)` over the support of `f`. -/ def sum [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_monoid γ] (f : Π₀ i, β i) (g : Π i, β i → γ) : γ := ∑ i in f.support, g i (f i) /-- `prod f g` is the product of `g i (f i)` over the support of `f`. -/ @[to_additive] def prod [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] (f : Π₀ i, β i) (g : Π i, β i → γ) : γ := ∏ i in f.support, g i (f i) @[to_additive] lemma prod_map_range_index {β₁ : ι → Type v₁} {β₂ : ι → Type v₂} [Π i, has_zero (β₁ i)] [Π i, has_zero (β₂ i)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i (x : β₂ i), decidable (x ≠ 0)] [comm_monoid γ] {f : Π i, β₁ i → β₂ i} {hf : ∀ i, f i 0 = 0} {g : Π₀ i, β₁ i} {h : Π i, β₂ i → γ} (h0 : ∀i, h i 0 = 1) : (map_range f hf g).prod h = g.prod (λi b, h i (f i b)) := begin rw [map_range_def], refine (finset.prod_subset support_mk_subset _).trans _, { intros i h1 h2, dsimp, simp [h1] at h2, dsimp at h2, simp [h1, h2, h0] }, { refine finset.prod_congr rfl _, intros i h1, simp [h1] } end @[to_additive] lemma prod_zero_index [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {h : Π i, β i → γ} : (0 : Π₀ i, β i).prod h = 1 := rfl @[to_additive] lemma prod_single_index [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {i : ι} {b : β i} {h : Π i, β i → γ} (h_zero : h i 0 = 1) : (single i b).prod h = h i b := begin by_cases h : b ≠ 0, { simp [dfinsupp.prod, support_single_ne_zero h] }, { rw [not_not] at h, simp [h, prod_zero_index, h_zero], refl } end @[to_additive] lemma prod_neg_index [Π i, add_group (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {g : Π₀ i, β i} {h : Π i, β i → γ} (h0 : ∀i, h i 0 = 1) : (-g).prod h = g.prod (λi b, h i (- b)) := prod_map_range_index h0 omit dec @[simp] lemma sum_apply {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i, add_comm_monoid (β i)] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} {i₂ : ι} : (f.sum g) i₂ = f.sum (λi₁ b, g i₁ b i₂) := (f.support.sum_hom (λf : Π₀ i, β i, f i₂)).symm include dec lemma support_sum {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} : (f.sum g).support ⊆ f.support.bind (λi, (g i (f i)).support) := have ∀i₁ : ι, f.sum (λ (i : ι₁) (b : β₁ i), (g i b) i₁) ≠ 0 → (∃ (i : ι₁), f i ≠ 0 ∧ ¬ (g i (f i)) i₁ = 0), from assume i₁ h, let ⟨i, hi, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in ⟨i, (f.mem_support_iff i).mp hi, ne⟩, by simpa [finset.subset_iff, mem_support_iff, finset.mem_bind, sum_apply] using this @[simp] lemma sum_zero [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_monoid γ] {f : Π₀ i, β i} : f.sum (λi b, (0 : γ)) = 0 := finset.sum_const_zero @[simp] lemma sum_add [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_monoid γ] {f : Π₀ i, β i} {h₁ h₂ : Π i, β i → γ} : f.sum (λi b, h₁ i b + h₂ i b) = f.sum h₁ + f.sum h₂ := finset.sum_add_distrib @[simp] lemma sum_neg [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_group γ] {f : Π₀ i, β i} {h : Π i, β i → γ} : f.sum (λi b, - h i b) = - f.sum h := f.support.sum_hom (@has_neg.neg γ _) @[to_additive] lemma prod_add_index [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {f g : Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : (f + g).prod h = f.prod h * g.prod h := have f_eq : ∏ i in f.support ∪ g.support, h i (f i) = f.prod h, from (finset.prod_subset (finset.subset_union_left _ _) $ by simp [mem_support_iff, h_zero] {contextual := tt}).symm, have g_eq : ∏ i in f.support ∪ g.support, h i (g i) = g.prod h, from (finset.prod_subset (finset.subset_union_right _ _) $ by simp [mem_support_iff, h_zero] {contextual := tt}).symm, calc ∏ i in (f + g).support, h i ((f + g) i) = ∏ i in f.support ∪ g.support, h i ((f + g) i) : finset.prod_subset support_add $ by simp [mem_support_iff, h_zero] {contextual := tt} ... = (∏ i in f.support ∪ g.support, h i (f i)) * (∏ i in f.support ∪ g.support, h i (g i)) : by simp [h_add, finset.prod_mul_distrib] ... = _ : by rw [f_eq, g_eq] lemma sum_sub_index [Π i, add_comm_group (β i)] [Π i (x : β i), decidable (x ≠ 0)] [add_comm_group γ] {f g : Π₀ i, β i} {h : Π i, β i → γ} (h_sub : ∀i b₁ b₂, h i (b₁ - b₂) = h i b₁ - h i b₂) : (f - g).sum h = f.sum h - g.sum h := have h_zero : ∀i, h i 0 = 0, from assume i, have h i (0 - 0) = h i 0 - h i 0, from h_sub i 0 0, by simpa using this, have h_neg : ∀i b, h i (- b) = - h i b, from assume i b, have h i (0 - b) = h i 0 - h i b, from h_sub i 0 b, by simpa [h_zero] using this, have h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ + h i b₂, from assume i b₁ b₂, have h i (b₁ - (- b₂)) = h i b₁ - h i (- b₂), from h_sub i b₁ (-b₂), by simpa [h_neg, sub_eq_add_neg] using this, by simp [sub_eq_add_neg]; simp [@sum_add_index ι β _ γ _ _ _ f (-g) h h_zero h_add]; simp [@sum_neg_index ι β _ γ _ _ _ g h h_zero, h_neg]; simp [@sum_neg ι β _ γ _ _ _ g h] @[to_additive] lemma prod_finset_sum_index {γ : Type w} {α : Type x} [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {s : finset α} {g : α → Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : ∏ i in s, (g i).prod h = (∑ i in s, g i).prod h := begin classical, exact finset.induction_on s (by simp [prod_zero_index]) (by simp [prod_add_index, h_zero, h_add] {contextual := tt}) end @[to_additive] lemma prod_sum_index {ι₁ : Type u₁} [decidable_eq ι₁] {β₁ : ι₁ → Type v₁} [Π i₁, has_zero (β₁ i₁)] [Π i (x : β₁ i), decidable (x ≠ 0)] [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {f : Π₀ i₁, β₁ i₁} {g : Π i₁, β₁ i₁ → Π₀ i, β i} {h : Π i, β i → γ} (h_zero : ∀i, h i 0 = 1) (h_add : ∀i b₁ b₂, h i (b₁ + b₂) = h i b₁ * h i b₂) : (f.sum g).prod h = f.prod (λi b, (g i b).prod h) := (prod_finset_sum_index h_zero h_add).symm @[simp] lemma sum_single [Π i, add_comm_monoid (β i)] [Π i (x : β i), decidable (x ≠ 0)] {f : Π₀ i, β i} : f.sum single = f := begin apply dfinsupp.induction f, {rw [sum_zero_index]}, intros i b f H hb ih, rw [sum_add_index, ih, sum_single_index], all_goals { intros, simp } end @[to_additive] lemma prod_subtype_domain_index [Π i, has_zero (β i)] [Π i (x : β i), decidable (x ≠ 0)] [comm_monoid γ] {v : Π₀ i, β i} {p : ι → Prop} [decidable_pred p] {h : Π i, β i → γ} (hp : ∀ x ∈ v.support, p x) : (v.subtype_domain p).prod (λi b, h i.1 b) = v.prod h := finset.prod_bij (λp _, p.val) (by { dsimp, simp }) (by simp) (assume ⟨a₀, ha₀⟩ ⟨a₁, ha₁⟩, by simp) (λ i hi, ⟨⟨i, hp i hi⟩, by simpa using hi, rfl⟩) omit dec lemma subtype_domain_sum [Π i, add_comm_monoid (β i)] {s : finset γ} {h : γ → Π₀ i, β i} {p : ι → Prop} [decidable_pred p] : (∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p := eq.symm (s.sum_hom _) lemma subtype_domain_finsupp_sum {δ : γ → Type x} [decidable_eq γ] [Π c, has_zero (δ c)] [Π c (x : δ c), decidable (x ≠ 0)] [Π i, add_comm_monoid (β i)] {p : ι → Prop} [decidable_pred p] {s : Π₀ c, δ c} {h : Π c, δ c → Π₀ i, β i} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum end prod_and_sum end dfinsupp
608c7cf6a18896ae5e89536e06f44a31b03fd3af
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/1022.lean
d0fc778dbf1358af9bc36913b22e651e4d2378d3
[ "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
875
lean
namespace STLC abbrev Var := Char inductive type where | base : type | arrow : type → type → type inductive term where | var : Var → term | lam : Var → type → term → term | app : term → term → term def ctx := List (Var × type) open type term in inductive typing : ctx → term → type → Prop where | var : typing ((x, A) :: Γ) (var x) A -- simplified | arri : typing ((x, A) :: Γ) M B → typing Γ (lam x A M) (arrow A B) | arre : typing Γ M (arrow A B) → typing Γ N A → typing Γ (app M N) B open type term in theorem no_δ : ¬ ∃ A B, typing nil (lam x A (app (var x) (var x))) (arrow A B) := fun h => match h with | Exists.intro A (Exists.intro B h) => match h with | typing.arri h => match h with | typing.arre (A := T) h₁ h₂ => match h₂ with | typing.var => nomatch h₁ namespace STLC
5c0f52f3b4c9aeefe3ad5abcfde663d5c3fb1db6
53618200bef52920c1e974173f78cd378d268f3e
/hott/homotopy/susp.hlean
4a5702cb1337b72f58ebfb707db2a8b32736eb68
[ "Apache-2.0" ]
permissive
sayantangkhan/lean2
cc41e61102e0fcc8b65e8501186dcca40b19b22e
0fc0378969678eec25ea425a426bb48a184a6db0
refs/heads/master
1,590,323,112,724
1,489,425,932,000
1,489,425,932,000
84,854,525
0
0
null
1,489,425,509,000
1,489,425,509,000
null
UTF-8
Lean
false
false
14,397
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Ulrik Buchholtz Declaration of suspension -/ import hit.pushout types.pointed cubical.square .connectedness open pushout unit eq equiv definition susp (A : Type) : Type := pushout (λ(a : A), star) (λ(a : A), star) namespace susp variable {A : Type} definition north {A : Type} : susp A := inl star definition south {A : Type} : susp A := inr star definition merid (a : A) : @north A = @south A := glue a protected definition rec {P : susp A → Type} (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) (x : susp A) : P x := begin induction x with u u, { cases u, exact PN}, { cases u, exact PS}, { apply Pm}, end protected definition rec_on [reducible] {P : susp A → Type} (y : susp A) (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) : P y := susp.rec PN PS Pm y theorem rec_merid {P : susp A → Type} (PN : P north) (PS : P south) (Pm : Π(a : A), PN =[merid a] PS) (a : A) : apd (susp.rec PN PS Pm) (merid a) = Pm a := !rec_glue protected definition elim {P : Type} (PN : P) (PS : P) (Pm : A → PN = PS) (x : susp A) : P := susp.rec PN PS (λa, pathover_of_eq _ (Pm a)) x protected definition elim_on [reducible] {P : Type} (x : susp A) (PN : P) (PS : P) (Pm : A → PN = PS) : P := susp.elim PN PS Pm x theorem elim_merid {P : Type} {PN PS : P} (Pm : A → PN = PS) (a : A) : ap (susp.elim PN PS Pm) (merid a) = Pm a := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (merid a)), rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑susp.elim,rec_merid], end protected definition elim_type (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (x : susp A) : Type := pushout.elim_type (λx, PN) (λx, PS) Pm x protected definition elim_type_on [reducible] (x : susp A) (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) : Type := susp.elim_type PN PS Pm x theorem elim_type_merid (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (a : A) : transport (susp.elim_type PN PS Pm) (merid a) = Pm a := !elim_type_glue theorem elim_type_merid_inv {A : Type} (PN : Type) (PS : Type) (Pm : A → PN ≃ PS) (a : A) : transport (susp.elim_type PN PS Pm) (merid a)⁻¹ = to_inv (Pm a) := !elim_type_glue_inv protected definition merid_square {a a' : A} (p : a = a') : square (merid a) (merid a') idp idp := by cases p; apply vrefl end susp attribute susp.north susp.south [constructor] attribute susp.rec susp.elim [unfold 6] [recursor 6] attribute susp.elim_type [unfold 5] attribute susp.rec_on susp.elim_on [unfold 3] attribute susp.elim_type_on [unfold 2] namespace susp open is_trunc is_conn trunc -- Theorem 8.2.1 definition is_conn_susp [instance] (n : trunc_index) (A : Type) [H : is_conn n A] : is_conn (n .+1) (susp A) := is_contr.mk (tr north) begin apply trunc.rec, fapply susp.rec, { reflexivity }, { exact (trunc.rec (λa, ap tr (merid a)) (center (trunc n A))) }, { intro a, generalize (center (trunc n A)), apply trunc.rec, intro a', apply pathover_of_tr_eq, rewrite [eq_transport_Fr,idp_con], revert H, induction n with [n, IH], { intro H, apply is_prop.elim }, { intros H, change ap (@tr n .+2 (susp A)) (merid a) = ap tr (merid a'), generalize a', apply is_conn_fun.elim n (is_conn_fun_from_unit n A a) (λx : A, trunctype.mk' n (ap (@tr n .+2 (susp A)) (merid a) = ap tr (merid x))), intros, change ap (@tr n .+2 (susp A)) (merid a) = ap tr (merid a), reflexivity } } end /- Flattening lemma -/ open prod prod.ops section universe variable u parameters (A : Type) (PN PS : Type.{u}) (Pm : A → PN ≃ PS) include Pm local abbreviation P [unfold 5] := susp.elim_type PN PS Pm local abbreviation F : A × PN → PN := λz, z.2 local abbreviation G : A × PN → PS := λz, Pm z.1 z.2 protected definition flattening : sigma P ≃ pushout F G := begin apply equiv.trans !pushout.flattening, fapply pushout.equiv, { exact sigma.equiv_prod A PN }, { apply sigma.sigma_unit_left }, { apply sigma.sigma_unit_left }, { reflexivity }, { reflexivity } end end end susp /- Functoriality and equivalence -/ namespace susp variables {A B : Type} (f : A → B) include f protected definition functor [unfold 4] : susp A → susp B := begin intro x, induction x with a, { exact north }, { exact south }, { exact merid (f a) } end variable [Hf : is_equiv f] include Hf open is_equiv protected definition is_equiv_functor [instance] [constructor] : is_equiv (susp.functor f) := adjointify (susp.functor f) (susp.functor f⁻¹) abstract begin intro sb, induction sb with b, do 2 reflexivity, apply eq_pathover, rewrite [ap_id,ap_compose' (susp.functor f) (susp.functor f⁻¹)], krewrite [susp.elim_merid,susp.elim_merid], apply transpose, apply susp.merid_square (right_inv f b) end end abstract begin intro sa, induction sa with a, do 2 reflexivity, apply eq_pathover, rewrite [ap_id,ap_compose' (susp.functor f⁻¹) (susp.functor f)], krewrite [susp.elim_merid,susp.elim_merid], apply transpose, apply susp.merid_square (left_inv f a) end end end susp namespace susp variables {A B : Type} (f : A ≃ B) protected definition equiv : susp A ≃ susp B := equiv.mk (susp.functor f) _ end susp namespace susp open pointed definition pointed_susp [instance] [constructor] (X : Type) : pointed (susp X) := pointed.mk north end susp open susp definition psusp [constructor] (X : Type) : Type* := pointed.mk' (susp X) notation `⅀` := psusp namespace susp open pointed is_trunc variables {X Y Z : Type*} definition is_conn_psusp [instance] (n : trunc_index) (A : Type*) [H : is_conn n A] : is_conn (n .+1) (psusp A) := is_conn_susp n A definition psusp_functor [constructor] (f : X →* Y) : psusp X →* psusp Y := begin fconstructor, { exact susp.functor f }, { reflexivity } end definition is_equiv_psusp_functor [constructor] (f : X →* Y) [Hf : is_equiv f] : is_equiv (psusp_functor f) := susp.is_equiv_functor f definition psusp_equiv [constructor] (f : X ≃* Y) : psusp X ≃* psusp Y := pequiv_of_equiv (susp.equiv f) idp definition psusp_functor_compose (g : Y →* Z) (f : X →* Y) : psusp_functor (g ∘* f) ~* psusp_functor g ∘* psusp_functor f := begin fconstructor, { intro a, induction a, { reflexivity }, { reflexivity }, { apply eq_pathover, apply hdeg_square, rewrite [▸*,ap_compose' _ (psusp_functor f)], krewrite +susp.elim_merid } }, { reflexivity } end -- adjunction from Coq-HoTT definition loop_psusp_unit [constructor] (X : Type*) : X →* Ω(psusp X) := begin fconstructor, { intro x, exact merid x ⬝ (merid pt)⁻¹ }, { apply con.right_inv }, end definition loop_psusp_unit_natural (f : X →* Y) : loop_psusp_unit Y ∘* f ~* ap1 (psusp_functor f) ∘* loop_psusp_unit X := begin induction X with X x, induction Y with Y y, induction f with f pf, esimp at *, induction pf, fconstructor, { intro x', esimp [psusp_functor], symmetry, exact !idp_con ⬝ (!ap_con ⬝ whisker_left _ !ap_inv) ⬝ (!elim_merid ◾ (inverse2 !elim_merid)) }, { rewrite [▸*,idp_con (con.right_inv _)], apply inv_con_eq_of_eq_con, refine _ ⬝ !con.assoc', rewrite inverse2_right_inv, refine _ ⬝ !con.assoc', rewrite [ap_con_right_inv], xrewrite [idp_con_idp, -ap_compose (concat idp)] }, end definition loop_psusp_counit [constructor] (X : Type*) : psusp (Ω X) →* X := begin fconstructor, { intro x, induction x, exact pt, exact pt, exact a }, { reflexivity }, end definition loop_psusp_counit_natural (f : X →* Y) : f ∘* loop_psusp_counit X ~* loop_psusp_counit Y ∘* (psusp_functor (ap1 f)) := begin induction X with X x, induction Y with Y y, induction f with f pf, esimp at *, induction pf, fconstructor, { intro x', induction x' with p, { reflexivity }, { reflexivity }, { esimp, apply eq_pathover, apply hdeg_square, xrewrite [ap_compose' f, ap_compose' (susp.elim (f x) (f x) (λ (a : f x = f x), a)),▸*], xrewrite [+elim_merid,▸*,idp_con] }}, { reflexivity } end definition loop_psusp_counit_unit (X : Type*) : ap1 (loop_psusp_counit X) ∘* loop_psusp_unit (Ω X) ~* pid (Ω X) := begin induction X with X x, fconstructor, { intro p, esimp, refine !idp_con ⬝ (!ap_con ⬝ whisker_left _ !ap_inv) ⬝ (!elim_merid ◾ inverse2 !elim_merid) }, { rewrite [▸*,inverse2_right_inv (elim_merid id idp)], refine !con.assoc ⬝ _, xrewrite [ap_con_right_inv (susp.elim x x (λa, a)) (merid idp),idp_con_idp,-ap_compose] } end definition loop_psusp_unit_counit (X : Type*) : loop_psusp_counit (psusp X) ∘* psusp_functor (loop_psusp_unit X) ~* pid (psusp X) := begin induction X with X x, fconstructor, { intro x', induction x', { reflexivity }, { exact merid pt }, { apply eq_pathover, xrewrite [▸*, ap_id, ap_compose' (susp.elim north north (λa, a)), +elim_merid,▸*], apply square_of_eq, exact !idp_con ⬝ !inv_con_cancel_right⁻¹ }}, { reflexivity } end definition psusp.elim [constructor] {X Y : Type*} (f : X →* Ω Y) : psusp X →* Y := loop_psusp_counit Y ∘* psusp_functor f definition loop_psusp_intro [constructor] {X Y : Type*} (f : psusp X →* Y) : X →* Ω Y := ap1 f ∘* loop_psusp_unit X definition psusp_adjoint_loop_right_inv {X Y : Type*} (g : X →* Ω Y) : loop_psusp_intro (psusp.elim g) ~* g := begin refine !pwhisker_right !ap1_pcompose ⬝* _, refine !passoc ⬝* _, refine !pwhisker_left !loop_psusp_unit_natural⁻¹* ⬝* _, refine !passoc⁻¹* ⬝* _, refine !pwhisker_right !loop_psusp_counit_unit ⬝* _, apply pid_pcompose end definition psusp_adjoint_loop_left_inv {X Y : Type*} (f : psusp X →* Y) : psusp.elim (loop_psusp_intro f) ~* f := begin refine !pwhisker_left !psusp_functor_compose ⬝* _, refine !passoc⁻¹* ⬝* _, refine !pwhisker_right !loop_psusp_counit_natural⁻¹* ⬝* _, refine !passoc ⬝* _, refine !pwhisker_left !loop_psusp_unit_counit ⬝* _, apply pcompose_pid end definition psusp_adjoint_loop_unpointed [constructor] (X Y : Type*) : psusp X →* Y ≃ X →* Ω Y := begin fapply equiv.MK, { exact loop_psusp_intro }, { exact psusp.elim }, { intro g, apply eq_of_phomotopy, exact psusp_adjoint_loop_right_inv g }, { intro f, apply eq_of_phomotopy, exact psusp_adjoint_loop_left_inv f } end definition psusp_adjoint_loop_pconst (X Y : Type*) : psusp_adjoint_loop_unpointed X Y (pconst (psusp X) Y) ~* pconst X (Ω Y) := begin refine pwhisker_right _ !ap1_pconst ⬝* _, apply pconst_pcompose end definition psusp_adjoint_loop [constructor] (X Y : Type*) : ppmap (psusp X) Y ≃* ppmap X (Ω Y) := begin apply pequiv_of_equiv (psusp_adjoint_loop_unpointed X Y), apply eq_of_phomotopy, apply psusp_adjoint_loop_pconst end definition ap1_psusp_elim {A : Type*} {X : Type*} (p : A →* Ω X) : Ω→(psusp.elim p) ∘* loop_psusp_unit A ~* p := psusp_adjoint_loop_right_inv p definition psusp_adjoint_loop_nat_right (f : psusp X →* Y) (g : Y →* Z) : psusp_adjoint_loop X Z (g ∘* f) ~* ap1 g ∘* psusp_adjoint_loop X Y f := begin esimp [psusp_adjoint_loop], refine _ ⬝* !passoc, apply pwhisker_right, apply ap1_pcompose end definition psusp_adjoint_loop_nat_left (f : Y →* Ω Z) (g : X →* Y) : (psusp_adjoint_loop X Z)⁻¹ᵉ (f ∘* g) ~* (psusp_adjoint_loop Y Z)⁻¹ᵉ f ∘* psusp_functor g := begin esimp [psusp_adjoint_loop], refine _ ⬝* !passoc⁻¹*, apply pwhisker_left, apply psusp_functor_compose end /- iterated suspension -/ definition iterate_susp (n : ℕ) (A : Type) : Type := iterate susp n A definition iterate_psusp (n : ℕ) (A : Type*) : Type* := iterate (λX, psusp X) n A open is_conn trunc_index nat definition iterate_susp_succ (n : ℕ) (A : Type) : iterate_susp (succ n) A = susp (iterate_susp n A) := idp definition is_conn_iterate_susp [instance] (n : ℕ₋₂) (m : ℕ) (A : Type) [H : is_conn n A] : is_conn (n + m) (iterate_susp m A) := begin induction m with m IH, exact H, exact @is_conn_susp _ _ IH end definition is_conn_iterate_psusp [instance] (n : ℕ₋₂) (m : ℕ) (A : Type*) [H : is_conn n A] : is_conn (n + m) (iterate_psusp m A) := begin induction m with m IH, exact H, exact @is_conn_susp _ _ IH end -- Separate cases for n = 0, which comes up often definition is_conn_iterate_susp_zero [instance] (m : ℕ) (A : Type) [H : is_conn 0 A] : is_conn m (iterate_susp m A) := begin induction m with m IH, exact H, exact @is_conn_susp _ _ IH end definition is_conn_iterate_psusp_zero [instance] (m : ℕ) (A : Type*) [H : is_conn 0 A] : is_conn m (iterate_psusp m A) := begin induction m with m IH, exact H, exact @is_conn_susp _ _ IH end definition iterate_psusp_functor (n : ℕ) {A B : Type*} (f : A →* B) : iterate_psusp n A →* iterate_psusp n B := begin induction n with n g, { exact f }, { exact psusp_functor g } end definition iterate_psusp_succ_in (n : ℕ) (A : Type*) : iterate_psusp (succ n) A ≃* iterate_psusp n (psusp A) := begin induction n with n IH, { reflexivity}, { exact psusp_equiv IH} end definition iterate_psusp_adjoint_loopn [constructor] (X Y : Type*) (n : ℕ) : ppmap (iterate_psusp n X) Y ≃* ppmap X (Ω[n] Y) := begin revert X Y, induction n with n IH: intro X Y, { reflexivity }, { refine !psusp_adjoint_loop ⬝e* !IH ⬝e* _, apply pequiv_ppcompose_left, symmetry, apply loopn_succ_in } end end susp
eda629ee1b006c6ca22155af94a9c6e7da81d3c1
ad0c7d243dc1bd563419e2767ed42fb323d7beea
/ring_theory/noetherian.lean
6824bce1b25a079eae2489509c36c6b060aa27e4
[ "Apache-2.0" ]
permissive
sebzim4500/mathlib
e0b5a63b1655f910dee30badf09bd7e191d3cf30
6997cafbd3a7325af5cb318561768c316ceb7757
refs/heads/master
1,585,549,958,618
1,538,221,723,000
1,538,221,723,000
150,869,076
0
0
Apache-2.0
1,538,229,323,000
1,538,229,323,000
null
UTF-8
Lean
false
false
3,898
lean
/- Copyright (c) 2018 Mario Carneiro and Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro and Kevin Buzzard -/ import order.order_iso import data.fintype data.polynomial import tactic.tidy import linear_algebra.submodule import ring_theory.ideals local attribute [instance] classical.dec open set lattice def is_fg {α β} [ring α] [module α β] (s : set β) [is_submodule s] : Prop := ∃ t : finset β, _root_.span ↑t = s namespace submodule universes u v variables {α : Type u} {β : Type v} [ring α] [module α β] def fg (s : submodule α β) : Prop := is_fg (s : set β) theorem fg_def {s : submodule α β} : s.fg ↔ ∃ t : set β, finite t ∧ span t = s := ⟨λ ⟨t, h⟩, ⟨_, finset.finite_to_set t, ext h⟩, begin rintro ⟨t', h, rfl⟩, rcases finite.exists_finset_coe h with ⟨t, rfl⟩, exact ⟨t, rfl⟩ end⟩ end submodule def is_noetherian (α β) [ring α] [module α β] : Prop := ∀ (s : submodule α β), s.fg theorem is_noetherian_iff_well_founded {α β} [ring α] [module α β] : is_noetherian α β ↔ well_founded ((>) : submodule α β → submodule α β → Prop) := ⟨λ h, begin apply order_embedding.well_founded_iff_no_descending_seq.2, swap, { apply is_strict_order.swap }, rintro ⟨⟨N, hN⟩⟩, let M := ⨆ n, N n, rcases submodule.fg_def.1 (h M) with ⟨t, h₁, h₂⟩, have hN' : ∀ {a b}, a ≤ b → N a ≤ N b := λ a b, (le_iff_le_of_strict_mono N (λ _ _, hN.1)).2, have : t ⊆ ⋃ i, (N i : set β), { rw [← submodule.Union_set_of_directed _ N _], { show t ⊆ M, rw ← h₂, apply subset_span }, { apply_instance }, { exact λ i j, ⟨max i j, hN' (le_max_left _ _), hN' (le_max_right _ _)⟩ } }, simp [subset_def] at this, have : ∀ x : t, (∃ (i : ℕ), x.1 ∈ N i), {simpa}, rcases classical.axiom_of_choice this with ⟨f, hf⟩, dsimp at f hf, cases h₁ with h₁, let A := finset.sup (@finset.univ t h₁) f, have : M ≤ N A, { rw ← h₂, apply submodule.span_subset_iff.2, exact λ x h, hN' (finset.le_sup (@finset.mem_univ t h₁ _)) (hf ⟨x, h⟩) }, exact not_le_of_lt (hN.1 (nat.lt_succ_self A)) (le_trans (le_supr _ _) this) end, λ h N, begin suffices : ∀ M ≤ N, ∃ s, finite s ∧ M ⊔ submodule.span s = N, { rcases this ⊥ bot_le with ⟨s, hs, e⟩, exact submodule.fg_def.2 ⟨s, hs, by simpa using e⟩ }, refine λ M, h.induction M _, intros M IH MN, by_cases h : ∀ x, x ∈ N → x ∈ M, { cases le_antisymm MN h, exact ⟨∅, by simp⟩ }, { simp [not_forall] at h, rcases h with ⟨x, h, h₂⟩, have : ¬M ⊔ submodule.span {x} ≤ M, { intro hn, apply h₂, simpa using submodule.span_subset_iff.1 (le_trans le_sup_right hn) }, rcases IH (M ⊔ submodule.span {x}) ⟨@le_sup_left _ _ M _, this⟩ (sup_le MN (submodule.span_subset_iff.2 (by simpa))) with ⟨s, hs, hs₂⟩, refine ⟨insert x s, finite_insert _ hs, _⟩, rw [← hs₂, sup_assoc, ← submodule.span_union], simp } end⟩ def is_noetherian_ring (α) [ring α] : Prop := is_noetherian α α theorem is_noetherian_of_submodule_of_noetherian (R M) [ring R] [module R M] (N : set M) [is_submodule N] (h : is_noetherian R M) : is_noetherian R N := begin rw is_noetherian_iff_well_founded at h ⊢, convert order_embedding.well_founded (order_embedding.rsymm (submodule.lt_order_embedding R M N)) h,end theorem is_noetherian_of_quotient_of_noetherian (R) [ring R] (M) [module R M] (N : set M) [is_submodule N] (h : is_noetherian R M) : is_noetherian R (quotient_module.quotient M N) := begin rw is_noetherian_iff_well_founded at h ⊢, convert order_embedding.well_founded (order_embedding.rsymm (quotient_module.lt_order_embedding R M N)) h,end
ab1e4f100809d1ade206bf511cb10058d27c9265
95dcf8dea2baf2b4b0a60d438f27c35ae3dd3990
/src/category_theory/limits/over.lean
b02d458a39318f4999b14e3ed1a8c351ef2d0ab6
[ "Apache-2.0" ]
permissive
uniformity1/mathlib
829341bad9dfa6d6be9adaacb8086a8a492e85a4
dd0e9bd8f2e5ec267f68e72336f6973311909105
refs/heads/master
1,588,592,015,670
1,554,219,842,000
1,554,219,842,000
179,110,702
0
0
Apache-2.0
1,554,220,076,000
1,554,220,076,000
null
UTF-8
Lean
false
false
5,413
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Johan Commelin, Reid Barton import category_theory.comma import category_theory.limits.preserves universes v u -- declare the `v`'s first; see `category_theory.category` for an explanation open category_theory category_theory.limits variables {J : Type v} [small_category J] variables {C : Sort u} [𝒞 : category.{v+1} C] include 𝒞 variable {X : C} namespace category_theory.functor def to_cocone (F : J ⥤ over X) : cocone (F ⋙ over.forget) := { X := X, ι := { app := λ j, (F.obj j).hom } } @[simp] lemma to_cocone_X (F : J ⥤ over X) : F.to_cocone.X = X := rfl @[simp] lemma to_cocone_ι (F : J ⥤ over X) (j : J) : F.to_cocone.ι.app j = (F.obj j).hom := rfl def to_cone (F : J ⥤ under X) : cone (F ⋙ under.forget) := { X := X, π := { app := λ j, (F.obj j).hom } } @[simp] lemma to_cone_X (F : J ⥤ under X) : F.to_cone.X = X := rfl @[simp] lemma to_cone_π (F : J ⥤ under X) (j : J) : F.to_cone.π.app j = (F.obj j).hom := rfl end category_theory.functor namespace category_theory.over def colimit (F : J ⥤ over X) [has_colimit (F ⋙ forget)] : cocone F := { X := mk $ colimit.desc (F ⋙ forget) F.to_cocone, ι := { app := λ j, hom_mk $ colimit.ι (F ⋙ forget) j, naturality' := begin intros j j' f, have := colimit.w (F ⋙ forget) f, tidy end } } @[simp] lemma colimit_X_hom (F : J ⥤ over X) [has_colimit (F ⋙ forget)] : ((colimit F).X).hom = colimit.desc (F ⋙ forget) F.to_cocone := rfl @[simp] lemma colimit_ι_app (F : J ⥤ over X) [has_colimit (F ⋙ forget)] (j : J) : ((colimit F).ι).app j = hom_mk (colimit.ι (F ⋙ forget) j) := rfl def forget_colimit_is_colimit (F : J ⥤ over X) [has_colimit (F ⋙ forget)] : is_colimit (forget.map_cocone (colimit F)) := is_colimit.of_iso_colimit (colimit.is_colimit (F ⋙ forget)) (cocones.ext (iso.refl _) (by tidy)) instance : reflects_colimits (forget : over X ⥤ C) := λ J 𝒥 F, by constructor; exactI λ t ht, { desc := λ s, hom_mk (ht.desc (forget.map_cocone s)) begin apply ht.hom_ext, intro j, rw [←category.assoc, ht.fac], transitivity (F.obj j).hom, exact w (s.ι.app j), -- TODO: How to write (s.ι.app j).w? exact (w (t.ι.app j)).symm, end, fac' := begin intros s j, ext, exact ht.fac (forget.map_cocone s) j -- TODO: Ask Simon about multiple ext lemmas for defeq types (comma_morphism & over.category.hom) end, uniq' := begin intros s m w, ext1 j, exact ht.uniq (forget.map_cocone s) m.left (λ j, congr_arg comma_morphism.left (w j)) end } instance has_colimit {F : J ⥤ over X} [has_colimit (F ⋙ forget)] : has_colimit F := { cocone := colimit F, is_colimit := reflects_colimit.reflects (forget_colimit_is_colimit F) } instance has_colimits_of_shape [has_colimits_of_shape J C] : has_colimits_of_shape J (over X) := λ F, by apply_instance instance has_colimits [has_colimits C] : has_colimits (over X) := λ J 𝒥, by resetI; apply_instance instance forget_preserves_colimits [has_colimits C] {X : C} : preserves_colimits (forget : over X ⥤ C) := λ J 𝒥 F, by exactI preserves_colimit_of_preserves_colimit_cocone (colimit.is_colimit F) (forget_colimit_is_colimit F) end category_theory.over namespace category_theory.under def limit (F : J ⥤ under X) [has_limit (F ⋙ forget)] : cone F := { X := mk $ limit.lift (F ⋙ forget) F.to_cone, π := { app := λ j, hom_mk $ limit.π (F ⋙ forget) j, naturality' := begin intros j j' f, have := (limit.w (F ⋙ forget) f).symm, tidy end } } @[simp] lemma limit_X_hom (F : J ⥤ under X) [has_limit (F ⋙ forget)] : ((limit F).X).hom = limit.lift (F ⋙ forget) F.to_cone := rfl @[simp] lemma limit_π_app (F : J ⥤ under X) [has_limit (F ⋙ forget)] (j : J) : ((limit F).π).app j = hom_mk (limit.π (F ⋙ forget) j) := rfl def forget_limit_is_limit (F : J ⥤ under X) [has_limit (F ⋙ forget)] : is_limit (forget.map_cone (limit F)) := is_limit.of_iso_limit (limit.is_limit (F ⋙ forget)) (cones.ext (iso.refl _) (by tidy)) instance : reflects_limits (forget : under X ⥤ C) := λ J 𝒥 F, by constructor; exactI λ t ht, { lift := λ s, hom_mk (ht.lift (forget.map_cone s)) begin apply ht.hom_ext, intro j, rw [category.assoc, ht.fac], transitivity (F.obj j).hom, exact w (s.π.app j), exact (w (t.π.app j)).symm, end, fac' := begin intros s j, ext, exact ht.fac (forget.map_cone s) j end, uniq' := begin intros s m w, ext1 j, exact ht.uniq (forget.map_cone s) m.right (λ j, congr_arg comma_morphism.right (w j)) end } instance has_limit {F : J ⥤ under X} [has_limit (F ⋙ forget)] : has_limit F := { cone := limit F, is_limit := reflects_limit.reflects (forget_limit_is_limit F) } instance has_limits_of_shape [has_limits_of_shape J C] : has_limits_of_shape J (under X) := λ F, by apply_instance instance has_limits [has_limits C] : has_limits (under X) := λ J 𝒥, by resetI; apply_instance instance forget_preserves_limits [has_limits C] {X : C} : preserves_limits (forget : under X ⥤ C) := λ J 𝒥 F, by exactI preserves_limit_of_preserves_limit_cone (limit.is_limit F) (forget_limit_is_limit F) end category_theory.under
06b3295958164faf2db739d2a578f0e2158f6a9b
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/08_Building_Theories_and_Proofs.org.7.lean
2af229ec3442c5d79d9e061deb2210074f4f60c5
[]
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
437
lean
import standard structure Semigroup : Type := (carrier : Type) (mul : carrier → carrier → carrier) (mul_assoc : ∀ a b c : carrier, mul (mul a b) c = mul a (mul b c)) notation a `*` b := Semigroup.mul _ a b attribute Semigroup.carrier [coercion] structure morphism (S1 S2 : Semigroup) : Type := (mor : S1 → S2) (resp_mul : ∀ a b : S1, mor (a * b) = (mor a) * (mor b)) -- BEGIN local attribute morphism.mor [coercion] -- END
fa55df4d3942e53d3d4250dc25f4ccde50bb07cb
6094e25ea0b7699e642463b48e51b2ead6ddc23f
/library/data/nat/sub.lean
90bae70bf2f493195165449d5a03376c8b907b87
[ "Apache-2.0" ]
permissive
gbaz/lean
a7835c4e3006fbbb079e8f8ffe18aacc45adebfb
a501c308be3acaa50a2c0610ce2e0d71becf8032
refs/heads/master
1,611,198,791,433
1,451,339,111,000
1,451,339,111,000
48,713,797
0
0
null
1,451,338,939,000
1,451,338,939,000
null
UTF-8
Lean
false
false
19,574
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad Subtraction on the natural numbers, as well as min, max, and distance. -/ import .order open eq.ops namespace nat /- subtraction -/ protected theorem sub_zero (n : ℕ) : n - 0 = n := rfl theorem sub_succ (n m : ℕ) : n - succ m = pred (n - m) := rfl protected theorem zero_sub (n : ℕ) : 0 - n = 0 := nat.induction_on n !nat.sub_zero (take k : nat, assume IH : 0 - k = 0, calc 0 - succ k = pred (0 - k) : sub_succ ... = pred 0 : IH ... = 0 : pred_zero) theorem succ_sub_succ (n m : ℕ) : succ n - succ m = n - m := succ_sub_succ_eq_sub n m protected theorem sub_self (n : ℕ) : n - n = 0 := nat.induction_on n !nat.sub_zero (take k IH, !succ_sub_succ ⬝ IH) protected theorem add_sub_add_right (n k m : ℕ) : (n + k) - (m + k) = n - m := nat.induction_on k (calc (n + 0) - (m + 0) = n - (m + 0) : {!add_zero} ... = n - m : {!add_zero}) (take l : nat, assume IH : (n + l) - (m + l) = n - m, calc (n + succ l) - (m + succ l) = succ (n + l) - (m + succ l) : {!add_succ} ... = succ (n + l) - succ (m + l) : {!add_succ} ... = (n + l) - (m + l) : !succ_sub_succ ... = n - m : IH) protected theorem add_sub_add_left (k n m : ℕ) : (k + n) - (k + m) = n - m := add.comm n k ▸ add.comm m k ▸ nat.add_sub_add_right n k m protected theorem add_sub_cancel (n m : ℕ) : n + m - m = n := nat.induction_on m (begin rewrite add_zero end) (take k : ℕ, assume IH : n + k - k = n, calc n + succ k - succ k = succ (n + k) - succ k : add_succ ... = n + k - k : succ_sub_succ ... = n : IH) protected theorem add_sub_cancel_left (n m : ℕ) : n + m - n = m := !add.comm ▸ !nat.add_sub_cancel protected theorem sub_sub (n m k : ℕ) : n - m - k = n - (m + k) := nat.induction_on k (calc n - m - 0 = n - m : nat.sub_zero ... = n - (m + 0) : add_zero) (take l : nat, assume IH : n - m - l = n - (m + l), calc n - m - succ l = pred (n - m - l) : !sub_succ ... = pred (n - (m + l)) : IH ... = n - succ (m + l) : sub_succ ... = n - (m + succ l) : by rewrite add_succ) theorem succ_sub_sub_succ (n m k : ℕ) : succ n - m - succ k = n - m - k := calc succ n - m - succ k = succ n - (m + succ k) : nat.sub_sub ... = succ n - succ (m + k) : add_succ ... = n - (m + k) : succ_sub_succ ... = n - m - k : nat.sub_sub theorem sub_self_add (n m : ℕ) : n - (n + m) = 0 := calc n - (n + m) = n - n - m : nat.sub_sub ... = 0 - m : nat.sub_self ... = 0 : nat.zero_sub protected theorem sub.right_comm (m n k : ℕ) : m - n - k = m - k - n := calc m - n - k = m - (n + k) : !nat.sub_sub ... = m - (k + n) : {!add.comm} ... = m - k - n : !nat.sub_sub⁻¹ theorem sub_one (n : ℕ) : n - 1 = pred n := rfl theorem succ_sub_one (n : ℕ) : succ n - 1 = n := rfl /- interaction with multiplication -/ theorem mul_pred_left (n m : ℕ) : pred n * m = n * m - m := nat.induction_on n (calc pred 0 * m = 0 * m : pred_zero ... = 0 : zero_mul ... = 0 - m : nat.zero_sub ... = 0 * m - m : zero_mul) (take k : nat, assume IH : pred k * m = k * m - m, calc pred (succ k) * m = k * m : pred_succ ... = k * m + m - m : nat.add_sub_cancel ... = succ k * m - m : succ_mul) theorem mul_pred_right (n m : ℕ) : n * pred m = n * m - n := calc n * pred m = pred m * n : mul.comm ... = m * n - n : mul_pred_left ... = n * m - n : mul.comm protected theorem mul_sub_right_distrib (n m k : ℕ) : (n - m) * k = n * k - m * k := nat.induction_on m (calc (n - 0) * k = n * k : nat.sub_zero ... = n * k - 0 : nat.sub_zero ... = n * k - 0 * k : zero_mul) (take l : nat, assume IH : (n - l) * k = n * k - l * k, calc (n - succ l) * k = pred (n - l) * k : sub_succ ... = (n - l) * k - k : mul_pred_left ... = n * k - l * k - k : IH ... = n * k - (l * k + k) : nat.sub_sub ... = n * k - (succ l * k) : succ_mul) protected theorem mul_sub_left_distrib (n m k : ℕ) : n * (m - k) = n * m - n * k := calc n * (m - k) = (m - k) * n : !mul.comm ... = m * n - k * n : !nat.mul_sub_right_distrib ... = n * m - k * n : {!mul.comm} ... = n * m - n * k : {!mul.comm} protected theorem mul_self_sub_mul_self_eq (a b : nat) : a * a - b * b = (a + b) * (a - b) := by rewrite [nat.mul_sub_left_distrib, *right_distrib, mul.comm b a, add.comm (a*a) (a*b), nat.add_sub_add_left] theorem succ_mul_succ_eq (a : nat) : succ a * succ a = a*a + a + a + 1 := calc succ a * succ a = (a+1)*(a+1) : by rewrite [add_one] ... = a*a + a + a + 1 : by rewrite [right_distrib, left_distrib, one_mul, mul_one] /- interaction with inequalities -/ theorem succ_sub {m n : ℕ} : m ≥ n → succ m - n = succ (m - n) := sub_induction n m (take k, assume H : 0 ≤ k, rfl) (take k, assume H : succ k ≤ 0, absurd H !not_succ_le_zero) (take k l, assume IH : k ≤ l → succ l - k = succ (l - k), take H : succ k ≤ succ l, calc succ (succ l) - succ k = succ l - k : succ_sub_succ ... = succ (l - k) : IH (le_of_succ_le_succ H) ... = succ (succ l - succ k) : succ_sub_succ) theorem sub_eq_zero_of_le {n m : ℕ} (H : n ≤ m) : n - m = 0 := obtain (k : ℕ) (Hk : n + k = m), from le.elim H, Hk ▸ !sub_self_add theorem add_sub_of_le {n m : ℕ} : n ≤ m → n + (m - n) = m := sub_induction n m (take k, assume H : 0 ≤ k, calc 0 + (k - 0) = k - 0 : zero_add ... = k : nat.sub_zero) (take k, assume H : succ k ≤ 0, absurd H !not_succ_le_zero) (take k l, assume IH : k ≤ l → k + (l - k) = l, take H : succ k ≤ succ l, calc succ k + (succ l - succ k) = succ k + (l - k) : succ_sub_succ ... = succ (k + (l - k)) : succ_add ... = succ l : IH (le_of_succ_le_succ H)) theorem add_sub_of_ge {n m : ℕ} (H : n ≥ m) : n + (m - n) = n := calc n + (m - n) = n + 0 : sub_eq_zero_of_le H ... = n : add_zero protected theorem sub_add_cancel {n m : ℕ} : n ≥ m → n - m + m = n := !add.comm ▸ !add_sub_of_le theorem sub_add_of_le {n m : ℕ} : n ≤ m → n - m + m = m := !add.comm ▸ add_sub_of_ge theorem sub.cases {P : ℕ → Prop} {n m : ℕ} (H1 : n ≤ m → P 0) (H2 : ∀k, m + k = n -> P k) : P (n - m) := or.elim !le.total (assume H3 : n ≤ m, (sub_eq_zero_of_le H3)⁻¹ ▸ (H1 H3)) (assume H3 : m ≤ n, H2 (n - m) (add_sub_of_le H3)) theorem exists_sub_eq_of_le {n m : ℕ} (H : n ≤ m) : ∃k, m - k = n := obtain (k : ℕ) (Hk : n + k = m), from le.elim H, exists.intro k (calc m - k = n + k - k : by rewrite Hk ... = n : nat.add_sub_cancel) protected theorem add_sub_assoc {m k : ℕ} (H : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) := have l1 : k ≤ m → n + m - k = n + (m - k), from sub_induction k m (take m : ℕ, assume H : 0 ≤ m, calc n + m - 0 = n + m : nat.sub_zero ... = n + (m - 0) : nat.sub_zero) (take k : ℕ, assume H : succ k ≤ 0, absurd H !not_succ_le_zero) (take k m, assume IH : k ≤ m → n + m - k = n + (m - k), take H : succ k ≤ succ m, calc n + succ m - succ k = succ (n + m) - succ k : add_succ ... = n + m - k : succ_sub_succ ... = n + (m - k) : IH (le_of_succ_le_succ H) ... = n + (succ m - succ k) : succ_sub_succ), l1 H theorem le_of_sub_eq_zero {n m : ℕ} : n - m = 0 → n ≤ m := sub.cases (assume H1 : n ≤ m, assume H2 : 0 = 0, H1) (take k : ℕ, assume H1 : m + k = n, assume H2 : k = 0, have H3 : n = m, from !add_zero ▸ H2 ▸ H1⁻¹, H3 ▸ !le.refl) theorem sub_sub.cases {P : ℕ → ℕ → Prop} {n m : ℕ} (H1 : ∀k, n = m + k -> P k 0) (H2 : ∀k, m = n + k → P 0 k) : P (n - m) (m - n) := or.elim !le.total (assume H3 : n ≤ m, (sub_eq_zero_of_le H3)⁻¹ ▸ (H2 (m - n) (add_sub_of_le H3)⁻¹)) (assume H3 : m ≤ n, (sub_eq_zero_of_le H3)⁻¹ ▸ (H1 (n - m) (add_sub_of_le H3)⁻¹)) protected theorem sub_eq_of_add_eq {n m k : ℕ} (H : n + m = k) : k - n = m := have H2 : k - n + n = m + n, from calc k - n + n = k : nat.sub_add_cancel (le.intro H) ... = n + m : H⁻¹ ... = m + n : !add.comm, add.right_cancel H2 protected theorem eq_sub_of_add_eq {a b c : ℕ} (H : a + c = b) : a = b - c := (nat.sub_eq_of_add_eq (!add.comm ▸ H))⁻¹ protected theorem sub_eq_of_eq_add {a b c : ℕ} (H : a = c + b) : a - b = c := nat.sub_eq_of_add_eq (!add.comm ▸ H⁻¹) protected theorem sub_le_sub_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n - k ≤ m - k := obtain (l : ℕ) (Hl : n + l = m), from le.elim H, or.elim !le.total (assume H2 : n ≤ k, (sub_eq_zero_of_le H2)⁻¹ ▸ !zero_le) (assume H2 : k ≤ n, have H3 : n - k + l = m - k, from calc n - k + l = l + (n - k) : add.comm ... = l + n - k : nat.add_sub_assoc H2 l ... = n + l - k : add.comm ... = m - k : Hl, le.intro H3) protected theorem sub_le_sub_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k - m ≤ k - n := obtain (l : ℕ) (Hl : n + l = m), from le.elim H, sub.cases (assume H2 : k ≤ m, !zero_le) (take m' : ℕ, assume Hm : m + m' = k, have H3 : n ≤ k, from le.trans H (le.intro Hm), have H4 : m' + l + n = k - n + n, from calc m' + l + n = n + (m' + l) : add.comm ... = n + (l + m') : add.comm ... = n + l + m' : add.assoc ... = m + m' : Hl ... = k : Hm ... = k - n + n : nat.sub_add_cancel H3, le.intro (add.right_cancel H4)) protected theorem sub_pos_of_lt {m n : ℕ} (H : m < n) : n - m > 0 := assert H1 : n = n - m + m, from (nat.sub_add_cancel (le_of_lt H))⁻¹, have H2 : 0 + m < n - m + m, begin rewrite [zero_add, -H1], exact H end, !lt_of_add_lt_add_right H2 protected theorem lt_of_sub_pos {m n : ℕ} (H : n - m > 0) : m < n := lt_of_not_ge (take H1 : m ≥ n, have H2 : n - m = 0, from sub_eq_zero_of_le H1, !lt.irrefl (H2 ▸ H)) protected theorem lt_of_sub_lt_sub_right {n m k : ℕ} (H : n - k < m - k) : n < m := lt_of_not_ge (assume H1 : m ≤ n, have H2 : m - k ≤ n - k, from nat.sub_le_sub_right H1 _, not_le_of_gt H H2) protected theorem lt_of_sub_lt_sub_left {n m k : ℕ} (H : n - m < n - k) : k < m := lt_of_not_ge (assume H1 : m ≤ k, have H2 : n - k ≤ n - m, from nat.sub_le_sub_left H1 _, not_le_of_gt H H2) protected theorem sub_lt_sub_add_sub (n m k : ℕ) : n - k ≤ (n - m) + (m - k) := sub.cases (assume H : n ≤ m, (zero_add (m - k))⁻¹ ▸ nat.sub_le_sub_right H k) (take mn : ℕ, assume Hmn : m + mn = n, sub.cases (assume H : m ≤ k, have H2 : n - k ≤ n - m, from nat.sub_le_sub_left H n, assert H3 : n - k ≤ mn, from nat.sub_eq_of_add_eq Hmn ▸ H2, show n - k ≤ mn + 0, begin rewrite add_zero, assumption end) (take km : ℕ, assume Hkm : k + km = m, have H : k + (mn + km) = n, from calc k + (mn + km) = k + (km + mn): add.comm ... = k + km + mn : add.assoc ... = m + mn : Hkm ... = n : Hmn, have H2 : n - k = mn + km, from nat.sub_eq_of_add_eq H, H2 ▸ !le.refl)) protected theorem sub_lt_self {m n : ℕ} (H1 : m > 0) (H2 : n > 0) : m - n < m := calc m - n = succ (pred m) - n : succ_pred_of_pos H1 ... = succ (pred m) - succ (pred n) : succ_pred_of_pos H2 ... = pred m - pred n : succ_sub_succ ... ≤ pred m : sub_le ... < succ (pred m) : lt_succ_self ... = m : succ_pred_of_pos H1 protected theorem le_sub_of_add_le {m n k : ℕ} (H : m + k ≤ n) : m ≤ n - k := calc m = m + k - k : nat.add_sub_cancel ... ≤ n - k : nat.sub_le_sub_right H k protected theorem lt_sub_of_add_lt {m n k : ℕ} (H : m + k < n) (H2 : k ≤ n) : m < n - k := lt_of_succ_le (nat.le_sub_of_add_le (calc succ m + k = succ (m + k) : succ_add_eq_succ_add ... ≤ n : succ_le_of_lt H)) protected theorem sub_lt_of_lt_add {v n m : nat} (h₁ : v < n + m) (h₂ : n ≤ v) : v - n < m := have succ v ≤ n + m, from succ_le_of_lt h₁, have succ (v - n) ≤ m, from calc succ (v - n) = succ v - n : succ_sub h₂ ... ≤ n + m - n : nat.sub_le_sub_right this n ... = m : nat.add_sub_cancel_left, lt_of_succ_le this /- distance -/ definition dist [reducible] (n m : ℕ) := (n - m) + (m - n) theorem dist.comm (n m : ℕ) : dist n m = dist m n := !add.comm theorem dist_self (n : ℕ) : dist n n = 0 := calc (n - n) + (n - n) = 0 + (n - n) : nat.sub_self ... = 0 + 0 : nat.sub_self ... = 0 : rfl theorem eq_of_dist_eq_zero {n m : ℕ} (H : dist n m = 0) : n = m := have H2 : n - m = 0, from eq_zero_of_add_eq_zero_right H, have H3 : n ≤ m, from le_of_sub_eq_zero H2, have H4 : m - n = 0, from eq_zero_of_add_eq_zero_left H, have H5 : m ≤ n, from le_of_sub_eq_zero H4, le.antisymm H3 H5 theorem dist_eq_zero {n m : ℕ} (H : n = m) : dist n m = 0 := by substvars; rewrite [↑dist, *nat.sub_self, add_zero] theorem dist_eq_sub_of_le {n m : ℕ} (H : n ≤ m) : dist n m = m - n := calc dist n m = 0 + (m - n) : {sub_eq_zero_of_le H} ... = m - n : zero_add theorem dist_eq_sub_of_lt {n m : ℕ} (H : n < m) : dist n m = m - n := dist_eq_sub_of_le (le_of_lt H) theorem dist_eq_sub_of_ge {n m : ℕ} (H : n ≥ m) : dist n m = n - m := !dist.comm ▸ dist_eq_sub_of_le H theorem dist_eq_sub_of_gt {n m : ℕ} (H : n > m) : dist n m = n - m := dist_eq_sub_of_ge (le_of_lt H) theorem dist_zero_right (n : ℕ) : dist n 0 = n := dist_eq_sub_of_ge !zero_le ⬝ !nat.sub_zero theorem dist_zero_left (n : ℕ) : dist 0 n = n := dist_eq_sub_of_le !zero_le ⬝ !nat.sub_zero theorem dist.intro {n m k : ℕ} (H : n + m = k) : dist k n = m := calc dist k n = k - n : dist_eq_sub_of_ge (le.intro H) ... = m : nat.sub_eq_of_add_eq H theorem dist_add_add_right (n k m : ℕ) : dist (n + k) (m + k) = dist n m := calc dist (n + k) (m + k) = ((n+k) - (m+k)) + ((m+k)-(n+k)) : rfl ... = (n - m) + ((m + k) - (n + k)) : nat.add_sub_add_right ... = (n - m) + (m - n) : nat.add_sub_add_right theorem dist_add_add_left (k n m : ℕ) : dist (k + n) (k + m) = dist n m := begin rewrite [add.comm k n, add.comm k m]; apply dist_add_add_right end theorem dist_add_eq_of_ge {n m : ℕ} (H : n ≥ m) : dist n m + m = n := calc dist n m + m = n - m + m : {dist_eq_sub_of_ge H} ... = n : nat.sub_add_cancel H theorem dist_eq_intro {n m k l : ℕ} (H : n + m = k + l) : dist n k = dist l m := calc dist n k = dist (n + m) (k + m) : dist_add_add_right ... = dist (k + l) (k + m) : H ... = dist l m : dist_add_add_left theorem dist_sub_eq_dist_add_left {n m : ℕ} (H : n ≥ m) (k : ℕ) : dist (n - m) k = dist n (k + m) := have H2 : n - m + (k + m) = k + n, from calc n - m + (k + m) = n - m + (m + k) : add.comm ... = n - m + m + k : add.assoc ... = n + k : nat.sub_add_cancel H ... = k + n : add.comm, dist_eq_intro H2 theorem dist_sub_eq_dist_add_right {k m : ℕ} (H : k ≥ m) (n : ℕ) : dist n (k - m) = dist (n + m) k := dist.comm (k - m) n ▸ dist.comm k (n + m) ▸ dist_sub_eq_dist_add_left H n theorem dist.triangle_inequality (n m k : ℕ) : dist n k ≤ dist n m + dist m k := have (n - m) + (m - k) + ((k - m) + (m - n)) = (n - m) + (m - n) + ((m - k) + (k - m)), begin rewrite [add.comm (k - m) (m - n), {n - m + _ + _}add.assoc, {m - k + _}add.left_comm, -add.assoc] end, this ▸ add_le_add !nat.sub_lt_sub_add_sub !nat.sub_lt_sub_add_sub theorem dist_add_add_le_add_dist_dist (n m k l : ℕ) : dist (n + m) (k + l) ≤ dist n k + dist m l := assert H : dist (n + m) (k + m) + dist (k + m) (k + l) = dist n k + dist m l, by rewrite [dist_add_add_left, dist_add_add_right], by rewrite -H; apply dist.triangle_inequality theorem dist_mul_right (n k m : ℕ) : dist (n * k) (m * k) = dist n m * k := assert ∀ n m, dist n m = n - m + (m - n), from take n m, rfl, by rewrite [this, this n m, right_distrib, *nat.mul_sub_right_distrib] theorem dist_mul_left (k n m : ℕ) : dist (k * n) (k * m) = k * dist n m := begin rewrite [mul.comm k n, mul.comm k m, dist_mul_right, mul.comm] end theorem dist_mul_dist (n m k l : ℕ) : dist n m * dist k l = dist (n * k + m * l) (n * l + m * k) := have aux : ∀k l, k ≥ l → dist n m * dist k l = dist (n * k + m * l) (n * l + m * k), from take k l : ℕ, assume H : k ≥ l, have H2 : m * k ≥ m * l, from !mul_le_mul_left H, have H3 : n * l + m * k ≥ m * l, from le.trans H2 !le_add_left, calc dist n m * dist k l = dist n m * (k - l) : dist_eq_sub_of_ge H ... = dist (n * (k - l)) (m * (k - l)) : dist_mul_right ... = dist (n * k - n * l) (m * k - m * l) : by rewrite [*nat.mul_sub_left_distrib] ... = dist (n * k) (m * k - m * l + n * l) : dist_sub_eq_dist_add_left (!mul_le_mul_left H) ... = dist (n * k) (n * l + (m * k - m * l)) : add.comm ... = dist (n * k) (n * l + m * k - m * l) : nat.add_sub_assoc H2 (n * l) ... = dist (n * k + m * l) (n * l + m * k) : dist_sub_eq_dist_add_right H3 _, or.elim !le.total (assume H : k ≤ l, !dist.comm ▸ !dist.comm ▸ aux l k H) (assume H : l ≤ k, aux k l H) lemma dist_eq_max_sub_min {i j : nat} : dist i j = (max i j) - min i j := or.elim (lt_or_ge i j) (suppose i < j, by rewrite [max_eq_right_of_lt this, min_eq_left_of_lt this, dist_eq_sub_of_lt this]) (suppose i ≥ j, by rewrite [max_eq_left this , min_eq_right this, dist_eq_sub_of_ge this]) lemma dist_succ {i j : nat} : dist (succ i) (succ j) = dist i j := by rewrite [↑dist, *succ_sub_succ] lemma dist_le_max {i j : nat} : dist i j ≤ max i j := begin rewrite dist_eq_max_sub_min, apply sub_le end lemma dist_pos_of_ne {i j : nat} : i ≠ j → dist i j > 0 := assume Pne, lt.by_cases (suppose i < j, begin rewrite [dist_eq_sub_of_lt this], apply nat.sub_pos_of_lt this end) (suppose i = j, by contradiction) (suppose i > j, begin rewrite [dist_eq_sub_of_gt this], apply nat.sub_pos_of_lt this end) end nat
e6632ea6aa54f1ba863157426a2ca0c7e46db9b2
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/analysis/normed_space/linear_isometry.lean
ece2c2e8a30c1e96174e2379c232201f42c4bd62
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
13,258
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.metric_space.isometry /-! # Linear isometries In this file we define `linear_isometry R E F` (notation: `E →ₗᵢ[R] F`) to be a linear isometric embedding of `E` into `F` and `linear_isometry_equiv` (notation: `E ≃ₗᵢ[R] F`) to be a linear isometric equivalence between `E` and `F`. We also prove some trivial lemmas and provide convenience constructors. Since a lot of elementary properties don't require `∥x∥ = 0 → x = 0` we start setting up the theory for `semi_normed_space` and we specialize to `normed_space` when needed. -/ open function set variables {R E F G G' E₁ : Type*} [semiring R] [semi_normed_group E] [semi_normed_group F] [semi_normed_group G] [semi_normed_group G'] [module R E] [module R F] [module R G] [module R G'] [normed_group E₁] [module R E₁] /-- An `R`-linear isometric embedding of one normed `R`-module into another. -/ structure linear_isometry (R E F : Type*) [semiring R] [semi_normed_group E] [semi_normed_group F] [module R E] [module R F] extends E →ₗ[R] F := (norm_map' : ∀ x, ∥to_linear_map x∥ = ∥x∥) notation E ` →ₗᵢ[`:25 R:25 `] `:0 F:0 := linear_isometry R E F namespace linear_isometry /-- We use `f₁` when we need the domain to be a `normed_space`. -/ variables (f : E →ₗᵢ[R] F) (f₁ : E₁ →ₗᵢ[R] F) instance : has_coe_to_fun (E →ₗᵢ[R] F) := ⟨_, λ f, f.to_fun⟩ @[simp] lemma coe_to_linear_map : ⇑f.to_linear_map = f := rfl lemma to_linear_map_injective : injective (to_linear_map : (E →ₗᵢ[R] F) → (E →ₗ[R] F)) | ⟨f, _⟩ ⟨g, _⟩ rfl := rfl lemma coe_fn_injective : injective (λ (f : E →ₗᵢ[R] F) (x : E), f x) := linear_map.coe_injective.comp to_linear_map_injective @[ext] lemma ext {f g : E →ₗᵢ[R] F} (h : ∀ x, f x = g x) : f = g := coe_fn_injective $ funext h @[simp] lemma map_zero : f 0 = 0 := f.to_linear_map.map_zero @[simp] lemma map_add (x y : E) : f (x + y) = f x + f y := f.to_linear_map.map_add x y @[simp] lemma map_sub (x y : E) : f (x - y) = f x - f y := f.to_linear_map.map_sub x y @[simp] lemma map_smul (c : R) (x : E) : f (c • x) = c • f x := f.to_linear_map.map_smul c x @[simp] lemma norm_map (x : E) : ∥f x∥ = ∥x∥ := f.norm_map' x @[simp] lemma nnnorm_map (x : E) : nnnorm (f x) = nnnorm x := nnreal.eq $ f.norm_map x protected lemma isometry : isometry f := f.to_linear_map.to_add_monoid_hom.isometry_of_norm f.norm_map @[simp] lemma dist_map (x y : E) : dist (f x) (f y) = dist x y := f.isometry.dist_eq x y @[simp] lemma edist_map (x y : E) : edist (f x) (f y) = edist x y := f.isometry.edist_eq x y protected lemma injective : injective f₁ := f₁.isometry.injective @[simp] lemma map_eq_iff {x y : E₁} : f₁ x = f₁ y ↔ x = y := f₁.injective.eq_iff lemma map_ne {x y : E₁} (h : x ≠ y) : f₁ x ≠ f₁ y := f₁.injective.ne h protected lemma lipschitz : lipschitz_with 1 f := f.isometry.lipschitz protected lemma antilipschitz : antilipschitz_with 1 f := f.isometry.antilipschitz @[continuity] protected lemma continuous : continuous f := f.isometry.continuous lemma ediam_image (s : set E) : emetric.diam (f '' s) = emetric.diam s := f.isometry.ediam_image s lemma ediam_range : emetric.diam (range f) = emetric.diam (univ : set E) := f.isometry.ediam_range lemma diam_image (s : set E) : metric.diam (f '' s) = metric.diam s := f.isometry.diam_image s lemma diam_range : metric.diam (range f) = metric.diam (univ : set E) := f.isometry.diam_range /-- Interpret a linear isometry as a continuous linear map. -/ def to_continuous_linear_map : E →L[R] F := ⟨f.to_linear_map, f.continuous⟩ @[simp] lemma coe_to_continuous_linear_map : ⇑f.to_continuous_linear_map = f := rfl @[simp] lemma comp_continuous_iff {α : Type*} [topological_space α] {g : α → E} : continuous (f ∘ g) ↔ continuous g := f.isometry.comp_continuous_iff /-- The identity linear isometry. -/ def id : E →ₗᵢ[R] E := ⟨linear_map.id, λ x, rfl⟩ @[simp] lemma coe_id : ⇑(id : E →ₗᵢ[R] E) = id := rfl instance : inhabited (E →ₗᵢ[R] E) := ⟨id⟩ /-- Composition of linear isometries. -/ def comp (g : F →ₗᵢ[R] G) (f : E →ₗᵢ[R] F) : E →ₗᵢ[R] G := ⟨g.to_linear_map.comp f.to_linear_map, λ x, (g.norm_map _).trans (f.norm_map _)⟩ @[simp] lemma coe_comp (g : F →ₗᵢ[R] G) (f : E →ₗᵢ[R] F) : ⇑(g.comp f) = g ∘ f := rfl @[simp] lemma id_comp : (id : F →ₗᵢ[R] F).comp f = f := ext $ λ x, rfl @[simp] lemma comp_id : f.comp id = f := ext $ λ x, rfl lemma comp_assoc (f : G →ₗᵢ[R] G') (g : F →ₗᵢ[R] G) (h : E →ₗᵢ[R] F) : (f.comp g).comp h = f.comp (g.comp h) := rfl instance : monoid (E →ₗᵢ[R] E) := { one := id, mul := comp, mul_assoc := comp_assoc, one_mul := id_comp, mul_one := comp_id } @[simp] lemma coe_one : ⇑(1 : E →ₗᵢ[R] E) = id := rfl @[simp] lemma coe_mul (f g : E →ₗᵢ[R] E) : ⇑(f * g) = f ∘ g := rfl end linear_isometry namespace submodule variables {R' : Type*} [ring R'] [module R' E] (p : submodule R' E) /-- `submodule.subtype` as a `linear_isometry`. -/ def subtypeₗᵢ : p →ₗᵢ[R'] E := ⟨p.subtype, λ x, rfl⟩ @[simp] lemma coe_subtypeₗᵢ : ⇑p.subtypeₗᵢ = p.subtype := rfl @[simp] lemma subtypeₗᵢ_to_linear_map : p.subtypeₗᵢ.to_linear_map = p.subtype := rfl /-- `submodule.subtype` as a `continuous_linear_map`. -/ def subtypeL : p →L[R'] E := p.subtypeₗᵢ.to_continuous_linear_map @[simp] lemma coe_subtypeL : (p.subtypeL : p →ₗ[R'] E) = p.subtype := rfl @[simp] lemma coe_subtypeL' : ⇑p.subtypeL = p.subtype := rfl @[simp] lemma range_subtypeL : p.subtypeL.range = p := range_subtype _ @[simp] lemma ker_subtypeL : p.subtypeL.ker = ⊥ := ker_subtype _ end submodule /-- A linear isometric equivalence between two normed vector spaces. -/ structure linear_isometry_equiv (R E F : Type*) [semiring R] [semi_normed_group E] [semi_normed_group F] [module R E] [module R F] extends E ≃ₗ[R] F := (norm_map' : ∀ x, ∥to_linear_equiv x∥ = ∥x∥) notation E ` ≃ₗᵢ[`:25 R:25 `] `:0 F:0 := linear_isometry_equiv R E F namespace linear_isometry_equiv variables (e : E ≃ₗᵢ[R] F) instance : has_coe_to_fun (E ≃ₗᵢ[R] F) := ⟨_, λ f, f.to_fun⟩ @[simp] lemma coe_mk (e : E ≃ₗ[R] F) (he : ∀ x, ∥e x∥ = ∥x∥) : ⇑(mk e he) = e := rfl @[simp] lemma coe_to_linear_equiv (e : E ≃ₗᵢ[R] F) : ⇑e.to_linear_equiv = e := rfl lemma to_linear_equiv_injective : injective (to_linear_equiv : (E ≃ₗᵢ[R] F) → (E ≃ₗ[R] F)) | ⟨e, _⟩ ⟨_, _⟩ rfl := rfl @[ext] lemma ext {e e' : E ≃ₗᵢ[R] F} (h : ∀ x, e x = e' x) : e = e' := to_linear_equiv_injective $ linear_equiv.ext h /-- Construct a `linear_isometry_equiv` from a `linear_equiv` and two inequalities: `∀ x, ∥e x∥ ≤ ∥x∥` and `∀ y, ∥e.symm y∥ ≤ ∥y∥`. -/ def of_bounds (e : E ≃ₗ[R] F) (h₁ : ∀ x, ∥e x∥ ≤ ∥x∥) (h₂ : ∀ y, ∥e.symm y∥ ≤ ∥y∥) : E ≃ₗᵢ[R] F := ⟨e, λ x, le_antisymm (h₁ x) $ by simpa only [e.symm_apply_apply] using h₂ (e x)⟩ @[simp] lemma norm_map (x : E) : ∥e x∥ = ∥x∥ := e.norm_map' x /-- Reinterpret a `linear_isometry_equiv` as a `linear_isometry`. -/ def to_linear_isometry : E →ₗᵢ[R] F := ⟨e.1, e.2⟩ @[simp] lemma coe_to_linear_isometry : ⇑e.to_linear_isometry = e := rfl protected lemma isometry : isometry e := e.to_linear_isometry.isometry /-- Reinterpret a `linear_isometry_equiv` as an `isometric`. -/ def to_isometric : E ≃ᵢ F := ⟨e.to_linear_equiv.to_equiv, e.isometry⟩ @[simp] lemma coe_to_isometric : ⇑e.to_isometric = e := rfl /-- Reinterpret a `linear_isometry_equiv` as an `homeomorph`. -/ def to_homeomorph : E ≃ₜ F := e.to_isometric.to_homeomorph @[simp] lemma coe_to_homeomorph : ⇑e.to_homeomorph = e := rfl protected lemma continuous : continuous e := e.isometry.continuous protected lemma continuous_at {x} : continuous_at e x := e.continuous.continuous_at protected lemma continuous_on {s} : continuous_on e s := e.continuous.continuous_on protected lemma continuous_within_at {s x} : continuous_within_at e s x := e.continuous.continuous_within_at variables (R E) /-- Identity map as a `linear_isometry_equiv`. -/ def refl : E ≃ₗᵢ[R] E := ⟨linear_equiv.refl R E, λ x, rfl⟩ variables {R E} instance : inhabited (E ≃ₗᵢ[R] E) := ⟨refl R E⟩ @[simp] lemma coe_refl : ⇑(refl R E) = id := rfl /-- The inverse `linear_isometry_equiv`. -/ def symm : F ≃ₗᵢ[R] E := ⟨e.to_linear_equiv.symm, λ x, (e.norm_map _).symm.trans $ congr_arg norm $ e.to_linear_equiv.apply_symm_apply x⟩ @[simp] lemma apply_symm_apply (x : F) : e (e.symm x) = x := e.to_linear_equiv.apply_symm_apply x @[simp] lemma symm_apply_apply (x : E) : e.symm (e x) = x := e.to_linear_equiv.symm_apply_apply x @[simp] lemma map_eq_zero_iff {x : E} : e x = 0 ↔ x = 0 := e.to_linear_equiv.map_eq_zero_iff @[simp] lemma symm_symm : e.symm.symm = e := ext $ λ x, rfl @[simp] lemma to_linear_equiv_symm : e.to_linear_equiv.symm = e.symm.to_linear_equiv := rfl @[simp] lemma to_isometric_symm : e.to_isometric.symm = e.symm.to_isometric := rfl @[simp] lemma to_homeomorph_symm : e.to_homeomorph.symm = e.symm.to_homeomorph := rfl /-- Composition of `linear_isometry_equiv`s as a `linear_isometry_equiv`. -/ def trans (e' : F ≃ₗᵢ[R] G) : E ≃ₗᵢ[R] G := ⟨e.to_linear_equiv.trans e'.to_linear_equiv, λ x, (e'.norm_map _).trans (e.norm_map _)⟩ @[simp] lemma coe_trans (e₁ : E ≃ₗᵢ[R] F) (e₂ : F ≃ₗᵢ[R] G) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl @[simp] lemma trans_refl : e.trans (refl R F) = e := ext $ λ x, rfl @[simp] lemma refl_trans : (refl R E).trans e = e := ext $ λ x, rfl @[simp] lemma trans_symm : e.trans e.symm = refl R E := ext e.symm_apply_apply @[simp] lemma symm_trans : e.symm.trans e = refl R F := ext e.apply_symm_apply @[simp] lemma coe_symm_trans (e₁ : E ≃ₗᵢ[R] F) (e₂ : F ≃ₗᵢ[R] G) : ⇑(e₁.trans e₂).symm = e₁.symm ∘ e₂.symm := rfl lemma trans_assoc (eEF : E ≃ₗᵢ[R] F) (eFG : F ≃ₗᵢ[R] G) (eGG' : G ≃ₗᵢ[R] G') : eEF.trans (eFG.trans eGG') = (eEF.trans eFG).trans eGG' := rfl instance : group (E ≃ₗᵢ[R] E) := { mul := λ e₁ e₂, e₂.trans e₁, one := refl _ _, inv := symm, one_mul := trans_refl, mul_one := refl_trans, mul_assoc := λ _ _ _, trans_assoc _ _ _, mul_left_inv := trans_symm } @[simp] lemma coe_one : ⇑(1 : E ≃ₗᵢ[R] E) = id := rfl @[simp] lemma coe_mul (e e' : E ≃ₗᵢ[R] E) : ⇑(e * e') = e ∘ e' := rfl @[simp] lemma coe_inv (e : E ≃ₗᵢ[R] E) : ⇑(e⁻¹) = e.symm := rfl /-- Reinterpret a `linear_isometry_equiv` as a `continuous_linear_equiv`. -/ instance : has_coe_t (E ≃ₗᵢ[R] F) (E ≃L[R] F) := ⟨λ e, ⟨e.to_linear_equiv, e.continuous, e.to_isometric.symm.continuous⟩⟩ instance : has_coe_t (E ≃ₗᵢ[R] F) (E →L[R] F) := ⟨λ e, ↑(e : E ≃L[R] F)⟩ @[simp] lemma coe_coe : ⇑(e : E ≃L[R] F) = e := rfl @[simp] lemma coe_coe' : ((e : E ≃L[R] F) : E →L[R] F) = e := rfl @[simp] lemma coe_coe'' : ⇑(e : E →L[R] F) = e := rfl @[simp] lemma map_zero : e 0 = 0 := e.1.map_zero @[simp] lemma map_add (x y : E) : e (x + y) = e x + e y := e.1.map_add x y @[simp] lemma map_sub (x y : E) : e (x - y) = e x - e y := e.1.map_sub x y @[simp] lemma map_smul (c : R) (x : E) : e (c • x) = c • e x := e.1.map_smul c x @[simp] lemma nnnorm_map (x : E) : nnnorm (e x) = nnnorm x := e.to_linear_isometry.nnnorm_map x @[simp] lemma dist_map (x y : E) : dist (e x) (e y) = dist x y := e.to_linear_isometry.dist_map x y @[simp] lemma edist_map (x y : E) : edist (e x) (e y) = edist x y := e.to_linear_isometry.edist_map x y protected lemma bijective : bijective e := e.1.bijective protected lemma injective : injective e := e.1.injective protected lemma surjective : surjective e := e.1.surjective @[simp] lemma map_eq_iff {x y : E} : e x = e y ↔ x = y := e.injective.eq_iff lemma map_ne {x y : E} (h : x ≠ y) : e x ≠ e y := e.injective.ne h protected lemma lipschitz : lipschitz_with 1 e := e.isometry.lipschitz protected lemma antilipschitz : antilipschitz_with 1 e := e.isometry.antilipschitz @[simp] lemma ediam_image (s : set E) : emetric.diam (e '' s) = emetric.diam s := e.isometry.ediam_image s @[simp] lemma diam_image (s : set E) : metric.diam (e '' s) = metric.diam s := e.isometry.diam_image s variables {α : Type*} [topological_space α] @[simp] lemma comp_continuous_on_iff {f : α → E} {s : set α} : continuous_on (e ∘ f) s ↔ continuous_on f s := e.isometry.comp_continuous_on_iff @[simp] lemma comp_continuous_iff {f : α → E} : continuous (e ∘ f) ↔ continuous f := e.isometry.comp_continuous_iff @[simp] lemma linear_isometry.id_apply (x : E) : (linear_isometry.id : E →ₗᵢ[R] E) x = x := rfl @[simp] lemma linear_isometry.id_to_linear_map : (linear_isometry.id.to_linear_map : E →ₗ[R] E) = linear_map.id := rfl end linear_isometry_equiv
4d512364358927a819cdc7c80eb5632dd383f5fb
e39f04f6ff425fe3b3f5e26a8998b817d1dba80f
/category_theory/examples/rings.lean
b39e2ddce4adee1f0c39e61ad5aa06d62ae5b22e
[ "Apache-2.0" ]
permissive
kristychoi/mathlib
c504b5e8f84e272ea1d8966693c42de7523bf0ec
257fd84fe98927ff4a5ffe044f68c4e9d235cc75
refs/heads/master
1,586,520,722,896
1,544,030,145,000
1,544,031,933,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,793
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl Introduce CommRing -- the category of commutative rings. Currently only the basic setup. -/ import category_theory.examples.monoids import category_theory.fully_faithful import algebra.ring universes u v open category_theory namespace category_theory.examples /-- The category of rings. -/ @[reducible] def Ring : Type (u+1) := bundled ring instance (x : Ring) : ring x := x.str instance concrete_is_ring_hom : concrete_category @is_ring_hom := ⟨by introsI α ia; apply_instance, by introsI α β γ ia ib ic f g hf hg; apply_instance⟩ instance Ring_hom_is_ring_hom {R S : Ring} (f : R ⟶ S) : is_ring_hom (f : R → S) := f.2 /-- The category of commutative rings. -/ @[reducible] def CommRing : Type (u+1) := bundled comm_ring instance (x : CommRing) : comm_ring x := x.str @[reducible] def is_comm_ring_hom {α β} [comm_ring α] [comm_ring β] (f : α → β) : Prop := is_ring_hom f instance concrete_is_comm_ring_hom : concrete_category @is_comm_ring_hom := ⟨by introsI α ia; apply_instance, by introsI α β γ ia ib ic f g hf hg; apply_instance⟩ instance CommRing_hom_is_comm_ring_hom {R S : CommRing} (f : R ⟶ S) : is_comm_ring_hom (f : R → S) := f.2 namespace CommRing /-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/ def forget_to_CommMon : CommRing ⥤ CommMon := concrete_functor (by intros _ c; exact { ..c }) (by introsI _ _ _ _ f i; exact { ..i }) instance : faithful (forget_to_CommMon) := {} example : faithful (forget_to_CommMon ⋙ CommMon.forget_to_Mon) := by apply_instance end CommRing end category_theory.examples
8e797973aa201b9bd6a6d4b01860e3735ce1267b
7cef822f3b952965621309e88eadf618da0c8ae9
/src/tactic/omega/nat/dnf.lean
b129024dcef39e4d14d5b60f2cec3023123de03c
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
4,698
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek DNF transformation. -/ import tactic.omega.clause import tactic.omega.nat.form namespace omega namespace nat open_locale omega.nat @[simp] def dnf_core : form → list clause | (p ∨* q) := (dnf_core p) ++ (dnf_core q) | (p ∧* q) := (list.product (dnf_core p) (dnf_core q)).map (λ pq, clause.append pq.fst pq.snd) | (t =* s) := [([term.sub (canonize s) (canonize t)],[])] | (t ≤* s) := [([],[term.sub (canonize s) (canonize t)])] | (¬* _) := [] lemma exists_clause_holds_core {v : nat → nat} : ∀ {p : form}, p.neg_free → p.sub_free → p.holds v → ∃ c ∈ (dnf_core p), clause.holds (λ x, ↑(v x)) c := begin form.induce `[intros h1 h0 h2], { apply list.exists_mem_cons_of, constructor, rw list.forall_mem_singleton, cases h0 with ht hs, simp only [val_canonize ht, val_canonize hs, term.val_sub, form.holds, sub_eq_add_neg] at *, rw [h2, add_neg_self], apply list.forall_mem_nil }, { apply list.exists_mem_cons_of, constructor, apply list.forall_mem_nil, rw list.forall_mem_singleton, simp only [val_canonize (h0.left), val_canonize (h0.right), term.val_sub, form.holds, sub_eq_add_neg] at *, rw [←sub_eq_add_neg, le_sub, sub_zero, int.coe_nat_le], assumption }, { cases h1 }, { cases h2 with h2 h2; [ {cases (ihp h1.left h0.left h2) with c h3}, {cases (ihq h1.right h0.right h2) with c h3}]; cases h3 with h3 h4; refine ⟨c, list.mem_append.elim_right _, h4⟩; [left,right]; assumption }, { rcases (ihp h1.left h0.left h2.left) with ⟨cp, hp1, hp2⟩, rcases (ihq h1.right h0.right h2.right) with ⟨cq, hq1, hq2⟩, refine ⟨clause.append cp cq, ⟨_, clause.holds_append hp2 hq2⟩⟩, simp only [dnf_core, list.mem_map], refine ⟨(cp,cq),⟨_,rfl⟩⟩, rw list.mem_product, constructor; assumption } end def term.vars_core (is : list int) : list bool := is.map (λ i, if i = 0 then ff else tt) def term.vars (t : term) : list bool := term.vars_core t.snd def bools.or : list bool → list bool → list bool | [] bs2 := bs2 | bs1 [] := bs1 | (b1::bs1) (b2::bs2) := (b1 || b2)::(bools.or bs1 bs2) def terms.vars : list term → list bool | [] := [] | (t::ts) := bools.or (term.vars t) (terms.vars ts) open_locale list.func -- get notation for list.func.set def nonneg_consts_core : nat → list bool → list term | _ [] := [] | k (ff::bs) := nonneg_consts_core (k+1) bs | k (tt::bs) := ⟨0, [] {k ↦ 1}⟩::nonneg_consts_core (k+1) bs def nonneg_consts (bs : list bool) : list term := nonneg_consts_core 0 bs def nonnegate : clause → clause | (eqs,les) := let xs := terms.vars eqs in let ys := terms.vars les in let bs := bools.or xs ys in (eqs, nonneg_consts bs ++ les) def dnf (p : form) : list clause := (dnf_core p).map nonnegate lemma holds_nonneg_consts_core {v : nat → int} (h1 : ∀ x, 0 ≤ v x) : ∀ m bs, (∀ t ∈ (nonneg_consts_core m bs), 0 ≤ term.val v t) | _ [] := λ _ h2, by cases h2 | k (ff::bs) := holds_nonneg_consts_core (k+1) bs | k (tt::bs) := begin simp only [nonneg_consts_core], rw list.forall_mem_cons, constructor, { simp only [term.val, one_mul, zero_add, coeffs.val_set], apply h1 }, { apply holds_nonneg_consts_core (k+1) bs } end lemma holds_nonneg_consts {v : nat → int} {bs : list bool} : (∀ x, 0 ≤ v x) → (∀ t ∈ (nonneg_consts bs), 0 ≤ term.val v t) | h1 := by apply holds_nonneg_consts_core h1 lemma exists_clause_holds {v : nat → nat} {p : form} : p.neg_free → p.sub_free → p.holds v → ∃ c ∈ (dnf p), clause.holds (λ x, ↑(v x)) c := begin intros h1 h2 h3, rcases (exists_clause_holds_core h1 h2 h3) with ⟨c,h4,h5⟩, existsi (nonnegate c), have h6 : nonnegate c ∈ dnf p, { simp only [dnf], rw list.mem_map, refine ⟨c,h4,rfl⟩ }, refine ⟨h6,_⟩, cases c with eqs les, simp only [nonnegate, clause.holds], constructor, apply h5.left, rw list.forall_mem_append, apply and.intro (holds_nonneg_consts _) h5.right, apply int.coe_nat_nonneg end lemma exists_clause_sat {p : form} : p.neg_free → p.sub_free → p.sat → ∃ c ∈ (dnf p), clause.sat c := begin intros h1 h2 h3, cases h3 with v h3, rcases (exists_clause_holds h1 h2 h3) with ⟨c,h4,h5⟩, refine ⟨c,h4,_,h5⟩ end lemma unsat_of_unsat_dnf (p : form) : p.neg_free → p.sub_free → clauses.unsat (dnf p) → p.unsat := begin intros hnf hsf h1 h2, apply h1, apply exists_clause_sat hnf hsf h2 end end nat end omega
0a32120d640c0149f507a93cb110db7f3707c730
b2fe74b11b57d362c13326bc5651244f111fa6f4
/src/group_theory/monoid_localization.lean
75741add724b5617b71d5cc4d3a05eba0be968ee
[ "Apache-2.0" ]
permissive
midfield/mathlib
c4db5fa898b5ac8f2f80ae0d00c95eb6f745f4c7
775edc615ecec631d65b6180dbcc7bc26c3abc26
refs/heads/master
1,675,330,551,921
1,608,304,514,000
1,608,304,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
54,861
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 -/ set_option old_structure_cmd true 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 @[to_additive] instance : comm_monoid (localization S) := (r S).comm_monoid 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) @[elab_as_eliminator, to_additive] theorem ind {p : localization S → Prop} (H : ∀ (y : M × S), p (mk y.1 y.2)) (x) : p x := by rcases x; convert H x; exact prod.mk.eta.symm @[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 @[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⟩ 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 @[to_additive, ext] lemma ext {f g : localization_map S N} (h : ∀ x, f.to_map x = g.to_map x) : f = g := by cases f; cases g; simp only; exact funext h attribute [ext] add_submonoid.localization_map.ext @[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.4 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.5 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.6 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.map (monoid_hom.of $ ((^ n) : P → P)) 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, g.map_nsmul], exact is_add_unit.map (add_monoid_hom.of $ (λ x, n •ℕ x)) 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 _⟩ @[to_additive, simp] 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 @[to_additive, simp] 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) @[to_additive, simp] 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] 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 @[to_additive, simp] 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 @[to_additive, simp] 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 ▸ (submonoid.ext' $ 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⟩⟩) @[to_additive, simp] 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) _ _ @[to_additive, simp] 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) := { map_units' := λ y, is_unit_iff_exists_inv.2 ⟨mk 1 y, (r S).eq.2 $ show r S (_, 1 * y) 1, by simpa using (r S).symm (one_rel y)⟩, surj' := λ z, induction_on z $ λ x, ⟨x, (r S).eq.2 $ show r S (x.1 * x.2, x.2 * 1) (x.1, 1), by rw [mul_comm x.2, ←mul_one (x.1, (1 : S))]; exact (r S).mul ((r S).refl (x.1, 1)) ((r S).symm $ one_rel x.2)⟩, eq_iff_exists' := λ x y, (r S).eq.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; refl, show mk x 1 = mk (x * 1) ((1 : S) * 1), by rw [mul_one, mul_one]], exact (con.eq _).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 _ _ 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
8e6960ddb567445a37691cbb3a4518723e0531d8
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/fun_info_auto.lean
57e73af87da116e012938e604163e1220db793f8
[]
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,300
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.tactic import Mathlib.Lean3Lib.init.meta.format import Mathlib.Lean3Lib.init.function universes l namespace Mathlib structure param_info where is_implicit : Bool is_inst_implicit : Bool is_prop : Bool has_fwd_deps : Bool back_deps : List ℕ structure fun_info where params : List param_info result_deps : List ℕ /-- specialized is true if the result of fun_info has been specifialized using this argument. For example, consider the function f : Pi (α : Type), α -> α Now, suppse we request get_specialize fun_info for the application f unit a fun_info_manager returns two param_info objects: 1) specialized = true 2) is_subsingleton = true Note that, in general, the second argument of f is not a subsingleton, but it is in this particular case/specialization. \remark This bit is only set if it is a dependent parameter. Moreover, we only set is_specialized IF another parameter becomes a subsingleton -/ structure subsingleton_info where specialized : Bool is_subsingleton : Bool end Mathlib
172d34cc6a247a8d4762bbd80d9779d75ff465de
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/equiv/nat_auto.lean
3c879f38e124ba23fb690ca3e206582a486dc58c
[]
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,926
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Additional facts about equiv and encodable using the pairing function on nat. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.pairing import Mathlib.data.pnat.basic import Mathlib.PostPort universes u_1 u_2 namespace Mathlib namespace equiv /-- An equivalence between `ℕ × ℕ` and `ℕ`, using the `mkpair` and `unpair` functions in `data.nat.pairing`. -/ @[simp] def nat_prod_nat_equiv_nat : ℕ × ℕ ≃ ℕ := mk (fun (p : ℕ × ℕ) => nat.mkpair (prod.fst p) (prod.snd p)) nat.unpair sorry nat.mkpair_unpair /-- An equivalence between `bool × ℕ` and `ℕ`, by mapping `(tt, x)` to `2 * x + 1` and `(ff, x)` to `2 * x`. -/ @[simp] def bool_prod_nat_equiv_nat : Bool × ℕ ≃ ℕ := mk (fun (_x : Bool × ℕ) => sorry) nat.bodd_div2 sorry sorry /-- An equivalence between `ℕ ⊕ ℕ` and `ℕ`, by mapping `(sum.inl x)` to `2 * x` and `(sum.inr x)` to `2 * x + 1`. -/ @[simp] def nat_sum_nat_equiv_nat : ℕ ⊕ ℕ ≃ ℕ := equiv.trans (equiv.symm (bool_prod_equiv_sum ℕ)) bool_prod_nat_equiv_nat /-- An equivalence between `ℤ` and `ℕ`, through `ℤ ≃ ℕ ⊕ ℕ` and `ℕ ⊕ ℕ ≃ ℕ`. -/ def int_equiv_nat : ℤ ≃ ℕ := equiv.trans int_equiv_nat_sum_nat nat_sum_nat_equiv_nat /-- An equivalence between `α × α` and `α`, given that there is an equivalence between `α` and `ℕ`. -/ def prod_equiv_of_equiv_nat {α : Type (max u_1 u_2)} (e : α ≃ ℕ) : α × α ≃ α := equiv.trans (equiv.trans (prod_congr e e) nat_prod_nat_equiv_nat) (equiv.symm e) /-- An equivalence between `ℕ+` and `ℕ`, by mapping `x` in `ℕ+` to `x - 1` in `ℕ`. -/ def pnat_equiv_nat : ℕ+ ≃ ℕ := mk (fun (n : ℕ+) => Nat.pred (subtype.val n)) nat.succ_pnat sorry sorry end Mathlib
e338be25b0293129976405b39e94b75fe2a57dc1
76ce87faa6bc3c2aa9af5962009e01e04f2a074a
/11_Predicates/00_intro.lean
83db3d0323dc9e12cc1a55545832b2e32dacebbe
[]
no_license
Mnormansell/Discrete-Notes
db423dd9206bbe7080aecb84b4c2d275b758af97
61f13b98be590269fc4822be7b47924a6ddc1261
refs/heads/master
1,585,412,435,424
1,540,919,483,000
1,540,919,483,000
148,684,638
0
0
null
null
null
null
UTF-8
Lean
false
false
732
lean
-- UNDER CONSTRUCTION /- Every Person is Mortal, Socrates is a Person --------------------------------------------- Socrates is Mortal Incorporating the notio of proof, and moving to more mathematical notation, we could write this as follows: prog : (∀ p : Person), Mortal p; p: Person ------------------------------------------ pf : (Mortal p) From a proof, mort, of Person → Mortal "If p is a Person then p is Mortal" and from (a proof of) "Socrates is a person" we derive (a proof of) "Socrates is Mortal." We can write "if p is any person then p is mortal" as Person → Mortal. ∀ p : Person, (mortal P) Another way to say that all people are mortal is to say that every person is -/
749c07aab27f8ee8ecea49f4362c15350ea41e7c
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/measure_theory/lebesgue_measure.lean
0e5671c2ebd0b467bf8a1c87e8404629d946c164
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,987
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Yury Kudryashov -/ import measure_theory.pi /-! # Lebesgue measure on the real line and on `ℝⁿ` -/ noncomputable theory open classical set filter open ennreal (of_real) open_locale big_operators ennreal namespace measure_theory /-! ### Preliminary definitions -/ /-- Length of an interval. This is the largest monotonic function which correctly measures all intervals. -/ def lebesgue_length (s : set ℝ) : ℝ≥0∞ := ⨅a b (h : s ⊆ Ico a b), of_real (b - a) @[simp] lemma lebesgue_length_empty : lebesgue_length ∅ = 0 := nonpos_iff_eq_zero.1 $ infi_le_of_le 0 $ infi_le_of_le 0 $ by simp @[simp] lemma lebesgue_length_Ico (a b : ℝ) : lebesgue_length (Ico a b) = of_real (b - a) := begin refine le_antisymm (infi_le_of_le a $ binfi_le b (subset.refl _)) (le_infi $ λ a', le_infi $ λ b', le_infi $ λ h, ennreal.coe_le_coe.2 _), cases le_or_lt b a with ab ab, { rw nnreal.of_real_of_nonpos (sub_nonpos.2 ab), apply zero_le }, cases (Ico_subset_Ico_iff ab).1 h with h₁ h₂, exact nnreal.of_real_le_of_real (sub_le_sub h₂ h₁) end lemma lebesgue_length_mono {s₁ s₂ : set ℝ} (h : s₁ ⊆ s₂) : lebesgue_length s₁ ≤ lebesgue_length s₂ := infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h', ⟨subset.trans h h', le_refl _⟩ lemma lebesgue_length_eq_infi_Ioo (s) : lebesgue_length s = ⨅a b (h : s ⊆ Ioo a b), of_real (b - a) := begin refine le_antisymm (infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h, ⟨subset.trans h Ioo_subset_Ico_self, le_refl _⟩) _, refine le_infi (λ a, le_infi $ λ b, le_infi $ λ h, _), refine ennreal.le_of_forall_pos_le_add (λ ε ε0 _, _), refine infi_le_of_le (a - ε) (infi_le_of_le b $ infi_le_of_le (subset.trans h $ Ico_subset_Ioo_left $ (sub_lt_self_iff _).2 ε0) _), rw ← sub_add, refine le_trans ennreal.of_real_add_le (add_le_add_left _ _), simp only [ennreal.of_real_coe_nnreal, le_refl] end @[simp] lemma lebesgue_length_Ioo (a b : ℝ) : lebesgue_length (Ioo a b) = of_real (b - a) := begin rw ← lebesgue_length_Ico, refine le_antisymm (lebesgue_length_mono Ioo_subset_Ico_self) _, rw lebesgue_length_eq_infi_Ioo (Ioo a b), refine (le_infi $ λ a', le_infi $ λ b', le_infi $ λ h, _), cases le_or_lt b a with ab ab, {simp [ab]}, cases (Ioo_subset_Ioo_iff ab).1 h with h₁ h₂, rw [lebesgue_length_Ico], exact ennreal.of_real_le_of_real (sub_le_sub h₂ h₁) end lemma lebesgue_length_eq_infi_Icc (s) : lebesgue_length s = ⨅a b (h : s ⊆ Icc a b), of_real (b - a) := begin refine le_antisymm _ (infi_le_infi $ λ a, infi_le_infi $ λ b, infi_le_infi2 $ λ h, ⟨subset.trans h Ico_subset_Icc_self, le_refl _⟩), refine le_infi (λ a, le_infi $ λ b, le_infi $ λ h, _), refine ennreal.le_of_forall_pos_le_add (λ ε ε0 _, _), refine infi_le_of_le a (infi_le_of_le (b + ε) $ infi_le_of_le (subset.trans h $ Icc_subset_Ico_right $ (lt_add_iff_pos_right _).2 ε0) _), rw [← sub_add_eq_add_sub], refine le_trans ennreal.of_real_add_le (add_le_add_left _ _), simp only [ennreal.of_real_coe_nnreal, le_refl] end @[simp] lemma lebesgue_length_Icc (a b : ℝ) : lebesgue_length (Icc a b) = of_real (b - a) := begin rw ← lebesgue_length_Ico, refine le_antisymm _ (lebesgue_length_mono Ico_subset_Icc_self), rw lebesgue_length_eq_infi_Icc (Icc a b), exact infi_le_of_le a (infi_le_of_le b $ infi_le_of_le (by refl) (by simp [le_refl])) end /-- The Lebesgue outer measure, as an outer measure of ℝ. -/ def lebesgue_outer : outer_measure ℝ := outer_measure.of_function lebesgue_length lebesgue_length_empty lemma lebesgue_outer_le_length (s : set ℝ) : lebesgue_outer s ≤ lebesgue_length s := outer_measure.of_function_le _ lemma lebesgue_length_subadditive {a b : ℝ} {c d : ℕ → ℝ} (ss : Icc a b ⊆ ⋃i, Ioo (c i) (d i)) : (of_real (b - a) : ℝ≥0∞) ≤ ∑' i, of_real (d i - c i) := begin suffices : ∀ (s:finset ℕ) b (cv : Icc a b ⊆ ⋃ i ∈ (↑s:set ℕ), Ioo (c i) (d i)), (of_real (b - a) : ℝ≥0∞) ≤ ∑ i in s, of_real (d i - c i), { rcases compact_Icc.elim_finite_subcover_image (λ (i : ℕ) (_ : i ∈ univ), @is_open_Ioo _ _ _ _ (c i) (d i)) (by simpa using ss) with ⟨s, su, hf, hs⟩, have e : (⋃ i ∈ (↑hf.to_finset:set ℕ), Ioo (c i) (d i)) = (⋃ i ∈ s, Ioo (c i) (d i)), {simp [set.ext_iff]}, rw ennreal.tsum_eq_supr_sum, refine le_trans _ (le_supr _ hf.to_finset), exact this hf.to_finset _ (by simpa [e]) }, clear ss b, refine λ s, finset.strong_induction_on s (λ s IH b cv, _), cases le_total b a with ab ab, { rw ennreal.of_real_eq_zero.2 (sub_nonpos.2 ab), exact zero_le _ }, have := cv ⟨ab, le_refl _⟩, simp at this, rcases this with ⟨i, is, cb, bd⟩, rw [← finset.insert_erase is] at cv ⊢, rw [finset.coe_insert, bUnion_insert] at cv, rw [finset.sum_insert (finset.not_mem_erase _ _)], refine le_trans _ (add_le_add_left (IH _ (finset.erase_ssubset is) (c i) _) _), { refine le_trans (ennreal.of_real_le_of_real _) ennreal.of_real_add_le, rw sub_add_sub_cancel, exact sub_le_sub_right (le_of_lt bd) _ }, { rintro x ⟨h₁, h₂⟩, refine (cv ⟨h₁, le_trans h₂ (le_of_lt cb)⟩).resolve_left (mt and.left (not_lt_of_le h₂)) } end @[simp] lemma lebesgue_outer_Icc (a b : ℝ) : lebesgue_outer (Icc a b) = of_real (b - a) := begin refine le_antisymm (by rw ← lebesgue_length_Icc; apply lebesgue_outer_le_length) (le_binfi $ λ f hf, ennreal.le_of_forall_pos_le_add $ λ ε ε0 h, _), rcases ennreal.exists_pos_sum_of_encodable (ennreal.zero_lt_coe_iff.2 ε0) ℕ with ⟨ε', ε'0, hε⟩, refine le_trans _ (add_le_add_left (le_of_lt hε) _), rw ← ennreal.tsum_add, choose g hg using show ∀ i, ∃ p:ℝ×ℝ, f i ⊆ Ioo p.1 p.2 ∧ (of_real (p.2 - p.1) : ℝ≥0∞) < lebesgue_length (f i) + ε' i, { intro i, have := (ennreal.lt_add_right (lt_of_le_of_lt (ennreal.le_tsum i) h) (ennreal.zero_lt_coe_iff.2 (ε'0 i))), conv at this {to_lhs, rw lebesgue_length_eq_infi_Ioo}, simpa [infi_lt_iff] }, refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hg i).2), exact lebesgue_length_subadditive (subset.trans hf $ Union_subset_Union $ λ i, (hg i).1) end @[simp] lemma lebesgue_outer_singleton (a : ℝ) : lebesgue_outer {a} = 0 := by simpa using lebesgue_outer_Icc a a @[simp] lemma lebesgue_outer_Ico (a b : ℝ) : lebesgue_outer (Ico a b) = of_real (b - a) := by rw [← Icc_diff_right, lebesgue_outer.diff_null _ (lebesgue_outer_singleton _), lebesgue_outer_Icc] @[simp] lemma lebesgue_outer_Ioo (a b : ℝ) : lebesgue_outer (Ioo a b) = of_real (b - a) := by rw [← Ico_diff_left, lebesgue_outer.diff_null _ (lebesgue_outer_singleton _), lebesgue_outer_Ico] @[simp] lemma lebesgue_outer_Ioc (a b : ℝ) : lebesgue_outer (Ioc a b) = of_real (b - a) := by rw [← Icc_diff_left, lebesgue_outer.diff_null _ (lebesgue_outer_singleton _), lebesgue_outer_Icc] lemma is_lebesgue_measurable_Iio {c : ℝ} : lebesgue_outer.caratheodory.measurable_set' (Iio c) := outer_measure.of_function_caratheodory $ λ t, le_infi $ λ a, le_infi $ λ b, le_infi $ λ h, begin refine le_trans (add_le_add (lebesgue_length_mono $ inter_subset_inter_left _ h) (lebesgue_length_mono $ diff_subset_diff_left h)) _, cases le_total a c with hac hca; cases le_total b c with hbc hcb; simp [*, -sub_eq_add_neg, sub_add_sub_cancel', le_refl], { simp [*, ← ennreal.of_real_add, -sub_eq_add_neg, sub_add_sub_cancel', le_refl] }, { simp only [ennreal.of_real_eq_zero.2 (sub_nonpos.2 (le_trans hbc hca)), zero_add, le_refl] } end theorem lebesgue_outer_trim : lebesgue_outer.trim = lebesgue_outer := begin refine le_antisymm (λ s, _) (outer_measure.le_trim _), rw outer_measure.trim_eq_infi, refine le_infi (λ f, le_infi $ λ hf, ennreal.le_of_forall_pos_le_add $ λ ε ε0 h, _), rcases ennreal.exists_pos_sum_of_encodable (ennreal.zero_lt_coe_iff.2 ε0) ℕ with ⟨ε', ε'0, hε⟩, refine le_trans _ (add_le_add_left (le_of_lt hε) _), rw ← ennreal.tsum_add, choose g hg using show ∀ i, ∃ s, f i ⊆ s ∧ measurable_set s ∧ lebesgue_outer s ≤ lebesgue_length (f i) + of_real (ε' i), { intro i, have := (ennreal.lt_add_right (lt_of_le_of_lt (ennreal.le_tsum i) h) (ennreal.zero_lt_coe_iff.2 (ε'0 i))), conv at this {to_lhs, rw lebesgue_length}, simp only [infi_lt_iff] at this, rcases this with ⟨a, b, h₁, h₂⟩, rw ← lebesgue_outer_Ico at h₂, exact ⟨_, h₁, measurable_set_Ico, le_of_lt $ by simpa using h₂⟩ }, simp at hg, apply infi_le_of_le (Union g) _, apply infi_le_of_le (subset.trans hf $ Union_subset_Union (λ i, (hg i).1)) _, apply infi_le_of_le (measurable_set.Union (λ i, (hg i).2.1)) _, exact le_trans (lebesgue_outer.Union _) (ennreal.tsum_le_tsum $ λ i, (hg i).2.2) end lemma borel_le_lebesgue_measurable : borel ℝ ≤ lebesgue_outer.caratheodory := begin rw real.borel_eq_generate_from_Iio_rat, refine measurable_space.generate_from_le _, simp [is_lebesgue_measurable_Iio] { contextual := tt } end /-! ### Definition of the Lebesgue measure and lengths of intervals -/ /-- Lebesgue measure on the Borel sets The outer Lebesgue measure is the completion of this measure. (TODO: proof this) -/ instance real.measure_space : measure_space ℝ := ⟨{to_outer_measure := lebesgue_outer, m_Union := λ f hf, lebesgue_outer.Union_eq_of_caratheodory $ λ i, borel_le_lebesgue_measurable _ (hf i), trimmed := lebesgue_outer_trim }⟩ @[simp] theorem lebesgue_to_outer_measure : (volume : measure ℝ).to_outer_measure = lebesgue_outer := rfl end measure_theory open measure_theory namespace real variables {ι : Type*} [fintype ι] open_locale topological_space theorem volume_val (s) : volume s = lebesgue_outer s := rfl instance has_no_atoms_volume : has_no_atoms (volume : measure ℝ) := ⟨lebesgue_outer_singleton⟩ @[simp] lemma volume_Ico {a b : ℝ} : volume (Ico a b) = of_real (b - a) := lebesgue_outer_Ico a b @[simp] lemma volume_Icc {a b : ℝ} : volume (Icc a b) = of_real (b - a) := lebesgue_outer_Icc a b @[simp] lemma volume_Ioo {a b : ℝ} : volume (Ioo a b) = of_real (b - a) := lebesgue_outer_Ioo a b @[simp] lemma volume_Ioc {a b : ℝ} : volume (Ioc a b) = of_real (b - a) := lebesgue_outer_Ioc a b @[simp] lemma volume_singleton {a : ℝ} : volume ({a} : set ℝ) = 0 := lebesgue_outer_singleton a @[simp] lemma volume_interval {a b : ℝ} : volume (interval a b) = of_real (abs (b - a)) := by rw [interval, volume_Icc, max_sub_min_eq_abs] @[simp] lemma volume_Ioi {a : ℝ} : volume (Ioi a) = ∞ := top_unique $ le_of_tendsto' ennreal.tendsto_nat_nhds_top $ λ n, calc (n : ℝ≥0∞) = volume (Ioo a (a + n)) : by simp ... ≤ volume (Ioi a) : measure_mono Ioo_subset_Ioi_self @[simp] lemma volume_Ici {a : ℝ} : volume (Ici a) = ∞ := by simp [← measure_congr Ioi_ae_eq_Ici] @[simp] lemma volume_Iio {a : ℝ} : volume (Iio a) = ∞ := top_unique $ le_of_tendsto' ennreal.tendsto_nat_nhds_top $ λ n, calc (n : ℝ≥0∞) = volume (Ioo (a - n) a) : by simp ... ≤ volume (Iio a) : measure_mono Ioo_subset_Iio_self @[simp] lemma volume_Iic {a : ℝ} : volume (Iic a) = ∞ := by simp [← measure_congr Iio_ae_eq_Iic] instance locally_finite_volume : locally_finite_measure (volume : measure ℝ) := ⟨λ x, ⟨Ioo (x - 1) (x + 1), mem_nhds_sets is_open_Ioo ⟨sub_lt_self _ zero_lt_one, lt_add_of_pos_right _ zero_lt_one⟩, by simp only [real.volume_Ioo, ennreal.of_real_lt_top]⟩⟩ /-! ### Volume of a box in `ℝⁿ` -/ lemma volume_Icc_pi {a b : ι → ℝ} : volume (Icc a b) = ∏ i, ennreal.of_real (b i - a i) := begin rw [← pi_univ_Icc, volume_pi_pi], { simp only [real.volume_Icc] }, { exact λ i, measurable_set_Icc } end @[simp] lemma volume_Icc_pi_to_real {a b : ι → ℝ} (h : a ≤ b) : (volume (Icc a b)).to_real = ∏ i, (b i - a i) := by simp only [volume_Icc_pi, ennreal.to_real_prod, ennreal.to_real_of_real (sub_nonneg.2 (h _))] lemma volume_pi_Ioo {a b : ι → ℝ} : volume (pi univ (λ i, Ioo (a i) (b i))) = ∏ i, ennreal.of_real (b i - a i) := (measure_congr measure.univ_pi_Ioo_ae_eq_Icc).trans volume_Icc_pi @[simp] lemma volume_pi_Ioo_to_real {a b : ι → ℝ} (h : a ≤ b) : (volume (pi univ (λ i, Ioo (a i) (b i)))).to_real = ∏ i, (b i - a i) := by simp only [volume_pi_Ioo, ennreal.to_real_prod, ennreal.to_real_of_real (sub_nonneg.2 (h _))] lemma volume_pi_Ioc {a b : ι → ℝ} : volume (pi univ (λ i, Ioc (a i) (b i))) = ∏ i, ennreal.of_real (b i - a i) := (measure_congr measure.univ_pi_Ioc_ae_eq_Icc).trans volume_Icc_pi @[simp] lemma volume_pi_Ioc_to_real {a b : ι → ℝ} (h : a ≤ b) : (volume (pi univ (λ i, Ioc (a i) (b i)))).to_real = ∏ i, (b i - a i) := by simp only [volume_pi_Ioc, ennreal.to_real_prod, ennreal.to_real_of_real (sub_nonneg.2 (h _))] lemma volume_pi_Ico {a b : ι → ℝ} : volume (pi univ (λ i, Ico (a i) (b i))) = ∏ i, ennreal.of_real (b i - a i) := (measure_congr measure.univ_pi_Ico_ae_eq_Icc).trans volume_Icc_pi @[simp] lemma volume_pi_Ico_to_real {a b : ι → ℝ} (h : a ≤ b) : (volume (pi univ (λ i, Ico (a i) (b i)))).to_real = ∏ i, (b i - a i) := by simp only [volume_pi_Ico, ennreal.to_real_prod, ennreal.to_real_of_real (sub_nonneg.2 (h _))] /-! ### Images of the Lebesgue measure under translation/multiplication/... -/ lemma map_volume_add_left (a : ℝ) : measure.map ((+) a) volume = volume := eq.symm $ real.measure_ext_Ioo_rat $ λ p q, by simp [measure.map_apply (measurable_const_add a) measurable_set_Ioo, sub_sub_sub_cancel_right] lemma map_volume_add_right (a : ℝ) : measure.map (+ a) volume = volume := by simpa only [add_comm] using real.map_volume_add_left a lemma smul_map_volume_mul_left {a : ℝ} (h : a ≠ 0) : ennreal.of_real (abs a) • measure.map ((*) a) volume = volume := begin refine (real.measure_ext_Ioo_rat $ λ p q, _).symm, cases lt_or_gt_of_ne h with h h, { simp only [real.volume_Ioo, measure.smul_apply, ← ennreal.of_real_mul (le_of_lt $ neg_pos.2 h), measure.map_apply (measurable_const_mul a) measurable_set_Ioo, neg_sub_neg, ← neg_mul_eq_neg_mul, preimage_const_mul_Ioo_of_neg _ _ h, abs_of_neg h, mul_sub, mul_div_cancel' _ (ne_of_lt h)] }, { simp only [real.volume_Ioo, measure.smul_apply, ← ennreal.of_real_mul (le_of_lt h), measure.map_apply (measurable_const_mul a) measurable_set_Ioo, preimage_const_mul_Ioo _ _ h, abs_of_pos h, mul_sub, mul_div_cancel' _ (ne_of_gt h)] } end lemma map_volume_mul_left {a : ℝ} (h : a ≠ 0) : measure.map ((*) a) volume = ennreal.of_real (abs a⁻¹) • volume := by conv_rhs { rw [← real.smul_map_volume_mul_left h, smul_smul, ← ennreal.of_real_mul (abs_nonneg _), ← abs_mul, inv_mul_cancel h, abs_one, ennreal.of_real_one, one_smul] } lemma smul_map_volume_mul_right {a : ℝ} (h : a ≠ 0) : ennreal.of_real (abs a) • measure.map (* a) volume = volume := by simpa only [mul_comm] using real.smul_map_volume_mul_left h lemma map_volume_mul_right {a : ℝ} (h : a ≠ 0) : measure.map (* a) volume = ennreal.of_real (abs a⁻¹) • volume := by simpa only [mul_comm] using real.map_volume_mul_left h @[simp] lemma map_volume_neg : measure.map has_neg.neg (volume : measure ℝ) = volume := eq.symm $ real.measure_ext_Ioo_rat $ λ p q, by simp [show measure.map has_neg.neg volume (Ioo (p : ℝ) q) = _, from measure.map_apply measurable_neg measurable_set_Ioo] end real open_locale topological_space lemma filter.eventually.volume_pos_of_nhds_real {p : ℝ → Prop} {a : ℝ} (h : ∀ᶠ x in 𝓝 a, p x) : (0 : ℝ≥0∞) < volume {x | p x} := begin rcases h.exists_Ioo_subset with ⟨l, u, hx, hs⟩, refine lt_of_lt_of_le _ (measure_mono hs), simpa [-mem_Ioo] using hx.1.trans hx.2 end section region_between open_locale classical variable {α : Type*} /-- The region between two real-valued functions on an arbitrary set. -/ def region_between (f g : α → ℝ) (s : set α) : set (α × ℝ) := {p : α × ℝ | p.1 ∈ s ∧ p.2 ∈ Ioo (f p.1) (g p.1)} lemma region_between_subset (f g : α → ℝ) (s : set α) : region_between f g s ⊆ s.prod univ := by simpa only [prod_univ, region_between, set.preimage, set_of_subset_set_of] using λ a, and.left variables [measurable_space α] {μ : measure α} {f g : α → ℝ} {s : set α} /-- The region between two measurable functions on a measurable set is measurable. -/ lemma measurable_set_region_between (hf : measurable f) (hg : measurable g) (hs : measurable_set s) : measurable_set (region_between f g s) := begin dsimp only [region_between, Ioo, mem_set_of_eq, set_of_and], refine measurable_set.inter _ ((measurable_set_lt (hf.comp measurable_fst) measurable_snd).inter (measurable_set_lt measurable_snd (hg.comp measurable_fst))), convert hs.prod measurable_set.univ, simp only [and_true, mem_univ], end theorem volume_region_between_eq_lintegral' (hf : measurable f) (hg : measurable g) (hs : measurable_set s) : μ.prod volume (region_between f g s) = ∫⁻ y in s, ennreal.of_real ((g - f) y) ∂μ := begin rw measure.prod_apply, { have h : (λ x, volume {a | x ∈ s ∧ a ∈ Ioo (f x) (g x)}) = s.indicator (λ x, ennreal.of_real (g x - f x)), { funext x, rw indicator_apply, split_ifs, { have hx : {a | x ∈ s ∧ a ∈ Ioo (f x) (g x)} = Ioo (f x) (g x) := by simp [h, Ioo], simp only [hx, real.volume_Ioo, sub_zero] }, { have hx : {a | x ∈ s ∧ a ∈ Ioo (f x) (g x)} = ∅ := by simp [h], simp only [hx, measure_empty] } }, dsimp only [region_between, preimage_set_of_eq], rw [h, lintegral_indicator]; simp only [hs, pi.sub_apply] }, { exact measurable_set_region_between hf hg hs }, end /-- The volume of the region between two almost everywhere measurable functions on a measurable set can be represented as a Lebesgue integral. -/ theorem volume_region_between_eq_lintegral [sigma_finite μ] (hf : ae_measurable f (μ.restrict s)) (hg : ae_measurable g (μ.restrict s)) (hs : measurable_set s) : μ.prod volume (region_between f g s) = ∫⁻ y in s, ennreal.of_real ((g - f) y) ∂μ := begin have h₁ : (λ y, ennreal.of_real ((g - f) y)) =ᵐ[μ.restrict s] λ y, ennreal.of_real ((ae_measurable.mk g hg - ae_measurable.mk f hf) y) := eventually_eq.fun_comp (eventually_eq.sub hg.ae_eq_mk hf.ae_eq_mk) _, have h₂ : (μ.restrict s).prod volume (region_between f g s) = (μ.restrict s).prod volume (region_between (ae_measurable.mk f hf) (ae_measurable.mk g hg) s), { apply measure_congr, apply eventually_eq.inter, { refl }, exact eventually_eq.inter (eventually_eq.comp₂ (ae_eq_comp' measurable_fst hf.ae_eq_mk measure.prod_fst_absolutely_continuous) _ eventually_eq.rfl) (eventually_eq.comp₂ eventually_eq.rfl _ (ae_eq_comp' measurable_fst hg.ae_eq_mk measure.prod_fst_absolutely_continuous)) }, rw [lintegral_congr_ae h₁, ← volume_region_between_eq_lintegral' hf.measurable_mk hg.measurable_mk hs], convert h₂ using 1, { rw measure.restrict_prod_eq_prod_univ, exacts [ (measure.restrict_eq_self_of_subset_of_measurable (hs.prod measurable_set.univ) (region_between_subset f g s)).symm, hs], }, { rw measure.restrict_prod_eq_prod_univ, exacts [(measure.restrict_eq_self_of_subset_of_measurable (hs.prod measurable_set.univ) (region_between_subset (ae_measurable.mk f hf) (ae_measurable.mk g hg) s)).symm, hs] }, end theorem volume_region_between_eq_integral' [sigma_finite μ] (f_int : integrable_on f s μ) (g_int : integrable_on g s μ) (hs : measurable_set s) (hfg : f ≤ᵐ[μ.restrict s] g ) : μ.prod volume (region_between f g s) = ennreal.of_real (∫ y in s, (g - f) y ∂μ) := begin have h : g - f =ᵐ[μ.restrict s] (λ x, nnreal.of_real (g x - f x)), { apply hfg.mono, simp only [nnreal.of_real, max, sub_nonneg, pi.sub_apply], intros x hx, split_ifs, refl }, rw [volume_region_between_eq_lintegral f_int.ae_measurable g_int.ae_measurable hs, integral_congr_ae h, lintegral_congr_ae, lintegral_coe_eq_integral _ ((integrable_congr h).mp (g_int.sub f_int))], simpa only, end /-- If two functions are integrable on a measurable set, and one function is less than or equal to the other on that set, then the volume of the region between the two functions can be represented as an integral. -/ theorem volume_region_between_eq_integral [sigma_finite μ] (f_int : integrable_on f s μ) (g_int : integrable_on g s μ) (hs : measurable_set s) (hfg : ∀ x ∈ s, f x ≤ g x) : μ.prod volume (region_between f g s) = ennreal.of_real (∫ y in s, (g - f) y ∂μ) := volume_region_between_eq_integral' f_int g_int hs ((ae_restrict_iff' hs).mpr (eventually_of_forall hfg)) end region_between /- section vitali def vitali_aux_h (x : ℝ) (h : x ∈ Icc (0:ℝ) 1) : ∃ y ∈ Icc (0:ℝ) 1, ∃ q:ℚ, ↑q = x - y := ⟨x, h, 0, by simp⟩ def vitali_aux (x : ℝ) (h : x ∈ Icc (0:ℝ) 1) : ℝ := classical.some (vitali_aux_h x h) theorem vitali_aux_mem (x : ℝ) (h : x ∈ Icc (0:ℝ) 1) : vitali_aux x h ∈ Icc (0:ℝ) 1 := Exists.fst (classical.some_spec (vitali_aux_h x h):_) theorem vitali_aux_rel (x : ℝ) (h : x ∈ Icc (0:ℝ) 1) : ∃ q:ℚ, ↑q = x - vitali_aux x h := Exists.snd (classical.some_spec (vitali_aux_h x h):_) def vitali : set ℝ := {x | ∃ h, x = vitali_aux x h} theorem vitali_nonmeasurable : ¬ null_measurable_set measure_space.μ vitali := sorry end vitali -/
90bf88ade695298a5397f029aabad1c26cb363eb
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/tactic27.lean
20fa0cc5dc619c229690a68af6fea41f353f9fe9
[ "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
252
lean
import standard using tactic definition my_tac := repeat ([ apply @and_intro | apply @refl ]) tactic_hint my_tac theorem T1 {A : Type} {B : Type} (a : A) (b c : B) : a = a ∧ b = b ∧ c = c
11786c4298d2af2aa584f9c4a860158de0495e57
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/nat/choose/factorization.lean
a4a9805e66acaad8aad79ff4ae8e4f31e893d318
[ "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
5,746
lean
/- Copyright (c) 2022 Bolton Bailey. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bolton Bailey, Patrick Stevens, Thomas Browning -/ import data.nat.choose.central import number_theory.padics.padic_norm import data.nat.multiplicity /-! # Factorization of Binomial Coefficients This file contains a few results on the multiplicity of prime factors within certain size bounds in binomial coefficients. These include: * `nat.factorization_choose_le_log`: a logarithmic upper bound on the multiplicity of a prime in a binomial coefficient. * `nat.factorization_choose_le_one`: Primes above `sqrt n` appear at most once in the factorization of `n` choose `k`. * `nat.factorization_central_binom_of_two_mul_self_lt_three_mul`: Primes from `2 * n / 3` to `n` do not appear in the factorization of the `n`th central binomial coefficient. * `nat.factorization_choose_eq_zero_of_lt`: Primes greater than `n` do not appear in the factorization of `n` choose `k`. These results appear in the [Erdős proof of Bertrand's postulate](aigner1999proofs). -/ namespace nat variables {p n k : ℕ} /-- A logarithmic upper bound on the multiplicity of a prime in a binomial coefficient. -/ lemma factorization_choose_le_log : (choose n k).factorization p ≤ log p n := begin by_cases h : (choose n k).factorization p = 0, { simp [h] }, have hp : p.prime := not.imp_symm (choose n k).factorization_eq_zero_of_non_prime h, have hkn : k ≤ n, { refine le_of_not_lt (λ hnk, h _), simp [choose_eq_zero_of_lt hnk] }, rw [←@padic_val_nat_eq_factorization p _ ⟨hp⟩, @padic_val_nat_def _ ⟨hp⟩ _ (choose_pos hkn)], simp only [hp.multiplicity_choose hkn (lt_add_one _), part_enat.get_coe], refine (finset.card_filter_le _ _).trans (le_of_eq (nat.card_Ico _ _)), end /-- A `pow` form of `nat.factorization_choose_le` -/ lemma pow_factorization_choose_le (hn : 0 < n) : p ^ (choose n k).factorization p ≤ n := begin cases le_or_lt p 1, { exact (pow_le_pow_of_le h).trans ((le_of_eq (one_pow _)).trans hn) }, { exact (pow_le_iff_le_log h hn).mpr factorization_choose_le_log }, end /-- Primes greater than about `sqrt n` appear only to multiplicity 0 or 1 in the binomial coefficient. -/ lemma factorization_choose_le_one (p_large : n < p ^ 2) : (choose n k).factorization p ≤ 1 := begin apply factorization_choose_le_log.trans, rcases n.eq_zero_or_pos with rfl | hn0, { simp }, refine lt_succ_iff.1 ((lt_pow_iff_log_lt _ hn0).1 p_large), contrapose! hn0, exact lt_succ_iff.1 (lt_of_lt_of_le p_large (pow_le_one' hn0 2)), end lemma factorization_choose_of_lt_three_mul (hp' : p ≠ 2) (hk : p ≤ k) (hk' : p ≤ n - k) (hn : n < 3 * p) : (choose n k).factorization p = 0 := begin cases em' p.prime with hp hp, { exact factorization_eq_zero_of_non_prime (choose n k) hp }, cases lt_or_le n k with hnk hkn, { simp [choose_eq_zero_of_lt hnk] }, rw [←@padic_val_nat_eq_factorization p _ ⟨hp⟩, @padic_val_nat_def _ ⟨hp⟩ _ (choose_pos hkn)], simp only [hp.multiplicity_choose hkn (lt_add_one _), part_enat.get_coe, finset.card_eq_zero, finset.filter_eq_empty_iff, not_le], intros i hi, rcases eq_or_lt_of_le (finset.mem_Ico.mp hi).1 with rfl | hi, { rw [pow_one, ←add_lt_add_iff_left (2 * p), ←succ_mul, two_mul, add_add_add_comm], exact lt_of_le_of_lt (add_le_add (add_le_add_right (le_mul_of_one_le_right' ((one_le_div_iff hp.pos).mpr hk)) (k % p)) (add_le_add_right (le_mul_of_one_le_right' ((one_le_div_iff hp.pos).mpr hk')) ((n - k) % p))) (by rwa [div_add_mod, div_add_mod, add_tsub_cancel_of_le hkn]) }, { replace hn : n < p ^ i, { calc n < 3 * p : hn ... ≤ p * p : mul_le_mul_right' (lt_of_le_of_ne hp.two_le hp'.symm) p ... = p ^ 2 : (sq p).symm ... ≤ p ^ i : pow_le_pow hp.one_lt.le hi }, rwa [mod_eq_of_lt (lt_of_le_of_lt hkn hn), mod_eq_of_lt (lt_of_le_of_lt tsub_le_self hn), add_tsub_cancel_of_le hkn] }, end /-- Primes greater than about `2 * n / 3` and less than `n` do not appear in the factorization of `central_binom n`. -/ lemma factorization_central_binom_of_two_mul_self_lt_three_mul (n_big : 2 < n) (p_le_n : p ≤ n) (big : 2 * n < 3 * p) : (central_binom n).factorization p = 0 := begin refine factorization_choose_of_lt_three_mul _ p_le_n (p_le_n.trans _) big, { rintro rfl, linarith }, { rw [two_mul, add_tsub_cancel_left] }, end lemma factorization_factorial_eq_zero_of_lt (h : n < p) : (factorial n).factorization p = 0 := begin induction n with n hn, { simp }, rw [factorial_succ, factorization_mul n.succ_ne_zero n.factorial_ne_zero, finsupp.coe_add, pi.add_apply, hn (lt_of_succ_lt h), add_zero, factorization_eq_zero_of_lt h], end lemma factorization_choose_eq_zero_of_lt (h : n < p) : (choose n k).factorization p = 0 := begin by_cases hnk : n < k, { simp [choose_eq_zero_of_lt hnk] }, rw [choose_eq_factorial_div_factorial (le_of_not_lt hnk), factorization_div (factorial_mul_factorial_dvd_factorial (le_of_not_lt hnk)), finsupp.coe_tsub, pi.sub_apply, factorization_factorial_eq_zero_of_lt h, zero_tsub], end /-- If a prime `p` has positive multiplicity in the `n`th central binomial coefficient, `p` is no more than `2 * n` -/ lemma factorization_central_binom_eq_zero_of_two_mul_lt (h : 2 * n < p) : (central_binom n).factorization p = 0 := factorization_choose_eq_zero_of_lt h /-- Contrapositive form of `nat.factorization_central_binom_eq_zero_of_two_mul_lt` -/ lemma le_two_mul_of_factorization_central_binom_pos (h_pos : 0 < (central_binom n).factorization p) : p ≤ 2 * n := le_of_not_lt (pos_iff_ne_zero.mp h_pos ∘ factorization_central_binom_eq_zero_of_two_mul_lt) end nat
31cebb3eb275e35289099f8199579b46553f3ab5
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/data/set/lattice.lean
333d8098a0054d89d7e6bfed5e73c21c7789b7c4
[ "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
34,752
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, Johannes Hölzl, Mario Carneiro -- QUESTION: can make the first argument in ∀ x ∈ a, ... implicit? -/ import logic.basic data.set.basic data.equiv.basic import order.complete_boolean_algebra category.basic import tactic.finish data.sigma.basic order.galois_connection open function tactic set auto universes u v w x y variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {ι' : Sort y} namespace set instance lattice_set : complete_lattice (set α) := { le := (⊆), lt := (⊂), sup := (∪), inf := (∩), top := univ, bot := ∅, Sup := λs, {a | ∃ t ∈ s, a ∈ t }, Inf := λs, {a | ∀ t ∈ s, a ∈ t }, le_Sup := assume s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := assume s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, le_Inf := assume s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := assume s t t_in a h, h _ t_in, .. (infer_instance : complete_lattice (α → Prop)) } instance : distrib_lattice (set α) := { le_sup_inf := λ s t u x, or_and_distrib_left.2, ..set.lattice_set } /-- Image is monotone. See `set.image_image` for the statement in terms of `⊆`. -/ lemma monotone_image {f : α → β} : monotone (image f) := assume s t, assume h : s ⊆ t, image_subset _ h theorem monotone_inter [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λx, f x ∩ g x) := assume b₁ b₂ h, inter_subset_inter (hf h) (hg h) theorem monotone_union [preorder β] {f g : β → set α} (hf : monotone f) (hg : monotone g) : monotone (λx, f x ∪ g x) := assume b₁ b₂ h, union_subset_union (hf h) (hg h) theorem monotone_set_of [preorder α] {p : α → β → Prop} (hp : ∀b, monotone (λa, p a b)) : monotone (λa, {b | p a b}) := assume a a' h b, hp b h section galois_connection variables {f : α → β} protected lemma image_preimage : galois_connection (image f) (preimage f) := assume a b, image_subset_iff def kern_image (f : α → β) (s : set α) : set β := {y | ∀x, f x = y → x ∈ s} protected lemma preimage_kern_image : galois_connection (preimage f) (kern_image f) := assume a b, ⟨ assume h x hx y hy, have f y ∈ a, from hy.symm ▸ hx, h this, assume h x (hx : f x ∈ a), h hx x rfl⟩ end galois_connection /- union and intersection over a family of sets indexed by a type -/ /-- Indexed union of a family of sets -/ @[reducible] def Union (s : ι → set β) : set β := supr s /-- Indexed intersection of a family of sets -/ @[reducible] def Inter (s : ι → set β) : set β := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `⋂` binders `, ` r:(scoped f, Inter f) := r @[simp] theorem mem_Union {x : β} {s : ι → set β} : x ∈ Union s ↔ ∃ i, x ∈ s i := ⟨assume ⟨t, ⟨⟨a, (t_eq : s a = t)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq.symm ▸ h⟩, assume ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ /- alternative proof: dsimp [Union, supr, Sup]; simp -/ -- TODO: more rewrite rules wrt forall / existentials and logical connectives -- TODO: also eliminate ∃i, ... ∧ i = t ∧ ... @[simp] theorem mem_Inter {x : β} {s : ι → set β} : x ∈ Inter s ↔ ∀ i, x ∈ s i := ⟨assume (h : ∀a ∈ {a : set β | ∃i, s i = a}, x ∈ a) a, h (s a) ⟨a, rfl⟩, assume h t ⟨a, (eq : s a = t)⟩, eq ▸ h a⟩ theorem Union_subset {s : ι → set β} {t : set β} (h : ∀ i, s i ⊆ t) : (⋃ i, s i) ⊆ t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set β) _ set.lattice_set _ _ h theorem Union_subset_iff {s : ι → set β} {t : set β} : (⋃ i, s i) ⊆ t ↔ (∀ i, s i ⊆ t) := ⟨assume h i, subset.trans (le_supr s _) h, Union_subset⟩ theorem mem_Inter_of_mem {x : β} {s : ι → set β} : (∀ i, x ∈ s i) → (x ∈ ⋂ i, s i) := mem_Inter.2 theorem subset_Inter {t : set β} {s : ι → set β} (h : ∀ i, t ⊆ s i) : t ⊆ ⋂ i, s i := -- TODO: should be simpler when sets' order is based on lattices @le_infi (set β) _ set.lattice_set _ _ h theorem subset_Union : ∀ (s : ι → set β) (i : ι), s i ⊆ (⋃ i, s i) := le_supr theorem Inter_subset : ∀ (s : ι → set β) (i : ι), (⋂ i, s i) ⊆ s i := infi_le lemma Inter_subset_of_subset {s : ι → set α} {t : set α} (i : ι) (h : s i ⊆ t) : (⋂ i, s i) ⊆ t := set.subset.trans (set.Inter_subset s i) h lemma Inter_subset_Inter {s t : ι → set α} (h : ∀ i, s i ⊆ t i) : (⋂ i, s i) ⊆ (⋂ i, t i) := set.subset_Inter $ λ i, set.Inter_subset_of_subset i (h i) lemma Inter_subset_Inter2 {s : ι → set α} {t : ι' → set α} (h : ∀ j, ∃ i, s i ⊆ t j) : (⋂ i, s i) ⊆ (⋂ j, t j) := set.subset_Inter $ λ j, let ⟨i, hi⟩ := h j in Inter_subset_of_subset i hi theorem Union_const [nonempty ι] (s : set β) : (⋃ i:ι, s) = s := ext $ by simp theorem Inter_const [nonempty ι] (s : set β) : (⋂ i:ι, s) = s := ext $ by simp @[simp] -- complete_boolean_algebra theorem compl_Union (s : ι → set β) : - (⋃ i, s i) = (⋂ i, - s i) := ext (by simp) -- classical -- complete_boolean_algebra theorem compl_Inter (s : ι → set β) : -(⋂ i, s i) = (⋃ i, - s i) := ext (λ x, by simp [classical.not_forall]) -- classical -- complete_boolean_algebra theorem Union_eq_comp_Inter_comp (s : ι → set β) : (⋃ i, s i) = - (⋂ i, - s i) := by simp [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_comp_Union_comp (s : ι → set β) : (⋂ i, s i) = - (⋃ i, -s i) := by simp [compl_compl] theorem inter_Union (s : set β) (t : ι → set β) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := ext $ by simp theorem Union_inter (s : set β) (t : ι → set β) : (⋃ i, t i) ∩ s = ⋃ i, t i ∩ s := ext $ by simp theorem Union_union_distrib (s : ι → set β) (t : ι → set β) : (⋃ i, s i ∪ t i) = (⋃ i, s i) ∪ (⋃ i, t i) := ext $ by simp [exists_or_distrib] theorem Inter_inter_distrib (s : ι → set β) (t : ι → set β) : (⋂ i, s i ∩ t i) = (⋂ i, s i) ∩ (⋂ i, t i) := ext $ by simp [forall_and_distrib] theorem union_Union [nonempty ι] (s : set β) (t : ι → set β) : s ∪ (⋃ i, t i) = ⋃ i, s ∪ t i := by rw [Union_union_distrib, Union_const] theorem Union_union [nonempty ι] (s : set β) (t : ι → set β) : (⋃ i, t i) ∪ s = ⋃ i, t i ∪ s := by rw [Union_union_distrib, Union_const] theorem inter_Inter [nonempty ι] (s : set β) (t : ι → set β) : s ∩ (⋂ i, t i) = ⋂ i, s ∩ t i := by rw [Inter_inter_distrib, Inter_const] theorem Inter_inter [nonempty ι] (s : set β) (t : ι → set β) : (⋂ i, t i) ∩ s = ⋂ i, t i ∩ s := by rw [Inter_inter_distrib, Inter_const] -- classical theorem union_Inter (s : set β) (t : ι → set β) : s ∪ (⋂ i, t i) = ⋂ i, s ∪ t i := ext $ assume x, by simp [classical.forall_or_distrib_left] theorem Union_diff (s : set β) (t : ι → set β) : (⋃ i, t i) \ s = ⋃ i, t i \ s := Union_inter _ _ theorem diff_Union [nonempty ι] (s : set β) (t : ι → set β) : s \ (⋃ i, t i) = ⋂ i, s \ t i := by rw [diff_eq, compl_Union, inter_Inter]; refl theorem diff_Inter (s : set β) (t : ι → set β) : s \ (⋂ i, t i) = ⋃ i, s \ t i := by rw [diff_eq, compl_Inter, inter_Union]; refl /- bounded unions and intersections -/ theorem mem_bUnion_iff {s : set α} {t : α → set β} {y : β} : y ∈ (⋃ x ∈ s, t x) ↔ ∃ x ∈ s, y ∈ t x := by simp theorem mem_bInter_iff {s : set α} {t : α → set β} {y : β} : y ∈ (⋂ x ∈ s, t x) ↔ ∀ x ∈ s, y ∈ t x := by simp theorem mem_bUnion {s : set α} {t : α → set β} {x : α} {y : β} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := by simp; exact ⟨x, ⟨xs, ytx⟩⟩ theorem mem_bInter {s : set α} {t : α → set β} {y : β} (h : ∀ x ∈ s, y ∈ t x) : y ∈ ⋂ x ∈ s, t x := by simp; assumption theorem bUnion_subset {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, u x ⊆ t) : (⋃ x ∈ s, u x) ⊆ t := show (⨆ x ∈ s, u x) ≤ t, -- TODO: should not be necessary when sets' order is based on lattices from supr_le $ assume x, supr_le (h x) theorem subset_bInter {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, t ⊆ u x) : t ⊆ (⋂ x ∈ s, u x) := subset_Inter $ assume x, subset_Inter $ h x theorem subset_bUnion_of_mem {s : set α} {u : α → set β} {x : α} (xs : x ∈ s) : u x ⊆ (⋃ x ∈ s, u x) := show u x ≤ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set α} {t : α → set β} {x : α} (xs : x ∈ s) : (⋂ x ∈ s, t x) ⊆ t x := show (⨅x ∈ s, t x) ≤ t x, from infi_le_of_le x $ infi_le _ xs theorem bUnion_subset_bUnion_left {s s' : set α} {t : α → set β} (h : s ⊆ s') : (⋃ x ∈ s, t x) ⊆ (⋃ x ∈ s', t x) := bUnion_subset (λ x xs, subset_bUnion_of_mem (h xs)) theorem bInter_subset_bInter_left {s s' : set α} {t : α → set β} (h : s' ⊆ s) : (⋂ x ∈ s, t x) ⊆ (⋂ x ∈ s', t x) := subset_bInter (λ x xs, bInter_subset_of_mem (h xs)) theorem bUnion_subset_bUnion_right {s : set α} {t1 t2 : α → set β} (h : ∀ x ∈ s, t1 x ⊆ t2 x) : (⋃ x ∈ s, t1 x) ⊆ (⋃ x ∈ s, t2 x) := bUnion_subset (λ x xs, subset.trans (h x xs) (subset_bUnion_of_mem xs)) theorem bInter_subset_bInter_right {s : set α} {t1 t2 : α → set β} (h : ∀ x ∈ s, t1 x ⊆ t2 x) : (⋂ x ∈ s, t1 x) ⊆ (⋂ x ∈ s, t2 x) := subset_bInter (λ x xs, subset.trans (bInter_subset_of_mem xs) (h x xs)) theorem bUnion_eq_Union (s : set α) (t : α → set β) : (⋃ x ∈ s, t x) = (⋃ x : s, t x.1) := set.ext $ by simp theorem bInter_eq_Inter (s : set α) (t : α → set β) : (⋂ x ∈ s, t x) = (⋂ x : s, t x.1) := set.ext $ by simp theorem bInter_empty (u : α → set β) : (⋂ x ∈ (∅ : set α), u x) = univ := show (⨅x ∈ (∅ : set α), u x) = ⊤, -- simplifier should be able to rewrite x ∈ ∅ to false. from infi_emptyset theorem bInter_univ (u : α → set β) : (⋂ x ∈ @univ α, u x) = ⋂ x, u x := infi_univ -- TODO(Jeremy): here is an artifact of the the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. @[simp] theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : set α), s x) = s a := show (⨅ x ∈ ({a} : set α), s x) = s a, by simp theorem bInter_union (s t : set α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := show (⨅ x ∈ s ∪ t, u x) = (⨅ x ∈ s, u x) ⊓ (⨅ x ∈ t, u x), from infi_union -- TODO(Jeremy): simp [insert_eq, bInter_union] doesn't work @[simp] theorem bInter_insert (a : α) (s : set α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := begin rw insert_eq, simp [bInter_union] end -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : α) (s : α → set β) : (⋂ x ∈ ({a, b} : set α), s x) = s a ∩ s b := by simp [inter_comm] theorem bUnion_empty (s : α → set β) : (⋃ x ∈ (∅ : set α), s x) = ∅ := supr_emptyset theorem bUnion_univ (s : α → set β) : (⋃ x ∈ @univ α, s x) = ⋃ x, s x := supr_univ @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : set α), s x) = s a := supr_singleton @[simp] theorem bUnion_of_singleton (s : set α) : (⋃ x ∈ s, {x}) = s := ext $ by simp theorem bUnion_union (s t : set α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union -- TODO(Jeremy): once again, simp doesn't do it alone. @[simp] theorem bUnion_insert (a : α) (s : set α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := begin rw [insert_eq], simp [bUnion_union] end theorem bUnion_pair (a b : α) (s : α → set β) : (⋃ x ∈ ({a, b} : set α), s x) = s a ∪ s b := by simp [union_comm] @[simp] -- complete_boolean_algebra theorem compl_bUnion (s : set α) (t : α → set β) : - (⋃ i ∈ s, t i) = (⋂ i ∈ s, - t i) := ext (λ x, by simp) -- classical -- complete_boolean_algebra theorem compl_bInter (s : set α) (t : α → set β) : -(⋂ i ∈ s, t i) = (⋃ i ∈ s, - t i) := ext (λ x, by simp [classical.not_forall]) theorem inter_bUnion (s : set α) (t : α → set β) (u : set β) : u ∩ (⋃ i ∈ s, t i) = ⋃ i ∈ s, u ∩ t i := begin ext x, simp only [exists_prop, mem_Union, mem_inter_eq], exact ⟨λ ⟨hx, ⟨i, is, xi⟩⟩, ⟨i, is, hx, xi⟩, λ ⟨i, is, hx, xi⟩, ⟨hx, ⟨i, is, xi⟩⟩⟩ end theorem bUnion_inter (s : set α) (t : α → set β) (u : set β) : (⋃ i ∈ s, t i) ∩ u = (⋃ i ∈ s, t i ∩ u) := by simp [@inter_comm _ _ u, inter_bUnion] /-- Intersection of a set of sets. -/ @[reducible] def sInter (S : set (set α)) : set α := Inf S prefix `⋂₀`:110 := sInter theorem mem_sUnion_of_mem {x : α} {t : set α} {S : set (set α)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ⟨ht, hx⟩⟩ theorem mem_sUnion {x : α} {S : set (set α)} : x ∈ ⋃₀ S ↔ ∃t ∈ S, x ∈ t := iff.rfl -- is this theorem really necessary? theorem not_mem_of_not_mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∉ ⋃₀ S) (ht : t ∈ S) : x ∉ t := λ h, hx ⟨t, ht, h⟩ @[simp] theorem mem_sInter {x : α} {S : set (set α)} : x ∈ ⋂₀ S ↔ ∀ t ∈ S, x ∈ t := iff.rfl theorem sInter_subset_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : ⋂₀ S ⊆ t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : t ⊆ ⋃₀ S := le_Sup tS lemma subset_sUnion_of_subset {s : set α} (t : set (set α)) (u : set α) (h₁ : s ⊆ u) (h₂ : u ∈ t) : s ⊆ ⋃₀ t := subset.trans h₁ (subset_sUnion_of_mem h₂) theorem sUnion_subset {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t' ⊆ t) : (⋃₀ S) ⊆ t := Sup_le h theorem sUnion_subset_iff {s : set (set α)} {t : set α} : ⋃₀ s ⊆ t ↔ ∀t' ∈ s, t' ⊆ t := ⟨assume h t' ht', subset.trans (subset_sUnion_of_mem ht') h, sUnion_subset⟩ theorem subset_sInter {S : set (set α)} {t : set α} (h : ∀t' ∈ S, t ⊆ t') : t ⊆ (⋂₀ S) := le_Inf h theorem sUnion_subset_sUnion {S T : set (set α)} (h : S ⊆ T) : ⋃₀ S ⊆ ⋃₀ T := sUnion_subset $ λ s hs, subset_sUnion_of_mem (h hs) theorem sInter_subset_sInter {S T : set (set α)} (h : S ⊆ T) : ⋂₀ T ⊆ ⋂₀ S := subset_sInter $ λ s hs, sInter_subset_of_mem (h hs) @[simp] theorem sUnion_empty : ⋃₀ ∅ = (∅ : set α) := Sup_empty @[simp] theorem sInter_empty : ⋂₀ ∅ = (univ : set α) := Inf_empty @[simp] theorem sUnion_singleton (s : set α) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set α) : ⋂₀ {s} = s := Inf_singleton theorem sUnion_union (S T : set (set α)) : ⋃₀ (S ∪ T) = ⋃₀ S ∪ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set α)) : ⋂₀ (S ∪ T) = ⋂₀ S ∩ ⋂₀ T := Inf_union @[simp] theorem sUnion_insert (s : set α) (T : set (set α)) : ⋃₀ (insert s T) = s ∪ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set α) (T : set (set α)) : ⋂₀ (insert s T) = s ∩ ⋂₀ T := Inf_insert theorem sUnion_pair (s t : set α) : ⋃₀ {s, t} = s ∪ t := Sup_pair theorem sInter_pair (s t : set α) : ⋂₀ {s, t} = s ∩ t := Inf_pair @[simp] theorem sUnion_image (f : α → set β) (s : set α) : ⋃₀ (f '' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : α → set β) (s : set α) : ⋂₀ (f '' s) = ⋂ x ∈ s, f x := Inf_image @[simp] theorem sUnion_range (f : ι → set β) : ⋃₀ (range f) = ⋃ x, f x := Sup_range @[simp] theorem sInter_range (f : ι → set β) : ⋂₀ (range f) = ⋂ x, f x := Inf_range lemma sUnion_eq_univ_iff {c : set (set α)} : ⋃₀ c = @set.univ α ↔ ∀ a, ∃ b ∈ c, a ∈ b := ⟨λ H a, let ⟨b, hm, hb⟩ := mem_sUnion.1 $ by rw H; exact mem_univ a in ⟨b, hm, hb⟩, λ H, set.univ_subset_iff.1 $ λ x hx, let ⟨b, hm, hb⟩ := H x in set.mem_sUnion_of_mem hb hm⟩ theorem compl_sUnion (S : set (set α)) : - ⋃₀ S = ⋂₀ (compl '' S) := set.ext $ assume x, ⟨assume : ¬ (∃s∈S, x ∈ s), assume s h, match s, h with ._, ⟨t, hs, rfl⟩ := assume h, this ⟨t, hs, h⟩ end, assume : ∀s, s ∈ compl '' S → x ∈ s, assume ⟨t, tS, xt⟩, this (compl t) (mem_image_of_mem _ tS) xt⟩ -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set α)) : ⋃₀ S = - ⋂₀ (compl '' S) := by rw [←compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set α)) : - ⋂₀ S = ⋃₀ (compl '' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_comp_sUnion_compl (S : set (set α)) : ⋂₀ S = -(⋃₀ (compl '' S)) := by rw [←compl_compl (⋂₀ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set α} {S : set (set α)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = ∅) : s ∩ t = ∅ := eq_empty_of_subset_empty $ by rw ← h; exact inter_subset_inter_right _ (subset_sUnion_of_mem hs) theorem range_sigma_eq_Union_range {γ : α → Type*} (f : sigma γ → β) : range f = ⋃ a, range (λ b, f ⟨a, b⟩) := set.ext $ by simp theorem Union_eq_range_sigma (s : α → set β) : (⋃ i, s i) = range (λ a : Σ i, s i, a.2) := by simp [set.ext_iff] theorem Union_image_preimage_sigma_mk_eq_self {ι : Type*} {σ : ι → Type*} (s : set (sigma σ)) : (⋃ i, sigma.mk i '' (sigma.mk i ⁻¹' s)) = s := begin ext x, simp only [mem_Union, mem_image, mem_preimage], split, { rintros ⟨i, a, h, rfl⟩, exact h }, { intro h, cases x with i a, exact ⟨i, a, h, rfl⟩ } end lemma sUnion_mono {s t : set (set α)} (h : s ⊆ t) : (⋃₀ s) ⊆ (⋃₀ t) := sUnion_subset $ assume t' ht', subset_sUnion_of_mem $ h ht' lemma Union_subset_Union {s t : ι → set α} (h : ∀i, s i ⊆ t i) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr (set α) ι _ s t h lemma Union_subset_Union2 {ι₂ : Sort*} {s : ι → set α} {t : ι₂ → set α} (h : ∀i, ∃j, s i ⊆ t j) : (⋃i, s i) ⊆ (⋃i, t i) := @supr_le_supr2 (set α) ι ι₂ _ s t h lemma Union_subset_Union_const {ι₂ : Sort x} {s : set α} (h : ι → ι₂) : (⋃ i:ι, s) ⊆ (⋃ j:ι₂, s) := @supr_le_supr_const (set α) ι ι₂ _ s h @[simp] lemma Union_of_singleton (α : Type u) : (⋃(x : α), {x}) = @set.univ α := ext $ λ x, ⟨λ h, ⟨⟩, λ h, ⟨{x}, ⟨⟨x, rfl⟩, mem_singleton x⟩⟩⟩ theorem bUnion_subset_Union (s : set α) (t : α → set β) : (⋃ x ∈ s, t x) ⊆ (⋃ x, t x) := Union_subset_Union $ λ i, Union_subset $ λ h, by refl lemma sUnion_eq_bUnion {s : set (set α)} : (⋃₀ s) = (⋃ (i : set α) (h : i ∈ s), i) := set.ext $ by simp lemma sInter_eq_bInter {s : set (set α)} : (⋂₀ s) = (⋂ (i : set α) (h : i ∈ s), i) := set.ext $ by simp lemma sUnion_eq_Union {s : set (set α)} : (⋃₀ s) = (⋃ (i : s), i.1) := set.ext $ λ x, by simp lemma sInter_eq_Inter {s : set (set α)} : (⋂₀ s) = (⋂ (i : s), i.1) := set.ext $ λ x, by simp lemma union_eq_Union {s₁ s₂ : set α} : s₁ ∪ s₂ = ⋃ b : bool, cond b s₁ s₂ := set.ext $ λ x, by simp [bool.exists_bool, or_comm] lemma inter_eq_Inter {s₁ s₂ : set α} : s₁ ∩ s₂ = ⋂ b : bool, cond b s₁ s₂ := set.ext $ λ x, by simp [bool.forall_bool, and_comm] instance : complete_boolean_algebra (set α) := { neg := compl, sub := (\), le_sup_inf := distrib_lattice.le_sup_inf, inf_compl_eq_bot := assume s, ext $ assume x, ⟨assume ⟨h, nh⟩, nh h, false.elim⟩, sup_compl_eq_top := assume s, ext $ assume x, ⟨assume h, trivial, assume _, classical.em $ x ∈ s⟩, sub_eq := assume x y, rfl, infi_sup_le_sup_Inf := assume s t x, show x ∈ (⋂ b ∈ t, s ∪ b) → x ∈ s ∪ (⋂₀ t), by simp; exact assume h, or.imp_right (assume hn : x ∉ s, assume i hi, or.resolve_left (h i hi) hn) (classical.em $ x ∈ s), inf_Sup_le_supr_inf := assume s t x, show x ∈ s ∩ (⋃₀ t) → x ∈ (⋃ b ∈ t, s ∩ b), by simp [-and_imp, and.left_comm], ..set.lattice_set } lemma sInter_union_sInter {S T : set (set α)} : (⋂₀S) ∪ (⋂₀T) = (⋂p ∈ set.prod S T, (p : (set α) × (set α)).1 ∪ p.2) := Inf_sup_Inf lemma sUnion_inter_sUnion {s t : set (set α)} : (⋃₀s) ∩ (⋃₀t) = (⋃p ∈ set.prod s t, (p : (set α) × (set α )).1 ∩ p.2) := Sup_inf_Sup lemma sInter_bUnion {S : set (set α)} {T : set α → set (set α)} (hT : ∀s∈S, s = ⋂₀ T s) : ⋂₀ (⋃s∈S, T s) = ⋂₀ S := begin ext, simp only [and_imp, exists_prop, set.mem_sInter, set.mem_Union, exists_imp_distrib], split, { assume H s sS, rw [hT s sS, mem_sInter], assume t tTs, apply H t s sS tTs }, { assume H t s sS tTs, have xs : x ∈ s := H s sS, have : s ⊆ t, { have Z := hT s sS, rw sInter_eq_bInter at Z, rw Z, apply bInter_subset_of_mem, exact tTs }, exact this xs } end lemma sUnion_bUnion {S : set (set α)} {T : set α → set (set α)} (hT : ∀s∈S, s = ⋃₀ T s) : ⋃₀ (⋃s∈S, T s) = ⋃₀ S := begin ext, simp only [exists_prop, set.mem_Union, set.mem_set_of_eq], split, { rintros ⟨t, ⟨⟨s, ⟨sS, tTs⟩⟩, xt⟩⟩, refine ⟨s, ⟨sS, _⟩⟩, rw hT s sS, exact subset_sUnion_of_mem tTs xt }, { rintros ⟨s, ⟨sS, xs⟩⟩, rw hT s sS at xs, rcases mem_sUnion.1 xs with ⟨t, tTs, xt⟩, exact ⟨t, ⟨⟨s, ⟨sS, tTs⟩⟩, xt⟩⟩ } end lemma Union_range_eq_sUnion {α β : Type*} (C : set (set α)) {f : ∀(s : C), β → s} (hf : ∀(s : C), surjective (f s)) : (⋃(y : β), range (λ(s : C), (f s y).val)) = ⋃₀ C := begin ext x, split, { rintro ⟨s, ⟨y, rfl⟩, ⟨⟨s, hs⟩, rfl⟩⟩, refine ⟨_, hs, _⟩, exact (f ⟨s, hs⟩ y).2 }, { rintro ⟨s, hs, hx⟩, cases hf ⟨s, hs⟩ ⟨x, hx⟩ with y hy, refine ⟨_, ⟨y, rfl⟩, ⟨⟨s, hs⟩, _⟩⟩, exact congr_arg subtype.val hy } end lemma Union_range_eq_Union {ι α β : Type*} (C : ι → set α) {f : ∀(x : ι), β → C x} (hf : ∀(x : ι), surjective (f x)) : (⋃(y : β), range (λ(x : ι), (f x y).val)) = ⋃x, C x := begin ext x, rw [mem_Union, mem_Union], split, { rintro ⟨y, ⟨i, rfl⟩⟩, exact ⟨i, (f i y).2⟩ }, { rintro ⟨i, hx⟩, cases hf i ⟨x, hx⟩ with y hy, refine ⟨y, ⟨i, congr_arg subtype.val hy⟩⟩ } end @[simp] theorem sub_eq_diff (s t : set α) : s - t = s \ t := rfl section variables {p : Prop} {μ : p → set α} @[simp] lemma Inter_pos (hp : p) : (⋂h:p, μ h) = μ hp := infi_pos hp @[simp] lemma Inter_neg (hp : ¬ p) : (⋂h:p, μ h) = univ := infi_neg hp @[simp] lemma Union_pos (hp : p) : (⋃h:p, μ h) = μ hp := supr_pos hp @[simp] lemma Union_neg (hp : ¬ p) : (⋃h:p, μ h) = ∅ := supr_neg hp @[simp] lemma Union_empty {ι : Sort*} : (⋃i:ι, ∅:set α) = ∅ := supr_bot @[simp] lemma Inter_univ {ι : Sort*} : (⋂i:ι, univ:set α) = univ := infi_top end section image lemma image_Union {f : α → β} {s : ι → set α} : f '' (⋃ i, s i) = (⋃i, f '' s i) := begin apply set.ext, intro x, simp [image, exists_and_distrib_right.symm, -exists_and_distrib_right], exact exists_swap end lemma univ_subtype {p : α → Prop} : (univ : set (subtype p)) = (⋃x (h : p x), {⟨x, h⟩}) := set.ext $ assume ⟨x, h⟩, by simp [h] lemma range_eq_Union {ι} (f : ι → α) : range f = (⋃i, {f i}) := set.ext $ assume a, by simp [@eq_comm α a] lemma image_eq_Union (f : α → β) (s : set α) : f '' s = (⋃i∈s, {f i}) := set.ext $ assume b, by simp [@eq_comm β b] @[simp] lemma bUnion_range {f : ι → α} {g : α → set β} : (⋃x ∈ range f, g x) = (⋃y, g (f y)) := by rw [← sUnion_image, ← range_comp, sUnion_range] @[simp] lemma bInter_range {f : ι → α} {g : α → set β} : (⋂x ∈ range f, g x) = (⋂y, g (f y)) := by rw [← sInter_image, ← range_comp, sInter_range] variables {s : set γ} {f : γ → α} {g : α → set β} @[simp] lemma bUnion_image : (⋃x∈ (f '' s), g x) = (⋃y ∈ s, g (f y)) := by rw [← sUnion_image, ← image_comp, sUnion_image] @[simp] lemma bInter_image : (⋂x∈ (f '' s), g x) = (⋂y ∈ s, g (f y)) := by rw [← sInter_image, ← image_comp, sInter_image] end image section preimage theorem monotone_preimage {f : α → β} : monotone (preimage f) := assume a b h, preimage_mono h @[simp] theorem preimage_Union {ι : Sort w} {f : α → β} {s : ι → set β} : preimage f (⋃i, s i) = (⋃i, preimage f (s i)) := set.ext $ by simp [preimage] theorem preimage_bUnion {ι} {f : α → β} {s : set ι} {t : ι → set β} : preimage f (⋃i ∈ s, t i) = (⋃i ∈ s, preimage f (t i)) := by simp @[simp] theorem preimage_sUnion {f : α → β} {s : set (set β)} : preimage f (⋃₀ s) = (⋃t ∈ s, preimage f t) := set.ext $ by simp [preimage] lemma preimage_Inter {ι : Sort*} {s : ι → set β} {f : α → β} : f ⁻¹' (⋂ i, s i) = (⋂ i, f ⁻¹' s i) := by ext; simp lemma preimage_bInter {s : γ → set β} {t : set γ} {f : α → β} : f ⁻¹' (⋂ i∈t, s i) = (⋂ i∈t, f ⁻¹' s i) := by ext; simp end preimage section seq def seq (s : set (α → β)) (t : set α) : set β := {b | ∃f∈s, ∃a∈t, (f : α → β) a = b} lemma seq_def {s : set (α → β)} {t : set α} : seq s t = ⋃f∈s, f '' t := set.ext $ by simp [seq] @[simp] lemma mem_seq_iff {s : set (α → β)} {t : set α} {b : β} : b ∈ seq s t ↔ ∃ (f ∈ s) (a ∈ t), (f : α → β) a = b := iff.rfl lemma seq_subset {s : set (α → β)} {t : set α} {u : set β} : seq s t ⊆ u ↔ (∀f∈s, ∀a∈t, (f : α → β) a ∈ u) := iff.intro (assume h f hf a ha, h ⟨f, hf, a, ha, rfl⟩) (assume h b ⟨f, hf, a, ha, eq⟩, eq ▸ h f hf a ha) lemma seq_mono {s₀ s₁ : set (α → β)} {t₀ t₁ : set α} (hs : s₀ ⊆ s₁) (ht : t₀ ⊆ t₁) : seq s₀ t₀ ⊆ seq s₁ t₁ := assume b ⟨f, hf, a, ha, eq⟩, ⟨f, hs hf, a, ht ha, eq⟩ lemma singleton_seq {f : α → β} {t : set α} : set.seq {f} t = f '' t := set.ext $ by simp lemma seq_singleton {s : set (α → β)} {a : α} : set.seq s {a} = (λf:α→β, f a) '' s := set.ext $ by simp lemma seq_seq {s : set (β → γ)} {t : set (α → β)} {u : set α} : seq s (seq t u) = seq (seq ((∘) '' s) t) u := begin refine set.ext (assume c, iff.intro _ _), { rintros ⟨f, hfs, b, ⟨g, hg, a, hau, rfl⟩, rfl⟩, exact ⟨f ∘ g, ⟨(∘) f, mem_image_of_mem _ hfs, g, hg, rfl⟩, a, hau, rfl⟩ }, { rintros ⟨fg, ⟨fc, ⟨f, hfs, rfl⟩, g, hgt, rfl⟩, a, ha, rfl⟩, exact ⟨f, hfs, g a, ⟨g, hgt, a, ha, rfl⟩, rfl⟩ } end lemma image_seq {f : β → γ} {s : set (α → β)} {t : set α} : f '' seq s t = seq ((∘) f '' s) t := by rw [← singleton_seq, ← singleton_seq, seq_seq, image_singleton] lemma prod_eq_seq {s : set α} {t : set β} : set.prod s t = (prod.mk '' s).seq t := begin ext ⟨a, b⟩, split, { rintros ⟨ha, hb⟩, exact ⟨prod.mk a, ⟨a, ha, rfl⟩, b, hb, rfl⟩ }, { rintros ⟨f, ⟨x, hx, rfl⟩, y, hy, eq⟩, rw ← eq, exact ⟨hx, hy⟩ } end lemma prod_image_seq_comm (s : set α) (t : set β) : (prod.mk '' s).seq t = seq ((λb a, (a, b)) '' t) s := by rw [← prod_eq_seq, ← image_swap_prod, prod_eq_seq, image_seq, ← image_comp] end seq theorem monotone_prod [preorder α] {f : α → set β} {g : α → set γ} (hf : monotone f) (hg : monotone g) : monotone (λx, set.prod (f x) (g x)) := assume a b h, prod_mono (hf h) (hg h) instance : monad set := { pure := λ(α : Type u) a, {a}, bind := λ(α β : Type u) s f, ⋃i∈s, f i, seq := λ(α β : Type u), set.seq, map := λ(α β : Type u), set.image } instance : is_lawful_monad set := { pure_bind := assume α β x f, by simp, bind_assoc := assume α β γ s f g, set.ext $ assume a, by simp [exists_and_distrib_right.symm, -exists_and_distrib_right, exists_and_distrib_left.symm, -exists_and_distrib_left, and_assoc]; exact exists_swap, id_map := assume α, id_map, bind_pure_comp_eq_map := assume α β f s, set.ext $ by simp [set.image, eq_comm], bind_map_eq_seq := assume α β s t, by simp [seq_def] } instance : is_comm_applicative (set : Type u → Type u) := ⟨ assume α β s t, prod_image_seq_comm s t ⟩ section monad variables {α' β' : Type u} {s : set α'} {f : α' → set β'} {g : set (α' → β')} @[simp] lemma bind_def : s >>= f = ⋃i∈s, f i := rfl @[simp] lemma fmap_eq_image (f : α' → β') : f <$> s = f '' s := rfl @[simp] lemma seq_eq_set_seq {α β : Type*} (s : set (α → β)) (t : set α) : s <*> t = s.seq t := rfl @[simp] lemma pure_def (a : α) : (pure a : set α) = {a} := rfl end monad section pi lemma pi_def {α : Type*} {π : α → Type*} (i : set α) (s : Πa, set (π a)) : pi i s = (⋂ a∈i, ((λf:(Πa, π a), f a) ⁻¹' (s a))) := by ext; simp [pi] end pi end set /- disjoint sets -/ section disjoint variable [semilattice_inf_bot α] /-- Two elements of a lattice are disjoint if their inf is the bottom element. (This generalizes disjoint sets, viewed as members of the subset lattice.) -/ def disjoint (a b : α) : Prop := a ⊓ b ≤ ⊥ theorem disjoint.eq_bot {a b : α} (h : disjoint a b) : a ⊓ b = ⊥ := eq_bot_iff.2 h theorem disjoint_iff {a b : α} : disjoint a b ↔ a ⊓ b = ⊥ := eq_bot_iff.symm theorem disjoint.comm {a b : α} : disjoint a b ↔ disjoint b a := by rw [disjoint, disjoint, inf_comm] theorem disjoint.symm {a b : α} : disjoint a b → disjoint b a := disjoint.comm.1 @[simp] theorem disjoint_bot_left {a : α} : disjoint ⊥ a := disjoint_iff.2 bot_inf_eq @[simp] theorem disjoint_bot_right {a : α} : disjoint a ⊥ := disjoint_iff.2 inf_bot_eq theorem disjoint_mono {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) : disjoint b d → disjoint a c := le_trans (inf_le_inf h₁ h₂) theorem disjoint_mono_left {a b c : α} (h : a ≤ b) : disjoint b c → disjoint a c := disjoint_mono h (le_refl _) theorem disjoint_mono_right {a b c : α} (h : b ≤ c) : disjoint a c → disjoint a b := disjoint_mono (le_refl _) h @[simp] lemma disjoint_self {a : α} : disjoint a a ↔ a = ⊥ := by simp [disjoint] lemma ne_of_disjoint {a b : α} (ha : a ≠ ⊥) (hab : disjoint a b) : a ≠ b := by { intro h, rw [←h, disjoint_self] at hab, exact ha hab } end disjoint namespace set protected theorem disjoint_iff {s t : set α} : disjoint s t ↔ s ∩ t ⊆ ∅ := iff.rfl lemma not_disjoint_iff {s t : set α} : ¬disjoint s t ↔ ∃x, x ∈ s ∧ x ∈ t := (not_congr (set.disjoint_iff.trans subset_empty_iff)).trans ne_empty_iff_nonempty lemma disjoint_left {s t : set α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := show (∀ x, ¬(x ∈ s ∩ t)) ↔ _, from ⟨λ h a, not_and.1 $ h a, λ h a, not_and.2 $ h a⟩ theorem disjoint_right {s t : set α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_diff {a b : set α} : disjoint a (b \ a) := disjoint_iff.2 (inter_diff_self _ _) theorem disjoint_compl (s : set α) : disjoint s (-s) := assume a ⟨h₁, h₂⟩, h₂ h₁ theorem disjoint_singleton_left {a : α} {s : set α} : disjoint {a} s ↔ a ∉ s := by simp [set.disjoint_iff, subset_def]; exact iff.rfl theorem disjoint_singleton_right {a : α} {s : set α} : disjoint s {a} ↔ a ∉ s := by rw [disjoint.comm]; exact disjoint_singleton_left theorem disjoint_image_image {f : β → α} {g : γ → α} {s : set β} {t : set γ} (h : ∀b∈s, ∀c∈t, f b ≠ g c) : disjoint (f '' s) (g '' t) := by rintros a ⟨⟨b, hb, eq⟩, ⟨c, hc, rfl⟩⟩; exact h b hb c hc eq def pairwise_disjoint (s : set (set α)) : Prop := pairwise_on s disjoint lemma pairwise_disjoint_subset {s t : set (set α)} (h : s ⊆ t) (ht : pairwise_disjoint t) : pairwise_disjoint s := pairwise_on.mono h ht lemma pairwise_disjoint_range {s : set (set α)} (f : s → set α) (hf : ∀(x : s), f x ⊆ x.1) (ht : pairwise_disjoint s) : pairwise_disjoint (range f) := begin rintro _ ⟨x, rfl⟩ _ ⟨y, rfl⟩ hxy, refine disjoint_mono (hf x) (hf y) (ht _ x.2 _ y.2 _), intro h, apply hxy, apply congr_arg f, exact subtype.eq h end /- warning: classical -/ lemma pairwise_disjoint_elim {s : set (set α)} (h : pairwise_disjoint s) {x y : set α} (hx : x ∈ s) (hy : y ∈ s) (z : α) (hzx : z ∈ x) (hzy : z ∈ y) : x = y := classical.not_not.1 $ λ h', h x hx y hy h' ⟨hzx, hzy⟩ end set namespace set variables (t : α → set β) def sigma_to_Union (x : Σi, t i) : (⋃i, t i) := ⟨x.2, mem_Union.2 ⟨x.1, x.2.2⟩⟩ lemma surjective_sigma_to_Union : surjective (sigma_to_Union t) | ⟨b, hb⟩ := have ∃a, b ∈ t a, by simpa using hb, let ⟨a, hb⟩ := this in ⟨⟨a, ⟨b, hb⟩⟩, rfl⟩ lemma injective_sigma_to_Union (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : injective (sigma_to_Union t) | ⟨a₁, ⟨b₁, h₁⟩⟩ ⟨a₂, ⟨b₂, h₂⟩⟩ eq := have b_eq : b₁ = b₂, from congr_arg subtype.val eq, have a_eq : a₁ = a₂, from classical.by_contradiction $ assume ne, have b₁ ∈ t a₁ ∩ t a₂, from ⟨h₁, b_eq.symm ▸ h₂⟩, h _ _ ne this, sigma.eq a_eq $ subtype.eq $ by subst b_eq; subst a_eq lemma bijective_sigma_to_Union (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : bijective (sigma_to_Union t) := ⟨injective_sigma_to_Union t h, surjective_sigma_to_Union t⟩ noncomputable def Union_eq_sigma_of_disjoint {t : α → set β} (h : ∀i j, i ≠ j → disjoint (t i) (t j)) : (⋃i, t i) ≃ (Σi, t i) := (equiv.of_bijective $ bijective_sigma_to_Union t h).symm noncomputable def bUnion_eq_sigma_of_disjoint {s : set α} {t : α → set β} (h : pairwise_on s (disjoint on t)) : (⋃i∈s, t i) ≃ (Σi:s, t i.val) := equiv.trans (equiv.set_congr (bUnion_eq_Union _ _)) $ Union_eq_sigma_of_disjoint $ assume ⟨i, hi⟩ ⟨j, hj⟩ ne, h _ hi _ hj $ assume eq, ne $ subtype.eq eq end set
1ac4945281272884ac210493ed9167b16acba9c1
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/category_theory/category/Twop.lean
af4b02063d683f85b16fb56c7d649dd5b3a2a7b6
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
5,118
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import category_theory.category.Bipointed import data.two_pointing /-! # The category of two-pointed types This defines `Twop`, the category of two-pointed types. ## References * [nLab, *coalgebra of the real interval*] (https://ncatlab.org/nlab/show/coalgebra+of+the+real+interval) -/ open category_theory option universes u variables {α β : Type*} /-- The category of two-pointed types. -/ structure Twop : Type.{u+1} := (X : Type.{u}) (to_two_pointing : two_pointing X) namespace Twop instance : has_coe_to_sort Twop Type* := ⟨X⟩ attribute [protected] Twop.X /-- Turns a two-pointing into a two-pointed type. -/ def of {X : Type*} (to_two_pointing : two_pointing X) : Twop := ⟨X, to_two_pointing⟩ @[simp] lemma coe_of {X : Type*} (to_two_pointing : two_pointing X) : ↥(of to_two_pointing) = X := rfl alias of ← _root_.two_pointing.Twop instance : inhabited Twop := ⟨of two_pointing.bool⟩ /-- Turns a two-pointed type into a bipointed type, by forgetting that the pointed elements are distinct. -/ def to_Bipointed (X : Twop) : Bipointed := X.to_two_pointing.to_prod.Bipointed @[simp] lemma coe_to_Bipointed (X : Twop) : ↥X.to_Bipointed = ↥X := rfl instance large_category : large_category Twop := induced_category.category to_Bipointed instance concrete_category : concrete_category Twop := induced_category.concrete_category to_Bipointed instance has_forget_to_Bipointed : has_forget₂ Twop Bipointed := induced_category.has_forget₂ to_Bipointed /-- Swaps the pointed elements of a two-pointed type. `two_pointing.swap` as a functor. -/ @[simps] def swap : Twop ⥤ Twop := { obj := λ X, ⟨X, X.to_two_pointing.swap⟩, map := λ X Y f, ⟨f.to_fun, f.map_snd, f.map_fst⟩ } /-- The equivalence between `Twop` and itself induced by `prod.swap` both ways. -/ @[simps] def swap_equiv : Twop ≌ Twop := equivalence.mk swap swap (nat_iso.of_components (λ X, { hom := ⟨id, rfl, rfl⟩, inv := ⟨id, rfl, rfl⟩ }) $ λ X Y f, rfl) (nat_iso.of_components (λ X, { hom := ⟨id, rfl, rfl⟩, inv := ⟨id, rfl, rfl⟩ }) $ λ X Y f, rfl) @[simp] lemma swap_equiv_symm : swap_equiv.symm = swap_equiv := rfl end Twop @[simp] lemma Twop_swap_comp_forget_to_Bipointed : Twop.swap ⋙ forget₂ Twop Bipointed = forget₂ Twop Bipointed ⋙ Bipointed.swap := rfl /-- The functor from `Pointed` to `Twop` which adds a second point. -/ @[simps] def Pointed_to_Twop_fst : Pointed.{u} ⥤ Twop := { obj := λ X, ⟨option X, ⟨X.point, none⟩, some_ne_none _⟩, map := λ X Y f, ⟨option.map f.to_fun, congr_arg _ f.map_point, rfl⟩, map_id' := λ X, Bipointed.hom.ext _ _ option.map_id, map_comp' := λ X Y Z f g, Bipointed.hom.ext _ _ (option.map_comp_map _ _).symm } /-- The functor from `Pointed` to `Twop` which adds a first point. -/ @[simps] def Pointed_to_Twop_snd : Pointed.{u} ⥤ Twop := { obj := λ X, ⟨option X, ⟨none, X.point⟩, (some_ne_none _).symm⟩, map := λ X Y f, ⟨option.map f.to_fun, rfl, congr_arg _ f.map_point⟩, map_id' := λ X, Bipointed.hom.ext _ _ option.map_id, map_comp' := λ X Y Z f g, Bipointed.hom.ext _ _ (option.map_comp_map _ _).symm } @[simp] lemma Pointed_to_Twop_fst_comp_swap : Pointed_to_Twop_fst ⋙ Twop.swap = Pointed_to_Twop_snd := rfl @[simp] lemma Pointed_to_Twop_snd_comp_swap : Pointed_to_Twop_snd ⋙ Twop.swap = Pointed_to_Twop_fst := rfl @[simp] lemma Pointed_to_Twop_fst_comp_forget_to_Bipointed : Pointed_to_Twop_fst ⋙ forget₂ Twop Bipointed = Pointed_to_Bipointed_fst := rfl @[simp] lemma Pointed_to_Twop_snd_comp_forget_to_Bipointed : Pointed_to_Twop_snd ⋙ forget₂ Twop Bipointed = Pointed_to_Bipointed_snd := rfl /-- Adding a second point is left adjoint to forgetting the second point. -/ def Pointed_to_Twop_fst_forget_comp_Bipointed_to_Pointed_fst_adjunction : Pointed_to_Twop_fst ⊣ forget₂ Twop Bipointed ⋙ Bipointed_to_Pointed_fst := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, { to_fun := λ f, ⟨f.to_fun ∘ option.some, f.map_fst⟩, inv_fun := λ f, ⟨λ o, o.elim Y.to_two_pointing.to_prod.2 f.to_fun, f.map_point, rfl⟩, left_inv := λ f, by { ext, cases x, exact f.map_snd.symm, refl }, right_inv := λ f, Pointed.hom.ext _ _ rfl }, hom_equiv_naturality_left_symm' := λ X' X Y f g, by { ext, cases x; refl } } /-- Adding a first point is left adjoint to forgetting the first point. -/ def Pointed_to_Twop_snd_forget_comp_Bipointed_to_Pointed_snd_adjunction : Pointed_to_Twop_snd ⊣ forget₂ Twop Bipointed ⋙ Bipointed_to_Pointed_snd := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, { to_fun := λ f, ⟨f.to_fun ∘ option.some, f.map_snd⟩, inv_fun := λ f, ⟨λ o, o.elim Y.to_two_pointing.to_prod.1 f.to_fun, rfl, f.map_point⟩, left_inv := λ f, by { ext, cases x, exact f.map_fst.symm, refl }, right_inv := λ f, Pointed.hom.ext _ _ rfl }, hom_equiv_naturality_left_symm' := λ X' X Y f g, by { ext, cases x; refl } }
4eea41d2305782c0e559f255d4f534a55ebdcf1e
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Declaration.lean
1b3fb68e390766e82b208f6e66e807edc90a5ecd
[ "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
16,996
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Expr namespace Lean /-- Reducibility hints are used in the convertibility checker. When trying to solve a constraint such a (f ...) =?= (g ...) where f and g are definitions, the checker has to decide which one will be unfolded. If f (g) is opaque, then g (f) is unfolded if it is also not marked as opaque, Else if f (g) is abbrev, then f (g) is unfolded if g (f) is also not marked as abbrev, Else if f and g are regular, then we unfold the one with the biggest definitional height. Otherwise both are unfolded. The arguments of the `regular` Constructor are: the definitional height and the flag `selfOpt`. The definitional height is by default computed by the kernel. It only takes into account other regular definitions used in a definition. When creating declarations using meta-programming, we can specify the definitional depth manually. Remark: the hint only affects performance. None of the hints prevent the kernel from unfolding a declaration during Type checking. Remark: the ReducibilityHints are not related to the attributes: reducible/irrelevance/semireducible. These attributes are used by the Elaborator. The ReducibilityHints are used by the kernel (and Elaborator). Moreover, the ReducibilityHints cannot be changed after a declaration is added to the kernel. -/ inductive ReducibilityHints where | opaque : ReducibilityHints | abbrev : ReducibilityHints | regular : UInt32 → ReducibilityHints deriving Inhabited @[export lean_mk_reducibility_hints_regular] def mkReducibilityHintsRegularEx (h : UInt32) : ReducibilityHints := ReducibilityHints.regular h @[export lean_reducibility_hints_get_height] def ReducibilityHints.getHeightEx (h : ReducibilityHints) : UInt32 := match h with | ReducibilityHints.regular h => h | _ => 0 namespace ReducibilityHints def lt : ReducibilityHints → ReducibilityHints → Bool | .abbrev, .abbrev => false | .abbrev, _ => true | .regular d₁, .regular d₂ => d₁ < d₂ | .regular _, .opaque => true | _, _ => false def isAbbrev : ReducibilityHints → Bool | .abbrev => true | _ => false def isRegular : ReducibilityHints → Bool | regular .. => true | _ => false end ReducibilityHints /-- Base structure for `AxiomVal`, `DefinitionVal`, `TheoremVal`, `InductiveVal`, `ConstructorVal`, `RecursorVal` and `QuotVal`. -/ structure ConstantVal where name : Name levelParams : List Name type : Expr deriving Inhabited structure AxiomVal extends ConstantVal where isUnsafe : Bool deriving Inhabited @[export lean_mk_axiom_val] def mkAxiomValEx (name : Name) (levelParams : List Name) (type : Expr) (isUnsafe : Bool) : AxiomVal := { name := name, levelParams := levelParams, type := type, isUnsafe := isUnsafe } @[export lean_axiom_val_is_unsafe] def AxiomVal.isUnsafeEx (v : AxiomVal) : Bool := v.isUnsafe inductive DefinitionSafety where | «unsafe» | safe | «partial» deriving Inhabited, BEq, Repr structure DefinitionVal extends ConstantVal where value : Expr hints : ReducibilityHints safety : DefinitionSafety /-- List of all (including this one) declarations in the same mutual block. Note that this information is not used by the kernel, and is only used to save the information provided by the user when using mutual blocks. Recall that the Lean kernel does not support recursive definitions and they are compiled using recursors and `WellFounded.fix`. -/ all : List Name := [name] deriving Inhabited @[export lean_mk_definition_val] def mkDefinitionValEx (name : Name) (levelParams : List Name) (type : Expr) (value : Expr) (hints : ReducibilityHints) (safety : DefinitionSafety) (all : List Name) : DefinitionVal := { name, levelParams, type, hints, safety, value, all } @[export lean_definition_val_get_safety] def DefinitionVal.getSafetyEx (v : DefinitionVal) : DefinitionSafety := v.safety structure TheoremVal extends ConstantVal where value : Expr /-- List of all (including this one) declarations in the same mutual block. See comment at `DefinitionVal.all`. -/ all : List Name := [name] deriving Inhabited /-- Value for an opaque constant declaration `opaque x : t := e` -/ structure OpaqueVal extends ConstantVal where value : Expr isUnsafe : Bool /-- List of all (including this one) declarations in the same mutual block. See comment at `DefinitionVal.all`. -/ all : List Name := [name] deriving Inhabited @[export lean_mk_opaque_val] def mkOpaqueValEx (name : Name) (levelParams : List Name) (type : Expr) (value : Expr) (isUnsafe : Bool) (all : List Name) : OpaqueVal := { name, levelParams, type, value, isUnsafe, all } @[export lean_opaque_val_is_unsafe] def OpaqueVal.isUnsafeEx (v : OpaqueVal) : Bool := v.isUnsafe structure Constructor where name : Name type : Expr deriving Inhabited structure InductiveType where name : Name type : Expr ctors : List Constructor deriving Inhabited /-- Declaration object that can be sent to the kernel. -/ inductive Declaration where | axiomDecl (val : AxiomVal) | defnDecl (val : DefinitionVal) | thmDecl (val : TheoremVal) | opaqueDecl (val : OpaqueVal) | quotDecl | mutualDefnDecl (defns : List DefinitionVal) -- All definitions must be marked as `unsafe` or `partial` | inductDecl (lparams : List Name) (nparams : Nat) (types : List InductiveType) (isUnsafe : Bool) deriving Inhabited @[export lean_mk_inductive_decl] def mkInductiveDeclEs (lparams : List Name) (nparams : Nat) (types : List InductiveType) (isUnsafe : Bool) : Declaration := Declaration.inductDecl lparams nparams types isUnsafe @[export lean_is_unsafe_inductive_decl] def Declaration.isUnsafeInductiveDeclEx : Declaration → Bool | Declaration.inductDecl _ _ _ isUnsafe => isUnsafe | _ => false @[specialize] def Declaration.foldExprM {α} {m : Type → Type} [Monad m] (d : Declaration) (f : α → Expr → m α) (a : α) : m α := match d with | Declaration.quotDecl => pure a | Declaration.axiomDecl { type := type, .. } => f a type | Declaration.defnDecl { type := type, value := value, .. } => do let a ← f a type; f a value | Declaration.opaqueDecl { type := type, value := value, .. } => do let a ← f a type; f a value | Declaration.thmDecl { type := type, value := value, .. } => do let a ← f a type; f a value | Declaration.mutualDefnDecl vals => vals.foldlM (fun a v => do let a ← f a v.type; f a v.value) a | Declaration.inductDecl _ _ inductTypes _ => inductTypes.foldlM (fun a inductType => do let a ← f a inductType.type inductType.ctors.foldlM (fun a ctor => f a ctor.type) a) a @[inline] def Declaration.forExprM {m : Type → Type} [Monad m] (d : Declaration) (f : Expr → m Unit) : m Unit := d.foldExprM (fun _ a => f a) () /-- The kernel compiles (mutual) inductive declarations (see `inductiveDecls`) into a set of - `Declaration.inductDecl` (for each inductive datatype in the mutual Declaration), - `Declaration.ctorDecl` (for each Constructor in the mutual Declaration), - `Declaration.recDecl` (automatically generated recursors). This data is used to implement iota-reduction efficiently and compile nested inductive declarations. A series of checks are performed by the kernel to check whether a `inductiveDecls` is valid or not. -/ structure InductiveVal extends ConstantVal where /-- Number of parameters. A parameter is an argument to the defined type that is fixed over constructors. An example of this is the `α : Type` argument in the vector constructors `nil : Vector α 0` and `cons : α → Vector α n → Vector α (n+1)`. The intuition is that the inductive type must exhibit _parametric polymorphism_ over the inductive parameter, as opposed to _ad-hoc polymorphism_. -/ numParams : Nat /-- Number of indices. An index is an argument that varies over constructors. An example of this is the `n : Nat` argument in the vector constructor `cons : α → Vector α n → Vector α (n+1)`. -/ numIndices : Nat /-- List of all (including this one) inductive datatypes in the mutual declaration containing this one -/ all : List Name /-- List of the names of the constructors for this inductive datatype. -/ ctors : List Name /-- `true` when recursive (that is, the inductive type appears as an argument in a constructor). -/ isRec : Bool /-- Whether the definition is flagged as unsafe. -/ isUnsafe : Bool /-- An inductive type is called reflexive if it has at least one constructor that takes as an argument a function returning the same type we are defining. Consider the type: ``` inductive WideTree where | branch: (Nat -> WideTree) -> WideTree | leaf: WideTree ``` this is reflexive due to the presence of the `branch : (Nat -> WideTree) -> WideTree` constructor. See also: 'Inductive Definitions in the system Coq Rules and Properties' by Christine Paulin-Mohring Section 2.2, Definition 3 -/ isReflexive : Bool /-- An inductive definition `T` is nested when there is a constructor with an argument `x : F T`, where `F : Type → Type` is some suitably behaved (ie strictly positive) function (Eg `Array T`, `List T`, `T × T`, ...). -/ isNested : Bool deriving Inhabited @[export lean_mk_inductive_val] def mkInductiveValEx (name : Name) (levelParams : List Name) (type : Expr) (numParams numIndices : Nat) (all ctors : List Name) (isRec isUnsafe isReflexive isNested : Bool) : InductiveVal := { name := name levelParams := levelParams type := type numParams := numParams numIndices := numIndices all := all ctors := ctors isRec := isRec isUnsafe := isUnsafe isReflexive := isReflexive isNested := isNested } @[export lean_inductive_val_is_rec] def InductiveVal.isRecEx (v : InductiveVal) : Bool := v.isRec @[export lean_inductive_val_is_unsafe] def InductiveVal.isUnsafeEx (v : InductiveVal) : Bool := v.isUnsafe @[export lean_inductive_val_is_reflexive] def InductiveVal.isReflexiveEx (v : InductiveVal) : Bool := v.isReflexive @[export lean_inductive_val_is_nested] def InductiveVal.isNestedEx (v : InductiveVal) : Bool := v.isNested def InductiveVal.numCtors (v : InductiveVal) : Nat := v.ctors.length structure ConstructorVal extends ConstantVal where /-- Inductive type this constructor is a member of -/ induct : Name /-- Constructor index (i.e., Position in the inductive declaration) -/ cidx : Nat /-- Number of parameters in inductive datatype. -/ numParams : Nat /-- Number of fields (i.e., arity - nparams) -/ numFields : Nat isUnsafe : Bool deriving Inhabited @[export lean_mk_constructor_val] def mkConstructorValEx (name : Name) (levelParams : List Name) (type : Expr) (induct : Name) (cidx numParams numFields : Nat) (isUnsafe : Bool) : ConstructorVal := { name := name, levelParams := levelParams, type := type, induct := induct, cidx := cidx, numParams := numParams, numFields := numFields, isUnsafe := isUnsafe } @[export lean_constructor_val_is_unsafe] def ConstructorVal.isUnsafeEx (v : ConstructorVal) : Bool := v.isUnsafe /-- Information for reducing a recursor -/ structure RecursorRule where /-- Reduction rule for this Constructor -/ ctor : Name /-- Number of fields (i.e., without counting inductive datatype parameters) -/ nfields : Nat /-- Right hand side of the reduction rule -/ rhs : Expr deriving Inhabited structure RecursorVal extends ConstantVal where /-- List of all inductive datatypes in the mutual declaration that generated this recursor -/ all : List Name /-- Number of parameters -/ numParams : Nat /-- Number of indices -/ numIndices : Nat /-- Number of motives -/ numMotives : Nat /-- Number of minor premises -/ numMinors : Nat /-- A reduction for each Constructor -/ rules : List RecursorRule /-- It supports K-like reduction. A recursor is said to support K-like reduction if one can assume it behaves like `Eq` under axiom `K` --- that is, it has one constructor, the constructor has 0 arguments, and it is an inductive predicate (ie, it lives in Prop). Examples of inductives with K-like reduction is `Eq`, `Acc`, and `And.intro`. Non-examples are `exists` (where the constructor has arguments) and `Or.intro` (which has multiple constructors). -/ k : Bool isUnsafe : Bool deriving Inhabited @[export lean_mk_recursor_val] def mkRecursorValEx (name : Name) (levelParams : List Name) (type : Expr) (all : List Name) (numParams numIndices numMotives numMinors : Nat) (rules : List RecursorRule) (k isUnsafe : Bool) : RecursorVal := { name := name, levelParams := levelParams, type := type, all := all, numParams := numParams, numIndices := numIndices, numMotives := numMotives, numMinors := numMinors, rules := rules, k := k, isUnsafe := isUnsafe } @[export lean_recursor_k] def RecursorVal.kEx (v : RecursorVal) : Bool := v.k @[export lean_recursor_is_unsafe] def RecursorVal.isUnsafeEx (v : RecursorVal) : Bool := v.isUnsafe def RecursorVal.getMajorIdx (v : RecursorVal) : Nat := v.numParams + v.numMotives + v.numMinors + v.numIndices def RecursorVal.getFirstIndexIdx (v : RecursorVal) : Nat := v.numParams + v.numMotives + v.numMinors def RecursorVal.getFirstMinorIdx (v : RecursorVal) : Nat := v.numParams + v.numMotives def RecursorVal.getInduct (v : RecursorVal) : Name := v.name.getPrefix inductive QuotKind where | type -- `Quot` | ctor -- `Quot.mk` | lift -- `Quot.lift` | ind -- `Quot.ind` deriving Inhabited structure QuotVal extends ConstantVal where kind : QuotKind deriving Inhabited @[export lean_mk_quot_val] def mkQuotValEx (name : Name) (levelParams : List Name) (type : Expr) (kind : QuotKind) : QuotVal := { name := name, levelParams := levelParams, type := type, kind := kind } @[export lean_quot_val_kind] def QuotVal.kindEx (v : QuotVal) : QuotKind := v.kind /-- Information associated with constant declarations. -/ inductive ConstantInfo where | axiomInfo (val : AxiomVal) | defnInfo (val : DefinitionVal) | thmInfo (val : TheoremVal) | opaqueInfo (val : OpaqueVal) | quotInfo (val : QuotVal) | inductInfo (val : InductiveVal) | ctorInfo (val : ConstructorVal) | recInfo (val : RecursorVal) deriving Inhabited namespace ConstantInfo def toConstantVal : ConstantInfo → ConstantVal | defnInfo {toConstantVal := d, ..} => d | axiomInfo {toConstantVal := d, ..} => d | thmInfo {toConstantVal := d, ..} => d | opaqueInfo {toConstantVal := d, ..} => d | quotInfo {toConstantVal := d, ..} => d | inductInfo {toConstantVal := d, ..} => d | ctorInfo {toConstantVal := d, ..} => d | recInfo {toConstantVal := d, ..} => d def isUnsafe : ConstantInfo → Bool | defnInfo v => v.safety == .unsafe | axiomInfo v => v.isUnsafe | thmInfo _ => false | opaqueInfo v => v.isUnsafe | quotInfo _ => false | inductInfo v => v.isUnsafe | ctorInfo v => v.isUnsafe | recInfo v => v.isUnsafe def isPartial : ConstantInfo → Bool | defnInfo v => v.safety == .partial | _ => false def name (d : ConstantInfo) : Name := d.toConstantVal.name def levelParams (d : ConstantInfo) : List Name := d.toConstantVal.levelParams def numLevelParams (d : ConstantInfo) : Nat := d.levelParams.length def type (d : ConstantInfo) : Expr := d.toConstantVal.type def value? : ConstantInfo → Option Expr | defnInfo {value := r, ..} => some r | thmInfo {value := r, ..} => some r | _ => none def hasValue : ConstantInfo → Bool | defnInfo _ => true | thmInfo _ => true | _ => false def value! : ConstantInfo → Expr | defnInfo {value := r, ..} => r | thmInfo {value := r, ..} => r | _ => panic! "declaration with value expected" def hints : ConstantInfo → ReducibilityHints | defnInfo {hints := r, ..} => r | _ => ReducibilityHints.opaque def isCtor : ConstantInfo → Bool | ctorInfo _ => true | _ => false def isInductive : ConstantInfo → Bool | inductInfo _ => true | _ => false /-- List of all (including this one) declarations in the same mutual block. -/ def all : ConstantInfo → List Name | inductInfo val => val.all | defnInfo val => val.all | thmInfo val => val.all | opaqueInfo val => val.all | info => [info.name] end ConstantInfo def mkRecName (declName : Name) : Name := Name.mkStr declName "rec" end Lean
9ad94e79602ef3a6c830466e73db1be99a1a7666
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/filter/indicator_function.lean
896626cbca3634d57930a4d1623172578807f9f3
[]
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,700
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.indicator_function import Mathlib.order.filter.at_top_bot import Mathlib.PostPort universes u_1 u_3 u_2 namespace Mathlib /-! # Indicator function and filters Properties of indicator functions involving `=ᶠ` and `≤ᶠ`. ## Tags indicator, characteristic, filter -/ theorem indicator_eventually_eq {α : Type u_1} {M : Type u_3} [HasZero M] {s : set α} {t : set α} {f : α → M} {g : α → M} {l : filter α} (hf : filter.eventually_eq (l ⊓ filter.principal s) f g) (hs : filter.eventually_eq l s t) : filter.eventually_eq l (set.indicator s f) (set.indicator t g) := sorry theorem indicator_union_eventually_eq {α : Type u_1} {M : Type u_3} [add_monoid M] {s : set α} {t : set α} {f : α → M} {l : filter α} (h : filter.eventually (fun (a : α) => ¬a ∈ s ∩ t) l) : filter.eventually_eq l (set.indicator (s ∪ t) f) (set.indicator s f + set.indicator t f) := filter.eventually.mono h fun (a : α) (ha : ¬a ∈ s ∩ t) => set.indicator_union_of_not_mem_inter ha f theorem indicator_eventually_le_indicator {α : Type u_1} {β : Type u_2} [HasZero β] [preorder β] {s : set α} {f : α → β} {g : α → β} {l : filter α} (h : filter.eventually_le (l ⊓ filter.principal s) f g) : filter.eventually_le l (set.indicator s f) (set.indicator s g) := filter.eventually.mono (iff.mp filter.eventually_inf_principal h) fun (a : α) (h : a ∈ s → f a ≤ g a) => set.indicator_rel_indicator (le_refl 0) h theorem tendsto_indicator_of_monotone {α : Type u_1} {β : Type u_2} {ι : Type u_3} [preorder ι] [HasZero β] (s : ι → set α) (hs : monotone s) (f : α → β) (a : α) : filter.tendsto (fun (i : ι) => set.indicator (s i) f a) filter.at_top (pure (set.indicator (set.Union fun (i : ι) => s i) f a)) := sorry theorem tendsto_indicator_of_antimono {α : Type u_1} {β : Type u_2} {ι : Type u_3} [preorder ι] [HasZero β] (s : ι → set α) (hs : ∀ {i j : ι}, i ≤ j → s j ⊆ s i) (f : α → β) (a : α) : filter.tendsto (fun (i : ι) => set.indicator (s i) f a) filter.at_top (pure (set.indicator (set.Inter fun (i : ι) => s i) f a)) := sorry theorem tendsto_indicator_bUnion_finset {α : Type u_1} {β : Type u_2} {ι : Type u_3} [HasZero β] (s : ι → set α) (f : α → β) (a : α) : filter.tendsto (fun (n : finset ι) => set.indicator (set.Union fun (i : ι) => set.Union fun (H : i ∈ n) => s i) f a) filter.at_top (pure (set.indicator (set.Union s) f a)) := sorry
37aab5755008e9539b73b42ae38b47f7636a3e09
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/tactic/split_ifs.lean
b9ebb090f29f4403287e0c1b4f0e97c51b44106e
[ "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
3,309
lean
/- Copyright (c) 2018 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner Tactic to split if-then-else-expressions. -/ import tactic.hint open expr tactic namespace tactic setup_tactic_parser meta def find_if_cond : expr → option expr | e := e.fold none $ λ e _ acc, acc <|> do c ← match e with | `(@ite _ %%c %%_ _ _) := some c | `(@dite _ %%c %%_ _ _) := some c | _ := none end, guard ¬c.has_var, find_if_cond c <|> return c meta def find_if_cond_at (at_ : loc) : tactic (option expr) := do lctx ← at_.get_locals, lctx ← lctx.mmap infer_type, tgt ← target, let es := if at_.include_goal then tgt::lctx else lctx, return $ find_if_cond $ es.foldr app (default expr) run_cmd mk_simp_attr `split_if_reduction run_cmd add_doc_string `simp_attr.split_if_reduction "Simp set for if-then-else statements" attribute [split_if_reduction] if_pos if_neg dif_pos dif_neg meta def reduce_ifs_at (at_ : loc) : tactic unit := do sls ← get_user_simp_lemmas `split_if_reduction, let cfg : simp_config := { fail_if_unchanged := ff }, let discharger := assumption <|> (applyc `not_not_intro >> assumption), hs ← at_.get_locals, hs.mmap' (λ h, simp_hyp sls [] h cfg discharger >> skip), when at_.include_goal (simp_target sls [] cfg discharger >> skip) meta def split_if1 (c : expr) (n : name) (at_ : loc) : tactic unit := by_cases c n; reduce_ifs_at at_ private meta def get_next_name (names : ref (list name)) : tactic name := do ns ← read_ref names, match ns with | [] := get_unused_name `h | n::ns := do write_ref names ns, return n end private meta def value_known (c : expr) : tactic bool := do lctx ← local_context, lctx ← lctx.mmap infer_type, return $ c ∈ lctx ∨ `(¬%%c) ∈ lctx private meta def split_ifs_core (at_ : loc) (names : ref (list name)) : list expr → tactic unit | done := do some cond ← find_if_cond_at at_ | fail "no if-then-else expressions to split", let cond := match cond with `(¬%%p) := p | p := p end, if cond ∈ done then skip else do no_split ← value_known cond, if no_split then reduce_ifs_at at_; try (split_ifs_core (cond :: done)) else do n ← get_next_name names, split_if1 cond n at_; try (split_ifs_core (cond :: done)) meta def split_ifs (names : list name) (at_ : loc := loc.ns [none]) := using_new_ref names $ λ names, split_ifs_core at_ names [] namespace interactive open interactive interactive.types expr lean.parser /-- Splits all if-then-else-expressions into multiple goals. Given a goal of the form `g (if p then x else y)`, `split_ifs` will produce two goals: `p ⊢ g x` and `¬p ⊢ g y`. If there are multiple ite-expressions, then `split_ifs` will split them all, starting with a top-most one whose condition does not contain another ite-expression. `split_ifs at *` splits all ite-expressions in all hypotheses as well as the goal. `split_ifs with h₁ h₂ h₃` overrides the default names for the hypotheses. -/ meta def split_ifs (at_ : parse location) (names : parse with_ident_list) : tactic unit := tactic.split_ifs names at_ add_hint_tactic "split_ifs" add_tactic_doc { name := "split_ifs", category := doc_category.tactic, decl_names := [``split_ifs], tags := ["case bashing"] } end interactive end tactic
f265a620b5ba83a90fdc0a540513c50f73a9ca85
1a61aba1b67cddccce19532a9596efe44be4285f
/library/init/sigma.lean
975cebfb393041bd95734abba1d1ef228911f095
[ "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
3,171
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Floris van Doorn -/ prelude import init.datatypes init.num init.wf init.logic init.tactic definition dpair := @sigma.mk notation `Σ` binders `,` r:(scoped P, sigma P) := r -- notation for n-ary tuples; input ⟨ ⟩ as \< \> notation `⟨`:max t:(foldr `,` (e r, sigma.mk e r)) `⟩`:0 := t lemma ex_of_sig {A : Type} {P : A → Prop} : (Σ x, P x) → ∃ x, P x := assume h, obtain x hx, from h, exists.intro x hx namespace sigma notation `pr₁` := pr1 notation `pr₂` := pr2 namespace ops postfix `.1`:(max+1) := pr1 postfix `.2`:(max+1) := pr2 end ops open ops well_founded section variables {A : Type} {B : A → Type} variable (Ra : A → A → Prop) variable (Rb : ∀ a, B a → B a → Prop) theorem dpair_eq : ∀ {a₁ a₂ : A} {b₁ : B a₁} {b₂ : B a₂} (H₁ : a₁ = a₂), eq.rec_on H₁ b₁ = b₂ → ⟨a₁, b₁⟩ = ⟨a₂, b₂⟩ | a₁ a₁ b₁ b₁ (eq.refl a₁) (eq.refl b₁) := rfl protected theorem eq {p₁ p₂ : Σa : A, B a} : ∀(H₁ : p₁.1 = p₂.1) (H₂ : eq.rec_on H₁ p₁.2 = p₂.2), p₁ = p₂ := destruct p₁ (take a₁ b₁, destruct p₂ (take a₂ b₂ H₁ H₂, dpair_eq H₁ H₂)) -- Lexicographical order based on Ra and Rb inductive lex : sigma B → sigma B → Prop := | left : ∀{a₁ b₁} a₂ b₂, Ra a₁ a₂ → lex ⟨a₁, b₁⟩ ⟨a₂, b₂⟩ | right : ∀a {b₁ b₂}, Rb a b₁ b₂ → lex ⟨a, b₁⟩ ⟨a, b₂⟩ end section parameters {A : Type} {B : A → Type} parameters {Ra : A → A → Prop} {Rb : Π a : A, B a → B a → Prop} local infix `≺`:50 := lex Ra Rb definition lex.accessible {a} (aca : acc Ra a) (acb : ∀a, well_founded (Rb a)) : ∀ (b : B a), acc (lex Ra Rb) ⟨a, b⟩ := acc.rec_on aca (λxa aca (iHa : ∀y, Ra y xa → ∀b : B y, acc (lex Ra Rb) ⟨y, b⟩), λb : B xa, acc.rec_on (acb xa b) (λxb acb (iHb : ∀ (y : B xa), Rb xa y xb → acc (lex Ra Rb) ⟨xa, y⟩), acc.intro ⟨xa, xb⟩ (λp (lt : p ≺ ⟨xa, xb⟩), have aux : xa = xa → xb == xb → acc (lex Ra Rb) p, from @sigma.lex.rec_on A B Ra Rb (λp₁ p₂, p₂.1 = xa → p₂.2 == xb → acc (lex Ra Rb) p₁) p ⟨xa, xb⟩ lt (λ (a₁ : A) (b₁ : B a₁) (a₂ : A) (b₂ : B a₂) (H : Ra a₁ a₂) (eq₂ : a₂ = xa) (eq₃ : b₂ == xb), begin cases eq₂, exact (iHa a₁ H b₁) end) (λ (a : A) (b₁ b₂ : B a) (H : Rb a b₁ b₂) (eq₂ : a = xa) (eq₃ : b₂ == xb), begin cases eq₂, cases eq₃, exact (iHb b₁ H) end), aux rfl !heq.refl))) -- The lexicographical order of well founded relations is well-founded definition lex.wf (Ha : well_founded Ra) (Hb : ∀ x, well_founded (Rb x)) : well_founded (lex Ra Rb) := well_founded.intro (λp, destruct p (λa b, lex.accessible (Ha a) Hb b)) end end sigma
be0a0baad8e5f9a26ca2f562955e4e40d8845761
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/analysis/calculus/formal_multilinear_series.lean
91bee53d1f31bf4ed41cc44e17082477181a6907
[ "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,016
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.normed_space.multilinear import ring_theory.power_series.basic /-! # Formal multilinear series In this file we define `formal_multilinear_series 𝕜 E F` to be a family of `n`-multilinear maps for all `n`, designed to model the sequence of derivatives of a function. In other files we use this notion to define `C^n` functions (called `times_cont_diff` in `mathlib`) and analytic functions. ## Notations We use the notation `E [×n]→L[𝕜] F` for the space of continuous multilinear maps on `E^n` with values in `F`. This is the space in which the `n`-th derivative of a function from `E` to `F` lives. ## Tags multilinear, formal series -/ noncomputable theory open set fin open_locale topological_space variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] /-- A formal multilinear series over a field `𝕜`, from `E` to `F`, is given by a family of multilinear maps from `E^n` to `F` for all `n`. -/ @[derive add_comm_group] def formal_multilinear_series (𝕜 : Type*) [nondiscrete_normed_field 𝕜] (E : Type*) [normed_group E] [normed_space 𝕜 E] (F : Type*) [normed_group F] [normed_space 𝕜 F] := Π (n : ℕ), (E [×n]→L[𝕜] F) instance : inhabited (formal_multilinear_series 𝕜 E F) := ⟨0⟩ section module /- `derive` is not able to find the module structure, probably because Lean is confused by the dependent types. We register it explicitly. -/ local attribute [reducible] formal_multilinear_series instance : module 𝕜 (formal_multilinear_series 𝕜 E F) := begin letI : ∀ n, module 𝕜 (continuous_multilinear_map 𝕜 (λ (i : fin n), E) F) := λ n, by apply_instance, apply_instance end end module namespace formal_multilinear_series variables (p : formal_multilinear_series 𝕜 E F) /-- Forgetting the zeroth term in a formal multilinear series, and interpreting the following terms as multilinear maps into `E →L[𝕜] F`. If `p` corresponds to the Taylor series of a function, then `p.shift` is the Taylor series of the derivative of the function. -/ def shift : formal_multilinear_series 𝕜 E (E →L[𝕜] F) := λn, (p n.succ).curry_right /-- Adding a zeroth term to a formal multilinear series taking values in `E →L[𝕜] F`. This corresponds to starting from a Taylor series for the derivative of a function, and building a Taylor series for the function itself. -/ def unshift (q : formal_multilinear_series 𝕜 E (E →L[𝕜] F)) (z : F) : formal_multilinear_series 𝕜 E F | 0 := (continuous_multilinear_curry_fin0 𝕜 E F).symm z | (n + 1) := continuous_multilinear_curry_right_equiv' 𝕜 n E F (q n) /-- Killing the zeroth coefficient in a formal multilinear series -/ def remove_zero (p : formal_multilinear_series 𝕜 E F) : formal_multilinear_series 𝕜 E F | 0 := 0 | (n + 1) := p (n + 1) @[simp] lemma remove_zero_coeff_zero : p.remove_zero 0 = 0 := rfl @[simp] lemma remove_zero_coeff_succ (n : ℕ) : p.remove_zero (n+1) = p (n+1) := rfl lemma remove_zero_of_pos {n : ℕ} (h : 0 < n) : p.remove_zero n = p n := by { rw ← nat.succ_pred_eq_of_pos h, refl } /-- Convenience congruence lemma stating in a dependent setting that, if the arguments to a formal multilinear series are equal, then the values are also equal. -/ lemma congr (p : formal_multilinear_series 𝕜 E F) {m n : ℕ} {v : fin m → E} {w : fin n → E} (h1 : m = n) (h2 : ∀ (i : ℕ) (him : i < m) (hin : i < n), v ⟨i, him⟩ = w ⟨i, hin⟩) : p m v = p n w := by { cases h1, congr' with ⟨i, hi⟩, exact h2 i hi hi } /-- Composing each term `pₙ` in a formal multilinear series with `(u, ..., u)` where `u` is a fixed continuous linear map, gives a new formal multilinear series `p.comp_continuous_linear_map u`. -/ def comp_continuous_linear_map (p : formal_multilinear_series 𝕜 F G) (u : E →L[𝕜] F) : formal_multilinear_series 𝕜 E G := λ n, (p n).comp_continuous_linear_map (λ (i : fin n), u) @[simp] lemma comp_continuous_linear_map_apply (p : formal_multilinear_series 𝕜 F G) (u : E →L[𝕜] F) (n : ℕ) (v : fin n → E) : (p.comp_continuous_linear_map u) n v = p n (u ∘ v) := rfl variables (𝕜) {𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] variables [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] variables [normed_space 𝕜' F] [is_scalar_tower 𝕜 𝕜' F] /-- Reinterpret a formal `𝕜'`-multilinear series as a formal `𝕜`-multilinear series, where `𝕜'` is a normed algebra over `𝕜`. -/ @[simp] protected def restrict_scalars (p : formal_multilinear_series 𝕜' E F) : formal_multilinear_series 𝕜 E F := λ n, (p n).restrict_scalars 𝕜 end formal_multilinear_series
c0cdbd27028c64b9ef4ff74867f79297f3260c5d
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/measure_theory/measure_space_def.lean
d95f64c8e1256e4cd7093ca6c2cd17165d46849d
[ "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
18,111
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import measure_theory.outer_measure import order.filter.countable_Inter import data.set.accumulate /-! # Measure spaces This file defines measure spaces, the almost-everywhere filter and ae_measurable functions. See `measure_theory.measure_space` for their properties and for extended documentation. Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the extended nonnegative reals that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint sets is equal to the measure of the individual sets. Every measure can be canonically extended to an outer measure, so that it assigns values to all subsets, not just the measurable subsets. On the other hand, a measure that is countably additive on measurable sets can be restricted to measurable sets to obtain a measure. In this file a measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ℝ≥0∞`. ## Implementation notes Given `μ : measure α`, `μ s` is the value of the *outer measure* applied to `s`. This conveniently allows us to apply the measure to sets without proving that they are measurable. We get countable subadditivity for all sets, but only countable additivity for measurable sets. See the documentation of `measure_theory.measure_space` for ways to construct measures and proving that two measure are equal. A `measure_space` is a class that is a measurable space with a canonical measure. The measure is denoted `volume`. This file does not import `measure_theory.measurable_space`, but only `measurable_space_def`. ## References * <https://en.wikipedia.org/wiki/Measure_(mathematics)> * <https://en.wikipedia.org/wiki/Almost_everywhere> ## Tags measure, almost everywhere, measure space -/ noncomputable theory open classical set filter (hiding map) function measurable_space open_locale classical topological_space big_operators filter ennreal nnreal variables {α β γ δ ι : Type*} namespace measure_theory /-- A measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. -/ structure measure (α : Type*) [measurable_space α] extends outer_measure α := (m_Union ⦃f : ℕ → set α⦄ : (∀ i, measurable_set (f i)) → pairwise (disjoint on f) → measure_of (⋃ i, f i) = ∑' i, measure_of (f i)) (trimmed : to_outer_measure.trim = to_outer_measure) /-- Measure projections for a measure space. For measurable sets this returns the measure assigned by the `measure_of` field in `measure`. But we can extend this to _all_ sets, but using the outer measure. This gives us monotonicity and subadditivity for all sets. -/ instance measure.has_coe_to_fun [measurable_space α] : has_coe_to_fun (measure α) := ⟨λ _, set α → ℝ≥0∞, λ m, m.to_outer_measure⟩ section variables [measurable_space α] {μ μ₁ μ₂ : measure α} {s s₁ s₂ t : set α} namespace measure /-! ### General facts about measures -/ /-- Obtain a measure by giving a countably additive function that sends `∅` to `0`. -/ def of_measurable (m : Π (s : set α), measurable_set s → ℝ≥0∞) (m0 : m ∅ measurable_set.empty = 0) (mU : ∀ {{f : ℕ → set α}} (h : ∀ i, measurable_set (f i)), pairwise (disjoint on f) → m (⋃ i, f i) (measurable_set.Union h) = ∑' i, m (f i) (h i)) : measure α := { m_Union := λ f hf hd, show induced_outer_measure m _ m0 (Union f) = ∑' i, induced_outer_measure m _ m0 (f i), begin rw [induced_outer_measure_eq m0 mU, mU hf hd], congr, funext n, rw induced_outer_measure_eq m0 mU end, trimmed := show (induced_outer_measure m _ m0).trim = induced_outer_measure m _ m0, begin unfold outer_measure.trim, congr, funext s hs, exact induced_outer_measure_eq m0 mU hs end, ..induced_outer_measure m _ m0 } lemma of_measurable_apply {m : Π (s : set α), measurable_set s → ℝ≥0∞} {m0 : m ∅ measurable_set.empty = 0} {mU : ∀ {{f : ℕ → set α}} (h : ∀ i, measurable_set (f i)), pairwise (disjoint on f) → m (⋃ i, f i) (measurable_set.Union h) = ∑' i, m (f i) (h i)} (s : set α) (hs : measurable_set s) : of_measurable m m0 mU s = m s hs := induced_outer_measure_eq m0 mU hs lemma to_outer_measure_injective : injective (to_outer_measure : measure α → outer_measure α) := λ ⟨m₁, u₁, h₁⟩ ⟨m₂, u₂, h₂⟩ h, by { congr, exact h } @[ext] lemma ext (h : ∀ s, measurable_set s → μ₁ s = μ₂ s) : μ₁ = μ₂ := to_outer_measure_injective $ by rw [← trimmed, outer_measure.trim_congr h, trimmed] lemma ext_iff : μ₁ = μ₂ ↔ ∀ s, measurable_set s → μ₁ s = μ₂ s := ⟨by { rintro rfl s hs, refl }, measure.ext⟩ end measure @[simp] lemma coe_to_outer_measure : ⇑μ.to_outer_measure = μ := rfl lemma to_outer_measure_apply (s : set α) : μ.to_outer_measure s = μ s := rfl lemma measure_eq_trim (s : set α) : μ s = μ.to_outer_measure.trim s := by rw μ.trimmed; refl lemma measure_eq_infi (s : set α) : μ s = ⨅ t (st : s ⊆ t) (ht : measurable_set t), μ t := by rw [measure_eq_trim, outer_measure.trim_eq_infi]; refl /-- A variant of `measure_eq_infi` which has a single `infi`. This is useful when applying a lemma next that only works for non-empty infima, in which case you can use `nonempty_measurable_superset`. -/ lemma measure_eq_infi' (μ : measure α) (s : set α) : μ s = ⨅ t : { t // s ⊆ t ∧ measurable_set t}, μ t := by simp_rw [infi_subtype, infi_and, subtype.coe_mk, ← measure_eq_infi] lemma measure_eq_induced_outer_measure : μ s = induced_outer_measure (λ s _, μ s) measurable_set.empty μ.empty s := measure_eq_trim _ lemma to_outer_measure_eq_induced_outer_measure : μ.to_outer_measure = induced_outer_measure (λ s _, μ s) measurable_set.empty μ.empty := μ.trimmed.symm lemma measure_eq_extend (hs : measurable_set s) : μ s = extend (λ t (ht : measurable_set t), μ t) s := by { rw [measure_eq_induced_outer_measure, induced_outer_measure_eq_extend _ _ hs], exact μ.m_Union } @[simp] lemma measure_empty : μ ∅ = 0 := μ.empty lemma nonempty_of_measure_ne_zero (h : μ s ≠ 0) : s.nonempty := ne_empty_iff_nonempty.1 $ λ h', h $ h'.symm ▸ measure_empty lemma measure_mono (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := μ.mono h lemma measure_mono_null (h : s₁ ⊆ s₂) (h₂ : μ s₂ = 0) : μ s₁ = 0 := nonpos_iff_eq_zero.1 $ h₂ ▸ measure_mono h lemma measure_mono_top (h : s₁ ⊆ s₂) (h₁ : μ s₁ = ∞) : μ s₂ = ∞ := top_unique $ h₁ ▸ measure_mono h /-- For every set there exists a measurable superset of the same measure. -/ lemma exists_measurable_superset (μ : measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = μ s := by simpa only [← measure_eq_trim] using μ.to_outer_measure.exists_measurable_superset_eq_trim s /-- For every set `s` and a countable collection of measures `μ i` there exists a measurable superset `t ⊇ s` such that each measure `μ i` takes the same value on `s` and `t`. -/ lemma exists_measurable_superset_forall_eq {ι} [encodable ι] (μ : ι → measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ ∀ i, μ i t = μ i s := by simpa only [← measure_eq_trim] using outer_measure.exists_measurable_superset_forall_eq_trim (λ i, (μ i).to_outer_measure) s /-- A measurable set `t ⊇ s` such that `μ t = μ s`. -/ def to_measurable (μ : measure α) (s : set α) : set α := classical.some (exists_measurable_superset μ s) lemma subset_to_measurable (μ : measure α) (s : set α) : s ⊆ to_measurable μ s := (classical.some_spec (exists_measurable_superset μ s)).1 @[simp] lemma measurable_set_to_measurable (μ : measure α) (s : set α) : measurable_set (to_measurable μ s) := (classical.some_spec (exists_measurable_superset μ s)).2.1 @[simp] lemma measure_to_measurable (s : set α) : μ (to_measurable μ s) = μ s := (classical.some_spec (exists_measurable_superset μ s)).2.2 lemma exists_measurable_superset_of_null (h : μ s = 0) : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = 0 := outer_measure.exists_measurable_superset_of_trim_eq_zero (by rw [← measure_eq_trim, h]) lemma exists_measurable_superset_iff_measure_eq_zero : (∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = 0) ↔ μ s = 0 := ⟨λ ⟨t, hst, _, ht⟩, measure_mono_null hst ht, exists_measurable_superset_of_null⟩ theorem measure_Union_le [encodable β] (s : β → set α) : μ (⋃ i, s i) ≤ ∑' i, μ (s i) := μ.to_outer_measure.Union _ lemma measure_bUnion_le {s : set β} (hs : countable s) (f : β → set α) : μ (⋃ b ∈ s, f b) ≤ ∑' p : s, μ (f p) := begin haveI := hs.to_encodable, rw [bUnion_eq_Union], apply measure_Union_le end lemma measure_bUnion_finset_le (s : finset β) (f : β → set α) : μ (⋃ b ∈ s, f b) ≤ ∑ p in s, μ (f p) := begin rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype], exact measure_bUnion_le s.countable_to_set f end lemma measure_bUnion_lt_top {s : set β} {f : β → set α} (hs : finite s) (hfin : ∀ i ∈ s, μ (f i) < ∞) : μ (⋃ i ∈ s, f i) < ∞ := begin convert (measure_bUnion_finset_le hs.to_finset f).trans_lt _, { ext, rw [finite.mem_to_finset] }, apply ennreal.sum_lt_top, simpa only [finite.mem_to_finset] end lemma measure_Union_null [encodable β] {s : β → set α} : (∀ i, μ (s i) = 0) → μ (⋃ i, s i) = 0 := μ.to_outer_measure.Union_null lemma measure_Union_null_iff [encodable ι] {s : ι → set α} : μ (⋃ i, s i) = 0 ↔ ∀ i, μ (s i) = 0 := ⟨λ h i, measure_mono_null (subset_Union _ _) h, measure_Union_null⟩ lemma measure_bUnion_null_iff {s : set ι} (hs : countable s) {t : ι → set α} : μ (⋃ i ∈ s, t i) = 0 ↔ ∀ i ∈ s, μ (t i) = 0 := by { haveI := hs.to_encodable, rw [← Union_subtype, measure_Union_null_iff, set_coe.forall], refl } theorem measure_union_le (s₁ s₂ : set α) : μ (s₁ ∪ s₂) ≤ μ s₁ + μ s₂ := μ.to_outer_measure.union _ _ lemma measure_union_null : μ s₁ = 0 → μ s₂ = 0 → μ (s₁ ∪ s₂) = 0 := μ.to_outer_measure.union_null lemma measure_union_null_iff : μ (s₁ ∪ s₂) = 0 ↔ μ s₁ = 0 ∧ μ s₂ = 0:= ⟨λ h, ⟨measure_mono_null (subset_union_left _ _) h, measure_mono_null (subset_union_right _ _) h⟩, λ h, measure_union_null h.1 h.2⟩ /-! ### The almost everywhere filter -/ /-- The “almost everywhere” filter of co-null sets. -/ def measure.ae (μ : measure α) : filter α := { sets := {s | μ sᶜ = 0}, univ_sets := by simp, inter_sets := λ s t hs ht, by simp only [compl_inter, mem_set_of_eq]; exact measure_union_null hs ht, sets_of_superset := λ s t hs hst, measure_mono_null (set.compl_subset_compl.2 hst) hs } notation `∀ᵐ` binders ` ∂` μ `, ` r:(scoped P, filter.eventually P (measure.ae μ)) := r notation `∃ᵐ` binders ` ∂` μ `, ` r:(scoped P, filter.frequently P (measure.ae μ)) := r notation f ` =ᵐ[`:50 μ:50 `] `:0 g:50 := f =ᶠ[measure.ae μ] g notation f ` ≤ᵐ[`:50 μ:50 `] `:0 g:50 := f ≤ᶠ[measure.ae μ] g lemma mem_ae_iff {s : set α} : s ∈ μ.ae ↔ μ sᶜ = 0 := iff.rfl lemma ae_iff {p : α → Prop} : (∀ᵐ a ∂ μ, p a) ↔ μ { a | ¬ p a } = 0 := iff.rfl lemma compl_mem_ae_iff {s : set α} : sᶜ ∈ μ.ae ↔ μ s = 0 := by simp only [mem_ae_iff, compl_compl] lemma frequently_ae_iff {p : α → Prop} : (∃ᵐ a ∂μ, p a) ↔ μ {a | p a} ≠ 0 := not_congr compl_mem_ae_iff lemma frequently_ae_mem_iff {s : set α} : (∃ᵐ a ∂μ, a ∈ s) ↔ μ s ≠ 0 := not_congr compl_mem_ae_iff lemma measure_zero_iff_ae_nmem {s : set α} : μ s = 0 ↔ ∀ᵐ a ∂ μ, a ∉ s := compl_mem_ae_iff.symm lemma ae_of_all {p : α → Prop} (μ : measure α) : (∀ a, p a) → ∀ᵐ a ∂ μ, p a := eventually_of_forall --instance ae_is_measurably_generated : is_measurably_generated μ.ae := --⟨λ s hs, let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs in -- ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩ instance : countable_Inter_filter μ.ae := ⟨begin intros S hSc hS, simp only [mem_ae_iff, compl_sInter, sUnion_image, bUnion_eq_Union] at hS ⊢, haveI := hSc.to_encodable, exact measure_Union_null (subtype.forall.2 hS) end⟩ lemma ae_imp_iff {p : α → Prop} {q : Prop} : (∀ᵐ x ∂μ, q → p x) ↔ (q → ∀ᵐ x ∂μ, p x) := filter.eventually_imp_distrib_left lemma ae_all_iff [encodable ι] {p : α → ι → Prop} : (∀ᵐ a ∂ μ, ∀ i, p a i) ↔ (∀ i, ∀ᵐ a ∂ μ, p a i) := eventually_countable_forall lemma ae_ball_iff {S : set ι} (hS : countable S) {p : Π (x : α) (i ∈ S), Prop} : (∀ᵐ x ∂ μ, ∀ i ∈ S, p x i ‹_›) ↔ ∀ i ∈ S, ∀ᵐ x ∂ μ, p x i ‹_› := eventually_countable_ball hS lemma ae_eq_refl (f : α → δ) : f =ᵐ[μ] f := eventually_eq.rfl lemma ae_eq_symm {f g : α → δ} (h : f =ᵐ[μ] g) : g =ᵐ[μ] f := h.symm lemma ae_eq_trans {f g h: α → δ} (h₁ : f =ᵐ[μ] g) (h₂ : g =ᵐ[μ] h) : f =ᵐ[μ] h := h₁.trans h₂ @[simp] lemma ae_eq_empty : s =ᵐ[μ] (∅ : set α) ↔ μ s = 0 := eventually_eq_empty.trans $ by simp [ae_iff] lemma ae_le_set : s ≤ᵐ[μ] t ↔ μ (s \ t) = 0 := calc s ≤ᵐ[μ] t ↔ ∀ᵐ x ∂μ, x ∈ s → x ∈ t : iff.rfl ... ↔ μ (s \ t) = 0 : by simp [ae_iff]; refl @[simp] lemma union_ae_eq_right : (s ∪ t : set α) =ᵐ[μ] t ↔ μ (s \ t) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set, union_diff_right, diff_eq_empty.2 (set.subset_union_right _ _)] lemma diff_ae_eq_self : (s \ t : set α) =ᵐ[μ] s ↔ μ (s ∩ t) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set, diff_diff_right, diff_diff, diff_eq_empty.2 (set.subset_union_right _ _)] lemma ae_eq_set {s t : set α} : s =ᵐ[μ] t ↔ μ (s \ t) = 0 ∧ μ (t \ s) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set] /-- If `s ⊆ t` modulo a set of measure `0`, then `μ s ≤ μ t`. -/ @[mono] lemma measure_mono_ae (H : s ≤ᵐ[μ] t) : μ s ≤ μ t := calc μ s ≤ μ (s ∪ t) : measure_mono $ subset_union_left s t ... = μ (t ∪ s \ t) : by rw [union_diff_self, set.union_comm] ... ≤ μ t + μ (s \ t) : measure_union_le _ _ ... = μ t : by rw [ae_le_set.1 H, add_zero] alias measure_mono_ae ← filter.eventually_le.measure_le /-- If two sets are equal modulo a set of measure zero, then `μ s = μ t`. -/ lemma measure_congr (H : s =ᵐ[μ] t) : μ s = μ t := le_antisymm H.le.measure_le H.symm.le.measure_le /-- A measure space is a measurable space equipped with a measure, referred to as `volume`. -/ class measure_space (α : Type*) extends measurable_space α := (volume : measure α) export measure_space (volume) /-- `volume` is the canonical measure on `α`. -/ add_decl_doc volume section measure_space notation `∀ᵐ` binders `, ` r:(scoped P, filter.eventually P (measure_theory.measure.ae measure_theory.measure_space.volume)) := r notation `∃ᵐ` binders `, ` r:(scoped P, filter.frequently P (measure_theory.measure.ae measure_theory.measure_space.volume)) := r /-- The tactic `exact volume`, to be used in optional (`auto_param`) arguments. -/ meta def volume_tac : tactic unit := `[exact measure_theory.measure_space.volume] end measure_space end end measure_theory section open measure_theory /-! # Almost everywhere measurable functions A function is almost everywhere measurable if it coincides almost everywhere with a measurable function. We define this property, called `ae_measurable f μ`. It's properties are discussed in `measure_theory.measure_space`. -/ variables [measurable_space α] [measurable_space β] {f g : α → β} {μ ν : measure α} /-- A function is almost everywhere measurable if it coincides almost everywhere with a measurable function. -/ def ae_measurable (f : α → β) (μ : measure α . measure_theory.volume_tac) : Prop := ∃ g : α → β, measurable g ∧ f =ᵐ[μ] g lemma measurable.ae_measurable (h : measurable f) : ae_measurable f μ := ⟨f, h, ae_eq_refl f⟩ namespace ae_measurable /-- Given an almost everywhere measurable function `f`, associate to it a measurable function that coincides with it almost everywhere. `f` is explicit in the definition to make sure that it shows in pretty-printing. -/ def mk (f : α → β) (h : ae_measurable f μ) : α → β := classical.some h lemma measurable_mk (h : ae_measurable f μ) : measurable (h.mk f) := (classical.some_spec h).1 lemma ae_eq_mk (h : ae_measurable f μ) : f =ᵐ[μ] (h.mk f) := (classical.some_spec h).2 lemma congr (hf : ae_measurable f μ) (h : f =ᵐ[μ] g) : ae_measurable g μ := ⟨hf.mk f, hf.measurable_mk, h.symm.trans hf.ae_eq_mk⟩ end ae_measurable lemma ae_measurable_congr (h : f =ᵐ[μ] g) : ae_measurable f μ ↔ ae_measurable g μ := ⟨λ hf, ae_measurable.congr hf h, λ hg, ae_measurable.congr hg h.symm⟩ @[simp] lemma ae_measurable_const {b : β} : ae_measurable (λ a : α, b) μ := measurable_const.ae_measurable lemma ae_measurable_id : ae_measurable id μ := measurable_id.ae_measurable lemma ae_measurable_id' : ae_measurable (λ x, x) μ := measurable_id.ae_measurable lemma measurable.comp_ae_measurable [measurable_space δ] {f : α → δ} {g : δ → β} (hg : measurable g) (hf : ae_measurable f μ) : ae_measurable (g ∘ f) μ := ⟨g ∘ hf.mk f, hg.comp hf.measurable_mk, eventually_eq.fun_comp hf.ae_eq_mk _⟩ end